Skip to content

Commit

Permalink
🎨 fix #8315
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 committed May 20, 2023
1 parent 701d4e8 commit a1f8339
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
29 changes: 16 additions & 13 deletions app/src/menus/protyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,6 @@ export const refMenu = (app: App, protyle: IProtyle, element: HTMLElement) => {
bind(menuItemElement) {
const inputElement = menuItemElement.querySelector("input");
inputElement.value = element.getAttribute("data-subtype") === "d" ? "" : element.textContent;
inputElement.addEventListener("blur", (event) => {
if (nodeElement.outerHTML !== oldHTML) {
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
updateTransaction(protyle, id, nodeElement.outerHTML, oldHTML);
oldHTML = nodeElement.outerHTML;
}
protyle.toolbar.range.selectNodeContents(element);
protyle.toolbar.range.collapse(false);
focusByRange(protyle.toolbar.range);
event.stopPropagation();
});
inputElement.addEventListener("input", () => {
if (inputElement.value) {
// 不能使用 textContent,否则 < 会变为 &lt;
Expand Down Expand Up @@ -290,6 +279,19 @@ export const refMenu = (app: App, protyle: IProtyle, element: HTMLElement) => {
h: 26
});
window.siyuan.menus.menu.element.querySelector("input").select();
window.siyuan.menus.menu.removeCB = () => {
if (nodeElement.outerHTML !== oldHTML) {
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
updateTransaction(protyle, id, nodeElement.outerHTML, oldHTML);
oldHTML = nodeElement.outerHTML;
}
const currentRange = getSelection().rangeCount === 0 ? undefined : getSelection().getRangeAt(0);
if (currentRange && !protyle.element.contains(currentRange.startContainer)) {
protyle.toolbar.range.selectNodeContents(element);
protyle.toolbar.range.collapse(false);
focusByRange(protyle.toolbar.range);
}
};
};

export const contentMenu = (protyle: IProtyle, nodeElement: Element) => {
Expand Down Expand Up @@ -890,9 +892,10 @@ export const linkMenu = (app: App, protyle: IProtyle, linkElement: HTMLElement,
linkElement.removeAttribute("data-title");
}
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, protyle.toolbar.range);
} else {
removeLink(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);
focusByRange(protyle.toolbar.range);
Expand Down
14 changes: 9 additions & 5 deletions app/src/protyle/toolbar/Link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ export class Link extends ToolbarItem {
}
}

export const removeLink = (linkElement: HTMLElement, range: Range) => {
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>";
focusByWbr(linkParentElement, range);
if (range) {
focusByWbr(linkParentElement, range);
}
} else {
types.find((itemType, index) => {
if ("a" === itemType) {
Expand All @@ -73,8 +75,10 @@ export const removeLink = (linkElement: HTMLElement, range: Range) => {
});
linkElement.setAttribute("data-type", types.join(" "));
linkElement.removeAttribute("data-href");
range.selectNodeContents(linkElement);
range.collapse(false);
focusByRange(range);
if (range) {
range.selectNodeContents(linkElement);
range.collapse(false);
focusByRange(range);
}
}
};
8 changes: 6 additions & 2 deletions app/src/protyle/toolbar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -807,9 +807,13 @@ export class Toolbar {
refElement.innerHTML = "*";
}
this.range.setStartAfter(refElement);
focusByRange(this.range);
if (getSelection().rangeCount === 0) {
focusByRange(this.range);
}
} else {
focusByWbr(nodeElement, this.range);
if (getSelection().rangeCount === 0) {
focusByWbr(nodeElement, this.range);
}
}
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
updateTransaction(protyle, id, nodeElement.outerHTML, html);
Expand Down

0 comments on commit a1f8339

Please sign in to comment.