「Markdown 向け、タスクリスト (* []) のチェック状態をトグル」の版間の差分

提供: MeryWiki
ナビゲーションに移動 検索に移動
Yuko (トーク | 投稿記録)
ページの作成:「Markdown のリスト記号をトグル (追加/削除) します。 複数行の選択状態で実行すると、複数行に適用できます。 <source lang="javas…」
 
Yuko (トーク | 投稿記録)
編集の要約なし
1行目: 1行目:
Markdown のリスト記号をトグル (追加/削除) します。
Markdown でのタスクリスト記号 (* [ ] / * [x]) のチェック状態をトグルします。
複数行の選択状態で実行すると、複数行に適用できます。
 
複数行選択及びマルチカーソル機能に対応しています。


<source lang="javascript">
<source lang="javascript">
#title = "リスト記号のトグル";
#title = 'Markdown タスクリストのチェックをトグル';
Redraw = false;
BeginUndoGroup();
BeginUndoGroup();


Redraw = false;
var sel = Document.Selection;


header_str = '*';
if (sel.Mode === meModeMulti) {
 
   doMultiAction(main);
// 選択状態ではない場合は、カーソル行のみを処理するようにする
if (Document.Selection.IsEmpty) {
  var empty_flg = true;
  // リスト文字の追加/削除を実行した後の位置合わせ用変数
  var margin = 0;
 
  var active_x = Document.Selection.GetActivePointX(mePosLogical);
   var active_y = Document.Selection.GetActivePointY(mePosLogical);
  var top_y = active_y;
  var bottom_y = active_y;
} else {
} else {
   var empty_flg = false;
   main();
  var top_y = Document.Selection.GetTopPointY(mePosLogical);
  var bottom_y = Document.Selection.GetBottomPointY(mePosLogical);
 
  // 選択範囲の末尾位置が行頭だった場合には、末尾行を対象外とする
  var bottom_x = Document.Selection.GetBottomPointX(mePosLogical);
  if (bottom_x == 1) {
    bottom_y--;
  }
}
}


for (var i = top_y; i <= bottom_y; i++) {
function main() {
   var y = i;
   if (Document.Selection.IsEmpty) {
  Document.Selection.SetActivePoint(mePosLogical, 1, y);
    var actX = sel.GetActivePointX(mePosLogical);
 
    var actY = sel.GetActivePointY(mePosLogical);
   // 現在行の内容を取得
    toggleCheck(actY);
  var s = Document.GetLine(y, meGetLineWithNewLines);
    sel.SetActivePoint(mePosLogical, actX, actY);
 
   } else {
  // 行頭の文字を取得
    var topX = Document.Selection.GetTopPointX(mePosLogical);
  var regexp = new RegExp('^(\\s*\\' + header_str + ' +)?', 'i');
    var bottomX = Document.Selection.GetBottomPointX(mePosLogical);
  var s_found = s.match(regexp);
    var topY = Document.Selection.GetTopPointY(mePosLogical);
  var s_header = '';
    var bottomY = Document.Selection.GetBottomPointY(mePosLogical);
  if (s_found) {
    var ignoreY = 0;
     var s_header = s_found[1];
    if (bottomX === 1) {
      ignoreY = bottomY;
     }
    for (var y = topY; y <= bottomY; y++) {
      if (y === ignoreY) {
        continue;
      }
      sel.SetActivePoint(mePosLogical, 1, y);
      toggleCheck(y);
    }
    Document.Selection.SetAnchorPoint(mePosLogical, topX, topY);
    Document.Selection.SetActivePoint(mePosLogical, bottomX, bottomY, true);
   }
   }
}


   // 行頭文字がない場合にはリスト記号を追加
function toggleCheck(y) {
   // ある場合にはリスト記号を削除
   var lineText = Document.GetLine(y, meGetLineWithNewLines);
   if (!s_header) {
  var checked = lineText.match(/^\s*[*-] \[x]/);
     if (empty_flg) {
   var unchecked = lineText.match(/^\s*[*-] \[ ]/);
      margin += 2;
   if (checked || unchecked) {
     }
     sel.SelectLine();
    Document.Selection.StartOfLine(false, mePosLogical);
     sel.Delete();
     Document.Selection.Text = header_str + ' ';
     if (checked) {
  } else {
      sel.Text = lineText.replace(/^(\s*[*-] )\[x]/, '$1[ ]');
    if (empty_flg) {
    } else {
       margin -= s_header.length;
       sel.Text = lineText.replace(/^(\s*[*-] )\[ ]/, '$1[x]');
     }
     }
    Document.Selection.SetAnchorPoint(mePosLogical, 1, y);
    Document.Selection.SetActivePoint(mePosLogical, s_header.length+1, y, true);
    Document.Selection.Delete();
   }
   }
}
}


