1Writer SimpleTC
作成日: 2025/08/26
次のバージョン: 1Writer SimpleTC v2
修正箇所
終了タスクで実行した際、コピー後のタスクのチェックボックスが空にならない問題を修正
概要
開始時刻、見積時刻、実績時間が入った、タスクシュート的なログを取ることを補助する
仕様
- [ ] (m) 未実行タスク
- [ ] hh:mm (m) 実行中タスク
- [x] hh:mm-hh:mm (m) 完了タスク
括弧内は見積時間または実績時間、省略可
カーソル行のタスクで下記処理を実施
未実行タスク→開始時刻打刻
実行中タスク→終了時刻打刻+チェックを入れる
完了タスク→タスク名をコピーしチェックを外す
code:js
function timestampTool() {
const currentTime = new Date();
const formattedTime = currentTime.toTimeString().slice(0, 5); // "hh:mm"
// ★追加: 実績(分)を算出(翌日またぎ対応)
function diffMinutes(startStr, endStr) {
const sh, sm = startStr.split(":").map(Number);
const eh, em = endStr.split(":").map(Number);
let d = (eh * 60 + em) - (sh * 60 + sm);
if (d < 0) d += 24 * 60;
return d;
}
// カーソル行の取得
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 = - [ ] ${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; // 既存開始 hh:mm
const actualMin = diffMinutes(startTime, formattedTime);
// 開始時刻以降の本文を取得し、先頭の (数字) を除去して上書き
const afterStart = currentLineText.replace(/^(-\s*\[ xX\]\s*)?\d{2}:\d{2}\s*/, "");
const bodyClean = afterStart.replace(/^\(\d+\)\s*/, "");
// ベース文字列を再構成
updatedLineText = ${prefixPart}${startTime}-${formattedTime} (${actualMin}) ${bodyClean};
// チェックリストなら完了状態に(- → - 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();