From 2c19cccd8ad542445cdf6f05ec5a0dccc62258b2 Mon Sep 17 00:00:00 2001 From: Quorafind Date: Sun, 29 Nov 2026 12:07:37 +0800 Subject: [PATCH] fix(quick-capture): save desktop quick capture tasks with one checkbox The desktop quick capture window formats each line of its input as a task ("- [ ] text"), and WriteAPI.createTask / createTaskInDailyNote add "- [ ] " to the content again, so captured tasks were saved as "- [ ] - [ ] text". Split the first line's list marker and checkbox off before calling the write API; a checked box ("[x]") is saved as completed. Later lines of a multi-line capture keep their markers and stay separate tasks. --- src/__tests__/capturedTaskLine.test.ts | 56 ++++++++++++++++++++++++++ src/managers/electron-quick-capture.ts | 11 ++++- src/utils/task/captured-task-line.ts | 23 +++++++++++ 3 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 src/__tests__/capturedTaskLine.test.ts create mode 100644 src/utils/task/captured-task-line.ts diff --git a/src/__tests__/capturedTaskLine.test.ts b/src/__tests__/capturedTaskLine.test.ts new file mode 100644 index 00000000..5c3a5666 --- /dev/null +++ b/src/__tests__/capturedTaskLine.test.ts @@ -0,0 +1,56 @@ +/** + * The desktop quick capture window sends "- [ ] text"; the write API adds its + * own "- [ ] ", so the first line's marker must be split off first. + */ +import { splitCapturedTaskLine } from "@/utils/task/captured-task-line"; + +describe("splitCapturedTaskLine", () => { + it("strips the task marker the capture window adds", () => { + expect(splitCapturedTaskLine("- [ ] Buy milk")).toEqual({ + content: "Buy milk", + completed: false, + }); + }); + + it("leaves text without a marker unchanged", () => { + expect(splitCapturedTaskLine("Buy milk")).toEqual({ + content: "Buy milk", + completed: false, + }); + expect(splitCapturedTaskLine("-not a list item")).toEqual({ + content: "-not a list item", + completed: false, + }); + }); + + it("strips plain list markers and numbered items", () => { + expect(splitCapturedTaskLine("* Buy milk").content).toBe("Buy milk"); + expect(splitCapturedTaskLine("+ Buy milk").content).toBe("Buy milk"); + expect(splitCapturedTaskLine("1. Buy milk").content).toBe("Buy milk"); + expect(splitCapturedTaskLine("2) Buy milk").content).toBe("Buy milk"); + }); + + it("reports a checked box as completed", () => { + expect(splitCapturedTaskLine("- [x] Done already")).toEqual({ + content: "Done already", + completed: true, + }); + expect(splitCapturedTaskLine("- [X] Done already").completed).toBe(true); + expect(splitCapturedTaskLine("- [/] In progress")).toEqual({ + content: "In progress", + completed: false, + }); + }); + + it("only touches the first line of multi-line captures", () => { + expect(splitCapturedTaskLine("- [ ] First\n- [ ] Second")).toEqual({ + content: "First\n- [ ] Second", + completed: false, + }); + }); + + it("returns empty content for a bare marker", () => { + expect(splitCapturedTaskLine("- [ ] ").content).toBe(""); + expect(splitCapturedTaskLine("- [ ]").content).toBe(""); + }); +}); diff --git a/src/managers/electron-quick-capture.ts b/src/managers/electron-quick-capture.ts index 1af3b48a..e19a46d7 100644 --- a/src/managers/electron-quick-capture.ts +++ b/src/managers/electron-quick-capture.ts @@ -4,6 +4,7 @@ import { t } from "../translations/helper"; import { formatDate as formatDateSmart } from "@/utils/date/date-utils"; import type { Task } from "@/types/task"; import type { CreateTaskArgs } from "@/dataflow/api/WriteAPI"; +import { splitCapturedTaskLine } from "@/utils/task/captured-task-line"; import type { ElectronBrowserWindow, ElectronBrowserWindowStatic, @@ -350,9 +351,14 @@ export class ElectronQuickCapture { private async handleSaveTask(data: QuickCaptureSaveData): Promise { try { // Parse the task content and metadata - const { content, project, context, dueDate, priority, tags } = data; + const { project, context, dueDate, priority, tags } = data; + // The window already formats lines as tasks; the write API adds + // the first line's "- [ ] " itself. + const { content, completed } = splitCapturedTaskLine( + data.content?.trim() ?? "", + ); - if (!content?.trim()) { + if (!content.trim()) { void this.captureWindow?.webContents?.executeJavaScript(` showError('Task content cannot be empty'); `); @@ -363,6 +369,7 @@ export class ElectronQuickCapture { const taskArgs: CreateTaskArgs = { content: content.trim(), }; + if (completed) taskArgs.completed = true; // Add optional metadata if (project) taskArgs.project = project; diff --git a/src/utils/task/captured-task-line.ts b/src/utils/task/captured-task-line.ts new file mode 100644 index 00000000..2d8a4a78 --- /dev/null +++ b/src/utils/task/captured-task-line.ts @@ -0,0 +1,23 @@ +/** + * Split the list marker and checkbox off the first line of captured task text. + * + * The desktop quick capture window turns every line of its input into a task + * line ("- [ ] …"), but WriteAPI.createTask and createTaskInDailyNote add the + * "- [ ] " prefix themselves, so the saved task started with two checkboxes. + * Strip the first line's marker and let the write API add it. A checked box + * ("[x]") is reported as `completed`; other status marks fall back to an open + * task. Later lines are left alone and stay separate task lines. + */ +export function splitCapturedTaskLine(content: string): { + content: string; + completed: boolean; +} { + const match = /^\s*(?:[-*+]|\d+[.)])\s+(?:\[(.)\]\s*)?/.exec(content); + if (!match) { + return { content, completed: false }; + } + return { + content: content.slice(match[0].length), + completed: match[1] === "x" || match[1] === "X", + }; +}