An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:sparkles: Copy block embeds from cross-block text selections https://github.com/siyuan-note/siyuan/issues/20054
Daniel1f40bfe3d589
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.syindex 3e20b23c43..d4a05ead1b 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": "20261002201736"+ "updated": "20261002202032"},"Children": [{@@ -4293,7 +4293,7 @@"HeadingLevel": 3,"Properties": {"id": "20210106154319-v9tt3e2",- "updated": "20260928091724"+ "updated": "20261002202032"},"Children": [{@@ -4313,7 +4313,7 @@"Properties": {"colgroup": "||","id": "20240112150534-wiik5sl",- "updated": "20260902224242"+ "updated": "20261002202032"},"Children": [{@@ -4862,7 +4862,13 @@},{"Type": "NodeTableCell",- "Data": "td"+ "Data": "td",+ "Children": [+ {+ "Type": "NodeText",+ "Data": "A text selection spanning multiple blocks copies the corresponding block embed list, just like selecting those blocks"+ }+ ]}]},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.syindex 99cdd8ece2..0fc2739e1d 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": "20261002201736"+ "updated": "20261002202032"},"Children": [{@@ -4287,7 +4287,7 @@"HeadingLevel": 3,"Properties": {"id": "20201227133317-kazyttz",- "updated": "20260928091724"+ "updated": "20261002202032"},"Children": [{@@ -4311,7 +4311,7 @@"Properties": {"colgroup": "||","id": "20201227133317-vopotjc",- "updated": "20260902224242"+ "updated": "20261002202032"},"Children": [{@@ -4856,7 +4856,13 @@},{"Type": "NodeTableCell",- "Data": "td"+ "Data": "td",+ "Children": [+ {+ "Type": "NodeText",+ "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.syindex 85560904ba..ad7ba0b8de 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": "20261002201736"+ "updated": "20261002202032"},"Children": [{@@ -4281,7 +4281,7 @@"HeadingLevel": 3,"Properties": {"id": "20211226122652-xp1hi60",- "updated": "20260928091724"+ "updated": "20261002202032"},"Children": [{@@ -4301,7 +4301,7 @@"Properties": {"colgroup": "||","id": "20240112151141-ggglv0f",- "updated": "20260902224242"+ "updated": "20261002202032"},"Children": [{@@ -4850,7 +4850,13 @@},{"Type": "NodeTableCell",- "Data": "td"+ "Data": "td",+ "Children": [+ {+ "Type": "NodeText",+ "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.syindex 27693965f5..d8be189ec8 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": "20261002201736"+ "updated": "20261002202032"},"Children": [{@@ -4254,7 +4254,7 @@"Properties": {"ID": "20240530101000-tkbdrth","id": "20240530101000-9wkmh8w",- "updated": "20260928091724"+ "updated": "20261002202032"},"Children": [{@@ -4274,7 +4274,7 @@"Properties": {"colgroup": "||","id": "20240530101000-1isfzez",- "updated": "20260902224242"+ "updated": "20261002202032"},"Children": [{@@ -4819,7 +4819,13 @@},{"Type": "NodeTableCell",- "Data": "td"+ "Data": "td",+ "Children": [+ {+ "Type": "NodeText",+ "Data": "複数ブロックにまたがるテキスト選択では、それらのブロックを選択した場合と同じ埋め込みブロックのリストをコピーします"+ }+ ]}]},diff --git a/app/src/protyle/wysiwyg/commonHotkey.test.ts b/app/src/protyle/wysiwyg/commonHotkey.test.tsnew file mode 100644index 0000000000..866685ea7b--- /dev/null+++ b/app/src/protyle/wysiwyg/commonHotkey.test.ts@@ -0,0 +1,56 @@+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 source = transpileModule(readFileSync("src/protyle/wysiwyg/commonHotkey.ts", "utf8"), {+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},+}).outputText;++const copy = (options: {selected?: string[], rangeBlocks?: string[], collapsed?: boolean, outside?: boolean, document?: boolean}) => {+ const block = (id: string) => ({getAttribute: () => id});+ const writes: {ids: string[], type: string}[] = [];+ const range = {collapsed: options.collapsed ?? false, commonAncestorContainer: {}};+ let rangeReads = 0;+ let prevented = false;+ const dependencies = {+ matchHotKey: (value: string) => value === "embed",+ getEditorRange: () => range,+ getBlockElementsByRange: (value: unknown) => {+ assert.equal(value, range);+ rangeReads++;+ return (options.rangeBlocks || []).map(block);+ },+ copyTextByType: (ids: string[], type: string) => writes.push({ids: Array.from(ids), type}),+ };+ const exports = {} as {commonHotkey: (...args: unknown[]) => boolean};+ runInNewContext(source, {exports, require: () => dependencies,+ window: {siyuan: {config: {keymap: {editor: {general: {copyBlockEmbed: "embed"}}}}}}});+ const result = exports.commonHotkey({block: {rootID: "document"}, wysiwyg: {element: {+ querySelectorAll: () => (options.selected || []).map(block),+ contains: () => !options.outside,+ }}}, {preventDefault: () => prevented = true, stopPropagation() {}}, options.document ? undefined : block("caret"));+ assert.equal(result, true);+ assert.equal(prevented, true);+ return {writes, rangeReads};+};++test("copy block embeds uses all blocks in a text range in document order", () => {+ const result = copy({rangeBlocks: ["first", "middle", "last"]});+ assert.deepEqual(result.writes, [{ids: ["first", "middle", "last"], type: "blockEmbed"}]);+ assert.equal(result.rangeReads, 1);+});++test("explicit block selection takes precedence over a retained text selection", () => {+ const result = copy({selected: ["selected-first", "selected-last"], rangeBlocks: ["stale"]});+ assert.deepEqual(result.writes, [{ids: ["selected-first", "selected-last"], type: "blockEmbed"}]);+ assert.equal(result.rangeReads, 0);+});++test("collapsed and foreign selections retain the current block and document fallbacks", () => {+ for (const options of [{collapsed: true}, {outside: true}, {rangeBlocks: [] as string[]}]) {+ assert.deepEqual(copy(options).writes, [{ids: ["caret"], type: "blockEmbed"}]);+ }+ assert.deepEqual(copy({document: true}).writes, [{ids: ["document"], type: "blockEmbed"}]);+});diff --git a/app/src/protyle/wysiwyg/commonHotkey.ts b/app/src/protyle/wysiwyg/commonHotkey.tsindex 41ce78a2bc..d370079753 100644--- a/app/src/protyle/wysiwyg/commonHotkey.ts+++ b/app/src/protyle/wysiwyg/commonHotkey.ts@@ -2,7 +2,7 @@ import type {FileTreeGetDocRequestInput} from "../../types/api";import {matchHotKey} from "../util/hotKey";import {fetchPost, fetchSyncPost} from "../../util/fetch";import {isMac, writeText} from "../util/compatibility";-import {focusBlock, getSelectionOffset, setFirstNodeRange, setLastNodeRange,} from "../util/selection";+import {focusBlock, getBlockElementsByRange, getEditorRange, getSelectionOffset, setFirstNodeRange, setLastNodeRange,} from "../util/selection";import {getContenteditableElement, getNextBlock} from "./getBlock";import {hideElements} from "../ui/hideElements";import {countBlockWord} from "../../layout/status";@@ -106,6 +106,12 @@ export const commonHotkey = (protyle: IProtyle, event: KeyboardEvent, nodeElemenif (matchHotKey(window.siyuan.config.keymap.editor.general.copyBlockEmbed, event)) {if (nodeElement) {const selectElements = Array.from(protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select"));+ if (selectElements.length === 0) {+ const range = getEditorRange(protyle.wysiwyg.element);+ if (!range.collapsed && protyle.wysiwyg.element.contains(range.commonAncestorContainer)) {+ selectElements.push(...getBlockElementsByRange(range));+ }+ }if (selectElements.length === 0) {selectElements.push(nodeElement);}