An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Preserve table scroll position during DOM rebuild https://github.com/siyuan-note/siyuan/issues/18235
Danielc259bda0974e
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.tsindex 6a60d7c4af..ba2a6a3a99 100644--- a/app/src/protyle/wysiwyg/index.ts+++ b/app/src/protyle/wysiwyg/index.ts@@ -2275,33 +2275,45 @@ export class WYSIWYG {range.deleteContents();tempElement.append(newSpanElement);} else {- if (selectTableRange) {- // 表格内跨多单元格的文本选区:按网格映射重建合法 table,重新计算 colspan/rowspan。- // 必须在 extractContents 删除原内容前计算,否则 getBoundingClientRect 拿不到原始位置- const tableElement = tableRangeElement.querySelector("table");- const newTableHTML = getTableRangeHTML(tableElement, tableRangeStartCell, tableRangeEndCell);- // 放入 tempElement 以便后续 html = tempElement.innerHTML 取用(裸 table,后续统一包 BlockDOM)- tempElement.innerHTML = newTableHTML;- textPlain = protyle.lute.HTML2Md(newTableHTML);- // 删除选区内容并修复表格 DOM- const wbrElement = document.createElement("wbr");- range.insertNode(wbrElement);- range.setStartAfter(wbrElement);- range.extractContents();- nodeElement.outerHTML = protyle.lute.SpinBlockDOM(nodeElement.outerHTML);- nodeElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${id}"]`) as HTMLElement;- mathRender(nodeElement);- focusByWbr(nodeElement, range);- } else if (cloneElement.querySelectorAll("td, th").length > 0) {- // 表格内多格子 cut https://github.com/siyuan-note/siyuan/issues/564- const wbrElement = document.createElement("wbr");- range.insertNode(wbrElement);- range.setStartAfter(wbrElement);- tempElement.append(range.extractContents());+ if (selectTableRange || cloneElement.querySelectorAll("td, th").length > 0) {+ const tableScrollLeft = nodeElement.firstElementChild.scrollLeft;+ const tableScrollTop = nodeElement.firstElementChild.scrollTop;+ const contentScrollTop = protyle.contentElement.scrollTop;+ if (selectTableRange) {+ // 表格内跨多单元格的文本选区:按网格映射重建合法 table,重新计算 colspan/rowspan。+ // 必须在 extractContents 删除原内容前计算,否则 getBoundingClientRect 拿不到原始位置+ const tableElement = tableRangeElement.querySelector("table");+ const newTableHTML = getTableRangeHTML(tableElement, tableRangeStartCell, tableRangeEndCell);+ // 放入 tempElement 以便后续 html = tempElement.innerHTML 取用(裸 table,后续统一包 BlockDOM)+ tempElement.innerHTML = newTableHTML;+ textPlain = protyle.lute.HTML2Md(newTableHTML);+ // 删除选区内容并修复表格 DOM+ const wbrElement = document.createElement("wbr");+ range.insertNode(wbrElement);+ range.setStartAfter(wbrElement);+ range.extractContents();+ } else {+ // 表格内多格子 cut https://github.com/siyuan-note/siyuan/issues/564+ const wbrElement = document.createElement("wbr");+ range.insertNode(wbrElement);+ range.setStartAfter(wbrElement);+ tempElement.append(range.extractContents());+ }nodeElement.outerHTML = protyle.lute.SpinBlockDOM(nodeElement.outerHTML);nodeElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${id}"]`) as HTMLElement;mathRender(nodeElement);focusByWbr(nodeElement, range);+ // SpinBlockDOM 替换整张表格后,恢复旧表格的内外层滚动位置+ if (tableScrollLeft > 0) {+ nodeElement.firstElementChild.scrollLeft = tableScrollLeft;+ }+ if (tableScrollTop > 0) {+ nodeElement.firstElementChild.scrollTop = tableScrollTop;+ }+ if (contentScrollTop > 0) {+ protyle.contentElement.scrollTop = contentScrollTop;+ protyle.scroll.lastScrollTop = contentScrollTop - 1;+ }} else {const inlineMathElement = hasClosestByAttribute(range.commonAncestorContainer, "data-type", "inline-math");if (inlineMathElement) {diff --git a/app/src/protyle/wysiwyg/input.ts b/app/src/protyle/wysiwyg/input.tsindex 97bab712ea..d4adece94c 100644--- a/app/src/protyle/wysiwyg/input.ts+++ b/app/src/protyle/wysiwyg/input.ts@@ -228,11 +228,13 @@ export const input = async (protyle: IProtyle, blockElement: HTMLElement, range:}let scrollLeft: number;let scrollTop: number;+ let contentScrollTop: number;if (blockElement.classList.contains("table")) {// 表格的横向、纵向滚动均发生在首个子节点(contenteditable 容器,overflow:auto)上,// 重建 DOM 后需一并还原,否则固定表头长表格输入会跳回开头 https://github.com/siyuan-note/siyuan/issues/18035scrollLeft = blockElement.firstElementChild.scrollLeft;scrollTop = blockElement.firstElementChild.scrollTop;+ contentScrollTop = protyle.contentElement.scrollTop;}if (!/<span data-type="backslash">.{1,8}<\/span><wbr>/.test(html)) {// 使用 md 闭合后继续输入应为普通文本, 转义不需要添加 zwsp@@ -315,6 +317,13 @@ export const input = async (protyle: IProtyle, blockElement: HTMLElement, range:if (scrollTop > 0) {blockElement.firstElementChild.scrollTop = scrollTop;}+ // SpinBlockDOM 会生成新表格并替换旧节点,旧节点移除时外层编辑器的滚动锚点会失效,+ // 因此需在恢复光标后还原滚动位置+ // https://github.com/siyuan-note/siyuan/issues/18235+ if (contentScrollTop > 0) {+ protyle.contentElement.scrollTop = contentScrollTop;+ protyle.scroll.lastScrollTop = contentScrollTop - 1;+ }}}// https://github.com/siyuan-note/siyuan/issues/14766diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.tsindex ee3041ef8a..16f84a3ad0 100644--- a/app/src/protyle/wysiwyg/transaction.ts+++ b/app/src/protyle/wysiwyg/transaction.ts@@ -400,11 +400,16 @@ const updateBlock = (updateElements: Element[], protyle: IProtyle, operation: IO// 表格的横向、纵向滚动均发生在首个子节点(contenteditable 容器,overflow:auto)上,// 更新块后需一并还原,否则固定表头长表格撤销/重做会跳回开头// https://github.com/siyuan-note/siyuan/issues/3650 https://github.com/siyuan-note/siyuan/issues/18035+ // https://github.com/siyuan-note/siyuan/issues/18235let tableScrollLeft: number;let tableScrollTop: number;+ let contentScrollTop: number;if (item.classList.contains("table")) {tableScrollLeft = (item.firstElementChild as HTMLElement).scrollLeft;tableScrollTop = (item.firstElementChild as HTMLElement).scrollTop;+ if (isRangeBlock) {+ contentScrollTop = protyle.contentElement.scrollTop;+ }}item.insertAdjacentHTML("afterend",// 图标撤销后无法渲染@@ -421,13 +426,17 @@ const updateBlock = (updateElements: Element[], protyle: IProtyle, operation: IO}}wbrElement?.remove();- // 聚焦后再还原滚动,避免 focusByWbr/focusBlock 改变滚动位置导致表格跳回开头+ // update 操作会生成新表格并替换旧节点,聚焦后需还原滚动,避免表格跳回开头if (tableScrollLeft > 0) {(item.firstElementChild as HTMLElement).scrollLeft = tableScrollLeft;}if (tableScrollTop > 0) {(item.firstElementChild as HTMLElement).scrollTop = tableScrollTop;}+ if (contentScrollTop > 0) {+ protyle.contentElement.scrollTop = contentScrollTop;+ protyle.scroll.lastScrollTop = contentScrollTop - 1;+ }processRender(item);highlightRender(item);