Skip to content

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

:sparkles: Support Alt-click folding of embedded list items https://github.com/siyuan-note/siyuan/issues/19665

Vanessa60e78614dd53
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 62efd40f80..166bd6b8bf 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": "20260919232200"
+ "updated": "20260919234652"
},
"Children": [
{
@@ -15266,7 +15266,7 @@
"HeadingLevel": 2,
"Properties": {
"id": "20220619002116-wyh4u48",
- "updated": "20260803171606"
+ "updated": "20260919234652"
},
"Children": [
{
@@ -17660,7 +17660,7 @@
"HeadingLevel": 3,
"Properties": {
"id": "20220619002135-bsqxfmk",
- "updated": "20260811122156"
+ "updated": "20260919234652"
},
"Children": [
{
@@ -17679,7 +17679,7 @@
"Properties": {
"colgroup": "|",
"id": "20220619002135-e776q6b",
- "updated": "20260811122156"
+ "updated": "20260919234652"
},
"Children": [
{
@@ -17722,98 +17722,6 @@
}
]
},
- {
- "Type": "NodeTableRow",
- "Data": "tr",
- "Children": [
- {
- "Type": "NodeTableCell",
- "Data": "td",
- "Children": [
- {
- "Type": "NodeText",
- "Data": "Pop-up window"
- },
- {
- "Type": "NodeBr",
- "Data": "br"
- }
- ]
- },
- {
- "Type": "NodeTableCell",
- "Data": "td",
- "Children": [
- {
- "Type": "NodeText",
- "Data": "​"
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "Click"
- },
- {
- "Type": "NodeText",
- "Data": "​"
- }
- ]
- }
- ]
- },
- {
- "Type": "NodeTableRow",
- "Data": "tr",
- "Children": [
- {
- "Type": "NodeTableCell",
- "Data": "td",
- "Children": [
- {
- "Type": "NodeText",
- "Data": "Open silently"
- }
- ]
- },
- {
- "Type": "NodeTableCell",
- "Data": "td",
- "Children": [
- {
- "Type": "NodeText",
- "Data": "​"
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "Ctrl"
- },
- {
- "Type": "NodeText",
- "Data": "​+"
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "Click"
- },
- {
- "Type": "NodeText",
- "Data": " / "
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "⌘Click"
- },
- {
- "Type": "NodeText",
- "Data": "​"
- }
- ]
- }
- ]
- },
{
"Type": "NodeTableRow",
"Data": "tr",
@@ -17866,59 +17774,6 @@
]
}
]
- },
- {
- "Type": "NodeTableRow",
- "Data": "tr",
- "Children": [
- {
- "Type": "NodeTableCell",
- "Data": "td",
- "Children": [
- {
- "Type": "NodeText",
- "Data": "Open on the bottom"
- }
- ]
- },
- {
- "Type": "NodeTableCell",
- "Data": "td",
- "Children": [
- {
- "Type": "NodeText",
- "Data": "​"
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "Shift"
- },
- {
- "Type": "NodeText",
- "Data": "​+"
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "Click"
- },
- {
- "Type": "NodeText",
- "Data": " / "
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "⇧Click"
- },
- {
- "Type": "NodeText",
- "Data": "​"
- }
- ]
- }
- ]
}
]
},
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 a3d48369c4..39d41f0266 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": "20260919232200"
+ "updated": "20260919234652"
},
"Children": [
{
@@ -15248,7 +15248,7 @@
"HeadingLevel": 2,
"Properties": {
"id": "20220618235609-cuv0am5",
- "updated": "20260803171606"
+ "updated": "20260919234652"
},
"Children": [
{
@@ -17609,7 +17609,7 @@
"HeadingLevel": 3,
"Properties": {
"id": "20220619000241-hjxmjgy",
- "updated": "20260811122156"
+ "updated": "20260919234652"
},
"Children": [
{
@@ -17628,7 +17628,7 @@
"Properties": {
"colgroup": "|",
"id": "20220618235855-q57o9sx",
- "updated": "20260811122156"
+ "updated": "20260919234652"
},
"Children": [
{
@@ -17663,94 +17663,6 @@
}
]
},
- {
- "Type": "NodeTableRow",
- "Data": "tr",
- "Children": [
- {
- "Type": "NodeTableCell",
- "Data": "td",
- "Children": [
- {
- "Type": "NodeText",
- "Data": "弹出悬浮窗"
- }
- ]
- },
- {
- "Type": "NodeTableCell",
- "Data": "td",
- "Children": [
- {
- "Type": "NodeText",
- "Data": "​"
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "点击"
- },
- {
- "Type": "NodeText",
- "Data": "​"
- }
- ]
- }
- ]
- },
- {
- "Type": "NodeTableRow",
- "Data": "tr",
- "Children": [
- {
- "Type": "NodeTableCell",
- "Data": "td",
- "Children": [
- {
- "Type": "NodeText",
- "Data": "静默打开"
- }
- ]
- },
- {
- "Type": "NodeTableCell",
- "Data": "td",
- "Children": [
- {
- "Type": "NodeText",
- "Data": "​"
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "Ctrl"
- },
- {
- "Type": "NodeText",
- "Data": "​+"
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "点击"
- },
- {
- "Type": "NodeText",
- "Data": " / "
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "⌘点击"
- },
- {
- "Type": "NodeText",
- "Data": "​"
- }
- ]
- }
- ]
- },
{
"Type": "NodeTableRow",
"Data": "tr",
@@ -17803,59 +17715,6 @@
]
}
]
- },
- {
- "Type": "NodeTableRow",
- "Data": "tr",
- "Children": [
- {
- "Type": "NodeTableCell",
- "Data": "td",
- "Children": [
- {
- "Type": "NodeText",
- "Data": "在下侧打开"
- }
- ]
- },
- {
- "Type": "NodeTableCell",
- "Data": "td",
- "Children": [
- {
- "Type": "NodeText",
- "Data": "​"
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "Shift"
- },
- {
- "Type": "NodeText",
- "Data": "​+"
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "点击"
- },
- {
- "Type": "NodeText",
- "Data": " / "
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "⇧点击"
- },
- {
- "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.sy
index a94f5ca1fd..537f3be6f2 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": "20260919232200"
+ "updated": "20260919234652"
},
"Children": [
{
@@ -15254,7 +15254,7 @@
"HeadingLevel": 2,
"Properties": {
"id": "20220619002039-klsrtup",
- "updated": "20260803171606"
+ "updated": "20260919234652"
},
"Children": [
{
@@ -17656,7 +17656,7 @@
"HeadingLevel": 3,
"Properties": {
"id": "20220619002223-7cvwac5",
- "updated": "20260811122156"
+ "updated": "20260919234652"
},
"Children": [
{
@@ -17675,7 +17675,7 @@
"Properties": {
"colgroup": "|",
"id": "20220619002223-zug3slt",
- "updated": "20260811122156"
+ "updated": "20260919234652"
},
"Children": [
{
@@ -17714,98 +17714,6 @@
}
]
},
- {
- "Type": "NodeTableRow",
- "Data": "tr",
- "Children": [
- {
- "Type": "NodeTableCell",
- "Data": "td",
- "Children": [
- {
- "Type": "NodeText",
- "Data": "彈出懸浮窗"
- }
- ]
- },
- {
- "Type": "NodeTableCell",
- "Data": "td",
- "Children": [
- {
- "Type": "NodeText",
- "Data": "​"
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "點擊"
- },
- {
- "Type": "NodeText",
- "Data": "​"
- }
- ]
- }
- ]
- },
- {
- "Type": "NodeTableRow",
- "Data": "tr",
- "Children": [
- {
- "Type": "NodeTableCell",
- "Data": "td",
- "Children": [
- {
- "Type": "NodeText",
- "Data": "靜默打開"
- },
- {
- "Type": "NodeBr",
- "Data": "br"
- }
- ]
- },
- {
- "Type": "NodeTableCell",
- "Data": "td",
- "Children": [
- {
- "Type": "NodeText",
- "Data": "​"
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "Ctrl"
- },
- {
- "Type": "NodeText",
- "Data": "​+"
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "點擊"
- },
- {
- "Type": "NodeText",
- "Data": " / "
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "⌘點擊"
- },
- {
- "Type": "NodeText",
- "Data": "​"
- }
- ]
- }
- ]
- },
{
"Type": "NodeTableRow",
"Data": "tr",
@@ -17862,63 +17770,6 @@
]
}
]
- },
- {
- "Type": "NodeTableRow",
- "Data": "tr",
- "Children": [
- {
- "Type": "NodeTableCell",
- "Data": "td",
- "Children": [
- {
- "Type": "NodeText",
- "Data": "在下側打開"
- },
- {
- "Type": "NodeBr",
- "Data": "br"
- }
- ]
- },
- {
- "Type": "NodeTableCell",
- "Data": "td",
- "Children": [
- {
- "Type": "NodeText",
- "Data": "​"
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "Shift"
- },
- {
- "Type": "NodeText",
- "Data": "​+"
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "點擊"
- },
- {
- "Type": "NodeText",
- "Data": " / "
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "⇧點擊"
- },
- {
- "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.sy
index 4d69425870..ae9d2cb1e9 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": "20260919232200"
+ "updated": "20260919234652"
},
"Children": [
{
@@ -15247,7 +15247,7 @@
"Properties": {
"ID": "20240530101000-vrk49kt",
"id": "20240530101000-lika2qh",
- "updated": "20260803171606"
+ "updated": "20260919234652"
},
"Children": [
{
@@ -17697,7 +17697,7 @@
"Properties": {
"ID": "20240530101000-gc0xgdh",
"id": "20240530101000-9gd269x",
- "updated": "20260811122156"
+ "updated": "20260919234652"
},
"Children": [
{
@@ -17726,7 +17726,7 @@
"Properties": {
"colgroup": "|",
"id": "20240530101000-p1pkn1q",
- "updated": "20260811122156"
+ "updated": "20260919234652"
},
"Children": [
{
@@ -17761,94 +17761,6 @@
}
]
},
- {
- "Type": "NodeTableRow",
- "Data": "tr",
- "Children": [
- {
- "Type": "NodeTableCell",
- "Data": "td",
- "Children": [
- {
- "Type": "NodeText",
- "Data": "フローティングウィンドウで開く"
- }
- ]
- },
- {
- "Type": "NodeTableCell",
- "Data": "td",
- "Children": [
- {
- "Type": "NodeText",
- "Data": "​"
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "クリック"
- },
- {
- "Type": "NodeText",
- "Data": "​"
- }
- ]
- }
- ]
- },
- {
- "Type": "NodeTableRow",
- "Data": "tr",
- "Children": [
- {
- "Type": "NodeTableCell",
- "Data": "td",
- "Children": [
- {
- "Type": "NodeText",
- "Data": "バックグラウンドで開く"
- }
- ]
- },
- {
- "Type": "NodeTableCell",
- "Data": "td",
- "Children": [
- {
- "Type": "NodeText",
- "Data": "​"
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "Ctrl"
- },
- {
- "Type": "NodeText",
- "Data": "​+"
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "クリック"
- },
- {
- "Type": "NodeText",
- "Data": " / "
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "⌘クリック"
- },
- {
- "Type": "NodeText",
- "Data": "​"
- }
- ]
- }
- ]
- },
{
"Type": "NodeTableRow",
"Data": "tr",
@@ -17901,59 +17813,6 @@
]
}
]
- },
- {
- "Type": "NodeTableRow",
- "Data": "tr",
- "Children": [
- {
- "Type": "NodeTableCell",
- "Data": "td",
- "Children": [
- {
- "Type": "NodeText",
- "Data": "下に開く"
- }
- ]
- },
- {
- "Type": "NodeTableCell",
- "Data": "td",
- "Children": [
- {
- "Type": "NodeText",
- "Data": "​"
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "Shift"
- },
- {
- "Type": "NodeText",
- "Data": "​+"
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "クリック"
- },
- {
- "Type": "NodeText",
- "Data": " / "
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "⇧クリック"
- },
- {
- "Type": "NodeText",
- "Data": "​"
- }
- ]
- }
- ]
}
]
},
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index ce167d941d..bd70ef82d2 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -219,7 +219,7 @@ import {
import {isCrossBlockTextRange} from "../gutter/multiSelect";
import {bindTouchBlockDragSelect} from "./touchBlockDragSelect";
import {formatPainter} from "../toolbar/FormatPainter";
-import {shouldOpenListItemAttr} from "./listContext";
+import {shouldFoldEmbeddedListByAlt, shouldOpenListItemAttr} from "./listContext";
import {getBlockEdgeCaretRange, isCaretRangeInsideElement} from "./blockEdgeCaret";
import {LargeListVirtualizer} from "./listVirtualization";
import {forEachPluginSubscriber} from "../../plugin/EventBusCore";
@@ -1167,6 +1167,11 @@ export class WYSIWYG {
const documentSelf = document;
documentSelf.onmouseup = null;
let target = event.target as HTMLElement;
+ if (shouldFoldEmbeddedListByAlt(event, protyle.disabled, hasClosestByClassName(target, "protyle-action"))) {
+ // 折叠控件不改变选区,避免浏览器把光标放到嵌入块前方。
+ event.preventDefault();
+ return;
+ }
if (event.button === 0 && !event.shiftKey && !event.ctrlKey && !event.metaKey &&
!event.altKey && !protyle.disabled) {
repairHiddenTabSelection(this.element, target);
@@ -4616,6 +4621,15 @@ export class WYSIWYG {
if (hasClosestByClassName(event.target, "av__views") && avClick(protyle, event)) {
return;
}
+ const actionElement = hasClosestByClassName(event.target, "protyle-action");
+ if (actionElement && shouldFoldEmbeddedListByAlt(event, protyle.disabled, actionElement)) {
+ // 在读取编辑器选区前处理折叠,避免无选区时兜底聚焦文档顶部。
+ toggleListFold(protyle, actionElement.parentElement.parentElement);
+ hideElements(["gutter"], protyle);
+ event.preventDefault();
+ event.stopPropagation();
+ return;
+ }
const range = getEditorRange(this.element);
const resumeBlockHint = !protyle.hint.element.classList.contains("fn__none") &&
Constants.BLOCK_HINT_KEYS.includes(protyle.hint.splitChar);
@@ -4987,7 +5001,6 @@ export class WYSIWYG {
return;
}
- const actionElement = hasClosestByClassName(event.target, "protyle-action");
if (actionElement) {
const type = actionElement.parentElement.parentElement.getAttribute("data-type");
if (type === "img" && !protyle.disabled) {
diff --git a/app/src/protyle/wysiwyg/listContext.ts b/app/src/protyle/wysiwyg/listContext.ts
index bf5edfcca3..4878936c8b 100644
--- a/app/src/protyle/wysiwyg/listContext.ts
+++ b/app/src/protyle/wysiwyg/listContext.ts
@@ -179,6 +179,12 @@ export const shouldOpenListItemAttr = (shiftKey: boolean, disabled: boolean,
return shiftKey && !disabled && isListItemActionElement(actionElement);
};
+export const shouldFoldEmbeddedListByAlt = (event: Pick<MouseEvent, "altKey" | "shiftKey" | "ctrlKey" | "metaKey" | "button">,
+ disabled: boolean, actionElement: Element | false) => {
+ return event.button === 0 && event.altKey && !event.shiftKey && !event.ctrlKey && !event.metaKey &&
+ !disabled && !!actionElement && isListItemActionElement(actionElement) && !!actionElement.closest(".protyle-wysiwyg__embed");
+};
+
export const getListConversionType = (sourceSubtype: TListSubtype, targetSubtype: TListSubtype) => {
if (sourceSubtype === targetSubtype) {
return;
diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.ts
index 58f3f81ee1..0cdd4fd2f9 100644
--- a/app/src/protyle/wysiwyg/transaction.ts
+++ b/app/src/protyle/wysiwyg/transaction.ts
@@ -142,6 +142,17 @@ const removeTopElement = (updateElement: Element, protyle: IProtyle) => {
}
};
+const canSyncListFoldInPlace = (embedElement: Element, operation: Extract<IOperation, {action: "setAttrs"}>) => {
+ const attrs = JSON.parse(operation.data);
+ if (Object.keys(attrs).length !== 1 || !Object.prototype.hasOwnProperty.call(attrs, "fold")) {
+ return false;
+ }
+ const items = Array.from(embedElement.querySelectorAll(`[data-node-id="${operation.id}"]`));
+ // 列表子块已在 DOM 中时只同步折叠属性,展开缺少子块的片段仍需重新查询。
+ return items.length > 0 && items.every(item => item.getAttribute("data-type") === "NodeListItem" &&
+ (attrs.fold === "1" || item.childElementCount > 3));
+};
+
const syncBlockAttrs = (element: Element, operation: Extract<IOperation, {action: "setAttrs"}>) => {
const attrs = JSON.parse(operation.data);
const hasFold = Object.prototype.hasOwnProperty.call(attrs, "fold");
@@ -531,9 +542,9 @@ const promiseTransaction = (options: {
if (gutterFoldElement) {
gutterFoldElement.removeAttribute("disabled");
}
- // 仅在 alt+click 箭头折叠时才会触发
+ // 就地同步列表折叠,保留嵌入内容中的光标节点和布局。
protyle.wysiwyg.element.querySelectorAll('[data-type="NodeBlockQueryEmbed"]').forEach((item) => {
- if (item.querySelector(`[data-node-id="${operation.id}"]`)) {
+ if (item.querySelector(`[data-node-id="${operation.id}"]`) && !canSyncListFoldInPlace(item, operation)) {
pendingEmbedElements.add(item);
}
});
diff --git a/app/tests/listFoldRemoval.test.js b/app/tests/listFoldRemoval.test.js
index 4200803749..84c9d48a08 100644
--- a/app/tests/listFoldRemoval.test.js
+++ b/app/tests/listFoldRemoval.test.js
@@ -30,6 +30,16 @@ const rendererSource = () => {
assert.equal(matches.length, 1, file);
return matches[0];
};
+ const editorSource = readFileSync(path.join(__dirname, "../src/protyle/wysiwyg/index.ts"), "utf8");
+ const foldStart = editorSource.indexOf(' const actionElement = hasClosestByClassName(event.target, "protyle-action");');
+ const foldEnd = editorSource.indexOf(" const range = getEditorRange(this.element);", foldStart);
+ assert.ok(foldStart >= 0 && foldEnd > foldStart);
+ const foldClick = editorSource.slice(foldStart, foldEnd);
+ const embedStart = editorSource.indexOf(' const embedItemElement = hasClosestByClassName(event.target, "protyle-wysiwyg__embed");');
+ const embedEnd = editorSource.indexOf(" if (commonClick(event, protyle))", embedStart);
+ assert.ok(embedStart >= 0 && embedEnd > embedStart);
+ const embedClick = editorSource.slice(embedStart, embedEnd)
+ .replace(/\/\/\/ #if MOBILE[\s\S]*?\/\/\/ #else/g, "");
// 使用实际删除、折叠和选区代码,只替代网络提交及无关的渲染副作用。
const source = `
const Constants = {ZWSP: "\\u200b", ATTRIBUTE_EDITING: "data-editing"};
@@ -59,12 +69,35 @@ const rendererSource = () => {
extract("wysiwyg/verticalVisibility", ["getFoldedNavigationOwner"]) +
extract("wysiwyg/remove", ["getOperationParentID", "removeBlock"]) +
extract("util/blockFold", ["applyFoldState", "toggleListFold"]) + `
+ ${extract("wysiwyg/transaction", ["canSyncListFoldInPlace", "syncBlockAttrs"])}
+ export const syncEmbeddedAttrs = (protyle, operation) => {
+ const pendingEmbedElements = new Set();
+ ${extractClick("wysiwyg/transaction", condition => condition === 'operation.action === "setAttrs"', "pendingEmbedElements.add(item)")}
+ return pendingEmbedElements;
+ };
+ ${extract("wysiwyg/listContext", ["isListItemActionElement", "shouldFoldEmbeddedListByAlt"])}
+ export const embeddedMouseDown = (protyle, event) => {
+ const target = event.target;
+ if (shouldFoldEmbeddedListByAlt(event, protyle.disabled, hasClosestByClassName(target, "protyle-action")))
+ ${extractClick("wysiwyg/index", condition => condition.includes("shouldFoldEmbeddedListByAlt") && condition.includes("hasClosestByClassName(target"), "event.preventDefault()")}
+ };
export const clickGutter = (protyle, foldElement, buttonElement) => ${extractClick("gutter/index",
condition => condition.includes('buttonElement.getAttribute("data-type") === "NodeListItem"'))};
export const clickArrow = (protyle, foldElement, buttonElement) => ${extractClick("gutter/index",
condition => condition === "event.altKey", 'toggleListFold(protyle, foldElement, "children")')};
export const clickDot = (protyle, actionElement) => ${extractClick("wysiwyg/index",
condition => condition === "event.altKey && !protyle.disabled")};
+ export const clickEmbed = (protyle, event) => {
+ const ctrlIsPressed = event.ctrlKey || event.metaKey;
+ const checkFold = (id, callback) => callback(false, []);
+ const openFileById = options => protyle.opened.push(options);
+ ${foldClick}
+ protyle.rangeReads = (protyle.rangeReads || 0) + 1;
+ ${embedClick}
+ if (event.altKey && !protyle.disabled && actionElement && actionElement.parentElement.classList.contains("li")) {
+ clickDot(protyle, actionElement);
+ }
+ };
`;
return ts.transpileModule(source, {compilerOptions: {
target: ts.ScriptTarget.ES2020, module: ts.ModuleKind.CommonJS,
@@ -73,7 +106,7 @@ const rendererSource = () => {
const runCases = async () => {
const assert = require("node:assert/strict");
- const {removeBlock, toggleListFold, clickGutter, clickArrow, clickDot,
+ const {removeBlock, toggleListFold, clickGutter, clickArrow, clickDot, clickEmbed, embeddedMouseDown, syncEmbeddedAttrs,
focusByRange, getContenteditableElement} = window.listFoldRemoval;
const ids = new Map();
const nodeID = name => {
@@ -204,6 +237,85 @@ const runCases = async () => {
cases++;
}
}
+ for (const marker of ["bullet", "number", "task"]) {
+ const embeddedList = list("embedded-list", item("embedded-leaf", "leaf") +
+ item("embedded-parent", "parent", list("embedded-children", item("embedded-child", "child"))));
+ const {protyle} = setup(list("source-list", item("source-parent", "source", paragraph("source-child", "child"))) +
+ `<div data-type="NodeBlockQueryEmbed" data-node-id="${nodeID("embed")}">
+ <div class="protyle-wysiwyg__embed" data-id="${nodeID("embedded-list")}">${embeddedList}${paragraph("embedded-after", "after")}</div>
+ <div class="protyle-wysiwyg__embed" data-id="${nodeID("other-result")}">
+ ${list("other-result", item("other-parent", "other", paragraph("other-child", "child")))}
+ </div>
+ </div>`);
+ protyle.opened = [];
+ protyle.gutter = {element: document.createElement("div")};
+ const action = byID("embedded-leaf").firstElementChild;
+ action.innerHTML = "<span>marker</span>";
+ if (marker === "task") {
+ action.classList.add("protyle-action--task");
+ }
+ byID("embedded-list").setAttribute("data-subtype", marker === "number" ? "o" : marker === "task" ? "t" : "u");
+ const click = (target, modifiers = {}) => clickEmbed(protyle, {
+ target, button: 0, altKey: true, shiftKey: false, ctrlKey: false, metaKey: false,
+ stopPropagation() {}, preventDefault() {}, ...modifiers,
+ });
+ const caret = byID("embedded-after").querySelector('[contenteditable="true"]').firstChild;
+ getSelection().collapse(caret, 2);
+ const mouseDown = new MouseEvent("mousedown", {button: 0, altKey: true, bubbles: true, cancelable: true});
+ action.addEventListener("mousedown", event => embeddedMouseDown(protyle, event), {once: true});
+ action.firstElementChild.dispatchEvent(mouseDown);
+ assert.equal(mouseDown.defaultPrevented, true, "folding must suppress native caret placement");
+ click(action.firstElementChild);
+ assert.equal(getSelection().anchorNode, caret);
+ assert.equal(getSelection().anchorOffset, 2);
+ assert.equal(protyle.rangeReads || 0, 0, "folding must run before editor range fallback");
+ assert.equal(protyle.opened.length, 0, "Alt-clicking embedded markers must not open a split");
+ assert.equal(byID("embedded-parent").getAttribute("fold"), "1");
+ assert.equal(byID("embedded-leaf").hasAttribute("fold"), false);
+ assert.equal(byID("source-parent").hasAttribute("fold"), false);
+ assert.equal(byID("other-parent").hasAttribute("fold"), false);
+ assert.deepEqual(protyle.transactions[0].doOperations.map(operation => operation.id), [nodeID("embedded-parent")]);
+ const listBeforeSync = byID("embedded-list");
+ for (const operations of [protyle.transactions[0].doOperations,
+ protyle.transactions[0].undoOperations, protyle.transactions[0].doOperations]) {
+ operations.forEach(operation => {
+ assert.equal(syncEmbeddedAttrs(protyle, operation).size, 0,
+ "list folding must not schedule a full embed reload after the transaction");
+ });
+ assert.equal(byID("embedded-list"), listBeforeSync);
+ assert.equal(getSelection().anchorNode, caret);
+ assert.equal(getSelection().anchorOffset, 2);
+ }
+ assert.equal(syncEmbeddedAttrs(protyle, {
+ action: "setAttrs", id: nodeID("embedded-parent"), data: JSON.stringify({fold: "1", name: "named"}),
+ }).size, 1, "other attributes must retain embed refresh behavior");
+ assert.equal(syncEmbeddedAttrs(protyle, {
+ action: "setAttrs", id: nodeID("embedded-leaf"), data: JSON.stringify({fold: ""}),
+ }).size, 1, "expanding fragments without loaded children must still query their content");
+ applyOperations(protyle.transactions[0].undoOperations);
+ assert.equal(byID("embedded-parent").hasAttribute("fold"), false);
+ applyOperations(protyle.transactions[0].doOperations);
+ click(action);
+ assert.equal(byID("embedded-parent").hasAttribute("fold"), false);
+ getSelection().removeAllRanges();
+ click(action);
+ assert.equal(getSelection().rangeCount, 0, "folding without a caret must not focus the document start");
+ assert.equal(protyle.rangeReads || 0, 0);
+ const count = protyle.transactions.length;
+ const embedResult = action.closest(".protyle-wysiwyg__embed");
+ click(embedResult);
+ click(byID("embedded-leaf-text"));
+ click(action, {altKey: false, shiftKey: true});
+ click(action, {shiftKey: true});
+ click(action, {altKey: false, ctrlKey: true});
+ click(action, {altKey: false, metaKey: true});
+ protyle.disabled = true;
+ click(action);
+ assert.deepEqual(protyle.opened.map(options => options.position),
+ ["right", "right", "bottom", "bottom", undefined, undefined, "right"]);
+ assert.equal(protyle.transactions.length, count, "navigation and read-only clicks must not fold source blocks");
+ cases++;
+ }
return cases;
};
@@ -263,7 +375,7 @@ if (process.versions.electron && process.type === "browser") {
const {stdout} = await promisify(execFile)(require("electron"), [__filename, profile], {
env, windowsHide: true, timeout: 40000,
});
- assert.match(stdout, /10 Electron cases passed/);
+ assert.match(stdout, /13 Electron cases passed/);
} finally {
assert.equal(path.dirname(path.resolve(profile)), path.resolve(os.tmpdir()));
assert.ok(path.basename(profile).startsWith("siyuan-list-fold-"));