diff --git a/app/src/protyle/toolbar/Font.ts b/app/src/protyle/toolbar/Font.ts index dd782e42b..10e4ef908 100644 --- a/app/src/protyle/toolbar/Font.ts +++ b/app/src/protyle/toolbar/Font.ts @@ -187,6 +187,16 @@ export const setFontStyle = (textElement: HTMLElement, textOption: ITextOption) textElement.removeAttribute("data-render"); textElement.textContent = ""; break; + case "a": + textElement.setAttribute("data-href", textOption.color); + textElement.removeAttribute("data-subtype"); + textElement.removeAttribute("data-id"); + break; + case "inline-memo": + textElement.removeAttribute("contenteditable"); + textElement.removeAttribute("data-subtype"); + textElement.removeAttribute("data-content"); + break; } } }; @@ -195,7 +205,7 @@ export const hasSameTextStyle = (currentElement: HTMLElement, sideElement: HTMLE if (!textObj) { return true; } - if (textObj.type === "inline-math") { + if (textObj.type === "inline-math" || textObj.type === "inline-memo" || textObj.type === "a") { return false; } if (textObj.type === "id") { diff --git a/app/src/protyle/toolbar/Link.ts b/app/src/protyle/toolbar/Link.ts index 8042ef5c3..cd94e931b 100644 --- a/app/src/protyle/toolbar/Link.ts +++ b/app/src/protyle/toolbar/Link.ts @@ -28,51 +28,23 @@ export class Link extends ToolbarItem { return; } - fixTableRange(range); - - if (!["DIV", "TD", "TH", "TR"].includes(range.startContainer.parentElement.tagName) && range.startOffset === 0 && !hasPreviousSibling(range.startContainer)) { - range.setStartBefore(range.startContainer.parentElement); - } - if (!["DIV", "TD", "TH", "TR"].includes(range.endContainer.parentElement.tagName) && range.endOffset === range.endContainer.textContent.length && !hasNextSibling(range.endContainer)) { - range.setEndAfter(range.endContainer.parentElement); - } - const wbrElement = document.createElement("wbr"); - range.insertNode(wbrElement); - const html = nodeElement.outerHTML; - - const newElement = document.createElement("span"); - newElement.setAttribute("data-type", "a"); - newElement.setAttribute("data-href", ""); - const rangeString = range.toString(); - newElement.textContent = rangeString; - range.extractContents(); - range.insertNode(newElement); - let needShowLink = true; - let focusText = false; + const rangeString = range.toString().trim() + let dataHref = ""; try { const clipText = await navigator.clipboard.readText(); // 选中链接时需忽略剪切板内容 https://ld246.com/article/1643035329737 - if (protyle.lute.IsValidLinkDest(rangeString.trim())) { - (newElement as HTMLElement).setAttribute("data-href", rangeString.trim()); - needShowLink = false; + if (protyle.lute.IsValidLinkDest(rangeString)) { + dataHref = rangeString; } else if (protyle.lute.IsValidLinkDest(clipText)) { - (newElement as HTMLElement).setAttribute("data-href", clipText); - if (newElement.textContent.replace(Constants.ZWSP, "") !== "") { - needShowLink = false; - } - focusText = true; + dataHref = clipText; } } catch (e) { console.log(e); } - if (needShowLink) { - linkMenu(protyle, newElement as HTMLElement, focusText); - } - nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss")); - updateTransaction(protyle, nodeElement.getAttribute("data-node-id"), nodeElement.outerHTML, html); - range.setStartAfter(newElement); - range.collapse(true); - wbrElement.remove(); + protyle.toolbar.setInlineMark(protyle, "a", "range", { + type: "a", + color: dataHref + }); }); } } diff --git a/app/src/protyle/toolbar/index.ts b/app/src/protyle/toolbar/index.ts index 5511535c0..e4616c982 100644 --- a/app/src/protyle/toolbar/index.ts +++ b/app/src/protyle/toolbar/index.ts @@ -43,6 +43,7 @@ import {showMessage} from "../../dialog/message"; import {InlineMath} from "./InlineMath"; import {InlineMemo} from "./InlineMemo"; import {mathRender} from "../markdown/mathRender"; +import {linkMenu} from "../../menus/protyle"; export class Toolbar { public element: HTMLElement; @@ -426,10 +427,18 @@ export class Toolbar { return true; } }); - } else if (type === "block-ref" && types.includes("virtual-block-ref")) { - // 虚拟引用和引用不能同时存在 + } else if (type === "block-ref" && (types.includes("virtual-block-ref") || types.includes("a"))) { + // 虚拟引用和引用、链接不能同时存在 types.find((item, index) => { - if (item === "virtual-block-ref") { + if (item === "virtual-block-ref" || item === "a") { + types.splice(index, 1); + return true; + } + }); + } else if (type === "a" && (types.includes("virtual-block-ref") || types.includes("block-ref"))) { + // 链接和引用、虚拟引用不能同时存在 + types.find((item, index) => { + if (item === "virtual-block-ref" || item === "block-ref") { types.splice(index, 1); return true; } @@ -574,6 +583,12 @@ export class Toolbar { protyle.toolbar.showRender(protyle, newNodes[0] as HTMLElement, newNodes as Element[], html); return; } + if (type === "a") { + const aElement = newNodes[0] as HTMLElement + if (aElement.textContent.replace(Constants.ZWSP, "") === "" || !aElement.getAttribute("data-href")) { + linkMenu(protyle, aElement, aElement.getAttribute("data-href") ? true : false); + } + } nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss")); updateTransaction(protyle, nodeElement.getAttribute("data-node-id"), nodeElement.outerHTML, html); const wbrElement = nodeElement.querySelector("wbr");