This commit is contained in:
Vanessa 2022-10-17 23:18:24 +08:00
parent 786a9ecedb
commit 9f0fcd8cbc
2 changed files with 23 additions and 4 deletions

View file

@ -157,6 +157,15 @@ export const setFontStyle = (textElement: HTMLElement, textOption: ITextOption)
textElement.setAttribute("data-subtype", blockRefData[1]);
textElement.innerText = blockRefData[2];
};
const setLink = (textOption: string) => {
const options = textOption.split(Constants.ZWSP)
textElement.setAttribute("data-href", options[0]);
textElement.removeAttribute("data-subtype");
textElement.removeAttribute("data-id");
if (options[1]) {
textElement.textContent = options[1];
}
};
if (textOption) {
switch (textOption.type) {
@ -188,9 +197,7 @@ export const setFontStyle = (textElement: HTMLElement, textOption: ITextOption)
textElement.textContent = "";
break;
case "a":
textElement.setAttribute("data-href", textOption.color);
textElement.removeAttribute("data-subtype");
textElement.removeAttribute("data-id");
setLink(textOption.color);
break;
case "inline-memo":
textElement.removeAttribute("contenteditable");

View file

@ -3,6 +3,7 @@ import {linkMenu} from "../../menus/protyle";
import {hasClosestBlock, hasClosestByAttribute} from "../util/hasClosest";
import {focusByRange, focusByWbr} from "../util/selection";
import {readText} from "../util/compatibility";
import {Constants} from "../../constants";
export class Link extends ToolbarItem {
public element: HTMLElement;
@ -27,20 +28,31 @@ export class Link extends ToolbarItem {
const rangeString = range.toString().trim();
let dataHref = "";
let dataText = "";
try {
const clipText = await readText();
// 选中链接时需忽略剪切板内容 https://ld246.com/article/1643035329737
if (protyle.lute.IsValidLinkDest(rangeString)) {
dataHref = rangeString;
} else if (protyle.lute.IsValidLinkDest(clipText)) {
dataHref = clipText;
} else {
// 360
const lastSpace = clipText.lastIndexOf(' ')
if (lastSpace > -1) {
if (protyle.lute.IsValidLinkDest(clipText.substring(lastSpace))) {
dataHref = clipText.substring(lastSpace);
dataText = clipText.substring(0, lastSpace);
}
}
}
} catch (e) {
console.log(e);
}
protyle.toolbar.setInlineMark(protyle, "a", "range", {
type: "a",
color: dataHref
color: dataHref + (dataText ? Constants.ZWSP + dataText : "")
});
});
}