This commit is contained in:
Vanessa 2023-10-17 11:37:05 +08:00
parent 505d339f6e
commit 9a814f58f0
3 changed files with 43 additions and 30 deletions

View file

@ -38,7 +38,7 @@ import {renameAsset} from "../editor/rename";
import {electronUndo} from "../protyle/undo";
import {pushBack} from "../mobile/util/MobileBackFoward";
import {copyPNG, exportAsset} from "./util";
import {removeLink} from "../protyle/toolbar/Link";
import {removeInlineType} from "../protyle/toolbar/util";
import {alignImgCenter, alignImgLeft} from "../protyle/wysiwyg/commonHotkey";
import {renameTag} from "../util/noRelyPCFunction";
import {hideElements} from "../protyle/ui/hideElements";
@ -58,9 +58,11 @@ export const fileAnnotationRefMenu = (protyle: IProtyle, refElement: HTMLElement
window.siyuan.menus.menu.remove();
let anchorElement: HTMLInputElement;
window.siyuan.menus.menu.append(new MenuItem({
iconHTML: "",
label: `<input style="margin: 4px 0" class="b3-text-field fn__block" value="${refElement.getAttribute("data-id") || ""}" readonly placeholder="ID">`,
}).element);
window.siyuan.menus.menu.append(new MenuItem({
iconHTML: "",
label: `<input style="margin: 4px 0" class="b3-text-field fn__block" data-type="anchor" placeholder="${window.siyuan.languages.anchor}">`,
bind(menuItemElement) {
anchorElement = menuItemElement.querySelector("input");
@ -105,6 +107,15 @@ export const fileAnnotationRefMenu = (protyle: IProtyle, refElement: HTMLElement
refElement.outerHTML = refElement.textContent + "<wbr>";
}
}).element);
window.siyuan.menus.menu.append(new MenuItem({
label: `${window.siyuan.languages.turnInto} <b>${window.siyuan.languages.text}</b>`,
icon: "iconRefresh",
click() {
const html = nodeElement.outerHTML;
removeInlineType(refElement, protyle.toolbar.range);
updateTransaction(protyle, id, nodeElement.outerHTML, html);
}
}).element);
if (protyle?.app?.plugins) {
emitOpenMenu({
@ -950,12 +961,12 @@ export const linkMenu = (protyle: IProtyle, linkElement: HTMLElement, focusText
}
inputElement.value = anchor;
inputElement.addEventListener("compositionend", () => {
linkElement.innerHTML = Lute.EscapeHTMLStr(inputElement.value.replace(/\n|\r\n|\r|\u2028|\u2029/g, "") || "");
linkElement.innerHTML = Lute.EscapeHTMLStr(inputElement.value.replace(/\n|\r\n|\r|\u2028|\u2029/g, "") || "*");
});
inputElement.addEventListener("input", (event: KeyboardEvent) => {
if (!event.isComposing) {
// https://github.com/siyuan-note/siyuan/issues/4511
linkElement.innerHTML = Lute.EscapeHTMLStr(inputElement.value.replace(/\n|\r\n|\r|\u2028|\u2029/g, "")) || "";
linkElement.innerHTML = Lute.EscapeHTMLStr(inputElement.value.replace(/\n|\r\n|\r|\u2028|\u2029/g, "")) || "*";
}
});
inputElement.addEventListener("keydown", (event) => {
@ -1014,7 +1025,7 @@ export const linkMenu = (protyle: IProtyle, linkElement: HTMLElement, focusText
label: `${window.siyuan.languages.turnInto} <b>${window.siyuan.languages.text}</b>`,
icon: "iconRefresh",
click() {
removeLink(linkElement, protyle.toolbar.range);
removeInlineType(linkElement, protyle.toolbar.range);
updateTransaction(protyle, id, nodeElement.outerHTML, html);
}
}).element);
@ -1088,7 +1099,7 @@ export const linkMenu = (protyle: IProtyle, linkElement: HTMLElement, focusText
linkElement.setAttribute("data-href", Lute.EscapeHTMLStr(textElements[0].value.replace(/\n|\r\n|\r|\u2028|\u2029/g, "")));
const currentRange = getSelection().rangeCount === 0 ? undefined : getSelection().getRangeAt(0);
if (linkElement.textContent === "" || linkElement.textContent === Constants.ZWSP) {
removeLink(linkElement, (currentRange && !protyle.element.contains(currentRange.startContainer)) ? protyle.toolbar.range : undefined);
removeInlineType(linkElement, (currentRange && !protyle.element.contains(currentRange.startContainer)) ? protyle.toolbar.range : undefined);
} else if (currentRange && !protyle.element.contains(currentRange.startContainer)) {
protyle.toolbar.range.selectNodeContents(linkElement);
protyle.toolbar.range.collapse(false);

View file

@ -56,28 +56,3 @@ export class Link extends ToolbarItem {
});
}
}
export const removeLink = (linkElement: HTMLElement, range?: Range) => {
const types = linkElement.getAttribute("data-type").split(" ");
if (types.length === 1) {
const linkParentElement = linkElement.parentElement;
linkElement.outerHTML = linkElement.innerHTML.replace(Constants.ZWSP, "") + "<wbr>";
if (range) {
focusByWbr(linkParentElement, range);
}
} else {
types.find((itemType, index) => {
if ("a" === itemType) {
types.splice(index, 1);
return true;
}
});
linkElement.setAttribute("data-type", types.join(" "));
linkElement.removeAttribute("data-href");
if (range) {
range.selectNodeContents(linkElement);
range.collapse(false);
focusByRange(range);
}
}
};

View file

@ -1,4 +1,6 @@
import {fetchPost} from "../../util/fetch";
import {Constants} from "../../constants";
import {focusByRange, focusByWbr} from "../util/selection";
export const previewTemplate = (pathString: string, element: Element, parentId: string) => {
if (!pathString) {
@ -60,3 +62,28 @@ export const removeSearchMark = (element: HTMLElement) => {
element.setAttribute("data-type", element.getAttribute("data-type").replace("search-mark", "").trim());
}
};
export const removeInlineType = (linkElement: HTMLElement, range?: Range) => {
const types = linkElement.getAttribute("data-type").split(" ");
if (types.length === 1) {
const linkParentElement = linkElement.parentElement;
linkElement.outerHTML = linkElement.innerHTML.replace(Constants.ZWSP, "") + "<wbr>";
if (range) {
focusByWbr(linkParentElement, range);
}
} else {
types.find((itemType, index) => {
if ("a" === itemType) {
types.splice(index, 1);
return true;
}
});
linkElement.setAttribute("data-type", types.join(" "));
linkElement.removeAttribute("data-href");
if (range) {
range.selectNodeContents(linkElement);
range.collapse(false);
focusByRange(range);
}
}
};