Skip to content

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

:bug: Refine tab title escape and block selection https://github.com/siyuan-note/siyuan/issues/20408

Daniel866430caf1cc
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 11b5f23152..98d54ce3cb 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": "20261010201951"
+ "updated": "20261010202520"
},
"Children": [
{
@@ -4274,7 +4274,7 @@
"HeadingLevel": 2,
"Properties": {
"id": "20210106154056-koo4wdj",
- "updated": "20261010201951"
+ "updated": "20261010202520"
},
"Children": [
{
@@ -14885,7 +14885,7 @@
"Type": "NodeHeading",
"Properties": {
"id": "20260905124213-pfrny4c",
- "updated": "20261010201951"
+ "updated": "20261010202520"
},
"HeadingLevel": 3,
"Children": [
@@ -14900,7 +14900,7 @@
"Type": "NodeParagraph",
"Properties": {
"id": "20260905124213-dh6bex8",
- "updated": "20261010201951"
+ "updated": "20261010202520"
},
"Children": [
{
@@ -14932,6 +14932,10 @@
{
"Type": "NodeText",
"Data": " Up and Down move between surrounding blocks, the active tab title, and its content. At the start or end of a tab title, Left and Right keep the caret inside the title."
+ },
+ {
+ "Type": "NodeText",
+ "Data": " Press Esc while editing a tab title to finish editing and keep the current text."
}
]
},
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 339b8d95d4..63786ccd8a 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": "20261010201951"
+ "updated": "20261010202520"
},
"Children": [
{
@@ -4272,7 +4272,7 @@
"HeadingLevel": 2,
"Properties": {
"id": "20201227133317-rkh4bji",
- "updated": "20261010201951"
+ "updated": "20261010202520"
},
"Children": [
{
@@ -14955,7 +14955,7 @@
"Type": "NodeHeading",
"Properties": {
"id": "20260905124213-avhukx7",
- "updated": "20261010201951"
+ "updated": "20261010202520"
},
"HeadingLevel": 3,
"Children": [
@@ -14970,7 +14970,7 @@
"Type": "NodeParagraph",
"Properties": {
"id": "20260905124213-w4xwhs9",
- "updated": "20261010201951"
+ "updated": "20261010202520"
},
"Children": [
{
@@ -15002,6 +15002,10 @@
{
"Type": "NodeText",
"Data": "上下方向键在相邻块、当前页签标题和正文之间移动焦点;在页签标题的开头或末尾按左右方向键,光标保持在标题内。"
+ },
+ {
+ "Type": "NodeText",
+ "Data": "编辑页签标题时,按 Esc 结束编辑并保留当前文字。"
}
]
},
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 486536259a..bab20f3b63 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": "20261010201951"
+ "updated": "20261010202520"
},
"Children": [
{
@@ -4266,7 +4266,7 @@
"HeadingLevel": 2,
"Properties": {
"id": "20211226122652-htptsmh",
- "updated": "20261010201951"
+ "updated": "20261010202520"
},
"Children": [
{
@@ -14873,7 +14873,7 @@
"Type": "NodeHeading",
"Properties": {
"id": "20260905124213-degzfbw",
- "updated": "20261010201951"
+ "updated": "20261010202520"
},
"HeadingLevel": 3,
"Children": [
@@ -14888,7 +14888,7 @@
"Type": "NodeParagraph",
"Properties": {
"id": "20260905124213-z6l8pda",
- "updated": "20261010201951"
+ "updated": "20261010202520"
},
"Children": [
{
@@ -14920,6 +14920,10 @@
{
"Type": "NodeText",
"Data": "上下方向鍵在相鄰區塊、目前頁籤標題和內文之間移動焦點;在頁籤標題的開頭或末尾按左右方向鍵,游標維持在標題內。"
+ },
+ {
+ "Type": "NodeText",
+ "Data": "編輯頁籤標題時,按 Esc 結束編輯並保留目前文字。"
}
]
},
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 e49adf9b66..61123fc3e7 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": "20261010201951"
+ "updated": "20261010202520"
},
"Children": [
{
@@ -4228,7 +4228,7 @@
"Properties": {
"ID": "20240530101000-twmuxke",
"id": "20240530101000-675gu9g",
- "updated": "20261010201951"
+ "updated": "20261010202520"
},
"Children": [
{
@@ -14802,7 +14802,7 @@
"Type": "NodeHeading",
"Properties": {
"id": "20260905124213-unrfwm9",
- "updated": "20261010201951"
+ "updated": "20261010202520"
},
"HeadingLevel": 3,
"Children": [
@@ -14817,7 +14817,7 @@
"Type": "NodeParagraph",
"Properties": {
"id": "20260905124213-fem82m0",
- "updated": "20261010201951"
+ "updated": "20261010202520"
},
"Children": [
{
@@ -14849,6 +14849,10 @@
{
"Type": "NodeText",
"Data": "上下の矢印キーで、隣接するブロック、現在のタブのタイトル、本文の間を移動できます。タブタイトルの先頭または末尾で左右の矢印キーを押しても、カーソルはタイトル内に留まります。"
+ },
+ {
+ "Type": "NodeText",
+ "Data": "タブタイトルの編集中に Esc を押すと、現在の文字を保持して編集を終了します。"
}
]
},
diff --git a/app/src/assets/scss/protyle/_tabs.scss b/app/src/assets/scss/protyle/_tabs.scss
index 8aefed45bc..bdce42e1fc 100644
--- a/app/src/assets/scss/protyle/_tabs.scss
+++ b/app/src/assets/scss/protyle/_tabs.scss
@@ -22,6 +22,14 @@
&.protyle-wysiwyg--hl::after {
border-radius: inherit;
}
+
+ &.protyle-wysiwyg--select-mode {
+ border-color: var(--b3-theme-primary-lighter);
+
+ &::after {
+ box-shadow: none;
+ }
+ }
}
.tabs-header {
diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.ts
index 789aac0645..f9e37f4450 100644
--- a/app/src/protyle/wysiwyg/keydown.ts
+++ b/app/src/protyle/wysiwyg/keydown.ts
@@ -458,7 +458,8 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
event.stopPropagation();
return;
}
- if (event.key === "Enter" || event.key === "Tab") {
+ if (event.key === "Enter" || event.key === "Tab" ||
+ (event.key === "Escape" && calloutTitleElement.classList.contains("tab-item-title"))) {
focusBlock(calloutTitleElement.closest(".tab-item") || nodeElement);
event.preventDefault();
event.stopPropagation();
diff --git a/app/tests/tabsFocus.test.js b/app/tests/tabsFocus.test.js
index 445c94b61e..4e4fdbbec0 100644
--- a/app/tests/tabsFocus.test.js
+++ b/app/tests/tabsFocus.test.js
@@ -14,6 +14,18 @@ const sources = () => {
return ts.transpileModule(statements.map(statement => statement.getText(source)).join("\n")
.replace(/^export /gm, ""), {compilerOptions: {target: ts.ScriptTarget.ES2021}}).outputText;
};
+ const keydownSource = ts.createSourceFile("keydown.ts", readFileSync(path.join(__dirname,
+ "../src/protyle/wysiwyg/keydown.ts"), "utf8"), ts.ScriptTarget.Latest, true);
+ let titleKeydown;
+ const visit = node => {
+ if (ts.isIfStatement(node) && node.expression.getText(keydownSource) ===
+ "calloutTitleElement && !event.isComposing && !blockSelectionModeElement") {
+ titleKeydown = node.getText(keydownSource);
+ }
+ ts.forEachChild(node, visit);
+ };
+ visit(keydownSource);
+ assert.ok(titleKeydown);
return [
compile("protyle/render/tabsState.ts"),
compile("protyle/render/tabsDrag.ts"),
@@ -29,6 +41,16 @@ const sources = () => {
compile("protyle/wysiwyg/transaction.ts", ["onTransaction"]),
compile("protyle/wysiwyg/tabs.ts", ["boundFocusedTitles", "canEdit", "changeTabs", "renameTab", "openTabsMenu", "initEditorTabs"]),
compile("protyle/util/selection.ts", ["selectAll"]),
+ ts.transpileModule(`const bindTitleKeydown = (protyle, calloutTitleElement) => {
+ calloutTitleElement.addEventListener("keydown", event => {
+ const range = getSelection().getRangeAt(0);
+ const nodeElement = calloutTitleElement.closest(".tab-item");
+ const blockSelectionModeElement = undefined;
+ const matchHotKey = () => false;
+ ${titleKeydown}
+ throw new Error("Title key fell through to block selection");
+ });
+ };`, {compilerOptions: {target: ts.ScriptTarget.ES2021}}).outputText,
].join("\n");
};
@@ -59,10 +81,18 @@ const cases = async (source) => {
"getSelectAllBlockAction", "countBlockWord", "getTaskStatusItems", "replayDeps",
"const {invalidateTrackedRangesByOperations, invalidateViewFoldRequests, handleViewFoldSourceOperation, " +
"updateBlock, syncTrackedRanges, applyViewFoldStates, queueHeadingNumberRefresh, refreshHeadingFoldIndicators} = replayDeps;" +
- source + "; return {initEditorTabs, openTabsMenu, changeTabs, selectAll, destroyTabsRender, onTransaction};")(
+ source + "; return {initEditorTabs, openTabsMenu, changeTabs, selectAll, destroyTabsRender, onTransaction, bindTitleKeydown};")(
{CB_GET_HISTORY: "history", ATTRIBUTE_EDITING: "data-editing"}, Menu, () => [],
(_protyle, forward, undo) => { transactions.push({forward, undo}); },
- options => { navigations.push(options.id); }, () => {}, () => {}, range => {
+ options => { navigations.push(options.id); }, () => {}, item => {
+ const content = item.querySelector('.tab-item-content [contenteditable="true"]');
+ content.focus();
+ const range = document.createRange();
+ range.selectNodeContents(content);
+ range.collapse(true);
+ window.getSelection().removeAllRanges();
+ window.getSelection().addRange(range);
+ }, range => {
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
}, (_protyle, task) => task(), () => {}, () => {}, () => {}, () => {},
@@ -96,6 +126,27 @@ const cases = async (source) => {
tabs.setAttribute("tabs-active-id", b.dataset.nodeId);
const fullGroup = tabs.outerHTML;
api.initEditorTabs(protyle);
+ if (!readonly) {
+ const title = b.querySelector(".tab-item-title");
+ api.bindTitleKeydown(protyle, title);
+ api.openTabsMenu(protyle, tabs, b, tabs);
+ menu.find(item => item.label === "Rename").click();
+ title.textContent = "Retained title";
+ title.focus();
+ const titleRange = document.createRange();
+ titleRange.selectNodeContents(title);
+ titleRange.collapse(false);
+ getSelection().removeAllRanges();
+ getSelection().addRange(titleRange);
+ const escape = new KeyboardEvent("keydown", {key: "Escape", bubbles: true, cancelable: true});
+ title.dispatchEvent(escape);
+ await new Promise(resolve => setTimeout(resolve, 0));
+ check.equal(escape.defaultPrevented, true);
+ check.equal(title.textContent, "Retained title");
+ check.equal(b.dataset.tabsEditing, "false");
+ check.ok(b.querySelector(".tab-item-content").contains(getSelection().anchorNode));
+ check.equal(root.querySelector(".protyle-wysiwyg--select-mode"), null);
+ }
const replayGroup = tabs.outerHTML;
api.openTabsMenu(protyle, tabs, a, tabs);
menu.find(item => item.label === "Zoom in").click();