@dinoxx/dinox-cli 2.18.5 → 2.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commands/notes/contentEngine.js +8 -4
- package/dist/commands/schema/renderSkills.js +3 -1
- package/dist/commands/todo/contentModel.d.ts +5 -3
- package/dist/commands/todo/contentModel.js +7 -5
- package/dist/commands/todo/dryRun.d.ts +9 -7
- package/dist/commands/todo/dryRun.js +99 -86
- package/dist/commands/todo/index.js +279 -93
- package/dist/commands/todo/repo.d.ts +128 -7
- package/dist/commands/todo/repo.js +474 -130
- package/dist/commands/todo/taskModel.d.ts +20 -1
- package/dist/commands/todo/taskModel.js +153 -20
- package/dist/commands/todo/taskPatch.d.ts +119 -0
- package/dist/commands/todo/taskPatch.js +502 -0
- package/dist/commands/todo/taskText.d.ts +26 -0
- package/dist/commands/todo/taskText.js +221 -0
- package/dist/dsh-plugin/lib/client.js +713 -289
- package/dist/dsh-plugin/lib/index.js +7 -2
- package/dist/dsh-plugin/package.json +1 -1
- package/dist/dsh-plugin/runtime.json +1 -1
- package/dist/editor/compat.d.ts +6 -0
- package/dist/editor/compat.js +18 -0
- package/dist/editor/daemon.js +27 -0
- package/dist/editor/mcp.js +99 -2
- package/dist/editor/service.d.ts +57 -1
- package/dist/editor/service.js +174 -7
- package/dist/editor/todos.d.ts +67 -0
- package/dist/editor/todos.js +293 -0
- package/dist/editor-ui/mcp-app.html +262 -253
- package/dist/integrations/dsh/host.js +10 -2
- package/dist/utils/tiptapMarkdown.js +15 -1
- package/dist/vendor/electron/shared/task-priority.d.ts +42 -0
- package/dist/vendor/electron/shared/task-priority.js +186 -0
- package/package.json +1 -1
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
// Id-stable task mutations on Tiptap `content_json`, ported from dinox-app
|
|
2
|
+
// (services/todo/{taskCheckedCascade,setTaskItemDueAt,patchNoteTasks,addTasks,resolveTodoDuePoint}).
|
|
3
|
+
//
|
|
4
|
+
// Every write edits the existing taskItem nodes in place, located by task id. Nothing here
|
|
5
|
+
// round-trips a note through Markdown: Markdown is only used to parse the inline text of a
|
|
6
|
+
// *new* task, and the taskItem node (with its uuidv7 id) is always built in code.
|
|
7
|
+
import { DinoxError } from '../../utils/errors.js';
|
|
8
|
+
import { generateId } from '../../utils/id.js';
|
|
9
|
+
import { convertMarkdownToNoteContent } from '../../utils/tiptapMarkdown.js';
|
|
10
|
+
import { normalizeTaskPriority, stripPriorityFromTaskContent, } from '../../vendor/electron/shared/task-priority.js';
|
|
11
|
+
import { buildLegacyTaskId } from './taskIdentity.js';
|
|
12
|
+
import { setTaskEditText, taskEditText, taskParagraph } from './taskText.js';
|
|
13
|
+
function isNode(value) {
|
|
14
|
+
return Boolean(value && typeof value === 'object' && !Array.isArray(value));
|
|
15
|
+
}
|
|
16
|
+
function normalizeChecked(value) {
|
|
17
|
+
if (value === true || value === 'true')
|
|
18
|
+
return true;
|
|
19
|
+
if (value === false || value === 'false')
|
|
20
|
+
return false;
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
function explicitTaskId(node) {
|
|
24
|
+
const own = typeof node.id === 'string' ? node.id.trim() : '';
|
|
25
|
+
if (own)
|
|
26
|
+
return own;
|
|
27
|
+
const attr = typeof node.attrs?.id === 'string' ? node.attrs.id.trim() : '';
|
|
28
|
+
return attr;
|
|
29
|
+
}
|
|
30
|
+
export function readTaskChecked(node) {
|
|
31
|
+
return normalizeChecked(node.checked) ?? normalizeChecked(node.attrs?.checked) ?? false;
|
|
32
|
+
}
|
|
33
|
+
function writeTaskChecked(node, checked) {
|
|
34
|
+
node.attrs = { ...(node.attrs ?? {}), checked };
|
|
35
|
+
// Legacy CLI nodes mirror id/checked at the top level; keep the mirror consistent.
|
|
36
|
+
if ('checked' in node)
|
|
37
|
+
node.checked = checked;
|
|
38
|
+
}
|
|
39
|
+
function cloneJson(value) {
|
|
40
|
+
return JSON.parse(JSON.stringify(value));
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Give every id-less taskItem its deterministic legacy id (both `id` and `attrs.id`, as the
|
|
44
|
+
* CLI always has), then index tasks by id. Duplicate ids keep the first occurrence, like the app.
|
|
45
|
+
*/
|
|
46
|
+
function indexTasks(doc, noteId) {
|
|
47
|
+
const byId = new Map();
|
|
48
|
+
const order = [];
|
|
49
|
+
let preorderIndex = 0;
|
|
50
|
+
let materialized = false;
|
|
51
|
+
const visit = (value, ancestors) => {
|
|
52
|
+
if (!isNode(value))
|
|
53
|
+
return;
|
|
54
|
+
let nextAncestors = ancestors;
|
|
55
|
+
if (value.type === 'taskItem') {
|
|
56
|
+
let taskId = explicitTaskId(value);
|
|
57
|
+
if (!taskId) {
|
|
58
|
+
taskId = buildLegacyTaskId(noteId, preorderIndex, value);
|
|
59
|
+
value.id = taskId;
|
|
60
|
+
value.attrs = { ...(value.attrs ?? {}), id: taskId };
|
|
61
|
+
materialized = true;
|
|
62
|
+
}
|
|
63
|
+
preorderIndex += 1;
|
|
64
|
+
order.push(taskId);
|
|
65
|
+
if (!byId.has(taskId))
|
|
66
|
+
byId.set(taskId, { node: value, ancestors });
|
|
67
|
+
nextAncestors = [...ancestors, value];
|
|
68
|
+
}
|
|
69
|
+
if (Array.isArray(value.content)) {
|
|
70
|
+
for (const child of value.content)
|
|
71
|
+
visit(child, nextAncestors);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
visit(doc, []);
|
|
75
|
+
return { byId, order, materialized };
|
|
76
|
+
}
|
|
77
|
+
function collectDescendantTasks(node, out) {
|
|
78
|
+
if (!Array.isArray(node.content))
|
|
79
|
+
return;
|
|
80
|
+
for (const child of node.content) {
|
|
81
|
+
if (!isNode(child))
|
|
82
|
+
continue;
|
|
83
|
+
if (child.type === 'taskItem')
|
|
84
|
+
out.push(child);
|
|
85
|
+
collectDescendantTasks(child, out);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
// ---- due time --------------------------------------------------------------------------
|
|
89
|
+
/** Paths to the task's own time nodes; nested task lists belong to child tasks. */
|
|
90
|
+
function collectTimePaths(node, path, paths) {
|
|
91
|
+
if (!Array.isArray(node.content))
|
|
92
|
+
return;
|
|
93
|
+
node.content.forEach((child, index) => {
|
|
94
|
+
if (!isNode(child) || child.type === 'taskList')
|
|
95
|
+
return;
|
|
96
|
+
const childPath = [...path, index];
|
|
97
|
+
if (child.type === 'time' || child.type === 'timeblock')
|
|
98
|
+
paths.push(childPath);
|
|
99
|
+
collectTimePaths(child, childPath, paths);
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
/** One time node is the due time; with a start and a due time, the second one is due. */
|
|
103
|
+
function duePath(taskItem) {
|
|
104
|
+
const paths = [];
|
|
105
|
+
collectTimePaths(taskItem, [], paths);
|
|
106
|
+
if (paths.length === 0)
|
|
107
|
+
return null;
|
|
108
|
+
return paths.length === 1 ? paths[0] : paths[1];
|
|
109
|
+
}
|
|
110
|
+
function nodeAt(root, path) {
|
|
111
|
+
let current = root;
|
|
112
|
+
for (const index of path) {
|
|
113
|
+
current = isNode(current) && Array.isArray(current.content) ? current.content[index] : undefined;
|
|
114
|
+
}
|
|
115
|
+
return isNode(current) ? current : null;
|
|
116
|
+
}
|
|
117
|
+
function readTimePoint(node) {
|
|
118
|
+
const iso = typeof node?.attrs?.iso === 'string' ? node.attrs.iso.trim() : '';
|
|
119
|
+
if (!iso)
|
|
120
|
+
return null;
|
|
121
|
+
const format = node?.attrs?.displayFormat;
|
|
122
|
+
const displayFormat = format === 'date' || format === 'datetime' || format === 'datetime-seconds' || format === 'time'
|
|
123
|
+
? format
|
|
124
|
+
: 'datetime';
|
|
125
|
+
return { iso, displayFormat };
|
|
126
|
+
}
|
|
127
|
+
export function readTaskDuePoint(taskItem) {
|
|
128
|
+
const path = duePath(taskItem);
|
|
129
|
+
return path ? readTimePoint(nodeAt(taskItem, path)) : null;
|
|
130
|
+
}
|
|
131
|
+
function isSpacerText(node) {
|
|
132
|
+
return isNode(node) && node.type === 'text' && typeof node.text === 'string' && node.text.trim() === '';
|
|
133
|
+
}
|
|
134
|
+
function removeAtPath(root, path) {
|
|
135
|
+
const parent = nodeAt(root, path.slice(0, -1)) ?? (path.length === 1 ? root : null);
|
|
136
|
+
const index = path[path.length - 1];
|
|
137
|
+
if (!parent || !Array.isArray(parent.content))
|
|
138
|
+
return;
|
|
139
|
+
// Drop the spacer that was inserted before the time node together with it.
|
|
140
|
+
const removeFrom = index > 0 && isSpacerText(parent.content[index - 1]) ? index - 1 : index;
|
|
141
|
+
parent.content.splice(removeFrom, index - removeFrom + 1);
|
|
142
|
+
}
|
|
143
|
+
function insertDueNode(taskItem, dueAt) {
|
|
144
|
+
const timeNode = { type: 'time', attrs: { iso: dueAt.iso, displayFormat: dueAt.displayFormat } };
|
|
145
|
+
if (!Array.isArray(taskItem.content))
|
|
146
|
+
taskItem.content = [];
|
|
147
|
+
const paragraph = taskItem.content.find((child) => isNode(child) && child.type === 'paragraph');
|
|
148
|
+
if (!paragraph) {
|
|
149
|
+
taskItem.content.push({ type: 'paragraph', content: [timeNode] });
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (!Array.isArray(paragraph.content))
|
|
153
|
+
paragraph.content = [];
|
|
154
|
+
const last = paragraph.content[paragraph.content.length - 1];
|
|
155
|
+
if (paragraph.content.length > 0) {
|
|
156
|
+
const lastText = isNode(last) && last.type === 'text' && typeof last.text === 'string' ? last.text : '';
|
|
157
|
+
if (!/\s$/.test(lastText))
|
|
158
|
+
paragraph.content.push({ type: 'text', text: ' ' });
|
|
159
|
+
}
|
|
160
|
+
paragraph.content.push(timeNode);
|
|
161
|
+
}
|
|
162
|
+
/** Returns true when the task's due time changed. */
|
|
163
|
+
function setTaskDueAt(taskItem, dueAt) {
|
|
164
|
+
const path = duePath(taskItem);
|
|
165
|
+
if (dueAt === null) {
|
|
166
|
+
if (!path)
|
|
167
|
+
return false;
|
|
168
|
+
removeAtPath(taskItem, path);
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
const current = path ? nodeAt(taskItem, path) : null;
|
|
172
|
+
if (!current) {
|
|
173
|
+
insertDueNode(taskItem, dueAt);
|
|
174
|
+
return true;
|
|
175
|
+
}
|
|
176
|
+
if (current.attrs?.iso === dueAt.iso && current.attrs?.displayFormat === dueAt.displayFormat)
|
|
177
|
+
return false;
|
|
178
|
+
current.attrs = { ...(current.attrs ?? {}), iso: dueAt.iso, displayFormat: dueAt.displayFormat };
|
|
179
|
+
return true;
|
|
180
|
+
}
|
|
181
|
+
// ---- patch application -----------------------------------------------------------------
|
|
182
|
+
/**
|
|
183
|
+
* Apply task patches to a document, editing only the targeted taskItem nodes.
|
|
184
|
+
*
|
|
185
|
+
* Checked cascade (same rules as the app): checking a task checks all of its descendants;
|
|
186
|
+
* unchecking a task also unchecks its checked ancestors; parents never auto-complete.
|
|
187
|
+
*/
|
|
188
|
+
export function applyTaskPatches(sourceDoc, noteId, patches) {
|
|
189
|
+
if (!isNode(sourceDoc) || sourceDoc.type !== 'doc') {
|
|
190
|
+
throw new DinoxError('Invalid note content_json: root.type must be "doc"');
|
|
191
|
+
}
|
|
192
|
+
const doc = cloneJson(sourceDoc);
|
|
193
|
+
const { byId, order } = indexTasks(doc, noteId);
|
|
194
|
+
const checkedChanges = new Map();
|
|
195
|
+
const results = [];
|
|
196
|
+
const missingTaskIds = [];
|
|
197
|
+
let changed = false;
|
|
198
|
+
for (const patch of patches) {
|
|
199
|
+
const indexed = byId.get(patch.taskId);
|
|
200
|
+
if (!indexed) {
|
|
201
|
+
missingTaskIds.push(patch.taskId);
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
const { node, ancestors } = indexed;
|
|
205
|
+
const checkedFrom = readTaskChecked(node);
|
|
206
|
+
const priorityFrom = normalizeTaskPriority(node.attrs?.priority);
|
|
207
|
+
const dueFrom = readTaskDuePoint(node);
|
|
208
|
+
const cascaded = [];
|
|
209
|
+
if (typeof patch.checked === 'boolean') {
|
|
210
|
+
const flip = (task, value, isSelf) => {
|
|
211
|
+
if (readTaskChecked(task) === value)
|
|
212
|
+
return;
|
|
213
|
+
writeTaskChecked(task, value);
|
|
214
|
+
const id = explicitTaskId(task);
|
|
215
|
+
if (id) {
|
|
216
|
+
checkedChanges.set(id, value);
|
|
217
|
+
if (!isSelf)
|
|
218
|
+
cascaded.push(id);
|
|
219
|
+
}
|
|
220
|
+
changed = true;
|
|
221
|
+
};
|
|
222
|
+
flip(node, patch.checked, true);
|
|
223
|
+
if (patch.cascade === false) {
|
|
224
|
+
// Exact restore: only this task.
|
|
225
|
+
}
|
|
226
|
+
else if (patch.checked) {
|
|
227
|
+
const descendants = [];
|
|
228
|
+
collectDescendantTasks(node, descendants);
|
|
229
|
+
for (const child of descendants)
|
|
230
|
+
flip(child, true, false);
|
|
231
|
+
}
|
|
232
|
+
else {
|
|
233
|
+
for (const ancestor of [...ancestors].reverse())
|
|
234
|
+
flip(ancestor, false, false);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
if (patch.priority !== undefined) {
|
|
238
|
+
const next = normalizeTaskPriority(patch.priority);
|
|
239
|
+
if (next !== priorityFrom) {
|
|
240
|
+
node.attrs = { ...(node.attrs ?? {}), priority: next };
|
|
241
|
+
changed = true;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
if (patch.dueAt !== undefined && setTaskDueAt(node, patch.dueAt)) {
|
|
245
|
+
changed = true;
|
|
246
|
+
}
|
|
247
|
+
let textChange = {};
|
|
248
|
+
if (patch.text !== undefined || patch.paragraph !== undefined) {
|
|
249
|
+
const textFrom = taskEditText(node);
|
|
250
|
+
const paragraphFrom = cloneJson(taskParagraph(node));
|
|
251
|
+
let edited = false;
|
|
252
|
+
if (patch.paragraph !== undefined) {
|
|
253
|
+
const next = cloneJson(patch.paragraph);
|
|
254
|
+
const content = Array.isArray(node.content) ? node.content : [];
|
|
255
|
+
const index = content.findIndex(child => isNode(child) && child.type === 'paragraph');
|
|
256
|
+
if (JSON.stringify(paragraphFrom) !== JSON.stringify(next)) {
|
|
257
|
+
node.content = index < 0 ? [next, ...content] : content.map((child, at) => (at === index ? next : child));
|
|
258
|
+
edited = true;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
if (patch.text !== undefined && setTaskEditText(node, patch.text))
|
|
262
|
+
edited = true;
|
|
263
|
+
if (edited) {
|
|
264
|
+
changed = true;
|
|
265
|
+
textChange = { text_from: textFrom, text_to: taskEditText(node), paragraph_from: paragraphFrom ?? { type: 'paragraph' } };
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
results.push({
|
|
269
|
+
task_id: patch.taskId,
|
|
270
|
+
checked_from: checkedFrom,
|
|
271
|
+
checked_to: readTaskChecked(node),
|
|
272
|
+
priority_from: priorityFrom,
|
|
273
|
+
priority_to: normalizeTaskPriority(node.attrs?.priority),
|
|
274
|
+
due_from: dueFrom,
|
|
275
|
+
due_to: readTaskDuePoint(node),
|
|
276
|
+
cascaded_task_ids: cascaded,
|
|
277
|
+
...(patch.remove ? { removed: true } : {}),
|
|
278
|
+
...textChange,
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
const doomed = new Set(patches.filter(patch => patch.remove && byId.has(patch.taskId)).map(patch => byId.get(patch.taskId).node));
|
|
282
|
+
if (doomed.size && removeTaskNodes(doc, doomed)) {
|
|
283
|
+
changed = true;
|
|
284
|
+
if (!Array.isArray(doc.content) || !doc.content.length)
|
|
285
|
+
doc.content = [{ type: 'paragraph' }];
|
|
286
|
+
}
|
|
287
|
+
return { doc, changed, missingTaskIds, results, checkedChanges, taskOrder: order };
|
|
288
|
+
}
|
|
289
|
+
/** Drops the given taskItem nodes (with their subtasks); a task list left empty is dropped too. */
|
|
290
|
+
function removeTaskNodes(node, doomed) {
|
|
291
|
+
if (!Array.isArray(node.content))
|
|
292
|
+
return false;
|
|
293
|
+
let changed = false;
|
|
294
|
+
const kept = [];
|
|
295
|
+
for (const child of node.content) {
|
|
296
|
+
if (isNode(child) && doomed.has(child)) {
|
|
297
|
+
changed = true;
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
if (isNode(child) && removeTaskNodes(child, doomed)) {
|
|
301
|
+
changed = true;
|
|
302
|
+
if (child.type === 'taskList' && !(Array.isArray(child.content) && child.content.length))
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
kept.push(child);
|
|
306
|
+
}
|
|
307
|
+
if (changed)
|
|
308
|
+
node.content = kept;
|
|
309
|
+
return changed;
|
|
310
|
+
}
|
|
311
|
+
/** Find a taskItem by id (explicit or legacy) inside an already-materialized document. */
|
|
312
|
+
export function findTaskItemById(doc, noteId, taskId) {
|
|
313
|
+
if (!isNode(doc))
|
|
314
|
+
return null;
|
|
315
|
+
return indexTasks(doc, noteId).byId.get(taskId)?.node ?? null;
|
|
316
|
+
}
|
|
317
|
+
/** Mirror checked-state changes into the legacy HTML `content` column (`<li data-id data-checked>`). */
|
|
318
|
+
export function patchLegacyHtmlChecked(html, changes) {
|
|
319
|
+
if (!html || changes.size === 0)
|
|
320
|
+
return html ?? null;
|
|
321
|
+
let next = html;
|
|
322
|
+
for (const [taskId, checked] of changes) {
|
|
323
|
+
const idPattern = taskId.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
324
|
+
const openTag = new RegExp(`<li[^>]*data-id=["']${idPattern}["'][^>]*>`, 'i');
|
|
325
|
+
const match = next.match(openTag);
|
|
326
|
+
if (!match)
|
|
327
|
+
continue;
|
|
328
|
+
const tag = match[0];
|
|
329
|
+
const replaced = /data-checked=["'](true|false)["']/i.test(tag)
|
|
330
|
+
? tag.replace(/data-checked=["'](true|false)["']/i, `data-checked="${checked}"`)
|
|
331
|
+
: tag.replace(/>$/, ` data-checked="${checked}">`);
|
|
332
|
+
next = next.replace(openTag, replaced);
|
|
333
|
+
}
|
|
334
|
+
return next;
|
|
335
|
+
}
|
|
336
|
+
const LOCAL_DATE_PATTERN = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
337
|
+
const CLOCK_TIME_PATTERN = /^([01]\d|2[0-3]):[0-5]\d$/;
|
|
338
|
+
export function parseLocalDateKey(value) {
|
|
339
|
+
const match = LOCAL_DATE_PATTERN.exec(value.trim());
|
|
340
|
+
if (!match)
|
|
341
|
+
return null;
|
|
342
|
+
const year = Number(match[1]);
|
|
343
|
+
const month = Number(match[2]);
|
|
344
|
+
const day = Number(match[3]);
|
|
345
|
+
const date = new Date(year, month - 1, day);
|
|
346
|
+
if (date.getFullYear() !== year || date.getMonth() !== month - 1 || date.getDate() !== day)
|
|
347
|
+
return null;
|
|
348
|
+
return date;
|
|
349
|
+
}
|
|
350
|
+
export function formatLocalDateKey(date) {
|
|
351
|
+
const pad = (value) => String(value).padStart(2, '0');
|
|
352
|
+
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
|
|
353
|
+
}
|
|
354
|
+
/** Resolve a local date (plus optional HH:mm) into the task due-time node, like the app. */
|
|
355
|
+
export function resolveTodoDuePoint(schedule) {
|
|
356
|
+
const date = parseLocalDateKey(String(schedule.date ?? ''));
|
|
357
|
+
if (!date) {
|
|
358
|
+
throw new DinoxError(`Invalid due date: ${schedule.date}. Use a real YYYY-MM-DD date.`, { code: 'INVALID_ARGUMENT' });
|
|
359
|
+
}
|
|
360
|
+
const time = schedule.time ?? null;
|
|
361
|
+
if (time !== null) {
|
|
362
|
+
if (!CLOCK_TIME_PATTERN.test(time)) {
|
|
363
|
+
throw new DinoxError(`Invalid due time: ${time}. Use HH:mm, or omit it for an all-day due date.`, { code: 'INVALID_ARGUMENT' });
|
|
364
|
+
}
|
|
365
|
+
const [hours, minutes] = time.split(':').map(Number);
|
|
366
|
+
date.setHours(hours, minutes, 0, 0);
|
|
367
|
+
if (date.getHours() !== hours || date.getMinutes() !== minutes) {
|
|
368
|
+
throw new DinoxError(`Local time ${schedule.date} ${time} does not exist in this time zone.`, { code: 'INVALID_ARGUMENT' });
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
return { iso: date.toISOString(), displayFormat: time === null ? 'date' : 'datetime' };
|
|
372
|
+
}
|
|
373
|
+
function normalizeDraftText(value) {
|
|
374
|
+
if (typeof value !== 'string')
|
|
375
|
+
return '';
|
|
376
|
+
return value
|
|
377
|
+
.replace(/\r\n?/g, '\n')
|
|
378
|
+
.split('\n')
|
|
379
|
+
.map((line) => line.trim())
|
|
380
|
+
.filter(Boolean)
|
|
381
|
+
.join(' ')
|
|
382
|
+
// A pasted `- [ ] foo` / `[x] foo` line is the task itself, not literal text.
|
|
383
|
+
.replace(/^(?:[-*+]|\d+[.)])\s+(?=\[(?: |x|X)\])/, '')
|
|
384
|
+
.replace(/^\[(?: |x|X)\]\s+/, '')
|
|
385
|
+
.trim();
|
|
386
|
+
}
|
|
387
|
+
function normalizeDrafts(items) {
|
|
388
|
+
return (Array.isArray(items) ? items : []).flatMap((item) => {
|
|
389
|
+
const text = normalizeDraftText(item?.text);
|
|
390
|
+
const subtasks = normalizeDrafts(item?.subtasks);
|
|
391
|
+
if (!text) {
|
|
392
|
+
if (subtasks.length > 0) {
|
|
393
|
+
throw new DinoxError('A task with subtasks needs its own text', { code: 'INVALID_ARGUMENT' });
|
|
394
|
+
}
|
|
395
|
+
return [];
|
|
396
|
+
}
|
|
397
|
+
return [{
|
|
398
|
+
text,
|
|
399
|
+
checked: item?.checked === true,
|
|
400
|
+
priority: normalizeTaskPriority(item?.priority),
|
|
401
|
+
schedule: item?.schedule ?? null,
|
|
402
|
+
subtasks,
|
|
403
|
+
}];
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
export function normalizeTodoDrafts(items) {
|
|
407
|
+
const normalized = normalizeDrafts(items);
|
|
408
|
+
if (normalized.length === 0) {
|
|
409
|
+
throw new DinoxError('At least one non-empty task is required', { code: 'INVALID_ARGUMENT' });
|
|
410
|
+
}
|
|
411
|
+
return normalized;
|
|
412
|
+
}
|
|
413
|
+
export function countDrafts(items) {
|
|
414
|
+
return items.reduce((total, item) => total + 1 + countDrafts(item.subtasks), 0);
|
|
415
|
+
}
|
|
416
|
+
function hasTimeNode(value) {
|
|
417
|
+
if (!isNode(value) || value.type === 'taskList')
|
|
418
|
+
return false;
|
|
419
|
+
if (value.type === 'time' || value.type === 'timeblock')
|
|
420
|
+
return true;
|
|
421
|
+
return Array.isArray(value.content) && value.content.some(hasTimeNode);
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* Inline content for a new task. The text is parsed as Markdown so `#tag`, links and
|
|
425
|
+
* `[time …]` become real nodes; anything that is not a single paragraph falls back to
|
|
426
|
+
* literal text so a task never turns into a heading or list.
|
|
427
|
+
*/
|
|
428
|
+
function buildTaskParagraph(text) {
|
|
429
|
+
try {
|
|
430
|
+
const parsed = convertMarkdownToNoteContent(text).contentJson;
|
|
431
|
+
const blocks = Array.isArray(parsed.content) ? parsed.content : [];
|
|
432
|
+
const only = blocks.length === 1 && isNode(blocks[0]) && blocks[0].type === 'paragraph' ? blocks[0] : null;
|
|
433
|
+
if (only && Array.isArray(only.content) && only.content.length > 0) {
|
|
434
|
+
const stripped = stripPriorityFromTaskContent([only]);
|
|
435
|
+
const paragraph = (stripped.content[0] ?? only);
|
|
436
|
+
if (Array.isArray(paragraph.content) && paragraph.content.length > 0) {
|
|
437
|
+
return { paragraph: { ...paragraph, type: 'paragraph' }, priority: stripped.priority };
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
catch {
|
|
442
|
+
// Fall through to literal text.
|
|
443
|
+
}
|
|
444
|
+
return { paragraph: { type: 'paragraph', content: [{ type: 'text', text }] }, priority: null };
|
|
445
|
+
}
|
|
446
|
+
/** Text of the task's own paragraph(s), for reporting. */
|
|
447
|
+
function inlineText(node) {
|
|
448
|
+
if (!isNode(node) || node.type === 'taskList')
|
|
449
|
+
return '';
|
|
450
|
+
if (node.type === 'text' && typeof node.text === 'string')
|
|
451
|
+
return node.text;
|
|
452
|
+
if (node.type === 'hashtag')
|
|
453
|
+
return `#${String(node.attrs?.label ?? node.attrs?.id ?? '')}`;
|
|
454
|
+
return Array.isArray(node.content) ? node.content.map(inlineText).join('') : '';
|
|
455
|
+
}
|
|
456
|
+
/**
|
|
457
|
+
* Build a taskList block for new tasks (subtasks become a nested taskList inside their parent).
|
|
458
|
+
* `created` lists every task in document order, parents before children.
|
|
459
|
+
*/
|
|
460
|
+
export function buildTaskListBlock(items, options) {
|
|
461
|
+
return {
|
|
462
|
+
type: 'taskList',
|
|
463
|
+
content: items.map((item) => {
|
|
464
|
+
const taskId = generateId();
|
|
465
|
+
const { paragraph, priority: cookiePriority } = buildTaskParagraph(item.text);
|
|
466
|
+
const priority = item.priority ?? cookiePriority;
|
|
467
|
+
const node = {
|
|
468
|
+
type: 'taskItem',
|
|
469
|
+
attrs: { id: taskId, checked: item.checked, ...(priority ? { priority } : {}) },
|
|
470
|
+
content: [paragraph],
|
|
471
|
+
};
|
|
472
|
+
const schedule = item.schedule ?? options.defaultSchedule;
|
|
473
|
+
// An inline `[time …]` already in the text wins; adding another would make a range.
|
|
474
|
+
if (schedule && !hasTimeNode(node))
|
|
475
|
+
insertDueNode(node, resolveTodoDuePoint(schedule));
|
|
476
|
+
const created = {
|
|
477
|
+
task_id: taskId,
|
|
478
|
+
text: inlineText(paragraph).trim(),
|
|
479
|
+
checked: item.checked,
|
|
480
|
+
priority: priority ?? null,
|
|
481
|
+
parent_task_id: options.parentTaskId,
|
|
482
|
+
due_time: readTaskDuePoint(node),
|
|
483
|
+
};
|
|
484
|
+
options.created.push(created);
|
|
485
|
+
if (item.subtasks.length > 0) {
|
|
486
|
+
node.content.push(buildTaskListBlock(item.subtasks, { ...options, parentTaskId: taskId }));
|
|
487
|
+
}
|
|
488
|
+
return node;
|
|
489
|
+
}),
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
/** Append a block of new subtasks to a task's own nested list, so siblings stay in one list. */
|
|
493
|
+
export function appendSubtaskBlock(taskItem, block) {
|
|
494
|
+
if (!Array.isArray(taskItem.content))
|
|
495
|
+
taskItem.content = [];
|
|
496
|
+
const last = taskItem.content[taskItem.content.length - 1];
|
|
497
|
+
if (isNode(last) && last.type === 'taskList') {
|
|
498
|
+
last.content = [...(Array.isArray(last.content) ? last.content : []), ...(block.content ?? [])];
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
taskItem.content.push(block);
|
|
502
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A task's words as the user edits them in place: the text of its first
|
|
3
|
+
* paragraph with hashtags written as `#label`. Time nodes (the schedule) and
|
|
4
|
+
* other atoms take no characters, so they are never touched by an edit.
|
|
5
|
+
*
|
|
6
|
+
* An edit is applied as the smallest splice between the old and new words, so
|
|
7
|
+
* formatting (bold, links, hashtags) outside the changed span survives exactly,
|
|
8
|
+
* and the task node with its id is never rebuilt.
|
|
9
|
+
*/
|
|
10
|
+
type JsonNode = {
|
|
11
|
+
type?: unknown;
|
|
12
|
+
text?: unknown;
|
|
13
|
+
attrs?: Record<string, unknown>;
|
|
14
|
+
marks?: unknown[];
|
|
15
|
+
content?: unknown[];
|
|
16
|
+
};
|
|
17
|
+
/** The task's own first paragraph (nested task lists belong to subtasks). */
|
|
18
|
+
export declare function taskParagraph(taskItem: JsonNode): JsonNode | null;
|
|
19
|
+
/** What the inline editor shows (and what an edit is compared against). */
|
|
20
|
+
export declare function taskEditText(taskItem: JsonNode): string;
|
|
21
|
+
/**
|
|
22
|
+
* Sets the task's words, editing only what changed. Returns false when the
|
|
23
|
+
* words are already `next`. Whitespace runs collapse; `next` must not be empty.
|
|
24
|
+
*/
|
|
25
|
+
export declare function setTaskEditText(taskItem: JsonNode, next: string): boolean;
|
|
26
|
+
export {};
|