SimpleTC7
前のバージョン SimpleTC6
チェックリストに対応
code:js
function timestampTool() {
const currentTime = new Date();
const formattedTime = currentTime.toTimeString().slice(0, 5); // "hh:mm"
// カーソル行の取得
const start, end = editor.getSelectedLineRange();
const currentLineText = editor.getTextInRange(start, end);
// チェックリスト判定とプレフィックス(- / - x など)
const checklistMatch = currentLineText.match(/^(-\s*\[( xX)\]\s*)/);
const checklistPrefix = checklistMatch ? checklistMatch0 : '';
const isChecklist = !!checklistMatch;
const isChecked = isChecklist ? /- \[xX\]/.test(checklistPrefix) : false;
// 既に「開始-終了」あり
const completedTaskRegex = /^(-\s*\[ xX\]\s*)?\d{2}:\d{2}-\d{2}:\d{2}/;
// 「開始のみ」あり(先頭、チェックリストはあってもなくてもOK)
const startTimeRegex = /^(-\s*\[ xX\]\s*)?(\d{2}:\d{2})(?!-)/;
let updatedLineText = currentLineText;
let cursorPosition = end;
if (completedTaskRegex.test(currentLineText)) {
// すでに開始-終了がある行は、新しい行を追加(現仕様踏襲)
const task = currentLineText.replace(completedTaskRegex, "").trim();
const newLineText = ${checklistPrefix}${task};
editor.replaceTextInRange(end, end, \n${newLineText});
cursorPosition = end + newLineText.length + 1;
} else if (startTimeRegex.test(currentLineText)) {
// 開始がある → 終了を付与
const match = currentLineText.match(startTimeRegex);
const prefixPart = match1 || ""; // 先頭のチェックリスト含む接頭辞
const startTime = match2; // 既存開始時刻
// 時刻部分を「開始-終了」に更新
updatedLineText = currentLineText.replace(startTimeRegex, ${prefixPart}${startTime}-${formattedTime});
// チェックリストなら完了状態に(- → - x)。既に x ならそのまま
if (isChecklist && !isChecked) {
updatedLineText = updatedLineText.replace(
/^(-\s*\[) xX(\]\s*)/,
(m, p1, p2) => ${p1}x${p2}
);
}
editor.replaceTextInRange(start, end, updatedLineText);
cursorPosition = start + updatedLineText.length;
} else {
// 開始がない → 追加(チェックリストなら記号の後に、通常行は行頭に)
if (isChecklist) {
updatedLineText = ${checklistPrefix}${formattedTime} ${currentLineText.replace(checklistPrefix, "")};
} else {
updatedLineText = ${formattedTime} ${currentLineText};
}
editor.replaceTextInRange(start, end, updatedLineText);
cursorPosition = start + updatedLineText.length;
}
editor.setSelectedRange(cursorPosition);
}
// 実行
timestampTool();