Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:sparkles: Improve mind map keyboard editing https://github.com/siyuan-note/siyuan/issues/19793

Daniel784023b039de
diff --git a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20200924100950-9op5xi1.sy b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20200924100950-9op5xi1.sy
index 0ad247f99f..7986608361 100644
--- a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20200924100950-9op5xi1.sy
+++ b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20200924100950-9op5xi1.sy
@@ -7,7 +7,7 @@
"id": "20200924100950-9op5xi1",
"title": "Shortcuts",
"type": "doc",
- "updated": "20260924173802"
+ "updated": "20260925094732"
},
"Children": [
{
@@ -4064,7 +4064,7 @@
"HeadingLevel": 2,
"Properties": {
"id": "20210106154056-koo4wdj",
- "updated": "20260924173802"
+ "updated": "20260925094732"
},
"Children": [
{
@@ -12469,7 +12469,7 @@
"Type": "NodeHeading",
"Properties": {
"id": "20260921105011-4zeuyl4",
- "updated": "20260924145647"
+ "updated": "20260925094732"
},
"HeadingLevel": 3,
"Children": [
@@ -12484,7 +12484,7 @@
"Type": "NodeTable",
"Properties": {
"id": "20260921105011-3srqnh0",
- "updated": "20260924145647",
+ "updated": "20260925094732",
"colgroup": "|"
},
"TableAligns": [
@@ -12534,7 +12534,7 @@
"Children": [
{
"Type": "NodeText",
- "Data": "Edit the selected node"
+ "Data": "Edit the selected node (caret at end)"
}
]
},
@@ -12551,6 +12551,32 @@
}
]
},
+ {
+ "Type": "NodeTableRow",
+ "Data": "tr",
+ "Children": [
+ {
+ "Type": "NodeTableCell",
+ "Data": "td",
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "Replace the first paragraph of the selected node (keep other blocks and child nodes)"
+ }
+ ]
+ },
+ {
+ "Type": "NodeTableCell",
+ "Data": "td",
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "Type text directly"
+ }
+ ]
+ }
+ ]
+ },
{
"Type": "NodeTableRow",
"Data": "tr",
diff --git a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20200813004551-gm0pbn1.sy b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20200813004551-gm0pbn1.sy
index 181f89dfca..c2c957382b 100644
--- a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20200813004551-gm0pbn1.sy
+++ b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20200813004551-gm0pbn1.sy
@@ -7,7 +7,7 @@
"id": "20200813004551-gm0pbn1",
"title": "快捷键",
"type": "doc",
- "updated": "20260924173802"
+ "updated": "20260925094732"
},
"Children": [
{
@@ -4062,7 +4062,7 @@
"HeadingLevel": 2,
"Properties": {
"id": "20201227133317-rkh4bji",
- "updated": "20260924173802"
+ "updated": "20260925094732"
},
"Children": [
{
@@ -12459,7 +12459,7 @@
"Type": "NodeHeading",
"Properties": {
"id": "20260921105011-hmrchvc",
- "updated": "20260924145647"
+ "updated": "20260925094732"
},
"HeadingLevel": 3,
"Children": [
@@ -12474,7 +12474,7 @@
"Type": "NodeTable",
"Properties": {
"id": "20260921105011-fr0uxw6",
- "updated": "20260924145647",
+ "updated": "20260925094732",
"colgroup": "|"
},
"TableAligns": [
@@ -12524,7 +12524,7 @@
"Children": [
{
"Type": "NodeText",
- "Data": "编辑选中节点"
+ "Data": "编辑选中节点(光标位于末尾)"
}
]
},
@@ -12549,6 +12549,32 @@
}
]
},
+ {
+ "Type": "NodeTableRow",
+ "Data": "tr",
+ "Children": [
+ {
+ "Type": "NodeTableCell",
+ "Data": "td",
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "替换选中节点的首段文字(保留其他块和子节点)"
+ }
+ ]
+ },
+ {
+ "Type": "NodeTableCell",
+ "Data": "td",
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "直接输入文字"
+ }
+ ]
+ }
+ ]
+ },
{
"Type": "NodeTableRow",
"Data": "tr",
diff --git a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20211226122549-jktxego.sy b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20211226122549-jktxego.sy
index bb0188967f..6d16da08df 100644
--- a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20211226122549-jktxego.sy
+++ b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20211226122549-jktxego.sy
@@ -7,7 +7,7 @@
"id": "20211226122549-jktxego",
"title": "快捷鍵",
"type": "doc",
- "updated": "20260924173802"
+ "updated": "20260925094732"
},
"Children": [
{
@@ -4056,7 +4056,7 @@
"HeadingLevel": 2,
"Properties": {
"id": "20211226122652-htptsmh",
- "updated": "20260924173802"
+ "updated": "20260925094732"
},
"Children": [
{
@@ -12465,7 +12465,7 @@
"Type": "NodeHeading",
"Properties": {
"id": "20260921105011-z0oyw36",
- "updated": "20260924145647"
+ "updated": "20260925094732"
},
"HeadingLevel": 3,
"Children": [
@@ -12480,7 +12480,7 @@
"Type": "NodeTable",
"Properties": {
"id": "20260921105011-f8y6pxv",
- "updated": "20260924145647",
+ "updated": "20260925094732",
"colgroup": "|"
},
"TableAligns": [
@@ -12530,7 +12530,7 @@
"Children": [
{
"Type": "NodeText",
- "Data": "編輯選取的節點"
+ "Data": "編輯選取的節點(游標位於末尾)"
}
]
},
@@ -12547,6 +12547,32 @@
}
]
},
+ {
+ "Type": "NodeTableRow",
+ "Data": "tr",
+ "Children": [
+ {
+ "Type": "NodeTableCell",
+ "Data": "td",
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "取代選取節點的首段文字(保留其他區塊與子節點)"
+ }
+ ]
+ },
+ {
+ "Type": "NodeTableCell",
+ "Data": "td",
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "直接輸入文字"
+ }
+ ]
+ }
+ ]
+ },
{
"Type": "NodeTableRow",
"Data": "tr",
diff --git a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-xsbxokr.sy b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-xsbxokr.sy
index 3e04431b2c..6dc01cda94 100644
--- a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-xsbxokr.sy
+++ b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-xsbxokr.sy
@@ -9,7 +9,7 @@
"id": "20240530101000-xsbxokr",
"title": "ショートカット",
"type": "doc",
- "updated": "20260924173802"
+ "updated": "20260925094732"
},
"Children": [
{
@@ -4022,7 +4022,7 @@
"Properties": {
"ID": "20240530101000-twmuxke",
"id": "20240530101000-675gu9g",
- "updated": "20260924173802"
+ "updated": "20260925094732"
},
"Children": [
{
@@ -12423,7 +12423,7 @@
"Type": "NodeHeading",
"Properties": {
"id": "20260921105011-icqlkb6",
- "updated": "20260924145647"
+ "updated": "20260925094732"
},
"HeadingLevel": 3,
"Children": [
@@ -12438,7 +12438,7 @@
"Type": "NodeTable",
"Properties": {
"id": "20260921105011-01k7syf",
- "updated": "20260924145647",
+ "updated": "20260925094732",
"colgroup": "|"
},
"TableAligns": [
@@ -12488,7 +12488,7 @@
"Children": [
{
"Type": "NodeText",
- "Data": "選択したノードを編集"
+ "Data": "選択したノードを編集(カーソルは末尾)"
}
]
},
@@ -12505,6 +12505,32 @@
}
]
},
+ {
+ "Type": "NodeTableRow",
+ "Data": "tr",
+ "Children": [
+ {
+ "Type": "NodeTableCell",
+ "Data": "td",
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "選択したノードの最初の段落を置換(他のブロックと子ノードは保持)"
+ }
+ ]
+ },
+ {
+ "Type": "NodeTableCell",
+ "Data": "td",
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "文字を直接入力"
+ }
+ ]
+ }
+ ]
+ },
{
"Type": "NodeTableRow",
"Data": "tr",
diff --git a/app/src/protyle/render/listMindmap/editor.test.ts b/app/src/protyle/render/listMindmap/editor.test.ts
index b529e57f11..6f0d937337 100644
--- a/app/src/protyle/render/listMindmap/editor.test.ts
+++ b/app/src/protyle/render/listMindmap/editor.test.ts
@@ -36,7 +36,7 @@ const browserCases = async (sourceCode: string, css: string) => {
block.textContent = text;
return block.outerHTML;
};
- const create = (initialBlockHTML = textHTML("Initial")) => {
+ const create = (initialBlockHTML = textHTML("Initial"), replaceFirstParagraph?: string) => {
let transactionOwner: (operations: Array<{action: string; id: string; parentID?: string;
previousID?: string; nextID?: string}>) => unknown;
const container = document.createElement("section");
@@ -76,6 +76,13 @@ const browserCases = async (sourceCode: string, css: string) => {
toolbar: {element: toolbarElement, subElement},
};
const dependencies = {
+ genEmptyElement: () => {
+ const paragraph = document.createElement("div");
+ paragraph.dataset.type = "NodeParagraph";
+ paragraph.dataset.nodeId = "created";
+ paragraph.innerHTML = '<div contenteditable="true"></div>';
+ return paragraph;
+ },
showMessage: (message: string) => state.messages.push(message),
escapeHtml: (value: string) => {
const node = document.createElement("span");
@@ -114,6 +121,20 @@ const browserCases = async (sourceCode: string, css: string) => {
setMobileToolbarUndo: noop,
getDefaultToolbar: (): unknown[] => [],
hideElements: noop,
+ getDefaultKeymapBindings: (item: {default?: string}) => item.default ? [item.default] : [],
+ getKeymapBindings: (item: {custom?: string}) => item.custom ? [item.custom] : [],
+ visitKeymapItems: (keymap: {editor: object}, visit: (item: unknown, path: string[]) => void) => {
+ const walk = (value: object, path: string[]) => {
+ Object.entries(value).forEach(([key, item]) => {
+ if (item && typeof item.custom === "string") {
+ visit(item, [...path, key]);
+ } else if (item && typeof item === "object") {
+ walk(item, [...path, key]);
+ }
+ });
+ };
+ walk(keymap.editor, ["editor"]);
+ },
matchHotKey: (key: {custom?: string} | string, event: KeyboardEvent) => {
const binding = typeof key === "string" ? key : key?.custom;
return !!binding && binding.endsWith("↩") && event.key === "Enter" &&
@@ -132,7 +153,7 @@ const browserCases = async (sourceCode: string, css: string) => {
const owner = {app: {}, notebookId: "notebook", block: {rootID: "document"}};
const editor = open({
owner,
- node: {element}, host,
+ node: {element}, host, replaceFirstParagraph,
canEdit: () => element.isConnected,
onResize: noop,
onAdd: async (kind: string) => { state.additions.push(kind); },
@@ -165,7 +186,11 @@ const browserCases = async (sourceCode: string, css: string) => {
window.siyuan = {
languages: {listMindmapStale: "Content changed", listMindmapUnsupported: "Unsupported"},
config: {keymap: {editor: {
- general: {undo: "undo", redo: "redo"},
+ general: {
+ undo: "undo", redo: "redo",
+ insertBefore: {default: "⇧⌘B", custom: "⇧⌘B"},
+ insertAfter: {default: "⇧⌘A", custom: "⇧⌘A"},
+ },
list: {
mindmapAddSibling: {default: "⌘↩", custom: "⌘↩"},
mindmapAddChild: {default: "⇧⌘↩", custom: "⇧⌘↩"},
@@ -173,6 +198,27 @@ const browserCases = async (sourceCode: string, css: string) => {
}}},
} as unknown as typeof window.siyuan;
+ // 直接键入只替换首个段落,保留其余内容块。
+ const paragraph = (id: string, html: string) =>
+ `<div data-type="NodeParagraph" data-node-id="${id}" class="p"><div contenteditable="true">${html}</div></div>`;
+ let typed = create(paragraph("first", "Old <strong>rich</strong>") +
+ paragraph("second", "Keep") + '<div data-type="NodeThematicBreak" data-node-id="special"></div>', "N");
+ check.equal(typed.wysiwyg.querySelector('[data-node-id="first"] > [contenteditable="true"]').textContent, "N");
+ check.equal(typed.wysiwyg.querySelector('[data-node-id="second"] > [contenteditable="true"]').textContent, "Keep");
+ await typed.editor.finish();
+ check.equal(typed.element.querySelector('[data-node-id="first"] > [contenteditable="true"]').textContent, "N");
+ check.equal(typed.element.querySelector('[data-node-id="second"] > [contenteditable="true"]').textContent, "Keep");
+ check.ok(typed.element.querySelector('[data-node-id="special"]'));
+ await typed.remove();
+
+ // 首个正文不是段落时,在其前面新建段落,不改写特殊块。
+ typed = create('<div data-type="NodeThematicBreak" data-node-id="special"></div>', "Z");
+ check.equal(typed.wysiwyg.firstElementChild.getAttribute("data-type"), "NodeParagraph");
+ check.equal(typed.wysiwyg.firstElementChild.textContent, "Z");
+ await typed.editor.finish();
+ check.ok(typed.element.querySelector('[data-node-id="special"]'));
+ await typed.remove();
+
// 特殊块与普通文字共存时仍可进入编辑,修改文字不会改变特殊块的源码和属性。
for (const type of ["NodeThematicBreak", "NodeTable", "NodeAttributeView", "NodeBlockQueryEmbed",
"NodeCustomBlock", "NodeHTMLBlock", "NodeIFrame", "NodeWidget", "NodeVideo", "NodeAudio",
@@ -417,6 +463,34 @@ const browserCases = async (sourceCode: string, css: string) => {
await current.remove();
}
+ // 用户为其他编辑命令新增相同绑定时,按键交给节点内部编辑器处理。
+ for (const [action, kind, binding, shiftKey] of [
+ ["insertAfter", "sibling", "⌘↩", false],
+ ["insertBefore", "child", "⇧⌘↩", true],
+ ] as const) {
+ current = create();
+ const keymap = window.siyuan.config.keymap.editor.general[action];
+ keymap.custom = binding;
+ let innerKeydowns = 0;
+ current.wysiwyg.addEventListener("keydown", () => innerKeydowns++);
+ const shortcut = () => {
+ const event = new KeyboardEvent("keydown", {
+ key: "Enter", ctrlKey: true, shiftKey, bubbles: true, cancelable: true,
+ });
+ current.wysiwyg.dispatchEvent(event);
+ return event;
+ };
+ check.equal(shortcut().defaultPrevented, false, action);
+ check.equal(innerKeydowns, 1, action);
+ check.deepEqual(current.state.additions, [], action);
+ keymap.custom = keymap.default;
+ check.equal(shortcut().defaultPrevented, true, kind);
+ await until(() => current.state.additions.length === 1);
+ check.deepEqual(current.state.additions, [kind]);
+ check.equal(innerKeydowns, 1, kind);
+ await current.remove();
+ }
+
// 保存失败时不得新增节点;修正后再次按快捷键可以完成操作。
current = create();
await current.type("Retry shortcut");
diff --git a/app/src/protyle/render/listMindmap/editor.ts b/app/src/protyle/render/listMindmap/editor.ts
index 63068b1651..5ce54fc399 100644
--- a/app/src/protyle/render/listMindmap/editor.ts
+++ b/app/src/protyle/render/listMindmap/editor.ts
@@ -1,7 +1,9 @@
+import {genEmptyElement} from "../../../block/util";
import {showMessage} from "../../../dialog/message";
import {setCustomBlockRootReady} from "../../../plugin/customBlockRender";
import {escapeHtml} from "../../../util/escape";
import {isMobile} from "../../../util/functions";
+import {getDefaultKeymapBindings, getKeymapBindings, visitKeymapItems} from "../../../util/keymapBindings";
import {hintRef, hintSlash} from "../../hint/extend";
import {registerBuiltinSlashHint} from "../../hint/builtinSlash";
import {mountProtyleLiteFragment} from "../../lite/fragmentEditor";
@@ -27,6 +29,7 @@ interface ListMindmapEditorOptions {
onAdd: (kind: "child" | "sibling") => Promise<void>;
onFinish: () => void;
onUndo: (redo: boolean) => void;
+ replaceFirstParagraph?: string;
}
const nestedBranchType = (node: ListMindmapNode) =>
@@ -79,7 +82,20 @@ const retainIndependentBlocks = (html: string, node: ListMindmapNode) => {
return template.innerHTML;
};
-// 编辑器只在双击时挂载到节点内容区,预览与编辑沿用同一尺寸和排版。
+const matchesCustomizedEditorShortcut = (event: KeyboardEvent) => {
+ let matched = false;
+ visitKeymapItems({editor: window.siyuan.config.keymap.editor}, (item, path) => {
+ if (matched || path[1] === "list" &&
+ (path[2] === "mindmapAddSibling" || path[2] === "mindmapAddChild")) {
+ return;
+ }
+ const defaults = getDefaultKeymapBindings(item);
+ matched = getKeymapBindings(item).some(key => !defaults.includes(key) && matchHotKey(key, event));
+ });
+ return matched;
+};
+
+// 编辑器挂载到节点内容区,预览与编辑沿用同一尺寸和排版。
export const openListMindmapEditor = (options: ListMindmapEditorOptions) => {
const {owner, node, host} = options;
const initialBlockHTML = nodeContent(node);
@@ -348,7 +364,7 @@ export const openListMindmapEditor = (options: ListMindmapEditorOptions) => {
event.preventDefault();
event.stopImmediatePropagation();
undo(matchHotKey(keymap.redo, event));
- } else if (kind && !finished && !closing && options.canEdit() &&
+ } else if (kind && !matchesCustomizedEditorShortcut(event) && !finished && !closing && options.canEdit() &&
fragment.hintElement.classList.contains("fn__none") &&
fragment.protyle.toolbar.element.classList.contains("fn__none") &&
fragment.protyle.toolbar.subElement.classList.contains("fn__none") &&
@@ -417,6 +433,28 @@ export const openListMindmapEditor = (options: ListMindmapEditorOptions) => {
window.addEventListener("pagehide", () => void finish(), {signal});
const observer = new ResizeObserver(() => options.onResize());
observer.observe(host);
- fragment.focus(true);
+ if (options.replaceFirstParagraph === undefined) {
+ fragment.focus(true);
+ } else {
+ let paragraph = fragment.wysiwyg.querySelector<HTMLElement>(":scope > [data-type='NodeParagraph']");
+ if (!paragraph) {
+ paragraph = genEmptyElement(false, false);
+ fragment.wysiwyg.prepend(paragraph);
+ }
+ const editable = paragraph.querySelector<HTMLElement>(":scope > [contenteditable='true']");
+ editable.focus({preventScroll: true});
+ const selection = getSelection();
+ const range = document.createRange();
+ range.selectNodeContents(editable);
+ selection.removeAllRanges();
+ selection.addRange(range);
+ if (options.replaceFirstParagraph && !document.execCommand("insertText", false, options.replaceFirstParagraph)) {
+ editable.textContent = options.replaceFirstParagraph;
+ range.selectNodeContents(editable);
+ range.collapse(false);
+ selection.removeAllRanges();
+ selection.addRange(range);
+ }
+ }
return {finish, destroy};
};
diff --git a/app/src/protyle/render/listMindmap/index.ts b/app/src/protyle/render/listMindmap/index.ts
index 6229cb676e..aefbacfe59 100644
--- a/app/src/protyle/render/listMindmap/index.ts
+++ b/app/src/protyle/render/listMindmap/index.ts
@@ -235,7 +235,7 @@ class ListMindmapController {
this.destroy();
}
},
- onEdit: (id, contentHost) => this.edit(id, contentHost),
+ onEdit: (id, contentHost, replaceFirstParagraph) => this.edit(id, contentHost, replaceFirstParagraph),
isAddSiblingShortcut: event => {
const key = window.siyuan.config.keymap.editor.list?.mindmapAddSibling;
return !!key && matchHotKey(key, event);
@@ -437,7 +437,7 @@ class ListMindmapController {
}
}
- private async edit(id: string, host: HTMLElement) {
+ private async edit(id: string, host: HTMLElement, replaceFirstParagraph?: string) {
const request = ++this.editRequest;
if (!canEdit(this.owner, this.list) || (this.activeEditor && !await this.activeEditor.finish()) ||
request !== this.editRequest || this.disposed || !host.isConnected) {
@@ -449,7 +449,7 @@ class ListMindmapController {
}
this.view.setEditing(id);
this.activeEditor = openListMindmapEditor({
- owner: this.owner, node, host,
+ owner: this.owner, node, host, replaceFirstParagraph,
canEdit: () => !this.disposed && this.list.isConnected && canEdit(this.owner, this.list),
onSave: html => this.change(() => replaceListMindmapContent(this.list, id, html), true),
onResize: () => this.view.refreshLayout(),
diff --git a/app/src/protyle/render/listMindmap/model.test.ts b/app/src/protyle/render/listMindmap/model.test.ts
index 7a2343e727..d14cd719de 100644
--- a/app/src/protyle/render/listMindmap/model.test.ts
+++ b/app/src/protyle/render/listMindmap/model.test.ts
@@ -756,6 +756,7 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,
const alpha = model.root.children[0].id;
const beta = model.root.children[1].id;
const edits: string[] = [];
+ const editTexts: Array<string | undefined> = [];
const moves: unknown[] = [];
const additions: unknown[] = [];
const folds: string[] = [];
@@ -780,7 +781,10 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,
interactions++;
return () => interactions--;
},
- onEdit: (id: string) => edits.push(id),
+ onEdit: (id: string, _contentHost: HTMLElement, text?: string) => {
+ edits.push(id);
+ editTexts.push(text);
+ },
isAddSiblingShortcut: (event: KeyboardEvent) => event.ctrlKey && event.key === "Enter" && !event.shiftKey,
isAddChildShortcut: (event: KeyboardEvent) => event.ctrlKey && event.key === "Enter" && event.shiftKey,
finishEdit: () => finishAllowed,
@@ -1081,9 +1085,33 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,
view.setReadOnly(false);
navigate(" ");
check.equal(edits[edits.length - 1], beta, "space opens the selected node editor");
+ check.equal(editTexts[editTexts.length - 1], undefined, "space keeps the node content");
navigate("ArrowUp");
check.equal(view.selectedId, beta, "editing keeps arrow keys inside the editor");
view.setEditing();
+ navigate("x");
+ check.equal(edits[edits.length - 1], beta, "typing opens the selected node editor");
+ check.equal(editTexts[editTexts.length - 1], "x", "typing replaces the first paragraph");
+ view.setEditing();
+ const editsBeforeModifiers = edits.length;
+ host.dispatchEvent(new KeyboardEvent("keydown", {key: "x", ctrlKey: true, bubbles: true}));
+ check.equal(edits.length, editsBeforeModifiers, "modified keys do not replace node text");
+ view.setReadOnly(true);
+ navigate("x");
+ check.equal(edits.length, editsBeforeModifiers, "read-only nodes cannot be edited by typing");
+ view.setReadOnly(false);
+ const composition = new KeyboardEvent("keydown", {key: "Process", bubbles: true, cancelable: true});
+ host.dispatchEvent(composition);
+ check.equal(composition.defaultPrevented, false, "composition can continue in the node editor");
+ check.equal(editTexts[editTexts.length - 1], "", "composition selects the first paragraph");
+ view.setEditing();
+ const composingKey = new KeyboardEvent("keydown", {
+ key: "a", isComposing: true, bubbles: true, cancelable: true,
+ });
+ host.dispatchEvent(composingKey);
+ check.equal(composingKey.defaultPrevented, false);
+ check.equal(editTexts[editTexts.length - 1], "", "composing text is not inserted as a literal key");
+ view.setEditing();
select(model.root.id);
navigate(" ");
const titleInput = host.querySelector<HTMLTextAreaElement>(".mindmap-view__root-title");
@@ -1092,6 +1120,11 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,
check.equal(titleInput.selectionEnd, titleInput.value.length);
titleInput.dispatchEvent(new KeyboardEvent("keydown", {key: "Escape", bubbles: true}));
check.equal(document.activeElement, host);
+ select(model.root.id);
+ navigate("R");
+ const typedTitleInput = host.querySelector<HTMLTextAreaElement>(".mindmap-view__root-title");
+ check.equal(typedTitleInput.value, "R", "typing replaces the virtual root title");
+ typedTitleInput.dispatchEvent(new KeyboardEvent("keydown", {key: "Escape", bubbles: true}));
edits.length = beforeSpaceEdit;
model.nodes.get(alpha).collapsed = true;
view.update(model);
@@ -1877,9 +1910,10 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,
await settle();
check.equal(host.querySelector('.mindmap-view__toolbar [aria-label="listMindmapChild"]'), null);
check.equal(host.querySelector('[aria-label="undo"]'), null);
+ const editsBeforeReadonly = edits.length;
nodeElement(beta).dispatchEvent(new MouseEvent("dblclick", {bubbles: true}));
host.dispatchEvent(new KeyboardEvent("keydown", {key: "z", ctrlKey: true, bubbles: true}));
- check.equal(edits.length, 1);
+ check.equal(edits.length, editsBeforeReadonly);
check.equal(undo, 0);
const persistedBeforeFold = list.outerHTML;
pressDelete();
diff --git a/app/src/protyle/render/listMindmap/view.ts b/app/src/protyle/render/listMindmap/view.ts
index cf55397a9c..30a851e4b6 100644
--- a/app/src/protyle/render/listMindmap/view.ts
+++ b/app/src/protyle/render/listMindmap/view.ts
@@ -33,7 +33,7 @@ export interface ListMindmapViewOptions {
onManageLineColors?: () => void;
onFullscreen?: (enter: boolean, button: HTMLButtonElement) => void;
onInteractionStart?: (event: PointerEvent) => () => void;
- onEdit?: (id: string, contentHost: HTMLElement) => void;
+ onEdit?: (id: string, contentHost: HTMLElement, replaceFirstParagraph?: string) => void;
isAddSiblingShortcut?: (event: KeyboardEvent) => boolean;
isAddChildShortcut?: (event: KeyboardEvent) => boolean;
onRootTitleChange?: (title: string) => void;
@@ -1778,13 +1778,13 @@ export class ListMindmapView {
});
};
- private editRootTitle(id: string, toEnd = false) {
+ private editRootTitle(id: string, toEnd = false, replaceText?: string) {
this.finishRelationEdit?.(true);
const content = this.getContentHost(id);
const input = createElement("textarea", "mindmap-view__root-title");
const measure = createElement("span", "mindmap-view__root-title-measure");
input.rows = 1;
- input.value = this.model.metadata.rootTitle || "";
+ input.value = replaceText === undefined ? this.model.metadata.rootTitle || "" : replaceText;
measure.textContent = input.value;
input.setAttribute("aria-label", this.label("text"));
content.classList.add("mindmap-view__root-title-host");
@@ -1821,7 +1821,7 @@ export class ListMindmapView {
}
});
input.focus();
- if (toEnd) {
+ if (toEnd || replaceText !== undefined) {
input.setSelectionRange(input.value.length, input.value.length);
} else {
input.select();
@@ -1978,17 +1978,24 @@ export class ListMindmapView {
private keyDown = (event: KeyboardEvent) => {
const target = event.target as HTMLElement;
- if (event.isComposing || target.closest("input, textarea, select, audio, video, iframe, .mindmap-view__node--editing")) {
+ const startingComposition = event.isComposing || event.key === "Process" || event.key === "Dead" ||
+ event.keyCode === 229;
+ if (target.closest("input, textarea, select, audio, video, iframe, .mindmap-view__node--editing")) {
return;
}
if (this.pointer?.relation && event.key !== "Escape") {
return;
}
const noModifiers = !event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey;
- const siblingShortcut = event.key === "Enter" && noModifiers || this.options.isAddSiblingShortcut?.(event);
- const childShortcut = event.key === "Tab" && noModifiers || this.options.isAddChildShortcut?.(event);
- const isTaskCompletionToggle = this.options.isTaskCompletionToggle?.(event);
- if (!this.options.readOnly && (isTaskCompletionToggle || this.options.isTaskCycle?.(event))) {
+ const siblingShortcut = !startingComposition &&
+ (event.key === "Enter" && noModifiers || this.options.isAddSiblingShortcut?.(event));
+ const childShortcut = !startingComposition &&
+ (event.key === "Tab" && noModifiers || this.options.isAddChildShortcut?.(event));
+ const text = !startingComposition && !event.ctrlKey && !event.metaKey && !event.altKey &&
+ event.key !== " " && Array.from(event.key).length === 1 ? event.key : undefined;
+ const isTaskCompletionToggle = !startingComposition && this.options.isTaskCompletionToggle?.(event);
+ if (!startingComposition && !this.options.readOnly &&
+ (isTaskCompletionToggle || this.options.isTaskCycle?.(event))) {
const id = target.closest<HTMLElement>(".mindmap-view__node")?.dataset.mindmapId || this.selectedId;
if (this.model.nodes.get(id)?.taskMarker !== undefined) {
event.preventDefault();
@@ -2003,7 +2010,7 @@ export class ListMindmapView {
}
return;
}
- if (["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown", " "].includes(event.key) &&
+ if (!startingComposition && ["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown", " "].includes(event.key) &&
!this.editingId && !this.relationFrom && this.selectedId &&
noModifiers &&
!target.isContentEditable && !target.closest("button, a")) {
@@ -2043,6 +2050,31 @@ export class ListMindmapView {
this.selectNode(next);
}
}
+ } else if (!this.options.readOnly && !this.editingId && !this.relationFrom && this.selectedId &&
+ (text !== undefined || startingComposition && !event.ctrlKey && !event.metaKey && !event.altKey) &&
+ !target.isContentEditable && !target.closest("button, a")) {
+ const id = this.selectedId;
+ if (event.repeat) {
+ event.preventDefault();
+ event.stopPropagation();
+ return;
+ }
+ this.finishThen(() => {
+ const node = this.model.nodes.get(id);
+ if (this.selectedId !== id || !node) {
+ return;
+ }
+ if (node.virtual) {
+ this.editRootTitle(id, false, text);
+ } else {
+ this.setEditing(id);
+ this.options.onEdit?.(id, this.getContentHost(id), text ?? "");
+ }
+ });
+ if (startingComposition) {
+ event.stopPropagation();
+ return;
+ }
} else if (!this.options.readOnly && !this.editingId && !this.relationFrom && this.selectedId &&
(siblingShortcut || childShortcut) &&
!target.isContentEditable && !target.closest("button, a")) {
@@ -2062,7 +2094,7 @@ export class ListMindmapView {
this.options.onAdd?.(id, kind);
}
});
- } else if ((event.key === "Delete" || event.key === "Backspace") &&
+ } else if (!startingComposition && (event.key === "Delete" || event.key === "Backspace") &&
!event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey) {
const selectedId = this.selectedId;
const selectedRelation = this.selectedRelation;
@@ -2071,7 +2103,7 @@ export class ListMindmapView {
this.deleteSelection();
}
});
- } else if (event.key === "Escape") {
+ } else if (!startingComposition && event.key === "Escape") {
const draggingRoute = !!this.pointer?.relation;
this.cancelPointer();
if (draggingRoute) {