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 {electronUndo} from "../protyle/undo";
import {pushBack} from "../mobile/util/MobileBackFoward"; import {pushBack} from "../mobile/util/MobileBackFoward";
import {copyPNG, exportAsset} from "./util"; 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 {alignImgCenter, alignImgLeft} from "../protyle/wysiwyg/commonHotkey";
import {renameTag} from "../util/noRelyPCFunction"; import {renameTag} from "../util/noRelyPCFunction";
import {hideElements} from "../protyle/ui/hideElements"; import {hideElements} from "../protyle/ui/hideElements";
@ -58,9 +58,11 @@ export const fileAnnotationRefMenu = (protyle: IProtyle, refElement: HTMLElement
window.siyuan.menus.menu.remove(); window.siyuan.menus.menu.remove();
let anchorElement: HTMLInputElement; let anchorElement: HTMLInputElement;
window.siyuan.menus.menu.append(new MenuItem({ 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">`, label: `<input style="margin: 4px 0" class="b3-text-field fn__block" value="${refElement.getAttribute("data-id") || ""}" readonly placeholder="ID">`,
}).element); }).element);
window.siyuan.menus.menu.append(new MenuItem({ 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}">`, label: `<input style="margin: 4px 0" class="b3-text-field fn__block" data-type="anchor" placeholder="${window.siyuan.languages.anchor}">`,
bind(menuItemElement) { bind(menuItemElement) {
anchorElement = menuItemElement.querySelector("input"); anchorElement = menuItemElement.querySelector("input");
@ -105,6 +107,15 @@ export const fileAnnotationRefMenu = (protyle: IProtyle, refElement: HTMLElement
refElement.outerHTML = refElement.textContent + "<wbr>"; refElement.outerHTML = refElement.textContent + "<wbr>";
} }
}).element); }).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) { if (protyle?.app?.plugins) {
emitOpenMenu({ emitOpenMenu({
@ -950,12 +961,12 @@ export const linkMenu = (protyle: IProtyle, linkElement: HTMLElement, focusText
} }
inputElement.value = anchor; inputElement.value = anchor;
inputElement.addEventListener("compositionend", () => { 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) => { inputElement.addEventListener("input", (event: KeyboardEvent) => {
if (!event.isComposing) { if (!event.isComposing) {
// https://github.com/siyuan-note/siyuan/issues/4511 // 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) => { 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>`, label: `${window.siyuan.languages.turnInto} <b>${window.siyuan.languages.text}</b>`,
icon: "iconRefresh", icon: "iconRefresh",
click() { click() {
removeLink(linkElement, protyle.toolbar.range); removeInlineType(linkElement, protyle.toolbar.range);
updateTransaction(protyle, id, nodeElement.outerHTML, html); updateTransaction(protyle, id, nodeElement.outerHTML, html);
} }
}).element); }).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, ""))); 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); const currentRange = getSelection().rangeCount === 0 ? undefined : getSelection().getRangeAt(0);
if (linkElement.textContent === "" || linkElement.textContent === Constants.ZWSP) { 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)) { } else if (currentRange && !protyle.element.contains(currentRange.startContainer)) {
protyle.toolbar.range.selectNodeContents(linkElement); protyle.toolbar.range.selectNodeContents(linkElement);
protyle.toolbar.range.collapse(false); 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 {fetchPost} from "../../util/fetch";
import {Constants} from "../../constants";
import {focusByRange, focusByWbr} from "../util/selection";
export const previewTemplate = (pathString: string, element: Element, parentId: string) => { export const previewTemplate = (pathString: string, element: Element, parentId: string) => {
if (!pathString) { if (!pathString) {
@ -60,3 +62,28 @@ export const removeSearchMark = (element: HTMLElement) => {
element.setAttribute("data-type", element.getAttribute("data-type").replace("search-mark", "").trim()); 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);
}
}
};