Skip to content

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

:bug: Unify Alt-click list item folding behavior https://github.com/siyuan-note/siyuan/issues/19667

Vanessa52e364694bc6
diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.ts
index 50b56de494..6ef3a89693 100644
--- a/app/src/protyle/gutter/index.ts
+++ b/app/src/protyle/gutter/index.ts
@@ -10,7 +10,7 @@ import {
} from "../util/hasClosest";
import {getIconByType} from "../../editor/getIcon";
import {enterBack, iframeMenu, tableMenu, videoMenu, zoomOut} from "../../menus/protyle";
-import {foldBlocksRecursively, foldHeadingGroup, setFold} from "../util/blockFold";
+import {foldBlocksRecursively, foldHeadingGroup, setFold, toggleListFold} from "../util/blockFold";
import {MenuItem} from "../../menus/Menu";
import {copySubMenu, openAttr, openFileAttr, openWechatNotify} from "../../menus/commonMenuItem";
import {
@@ -130,7 +130,7 @@ import {
} from "./layout";
import {closeSubElement} from "../toolbar/subElementLifecycle";
import {canShowGutterInsert, genGutterBlockButtonHTML} from "./button";
-import {getViewFoldOccurrenceID, hasViewFoldContext, setViewFold} from "../util/viewFold";
+import {getViewFoldOccurrenceID, hasViewFoldContext} from "../util/viewFold";
import {exportImage} from "../export/util";
import {CALLOUT_PRESETS, updateCalloutType, updateCustomCalloutType} from "../wysiwyg/callout";
import {setTabsPosition, toggleTabsTasks, unwrapTabs} from "../wysiwyg/tabs";
@@ -434,62 +434,7 @@ export class Gutter {
});
} else if (event.altKey) {
// 折叠所有子集
- let hasFold = true;
- Array.from(foldElement.children).find((ulElement) => {
- if (ulElement.classList.contains("list")) {
- const foldElement = Array.from(ulElement.children).find((listItemElement) => {
- if (listItemElement.classList.contains("li")) {
- if (listItemElement.getAttribute("fold") !== "1" && listItemElement.childElementCount > 3) {
- hasFold = false;
- return true;
- }
- }
- });
- if (foldElement) {
- return true;
- }
- }
- });
- if (hasViewFoldContext(protyle)) {
- Array.from(foldElement.children).forEach(ulElement => {
- if (ulElement.classList.contains("list")) {
- Array.from(ulElement.children).forEach(listItemElement => {
- if (listItemElement.classList.contains("li") &&
- (hasFold || listItemElement.childElementCount > 3)) {
- setViewFold(protyle, listItemElement, !hasFold);
- }
- });
- }
- });
- } else {
- const doOperations: IOperation[] = [];
- const undoOperations: IOperation[] = [];
- Array.from(foldElement.children).forEach((ulElement) => {
- if (ulElement.classList.contains("list")) {
- Array.from(ulElement.children).forEach((listItemElement) => {
- if (listItemElement.classList.contains("li")) {
- if (hasFold) {
- listItemElement.removeAttribute("fold");
- } else if (listItemElement.childElementCount > 3) {
- listItemElement.setAttribute("fold", "1");
- }
- const listId = listItemElement.getAttribute("data-node-id");
- doOperations.push({
- action: "setAttrs",
- id: listId,
- data: JSON.stringify({fold: hasFold ? "" : "1"})
- });
- undoOperations.push({
- action: "setAttrs",
- id: listId,
- data: JSON.stringify({fold: hasFold ? "1" : ""})
- });
- }
- });
- }
- });
- transaction(protyle, doOperations, undoOperations);
- }
+ toggleListFold(protyle, foldElement, "children");
buttonElement.removeAttribute("disabled");
} else {
foldStatus = setFold(protyle, foldElement).fold;
@@ -620,50 +565,10 @@ export class Gutter {
foldHeadingGroup(protyle, foldElement, "siblings");
} else if (buttonElement.getAttribute("data-type") === "NodeListItem" && foldElement.parentElement.getAttribute("data-node-id")) {
// 折叠同级
- let hasFold = true;
- Array.from(foldElement.parentElement.children).find((listItemElement) => {
- if (listItemElement.classList.contains("li")) {
- if (listItemElement.getAttribute("fold") !== "1" && listItemElement.childElementCount > 3) {
- hasFold = false;
- return true;
- }
- }
- });
+ toggleListFold(protyle, foldElement.parentElement);
const arrowElement = buttonElement.parentElement.querySelector("[data-type='fold'] > svg") as HTMLElement;
if (arrowElement) {
- arrowElement.style.transform = hasFold ? "rotate(90deg)" : "";
- }
- if (hasViewFoldContext(protyle)) {
- Array.from(foldElement.parentElement.children).forEach(listItemElement => {
- if (listItemElement.classList.contains("li") &&
- (hasFold || listItemElement.childElementCount > 3)) {
- setViewFold(protyle, listItemElement, !hasFold);
- }
- });
- } else {
- const doOperations: IOperation[] = [];
- const undoOperations: IOperation[] = [];
- Array.from(foldElement.parentElement.children).find((listItemElement) => {
- if (listItemElement.classList.contains("li")) {
- if (hasFold) {
- listItemElement.removeAttribute("fold");
- } else if (listItemElement.childElementCount > 3) {
- listItemElement.setAttribute("fold", "1");
- }
- const listId = listItemElement.getAttribute("data-node-id");
- doOperations.push({
- action: "setAttrs",
- id: listId,
- data: JSON.stringify({fold: hasFold ? "" : "1"})
- });
- undoOperations.push({
- action: "setAttrs",
- id: listId,
- data: JSON.stringify({fold: hasFold ? "1" : ""})
- });
- }
- });
- transaction(protyle, doOperations, undoOperations);
+ arrowElement.style.transform = foldElement.getAttribute("fold") === "1" ? "" : "rotate(90deg)";
}
} else {
const hasFold = setFold(protyle, foldElement).fold;
diff --git a/app/src/protyle/util/blockFold.ts b/app/src/protyle/util/blockFold.ts
index 391e40e027..da3f004ba2 100644
--- a/app/src/protyle/util/blockFold.ts
+++ b/app/src/protyle/util/blockFold.ts
@@ -166,8 +166,10 @@ export const setFold = (protyle: IProtyle, nodeElement: Element, isOpen?: boolea
return {fold: !hasFold ? 1 : 0, undoOperations, doOperations, ready: Promise.resolve()};
};
-export const toggleListFold = (protyle: IProtyle, listElement: Element) => {
- const items = Array.from(listElement.children).filter(item => item.classList.contains("li"));
+export const toggleListFold = (protyle: IProtyle, listElement: Element, scope: "siblings" | "children" = "siblings") => {
+ const lists = scope === "children" ?
+ Array.from(listElement.children).filter(item => item.classList.contains("list")) : [listElement];
+ const items = lists.flatMap(list => Array.from(list.children).filter(item => item.classList.contains("li")));
const folded = items.some(item => item.getAttribute("fold") !== "1" && item.childElementCount > 3);
const doOperations: IOperation[] = [];
const undoOperations: IOperation[] = [];
diff --git a/app/tests/listFoldRemoval.test.js b/app/tests/listFoldRemoval.test.js
index 7f91e22f37..4200803749 100644
--- a/app/tests/listFoldRemoval.test.js
+++ b/app/tests/listFoldRemoval.test.js
@@ -14,6 +14,22 @@ const rendererSource = () => {
assert.equal(statements.length, names.length, file);
return statements.map(statement => statement.getText(source)).join("\n");
};
+ const extractClick = (file, condition, call = "toggleListFold(") => {
+ const source = ts.createSourceFile(file,
+ readFileSync(path.join(__dirname, "../src/protyle", `${file}.ts`), "utf8"),
+ ts.ScriptTarget.Latest, true);
+ const matches = [];
+ const visit = node => {
+ if (ts.isIfStatement(node) && condition(node.expression.getText(source)) &&
+ node.thenStatement.getText(source).includes(call)) {
+ matches.push(node.thenStatement.getText(source));
+ }
+ ts.forEachChild(node, visit);
+ };
+ visit(source);
+ assert.equal(matches.length, 1, file);
+ return matches[0];
+ };
// 使用实际删除、折叠和选区代码,只替代网络提交及无关的渲染副作用。
const source = `
const Constants = {ZWSP: "\\u200b", ATTRIBUTE_EDITING: "data-editing"};
@@ -22,6 +38,7 @@ const rendererSource = () => {
const getEmbedChildOperationParentID = () => undefined;
const confirmRefRemoval = async () => true;
const preventScroll = () => {}, mathRender = () => {}, scrollCenter = () => {};
+ const hideElements = () => {};
const lineNumberRender = () => {}, clearSelect = () => {}, revealTabsForTarget = () => {};
const getTextWithoutSemanticMarkers = element => element.textContent;
const getSemanticMarkerPrefixLengthForNode = () => 0;
@@ -41,7 +58,14 @@ const rendererSource = () => {
extract("util/selection", ["focusByRange", "focusByWbr"]) +
extract("wysiwyg/verticalVisibility", ["getFoldedNavigationOwner"]) +
extract("wysiwyg/remove", ["getOperationParentID", "removeBlock"]) +
- extract("util/blockFold", ["applyFoldState", "toggleListFold"]);
+ extract("util/blockFold", ["applyFoldState", "toggleListFold"]) + `
+ 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")};
+ `;
return ts.transpileModule(source, {compilerOptions: {
target: ts.ScriptTarget.ES2020, module: ts.ModuleKind.CommonJS,
}}).outputText;
@@ -49,7 +73,8 @@ const rendererSource = () => {
const runCases = async () => {
const assert = require("node:assert/strict");
- const {removeBlock, toggleListFold, focusByRange, getContenteditableElement} = window.listFoldRemoval;
+ const {removeBlock, toggleListFold, clickGutter, clickArrow, clickDot,
+ focusByRange, getContenteditableElement} = window.listFoldRemoval;
const ids = new Map();
const nodeID = name => {
if (!ids.has(name)) {
@@ -125,36 +150,59 @@ const runCases = async () => {
cases++;
}
}
- for (const viewFold of [false, true]) {
- const {protyle} = setup(list("mixed",
- item("leaf", "leaf") + item("expanded", "expanded", paragraph("expanded-child", "child")) +
- item("folded", "folded", paragraph("folded-child", "child"), true) +
- item("missing", "missing children", "", true)), viewFold);
- toggleListFold(protyle, byID("mixed"));
- assert.equal(byID("leaf").hasAttribute("fold"), false);
- assert.equal(byID("expanded").getAttribute("fold"), "1");
- assert.equal(byID("folded").getAttribute("fold"), "1");
- if (!viewFold) {
- assert.equal(protyle.transactions.length, 1, "batch folding must be one undo step");
- assert.deepEqual(protyle.transactions[0].doOperations.map(operation => operation.id), [nodeID("expanded")]);
- applyOperations(protyle.transactions[0].undoOperations);
- assert.equal(byID("expanded").hasAttribute("fold"), false);
- assert.equal(byID("folded").getAttribute("fold"), "1", "undo must restore mixed fold states");
- applyOperations(protyle.transactions[0].doOperations);
- }
- toggleListFold(protyle, byID("mixed"));
- assert.equal(byID("mixed").querySelectorAll('[fold="1"]').length, 0);
- if (viewFold) {
- assert.equal(protyle.transactions.length, 0, "view folding must not write document data");
- assert.deepEqual(protyle.viewChanges.map(change => change.id),
- ["expanded", "folded", "leaf", "expanded", "folded", "missing"].map(nodeID));
- } else {
- const unfolded = protyle.transactions[1];
- assert.deepEqual(unfolded.doOperations.map(operation => operation.id), ["expanded", "folded", "missing"].map(nodeID));
- applyOperations(unfolded.undoOperations);
- applyOperations(unfolded.doOperations);
+ for (const entry of ["gutter", "dot", "arrow"]) {
+ for (const viewFold of [false, true]) {
+ const mixed = list("mixed",
+ item("leaf", "leaf") + item("expanded", "expanded", paragraph("expanded-child", "child")) +
+ item("folded", "folded", paragraph("folded-child", "child"), true) +
+ item("missing", "missing children", "", true));
+ const {protyle} = setup(entry === "arrow" ? list("outer", item("parent", "parent", mixed +
+ list("extra", item("extra-folded", "extra", paragraph("extra-child", "child"), true)))) : mixed, viewFold);
+ const gutter = document.createElement("div");
+ gutter.innerHTML = '<button></button><button data-type="fold"><svg></svg></button>';
+ const button = gutter.firstElementChild;
+ const toggle = () => {
+ if (entry === "gutter") {
+ clickGutter(protyle, byID("expanded"), button);
+ assert.equal(gutter.querySelector("svg").style.transform,
+ byID("expanded").getAttribute("fold") === "1" ? "" : "rotate(90deg)");
+ } else if (entry === "dot") {
+ clickDot(protyle, byID("expanded").firstElementChild);
+ } else {
+ clickArrow(protyle, byID("parent"), button);
+ assert.equal(byID("parent").hasAttribute("fold"), false);
+ }
+ };
+ toggle();
+ assert.equal(byID("leaf").hasAttribute("fold"), false);
+ assert.equal(byID("expanded").getAttribute("fold"), "1");
+ assert.equal(byID("folded").getAttribute("fold"), "1");
+ if (!viewFold) {
+ assert.equal(protyle.transactions.length, 1, "batch folding must be one undo step");
+ assert.deepEqual(protyle.transactions[0].doOperations.map(operation => operation.id), [nodeID("expanded")]);
+ applyOperations(protyle.transactions[0].undoOperations);
+ assert.equal(byID("expanded").hasAttribute("fold"), false);
+ assert.equal(byID("folded").getAttribute("fold"), "1", "undo must restore mixed fold states");
+ applyOperations(protyle.transactions[0].doOperations);
+ }
+ toggle();
+ assert.equal(byID("mixed").querySelectorAll('[fold="1"]').length, 0);
+ if (viewFold) {
+ assert.equal(protyle.transactions.length, 0, "view folding must not write document data");
+ assert.deepEqual(protyle.viewChanges.map(change => change.id),
+ ["expanded", "folded", ...(entry === "arrow" ? ["extra-folded"] : []),
+ "leaf", "expanded", "folded", "missing", ...(entry === "arrow" ? ["extra-folded"] : [])].map(nodeID));
+ } else {
+ const unfolded = protyle.transactions[1];
+ assert.deepEqual(unfolded.doOperations.map(operation => operation.id),
+ ["expanded", "folded", "missing", ...(entry === "arrow" ? ["extra-folded"] : [])].map(nodeID));
+ applyOperations(unfolded.undoOperations);
+ assert.equal(byID("missing").getAttribute("fold"), "1");
+ applyOperations(unfolded.doOperations);
+ assert.equal(byID("missing").hasAttribute("fold"), false);
+ }
+ cases++;
}
- cases++;
}
return cases;
};
@@ -215,7 +263,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, /6 Electron cases passed/);
+ assert.match(stdout, /10 Electron cases passed/);
} finally {
assert.equal(path.dirname(path.resolve(profile)), path.resolve(os.tmpdir()));
assert.ok(path.basename(profile).startsWith("siyuan-list-fold-"));