Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/__tests__/capturedTaskLine.test.ts
Original file line number Diff line number Diff line change
@@ -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("");
});
});
11 changes: 9 additions & 2 deletions src/managers/electron-quick-capture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -350,9 +351,14 @@ export class ElectronQuickCapture {
private async handleSaveTask(data: QuickCaptureSaveData): Promise<void> {
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');
`);
Expand All @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions src/utils/task/captured-task-line.ts
Original file line number Diff line number Diff line change
@@ -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",
};
}
Loading