Skip to content

Commit 4f9efb1

Browse files
committed
feat: Add autocomplete for task/done
1 parent 369e1a0 commit 4f9efb1

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/autocomplete.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ import {Library} from './library.js';
3131
import {BlockCommandBundle} from './block-command-bundle.js';
3232
import {noAwait} from './async.js';
3333
import {EditContext, Editor} from './editor.js';
34+
import {findAncestor} from './markdown/view-model-util.js';
35+
import {findIndentTarget, indent} from './indent-util.js';
36+
import {assert} from './asserts.js';
3437

3538
@customElement('pkm-autocomplete')
3639
export class Autocomplete extends LitElement {
@@ -194,6 +197,62 @@ export class Autocomplete extends LitElement {
194197
inline,
195198
this.getLinkInsertionCommand(inline),
196199
),
200+
{
201+
description: 'Task',
202+
execute: async () => {
203+
this.editor.runEditAction(inline, (context: EditContext) => {
204+
let target = findIndentTarget(node, context.root);
205+
if (target[viewModel].parent?.type !== 'list-item') {
206+
indent(node, context.root);
207+
target = findIndentTarget(node, context.root);
208+
}
209+
const listItem = target[viewModel].parent;
210+
assert(listItem);
211+
assert(listItem.type === 'list-item');
212+
context.startEditing();
213+
if (listItem.checked === undefined) {
214+
listItem[viewModel].updateChecked(false);
215+
} else {
216+
listItem[viewModel].updateChecked(undefined);
217+
}
218+
219+
node[viewModel].edit({
220+
// TODO: numbers are too contextual
221+
startIndex: this.startIndex - 1,
222+
newEndIndex: this.startIndex + 2,
223+
oldEndIndex: this.endIndex,
224+
newText: '',
225+
});
226+
this.endIndex = this.startIndex;
227+
context.focus(node, this.startIndex - 1);
228+
});
229+
},
230+
},
231+
{
232+
description: 'Done',
233+
execute: async () => {
234+
this.editor.runEditAction(inline, (context: EditContext) => {
235+
const {ancestor: target} = findAncestor(
236+
node,
237+
context.root,
238+
'list-item',
239+
);
240+
if (target) {
241+
assert(target.type === 'list-item');
242+
target[viewModel].updateChecked(true);
243+
}
244+
node[viewModel].edit({
245+
// TODO: numbers are too contextual
246+
startIndex: this.startIndex - 1,
247+
newEndIndex: this.startIndex + 2,
248+
oldEndIndex: this.endIndex,
249+
newText: '',
250+
});
251+
this.endIndex = this.startIndex;
252+
context.focus(node, this.startIndex - 1);
253+
});
254+
},
255+
},
197256
]),
198257
);
199258
this.activate(inline, cursorIndex);

src/indent-util.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export function unindent(node: ViewModelNode, root: ViewModelNode) {
6767
}
6868
}
6969

70-
export function indent(node: ViewModelNode, root: ViewModelNode) {
70+
export function findIndentTarget(node: ViewModelNode, root: ViewModelNode) {
7171
let target = node;
7272
for (const ancestor of ancestors(node, root)) {
7373
if (ancestor.type === 'list-item') {
@@ -81,6 +81,11 @@ export function indent(node: ViewModelNode, root: ViewModelNode) {
8181
}
8282
target = ancestor;
8383
}
84+
return target;
85+
}
86+
87+
export function indent(node: ViewModelNode, root: ViewModelNode) {
88+
const target = findIndentTarget(node, root);
8489
let listItem: ViewModelNode;
8590
if (target[viewModel].parent!.type === 'list-item') {
8691
listItem = target[viewModel].parent!;

0 commit comments

Comments
 (0)