Vanessa 2024-01-15 00:20:15 +08:00
parent f40529c362
commit 9831a3d2c6
2 changed files with 9 additions and 32 deletions

View File

@ -1,14 +1,14 @@
import {hasClosestBlock, hasClosestByAttribute, hasClosestByClassName, hasClosestByMatchTag} from "./hasClosest"; import {hasClosestBlock, hasClosestByAttribute, hasClosestByClassName, hasClosestByMatchTag} from "./hasClosest";
import * as dayjs from "dayjs"; import * as dayjs from "dayjs";
import {transaction, updateTransaction} from "../wysiwyg/transaction"; import {transaction, updateTransaction} from "../wysiwyg/transaction";
import {getContenteditableElement, hasNextSibling, hasPreviousSibling} from "../wysiwyg/getBlock"; import {getContenteditableElement} from "../wysiwyg/getBlock";
import {fixTableRange, focusBlock, focusByWbr, getEditorRange} from "./selection"; import {fixTableRange, focusBlock, focusByWbr, getEditorRange} from "./selection";
import {mathRender} from "../render/mathRender";
import {Constants} from "../../constants"; import {Constants} from "../../constants";
import {highlightRender} from "../render/highlightRender"; import {highlightRender} from "../render/highlightRender";
import {scrollCenter} from "../../util/highlightById"; import {scrollCenter} from "../../util/highlightById";
import {updateAVName} from "../render/av/action"; import {updateAVName} from "../render/av/action";
import {updateCellsValue} from "../render/av/cell"; import {updateCellsValue} from "../render/av/cell";
import {input} from "../wysiwyg/input";
const processAV = (range: Range, html: string, protyle: IProtyle, blockElement: Element) => { const processAV = (range: Range, html: string, protyle: IProtyle, blockElement: Element) => {
const text = protyle.lute.BlockDOM2EscapeMarkerContent(html); const text = protyle.lute.BlockDOM2EscapeMarkerContent(html);
@ -145,36 +145,8 @@ export const insertHTML = (html: string, protyle: IProtyle, isBlock = false,
} }
range.insertNode(tempElement.content.cloneNode(true)); range.insertNode(tempElement.content.cloneNode(true));
range.collapse(false); range.collapse(false);
blockElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss")); blockElement.querySelector("wbr")?.remove();
// 使用 innerHTML,避免行内元素为代码块 input(protyle, blockElement as HTMLElement, range);
const trimStartText = editableElement ? editableElement.innerHTML.trimStart() : "";
if (editableElement && (trimStartText.startsWith("```") || trimStartText.startsWith("~~~") || trimStartText.startsWith("···") ||
trimStartText.indexOf("\n```") > -1 || trimStartText.indexOf("\n~~~") > -1 || trimStartText.indexOf("\n···") > -1)) {
if (trimStartText.indexOf("\n") === -1 && trimStartText.replace(/·|~/g, "`").replace(/^`{3,}/g, "").indexOf("`") > -1) {
// ```test` 不处理
} else {
let replaceInnerHTML = editableElement.innerHTML.replace(/^(~|·|`){3,}/g, "```").replace(/\n(~|·|`){3,}/g, "\n```").trim();
if (!replaceInnerHTML.endsWith("\n```")) {
replaceInnerHTML += "\n```";
}
const languageIndex = replaceInnerHTML.indexOf("```") + 3;
replaceInnerHTML = replaceInnerHTML.substring(0, languageIndex) + (localStorage["local-codelang"] || "") + replaceInnerHTML.substring(languageIndex);
editableElement.innerHTML = replaceInnerHTML;
}
}
const editWbrElement = editableElement.querySelector("wbr");
if (editWbrElement && editableElement && !trimStartText.endsWith("\n")) {
// 数学公式后无换行,后期渲染后添加导致 rang 错误,中文输入错误 https://github.com/siyuan-note/siyuan/issues/9054
const previousElement = hasPreviousSibling(editWbrElement) as HTMLElement;
if (previousElement && previousElement.nodeType !== 3 && (previousElement.dataset.type || "").indexOf("inline-math") > -1 &&
!hasNextSibling(editWbrElement)) {
editWbrElement.insertAdjacentText("afterend", "\n");
}
}
mathRender(blockElement);
updateTransaction(protyle, id, blockElement.outerHTML, oldHTML);
focusByWbr(protyle.wysiwyg.element, range);
return; return;
} }
} }

View File

@ -582,6 +582,11 @@ export const focusBlock = (element: Element, parentElement?: HTMLElement, toStar
if (cursorElement.classList.contains("hljs")) { if (cursorElement.classList.contains("hljs")) {
// 代码块末尾定位需在 /n 之前 https://github.com/siyuan-note/siyuan/issues/9141https://github.com/siyuan-note/siyuan/issues/9189 // 代码块末尾定位需在 /n 之前 https://github.com/siyuan-note/siyuan/issues/9141https://github.com/siyuan-note/siyuan/issues/9189
let lastNode = cursorElement.lastChild; let lastNode = cursorElement.lastChild;
if (!lastNode) {
// 粘贴 ``` 报错
cursorElement.innerHTML = "\n";
lastNode = cursorElement.lastChild;
}
if (lastNode.textContent === "" && lastNode.nodeType === 3) { if (lastNode.textContent === "" && lastNode.nodeType === 3) {
lastNode = hasPreviousSibling(cursorElement.lastChild) as HTMLElement; lastNode = hasPreviousSibling(cursorElement.lastChild) as HTMLElement;
} }