Skip to content

Commit

Permalink
feat: Add autocomplete for task/done
Browse files Browse the repository at this point in the history
  • Loading branch information
dstoc committed Nov 6, 2024
1 parent 369e1a0 commit 4f9efb1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
59 changes: 59 additions & 0 deletions src/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import {Library} from './library.js';
import {BlockCommandBundle} from './block-command-bundle.js';
import {noAwait} from './async.js';
import {EditContext, Editor} from './editor.js';
import {findAncestor} from './markdown/view-model-util.js';
import {findIndentTarget, indent} from './indent-util.js';
import {assert} from './asserts.js';

@customElement('pkm-autocomplete')
export class Autocomplete extends LitElement {
Expand Down Expand Up @@ -194,6 +197,62 @@ export class Autocomplete extends LitElement {
inline,
this.getLinkInsertionCommand(inline),
),
{
description: 'Task',
execute: async () => {
this.editor.runEditAction(inline, (context: EditContext) => {
let target = findIndentTarget(node, context.root);
if (target[viewModel].parent?.type !== 'list-item') {
indent(node, context.root);
target = findIndentTarget(node, context.root);
}
const listItem = target[viewModel].parent;
assert(listItem);
assert(listItem.type === 'list-item');
context.startEditing();
if (listItem.checked === undefined) {
listItem[viewModel].updateChecked(false);
} else {
listItem[viewModel].updateChecked(undefined);
}

node[viewModel].edit({
// TODO: numbers are too contextual
startIndex: this.startIndex - 1,
newEndIndex: this.startIndex + 2,
oldEndIndex: this.endIndex,
newText: '',
});
this.endIndex = this.startIndex;
context.focus(node, this.startIndex - 1);
});
},
},
{
description: 'Done',
execute: async () => {
this.editor.runEditAction(inline, (context: EditContext) => {
const {ancestor: target} = findAncestor(
node,
context.root,
'list-item',
);
if (target) {
assert(target.type === 'list-item');
target[viewModel].updateChecked(true);
}
node[viewModel].edit({
// TODO: numbers are too contextual
startIndex: this.startIndex - 1,
newEndIndex: this.startIndex + 2,
oldEndIndex: this.endIndex,
newText: '',
});
this.endIndex = this.startIndex;
context.focus(node, this.startIndex - 1);
});
},
},
]),
);
this.activate(inline, cursorIndex);
Expand Down
7 changes: 6 additions & 1 deletion src/indent-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function unindent(node: ViewModelNode, root: ViewModelNode) {
}
}

export function indent(node: ViewModelNode, root: ViewModelNode) {
export function findIndentTarget(node: ViewModelNode, root: ViewModelNode) {
let target = node;
for (const ancestor of ancestors(node, root)) {
if (ancestor.type === 'list-item') {
Expand All @@ -81,6 +81,11 @@ export function indent(node: ViewModelNode, root: ViewModelNode) {
}
target = ancestor;
}
return target;
}

export function indent(node: ViewModelNode, root: ViewModelNode) {
const target = findIndentTarget(node, root);
let listItem: ViewModelNode;
if (target[viewModel].parent!.type === 'list-item') {
listItem = target[viewModel].parent!;
Expand Down

0 comments on commit 4f9efb1

Please sign in to comment.