Skip to content

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

:bug: Open block attributes by shortcut in read-only editors https://github.com/siyuan-note/siyuan/issues/20339

Daniel896def9e206f
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 3383170ffc..d45798f3e6 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": "20261008221006"
+ "updated": "20261009181520"
},
"Children": [
{
@@ -4293,7 +4293,7 @@
"HeadingLevel": 3,
"Properties": {
"id": "20210106154319-v9tt3e2",
- "updated": "20261008101123"
+ "updated": "20261009181520"
},
"Children": [
{
@@ -4313,7 +4313,7 @@
"Properties": {
"colgroup": "||",
"id": "20240112150534-wiik5sl",
- "updated": "20261008101123"
+ "updated": "20261009181520"
},
"Children": [
{
@@ -5379,7 +5379,7 @@
"Children": [
{
"Type": "NodeText",
- "Data": "Set block name when selecting content"
+ "Data": "In editable mode, selected text is set as the block name; in read-only mode, opens the attribute panel for viewing without changing the block name"
}
]
}
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 510fabf39c..d144c89c79 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": "20261008221006"
+ "updated": "20261009181520"
},
"Children": [
{
@@ -4287,7 +4287,7 @@
"HeadingLevel": 3,
"Properties": {
"id": "20201227133317-kazyttz",
- "updated": "20261008101123"
+ "updated": "20261009181520"
},
"Children": [
{
@@ -4311,7 +4311,7 @@
"Properties": {
"colgroup": "||",
"id": "20201227133317-vopotjc",
- "updated": "20261008101123"
+ "updated": "20261009181520"
},
"Children": [
{
@@ -5335,7 +5335,7 @@
"Children": [
{
"Type": "NodeText",
- "Data": "选择内容时为设置块命名"
+ "Data": "可编辑时将选中内容设置为块命名;只读时打开属性面板供查看,不修改块命名"
}
]
}
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 e0685970ef..4a7ec254a5 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": "20261008221006"
+ "updated": "20261009181520"
},
"Children": [
{
@@ -4281,7 +4281,7 @@
"HeadingLevel": 3,
"Properties": {
"id": "20211226122652-xp1hi60",
- "updated": "20261008101123"
+ "updated": "20261009181520"
},
"Children": [
{
@@ -4301,7 +4301,7 @@
"Properties": {
"colgroup": "||",
"id": "20240112151141-ggglv0f",
- "updated": "20261008101123"
+ "updated": "20261009181520"
},
"Children": [
{
@@ -5348,7 +5348,7 @@
"Children": [
{
"Type": "NodeText",
- "Data": "選擇內容時為設定區塊命名"
+ "Data": "可編輯時將選取內容設為區塊命名;唯讀時開啟屬性面板供檢視,不修改區塊命名"
}
]
}
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 30207ca6d4..f99d083f8d 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": "20261008221006"
+ "updated": "20261009181520"
},
"Children": [
{
@@ -4254,7 +4254,7 @@
"Properties": {
"ID": "20240530101000-tkbdrth",
"id": "20240530101000-9wkmh8w",
- "updated": "20261008101123"
+ "updated": "20261009181520"
},
"Children": [
{
@@ -4274,7 +4274,7 @@
"Properties": {
"colgroup": "||",
"id": "20240530101000-1isfzez",
- "updated": "20261008101123"
+ "updated": "20261009181520"
},
"Children": [
{
@@ -5340,7 +5340,7 @@
"Children": [
{
"Type": "NodeText",
- "Data": "選択したコンテンツはブロック名として設定されます"
+ "Data": "編集可能なときは選択した内容をブロック名に設定します。読み取り専用のときは属性パネルを閲覧用に開き、ブロック名は変更しません"
}
]
}
diff --git a/app/src/boot/globalEvent/readonlyKeydown.test.ts b/app/src/boot/globalEvent/readonlyKeydown.test.ts
index 223737c5ce..71492e8743 100644
--- a/app/src/boot/globalEvent/readonlyKeydown.test.ts
+++ b/app/src/boot/globalEvent/readonlyKeydown.test.ts
@@ -57,6 +57,7 @@ test("readonly body and tab header preserve keyboard defaults and bubbling", asy
bindVerticalNavigationReset: (): void => undefined,
getAVTemplateInteractiveElement: () => false,
hasClosestByAttribute: () => false,
+ handleReadonlyAttributeHotkey: () => false,
});
for (const [disabled, header] of [[true, false], [true, true], [false, true]]) {
let listener: (event: any) => Promise<void>;
diff --git a/app/src/protyle/util/attributeHotkey.test.ts b/app/src/protyle/util/attributeHotkey.test.ts
new file mode 100644
index 0000000000..cf9c433835
--- /dev/null
+++ b/app/src/protyle/util/attributeHotkey.test.ts
@@ -0,0 +1,77 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {ModuleKind, ScriptTarget, transpileModule} from "typescript";
+
+const fixture = () => {
+ const calls: unknown[][] = [];
+ const target = {closest: (): unknown => null};
+ const foreign = {};
+ const block = {getAttribute: () => "NodeParagraph"};
+ const top = {};
+ const range = {startContainer: block, commonAncestorContainer: block, toString: () => "selected name"};
+ const selected: object[] = [];
+ const protyle = {disabled: true, lite: false, wysiwyg: {element: {
+ contains: (node: unknown) => node === target || node === block,
+ querySelectorAll: () => selected,
+ }}};
+ const event = {
+ target, key: "attr", isComposing: false, prevented: false, stopped: false,
+ preventDefault() { this.prevented = true; }, stopPropagation() { this.stopped = true; },
+ };
+ const selection = {rangeCount: 1, getRangeAt: () => range};
+ const code = transpileModule(readFileSync("src/protyle/util/attributeHotkey.ts", "utf8"), {
+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},
+ }).outputText;
+ const exports: {handleReadonlyAttributeHotkey?: (protyle: object, event: object) => boolean} = {};
+ runInNewContext(code, {
+ exports,
+ require: (name: string) => ({
+ "../../menus/commonMenuItem": {openAttr: (...args: unknown[]) => calls.push(args)},
+ "./hasClosest": {hasClosestBlock: () => block},
+ "../wysiwyg/getBlock": {getTopAloneElement: () => top},
+ "./hotKey": {matchHotKey: (_keymap: unknown, item: {key: string}) => item.key === "attr"},
+ }[name]),
+ window: {siyuan: {config: {keymap: {editor: {general: {attr: "attr"}}}}}},
+ getSelection: () => selection,
+ });
+ return {calls, protyle, event, range, selected, selection, target, foreign, block, top,
+ run: () => exports.handleReadonlyAttributeHotkey(protyle, event)};
+};
+
+test("readonly attribute shortcut opens the current block even with selected text, without naming it", () => {
+ const subject = fixture();
+ assert.equal(subject.run(), true);
+ assert.deepEqual(subject.calls, [[subject.top, "bookmark", subject.protyle]]);
+ assert.equal(subject.event.prevented, true);
+ assert.equal(subject.event.stopped, true);
+});
+
+test("readonly attribute shortcut uses a single explicitly selected block", () => {
+ const subject = fixture();
+ const selected = {};
+ subject.selected.push(selected);
+ assert.equal(subject.run(), true);
+ assert.equal(subject.calls[0][0], selected);
+});
+
+test("attribute shortcut rejects foreign selections, controls, composition and unsupported contexts", () => {
+ const changes = [
+ (subject: ReturnType<typeof fixture>) => { subject.protyle.disabled = false; },
+ (subject: ReturnType<typeof fixture>) => { subject.protyle.lite = true; },
+ (subject: ReturnType<typeof fixture>) => { subject.event.isComposing = true; },
+ (subject: ReturnType<typeof fixture>) => { subject.event.key = "other"; },
+ (subject: ReturnType<typeof fixture>) => { subject.selection.rangeCount = 0; },
+ (subject: ReturnType<typeof fixture>) => { subject.range.commonAncestorContainer = subject.foreign as typeof subject.block; },
+ (subject: ReturnType<typeof fixture>) => { subject.target.closest = () => ({}); },
+ (subject: ReturnType<typeof fixture>) => { subject.block.getAttribute = () => "NodeThematicBreak"; },
+ ];
+ changes.forEach(change => {
+ const subject = fixture();
+ change(subject);
+ assert.equal(subject.run(), false);
+ assert.equal(subject.calls.length, 0);
+ assert.equal(subject.event.prevented, false);
+ });
+});
diff --git a/app/src/protyle/util/attributeHotkey.ts b/app/src/protyle/util/attributeHotkey.ts
new file mode 100644
index 0000000000..34f28ce2ae
--- /dev/null
+++ b/app/src/protyle/util/attributeHotkey.ts
@@ -0,0 +1,31 @@
+import {openAttr} from "../../menus/commonMenuItem";
+import {hasClosestBlock} from "./hasClosest";
+import {getTopAloneElement} from "../wysiwyg/getBlock";
+import {matchHotKey} from "./hotKey";
+
+// 只读正文的属性快捷键仅打开面板,不将选中文本写入命名。
+export const handleReadonlyAttributeHotkey = (protyle: IProtyle, event: KeyboardEvent): boolean => {
+ if (!protyle.disabled || event.isComposing ||
+ !protyle.wysiwyg.element.contains(event.target as Node) ||
+ (event.target as Element).closest(".protyle-action, .tabs-header, input, textarea, button") ||
+ !matchHotKey(window.siyuan.config.keymap.editor.general.attr, event)) {
+ return false;
+ }
+ const selection = getSelection();
+ if (!selection?.rangeCount) {
+ return false;
+ }
+ const range = selection.getRangeAt(0);
+ if (!protyle.wysiwyg.element.contains(range.commonAncestorContainer)) {
+ return false;
+ }
+ const block = hasClosestBlock(range.startContainer);
+ if (!block || protyle.lite || block.getAttribute("data-type") === "NodeThematicBreak") {
+ return false;
+ }
+ const selected = protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select");
+ openAttr(selected.length === 1 ? selected[0] : getTopAloneElement(block), "bookmark", protyle);
+ event.preventDefault();
+ event.stopPropagation();
+ return true;
+};
diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.ts
index 094865063e..cd812d6bd4 100644
--- a/app/src/protyle/wysiwyg/keydown.ts
+++ b/app/src/protyle/wysiwyg/keydown.ts
@@ -1,4 +1,5 @@
import type {BlockQueryRequestInput} from "../../types/api";
+import {handleReadonlyAttributeHotkey} from "../util/attributeHotkey";
import {logKeyboardDiagnostic} from "../../util/keyboardDiagnostic";
import {isProtyleListItemFragment} from "../runtimeCapabilities";
import {toggleListMindmap} from "../render/listMindmap";
@@ -328,6 +329,9 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
return;
}
// 只读正文和页签标题不执行正文编辑逻辑,保留默认行为并交给全局快捷键处理器。
+ if (handleReadonlyAttributeHotkey(protyle, event)) {
+ return true;
+ }
if (protyle.disabled || event.target.closest(".tabs-header")) {
return;
}