This commit is contained in:
Vanessa 2023-09-30 19:25:20 +08:00
parent 82bed847e6
commit 6b2a4ff0aa

View File

@ -71,6 +71,21 @@ import {insertHTML} from "../util/insertHTML";
import {removeSearchMark} from "../toolbar/util"; import {removeSearchMark} from "../toolbar/util";
import {copyPNG} from "../../menus/util"; import {copyPNG} from "../../menus/util";
const getContentByInlineHTML = (range: Range, cb: (content: string) => void) => {
let html = "";
Array.from(range.cloneContents().childNodes).forEach((item: HTMLElement) => {
if (item.nodeType === 3) {
html += item.textContent
} else {
html += item.outerHTML
}
})
fetchPost("/api/block/getDOMText", {dom: html}, (response) => {
cb(response.data)
})
}
export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => { export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
editorElement.addEventListener("keydown", (event: KeyboardEvent & { target: HTMLElement }) => { editorElement.addEventListener("keydown", (event: KeyboardEvent & { target: HTMLElement }) => {
if (event.target.localName === "protyle-html") { if (event.target.localName === "protyle-html") {
@ -983,7 +998,9 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
// 用于标识复制文本 * // 用于标识复制文本 *
if (selectText !== "") { if (selectText !== "") {
// 和复制块引用保持一致 https://github.com/siyuan-note/siyuan/issues/9093 // 和复制块引用保持一致 https://github.com/siyuan-note/siyuan/issues/9093
writeText(`${Lute.EscapeHTMLStr(selectText)} ((${nodeElement.getAttribute("data-node-id")} "*"))`); getContentByInlineHTML(range, (content) => {
writeText(`${Lute.EscapeHTMLStr(content.trim())} ((${nodeElement.getAttribute("data-node-id")} "*"))`);
});
} else { } else {
nodeElement.setAttribute("data-reftext", "true"); nodeElement.setAttribute("data-reftext", "true");
focusByRange(getEditorRange(nodeElement)); focusByRange(getEditorRange(nodeElement));
@ -1010,7 +1027,9 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
} }
const actionElementId = actionElement.getAttribute("data-node-id"); const actionElementId = actionElement.getAttribute("data-node-id");
if (selectText !== "") { if (selectText !== "") {
writeText(`((${actionElementId} "${Lute.EscapeHTMLStr(selectText)}"))`); getContentByInlineHTML(range, (content) => {
writeText(`((${actionElementId} "${Lute.EscapeHTMLStr(content.trim())}"))`);
});
} else { } else {
fetchPost("/api/block/getRefText", {id: actionElementId}, (response) => { fetchPost("/api/block/getRefText", {id: actionElementId}, (response) => {
writeText(`((${actionElementId} '${response.data}'))`); writeText(`((${actionElementId} '${response.data}'))`);
@ -1044,16 +1063,18 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
} }
openAttr(actionElement, "bookmark", protyle); openAttr(actionElement, "bookmark", protyle);
} else { } else {
const oldHTML = topElement.outerHTML; getContentByInlineHTML(range, (content) => {
const name = Lute.EscapeHTMLStr(selectText); const oldHTML = topElement.outerHTML;
const nameElement = topElement.lastElementChild.querySelector(".protyle-attr--name"); const name = Lute.EscapeHTMLStr(content.trim());
if (nameElement) { const nameElement = topElement.lastElementChild.querySelector(".protyle-attr--name");
nameElement.innerHTML = `<svg><use xlink:href="#iconN"></use></svg>${name}`; if (nameElement) {
} else { nameElement.innerHTML = `<svg><use xlink:href="#iconN"></use></svg>${name}`;
topElement.lastElementChild.insertAdjacentHTML("afterbegin", `<div class="protyle-attr--name"><svg><use xlink:href="#iconN"></use></svg>${name}</div>`); } else {
} topElement.lastElementChild.insertAdjacentHTML("afterbegin", `<div class="protyle-attr--name"><svg><use xlink:href="#iconN"></use></svg>${name}</div>`);
topElement.setAttribute("name", name); }
updateTransaction(protyle, topElement.getAttribute("data-node-id"), topElement.outerHTML, oldHTML); topElement.setAttribute("name", name);
updateTransaction(protyle, topElement.getAttribute("data-node-id"), topElement.outerHTML, oldHTML);
});
} }
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();