if (empty_flg) {
function doMultiAction(fn) {
   Document.Selection.SetActivePoint(mePosLogical, active_x + margin, active_y, false)
   var d = document,
} else {
    s = d.selection;
  Document.Selection.SetAnchorPoint(mePosLogical, 1, top_y);
  s.Mode = meModeMulti;
   Document.Selection.SetActivePoint(mePosLogical, 1, bottom_y + 1, true)
  // ① まず、GetActivePos と GetAnchorPos で複数選択のリストを作成します
  var selections = [{s: s.GetAnchorPos(), e: s.GetActivePos()}];
  if (s.Count > 0) {
    selections = [];
    for (var i = 0; i < s.Count; i++)
      selections.push({s: s.GetActivePos(i), e: s.GetAnchorPos(i)});
  }
  var p = 0;
  var q = 0;
  for (var i = 0; i < selections.length; i++) {
    // ② そのリストを上から順に SetActivePos で、「シングルカーソル」で範囲選択します。この段階で複数選択は解除され、通常のマクロ操作が可能となります
    s.SetActivePos(selections[i].s + p);
    s.SetActivePos(selections[i].e + p, true);
    q = d.TextLength;
    // ③ <ここで通常のマクロの機能を使って普通に編集などを行います>
    fn();
    p += d.TextLength - q;
    // ④ 処理を行った後の選択範囲をリストに反映し、これをリストの最後まで繰り返します
    selections[i] = {s: s.GetAnchorPos(), e: s.GetActivePos()};
   }
  // ⑤ そのリストを上から順に document.selection.AddPos(StartPos, EndPos) で複数選択として復元します
  for (var i = 0; i < selections.length; i++)
    s.AddPos(selections[i].s, selections[i].e);
}
}
Redraw = true;
</source>
</source>

2020年9月4日 (金) 10:48時点における版

Markdown でのタスクリスト記号 (* [ ] / * [x]) のチェック状態をトグルします。

複数行選択及びマルチカーソル機能に対応しています。

#title = 'Markdown タスクリストのチェックをトグル';
Redraw = false;
BeginUndoGroup();

var sel = Document.Selection;

if (sel.Mode === meModeMulti) {
  doMultiAction(main);
} else {
  main();
}

function main() {
  if (Document.Selection.IsEmpty) {
    var actX = sel.GetActivePointX(mePosLogical);
    var actY = sel.GetActivePointY(mePosLogical);
    toggleCheck(actY);
    sel.SetActivePoint(mePosLogical, actX, actY);
  } else {
    var topX = Document.Selection.GetTopPointX(mePosLogical);
    var bottomX = Document.Selection.GetBottomPointX(mePosLogical);
    var topY = Document.Selection.GetTopPointY(mePosLogical);
    var bottomY = Document.Selection.GetBottomPointY(mePosLogical);
    var ignoreY = 0;
    if (bottomX === 1) {
      ignoreY = bottomY;
    }
    for (var y = topY; y <= bottomY; y++) {
      if (y === ignoreY) {
        continue;
      }
      sel.SetActivePoint(mePosLogical, 1, y);
      toggleCheck(y);
    }
    Document.Selection.SetAnchorPoint(mePosLogical, topX, topY);
    Document.Selection.SetActivePoint(mePosLogical, bottomX, bottomY, true);
  }
}

function toggleCheck(y) {
  var lineText = Document.GetLine(y, meGetLineWithNewLines);
  var checked = lineText.match(/^\s*[*-] \[x]/);
  var unchecked = lineText.match(/^\s*[*-] \[ ]/);
  if (checked || unchecked) {
    sel.SelectLine();
    sel.Delete();
    if (checked) {
      sel.Text = lineText.replace(/^(\s*[*-] )\[x]/, '$1[ ]');
    } else {
      sel.Text = lineText.replace(/^(\s*[*-] )\[ ]/, '$1[x]');
    }
  }
}

function doMultiAction(fn) {
  var d = document,
    s = d.selection;
  s.Mode = meModeMulti;
  // ① まず、GetActivePos と GetAnchorPos で複数選択のリストを作成します
  var selections = [{s: s.GetAnchorPos(), e: s.GetActivePos()}];
  if (s.Count > 0) {
    selections = [];
    for (var i = 0; i < s.Count; i++)
      selections.push({s: s.GetActivePos(i), e: s.GetAnchorPos(i)});
  }
  var p = 0;
  var q = 0;
  for (var i = 0; i < selections.length; i++) {
    // ② そのリストを上から順に SetActivePos で、「シングルカーソル」で範囲選択します。この段階で複数選択は解除され、通常のマクロ操作が可能となります
    s.SetActivePos(selections[i].s + p);
    s.SetActivePos(selections[i].e + p, true);
    q = d.TextLength;
    // ③ <ここで通常のマクロの機能を使って普通に編集などを行います>
    fn();
    p += d.TextLength - q;
    // ④ 処理を行った後の選択範囲をリストに反映し、これをリストの最後まで繰り返します
    selections[i] = {s: s.GetAnchorPos(), e: s.GetActivePos()};
  }
  // ⑤ そのリストを上から順に document.selection.AddPos(StartPos, EndPos) で複数選択として復元します
  for (var i = 0; i < selections.length; i++)
    s.AddPos(selections[i].s, selections[i].e);
}
スポンサーリンク