Skip to content

Commit

Permalink
demo: Enter at end of node text with expanded children will insert a …
Browse files Browse the repository at this point in the history
…child node. closes #29
  • Loading branch information
progrium committed Mar 6, 2023
1 parent 28e6387 commit ec59f4b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,13 @@ export async function setup(document: Document, target: HTMLElement, backend: Ba
workspace.commands.registerCommand({
id: "insert-child",
title: "Insert Child",
action: (ctx: Context, name: string = "") => {
action: (ctx: Context, name: string = "", siblingIndex?: number) => {
if (!ctx.node) return;
const node = workspace.nodes.new(name);
node.setParent(ctx.node);
if (siblingIndex !== undefined) {
node.setSiblingIndex(siblingIndex);
}
if (ctx.node.panel) {
workspace.setExpanded(ctx.node, true);
}
Expand Down
6 changes: 5 additions & 1 deletion lib/ui/outline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ export const OutlineNode: m.Component<Attrs, State> = {
if (e.ctrlKey || e.shiftKey || e.metaKey || e.altKey) return;
// cursor at end of text
if (e.target.selectionStart === e.target.value.length) {
workspace.executeCommand("insert", {node});
if (node.childCount() > 0 && workspace.getExpanded(node)) {
workspace.executeCommand("insert-child", {node}, "", 0);
} else {
workspace.executeCommand("insert", {node});
}
e.stopPropagation();
return;
}
Expand Down

0 comments on commit ec59f4b

Please # to comment.