SimpleTC5
SimpleTC6
2025/04/24
変更点
完了タスクにて実行した際の挙動を「現在時刻を追加してコピー」から「現在時刻を追加せずにタスクのみコピー」に変更
機能
現在のカーソル行のタスクにタイムスタンプを追加・更新。
行頭に - (リスト記号)がある場合、それを保持したまま処理。
動作仕様
完了済みのタスク(hh:mm-hh:mm)がある場合: タスクをコピーし、次の行にタスクを新規追加。
開始時刻のみ(hh:mm)がある場合: hh:mm-現在時刻 に更新(終了時刻を追加)し、次の行を選択 (1Writerは次の行の先頭、Obsidianは次の行の末尾)。
タイムスタンプがない場合: hh:mm タスク の形式で追加(リスト記号を保持)。
Obsidian
SimpleTC.md
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 listPrefixMatch = currentLineText.match(/^(-\s*)?/);
const listPrefix = listPrefixMatch ? listPrefixMatch0 : '';
const completedTaskRegex = /^(-\s*)?(\d{2}:\d{2}-\d{2}:\d{2})/;
const startTimeRegex = /^(-\s*)?(\d{2}:\d{2})(?!-)/;
let updatedLineText;
let cursorPosition;
if (completedTaskRegex.test(currentLineText)) {
// 完了しているタスクなら、新しい行に新規タスクを作成
const task = currentLineText.replace(completedTaskRegex, '').trim();
const newLineText = ${listPrefix}${task};
editor.setLine(cursorLine, ${currentLineText}\n${newLineText});
cursorPosition = newLineText.length;
editor.setCursor({ line: cursorLine + 1, ch: cursorPosition });
} else if (startTimeRegex.test(currentLineText)) {
// 開始時間がある場合 → 終了時間を追加して、次のタスクにカーソルを移動
updatedLineText = currentLineText.replace(startTimeRegex, $1$2-${formattedTime});
editor.setLine(cursorLine, updatedLineText);
const nextLine = cursorLine + 1;
const totalLines = editor.lineCount();
if (nextLine < totalLines) {
const nextLineText = editor.getLine(nextLine);
cursorPosition = nextLineText.length;
editor.setCursor({ line: nextLine, ch: cursorPosition });
} else {
// 次の行がなければ、新しい行を作成してカーソルを移動
editor.replaceRange('\n', { line: cursorLine });
editor.setCursor({ line: nextLine, ch: 0 });
}
} else {
// 開始時間がない場合 → 行頭に開始時間を追加
updatedLineText = ${listPrefix}${formattedTime} ${currentLineText.replace(listPrefix, '')};
cursorPosition = updatedLineText.length;
editor.setLine(cursorLine, updatedLineText);
editor.setCursor({ line: cursorLine, ch: cursorPosition });
}
}
await timestampTool(tp);
%>
1Writer
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);
// リストの先頭記号を取得("- " の場合も考慮)
const listPrefixMatch = currentLineText.match(/^(-\s*)?/);
const listPrefix = listPrefixMatch ? listPrefixMatch0 : '';
// 正規表現で "hh:mm-"、"hh:mm-"、または "hh:mm-hh:mm" を探す
const completedTaskRegex = /^(-\s*)?(\d{2}:\d{2}-\d{2}:\d{2})/;
const startTimeRegex = /^(-\s*)?(\d{2}:\d{2})/;
let updatedLineText;
let cursorPosition;
if (completedTaskRegex.test(currentLineText)) {
// "hh:mm-hh:mm" が既にある場合、新しいタスクを次の行に追加
const task = currentLineText.replace(completedTaskRegex, '').trim();
const newLineText = ${listPrefix}${task};
editor.replaceTextInRange(end, end, \n${newLineText});
cursorPosition = end + newLineText.length + 1; // 追加行の行頭を選択
} else if (startTimeRegex.test(currentLineText)) {
// "hh:mm" が既にある場合、終了時刻を追加
updatedLineText = currentLineText.replace(startTimeRegex, $1$2-${formattedTime});
editor.replaceTextInRange(start, end, updatedLineText);
cursorPosition = start + updatedLineText.length; // 行末を選択
} else {
// "hh:mm" がない場合、"hh:mm" を行頭に追加(リスト記号を保持)
updatedLineText = ${listPrefix}${formattedTime} ${currentLineText.replace(listPrefix, '')};
editor.replaceTextInRange(start, end, updatedLineText);
cursorPosition = start + updatedLineText.length;
}
// カーソル位置を更新
editor.setSelectedRange(cursorPosition);
}
// 実行
timestampTool();