Skip to content

Commit

Permalink
🎨 #13027
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 committed Dec 4, 2024
1 parent a82b9c2 commit a5ca9f1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
24 changes: 24 additions & 0 deletions app/src/protyle/util/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {scrollCenter} from "../../util/highlightById";
import {insertEmptyBlock} from "../../block/util";
import {removeBlock} from "../wysiwyg/remove";
import {hasPreviousSibling} from "../wysiwyg/getBlock";
import * as dayjs from "dayjs";

const scrollToView = (nodeElement: Element, rowElement: HTMLElement, protyle: IProtyle) => {
if (nodeElement.getAttribute("custom-pinthead") === "true") {
Expand Down Expand Up @@ -721,3 +722,26 @@ export const fixTable = (protyle: IProtyle, event: KeyboardEvent, range: Range)
return true;
}
};

export const clearTableCell = (protyle: IProtyle, tableBlockElement: HTMLElement) => {
if (!tableBlockElement) {
return;
}
const tableSelectElement = tableBlockElement.querySelector(".table__select") as HTMLElement;
const selectCellElements: HTMLTableCellElement[] = [];
const scrollLeft = tableBlockElement.firstElementChild.scrollLeft;
tableBlockElement.querySelectorAll("th, td").forEach((item: HTMLTableCellElement) => {
if (!item.classList.contains("fn__none") &&
item.offsetLeft + 6 > tableSelectElement.offsetLeft + scrollLeft && item.offsetLeft + item.clientWidth - 6 < tableSelectElement.offsetLeft + scrollLeft + tableSelectElement.clientWidth &&
item.offsetTop + 6 > tableSelectElement.offsetTop && item.offsetTop + item.clientHeight - 6 < tableSelectElement.offsetTop + tableSelectElement.clientHeight) {
selectCellElements.push(item);
}
});
tableSelectElement.removeAttribute("style");
const oldHTML = tableBlockElement.outerHTML;
tableBlockElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
selectCellElements.forEach(item => {
item.innerHTML = "";
});
updateTransaction(protyle, tableBlockElement.getAttribute("data-node-id"), tableBlockElement.outerHTML, oldHTML);
}
22 changes: 3 additions & 19 deletions app/src/protyle/wysiwyg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ import {isInIOS, isMac, isOnlyMeta, readText} from "../util/compatibility";
import {MenuItem} from "../../menus/Menu";
import {fetchPost} from "../../util/fetch";
import {onGet} from "../util/onGet";
import {setTableAlign} from "../util/table";
import {clearTableCell, setTableAlign} from "../util/table";
import {countBlockWord, countSelectWord} from "../../layout/status";
import {showMessage} from "../../dialog/message";
import {getBacklinkHeadingMore, loadBreadcrumb} from "./renderBacklink";
Expand Down Expand Up @@ -1295,25 +1295,9 @@ export class WYSIWYG {
window.siyuan.menus.menu.append(new MenuItem({
label: window.siyuan.languages.clear,
icon: "iconTrashcan",
accelerator: "⌦",
click() {
if (tableBlockElement) {
const selectCellElements: HTMLTableCellElement[] = [];
const scrollLeft = tableBlockElement.firstElementChild.scrollLeft;
tableBlockElement.querySelectorAll("th, td").forEach((item: HTMLTableCellElement) => {
if (!item.classList.contains("fn__none") &&
item.offsetLeft + 6 > tableSelectElement.offsetLeft + scrollLeft && item.offsetLeft + item.clientWidth - 6 < tableSelectElement.offsetLeft + scrollLeft + tableSelectElement.clientWidth &&
item.offsetTop + 6 > tableSelectElement.offsetTop && item.offsetTop + item.clientHeight - 6 < tableSelectElement.offsetTop + tableSelectElement.clientHeight) {
selectCellElements.push(item);
}
});
tableSelectElement.removeAttribute("style");
const oldHTML = tableBlockElement.outerHTML;
tableBlockElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
selectCellElements.forEach(item => {
item.innerHTML = "";
});
updateTransaction(protyle, tableBlockElement.getAttribute("data-node-id"), tableBlockElement.outerHTML, oldHTML);
}
clearTableCell(protyle, tableBlockElement as HTMLElement)
}
}).element);
window.siyuan.menus.menu.append(new MenuItem({
Expand Down
8 changes: 7 additions & 1 deletion app/src/protyle/wysiwyg/keydown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
} from "./getBlock";
import {matchHotKey} from "../util/hotKey";
import {enter, softEnter} from "./enter";
import {fixTable} from "../util/table";
import {clearTableCell, fixTable} from "../util/table";
import {
turnsIntoOneTransaction,
turnsIntoTransaction,
Expand Down Expand Up @@ -808,6 +808,12 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
return;
}
} else if (selectText === "") {
if (nodeElement.classList.contains("table") && nodeElement.querySelector(".table__select").clientHeight > 0) {
clearTableCell(protyle, nodeElement);
event.stopPropagation();
event.preventDefault();
return;
}
const editElement = getContenteditableElement(nodeElement) as HTMLElement;
if (!editElement) {
nodeElement.classList.add("protyle-wysiwyg--select");
Expand Down

0 comments on commit a5ca9f1

Please sign in to comment.