diff --git a/app/src/protyle/render/av/action.ts b/app/src/protyle/render/av/action.ts index ede01846d..06e26c043 100644 --- a/app/src/protyle/render/av/action.ts +++ b/app/src/protyle/render/av/action.ts @@ -25,6 +25,7 @@ import * as dayjs from "dayjs"; import {openCalcMenu} from "./calc"; import {avRender} from "./render"; import {addView, openViewMenu} from "./view"; +import {writeText} from "../../util/compatibility"; export const avClick = (protyle: IProtyle, event: MouseEvent & { target: HTMLElement }) => { const blockElement = hasClosestBlock(event.target); @@ -41,7 +42,7 @@ export const avClick = (protyle: IProtyle, event: MouseEvent & { target: HTMLEle const copyElement = hasClosestByAttribute(event.target, "data-type", "copy"); if (copyElement) { - getCellText(hasClosestByClassName(copyElement, "av__cell")); + writeText(getCellText(hasClosestByClassName(copyElement, "av__cell"))); showMessage(window.siyuan.languages.copied); event.preventDefault(); event.stopPropagation(); diff --git a/app/src/protyle/render/av/cell.ts b/app/src/protyle/render/av/cell.ts index 0b7e170b0..21bdf3ff9 100644 --- a/app/src/protyle/render/av/cell.ts +++ b/app/src/protyle/render/av/cell.ts @@ -2,7 +2,7 @@ import {transaction} from "../../wysiwyg/transaction"; import {hasClosestBlock, hasClosestByClassName} from "../../util/hasClosest"; import {openMenuPanel} from "./openMenuPanel"; import {updateAttrViewCellAnimation} from "./action"; -import {isNotCtrl, writeText} from "../../util/compatibility"; +import {isNotCtrl} from "../../util/compatibility"; import {objEquals} from "../../../util/functions"; import {fetchPost} from "../../../util/fetch"; import {focusBlock} from "../../util/selection"; @@ -10,19 +10,21 @@ import * as dayjs from "dayjs"; export const getCellText = (cellElement: HTMLElement | false) => { if (!cellElement) { - return + return ""; } + let cellText = "" const textElement = cellElement.querySelector(".av__celltext"); if (textElement) { if (textElement.querySelector(".av__cellicon")) { - writeText(`${textElement.firstChild.textContent} → ${textElement.lastChild.textContent}`); + cellText = `${textElement.firstChild.textContent} → ${textElement.lastChild.textContent}`; } else { - writeText(textElement.textContent); + cellText = textElement.textContent; } } else { - writeText(cellElement.textContent); + cellText = cellElement.textContent; } -} + return cellText; +}; const genCellValueByElement = (colType: TAVCol, cellElement: HTMLElement) => { let cellValue: IAVCellValue; @@ -55,7 +57,6 @@ const genCellValueByElement = (colType: TAVCol, cellElement: HTMLElement) => { mSelect }; } else if (["date", "created", "updated"].includes(colType)) { - debugger; cellValue = { type: colType, [colType]: JSON.parse(cellElement.querySelector(".av__celltext").getAttribute("data-value")) @@ -97,7 +98,7 @@ export const genCellValue = (colType: TAVCol, value: string | any) => { type: colType, mSelect: [{ content: value, - color: "" + color: value ? "1" : "" }] }; } else if (["date", "created", "updated"].includes(colType) && value === "") { @@ -435,7 +436,7 @@ const updateCellValueByInput = (protyle: IProtyle, type: TAVCol, cellElements: H }); }; -export const updateCellsValue = (protyle: IProtyle, nodeElement: HTMLElement) => { +export const updateCellsValue = (protyle: IProtyle, nodeElement: HTMLElement, value = "") => { const doOperations: IOperation[] = [] const undoOperations: IOperation[] = [] @@ -471,7 +472,7 @@ export const updateCellsValue = (protyle: IProtyle, nodeElement: HTMLElement) => avID, keyID: colId, rowID, - data: genCellValue(type, "") + data: genCellValue(type, value) }); undoOperations.push({ action: "updateAttrViewCell", diff --git a/app/src/protyle/render/av/keydown.ts b/app/src/protyle/render/av/keydown.ts index 5742ae918..bcc5d79f0 100644 --- a/app/src/protyle/render/av/keydown.ts +++ b/app/src/protyle/render/av/keydown.ts @@ -1,6 +1,6 @@ import {matchHotKey} from "../../util/hotKey"; import {selectRow} from "./row"; -import {cellScrollIntoView, popTextCell} from "./cell"; +import {cellScrollIntoView, popTextCell, updateCellsValue} from "./cell"; import {avContextmenu} from "./action"; import {hasClosestByClassName} from "../../util/hasClosest"; import {Constants} from "../../../constants"; @@ -34,6 +34,11 @@ export const avKeydown = (event: KeyboardEvent, nodeElement: HTMLElement, protyl event.preventDefault(); return true; } + if (event.key === "Backspace") { + updateCellsValue(protyle, nodeElement); + event.preventDefault(); + return true; + } let newCellElement; if (event.key === "ArrowLeft" || matchHotKey("⇧⇥", event)) { const previousRowElement = rowElement.previousElementSibling; diff --git a/app/src/protyle/util/insertHTML.ts b/app/src/protyle/util/insertHTML.ts index 556c54357..a90b90a17 100644 --- a/app/src/protyle/util/insertHTML.ts +++ b/app/src/protyle/util/insertHTML.ts @@ -9,6 +9,17 @@ import {highlightRender} from "../render/highlightRender"; import {scrollCenter} from "../../util/highlightById"; import {updateAVName} from "../render/av/action"; import {readText} from "./compatibility"; +import {updateCellsValue} from "../render/av/cell"; + +const processAV = (range: Range, text: string, protyle: IProtyle, blockElement: Element) => { + if (blockElement.querySelector(".av__cell--select, .av__row--select")) { + updateCellsValue(protyle, blockElement as HTMLElement, text); + } else { + range.insertNode(document.createTextNode(text)); + range.collapse(false); + updateAVName(protyle, blockElement); + } +}; export const insertHTML = (html: string, protyle: IProtyle, isBlock = false, // 移动端插入嵌入块时,获取到的 range 为旧值 @@ -43,14 +54,10 @@ export const insertHTML = (html: string, protyle: IProtyle, isBlock = false, range.deleteContents(); const text = readText(); if (typeof text === "string") { - range.insertNode(document.createTextNode(text)); - range.collapse(false); - updateAVName(protyle, blockElement); + processAV(range, text, protyle, blockElement) } else { text.then((t) => { - range.insertNode(document.createTextNode(t)); - range.collapse(false); - updateAVName(protyle, blockElement); + processAV(range, t, protyle, blockElement) }); } return;