2022-05-26 15:18:53 +08:00
|
|
|
|
import {hasClosestBlock, hasClosestByMatchTag} from "../protyle/util/hasClosest";
|
|
|
|
|
|
import {MenuItem} from "./Menu";
|
2022-11-24 23:57:22 +08:00
|
|
|
|
import {
|
|
|
|
|
|
focusBlock,
|
|
|
|
|
|
focusByRange,
|
|
|
|
|
|
focusByWbr,
|
|
|
|
|
|
getEditorRange,
|
|
|
|
|
|
selectAll,
|
|
|
|
|
|
} from "../protyle/util/selection";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
import {
|
|
|
|
|
|
deleteColumn,
|
|
|
|
|
|
deleteRow,
|
|
|
|
|
|
getColIndex,
|
|
|
|
|
|
insertColumn,
|
|
|
|
|
|
insertRow,
|
|
|
|
|
|
insertRowAbove,
|
|
|
|
|
|
moveColumnToLeft,
|
|
|
|
|
|
moveColumnToRight,
|
|
|
|
|
|
moveRowToDown,
|
|
|
|
|
|
moveRowToUp,
|
|
|
|
|
|
setTableAlign
|
|
|
|
|
|
} from "../protyle/util/table";
|
|
|
|
|
|
import {mathRender} from "../protyle/markdown/mathRender";
|
|
|
|
|
|
import {transaction, updateTransaction} from "../protyle/wysiwyg/transaction";
|
|
|
|
|
|
import {openMenu} from "./commonMenuItem";
|
|
|
|
|
|
import {fetchPost} from "../util/fetch";
|
|
|
|
|
|
import {Constants} from "../constants";
|
2022-11-22 00:56:37 +08:00
|
|
|
|
import {copyPlainText, readText, writeText} from "../protyle/util/compatibility";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
import {preventScroll} from "../protyle/scroll/preventScroll";
|
|
|
|
|
|
import {onGet} from "../protyle/util/onGet";
|
|
|
|
|
|
import {getAllModels} from "../layout/getAll";
|
2022-11-08 18:53:03 +08:00
|
|
|
|
import {pasteAsPlainText, pasteText} from "../protyle/util/paste";
|
2022-06-29 15:36:44 +08:00
|
|
|
|
/// #if !MOBILE
|
2022-05-26 15:18:53 +08:00
|
|
|
|
import {openFileById, updateBacklinkGraph} from "../editor/util";
|
2022-06-29 15:36:44 +08:00
|
|
|
|
/// #endif
|
2022-05-26 15:18:53 +08:00
|
|
|
|
import {isMobile} from "../util/functions";
|
|
|
|
|
|
import {removeFoldHeading} from "../protyle/util/heading";
|
|
|
|
|
|
import {lineNumberRender} from "../protyle/markdown/highlightRender";
|
|
|
|
|
|
import * as dayjs from "dayjs";
|
|
|
|
|
|
import {blockRender} from "../protyle/markdown/blockRender";
|
2022-07-15 11:22:14 +08:00
|
|
|
|
import {renameAsset} from "../editor/rename";
|
2022-08-15 10:24:37 +08:00
|
|
|
|
import {electronUndo} from "../protyle/undo";
|
2022-08-23 11:55:40 +08:00
|
|
|
|
import {pushBack} from "../mobile/util/MobileBackFoward";
|
2022-08-23 20:13:41 +08:00
|
|
|
|
import {exportAsset} from "./util";
|
2022-09-15 11:57:33 +08:00
|
|
|
|
import {removeLink} from "../protyle/toolbar/Link";
|
2022-11-21 10:40:29 +08:00
|
|
|
|
import {alignImgCenter, alignImgLeft} from "../protyle/wysiwyg/commonHotkey";
|
2022-11-24 23:57:22 +08:00
|
|
|
|
import {getEnableHTML} from "../protyle/wysiwyg/removeEmbed";
|
|
|
|
|
|
import {getContenteditableElement} from "../protyle/wysiwyg/getBlock";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
|
|
|
|
|
export const refMenu = (protyle: IProtyle, element: HTMLElement) => {
|
|
|
|
|
|
const nodeElement = hasClosestBlock(element);
|
|
|
|
|
|
if (!nodeElement) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
const refBlockId = element.getAttribute("data-id");
|
|
|
|
|
|
const id = nodeElement.getAttribute("data-node-id");
|
2022-09-26 16:20:53 +08:00
|
|
|
|
let oldHTML = nodeElement.outerHTML;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
window.siyuan.menus.menu.remove();
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
label: `<input style="margin: 4px 0" class="b3-text-field" placeholder="${window.siyuan.languages.anchor}">`,
|
|
|
|
|
|
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);
|
2022-09-26 16:20:53 +08:00
|
|
|
|
oldHTML = nodeElement.outerHTML;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2022-05-28 20:29:02 +08:00
|
|
|
|
protyle.toolbar.range.selectNodeContents(element);
|
|
|
|
|
|
protyle.toolbar.range.collapse(false);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
focusByRange(protyle.toolbar.range);
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
});
|
|
|
|
|
|
inputElement.addEventListener("input", () => {
|
|
|
|
|
|
if (inputElement.value) {
|
|
|
|
|
|
element.textContent = Lute.EscapeHTMLStr(inputElement.value);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
fetchPost("/api/block/getRefText", {id: refBlockId}, (response) => {
|
2022-08-31 18:17:43 +08:00
|
|
|
|
element.innerHTML = response.data;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
2022-09-26 16:20:53 +08:00
|
|
|
|
element.setAttribute("data-subtype", inputElement.value ? "s" : "d");
|
2022-05-26 15:18:53 +08:00
|
|
|
|
});
|
2022-05-27 21:33:30 +08:00
|
|
|
|
inputElement.addEventListener("keydown", (event) => {
|
|
|
|
|
|
if (event.key === "Enter" && !event.isComposing) {
|
|
|
|
|
|
window.siyuan.menus.menu.remove();
|
2022-08-15 11:44:11 +08:00
|
|
|
|
} else if (electronUndo(event)) {
|
2022-08-15 10:24:37 +08:00
|
|
|
|
return;
|
2022-05-27 21:33:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
type: "separator"
|
|
|
|
|
|
}).element);
|
2022-06-29 15:36:44 +08:00
|
|
|
|
/// #if !MOBILE
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
label: window.siyuan.languages.openBy,
|
2022-09-02 12:17:10 +08:00
|
|
|
|
accelerator: window.siyuan.config.keymap.editor.general.openBy.custom + "/Click",
|
|
|
|
|
|
click() {
|
|
|
|
|
|
fetchPost("/api/block/checkBlockFold", {id: refBlockId}, (foldResponse) => {
|
|
|
|
|
|
openFileById({
|
|
|
|
|
|
id: refBlockId,
|
|
|
|
|
|
action: foldResponse.data ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL] : [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT],
|
|
|
|
|
|
zoomIn: foldResponse.data
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
label: window.siyuan.languages.refTab,
|
|
|
|
|
|
accelerator: window.siyuan.config.keymap.editor.general.refTab.custom + "/⌘Click",
|
2022-06-29 15:36:44 +08:00
|
|
|
|
click() {
|
|
|
|
|
|
fetchPost("/api/block/checkBlockFold", {id: refBlockId}, (foldResponse) => {
|
|
|
|
|
|
openFileById({
|
|
|
|
|
|
id: refBlockId,
|
2022-08-07 09:26:08 +08:00
|
|
|
|
action: foldResponse.data ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL] : [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT],
|
2022-06-29 15:36:44 +08:00
|
|
|
|
keepCursor: true,
|
|
|
|
|
|
zoomIn: foldResponse.data
|
2022-05-26 15:18:53 +08:00
|
|
|
|
});
|
2022-06-29 15:36:44 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
label: window.siyuan.languages.insertRight,
|
2022-10-24 22:18:53 +08:00
|
|
|
|
icon: "iconLayoutRight",
|
2022-09-02 12:17:10 +08:00
|
|
|
|
accelerator: window.siyuan.config.keymap.editor.general.insertRight.custom + "/⌥Click",
|
2022-06-29 15:36:44 +08:00
|
|
|
|
click() {
|
|
|
|
|
|
fetchPost("/api/block/checkBlockFold", {id: refBlockId}, (foldResponse) => {
|
|
|
|
|
|
openFileById({
|
|
|
|
|
|
id: refBlockId,
|
|
|
|
|
|
position: "right",
|
2022-08-07 09:26:08 +08:00
|
|
|
|
action: foldResponse.data ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL] : [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT],
|
2022-06-29 15:36:44 +08:00
|
|
|
|
zoomIn: foldResponse.data
|
2022-05-26 15:18:53 +08:00
|
|
|
|
});
|
2022-06-29 15:36:44 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
label: window.siyuan.languages.insertBottom,
|
2022-10-24 22:18:53 +08:00
|
|
|
|
icon: "iconLayoutBottom",
|
2022-09-02 12:17:10 +08:00
|
|
|
|
accelerator: window.siyuan.config.keymap.editor.general.insertBottom.custom + (window.siyuan.config.keymap.editor.general.insertBottom.custom ? "/" : "") + "⇧Click",
|
2022-06-29 15:36:44 +08:00
|
|
|
|
click() {
|
|
|
|
|
|
fetchPost("/api/block/checkBlockFold", {id: refBlockId}, (foldResponse) => {
|
|
|
|
|
|
openFileById({
|
|
|
|
|
|
id: refBlockId,
|
|
|
|
|
|
position: "bottom",
|
2022-08-07 09:26:08 +08:00
|
|
|
|
action: foldResponse.data ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL] : [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT],
|
2022-06-29 15:36:44 +08:00
|
|
|
|
zoomIn: foldResponse.data
|
2022-05-26 15:18:53 +08:00
|
|
|
|
});
|
2022-06-29 15:36:44 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
|
|
|
|
|
/// #endif
|
2022-05-26 15:18:53 +08:00
|
|
|
|
let submenu: IMenu[] = [];
|
|
|
|
|
|
if (element.getAttribute("data-subtype") === "s") {
|
|
|
|
|
|
submenu.push({
|
|
|
|
|
|
label: window.siyuan.languages.turnToDynamic,
|
|
|
|
|
|
click() {
|
|
|
|
|
|
element.setAttribute("data-subtype", "d");
|
|
|
|
|
|
fetchPost("/api/block/getRefText", {id: refBlockId}, (response) => {
|
2022-08-31 18:17:43 +08:00
|
|
|
|
element.innerHTML = response.data;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
|
|
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, oldHTML);
|
|
|
|
|
|
});
|
|
|
|
|
|
focusByRange(protyle.toolbar.range);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
} else {
|
|
|
|
|
|
submenu.push({
|
|
|
|
|
|
label: window.siyuan.languages.turnToStatic,
|
|
|
|
|
|
click() {
|
|
|
|
|
|
element.setAttribute("data-subtype", "s");
|
|
|
|
|
|
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
|
|
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, oldHTML);
|
|
|
|
|
|
focusByRange(protyle.toolbar.range);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
submenu = submenu.concat([{
|
|
|
|
|
|
label: window.siyuan.languages.text,
|
|
|
|
|
|
click() {
|
2022-08-31 18:17:43 +08:00
|
|
|
|
element.outerHTML = `${element.innerHTML}<wbr>`;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
|
|
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, oldHTML);
|
|
|
|
|
|
focusByWbr(nodeElement, protyle.toolbar.range);
|
|
|
|
|
|
}
|
|
|
|
|
|
}, {
|
|
|
|
|
|
label: "*",
|
|
|
|
|
|
click() {
|
|
|
|
|
|
element.setAttribute("data-subtype", "s");
|
|
|
|
|
|
element.textContent = "*";
|
|
|
|
|
|
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
|
|
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, oldHTML);
|
|
|
|
|
|
focusByRange(protyle.toolbar.range);
|
|
|
|
|
|
}
|
|
|
|
|
|
}, {
|
|
|
|
|
|
label: window.siyuan.languages.text + " *",
|
|
|
|
|
|
click() {
|
2022-08-31 18:17:43 +08:00
|
|
|
|
element.insertAdjacentHTML("beforebegin", element.innerHTML + " ");
|
2022-05-26 15:18:53 +08:00
|
|
|
|
element.setAttribute("data-subtype", "s");
|
|
|
|
|
|
element.textContent = "*";
|
|
|
|
|
|
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
|
|
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, oldHTML);
|
|
|
|
|
|
focusByRange(protyle.toolbar.range);
|
|
|
|
|
|
}
|
|
|
|
|
|
}, {
|
|
|
|
|
|
label: window.siyuan.languages.link,
|
|
|
|
|
|
icon: "iconLink",
|
|
|
|
|
|
click() {
|
2022-08-31 18:17:43 +08:00
|
|
|
|
element.outerHTML = `<span data-type="a" data-href="siyuan://blocks/${element.getAttribute("data-id")}">${element.innerHTML}</span><wbr>`;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
|
|
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, oldHTML);
|
|
|
|
|
|
focusByWbr(nodeElement, protyle.toolbar.range);
|
|
|
|
|
|
}
|
|
|
|
|
|
}]);
|
|
|
|
|
|
if (element.parentElement.textContent.trim() === element.textContent.trim() && element.parentElement.tagName === "DIV") {
|
|
|
|
|
|
submenu.push({
|
|
|
|
|
|
label: window.siyuan.languages.blockEmbed,
|
|
|
|
|
|
icon: "iconSQL",
|
|
|
|
|
|
click() {
|
|
|
|
|
|
const html = `<div data-content="select * from blocks where id='${refBlockId}'" data-node-id="${id}" data-type="NodeBlockQueryEmbed" class="render-node" updated="${dayjs().format("YYYYMMDDHHmmss")}">${nodeElement.querySelector(".protyle-attr").outerHTML}</div>`;
|
|
|
|
|
|
nodeElement.outerHTML = html;
|
|
|
|
|
|
updateTransaction(protyle, id, html, oldHTML);
|
|
|
|
|
|
blockRender(protyle, protyle.wysiwyg.element);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2022-10-03 16:19:49 +08:00
|
|
|
|
submenu.push({
|
2022-10-03 16:51:30 +08:00
|
|
|
|
label: window.siyuan.languages.defBlock,
|
2022-10-03 16:19:49 +08:00
|
|
|
|
click() {
|
|
|
|
|
|
fetchPost("/api/block/swapBlockRef", {
|
|
|
|
|
|
refID: id,
|
|
|
|
|
|
defID: refBlockId,
|
|
|
|
|
|
includeChildren: false
|
2022-10-03 16:22:18 +08:00
|
|
|
|
});
|
2022-10-03 16:19:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
submenu.push({
|
2022-10-03 16:51:30 +08:00
|
|
|
|
label: window.siyuan.languages.defBlockChildren,
|
2022-10-03 16:19:49 +08:00
|
|
|
|
click() {
|
|
|
|
|
|
fetchPost("/api/block/swapBlockRef", {
|
|
|
|
|
|
refID: id,
|
|
|
|
|
|
defID: refBlockId,
|
|
|
|
|
|
includeChildren: true
|
2022-10-03 16:22:18 +08:00
|
|
|
|
});
|
2022-10-03 16:19:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
2022-05-26 15:18:53 +08:00
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
label: window.siyuan.languages.turnInto,
|
|
|
|
|
|
icon: "iconRefresh",
|
|
|
|
|
|
submenu
|
|
|
|
|
|
}).element);
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
label: window.siyuan.languages.copy,
|
|
|
|
|
|
icon: "iconCopy",
|
|
|
|
|
|
click() {
|
|
|
|
|
|
const quote = element.getAttribute("data-subtype") === "s" ? '"' : "'";
|
|
|
|
|
|
writeText(`((${refBlockId} ${quote}${element.textContent}${quote}))`);
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
label: window.siyuan.languages.remove,
|
|
|
|
|
|
icon: "iconTrashcan",
|
|
|
|
|
|
click() {
|
|
|
|
|
|
element.insertAdjacentHTML("afterend", "<wbr>");
|
|
|
|
|
|
element.remove();
|
|
|
|
|
|
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
|
|
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, oldHTML);
|
|
|
|
|
|
focusByWbr(nodeElement, protyle.toolbar.range);
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
2022-08-02 11:13:11 +08:00
|
|
|
|
const rect = element.getBoundingClientRect();
|
|
|
|
|
|
window.siyuan.menus.menu.popup({
|
|
|
|
|
|
x: rect.left,
|
2022-08-15 11:44:11 +08:00
|
|
|
|
y: rect.top + 26,
|
2022-08-02 11:13:11 +08:00
|
|
|
|
h: 26
|
2022-08-02 11:15:18 +08:00
|
|
|
|
});
|
2022-05-26 15:18:53 +08:00
|
|
|
|
window.siyuan.menus.menu.element.querySelector("input").select();
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export const contentMenu = (protyle: IProtyle, nodeElement: Element) => {
|
|
|
|
|
|
const range = getEditorRange(nodeElement);
|
|
|
|
|
|
window.siyuan.menus.menu.remove();
|
2022-11-20 22:26:55 +08:00
|
|
|
|
if (range.toString() !== "" || (range.cloneContents().childNodes[0] as HTMLElement)?.classList.contains("emoji")) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
icon: "iconCopy",
|
|
|
|
|
|
accelerator: "⌘C",
|
|
|
|
|
|
label: window.siyuan.languages.copy,
|
|
|
|
|
|
click() {
|
|
|
|
|
|
// range 需要重新计算 https://ld246.com/article/1644979219025
|
|
|
|
|
|
focusByRange(getEditorRange(nodeElement));
|
|
|
|
|
|
document.execCommand("copy");
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
2022-08-20 13:13:21 +08:00
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
label: window.siyuan.languages.copyPlainText,
|
2022-08-29 12:24:19 +08:00
|
|
|
|
accelerator: window.siyuan.config.keymap.editor.general.copyPlainText.custom,
|
2022-08-20 13:13:21 +08:00
|
|
|
|
click() {
|
|
|
|
|
|
focusByRange(getEditorRange(nodeElement));
|
2022-08-28 12:44:28 +08:00
|
|
|
|
const cloneContents = getSelection().getRangeAt(0).cloneContents();
|
2022-08-28 10:49:42 +08:00
|
|
|
|
cloneContents.querySelectorAll('[data-type="backslash"]').forEach(item => {
|
|
|
|
|
|
item.firstElementChild.remove();
|
|
|
|
|
|
});
|
2022-11-22 00:56:37 +08:00
|
|
|
|
copyPlainText(cloneContents.textContent);
|
2022-08-20 13:13:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
2022-08-22 22:58:39 +08:00
|
|
|
|
label: window.siyuan.languages.copy + " HTML",
|
2022-08-20 13:13:21 +08:00
|
|
|
|
click() {
|
|
|
|
|
|
focusByRange(getEditorRange(nodeElement));
|
2022-08-21 17:38:09 +08:00
|
|
|
|
let html = "";
|
2022-08-22 22:58:39 +08:00
|
|
|
|
getSelection().getRangeAt(0).cloneContents().childNodes.forEach(item => {
|
2022-08-20 13:13:21 +08:00
|
|
|
|
if (item.nodeType === 3) {
|
2022-08-21 17:38:09 +08:00
|
|
|
|
html += item.textContent;
|
2022-08-20 13:13:21 +08:00
|
|
|
|
} else {
|
2022-08-21 17:38:09 +08:00
|
|
|
|
html += (item as Element).outerHTML;
|
2022-08-20 13:13:21 +08:00
|
|
|
|
}
|
2022-08-21 17:38:09 +08:00
|
|
|
|
});
|
2022-11-24 23:57:22 +08:00
|
|
|
|
if (protyle.disabled) {
|
|
|
|
|
|
html = getEnableHTML(html)
|
|
|
|
|
|
}
|
2022-08-21 17:38:09 +08:00
|
|
|
|
const tempElement = document.createElement("template");
|
|
|
|
|
|
tempElement.innerHTML = protyle.lute.BlockDOM2HTML(html);
|
2022-08-20 13:13:21 +08:00
|
|
|
|
writeText(tempElement.content.firstElementChild.innerHTML);
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
2022-11-24 23:57:22 +08:00
|
|
|
|
if (protyle.disabled) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
icon: "iconCut",
|
|
|
|
|
|
accelerator: "⌘X",
|
|
|
|
|
|
label: window.siyuan.languages.cut,
|
|
|
|
|
|
click() {
|
|
|
|
|
|
focusByRange(getEditorRange(nodeElement));
|
|
|
|
|
|
document.execCommand("cut");
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
icon: "iconTrashcan",
|
|
|
|
|
|
accelerator: "⌫",
|
|
|
|
|
|
label: window.siyuan.languages.delete,
|
|
|
|
|
|
click() {
|
|
|
|
|
|
const currentRange = getEditorRange(nodeElement);
|
|
|
|
|
|
currentRange.insertNode(document.createElement("wbr"));
|
|
|
|
|
|
const oldHTML = nodeElement.outerHTML;
|
|
|
|
|
|
currentRange.extractContents();
|
|
|
|
|
|
focusByWbr(nodeElement, currentRange);
|
|
|
|
|
|
focusByRange(currentRange);
|
|
|
|
|
|
updateTransaction(protyle, nodeElement.getAttribute("data-node-id"), nodeElement.outerHTML, oldHTML);
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
|
|
|
|
|
}
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
label: window.siyuan.languages.paste,
|
|
|
|
|
|
accelerator: "⌘V",
|
|
|
|
|
|
async click() {
|
|
|
|
|
|
if (document.queryCommandSupported("paste")) {
|
|
|
|
|
|
document.execCommand("paste");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
try {
|
2022-10-07 11:00:52 +08:00
|
|
|
|
const clipText = await readText();
|
2022-05-26 15:18:53 +08:00
|
|
|
|
pasteText(protyle, clipText, nodeElement);
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.log(e);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
2022-09-19 11:33:11 +08:00
|
|
|
|
/// #if !BROWSER && !MOBILE
|
2022-05-26 15:18:53 +08:00
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
label: window.siyuan.languages.pasteAsPlainText,
|
2022-11-05 22:47:26 +08:00
|
|
|
|
accelerator: "⇧⌘V",
|
2022-05-26 15:18:53 +08:00
|
|
|
|
click() {
|
|
|
|
|
|
focusByRange(getEditorRange(nodeElement));
|
2022-11-08 18:53:03 +08:00
|
|
|
|
pasteAsPlainText(protyle);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
|
|
|
|
|
/// #endif
|
2022-06-22 11:52:22 +08:00
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
label: window.siyuan.languages.pasteEscaped,
|
|
|
|
|
|
async click() {
|
|
|
|
|
|
try {
|
|
|
|
|
|
// * _ [ ] ! \ ` < > & ~ { } ( ) = # $ ^ |
|
2022-10-07 11:00:52 +08:00
|
|
|
|
let clipText = await readText();
|
2022-07-27 21:32:04 +08:00
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/5446
|
|
|
|
|
|
// A\B\C\D\
|
|
|
|
|
|
// E
|
2022-10-25 00:37:26 +08:00
|
|
|
|
// task-blog-2~default~baiduj 无法原义粘贴含有 `~foo~` 的文本 https://github.com/siyuan-note/siyuan/issues/5523
|
|
|
|
|
|
|
|
|
|
|
|
// 这里必须多加一个反斜杆,因为 Lute 在进行 Markdown 嵌套节点转换平铺标记节点时会剔除 Backslash 节点,
|
|
|
|
|
|
// 多加入的一个反斜杆会作为文本节点保留下来,后续 Spin 时刚好用于转义标记符 https://github.com/siyuan-note/siyuan/issues/6341
|
2022-10-25 00:33:06 +08:00
|
|
|
|
clipText = clipText.replace(/\\/g, "\\\\\\\\")
|
|
|
|
|
|
.replace(/\*/g, "\\\\\\*")
|
|
|
|
|
|
.replace(/\_/g, "\\\\\\_")
|
|
|
|
|
|
.replace(/\[/g, "\\\\\\[")
|
|
|
|
|
|
.replace(/\]/g, "\\\\\\]")
|
|
|
|
|
|
.replace(/\!/g, "\\\\\\!")
|
|
|
|
|
|
.replace(/\`/g, "\\\\\\`")
|
|
|
|
|
|
.replace(/\</g, "\\\\\\<")
|
|
|
|
|
|
.replace(/\>/g, "\\\\\\>")
|
|
|
|
|
|
.replace(/\&/g, "\\\\\\&")
|
|
|
|
|
|
.replace(/\~/g, "\\\\\\~")
|
|
|
|
|
|
.replace(/\{/g, "\\\\\\{")
|
|
|
|
|
|
.replace(/\}/g, "\\\\\\}")
|
|
|
|
|
|
.replace(/\(/g, "\\\\\\(")
|
|
|
|
|
|
.replace(/\)/g, "\\\\\\)")
|
|
|
|
|
|
.replace(/\=/g, "\\\\\\=")
|
|
|
|
|
|
.replace(/\#/g, "\\\\\\#")
|
|
|
|
|
|
.replace(/\$/g, "\\\\\\$")
|
|
|
|
|
|
.replace(/\^/g, "\\\\\\^")
|
|
|
|
|
|
.replace(/\|/g, "\\\\\\|");
|
2022-06-22 11:52:22 +08:00
|
|
|
|
pasteText(protyle, clipText, nodeElement);
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.log(e);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
label: window.siyuan.languages.selectAll,
|
|
|
|
|
|
accelerator: "⌘A",
|
|
|
|
|
|
click() {
|
|
|
|
|
|
selectAll(protyle, nodeElement, range);
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
|
|
|
|
|
if (nodeElement.classList.contains("table")) {
|
|
|
|
|
|
const cellElement = hasClosestByMatchTag(range.startContainer, "TD") || hasClosestByMatchTag(range.startContainer, "TH");
|
|
|
|
|
|
if (cellElement) {
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
type: "submenu",
|
|
|
|
|
|
icon: "iconTable",
|
|
|
|
|
|
label: window.siyuan.languages.table,
|
|
|
|
|
|
submenu: tableMenu(protyle, nodeElement, cellElement as HTMLTableCellElement, range) as IMenu[]
|
|
|
|
|
|
}).element);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2022-08-07 00:14:50 +08:00
|
|
|
|
export const zoomOut = (protyle: IProtyle, id: string, focusId?: string, isPushBack = true, callback?: () => void) => {
|
2022-10-02 20:56:48 +08:00
|
|
|
|
if (protyle.options.backlinkData) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-08-31 01:14:45 +08:00
|
|
|
|
const breadcrumbHLElement = protyle.breadcrumb?.element.querySelector(".protyle-breadcrumb__item--active");
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if (breadcrumbHLElement && breadcrumbHLElement.getAttribute("data-node-id") === id) {
|
|
|
|
|
|
if (id === protyle.block.rootID) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
const focusElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${focusId || id}"]`);
|
|
|
|
|
|
if (focusElement) {
|
|
|
|
|
|
focusBlock(focusElement);
|
|
|
|
|
|
focusElement.scrollIntoView();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (window.siyuan.mobileEditor) {
|
2022-10-30 23:57:46 +08:00
|
|
|
|
localStorage.setItem(Constants.LOCAL_DOCINFO, JSON.stringify({
|
2022-06-01 10:12:14 +08:00
|
|
|
|
id,
|
2022-08-07 09:26:08 +08:00
|
|
|
|
action: id === protyle.block.rootID ? [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT] : [Constants.CB_GET_ALL]
|
2022-06-01 10:12:14 +08:00
|
|
|
|
}));
|
2022-08-23 11:55:40 +08:00
|
|
|
|
if (isPushBack) {
|
|
|
|
|
|
pushBack();
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
fetchPost("/api/filetree/getDoc", {
|
|
|
|
|
|
id,
|
2022-10-30 23:13:41 +08:00
|
|
|
|
size: id === protyle.block.rootID ? window.siyuan.config.editor.dynamicLoadBlocks : Constants.SIZE_GET_MAX,
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}, getResponse => {
|
|
|
|
|
|
if (isPushBack) {
|
2022-09-28 23:48:59 +08:00
|
|
|
|
onGet(getResponse, protyle, id === protyle.block.rootID ? [Constants.CB_GET_FOCUS, Constants.CB_GET_HTML] : [Constants.CB_GET_ALL, Constants.CB_GET_FOCUS, Constants.CB_GET_HTML]);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
} else {
|
2022-09-28 23:48:59 +08:00
|
|
|
|
onGet(getResponse, protyle, id === protyle.block.rootID ? [Constants.CB_GET_FOCUS, Constants.CB_GET_HTML, Constants.CB_GET_UNUNDO] : [Constants.CB_GET_ALL, Constants.CB_GET_FOCUS, Constants.CB_GET_UNUNDO, Constants.CB_GET_HTML]);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/4874
|
|
|
|
|
|
if (focusId) {
|
|
|
|
|
|
const focusElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${focusId}"]`);
|
|
|
|
|
|
if (focusElement) {
|
|
|
|
|
|
focusBlock(focusElement);
|
|
|
|
|
|
focusElement.scrollIntoView();
|
|
|
|
|
|
} else if (id === protyle.block.rootID) { // 聚焦返回后,该块是动态加载的,但是没加载出来
|
|
|
|
|
|
fetchPost("/api/filetree/getDoc", {
|
|
|
|
|
|
id: focusId,
|
|
|
|
|
|
mode: 3,
|
2022-10-30 23:13:41 +08:00
|
|
|
|
size: window.siyuan.config.editor.dynamicLoadBlocks,
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}, getFocusResponse => {
|
|
|
|
|
|
onGet(getFocusResponse, protyle, isPushBack ? [Constants.CB_GET_FOCUS] : [Constants.CB_GET_FOCUS, Constants.CB_GET_UNUNDO]);
|
|
|
|
|
|
});
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-06-29 15:36:44 +08:00
|
|
|
|
/// #if !MOBILE
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if (protyle.model) {
|
|
|
|
|
|
updateBacklinkGraph(getAllModels(), protyle);
|
|
|
|
|
|
}
|
2022-06-29 15:36:44 +08:00
|
|
|
|
/// #endif
|
2022-08-07 00:14:50 +08:00
|
|
|
|
if (callback) {
|
|
|
|
|
|
callback();
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export const imgMenu = (protyle: IProtyle, range: Range, assetElement: HTMLElement, position: { clientX: number, clientY: number }) => {
|
|
|
|
|
|
window.siyuan.menus.menu.remove();
|
|
|
|
|
|
const nodeElement = hasClosestBlock(assetElement);
|
|
|
|
|
|
if (!nodeElement) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
const id = nodeElement.getAttribute("data-node-id");
|
|
|
|
|
|
const imgElement = assetElement.querySelector("img");
|
|
|
|
|
|
const titleElement = assetElement.querySelector(".protyle-action__title") as HTMLElement;
|
|
|
|
|
|
|
|
|
|
|
|
let html = nodeElement.outerHTML;
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
label: `<div class="fn__hr--small"></div><input class="b3-text-field fn__size200" value="${imgElement.getAttribute("src")}" placeholder="${window.siyuan.languages.imageURL}"><div class="fn__hr--small"></div>`,
|
|
|
|
|
|
bind(element) {
|
|
|
|
|
|
element.querySelector("input").addEventListener("change", (event) => {
|
|
|
|
|
|
const value = (event.target as HTMLInputElement).value;
|
|
|
|
|
|
imgElement.setAttribute("src", value);
|
|
|
|
|
|
imgElement.setAttribute("data-src", value);
|
|
|
|
|
|
if (value.startsWith("assets/")) {
|
|
|
|
|
|
const imgNetElement = assetElement.querySelector(".img__net");
|
|
|
|
|
|
if (imgNetElement) {
|
|
|
|
|
|
imgNetElement.remove();
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if (window.siyuan.config.editor.displayNetImgMark) {
|
|
|
|
|
|
assetElement.querySelector(".protyle-action__drag").insertAdjacentHTML("afterend", '<span class="img__net"><svg><use xlink:href="#iconLanguage"></use></svg></span>');
|
|
|
|
|
|
}
|
2022-06-10 00:54:53 +08:00
|
|
|
|
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
|
2022-05-26 15:18:53 +08:00
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, html);
|
|
|
|
|
|
html = nodeElement.outerHTML;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
label: `<div class="fn__hr--small"></div><input class="b3-text-field fn__size200" placeholder="${window.siyuan.languages.title}"><div class="fn__hr--small"></div>`,
|
|
|
|
|
|
bind(element) {
|
|
|
|
|
|
const inputElement = element.querySelector("input");
|
|
|
|
|
|
inputElement.value = titleElement.textContent;
|
|
|
|
|
|
inputElement.addEventListener("input", (event) => {
|
|
|
|
|
|
const value = (event.target as HTMLInputElement).value;
|
|
|
|
|
|
imgElement.setAttribute("title", value);
|
|
|
|
|
|
titleElement.textContent = value;
|
|
|
|
|
|
mathRender(titleElement);
|
|
|
|
|
|
assetElement.style.maxWidth = (imgElement.clientWidth + 10) + "px";
|
|
|
|
|
|
});
|
|
|
|
|
|
inputElement.addEventListener("change", () => {
|
2022-06-10 00:54:53 +08:00
|
|
|
|
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
|
2022-05-26 15:18:53 +08:00
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, html);
|
|
|
|
|
|
html = nodeElement.outerHTML;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
label: `<div class="fn__hr--small"></div><input class="b3-text-field fn__size200" placeholder="${window.siyuan.languages.tooltipText}"><div class="fn__hr--small"></div>`,
|
|
|
|
|
|
bind(element) {
|
|
|
|
|
|
const inputElement = element.querySelector("input");
|
|
|
|
|
|
inputElement.value = imgElement.getAttribute("alt") || "";
|
|
|
|
|
|
inputElement.addEventListener("change", (event) => {
|
|
|
|
|
|
imgElement.setAttribute("alt", (event.target as HTMLInputElement).value);
|
2022-06-10 00:54:53 +08:00
|
|
|
|
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
|
2022-05-26 15:18:53 +08:00
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, html);
|
|
|
|
|
|
html = nodeElement.outerHTML;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({type: "separator"}).element);
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
label: window.siyuan.languages.copy,
|
2022-06-09 11:28:35 +08:00
|
|
|
|
accelerator: "⌘C",
|
2022-05-26 15:18:53 +08:00
|
|
|
|
icon: "iconCopy",
|
|
|
|
|
|
click() {
|
2022-09-19 16:53:01 +08:00
|
|
|
|
writeText(protyle.lute.BlockDOM2StdMd(assetElement.outerHTML));
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
label: window.siyuan.languages.copy + " PNG",
|
|
|
|
|
|
icon: "iconImage",
|
|
|
|
|
|
click() {
|
2022-07-26 10:34:53 +08:00
|
|
|
|
if ("android" === window.siyuan.config.system.container && window.JSAndroid) {
|
|
|
|
|
|
window.JSAndroid.writeImageClipboard(imgElement.getAttribute("src"));
|
|
|
|
|
|
return;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
const canvas = document.createElement("canvas");
|
|
|
|
|
|
const tempElement = document.createElement("img");
|
|
|
|
|
|
tempElement.onload = (e: Event & { target: HTMLImageElement }) => {
|
|
|
|
|
|
canvas.width = e.target.width;
|
|
|
|
|
|
canvas.height = e.target.height;
|
|
|
|
|
|
canvas.getContext("2d").drawImage(e.target, 0, 0, e.target.width, e.target.height);
|
|
|
|
|
|
canvas.toBlob((blob) => {
|
|
|
|
|
|
navigator.clipboard.write([
|
|
|
|
|
|
new ClipboardItem({
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
|
["image/png"]: blob
|
|
|
|
|
|
})
|
|
|
|
|
|
]);
|
|
|
|
|
|
}, "image/png", 1);
|
|
|
|
|
|
};
|
|
|
|
|
|
tempElement.src = imgElement.getAttribute("src");
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
2022-08-23 20:13:41 +08:00
|
|
|
|
/// #if !BROWSER
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem(exportAsset(imgElement.getAttribute("data-src"))).element);
|
|
|
|
|
|
/// #endif
|
2022-06-09 11:28:35 +08:00
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
icon: "iconCut",
|
|
|
|
|
|
accelerator: "⌘X",
|
|
|
|
|
|
label: window.siyuan.languages.cut,
|
|
|
|
|
|
click() {
|
2022-09-19 16:53:01 +08:00
|
|
|
|
writeText(protyle.lute.BlockDOM2StdMd(assetElement.outerHTML));
|
2022-06-09 11:28:35 +08:00
|
|
|
|
(assetElement as HTMLElement).outerHTML = "<wbr>";
|
2022-06-10 00:54:53 +08:00
|
|
|
|
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
|
2022-06-09 11:28:35 +08:00
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, html);
|
|
|
|
|
|
focusByWbr(protyle.wysiwyg.element, range);
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
icon: "iconTrashcan",
|
2022-06-09 11:28:35 +08:00
|
|
|
|
accelerator: "⌫",
|
2022-05-26 15:18:53 +08:00
|
|
|
|
label: window.siyuan.languages.delete,
|
|
|
|
|
|
click: function () {
|
|
|
|
|
|
(assetElement as HTMLElement).outerHTML = "<wbr>";
|
2022-06-10 00:54:53 +08:00
|
|
|
|
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
|
2022-05-26 15:18:53 +08:00
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, html);
|
|
|
|
|
|
focusByWbr(protyle.wysiwyg.element, range);
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({type: "separator"}).element);
|
2022-07-15 12:07:33 +08:00
|
|
|
|
const imagePath = imgElement.getAttribute("data-src");
|
2022-07-15 11:22:14 +08:00
|
|
|
|
if (imagePath.startsWith("assets/")) {
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
label: window.siyuan.languages.rename,
|
|
|
|
|
|
click() {
|
|
|
|
|
|
renameAsset(imagePath);
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
icon: "iconAlignCenter",
|
|
|
|
|
|
label: window.siyuan.languages.alignCenter,
|
|
|
|
|
|
accelerator: window.siyuan.config.keymap.editor.general.alignCenter.custom,
|
|
|
|
|
|
click() {
|
2022-11-21 10:40:29 +08:00
|
|
|
|
alignImgCenter(protyle, nodeElement, [assetElement], id, html);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
icon: "iconAlignLeft",
|
|
|
|
|
|
label: window.siyuan.languages.alignLeft,
|
|
|
|
|
|
accelerator: window.siyuan.config.keymap.editor.general.alignLeft.custom,
|
|
|
|
|
|
click() {
|
2022-11-22 21:54:52 +08:00
|
|
|
|
alignImgLeft(protyle, nodeElement, [assetElement], id, html);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
|
|
|
|
|
const width = parseInt(assetElement.style.width || "0");
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
label: window.siyuan.languages.width,
|
|
|
|
|
|
submenu: [genImageWidthMenu("25%", assetElement, imgElement, protyle, id, nodeElement, html),
|
|
|
|
|
|
genImageWidthMenu("33%", assetElement, imgElement, protyle, id, nodeElement, html),
|
|
|
|
|
|
genImageWidthMenu("50%", assetElement, imgElement, protyle, id, nodeElement, html),
|
|
|
|
|
|
genImageWidthMenu("67%", assetElement, imgElement, protyle, id, nodeElement, html),
|
|
|
|
|
|
genImageWidthMenu("75%", assetElement, imgElement, protyle, id, nodeElement, html),
|
|
|
|
|
|
genImageWidthMenu("100%", assetElement, imgElement, protyle, id, nodeElement, html), {
|
|
|
|
|
|
type: "separator",
|
|
|
|
|
|
}, {
|
|
|
|
|
|
label: `<div aria-label="${width === 0 ? window.siyuan.languages.default : width + "%"}" class="b3-tooltips b3-tooltips__n${isMobile() ? "" : " fn__size200"}">
|
|
|
|
|
|
<input style="box-sizing: border-box" value="${width}" class="b3-slider fn__block" max="100" min="1" step="1" type="range">
|
|
|
|
|
|
</div>`,
|
|
|
|
|
|
bind(element) {
|
|
|
|
|
|
const rangeElement = element.querySelector("input");
|
|
|
|
|
|
rangeElement.addEventListener("input", () => {
|
|
|
|
|
|
assetElement.style.width = rangeElement.value + "%";
|
|
|
|
|
|
imgElement.style.width = "10000px";
|
|
|
|
|
|
assetElement.style.maxWidth = "";
|
|
|
|
|
|
rangeElement.parentElement.setAttribute("aria-label", `${rangeElement.value}%`);
|
|
|
|
|
|
});
|
|
|
|
|
|
rangeElement.addEventListener("change", () => {
|
2022-06-10 00:54:53 +08:00
|
|
|
|
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
|
2022-05-26 15:18:53 +08:00
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, html);
|
|
|
|
|
|
window.siyuan.menus.menu.remove();
|
|
|
|
|
|
focusBlock(nodeElement);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}, {
|
|
|
|
|
|
type: "separator",
|
|
|
|
|
|
},
|
|
|
|
|
|
genImageWidthMenu(window.siyuan.languages.default, assetElement, imgElement, protyle, id, nodeElement, html),
|
|
|
|
|
|
]
|
|
|
|
|
|
}).element);
|
2022-07-27 07:20:27 +08:00
|
|
|
|
const imgSrc = imgElement.getAttribute("src");
|
2022-07-27 07:17:45 +08:00
|
|
|
|
if (imgSrc) {
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({type: "separator"}).element);
|
2022-08-31 20:22:09 +08:00
|
|
|
|
openMenu(imgSrc, false, false);
|
2022-07-27 07:17:45 +08:00
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
window.siyuan.menus.menu.popup({x: position.clientX, y: position.clientY});
|
|
|
|
|
|
window.siyuan.menus.menu.element.querySelector("input").focus();
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2022-06-01 23:52:22 +08:00
|
|
|
|
export const linkMenu = (protyle: IProtyle, linkElement: HTMLElement, focusText = false) => {
|
|
|
|
|
|
window.siyuan.menus.menu.remove();
|
|
|
|
|
|
const nodeElement = hasClosestBlock(linkElement);
|
|
|
|
|
|
if (!nodeElement) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
const id = nodeElement.getAttribute("data-node-id");
|
|
|
|
|
|
let html = nodeElement.outerHTML;
|
|
|
|
|
|
const linkAddress = linkElement.getAttribute("data-href");
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
label: `<div class="fn__hr--small"></div><input class="b3-text-field fn__size200" placeholder="${window.siyuan.languages.link}"><div class="fn__hr--small"></div>`,
|
|
|
|
|
|
bind(element) {
|
|
|
|
|
|
const inputElement = element.querySelector("input");
|
|
|
|
|
|
inputElement.value = linkAddress || "";
|
|
|
|
|
|
inputElement.addEventListener("change", () => {
|
|
|
|
|
|
linkElement.setAttribute("data-href", inputElement.value);
|
|
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, html);
|
|
|
|
|
|
html = nodeElement.outerHTML;
|
|
|
|
|
|
});
|
|
|
|
|
|
inputElement.addEventListener("keydown", (event) => {
|
|
|
|
|
|
if ((event.key === "Enter" || event.key === "Escape") && !event.isComposing) {
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
event.stopPropagation();
|
2022-07-08 11:20:08 +08:00
|
|
|
|
if (linkElement.textContent === "" || linkElement.textContent === Constants.ZWSP) {
|
2022-09-15 22:22:33 +08:00
|
|
|
|
removeLink(linkElement, protyle.toolbar.range);
|
2022-07-08 11:20:08 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
protyle.toolbar.range.selectNodeContents(linkElement);
|
|
|
|
|
|
protyle.toolbar.range.collapse(false);
|
|
|
|
|
|
focusByRange(protyle.toolbar.range);
|
|
|
|
|
|
}
|
2022-06-01 23:52:22 +08:00
|
|
|
|
window.siyuan.menus.menu.remove();
|
2022-07-16 17:07:50 +08:00
|
|
|
|
} else if (event.key === "Tab" && !event.isComposing) {
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
element.nextElementSibling.querySelector("input").focus();
|
2022-08-15 11:44:11 +08:00
|
|
|
|
} else if (electronUndo(event)) {
|
2022-08-15 10:24:37 +08:00
|
|
|
|
return;
|
2022-06-01 23:52:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
label: `<div class="fn__hr--small"></div><input class="b3-text-field fn__size200" placeholder="${window.siyuan.languages.anchor}"><div class="fn__hr--small"></div>`,
|
|
|
|
|
|
bind(element) {
|
|
|
|
|
|
const inputElement = element.querySelector("input");
|
|
|
|
|
|
inputElement.value = linkElement.textContent.replace(Constants.ZWSP, "");
|
|
|
|
|
|
inputElement.addEventListener("change", () => {
|
|
|
|
|
|
if (!inputElement.value) {
|
2022-07-16 17:35:44 +08:00
|
|
|
|
linkElement.remove();
|
2022-06-01 23:52:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, html);
|
|
|
|
|
|
html = nodeElement.outerHTML;
|
|
|
|
|
|
});
|
|
|
|
|
|
inputElement.addEventListener("compositionend", () => {
|
2022-11-11 14:48:45 +08:00
|
|
|
|
linkElement.innerHTML = Lute.EscapeHTMLStr(inputElement.value || "");
|
2022-06-01 23:52:22 +08:00
|
|
|
|
});
|
|
|
|
|
|
inputElement.addEventListener("input", (event: KeyboardEvent) => {
|
|
|
|
|
|
if (!event.isComposing) {
|
2022-09-02 23:29:07 +08:00
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/4511
|
2022-11-11 10:58:16 +08:00
|
|
|
|
linkElement.innerHTML = Lute.EscapeHTMLStr(inputElement.value) || "";
|
2022-06-01 23:52:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
inputElement.addEventListener("keydown", (event) => {
|
|
|
|
|
|
if ((event.key === "Enter" || event.key === "Escape") && !event.isComposing) {
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
event.stopPropagation();
|
2022-07-08 11:20:08 +08:00
|
|
|
|
if (!inputElement.value) {
|
2022-09-15 22:22:33 +08:00
|
|
|
|
removeLink(linkElement, protyle.toolbar.range);
|
2022-07-08 11:20:08 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
protyle.toolbar.range.selectNodeContents(linkElement);
|
|
|
|
|
|
protyle.toolbar.range.collapse(false);
|
|
|
|
|
|
focusByRange(protyle.toolbar.range);
|
|
|
|
|
|
}
|
2022-06-01 23:52:22 +08:00
|
|
|
|
window.siyuan.menus.menu.remove();
|
2022-07-16 17:07:50 +08:00
|
|
|
|
} else if (event.key === "Tab" && !event.isComposing) {
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
if (event.shiftKey) {
|
|
|
|
|
|
element.previousElementSibling.querySelector("input").focus();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
element.nextElementSibling.querySelector("input").focus();
|
|
|
|
|
|
}
|
2022-08-15 11:44:11 +08:00
|
|
|
|
} else if (electronUndo(event)) {
|
2022-08-15 10:24:37 +08:00
|
|
|
|
return;
|
2022-06-01 23:52:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
label: `<div class="fn__hr--small"></div><input class="b3-text-field fn__size200" placeholder="${window.siyuan.languages.title}"><div class="fn__hr--small"></div>`,
|
|
|
|
|
|
bind(element) {
|
|
|
|
|
|
const inputElement = element.querySelector("input");
|
|
|
|
|
|
inputElement.value = Lute.UnEscapeHTMLStr(linkElement.getAttribute("data-title") || "");
|
|
|
|
|
|
inputElement.addEventListener("change", () => {
|
|
|
|
|
|
if (inputElement.value) {
|
|
|
|
|
|
linkElement.setAttribute("data-title", Lute.EscapeHTMLStr(inputElement.value));
|
|
|
|
|
|
} else {
|
|
|
|
|
|
linkElement.removeAttribute("data-title");
|
|
|
|
|
|
}
|
|
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, html);
|
|
|
|
|
|
html = nodeElement.outerHTML;
|
|
|
|
|
|
});
|
|
|
|
|
|
inputElement.addEventListener("keydown", (event) => {
|
|
|
|
|
|
if ((event.key === "Enter" || event.key === "Escape") && !event.isComposing) {
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
event.stopPropagation();
|
2022-07-08 11:20:08 +08:00
|
|
|
|
if (linkElement.textContent === "" || linkElement.textContent === Constants.ZWSP) {
|
2022-09-15 22:22:33 +08:00
|
|
|
|
removeLink(linkElement, protyle.toolbar.range);
|
2022-07-08 11:20:08 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
protyle.toolbar.range.selectNodeContents(linkElement);
|
|
|
|
|
|
protyle.toolbar.range.collapse(false);
|
|
|
|
|
|
focusByRange(protyle.toolbar.range);
|
|
|
|
|
|
}
|
2022-06-01 23:52:22 +08:00
|
|
|
|
window.siyuan.menus.menu.remove();
|
2022-07-16 17:07:50 +08:00
|
|
|
|
} else if (event.key === "Tab" && event.shiftKey && !event.isComposing) {
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
element.previousElementSibling.querySelector("input").focus();
|
2022-08-15 11:44:11 +08:00
|
|
|
|
} else if (electronUndo(event)) {
|
2022-08-15 10:24:37 +08:00
|
|
|
|
return;
|
2022-06-01 23:52:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
2022-08-16 11:38:30 +08:00
|
|
|
|
if (linkAddress) {
|
2022-08-31 20:22:09 +08:00
|
|
|
|
openMenu(linkAddress, false, true);
|
2022-07-15 12:01:59 +08:00
|
|
|
|
}
|
2022-06-01 23:52:22 +08:00
|
|
|
|
if (linkAddress?.startsWith("siyuan://blocks/")) {
|
2022-08-27 21:26:04 +08:00
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
label: `${window.siyuan.languages.turnInto} <b>${window.siyuan.languages.blockRef}</b>`,
|
2022-10-15 16:05:26 +08:00
|
|
|
|
icon: "iconRef",
|
2022-06-01 23:52:22 +08:00
|
|
|
|
click() {
|
|
|
|
|
|
linkElement.setAttribute("data-subtype", "s");
|
2022-09-14 23:26:02 +08:00
|
|
|
|
const types = linkElement.getAttribute("data-type").split(" ");
|
|
|
|
|
|
types.push("block-ref");
|
|
|
|
|
|
types.splice(types.indexOf("a"), 1);
|
|
|
|
|
|
linkElement.setAttribute("data-type", types.join(" "));
|
2022-06-01 23:52:22 +08:00
|
|
|
|
linkElement.setAttribute("data-id", linkAddress?.replace("siyuan://blocks/", ""));
|
|
|
|
|
|
linkElement.removeAttribute("data-href");
|
|
|
|
|
|
linkElement.removeAttribute("data-title");
|
|
|
|
|
|
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
|
|
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, html);
|
|
|
|
|
|
protyle.toolbar.range.selectNode(linkElement);
|
|
|
|
|
|
protyle.toolbar.range.collapse(false);
|
|
|
|
|
|
focusByRange(protyle.toolbar.range);
|
|
|
|
|
|
}
|
2022-08-27 21:26:04 +08:00
|
|
|
|
}).element);
|
2022-06-01 23:52:22 +08:00
|
|
|
|
}
|
2022-08-27 21:26:04 +08:00
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
label: `${window.siyuan.languages.turnInto} <b>${window.siyuan.languages.text}</b>`,
|
|
|
|
|
|
icon: "iconRefresh",
|
2022-08-16 11:38:30 +08:00
|
|
|
|
click() {
|
2022-09-15 11:57:33 +08:00
|
|
|
|
removeLink(linkElement, protyle.toolbar.range);
|
2022-09-24 20:01:52 +08:00
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, html);
|
2022-08-16 11:38:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
|
|
|
|
|
if (linkAddress?.startsWith("assets/")) {
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
label: window.siyuan.languages.rename,
|
|
|
|
|
|
click() {
|
|
|
|
|
|
renameAsset(linkAddress);
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
2022-07-27 07:17:45 +08:00
|
|
|
|
}
|
2022-08-16 11:38:30 +08:00
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
icon: "iconTrashcan",
|
|
|
|
|
|
label: window.siyuan.languages.remove,
|
|
|
|
|
|
click() {
|
|
|
|
|
|
const oldHTML = nodeElement.outerHTML;
|
|
|
|
|
|
linkElement.insertAdjacentHTML("afterend", "<wbr>");
|
|
|
|
|
|
linkElement.remove();
|
|
|
|
|
|
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
|
|
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, oldHTML);
|
|
|
|
|
|
focusByWbr(nodeElement, protyle.toolbar.range);
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
2022-08-02 11:13:11 +08:00
|
|
|
|
const rect = linkElement.getBoundingClientRect();
|
|
|
|
|
|
window.siyuan.menus.menu.popup({
|
|
|
|
|
|
x: rect.left,
|
2022-08-15 11:44:11 +08:00
|
|
|
|
y: rect.top + 26,
|
2022-08-02 11:13:11 +08:00
|
|
|
|
h: 26
|
2022-08-02 11:15:18 +08:00
|
|
|
|
});
|
2022-06-01 23:52:22 +08:00
|
|
|
|
if (focusText || protyle.lute.IsValidLinkDest(linkAddress)) {
|
|
|
|
|
|
window.siyuan.menus.menu.element.querySelectorAll("input")[1].select();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
window.siyuan.menus.menu.element.querySelector("input").select();
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
const genImageWidthMenu = (label: string, assetElement: HTMLElement, imgElement: HTMLElement, protyle: IProtyle, id: string, nodeElement: HTMLElement, html: string) => {
|
|
|
|
|
|
return {
|
|
|
|
|
|
label,
|
|
|
|
|
|
click() {
|
2022-06-10 00:54:53 +08:00
|
|
|
|
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
|
2022-05-26 15:18:53 +08:00
|
|
|
|
assetElement.style.width = label === window.siyuan.languages.default ? "" : label;
|
|
|
|
|
|
imgElement.style.width = label === window.siyuan.languages.default ? "" : "10000px";
|
|
|
|
|
|
assetElement.style.maxWidth = label === window.siyuan.languages.default ? (imgElement.clientWidth + 10) + "px" : "";
|
|
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, html);
|
|
|
|
|
|
focusBlock(nodeElement);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export const iframeMenu = (protyle: IProtyle, nodeElement: Element) => {
|
|
|
|
|
|
const id = nodeElement.getAttribute("data-node-id");
|
|
|
|
|
|
const iframeElement = nodeElement.querySelector("iframe");
|
|
|
|
|
|
let html = nodeElement.outerHTML;
|
|
|
|
|
|
const subMenus: IMenu[] = [{
|
|
|
|
|
|
label: `<div class="fn__hr--small"></div><input style="margin: 4px 0" class="b3-text-field fn__size200" value="${iframeElement.getAttribute("src") || ""}" placeholder="${window.siyuan.languages.link}"><div class="fn__hr--small"></div>`,
|
|
|
|
|
|
bind(element) {
|
|
|
|
|
|
element.querySelector("input").addEventListener("change", (event) => {
|
|
|
|
|
|
const value = (event.target as HTMLInputElement).value;
|
|
|
|
|
|
const biliMatch = value.match(/(?:www\.|\/\/)bilibili\.com\/video\/(\w+)/);
|
|
|
|
|
|
if (value.indexOf("bilibili.com") > -1 && (value.indexOf("bvid=") > -1 || (biliMatch && biliMatch[1]))) {
|
|
|
|
|
|
const params: IObject = {
|
|
|
|
|
|
bvid: value.indexOf("bvid=") > -1 ? value.split("bvid=")[1].split("&")[0] : (biliMatch && biliMatch[1]),
|
|
|
|
|
|
page: "1",
|
|
|
|
|
|
high_quality: "1",
|
|
|
|
|
|
as_wide: "1",
|
|
|
|
|
|
allowfullscreen: "true"
|
|
|
|
|
|
};
|
|
|
|
|
|
if (value.indexOf("player.bilibili.com/player.html") > -1) {
|
|
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/4434
|
|
|
|
|
|
new URL(value.startsWith("http") ? value : "https:" + value).search.split("&").forEach((item, index) => {
|
|
|
|
|
|
if (index === 0) {
|
|
|
|
|
|
item = item.substr(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
const keyValue = item.split("=");
|
|
|
|
|
|
params[keyValue[0]] = keyValue[1];
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
let src = "https://player.bilibili.com/player.html?";
|
|
|
|
|
|
const keys = Object.keys(params);
|
|
|
|
|
|
keys.forEach((key, index) => {
|
|
|
|
|
|
src += `${key}=${params[key]}`;
|
|
|
|
|
|
if (index < keys.length - 1) {
|
|
|
|
|
|
src += "&";
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
iframeElement.setAttribute("src", src);
|
|
|
|
|
|
iframeElement.setAttribute("sandbox", "allow-top-navigation-by-user-activation allow-same-origin allow-forms allow-scripts allow-popups");
|
|
|
|
|
|
if (!iframeElement.style.height) {
|
|
|
|
|
|
iframeElement.style.height = "360px";
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!iframeElement.style.width) {
|
|
|
|
|
|
iframeElement.style.width = "640px";
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
iframeElement.setAttribute("src", value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, html);
|
|
|
|
|
|
html = nodeElement.outerHTML;
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}];
|
2022-07-27 07:17:45 +08:00
|
|
|
|
const iframeSrc = iframeElement.getAttribute("src");
|
|
|
|
|
|
if (iframeSrc) {
|
|
|
|
|
|
subMenus.push({
|
|
|
|
|
|
type: "separator"
|
2022-07-27 07:20:27 +08:00
|
|
|
|
});
|
2022-08-31 20:22:09 +08:00
|
|
|
|
return subMenus.concat(openMenu(iframeSrc, true, false) as IMenu[]);
|
2022-07-27 07:17:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
return subMenus;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export const videoMenu = (protyle: IProtyle, nodeElement: Element, type: string) => {
|
|
|
|
|
|
const id = nodeElement.getAttribute("data-node-id");
|
|
|
|
|
|
const videoElement = nodeElement.querySelector(type === "NodeVideo" ? "video" : "audio");
|
|
|
|
|
|
let html = nodeElement.outerHTML;
|
|
|
|
|
|
const subMenus: IMenu[] = [{
|
|
|
|
|
|
label: `<input style="margin: 4px 0" class="b3-text-field" value="${videoElement.getAttribute("src")}" placeholder="${window.siyuan.languages.link}">`,
|
|
|
|
|
|
bind(element) {
|
|
|
|
|
|
element.querySelector("input").addEventListener("change", (event) => {
|
|
|
|
|
|
const value = (event.target as HTMLInputElement).value;
|
|
|
|
|
|
videoElement.setAttribute("src", value);
|
|
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, html);
|
|
|
|
|
|
html = nodeElement.outerHTML;
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}];
|
2022-07-15 12:07:33 +08:00
|
|
|
|
const src = videoElement.getAttribute("src");
|
2022-07-15 12:01:59 +08:00
|
|
|
|
if (src.startsWith("assets/")) {
|
2022-07-27 07:17:45 +08:00
|
|
|
|
subMenus.push({
|
|
|
|
|
|
type: "separator"
|
2022-07-27 07:20:27 +08:00
|
|
|
|
});
|
2022-07-15 12:01:59 +08:00
|
|
|
|
subMenus.push({
|
|
|
|
|
|
label: window.siyuan.languages.rename,
|
|
|
|
|
|
click() {
|
|
|
|
|
|
renameAsset(src);
|
|
|
|
|
|
}
|
2022-07-15 12:07:33 +08:00
|
|
|
|
});
|
2022-07-15 12:01:59 +08:00
|
|
|
|
}
|
2022-08-23 20:13:41 +08:00
|
|
|
|
/// #if !BROWSER
|
|
|
|
|
|
subMenus.push(exportAsset(src));
|
|
|
|
|
|
/// #endif
|
2022-07-27 07:17:45 +08:00
|
|
|
|
const VideoSrc = videoElement.getAttribute("src");
|
|
|
|
|
|
if (VideoSrc) {
|
2022-08-31 20:22:09 +08:00
|
|
|
|
return subMenus.concat(openMenu(VideoSrc, true, false) as IMenu[]);
|
2022-07-27 07:17:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
return subMenus;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export const tableMenu = (protyle: IProtyle, nodeElement: Element, cellElement: HTMLTableCellElement, range: Range) => {
|
|
|
|
|
|
const menus: IMenu[] = [];
|
|
|
|
|
|
const colIndex = getColIndex(cellElement);
|
|
|
|
|
|
if (cellElement.rowSpan > 1 || cellElement.colSpan > 1) {
|
|
|
|
|
|
menus.push({
|
|
|
|
|
|
label: window.siyuan.languages.cancelMerged,
|
|
|
|
|
|
click: () => {
|
|
|
|
|
|
const oldHTML = nodeElement.outerHTML;
|
|
|
|
|
|
let rowSpan = cellElement.rowSpan;
|
|
|
|
|
|
let currentRowElement: Element = cellElement.parentElement;
|
|
|
|
|
|
const orgColSpan = cellElement.colSpan;
|
|
|
|
|
|
while (rowSpan > 0 && currentRowElement) {
|
|
|
|
|
|
let currentCellElement = currentRowElement.children[colIndex] as HTMLTableCellElement;
|
|
|
|
|
|
let colSpan = orgColSpan;
|
|
|
|
|
|
while (colSpan > 0 && currentCellElement) {
|
|
|
|
|
|
currentCellElement.classList.remove("fn__none");
|
|
|
|
|
|
currentCellElement.colSpan = 1;
|
|
|
|
|
|
currentCellElement.rowSpan = 1;
|
|
|
|
|
|
currentCellElement = currentCellElement.nextElementSibling as HTMLTableCellElement;
|
|
|
|
|
|
colSpan--;
|
|
|
|
|
|
}
|
|
|
|
|
|
currentRowElement = currentRowElement.nextElementSibling;
|
|
|
|
|
|
rowSpan--;
|
|
|
|
|
|
}
|
|
|
|
|
|
cellElement.rowSpan = 1;
|
|
|
|
|
|
cellElement.colSpan = 1;
|
|
|
|
|
|
if (cellElement.tagName === "TH") {
|
|
|
|
|
|
let prueTrElement: HTMLElement;
|
|
|
|
|
|
Array.from(nodeElement.querySelectorAll("thead tr")).find((item: HTMLElement) => {
|
|
|
|
|
|
prueTrElement = item;
|
|
|
|
|
|
Array.from(item.children).forEach((cellElement: HTMLTableCellElement) => {
|
|
|
|
|
|
if (cellElement.rowSpan !== 1 || cellElement.classList.contains("fn__none")) {
|
|
|
|
|
|
prueTrElement = undefined;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
if (prueTrElement) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
if (prueTrElement) {
|
|
|
|
|
|
const tbodyElement = nodeElement.querySelector("tbody");
|
|
|
|
|
|
const theadElement = nodeElement.querySelector("thead");
|
|
|
|
|
|
while (!prueTrElement.isSameNode(theadElement.lastElementChild)) {
|
|
|
|
|
|
tbodyElement.insertAdjacentElement("afterbegin", theadElement.lastElementChild);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-10-21 11:35:12 +08:00
|
|
|
|
focusByRange(range);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
updateTransaction(protyle, nodeElement.getAttribute("data-node-id"), nodeElement.outerHTML, oldHTML);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
const thMatchElement = nodeElement.querySelectorAll("col")[colIndex];
|
|
|
|
|
|
if (thMatchElement.style.width) {
|
|
|
|
|
|
menus.push({
|
|
|
|
|
|
label: window.siyuan.languages.useDefaultWidth,
|
|
|
|
|
|
click: () => {
|
|
|
|
|
|
const html = nodeElement.outerHTML;
|
|
|
|
|
|
thMatchElement.style.width = "";
|
|
|
|
|
|
updateTransaction(protyle, nodeElement.getAttribute("data-node-id"), nodeElement.outerHTML, html);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
if (cellElement.rowSpan > 1 || cellElement.colSpan > 1 || thMatchElement.style.width) {
|
|
|
|
|
|
menus.push({
|
|
|
|
|
|
type: "separator"
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
menus.push({
|
|
|
|
|
|
icon: "iconAlignLeft",
|
|
|
|
|
|
accelerator: window.siyuan.config.keymap.editor.general.alignLeft.custom,
|
|
|
|
|
|
label: window.siyuan.languages.alignLeft,
|
|
|
|
|
|
click: () => {
|
2022-06-12 22:06:19 +08:00
|
|
|
|
setTableAlign(protyle, [cellElement], nodeElement, "left", range);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
menus.push({
|
|
|
|
|
|
icon: "iconAlignCenter",
|
|
|
|
|
|
label: window.siyuan.languages.alignCenter,
|
|
|
|
|
|
accelerator: window.siyuan.config.keymap.editor.general.alignCenter.custom,
|
|
|
|
|
|
click: () => {
|
2022-06-12 22:06:19 +08:00
|
|
|
|
setTableAlign(protyle, [cellElement], nodeElement, "center", range);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
menus.push({
|
|
|
|
|
|
icon: "iconAlignRight",
|
|
|
|
|
|
label: window.siyuan.languages.alignRight,
|
|
|
|
|
|
accelerator: window.siyuan.config.keymap.editor.general.alignRight.custom,
|
|
|
|
|
|
click: () => {
|
2022-06-12 22:06:19 +08:00
|
|
|
|
setTableAlign(protyle, [cellElement], nodeElement, "right", range);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
menus.push({
|
|
|
|
|
|
type: "separator"
|
|
|
|
|
|
});
|
|
|
|
|
|
const tableElement = nodeElement.querySelector("table");
|
|
|
|
|
|
const hasNone = cellElement.parentElement.querySelector(".fn__none");
|
|
|
|
|
|
let hasColSpan = false;
|
|
|
|
|
|
let hasRowSpan = false;
|
|
|
|
|
|
Array.from(cellElement.parentElement.children).forEach((item: HTMLTableCellElement) => {
|
|
|
|
|
|
if (item.colSpan > 1) {
|
|
|
|
|
|
hasColSpan = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (item.rowSpan > 1) {
|
|
|
|
|
|
hasRowSpan = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
let previousHasNone: false | Element = false;
|
|
|
|
|
|
let previousHasColSpan = false;
|
|
|
|
|
|
let previousHasRowSpan = false;
|
|
|
|
|
|
let previousRowElement = cellElement.parentElement.previousElementSibling;
|
|
|
|
|
|
if (!previousRowElement && cellElement.parentElement.parentElement.tagName === "TBODY") {
|
|
|
|
|
|
previousRowElement = tableElement.querySelector("thead").lastElementChild;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (previousRowElement) {
|
|
|
|
|
|
previousHasNone = previousRowElement.querySelector(".fn__none");
|
|
|
|
|
|
Array.from(previousRowElement.children).forEach((item: HTMLTableCellElement) => {
|
|
|
|
|
|
if (item.colSpan > 1) {
|
|
|
|
|
|
previousHasColSpan = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (item.rowSpan > 1) {
|
|
|
|
|
|
previousHasRowSpan = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
let nextHasNone: false | Element = false;
|
|
|
|
|
|
let nextHasColSpan = false;
|
|
|
|
|
|
let nextHasRowSpan = false;
|
|
|
|
|
|
let nextRowElement = cellElement.parentElement.nextElementSibling;
|
|
|
|
|
|
if (!nextRowElement && cellElement.parentElement.parentElement.tagName === "THEAD") {
|
|
|
|
|
|
nextRowElement = tableElement.querySelector("tbody")?.firstElementChild;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (nextRowElement) {
|
|
|
|
|
|
nextHasNone = nextRowElement.querySelector(".fn__none");
|
|
|
|
|
|
Array.from(nextRowElement.children).forEach((item: HTMLTableCellElement) => {
|
|
|
|
|
|
if (item.colSpan > 1) {
|
|
|
|
|
|
nextHasColSpan = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (item.rowSpan > 1) {
|
|
|
|
|
|
nextHasRowSpan = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
let colIsPure = true;
|
|
|
|
|
|
Array.from(tableElement.rows).find(item => {
|
|
|
|
|
|
const cellElement = item.cells[colIndex];
|
|
|
|
|
|
if (cellElement.classList.contains("fn__none") || cellElement.colSpan > 1 || cellElement.rowSpan > 1) {
|
|
|
|
|
|
colIsPure = false;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
let nextColIsPure = true;
|
|
|
|
|
|
Array.from(tableElement.rows).find(item => {
|
|
|
|
|
|
const cellElement = item.cells[colIndex + 1];
|
|
|
|
|
|
if (cellElement && (cellElement.classList.contains("fn__none") || cellElement.colSpan > 1 || cellElement.rowSpan > 1)) {
|
|
|
|
|
|
nextColIsPure = false;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
let previousColIsPure = true;
|
|
|
|
|
|
Array.from(tableElement.rows).find(item => {
|
|
|
|
|
|
const cellElement = item.cells[colIndex - 1];
|
|
|
|
|
|
if (cellElement && (cellElement.classList.contains("fn__none") || cellElement.colSpan > 1 || cellElement.rowSpan > 1)) {
|
|
|
|
|
|
previousColIsPure = false;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
menus.push({
|
|
|
|
|
|
icon: "iconBefore",
|
|
|
|
|
|
label: window.siyuan.languages.insertRowAbove,
|
|
|
|
|
|
accelerator: window.siyuan.config.keymap.editor.table.insertRowAbove.custom,
|
|
|
|
|
|
click: () => {
|
|
|
|
|
|
insertRowAbove(protyle, range, cellElement, nodeElement);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
if (!nextHasNone || (nextHasNone && !nextHasRowSpan && nextHasColSpan)) {
|
|
|
|
|
|
menus.push({
|
|
|
|
|
|
icon: "iconAfter",
|
|
|
|
|
|
label: window.siyuan.languages.insertRowBelow,
|
|
|
|
|
|
accelerator: window.siyuan.config.keymap.editor.table.insertRowBelow.custom,
|
|
|
|
|
|
click: () => {
|
|
|
|
|
|
insertRow(protyle, range, cellElement, nodeElement);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
if (colIsPure || previousColIsPure) {
|
|
|
|
|
|
menus.push({
|
|
|
|
|
|
icon: "iconInsertLeft",
|
|
|
|
|
|
label: window.siyuan.languages.insertColumnLeft,
|
|
|
|
|
|
accelerator: window.siyuan.config.keymap.editor.table.insertColumnLeft.custom,
|
|
|
|
|
|
click: () => {
|
|
|
|
|
|
insertColumn(protyle, nodeElement, cellElement, "beforebegin", range);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
if (colIsPure || nextColIsPure) {
|
|
|
|
|
|
menus.push({
|
|
|
|
|
|
icon: "iconInsertRight",
|
|
|
|
|
|
label: window.siyuan.languages.insertColumnRight,
|
|
|
|
|
|
accelerator: window.siyuan.config.keymap.editor.table.insertColumnRight.custom,
|
|
|
|
|
|
click: () => {
|
|
|
|
|
|
insertColumn(protyle, nodeElement, cellElement, "afterend", range);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
if (((!hasNone || (hasNone && !hasRowSpan && hasColSpan)) &&
|
|
|
|
|
|
(!previousHasNone || (previousHasNone && !previousHasRowSpan && previousHasColSpan))) ||
|
|
|
|
|
|
((!hasNone || (hasNone && !hasRowSpan && hasColSpan)) &&
|
|
|
|
|
|
(!nextHasNone || (nextHasNone && !nextHasRowSpan && nextHasColSpan))) ||
|
|
|
|
|
|
(colIsPure && previousColIsPure) ||
|
|
|
|
|
|
(colIsPure && nextColIsPure)
|
|
|
|
|
|
) {
|
|
|
|
|
|
menus.push({
|
|
|
|
|
|
type: "separator"
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ((!hasNone || (hasNone && !hasRowSpan && hasColSpan)) &&
|
|
|
|
|
|
(!previousHasNone || (previousHasNone && !previousHasRowSpan && previousHasColSpan))) {
|
|
|
|
|
|
menus.push({
|
|
|
|
|
|
icon: "iconUp",
|
|
|
|
|
|
label: window.siyuan.languages.moveToUp,
|
|
|
|
|
|
accelerator: window.siyuan.config.keymap.editor.table.moveToUp.custom,
|
|
|
|
|
|
click: () => {
|
|
|
|
|
|
moveRowToUp(protyle, range, cellElement, nodeElement);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
if ((!hasNone || (hasNone && !hasRowSpan && hasColSpan)) &&
|
|
|
|
|
|
(!nextHasNone || (nextHasNone && !nextHasRowSpan && nextHasColSpan))) {
|
|
|
|
|
|
menus.push({
|
|
|
|
|
|
icon: "iconDown",
|
|
|
|
|
|
label: window.siyuan.languages.moveToDown,
|
|
|
|
|
|
accelerator: window.siyuan.config.keymap.editor.table.moveToDown.custom,
|
|
|
|
|
|
click: () => {
|
|
|
|
|
|
moveRowToDown(protyle, range, cellElement, nodeElement);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
if (colIsPure && previousColIsPure) {
|
|
|
|
|
|
menus.push({
|
|
|
|
|
|
icon: "iconLeft",
|
|
|
|
|
|
label: window.siyuan.languages.moveToLeft,
|
|
|
|
|
|
accelerator: window.siyuan.config.keymap.editor.table.moveToLeft.custom,
|
|
|
|
|
|
click: () => {
|
|
|
|
|
|
moveColumnToLeft(protyle, range, cellElement, nodeElement);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
if (colIsPure && nextColIsPure) {
|
|
|
|
|
|
menus.push({
|
|
|
|
|
|
icon: "iconRight",
|
|
|
|
|
|
label: window.siyuan.languages.moveToRight,
|
|
|
|
|
|
accelerator: window.siyuan.config.keymap.editor.table.moveToRight.custom,
|
|
|
|
|
|
click: () => {
|
|
|
|
|
|
moveColumnToRight(protyle, range, cellElement, nodeElement);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2022-06-10 16:35:08 +08:00
|
|
|
|
if ((cellElement.parentElement.parentElement.tagName !== "THEAD" &&
|
|
|
|
|
|
((!hasNone && !hasRowSpan) || (hasNone && !hasRowSpan && hasColSpan))) || colIsPure) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
menus.push({
|
|
|
|
|
|
type: "separator"
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2022-06-10 16:35:08 +08:00
|
|
|
|
if (cellElement.parentElement.parentElement.tagName !== "THEAD" &&
|
|
|
|
|
|
((!hasNone && !hasRowSpan) || (hasNone && !hasRowSpan && hasColSpan))) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
menus.push({
|
|
|
|
|
|
icon: "iconDeleteRow",
|
|
|
|
|
|
label: window.siyuan.languages["delete-row"],
|
|
|
|
|
|
accelerator: window.siyuan.config.keymap.editor.table["delete-row"].custom,
|
|
|
|
|
|
click: () => {
|
|
|
|
|
|
deleteRow(protyle, range, cellElement, nodeElement);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
if (colIsPure) {
|
|
|
|
|
|
menus.push({
|
|
|
|
|
|
icon: "iconDeleteColumn",
|
|
|
|
|
|
label: window.siyuan.languages["delete-column"],
|
|
|
|
|
|
accelerator: window.siyuan.config.keymap.editor.table["delete-column"].custom,
|
|
|
|
|
|
click: () => {
|
|
|
|
|
|
deleteColumn(protyle, range, nodeElement, cellElement);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
return menus;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export const setFold = (protyle: IProtyle, nodeElement: Element, isOpen?: boolean, isRemove?: boolean) => {
|
|
|
|
|
|
if (nodeElement.getAttribute("data-type") === "NodeListItem" && nodeElement.childElementCount < 4) {
|
|
|
|
|
|
// 没有子列表或多个块的列表项不进行折叠
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
2022-10-08 16:13:24 +08:00
|
|
|
|
if (nodeElement.getAttribute("data-type") === "NodeThematicBreak") {
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
// 0 正常;1 折叠
|
|
|
|
|
|
let fold = "0";
|
|
|
|
|
|
if (nodeElement.getAttribute("fold") === "1") {
|
|
|
|
|
|
if (typeof isOpen === "boolean" && !isOpen) {
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
nodeElement.removeAttribute("fold");
|
|
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/4411
|
|
|
|
|
|
nodeElement.querySelectorAll(".protyle-linenumber").forEach((item: HTMLElement) => {
|
|
|
|
|
|
lineNumberRender(item);
|
|
|
|
|
|
});
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if (typeof isOpen === "boolean" && isOpen) {
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
fold = "1";
|
|
|
|
|
|
nodeElement.setAttribute("fold", "1");
|
|
|
|
|
|
// 光标在子列表中,再次 focus 段尾的时候不会变 https://ld246.com/article/1647099132461
|
|
|
|
|
|
if (getSelection().rangeCount > 0) {
|
|
|
|
|
|
const range = getSelection().getRangeAt(0);
|
|
|
|
|
|
const blockElement = hasClosestBlock(range.startContainer);
|
|
|
|
|
|
if (blockElement && blockElement.getBoundingClientRect().width === 0) {
|
2022-09-06 23:03:43 +08:00
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/5833
|
|
|
|
|
|
focusBlock(nodeElement, undefined, false);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
const id = nodeElement.getAttribute("data-node-id");
|
|
|
|
|
|
if (nodeElement.getAttribute("data-type") === "NodeHeading") {
|
|
|
|
|
|
if (fold === "0") {
|
|
|
|
|
|
nodeElement.insertAdjacentHTML("beforeend", "<div spin=\"1\" style=\"text-align: center\"><img width=\"24px\" src=\"/stage/loading-pure.svg\"></div>");
|
|
|
|
|
|
transaction(protyle, [{
|
|
|
|
|
|
action: "unfoldHeading",
|
|
|
|
|
|
id,
|
|
|
|
|
|
data: isRemove ? "remove" : undefined,
|
|
|
|
|
|
}], [{
|
|
|
|
|
|
action: "foldHeading",
|
|
|
|
|
|
id
|
|
|
|
|
|
}]);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
transaction(protyle, [{
|
|
|
|
|
|
action: "foldHeading",
|
|
|
|
|
|
id
|
|
|
|
|
|
}], [{
|
|
|
|
|
|
action: "unfoldHeading",
|
|
|
|
|
|
id
|
|
|
|
|
|
}]);
|
|
|
|
|
|
removeFoldHeading(nodeElement);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
transaction(protyle, [{
|
|
|
|
|
|
action: "setAttrs",
|
|
|
|
|
|
id,
|
|
|
|
|
|
data: JSON.stringify({fold})
|
|
|
|
|
|
}], [{
|
|
|
|
|
|
action: "setAttrs",
|
|
|
|
|
|
id,
|
|
|
|
|
|
data: JSON.stringify({fold: fold === "0" ? "1" : "0"})
|
|
|
|
|
|
}]);
|
|
|
|
|
|
}
|
|
|
|
|
|
// 折叠后,防止滚动条滚动后调用 get 请求 https://github.com/siyuan-note/siyuan/issues/2248
|
|
|
|
|
|
preventScroll(protyle);
|
|
|
|
|
|
return fold;
|
|
|
|
|
|
};
|