From 1d1e224dd20f362f763cc465376ed7d11afe2f61 Mon Sep 17 00:00:00 2001 From: Vanessa Date: Mon, 10 Oct 2022 19:12:24 +0800 Subject: [PATCH] :art: fix https://github.com/siyuan-note/siyuan/issues/6141 --- app/src/protyle/util/table.ts | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/app/src/protyle/util/table.ts b/app/src/protyle/util/table.ts index 60cb3eb68..556aaa0cb 100644 --- a/app/src/protyle/util/table.ts +++ b/app/src/protyle/util/table.ts @@ -65,7 +65,7 @@ export const setTableAlign = (protyle: IProtyle, cellElements: HTMLElement[], no }); } updateTransaction(protyle, nodeElement.getAttribute("data-node-id"), nodeElement.outerHTML, html); - nodeElement.querySelector("wbr").remove(); + focusByWbr(tableElement, range) }; export const insertRow = (protyle: IProtyle, range: Range, cellElement: HTMLElement, nodeElement: Element) => { @@ -203,13 +203,19 @@ export const deleteRow = (protyle: IProtyle, range: Range, cellElement: HTMLElem }; export const deleteColumn = (protyle: IProtyle, range: Range, nodeElement: Element, cellElement: HTMLElement) => { - range.insertNode(document.createElement("wbr")); + const wbrElement = document.createElement("wbr"); + range.insertNode(wbrElement); const html = nodeElement.outerHTML; + wbrElement.remove(); const index = getColIndex(cellElement); - if (cellElement.previousElementSibling || cellElement.nextElementSibling) { - range.selectNodeContents(cellElement.previousElementSibling || cellElement.nextElementSibling); + const sideCellElement = (cellElement.previousElementSibling || cellElement.nextElementSibling) as HTMLElement + if (sideCellElement) { + range.selectNodeContents(sideCellElement); range.collapse(true); - range.insertNode(document.createElement("wbr")); + // 滚动条横向定位 + if (sideCellElement.offsetLeft + sideCellElement.clientWidth > nodeElement.firstElementChild.scrollLeft + nodeElement.firstElementChild.clientWidth) { + nodeElement.firstElementChild.scrollLeft = sideCellElement.offsetLeft + sideCellElement.clientWidth - nodeElement.firstElementChild.clientWidth; + } } const tableElement = nodeElement.querySelector("table"); for (let i = 0; i < tableElement.rows.length; i++) { @@ -222,7 +228,7 @@ export const deleteColumn = (protyle: IProtyle, range: Range, nodeElement: Eleme } nodeElement.querySelectorAll("col")[index]?.remove(); updateTransaction(protyle, nodeElement.getAttribute("data-node-id"), nodeElement.outerHTML, html); - focusByWbr(nodeElement, range); + focusByRange(range); }; export const moveRowToUp = (protyle: IProtyle, range: Range, cellElement: HTMLElement, nodeElement: Element) => { @@ -251,6 +257,7 @@ export const moveRowToUp = (protyle: IProtyle, range: Range, cellElement: HTMLEl } updateTransaction(protyle, nodeElement.getAttribute("data-node-id"), nodeElement.outerHTML, html); focusByWbr(nodeElement, range); + scrollCenter(protyle, rowElement); }; export const moveRowToDown = (protyle: IProtyle, range: Range, cellElement: HTMLElement, nodeElement: Element) => { @@ -280,6 +287,7 @@ export const moveRowToDown = (protyle: IProtyle, range: Range, cellElement: HTML } updateTransaction(protyle, nodeElement.getAttribute("data-node-id"), nodeElement.outerHTML, html); focusByWbr(nodeElement, range); + scrollCenter(protyle, rowElement); }; export const moveColumnToLeft = (protyle: IProtyle, range: Range, cellElement: HTMLElement, nodeElement: Element) => { @@ -295,9 +303,14 @@ export const moveColumnToLeft = (protyle: IProtyle, range: Range, cellElement: H return true; } }); + nodeElement.querySelectorAll("tr").forEach((trElement) => { trElement.cells[cellIndex].after(trElement.cells[cellIndex - 1]); }); + // 滚动条横向定位 + if (cellElement.offsetLeft < nodeElement.firstElementChild.scrollLeft) { + nodeElement.firstElementChild.scrollLeft = cellElement.offsetLeft; + } const colElements = nodeElement.querySelectorAll("col"); colElements[cellIndex].after(colElements[cellIndex - 1]); updateTransaction(protyle, nodeElement.getAttribute("data-node-id"), nodeElement.outerHTML, html); @@ -320,6 +333,10 @@ export const moveColumnToRight = (protyle: IProtyle, range: Range, cellElement: nodeElement.querySelectorAll("tr").forEach((trElement) => { trElement.cells[cellIndex].before(trElement.cells[cellIndex + 1]); }); + // 滚动条横向定位 + if (cellElement.offsetLeft + cellElement.clientWidth > nodeElement.firstElementChild.scrollLeft + nodeElement.firstElementChild.clientWidth) { + nodeElement.firstElementChild.scrollLeft = cellElement.offsetLeft + cellElement.clientWidth - nodeElement.firstElementChild.clientWidth; + } const colElements = nodeElement.querySelectorAll("col"); colElements[cellIndex].before(colElements[cellIndex + 1]); updateTransaction(protyle, nodeElement.getAttribute("data-node-id"), nodeElement.outerHTML, html);