Skip to content

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

:zap: Preserve large table virtualization with virtual references and inline editors https://github.com/siyuan-note/siyuan/issues/19911

Daniel293c2be8f5b6
diff --git a/app/src/protyle/wysiwyg/tableVirtualization.test.ts b/app/src/protyle/wysiwyg/tableVirtualization.test.ts
index 055c3f18d2..67a24a623c 100644
--- a/app/src/protyle/wysiwyg/tableVirtualization.test.ts
+++ b/app/src/protyle/wysiwyg/tableVirtualization.test.ts
@@ -80,7 +80,14 @@ body {margin:0; --b3-theme-surface-lighter:#ddd; --b3-font-size-editor:16px; --b
check.doesNotMatch(html, /data-sy-table-virtual/);
};
for (const pinned of [false, true]) {
- const f = fixture(2000, pinned);
+ const f = fixture(2000, pinned, table => {
+ // 自动识别的虚拟块引用可出现在表头和任意正文行中。
+ table.rows[0].cells[0].innerHTML = '<span data-type="virtual-block-ref">Column</span> 0';
+ Array.from(table.tBodies[0].rows).forEach(row => {
+ const link = row.cells[0].firstElementChild;
+ link.innerHTML = link.innerHTML.replace("Row", '<span data-type="virtual-block-ref">Row</span>');
+ });
+ });
await tick();
reduced(f.table, `initial pinned=${pinned}`);
check.equal(api.cleanTableVirtualizationHTML(f.editor.innerHTML), f.original, "source round trip is lossless");
@@ -89,6 +96,7 @@ body {margin:0; --b3-theme-surface-lighter:#ddd; --b3-font-size-editor:16px; --b
f.scroller.scrollTop = (f.scroller.scrollHeight - f.scroller.clientHeight) * progress;
await tick();
reduced(f.table, `progress=${progress}, pinned=${pinned}`);
+ check.ok(f.table.querySelector('tbody [data-type="virtual-block-ref"]'), "restored rows retain virtual references");
check.ok(Math.abs(f.table.getBoundingClientRect().height - f.height) < 2, "scrolling retains variable row heights");
const row = f.table.querySelector<HTMLTableRowElement>("tbody > tr:not([data-sy-table-virtual-rows])");
Array.from(row.cells).forEach((cell, index) => {
@@ -108,6 +116,11 @@ body {margin:0; --b3-theme-surface-lighter:#ddd; --b3-font-size-editor:16px; --b
const protectedLute = api.protectLuteTableVirtualization(lute);
check.ok(lute.BlockDOM2StdMd(f.original).includes("Row 1999 cell 4"));
check.equal(protectedLute.BlockDOM2StdMd(snapshot), lute.BlockDOM2StdMd(f.original), "copy/export retains all rows");
+ const reference = f.table.querySelector('tbody [data-type="virtual-block-ref"]');
+ reference.dispatchEvent(new PointerEvent("pointerdown", {bubbles: true, pointerType: pinned ? "touch" : "mouse"}));
+ check.ok(reference.isConnected, "reference interaction retains its event target");
+ check.equal(f.table.tBodies[0].rows.length, 2000, "reference interaction restores the complete table");
+ reference.dispatchEvent(new PointerEvent("pointerup", {bubbles: true}));
rowsMatch(protectedLute.SpinBlockDOM(snapshot));
const operations: IOperation[] = [{action: "update", id: "table", data: snapshot},
{action: "insert", id: "copy", data: snapshot}];
@@ -202,6 +215,31 @@ body {margin:0; --b3-theme-surface-lighter:#ddd; --b3-font-size-editor:16px; --b
rowsMatch(f.editor.innerHTML);
f.destroy();
}
+ {
+ const f = fixture();
+ await tick();
+ const cell = f.table.querySelector("tbody td");
+ cell.dispatchEvent(new PointerEvent("pointerdown", {bubbles: true}));
+ cell.innerHTML = '<div class="table__cell-editor table__cell--inline"><div contenteditable="true">Editing</div>' +
+ '<button class="fn__none"><svg></svg></button></div>';
+ const host = cell.firstElementChild;
+ cell.dispatchEvent(new PointerEvent("pointerup", {bubbles: true}));
+ await tick();
+ for (const progress of [0.5, 1, 0]) {
+ f.scroller.scrollTop = (f.scroller.scrollHeight - f.scroller.clientHeight) * progress;
+ f.root.dispatchEvent(new WheelEvent("wheel", {bubbles: true}));
+ await tick();
+ reduced(f.table, "scrolling with an inline cell editor");
+ host.querySelector("button").style.top = `${progress * 100}px`;
+ await tick();
+ reduced(f.table, "inline editor overlay updates after scrolling");
+ check.ok(host.isConnected, "the active editor survives scrolling outside the viewport");
+ check.equal(cell.firstElementChild, host, "the editor is never recreated from serialized HTML");
+ check.ok(Array.from(f.table.querySelectorAll("tr[data-sy-table-virtual-rows]")).every(row =>
+ !row.getAttribute("data-sy-table-virtual-rows").includes("table__cell-editor")));
+ }
+ f.destroy();
+ }
for (const event of ["copy", "cut", "paste", "keydown", "beforeinput", "compositionstart", "beforeprint"]) {
const f = fixture(300);
await tick();
diff --git a/app/src/protyle/wysiwyg/tableVirtualization.ts b/app/src/protyle/wysiwyg/tableVirtualization.ts
index 81ac4a8bf5..8dd56cadd1 100644
--- a/app/src/protyle/wysiwyg/tableVirtualization.ts
+++ b/app/src/protyle/wysiwyg/tableVirtualization.ts
@@ -126,6 +126,12 @@ export class LargeTableVirtualizer {
}
private onMutations(records: MutationRecord[]) {
+ // 内嵌编辑器的浮动控件随滚动更新样式,不属于表格正文变化。
+ records = records.filter(record => {
+ const element = record.target instanceof Element ? record.target : record.target.parentElement;
+ const host = element?.closest(".table__cell-editor.table__cell--inline");
+ return !host || element === host || host.contains(element.closest(".protyle-wysiwyg"));
+ });
if (records.length === 0) {
return;
}
@@ -142,8 +148,15 @@ export class LargeTableVirtualizer {
this.restore(table);
}
});
+ // 内联编辑器异步挂载完成后重新扫描,避免首次滚动早于编辑器就绪而停留在完整表格。
+ const inlineEditorReady = records.some(record => {
+ const element = record.target instanceof Element ? record.target : record.target.parentElement;
+ return element?.matches(".table__cell-editor.table__cell--inline") ||
+ Array.from(record.addedNodes).some(node => node instanceof Element &&
+ node.matches(".table__cell-editor.table__cell--inline"));
+ });
// 文档加载及事务替换会引入新的表格,单元格输入无需反复初始化。
- if (records.some(record => Array.from(record.addedNodes).some(node => node instanceof Element &&
+ if (inlineEditorReady || records.some(record => Array.from(record.addedNodes).some(node => node instanceof Element &&
(node.matches('[data-type="NodeTable"]') || !!node.querySelector('[data-type="NodeTable"]'))))) {
this.scan = true;
this.schedule();
@@ -171,11 +184,12 @@ export class LargeTableVirtualizer {
const body = table.tBodies[0];
if (table.tBodies.length !== 1 || table.tFoot || !body || body.rows.length <= LARGE_TABLE_ROW_THRESHOLD ||
table.closest(".protyle-wysiwyg__embed, .protyle-custom, .mindmap-view, .table__cell-rich") ||
- table.querySelector("table, img, video, audio, iframe, canvas, svg, math, input, textarea, select, button, " +
+ Array.from(table.querySelectorAll("table, img, video, audio, iframe, canvas, svg, math, input, textarea, select, button, " +
"object, embed, details, [hidden], [style*='display: none'], [style*='display:none'], " +
".table__cell-rich, .table__cell-editor, " +
'[data-type~="inline-math"], [data-type~="search-mark"], [data-type~="block-ref"], ' +
- '[data-type~="virtual-block-ref"], wbr, .fn__none') ||
+ "wbr, .fn__none")).some(element =>
+ !element.closest(".table__cell-editor.table__cell--inline")) ||
table.querySelectorAll(":scope > colgroup").length > 1) {
this.excluded.add(table);
return;
@@ -272,7 +286,9 @@ export class LargeTableVirtualizer {
// 先完成全部几何读取,再移除或恢复行;光标所在分段保持连接以保护原生选区。
plans.forEach(({state, top, bottom}) => {
state.chunks.forEach(chunk => {
- const pinned = chunk.rows?.some(row => row.contains(selection?.anchorNode));
+ // 内联单元格编辑器包含交互状态,即使光标移走也不能序列化或卸载。
+ const pinned = chunk.rows?.some(row => row.contains(selection?.anchorNode) ||
+ !!row.querySelector(".table__cell-editor"));
if (pinned || isTableChunkVisible(chunk.top, chunk.height, top, bottom)) {
if (!chunk.rows) {
const source = document.createElement("tbody");