import { getTopList, hasClosestBlock, hasClosestByAttribute, hasTopClosestByTag, } from "../util/hasClosest"; import { hasClosestByTag} from "../util/hasClosestByHeadings"; import {log} from "../util/log"; import {processCodeRender} from "../util/processCode"; import {setRangeByWbr} from "../util/selection"; import {renderToc} from "../util/toc"; import {afterRenderEvent} from "./afterRenderEvent"; import {previoueIsEmptyA} from "./inlineTag"; export const input = (vditor: IVditor, range: Range, event?: InputEvent | InsertValueEvent, insertValue?: string) => { let blockElement = hasClosestBlock(range.startContainer); if (!blockElement) { // 使用顶级块元素,应使用 innerHTML blockElement = vditor.wysiwyg.element; } if (event && event.inputType !== "formatItalic" && event.inputType !== "deleteByDrag" && event.inputType !== "insertFromDrop" && event.inputType !== "formatBold" && event.inputType !== "formatRemove" && event.inputType !== "formatStrikeThrough" && event.inputType !== "insertUnorderedList" && event.inputType !== "insertOrderedList" && event.inputType !== "formatOutdent" && event.inputType !== "formatIndent" && event.inputType !== "" // document.execCommand('unlink', false) || !event ) { const previousAEmptyElement = previoueIsEmptyA(range.startContainer); if (previousAEmptyElement) { // 链接结尾回车不应该复制到下一行 https://github.com/Vanessa219/vditor/issues/163 previousAEmptyElement.remove(); } // 保存光标 vditor.wysiwyg.element.querySelectorAll("wbr").forEach((wbr) => { wbr.remove(); }); range.insertNode(document.createElement("wbr")); // 在行首进行删除,后面的元素会带有样式,需清除 blockElement.querySelectorAll("[style]").forEach((item) => { item.removeAttribute("style"); }); // 移除空评论 blockElement.querySelectorAll(".vditor-comment").forEach((item) => { if (item.textContent.trim() === "") { item.classList.remove("vditor-comment", "vditor-comment--focus"); item.removeAttribute("data-cmtids"); } }); // 在有评论的行首换行后,该行的前一段会带有评论标识 blockElement.previousElementSibling?.querySelectorAll(".vditor-comment").forEach((item) => { if (item.textContent.trim() === "") { item.classList.remove("vditor-comment", "vditor-comment--focus"); item.removeAttribute("data-cmtids"); } }); let html = ""; if (blockElement.getAttribute("data-type") === "link-ref-defs-block") { // 修改链接引用 blockElement = vditor.wysiwyg.element; } const isWYSIWYGElement = blockElement.isEqualNode(vditor.wysiwyg.element); const footnoteElement = hasClosestByAttribute(blockElement, "data-type", "footnotes-block"); if (!isWYSIWYGElement) { // 列表需要到最顶层 const topListElement = getTopList(range.startContainer); if (topListElement && !footnoteElement) { const blockquoteElement = hasClosestByTag(range.startContainer, "BLOCKQUOTE"); if (blockquoteElement) { // li 中有 blockquote 就只渲染 blockquote blockElement = hasClosestBlock(range.startContainer) || blockElement; } else { blockElement = topListElement; } } // 修改脚注 if (footnoteElement) { blockElement = footnoteElement; } html = blockElement.outerHTML; if (blockElement.tagName === "UL" || blockElement.tagName === "OL") { // 如果为列表的话,需要把上下的列表都重绘 const listPrevElement = blockElement.previousElementSibling; const listNextElement = blockElement.nextElementSibling; if (listPrevElement && (listPrevElement.tagName === "UL" || listPrevElement.tagName === "OL")) { html = listPrevElement.outerHTML + html; listPrevElement.remove(); } if (listNextElement && (listNextElement.tagName === "UL" || listNextElement.tagName === "OL")) { html = html + listNextElement.outerHTML; listNextElement.remove(); } // firefox 列表回车不会产生新的 list item https://github.com/Vanessa219/vditor/issues/194 html = html.replace("
)(\u200b*)(\s*)(.*)( )( ``` ```
)/, (p1, p2, p3, p4) => {
// return p2 + ' ' + p3 + p4;
// });
if (html === '