SimpleTC6
2025/08/12
code:js
<%*
async function timestampTool(tp) {
const currentTime = new Date();
const formattedTime = currentTime.toTimeString().slice(0, 5);
const editor = app.workspace.activeLeaf.view.editor;
const cursorLine = editor.getCursor().line;
const currentLineText = editor.getLine(cursorLine);
// 正規化(ダッシュ等の揺れ対策)
const norm = currentLineText.normalize("NFKC");
// 完了判定(元条件+追加条件)
const hasDoneMarkAtStart = /^\s*(?:-\s*\[xX\]\s*|✅✔️\s*)/.test(norm);
const hasTimeSpanAtStart = /^\s*(?:-\s*\\s\\s*|-\s*)?(?:\d{2}:\d{2})\s*-–—−\s*\d{2}:\d{2}\b/.test(norm);
// 開始のみ(末尾にダッシュが続かない)
// 先頭のインデント/箇条書き/開始時刻をそれぞれ捕捉
const startTimeRegex = /^(\s*)(-\s*\\s\\s*|-\s*)?(\d{2}:\d{2})(?!\s*-–—−)/;
let updatedLineText;
if (hasDoneMarkAtStart || hasTimeSpanAtStart) {
// 既に完了扱い:本文を抜き出して新規行(未完に戻す運用は従来どおり)
const body = currentLineText
.replace(/^\s*(?:-\s*\[xX\]\s*|✅✔️\s*)/, "")
.replace(/^\s*(?:-\s*\\s\\s*|-\s*)?(?:\d{2}:\d{2})\s*-–—−\s*\d{2}:\d{2}\s*/, "")
.trim();
// 先頭にチェックボックスがある場合は未完 (- ) にして複製
const indent = (currentLineText.match(/^\s*/) || "")0;
const hasBullet = /^\s*-\s*/.test(currentLineText);
const newPrefix = hasBullet ? ${indent}- [ ] : indent;
const newLineText = ${newPrefix}${body};
editor.setLine(cursorLine, ${currentLineText}\n${newLineText});
// ここは従来どおり次行を編集対象にする運用なら以下でOK。
// ご希望があればここも「行末」固定に変更できます。
editor.setCursor({ line: cursorLine + 1, ch: newLineText.length });
} else if (startTimeRegex.test(currentLineText)) {
// 開始時刻がある → 終了時刻を付与しつつ、箇条書きなら - x 化
updatedLineText = currentLineText.replace(startTimeRegex, (m, indent, bullet, hhmm) => {
const bulletCompleted = bullet ? "- x " : ""; // 箇条書きなら完了チェックに置換
return ${indent}${bulletCompleted}${hhmm}-${formattedTime};
});
editor.setLine(cursorLine, updatedLineText);
// ★ カーソルは当該行の末尾
editor.setCursor({ line: cursorLine, ch: updatedLineText.length });
} else {
// 開始時刻が無い → 行頭に開始時刻を追加(- や - があれば保持)
const listPrefixMatch = currentLineText.match(/^(\s*(?:-\s*\\s\\s*|-\s*)?)/);
const listPrefix = listPrefixMatch ? listPrefixMatch1 : "";
const body = currentLineText.slice(listPrefix.length);
updatedLineText = ${listPrefix}${formattedTime} ${body};
editor.setLine(cursorLine, updatedLineText);
// 行末にカーソル
editor.setCursor({ line: cursorLine, ch: updatedLineText.length });
}
}
await timestampTool(tp);
%>