2022-05-26 15:18:53 +08:00
|
|
|
|
import {Divider} from "./Divider";
|
2022-09-14 19:58:16 +08:00
|
|
|
|
import {Font, hasSameTextStyle, setFontStyle} from "./Font";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
import {ToolbarItem} from "./ToolbarItem";
|
|
|
|
|
|
import {
|
2022-09-19 16:47:38 +08:00
|
|
|
|
fixTableRange,
|
2022-05-26 15:18:53 +08:00
|
|
|
|
focusByRange,
|
|
|
|
|
|
focusByWbr,
|
|
|
|
|
|
focusSideBlock,
|
|
|
|
|
|
getEditorRange,
|
2022-07-25 11:17:28 +08:00
|
|
|
|
getSelectionPosition,
|
|
|
|
|
|
setFirstNodeRange,
|
|
|
|
|
|
setLastNodeRange
|
2022-05-26 15:18:53 +08:00
|
|
|
|
} from "../util/selection";
|
2022-09-19 23:56:47 +08:00
|
|
|
|
import {hasClosestBlock, hasClosestByClassName} from "../util/hasClosest";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
import {Link} from "./Link";
|
|
|
|
|
|
import {setPosition} from "../../util/setPosition";
|
|
|
|
|
|
import {updateTransaction} from "../wysiwyg/transaction";
|
|
|
|
|
|
import {Constants} from "../../constants";
|
|
|
|
|
|
import {getEventName} from "../util/compatibility";
|
|
|
|
|
|
import {upDownHint} from "../../util/upDownHint";
|
|
|
|
|
|
import {highlightRender} from "../markdown/highlightRender";
|
2022-07-25 11:17:28 +08:00
|
|
|
|
import {getContenteditableElement, hasNextSibling, hasPreviousSibling} from "../wysiwyg/getBlock";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
import {processRender} from "../util/processCode";
|
|
|
|
|
|
import {BlockRef} from "./BlockRef";
|
2022-09-15 22:22:33 +08:00
|
|
|
|
import {hintMoveBlock, hintRenderAssets, hintRenderTemplate, hintRenderWidget} from "../hint/extend";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
import {blockRender} from "../markdown/blockRender";
|
|
|
|
|
|
/// #if !BROWSER
|
|
|
|
|
|
import {clipboard, nativeImage, NativeImage} from "electron";
|
|
|
|
|
|
import {getCurrentWindow} from "@electron/remote";
|
|
|
|
|
|
/// #endif
|
|
|
|
|
|
import {fetchPost} from "../../util/fetch";
|
2022-09-13 23:31:12 +08:00
|
|
|
|
import {isArrayEqual, isBrowser, isMobile} from "../../util/functions";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
import * as dayjs from "dayjs";
|
|
|
|
|
|
import {insertEmptyBlock} from "../../block/util";
|
|
|
|
|
|
import {matchHotKey} from "../util/hotKey";
|
|
|
|
|
|
import {unicode2Emoji} from "../../emoji";
|
|
|
|
|
|
import {escapeHtml} from "../../util/escape";
|
|
|
|
|
|
import {hideElements} from "../ui/hideElements";
|
2022-07-16 23:20:50 +08:00
|
|
|
|
import {renderAssetsPreview} from "../../asset/renderAssets";
|
2022-08-15 10:24:37 +08:00
|
|
|
|
import {electronUndo} from "../undo";
|
2022-08-18 20:07:58 +08:00
|
|
|
|
import {previewTemplate} from "./util";
|
2022-08-28 10:28:47 +08:00
|
|
|
|
import {showMessage} from "../../dialog/message";
|
2022-09-15 22:48:03 +08:00
|
|
|
|
import {InlineMath} from "./InlineMath";
|
2022-09-17 23:14:02 +08:00
|
|
|
|
import {InlineMemo} from "./InlineMemo";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
|
|
|
|
|
export class Toolbar {
|
|
|
|
|
|
public element: HTMLElement;
|
|
|
|
|
|
public subElement: HTMLElement;
|
2022-09-20 18:28:09 +08:00
|
|
|
|
public subElementCloseCB: () => void;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
public range: Range;
|
|
|
|
|
|
private toolbarHeight: number;
|
|
|
|
|
|
|
|
|
|
|
|
constructor(protyle: IProtyle) {
|
|
|
|
|
|
const options = protyle.options;
|
|
|
|
|
|
|
|
|
|
|
|
const element = document.createElement("div");
|
|
|
|
|
|
element.className = "protyle-toolbar fn__none";
|
|
|
|
|
|
this.element = element;
|
|
|
|
|
|
this.subElement = document.createElement("div");
|
|
|
|
|
|
this.subElement.className = "protyle-util fn__none";
|
|
|
|
|
|
this.toolbarHeight = 29;
|
|
|
|
|
|
|
|
|
|
|
|
options.toolbar.forEach((menuItem: IMenuItem) => {
|
|
|
|
|
|
const itemElement = this.genItem(protyle, menuItem);
|
|
|
|
|
|
this.element.appendChild(itemElement);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public render(protyle: IProtyle, range: Range, event?: KeyboardEvent) {
|
|
|
|
|
|
this.range = range;
|
|
|
|
|
|
const nodeElement = hasClosestBlock(range.startContainer);
|
|
|
|
|
|
if (!nodeElement || protyle.disabled) {
|
|
|
|
|
|
this.element.classList.add("fn__none");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-06-11 00:50:51 +08:00
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/5157
|
|
|
|
|
|
let hasImg = true;
|
|
|
|
|
|
let noText = true;
|
|
|
|
|
|
Array.from(range.cloneContents().childNodes).find(item => {
|
|
|
|
|
|
if (item.nodeType !== 1) {
|
|
|
|
|
|
if (item.textContent.length > 0) {
|
2022-06-11 00:51:42 +08:00
|
|
|
|
noText = false;
|
|
|
|
|
|
return true;
|
2022-06-11 00:50:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else if (!(item as HTMLElement).classList.contains("img")) {
|
2022-06-11 00:51:42 +08:00
|
|
|
|
hasImg = false;
|
|
|
|
|
|
return true;
|
2022-06-11 00:50:51 +08:00
|
|
|
|
}
|
2022-06-11 00:51:42 +08:00
|
|
|
|
});
|
2022-06-11 00:50:51 +08:00
|
|
|
|
if (hasImg && noText) {
|
|
|
|
|
|
this.element.classList.add("fn__none");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
// shift+方向键或三击选中,不同的块 https://github.com/siyuan-note/siyuan/issues/3891
|
|
|
|
|
|
const startElement = hasClosestBlock(range.startContainer);
|
|
|
|
|
|
const endElement = hasClosestBlock(range.endContainer);
|
|
|
|
|
|
if (startElement && endElement && !startElement.isSameNode(endElement)) {
|
|
|
|
|
|
if (event) { // 在 keyup 中使用 shift+方向键选中
|
|
|
|
|
|
if (event.key === "ArrowLeft") {
|
|
|
|
|
|
this.range = setLastNodeRange(getContenteditableElement(startElement), range, false);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.range = setFirstNodeRange(getContenteditableElement(endElement), range);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.range = setLastNodeRange(getContenteditableElement(nodeElement), range, false);
|
|
|
|
|
|
}
|
|
|
|
|
|
focusByRange(this.range);
|
|
|
|
|
|
if (this.range.toString() === "") {
|
|
|
|
|
|
this.element.classList.add("fn__none");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// 需放在 range 修改之后,否则 https://github.com/siyuan-note/siyuan/issues/4726
|
|
|
|
|
|
if (nodeElement.getAttribute("data-type") === "NodeCodeBlock") {
|
|
|
|
|
|
this.element.classList.add("fn__none");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
const rangePosition = getSelectionPosition(nodeElement, range);
|
|
|
|
|
|
this.element.classList.remove("fn__none");
|
2022-08-06 18:01:20 +08:00
|
|
|
|
const y = rangePosition.top - this.toolbarHeight - 4;
|
2022-08-03 18:08:28 +08:00
|
|
|
|
this.element.setAttribute("data-inity", y + Constants.ZWSP + protyle.contentElement.scrollTop.toString());
|
|
|
|
|
|
setPosition(this.element, rangePosition.left - 52, y);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
this.element.querySelectorAll(".protyle-toolbar__item--current").forEach(item => {
|
|
|
|
|
|
item.classList.remove("protyle-toolbar__item--current");
|
|
|
|
|
|
});
|
|
|
|
|
|
const types = this.getCurrentType();
|
|
|
|
|
|
types.forEach(item => {
|
2022-09-17 23:14:02 +08:00
|
|
|
|
if (["a", "block-ref", "text", "file-annotation-ref", "inline-math", "inline-memo", ""].includes(item)) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
this.element.querySelector(`[data-type="${item}"]`).classList.add("protyle-toolbar__item--current");
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-17 17:29:43 +08:00
|
|
|
|
public getCurrentType(range = this.range) {
|
2022-09-13 10:19:13 +08:00
|
|
|
|
let types: string[] = [];
|
2022-05-26 15:18:53 +08:00
|
|
|
|
let startElement = range.startContainer as HTMLElement;
|
|
|
|
|
|
if (startElement.nodeType === 3) {
|
|
|
|
|
|
startElement = startElement.parentElement;
|
|
|
|
|
|
} else if (startElement.childElementCount > 0 && startElement.childNodes[range.startOffset]?.nodeType !== 3) {
|
|
|
|
|
|
startElement = startElement.childNodes[range.startOffset] as HTMLElement;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!startElement || startElement.nodeType === 3) {
|
|
|
|
|
|
return [];
|
|
|
|
|
|
}
|
2022-09-16 09:41:37 +08:00
|
|
|
|
if (!["DIV", "TD", "TH", "TR"].includes(startElement.tagName)) {
|
2022-09-17 17:29:43 +08:00
|
|
|
|
types = (startElement.getAttribute("data-type") || "").split(" ");
|
2022-09-13 10:19:13 +08:00
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
let endElement = range.endContainer as HTMLElement;
|
|
|
|
|
|
if (endElement.nodeType === 3) {
|
|
|
|
|
|
endElement = endElement.parentElement;
|
|
|
|
|
|
} else if (endElement.childElementCount > 0 && endElement.childNodes[range.endOffset]?.nodeType !== 3) {
|
|
|
|
|
|
endElement = endElement.childNodes[range.endOffset] as HTMLElement;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!endElement || endElement.nodeType === 3) {
|
|
|
|
|
|
return [];
|
|
|
|
|
|
}
|
2022-09-16 09:41:37 +08:00
|
|
|
|
if (!["DIV", "TD", "TH", "TR"].includes(endElement.tagName) && !startElement.isSameNode(endElement)) {
|
2022-09-15 20:19:56 +08:00
|
|
|
|
types = types.concat((endElement.getAttribute("data-type") || "").split(" "));
|
2022-09-13 10:19:13 +08:00
|
|
|
|
}
|
2022-09-13 23:31:12 +08:00
|
|
|
|
range.cloneContents().childNodes.forEach((item: HTMLElement) => {
|
2022-09-13 10:19:13 +08:00
|
|
|
|
if (item.nodeType !== 3) {
|
2022-09-16 09:41:37 +08:00
|
|
|
|
types = types.concat((item.getAttribute("data-type") || "").split(" "));
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2022-09-13 10:19:13 +08:00
|
|
|
|
});
|
|
|
|
|
|
types = [...new Set(types)];
|
2022-09-16 09:41:37 +08:00
|
|
|
|
types.find((item, index) => {
|
|
|
|
|
|
if (item === "") {
|
|
|
|
|
|
types.splice(index, 1);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return types;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private genItem(protyle: IProtyle, menuItem: IMenuItem) {
|
|
|
|
|
|
let menuItemObj;
|
|
|
|
|
|
switch (menuItem.name) {
|
2022-09-13 10:19:13 +08:00
|
|
|
|
case "strong":
|
|
|
|
|
|
case "em":
|
|
|
|
|
|
case "s":
|
|
|
|
|
|
case "code":
|
2022-05-26 15:18:53 +08:00
|
|
|
|
case "mark":
|
|
|
|
|
|
case "tag":
|
2022-09-13 10:19:13 +08:00
|
|
|
|
case "u":
|
2022-05-26 15:18:53 +08:00
|
|
|
|
case "sup":
|
2022-09-21 11:03:32 +08:00
|
|
|
|
case "clear":
|
2022-05-26 15:18:53 +08:00
|
|
|
|
case "sub":
|
|
|
|
|
|
case "kbd":
|
|
|
|
|
|
menuItemObj = new ToolbarItem(protyle, menuItem);
|
|
|
|
|
|
break;
|
2022-09-13 10:19:13 +08:00
|
|
|
|
case "block-ref":
|
2022-05-26 15:18:53 +08:00
|
|
|
|
menuItemObj = new BlockRef(protyle, menuItem);
|
|
|
|
|
|
break;
|
2022-09-15 22:48:03 +08:00
|
|
|
|
case "inline-math":
|
|
|
|
|
|
menuItemObj = new InlineMath(protyle, menuItem);
|
|
|
|
|
|
break;
|
2022-09-17 23:14:02 +08:00
|
|
|
|
case "inline-memo":
|
|
|
|
|
|
menuItemObj = new InlineMemo(protyle, menuItem);
|
|
|
|
|
|
break;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
case "|":
|
|
|
|
|
|
menuItemObj = new Divider();
|
|
|
|
|
|
break;
|
2022-09-13 10:19:13 +08:00
|
|
|
|
case "text":
|
2022-05-26 15:18:53 +08:00
|
|
|
|
menuItemObj = new Font(protyle, menuItem);
|
|
|
|
|
|
break;
|
2022-09-13 10:19:13 +08:00
|
|
|
|
case "a":
|
2022-05-26 15:18:53 +08:00
|
|
|
|
menuItemObj = new Link(protyle, menuItem);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!menuItemObj) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
return menuItemObj.element;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-14 19:58:16 +08:00
|
|
|
|
// 合并多个 text 为一个 text
|
|
|
|
|
|
private mergeNode(nodes: NodeListOf<ChildNode>) {
|
|
|
|
|
|
for (let i = 0; i < nodes.length; i++) {
|
|
|
|
|
|
if (nodes[i].nodeType !== 3 && (nodes[i] as HTMLElement).tagName === "WBR") {
|
|
|
|
|
|
nodes[i].remove();
|
|
|
|
|
|
i--;
|
|
|
|
|
|
}
|
2022-09-14 18:33:17 +08:00
|
|
|
|
}
|
2022-09-14 19:58:16 +08:00
|
|
|
|
for (let i = 0; i < nodes.length; i++) {
|
|
|
|
|
|
if (nodes[i].nodeType === 3) {
|
|
|
|
|
|
if (nodes[i].textContent === "") {
|
|
|
|
|
|
nodes[i].remove();
|
|
|
|
|
|
i--;
|
|
|
|
|
|
} else if (nodes[i + 1] && nodes[i + 1].nodeType === 3) {
|
|
|
|
|
|
nodes[i].textContent = nodes[i].textContent + nodes[i + 1].textContent;
|
|
|
|
|
|
nodes[i + 1].remove();
|
|
|
|
|
|
i--;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-09-14 18:33:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-15 11:57:33 +08:00
|
|
|
|
public setInlineMark(protyle: IProtyle, type: string, action: "range" | "toolbar", textObj?: ITextOption) {
|
2022-09-23 21:42:14 +08:00
|
|
|
|
if (["a", "block-ref", "inline-math", "inline-memo"].includes(type) || (type === "text" && !textObj)) {
|
2022-09-18 20:20:53 +08:00
|
|
|
|
protyle.toolbar.element.querySelector(`[data-type="${type}"]`).dispatchEvent(new CustomEvent("block-ref" === type ? getEventName() : "click"));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-09-13 10:19:13 +08:00
|
|
|
|
const nodeElement = hasClosestBlock(this.range.startContainer);
|
|
|
|
|
|
if (!nodeElement) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-09-17 17:29:43 +08:00
|
|
|
|
const rangeTypes = this.getCurrentType(this.range);
|
2022-09-15 20:19:56 +08:00
|
|
|
|
const selectText = this.range.toString();
|
2022-09-19 16:47:38 +08:00
|
|
|
|
fixTableRange(this.range);
|
2022-09-14 18:33:17 +08:00
|
|
|
|
let previousElement: HTMLElement;
|
|
|
|
|
|
let nextElement: HTMLElement;
|
2022-09-14 11:18:44 +08:00
|
|
|
|
let previousIndex: number;
|
|
|
|
|
|
let nextIndex: number;
|
|
|
|
|
|
const previousSibling = hasPreviousSibling(this.range.startContainer);
|
2022-09-16 09:41:37 +08:00
|
|
|
|
if (!["DIV", "TD", "TH", "TR"].includes(this.range.startContainer.parentElement.tagName)) {
|
2022-09-13 23:31:12 +08:00
|
|
|
|
if (this.range.startOffset === 0 && !previousSibling) {
|
2022-09-14 18:33:17 +08:00
|
|
|
|
previousElement = this.range.startContainer.parentElement.previousSibling as HTMLElement;
|
2022-09-14 11:18:44 +08:00
|
|
|
|
this.range.setStartBefore(this.range.startContainer.parentElement);
|
2022-09-13 23:31:12 +08:00
|
|
|
|
} else {
|
2022-09-14 11:18:44 +08:00
|
|
|
|
previousElement = this.range.startContainer.parentElement;
|
2022-09-13 23:31:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else if (previousSibling && previousSibling.nodeType !== 3 && this.range.startOffset === 0) {
|
|
|
|
|
|
// **aaa**bbb 选中 bbb 加粗
|
2022-09-14 18:33:17 +08:00
|
|
|
|
previousElement = previousSibling as HTMLElement;
|
2022-09-13 23:31:12 +08:00
|
|
|
|
}
|
2022-09-14 11:18:44 +08:00
|
|
|
|
const nextSibling = hasNextSibling(this.range.endContainer);
|
2022-09-16 09:41:37 +08:00
|
|
|
|
if (!["DIV", "TD", "TH", "TR"].includes(this.range.endContainer.parentElement.tagName)) {
|
2022-09-13 23:31:12 +08:00
|
|
|
|
if (this.range.endOffset === this.range.endContainer.textContent.length && !nextSibling) {
|
2022-09-14 18:33:17 +08:00
|
|
|
|
nextElement = this.range.endContainer.parentElement.nextSibling as HTMLElement;
|
2022-09-14 11:18:44 +08:00
|
|
|
|
this.range.setEndAfter(this.range.endContainer.parentElement);
|
2022-09-13 23:31:12 +08:00
|
|
|
|
} else {
|
2022-09-14 11:18:44 +08:00
|
|
|
|
nextElement = this.range.endContainer.parentElement;
|
2022-09-13 23:31:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else if (nextSibling && nextSibling.nodeType !== 3 && this.range.endOffset === this.range.endContainer.textContent.length) {
|
|
|
|
|
|
// aaa**bbb** 选中 aaa 加粗
|
2022-09-14 18:33:17 +08:00
|
|
|
|
nextElement = nextSibling as HTMLElement;
|
2022-09-13 23:31:12 +08:00
|
|
|
|
}
|
2022-09-16 17:29:04 +08:00
|
|
|
|
this.range.insertNode(document.createElement("wbr"));
|
2022-09-13 10:19:13 +08:00
|
|
|
|
const html = nodeElement.outerHTML;
|
2022-09-13 23:31:12 +08:00
|
|
|
|
const contents = this.range.extractContents();
|
2022-09-14 19:58:16 +08:00
|
|
|
|
this.mergeNode(contents.childNodes);
|
2022-09-16 17:29:04 +08:00
|
|
|
|
// 选择 span 中的一部分需进行包裹
|
2022-09-17 17:29:43 +08:00
|
|
|
|
if (previousElement && nextElement && previousElement.isSameNode(nextElement) && contents.firstChild?.nodeType === 3) {
|
2022-09-16 17:29:04 +08:00
|
|
|
|
const attributes = previousElement.attributes;
|
|
|
|
|
|
contents.childNodes.forEach(item => {
|
|
|
|
|
|
const spanElement = document.createElement("span");
|
|
|
|
|
|
for (let i = 0; i < attributes.length; i++) {
|
|
|
|
|
|
spanElement.setAttribute(attributes[i].name, attributes[i].value);
|
|
|
|
|
|
}
|
|
|
|
|
|
spanElement.innerHTML = item.textContent;
|
|
|
|
|
|
item.replaceWith(spanElement);
|
2022-09-18 17:51:19 +08:00
|
|
|
|
});
|
2022-09-16 17:29:04 +08:00
|
|
|
|
}
|
2022-09-13 10:19:13 +08:00
|
|
|
|
const actionBtn = action === "toolbar" ? this.element.querySelector(`[data-type="${type}"]`) : undefined;
|
2022-09-16 09:41:37 +08:00
|
|
|
|
const newNodes: Node[] = [];
|
2022-09-17 17:29:43 +08:00
|
|
|
|
|
2022-09-21 11:03:32 +08:00
|
|
|
|
if (type === "clear" || actionBtn?.classList.contains("protyle-toolbar__item--current") || (
|
|
|
|
|
|
action === "range" && rangeTypes.length > 0 && rangeTypes.includes(type) && !textObj
|
2022-09-15 20:19:56 +08:00
|
|
|
|
)) {
|
2022-09-14 18:33:17 +08:00
|
|
|
|
// 移除
|
2022-09-21 11:03:32 +08:00
|
|
|
|
if (type === "clear") {
|
|
|
|
|
|
this.element.querySelectorAll('[data-type="em"],[data-type="u"],[data-type="s"],[data-type="mark"],[data-type="sup"],[data-type="sub"],[data-type="strong"]').forEach(item => {
|
|
|
|
|
|
item.classList.remove("protyle-toolbar__item--current");
|
|
|
|
|
|
});
|
|
|
|
|
|
} else if (actionBtn) {
|
2022-09-14 09:49:42 +08:00
|
|
|
|
actionBtn.classList.remove("protyle-toolbar__item--current");
|
|
|
|
|
|
}
|
2022-09-15 22:22:33 +08:00
|
|
|
|
if (contents.childNodes.length === 0) {
|
|
|
|
|
|
rangeTypes.find((itemType, index) => {
|
|
|
|
|
|
if (type === itemType) {
|
|
|
|
|
|
rangeTypes.splice(index, 1);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
if (rangeTypes.length === 0) {
|
|
|
|
|
|
newNodes.push(document.createTextNode(Constants.ZWSP));
|
|
|
|
|
|
} else {
|
|
|
|
|
|
const inlineElement = document.createElement("span");
|
|
|
|
|
|
inlineElement.setAttribute("data-type", rangeTypes.join(" "));
|
|
|
|
|
|
inlineElement.textContent = Constants.ZWSP;
|
|
|
|
|
|
newNodes.push(inlineElement);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-09-13 23:31:12 +08:00
|
|
|
|
contents.childNodes.forEach((item: HTMLElement, index) => {
|
2022-09-16 09:41:37 +08:00
|
|
|
|
if (item.nodeType !== 3 && item.tagName !== "BR") {
|
2022-09-13 23:31:12 +08:00
|
|
|
|
const types = item.getAttribute("data-type").split(" ");
|
2022-09-21 11:03:32 +08:00
|
|
|
|
if (type === "clear") {
|
|
|
|
|
|
for (let i = 0; i < types.length; i++) {
|
|
|
|
|
|
if (["strong", "em", "u", "s", "mark", "sup", "sub"].includes(types[i])) {
|
|
|
|
|
|
types.splice(i, 1);
|
|
|
|
|
|
i--;
|
|
|
|
|
|
}
|
2022-09-13 23:31:12 +08:00
|
|
|
|
}
|
2022-09-21 11:03:32 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
types.find((itemType, typeIndex) => {
|
|
|
|
|
|
if (type === itemType) {
|
|
|
|
|
|
types.splice(typeIndex, 1);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2022-09-13 23:31:12 +08:00
|
|
|
|
if (types.length === 0) {
|
2022-09-17 17:29:43 +08:00
|
|
|
|
if (item.textContent === "") {
|
|
|
|
|
|
item.textContent = Constants.ZWSP;
|
|
|
|
|
|
}
|
2022-09-13 23:31:12 +08:00
|
|
|
|
newNodes.push(document.createTextNode(item.textContent));
|
|
|
|
|
|
} else {
|
2022-09-21 11:03:32 +08:00
|
|
|
|
if (type === "clear") {
|
2022-09-14 19:08:24 +08:00
|
|
|
|
item.style.color = "";
|
|
|
|
|
|
item.style.webkitTextFillColor = "";
|
|
|
|
|
|
item.style.webkitTextStroke = "";
|
|
|
|
|
|
item.style.textShadow = "";
|
|
|
|
|
|
item.style.backgroundColor = "";
|
2022-09-21 11:03:32 +08:00
|
|
|
|
item.style.fontSize = "";
|
2022-09-14 19:08:24 +08:00
|
|
|
|
}
|
2022-09-14 18:33:17 +08:00
|
|
|
|
if (index === 0 && previousElement && previousElement.nodeType !== 3 &&
|
|
|
|
|
|
isArrayEqual(types, previousElement.getAttribute("data-type").split(" ")) &&
|
2022-09-14 19:58:16 +08:00
|
|
|
|
hasSameTextStyle(item, previousElement, textObj)) {
|
2022-09-13 23:31:12 +08:00
|
|
|
|
previousIndex = previousElement.textContent.length;
|
|
|
|
|
|
previousElement.innerHTML = previousElement.innerHTML + item.innerHTML;
|
2022-09-14 18:33:17 +08:00
|
|
|
|
} else if (index === contents.childNodes.length - 1 && nextElement && nextElement.nodeType !== 3 &&
|
|
|
|
|
|
isArrayEqual(types, nextElement.getAttribute("data-type").split(" ")) &&
|
2022-09-14 19:58:16 +08:00
|
|
|
|
hasSameTextStyle(item, nextElement, textObj)) {
|
2022-09-13 23:31:12 +08:00
|
|
|
|
nextIndex = item.textContent.length;
|
|
|
|
|
|
nextElement.innerHTML = item.innerHTML + nextElement.innerHTML;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
item.setAttribute("data-type", types.join(" "));
|
|
|
|
|
|
newNodes.push(item);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
newNodes.push(item);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
} else {
|
2022-09-14 18:33:17 +08:00
|
|
|
|
// 添加
|
2022-09-14 11:17:41 +08:00
|
|
|
|
if (!this.element.classList.contains("fn__none") && type !== "text") {
|
2022-09-14 09:49:42 +08:00
|
|
|
|
this.element.querySelector(`[data-type="${type}"]`).classList.add("protyle-toolbar__item--current");
|
|
|
|
|
|
}
|
2022-09-15 20:19:56 +08:00
|
|
|
|
if (selectText === "") {
|
|
|
|
|
|
const inlineElement = document.createElement("span");
|
2022-09-16 10:13:01 +08:00
|
|
|
|
rangeTypes.push(type);
|
2022-09-15 22:22:33 +08:00
|
|
|
|
inlineElement.setAttribute("data-type", [...new Set(rangeTypes)].join(" "));
|
2022-09-15 20:19:56 +08:00
|
|
|
|
inlineElement.textContent = Constants.ZWSP;
|
2022-09-15 22:22:33 +08:00
|
|
|
|
newNodes.push(inlineElement);
|
2022-09-15 20:19:56 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
contents.childNodes.forEach((item: HTMLElement, index) => {
|
|
|
|
|
|
if (item.nodeType === 3) {
|
|
|
|
|
|
if (index === 0 && previousElement && previousElement.nodeType !== 3 &&
|
|
|
|
|
|
type === previousElement.getAttribute("data-type") &&
|
|
|
|
|
|
hasSameTextStyle(item, previousElement, textObj)) {
|
|
|
|
|
|
previousIndex = previousElement.textContent.length;
|
|
|
|
|
|
previousElement.innerHTML = previousElement.innerHTML + item.textContent;
|
|
|
|
|
|
} else if (index === contents.childNodes.length - 1 && nextElement && nextElement.nodeType !== 3 &&
|
|
|
|
|
|
type === nextElement.getAttribute("data-type") &&
|
|
|
|
|
|
hasSameTextStyle(item, nextElement, textObj)) {
|
|
|
|
|
|
nextIndex = item.textContent.length;
|
|
|
|
|
|
nextElement.innerHTML = item.textContent + nextElement.innerHTML;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
const inlineElement = document.createElement("span");
|
|
|
|
|
|
inlineElement.setAttribute("data-type", type);
|
|
|
|
|
|
inlineElement.textContent = item.textContent;
|
|
|
|
|
|
setFontStyle(inlineElement, textObj);
|
|
|
|
|
|
newNodes.push(inlineElement);
|
|
|
|
|
|
}
|
2022-09-13 23:31:12 +08:00
|
|
|
|
} else {
|
2022-09-15 20:19:56 +08:00
|
|
|
|
let types = (item.getAttribute("data-type") || "").split(" ");
|
|
|
|
|
|
types.push(type);
|
2022-09-19 11:54:29 +08:00
|
|
|
|
// 上标和下标不能同时存在 https://github.com/siyuan-note/insider/issues/1049
|
|
|
|
|
|
if (type === "sub" && types.includes("sup")) {
|
|
|
|
|
|
types.find((item, index) => {
|
|
|
|
|
|
if (item === "sup") {
|
2022-09-19 15:33:30 +08:00
|
|
|
|
types.splice(index, 1);
|
2022-09-19 11:54:29 +08:00
|
|
|
|
if (!this.element.classList.contains("fn__none")) {
|
2022-09-19 15:33:30 +08:00
|
|
|
|
this.element.querySelector("[data-type=\"sup\"]").classList.remove("protyle-toolbar__item--current");
|
2022-09-19 11:54:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2022-09-19 15:33:30 +08:00
|
|
|
|
});
|
2022-09-19 11:54:29 +08:00
|
|
|
|
} else if (type === "sup" && types.includes("sub")) {
|
|
|
|
|
|
types.find((item, index) => {
|
|
|
|
|
|
if (item === "sub") {
|
2022-09-19 15:10:11 +08:00
|
|
|
|
types.splice(index, 1);
|
2022-09-19 11:54:29 +08:00
|
|
|
|
if (!this.element.classList.contains("fn__none")) {
|
2022-09-19 15:33:30 +08:00
|
|
|
|
this.element.querySelector("[data-type=\"sub\"]").classList.remove("protyle-toolbar__item--current");
|
2022-09-19 11:54:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2022-09-19 15:33:30 +08:00
|
|
|
|
});
|
2022-09-19 11:54:29 +08:00
|
|
|
|
}
|
2022-09-15 20:19:56 +08:00
|
|
|
|
types = [...new Set(types)];
|
|
|
|
|
|
if (index === 0 && previousElement && previousElement.nodeType !== 3 &&
|
|
|
|
|
|
isArrayEqual(types, previousElement.getAttribute("data-type").split(" ")) &&
|
|
|
|
|
|
hasSameTextStyle(item, previousElement, textObj)) {
|
|
|
|
|
|
previousIndex = previousElement.textContent.length;
|
|
|
|
|
|
previousElement.innerHTML = previousElement.innerHTML + item.innerHTML;
|
|
|
|
|
|
} else if (index === contents.childNodes.length - 1 && nextElement && nextElement.nodeType !== 3 &&
|
|
|
|
|
|
isArrayEqual(types, nextElement.getAttribute("data-type").split(" ")) &&
|
|
|
|
|
|
hasSameTextStyle(item, nextElement, textObj)) {
|
|
|
|
|
|
nextIndex = item.textContent.length;
|
|
|
|
|
|
nextElement.innerHTML = item.innerHTML + nextElement.innerHTML;
|
2022-09-16 10:13:01 +08:00
|
|
|
|
} else if (item.tagName !== "BR") {
|
2022-09-15 20:19:56 +08:00
|
|
|
|
item.setAttribute("data-type", types.join(" "));
|
|
|
|
|
|
setFontStyle(item, textObj);
|
|
|
|
|
|
newNodes.push(item);
|
2022-09-16 09:41:37 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
newNodes.push(item);
|
2022-09-15 20:19:56 +08:00
|
|
|
|
}
|
2022-09-13 23:31:12 +08:00
|
|
|
|
}
|
2022-09-15 20:19:56 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
2022-09-13 23:31:12 +08:00
|
|
|
|
}
|
2022-09-15 22:22:33 +08:00
|
|
|
|
if (this.range.startContainer.nodeType !== 3 && (this.range.startContainer as HTMLElement).tagName === "SPAN" &&
|
|
|
|
|
|
this.range.startContainer.isSameNode(this.range.endContainer)) {
|
|
|
|
|
|
// 切割元素
|
|
|
|
|
|
const startContainer = this.range.startContainer as HTMLElement;
|
|
|
|
|
|
const afterElement = document.createElement("span");
|
2022-09-16 11:54:27 +08:00
|
|
|
|
const attributes = startContainer.attributes;
|
|
|
|
|
|
for (let i = 0; i < attributes.length; i++) {
|
|
|
|
|
|
afterElement.setAttribute(attributes[i].name, attributes[i].value);
|
|
|
|
|
|
}
|
2022-09-15 22:22:33 +08:00
|
|
|
|
this.range.setEnd(startContainer.lastChild, startContainer.lastChild.textContent.length);
|
|
|
|
|
|
afterElement.append(this.range.extractContents());
|
|
|
|
|
|
startContainer.after(afterElement);
|
|
|
|
|
|
this.range.setStartBefore(afterElement);
|
|
|
|
|
|
this.range.collapse(true);
|
|
|
|
|
|
}
|
2022-09-16 11:54:27 +08:00
|
|
|
|
for (let i = 0; i < newNodes.length; i++) {
|
2022-09-18 17:51:19 +08:00
|
|
|
|
const currentNewNode = newNodes[i] as HTMLElement;
|
|
|
|
|
|
const nextNewNode = newNodes[i + 1] as HTMLElement;
|
2022-09-16 11:54:27 +08:00
|
|
|
|
if (currentNewNode.nodeType !== 3 && nextNewNode && nextNewNode.nodeType !== 3 &&
|
|
|
|
|
|
isArrayEqual(nextNewNode.getAttribute("data-type").split(" "), currentNewNode.getAttribute("data-type").split(" ")) &&
|
|
|
|
|
|
currentNewNode.style.color === nextNewNode.style.color &&
|
|
|
|
|
|
currentNewNode.style.webkitTextFillColor === nextNewNode.style.webkitTextFillColor &&
|
|
|
|
|
|
currentNewNode.style.webkitTextStroke === nextNewNode.style.webkitTextStroke &&
|
|
|
|
|
|
currentNewNode.style.textShadow === nextNewNode.style.textShadow &&
|
2022-09-21 11:03:32 +08:00
|
|
|
|
currentNewNode.style.fontSize === nextNewNode.style.fontSize &&
|
2022-09-16 11:54:27 +08:00
|
|
|
|
currentNewNode.style.backgroundColor === nextNewNode.style.backgroundColor) {
|
|
|
|
|
|
// 合并相同的 node
|
|
|
|
|
|
nextNewNode.innerHTML = currentNewNode.innerHTML + nextNewNode.innerHTML;
|
|
|
|
|
|
newNodes.splice(i, 1);
|
|
|
|
|
|
i--;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.range.insertNode(newNodes[i]);
|
|
|
|
|
|
this.range.collapse(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-09-14 19:58:16 +08:00
|
|
|
|
if (previousElement) {
|
|
|
|
|
|
this.mergeNode(previousElement.childNodes);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (nextElement) {
|
|
|
|
|
|
this.mergeNode(nextElement.childNodes);
|
|
|
|
|
|
}
|
2022-09-13 23:31:12 +08:00
|
|
|
|
if (previousIndex) {
|
|
|
|
|
|
this.range.setStart(previousElement.firstChild, previousIndex);
|
|
|
|
|
|
} else if (newNodes.length > 0) {
|
|
|
|
|
|
if (newNodes[0].firstChild) {
|
|
|
|
|
|
this.range.setStart(newNodes[0].firstChild, 0);
|
2022-09-16 09:41:37 +08:00
|
|
|
|
} else if (newNodes[0].nodeType === 3) {
|
2022-09-13 23:31:12 +08:00
|
|
|
|
this.range.setStart(newNodes[0], 0);
|
2022-09-16 09:41:37 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
this.range.setStartBefore(newNodes[0]);
|
2022-09-13 10:19:13 +08:00
|
|
|
|
}
|
2022-09-15 20:19:56 +08:00
|
|
|
|
} else if (nextElement) {
|
2022-09-13 23:31:12 +08:00
|
|
|
|
// aaa**bbb** 选中 aaa 加粗
|
|
|
|
|
|
this.range.setStart(nextElement.firstChild, 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (nextIndex) {
|
|
|
|
|
|
this.range.setEnd(nextElement.lastChild, nextIndex);
|
|
|
|
|
|
} else if (newNodes.length > 0) {
|
2022-09-14 11:18:44 +08:00
|
|
|
|
const lastNewNode = newNodes[newNodes.length - 1];
|
2022-09-13 23:31:12 +08:00
|
|
|
|
if (lastNewNode.lastChild) {
|
|
|
|
|
|
this.range.setEnd(lastNewNode.lastChild, lastNewNode.lastChild.textContent.length);
|
2022-09-16 09:41:37 +08:00
|
|
|
|
} else if (lastNewNode.nodeType === 3) {
|
2022-09-13 23:31:12 +08:00
|
|
|
|
this.range.setEnd(lastNewNode, lastNewNode.textContent.length);
|
2022-09-19 11:42:16 +08:00
|
|
|
|
if (lastNewNode.textContent === Constants.ZWSP) {
|
2022-09-19 11:54:29 +08:00
|
|
|
|
// 粗体后取消粗体光标不存在 https://github.com/siyuan-note/insider/issues/1056
|
2022-09-19 11:42:16 +08:00
|
|
|
|
this.range.collapse(false);
|
|
|
|
|
|
}
|
2022-09-16 09:41:37 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
// eg: 表格中有3行时,选中第二行三级,多次加粗会增加换行
|
|
|
|
|
|
this.range.setEndAfter(lastNewNode);
|
2022-09-13 10:19:13 +08:00
|
|
|
|
}
|
2022-09-15 20:19:56 +08:00
|
|
|
|
} else if (previousElement) {
|
2022-09-13 23:31:12 +08:00
|
|
|
|
// **aaa**bbb 选中 bbb 加粗
|
2022-09-14 19:58:16 +08:00
|
|
|
|
// 需进行 mergeNode ,否用 alt+x 为相同颜色 aaabbb 中的 bbb 再次赋值后无法选中
|
2022-09-13 23:31:12 +08:00
|
|
|
|
this.range.setEnd(previousElement.firstChild, previousElement.firstChild.textContent.length);
|
|
|
|
|
|
}
|
2022-09-13 10:19:13 +08:00
|
|
|
|
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
|
|
|
|
|
|
updateTransaction(protyle, nodeElement.getAttribute("data-node-id"), nodeElement.outerHTML, html);
|
2022-09-18 17:51:19 +08:00
|
|
|
|
const wbrElement = nodeElement.querySelector("wbr");
|
2022-09-16 17:29:04 +08:00
|
|
|
|
if (wbrElement) {
|
|
|
|
|
|
wbrElement.remove();
|
|
|
|
|
|
}
|
2022-09-13 10:19:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
public showFileAnnotationRef(protyle: IProtyle, refElement: HTMLElement) {
|
|
|
|
|
|
const nodeElement = hasClosestBlock(refElement);
|
|
|
|
|
|
if (!nodeElement) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
const id = nodeElement.getAttribute("data-node-id");
|
2022-09-20 21:16:02 +08:00
|
|
|
|
const html = nodeElement.outerHTML;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
this.subElement.style.width = isMobile() ? "80vw" : Math.min(480, window.innerWidth) + "px";
|
|
|
|
|
|
this.subElement.style.padding = "";
|
|
|
|
|
|
this.subElement.innerHTML = `<div class="b3-form__space--small">
|
|
|
|
|
|
<label class="fn__flex">
|
|
|
|
|
|
<span class="ft__on-surface fn__flex-center" style="width: 64px">ID</span>
|
|
|
|
|
|
<div class="fn__space"></div>
|
|
|
|
|
|
<input data-type="id" value="${refElement.getAttribute("data-id") || ""}" class="b3-text-field fn__block" readonly />
|
|
|
|
|
|
</label>
|
|
|
|
|
|
<div class="fn__hr"></div>
|
|
|
|
|
|
<label class="fn__flex">
|
|
|
|
|
|
<span class="ft__on-surface fn__flex-center" style="width: 64px">${window.siyuan.languages.anchor}</span>
|
|
|
|
|
|
<div class="fn__space"></div>
|
|
|
|
|
|
<input data-type="anchor" class="b3-text-field fn__block" placeholder="${window.siyuan.languages.anchor}" />
|
|
|
|
|
|
</label>
|
|
|
|
|
|
<div class="fn__hr"></div>
|
|
|
|
|
|
<div class="fn__hr"></div>
|
|
|
|
|
|
<div class="fn__flex"><span class="fn__flex-1"></span>
|
|
|
|
|
|
<button class="b3-button b3-button--cancel">${window.siyuan.languages.remove}</button>
|
|
|
|
|
|
</div></div>`;
|
|
|
|
|
|
this.subElement.querySelector(".b3-button--cancel").addEventListener(getEventName(), () => {
|
2022-09-20 21:16:02 +08:00
|
|
|
|
refElement.outerHTML = refElement.textContent + "<wbr>";
|
2022-09-23 23:30:01 +08:00
|
|
|
|
hideElements(["util"], protyle);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
});
|
|
|
|
|
|
const anchorElement = this.subElement.querySelector('[data-type="anchor"]') as HTMLInputElement;
|
2022-09-14 23:44:14 +08:00
|
|
|
|
anchorElement.value = refElement.textContent;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
anchorElement.addEventListener("input", (event) => {
|
2022-09-20 21:16:02 +08:00
|
|
|
|
if (anchorElement.value) {
|
|
|
|
|
|
refElement.innerHTML = Lute.EscapeHTMLStr(anchorElement.value);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
refElement.innerHTML = "*";
|
|
|
|
|
|
}
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
});
|
|
|
|
|
|
anchorElement.addEventListener("keydown", (event: KeyboardEvent) => {
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
if (event.isComposing) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (event.key === "Enter" || event.key === "Escape") {
|
2022-09-23 23:30:01 +08:00
|
|
|
|
hideElements(["util"], protyle);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
this.subElement.classList.remove("fn__none");
|
2022-09-20 21:16:02 +08:00
|
|
|
|
this.subElementCloseCB = () => {
|
|
|
|
|
|
if (refElement.parentElement) {
|
|
|
|
|
|
if (anchorElement.value) {
|
|
|
|
|
|
refElement.innerHTML = Lute.EscapeHTMLStr(anchorElement.value);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
refElement.innerHTML = "*";
|
|
|
|
|
|
}
|
2022-09-23 23:30:01 +08:00
|
|
|
|
this.range.setStartAfter(refElement);
|
2022-09-20 21:16:02 +08:00
|
|
|
|
focusByRange(this.range);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
focusByWbr(nodeElement, this.range);
|
|
|
|
|
|
}
|
|
|
|
|
|
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
|
|
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, html);
|
|
|
|
|
|
};
|
2022-05-26 15:18:53 +08:00
|
|
|
|
const nodeRect = refElement.getBoundingClientRect();
|
|
|
|
|
|
setPosition(this.subElement, nodeRect.left, nodeRect.bottom, nodeRect.height + 4);
|
|
|
|
|
|
this.element.classList.add("fn__none");
|
|
|
|
|
|
anchorElement.select();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-20 18:28:09 +08:00
|
|
|
|
public showRender(protyle: IProtyle, renderElement: Element, updateElements?: Element[], oldHTML?: string) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
const nodeElement = hasClosestBlock(renderElement);
|
|
|
|
|
|
if (!nodeElement) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
const id = nodeElement.getAttribute("data-node-id");
|
2022-09-20 18:28:09 +08:00
|
|
|
|
const types = (renderElement.getAttribute("data-type") || "").split(" ");
|
|
|
|
|
|
const html = oldHTML || protyle.lute.SpinBlockDOM(nodeElement.outerHTML);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
let title = "HTML";
|
|
|
|
|
|
let placeholder = "";
|
2022-09-20 18:28:09 +08:00
|
|
|
|
const isInlineMemo = types.includes("inline-memo");
|
2022-05-26 15:18:53 +08:00
|
|
|
|
switch (renderElement.getAttribute("data-subtype")) {
|
|
|
|
|
|
case "abc":
|
|
|
|
|
|
title = window.siyuan.languages.staff;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "echarts":
|
|
|
|
|
|
title = window.siyuan.languages.chart;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "flowchart":
|
|
|
|
|
|
title = "Flow Chart";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "graphviz":
|
|
|
|
|
|
title = "Graphviz";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "mermaid":
|
|
|
|
|
|
title = "Mermaid";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "mindmap":
|
|
|
|
|
|
placeholder = `- foo
|
|
|
|
|
|
- bar
|
|
|
|
|
|
- baz`;
|
|
|
|
|
|
title = window.siyuan.languages.mindmap;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "plantuml":
|
|
|
|
|
|
title = "UML";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "math":
|
2022-09-20 18:28:09 +08:00
|
|
|
|
if (types.includes("NodeMathBlock")) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
title = window.siyuan.languages.math;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
title = window.siyuan.languages["inline-math"];
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2022-09-20 18:28:09 +08:00
|
|
|
|
if (types.includes("NodeBlockQueryEmbed")) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
title = window.siyuan.languages.blockEmbed;
|
2022-09-19 10:05:40 +08:00
|
|
|
|
} else if (isInlineMemo) {
|
2022-09-17 23:14:02 +08:00
|
|
|
|
title = window.siyuan.languages.memo;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2022-06-20 21:47:12 +08:00
|
|
|
|
const isPin = this.subElement.querySelector('[data-type="pin"]')?.classList.contains("block__icon--active");
|
2022-05-26 15:18:53 +08:00
|
|
|
|
const pinData: IObject = {};
|
|
|
|
|
|
if (isPin) {
|
|
|
|
|
|
const textElement = this.subElement.querySelector(".b3-text-field") as HTMLTextAreaElement;
|
|
|
|
|
|
pinData.styleH = textElement.style.height;
|
|
|
|
|
|
pinData.styleW = textElement.style.width;
|
|
|
|
|
|
} else {
|
2022-09-03 11:20:36 +08:00
|
|
|
|
this.subElement.style.width = isMobile() ? "100vw" : "";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
this.subElement.style.padding = "0";
|
|
|
|
|
|
}
|
|
|
|
|
|
this.subElement.innerHTML = `<div ${(isPin && this.subElement.firstElementChild.getAttribute("data-drag") === "true") ? 'data-drag="true"' : ""} class="block__popover--move"><div class="block__icons block__icons--border fn__flex">
|
|
|
|
|
|
${title}
|
|
|
|
|
|
<span class="fn__flex-1"></span>
|
2022-09-20 18:28:09 +08:00
|
|
|
|
<label aria-label="${window.siyuan.languages.hideHeadingBelowBlocks}" style="overflow:inherit;" class="b3-tooltips b3-tooltips__nw${!types.includes("NodeBlockQueryEmbed") ? " fn__none" : ""}">
|
2022-05-26 15:18:53 +08:00
|
|
|
|
<input type="checkbox" class="b3-switch">
|
|
|
|
|
|
<span class="fn__space"></span>
|
|
|
|
|
|
</label>
|
2022-09-20 18:28:09 +08:00
|
|
|
|
<button data-type="refresh" class="block__icon b3-tooltips b3-tooltips__nw${(isPin && !this.subElement.querySelector('[data-type="refresh"]').classList.contains("block__icon--active")) ? "" : " block__icon--active"}${types.includes("NodeBlockQueryEmbed") ? " fn__none" : ""}" aria-label="${window.siyuan.languages.refresh}"><svg><use xlink:href="#iconRefresh"></use></svg></button>
|
2022-05-26 15:18:53 +08:00
|
|
|
|
<span class="fn__space"></span>
|
|
|
|
|
|
<button data-type="before" class="block__icon b3-tooltips b3-tooltips__nw" aria-label="${window.siyuan.languages["insert-before"]}"><svg><use xlink:href="#iconBefore"></use></svg></button>
|
|
|
|
|
|
<span class="fn__space"></span>
|
|
|
|
|
|
<button data-type="after" class="block__icon b3-tooltips b3-tooltips__nw" aria-label="${window.siyuan.languages["insert-after"]}"><svg><use xlink:href="#iconAfter"></use></svg></button>
|
|
|
|
|
|
<span class="fn__space"></span>
|
2022-09-19 10:05:40 +08:00
|
|
|
|
<button data-type="copy" class="block__icon b3-tooltips b3-tooltips__nw${(isBrowser() || isInlineMemo) ? " fn__none" : ""}" aria-label="${window.siyuan.languages.copy} PNG"><svg><use xlink:href="#iconCopy"></use></svg></button>
|
2022-05-26 15:18:53 +08:00
|
|
|
|
<span class="fn__space"></span>
|
2022-06-20 21:47:12 +08:00
|
|
|
|
<button data-type="pin" class="block__icon b3-tooltips b3-tooltips__nw${isPin ? " block__icon--active" : ""}" aria-label="${window.siyuan.languages.pin}"><svg><use xlink:href="#iconPin"></use></svg></button>
|
2022-05-26 15:18:53 +08:00
|
|
|
|
<span class="fn__space"></span>
|
|
|
|
|
|
<button data-type="close" class="block__icon b3-tooltips b3-tooltips__nw" aria-label="${window.siyuan.languages.close}"><svg style="width: 10px"><use xlink:href="#iconClose"></use></svg></button>
|
|
|
|
|
|
</div>
|
2022-09-03 11:22:59 +08:00
|
|
|
|
<textarea spellcheck="false" class="b3-text-field b3-text-field--text fn__block" placeholder="${placeholder}" style="width:${isMobile() ? "100vw" : Math.max(480, renderElement.clientWidth * 0.7) + "px"};max-height:50vh"></textarea></div>`;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
const autoHeight = () => {
|
|
|
|
|
|
textElement.style.height = textElement.scrollHeight + "px";
|
2022-09-03 11:20:36 +08:00
|
|
|
|
if (isMobile()) {
|
|
|
|
|
|
setPosition(this.subElement, 0, 0);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if (this.subElement.firstElementChild.getAttribute("data-drag") === "true") {
|
|
|
|
|
|
if (textElement.getBoundingClientRect().bottom > window.innerHeight) {
|
|
|
|
|
|
this.subElement.style.top = window.innerHeight - this.subElement.clientHeight + "px";
|
|
|
|
|
|
}
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (this.subElement.clientHeight <= window.innerHeight - nodeRect.bottom || this.subElement.clientHeight <= nodeRect.top) {
|
2022-09-20 18:28:09 +08:00
|
|
|
|
if (types.includes("inline-math") || isInlineMemo) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
setPosition(this.subElement, nodeRect.left, nodeRect.bottom, nodeRect.height);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
setPosition(this.subElement, nodeRect.left + (nodeRect.width - this.subElement.clientWidth) / 2, nodeRect.bottom, nodeRect.height);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
setPosition(this.subElement, nodeRect.right, nodeRect.bottom);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2022-08-21 17:38:09 +08:00
|
|
|
|
const headerElement = this.subElement.querySelector(".block__icons");
|
2022-08-19 14:39:19 +08:00
|
|
|
|
headerElement.addEventListener("click", (event: MouseEvent) => {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
const target = event.target as HTMLElement;
|
|
|
|
|
|
const btnElement = hasClosestByClassName(target, "b3-tooltips");
|
|
|
|
|
|
if (!btnElement) {
|
2022-08-19 14:39:19 +08:00
|
|
|
|
if (event.detail === 2) {
|
|
|
|
|
|
const pingElement = headerElement.querySelector('[data-type="pin"]');
|
|
|
|
|
|
if (pingElement.classList.contains("block__icon--active")) {
|
|
|
|
|
|
pingElement.classList.remove("block__icon--active");
|
|
|
|
|
|
pingElement.setAttribute("aria-label", window.siyuan.languages.pin);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
pingElement.classList.add("block__icon--active");
|
|
|
|
|
|
pingElement.setAttribute("aria-label", window.siyuan.languages.unpin);
|
|
|
|
|
|
}
|
2022-08-21 17:38:09 +08:00
|
|
|
|
event.preventDefault();
|
2022-08-19 17:34:47 +08:00
|
|
|
|
event.stopPropagation();
|
2022-08-19 14:39:19 +08:00
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
switch (btnElement.getAttribute("data-type")) {
|
|
|
|
|
|
case "close":
|
2022-06-20 21:47:12 +08:00
|
|
|
|
this.subElement.querySelector('[data-type="pin"]').classList.remove("block__icon--active");
|
2022-09-20 18:40:05 +08:00
|
|
|
|
hideElements(["util"], protyle);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case "pin":
|
2022-08-19 14:39:19 +08:00
|
|
|
|
if (btnElement.classList.contains("block__icon--active")) {
|
|
|
|
|
|
btnElement.classList.remove("block__icon--active");
|
|
|
|
|
|
btnElement.setAttribute("aria-label", window.siyuan.languages.pin);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
btnElement.classList.add("block__icon--active");
|
|
|
|
|
|
btnElement.setAttribute("aria-label", window.siyuan.languages.unpin);
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case "refresh":
|
2022-06-20 21:47:12 +08:00
|
|
|
|
btnElement.classList.toggle("block__icon--active");
|
2022-05-26 15:18:53 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case "before":
|
|
|
|
|
|
insertEmptyBlock(protyle, "beforebegin", id);
|
|
|
|
|
|
hideElements(["util"], protyle);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "after":
|
|
|
|
|
|
insertEmptyBlock(protyle, "afterend", id);
|
|
|
|
|
|
hideElements(["util"], protyle);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "copy":
|
|
|
|
|
|
/// #if !BROWSER
|
|
|
|
|
|
hideElements(["util"], protyle);
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
const rect = renderElement.getBoundingClientRect();
|
|
|
|
|
|
getCurrentWindow().webContents.capturePage({
|
|
|
|
|
|
x: Math.floor(rect.x),
|
|
|
|
|
|
y: Math.floor(rect.y) - 4, // 行内数学公式头部截不到
|
|
|
|
|
|
width: Math.floor(rect.width),
|
|
|
|
|
|
height: Math.floor(rect.height) + 4
|
|
|
|
|
|
}).then((image: NativeImage) => {
|
|
|
|
|
|
clipboard.writeImage(nativeImage.createFromBuffer(image.toPNG()));
|
|
|
|
|
|
});
|
|
|
|
|
|
}, 100);
|
|
|
|
|
|
/// #endif
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2022-08-19 14:39:19 +08:00
|
|
|
|
headerElement.addEventListener("mousedown", (event: MouseEvent) => {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if (hasClosestByClassName(event.target as HTMLElement, "block__icon")) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
const documentSelf = document;
|
|
|
|
|
|
this.subElement.style.userSelect = "none";
|
|
|
|
|
|
const x = event.clientX - parseInt(this.subElement.style.left);
|
|
|
|
|
|
const y = event.clientY - parseInt(this.subElement.style.top);
|
2022-08-19 17:34:47 +08:00
|
|
|
|
documentSelf.onmousemove = (moveEvent: MouseEvent) => {
|
|
|
|
|
|
let positionX = moveEvent.clientX - x;
|
|
|
|
|
|
let positionY = moveEvent.clientY - y;
|
|
|
|
|
|
if (positionX > window.innerWidth - this.subElement.clientWidth) {
|
|
|
|
|
|
positionX = window.innerWidth - this.subElement.clientWidth;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (positionY > window.innerHeight - this.subElement.clientHeight) {
|
|
|
|
|
|
positionY = window.innerHeight - this.subElement.clientHeight;
|
|
|
|
|
|
}
|
|
|
|
|
|
this.subElement.style.left = Math.max(positionX, 0) + "px";
|
|
|
|
|
|
this.subElement.style.top = Math.max(positionY, Constants.SIZE_TOOLBAR_HEIGHT) + "px";
|
|
|
|
|
|
this.subElement.firstElementChild.setAttribute("data-drag", "true");
|
|
|
|
|
|
};
|
|
|
|
|
|
documentSelf.onmouseup = () => {
|
|
|
|
|
|
this.subElement.style.userSelect = "auto";
|
|
|
|
|
|
documentSelf.onmousemove = null;
|
|
|
|
|
|
documentSelf.onmouseup = null;
|
|
|
|
|
|
documentSelf.ondragstart = null;
|
|
|
|
|
|
documentSelf.onselectstart = null;
|
|
|
|
|
|
documentSelf.onselect = null;
|
|
|
|
|
|
};
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return;
|
|
|
|
|
|
});
|
|
|
|
|
|
const textElement = this.subElement.querySelector(".b3-text-field") as HTMLTextAreaElement;
|
2022-09-20 18:28:09 +08:00
|
|
|
|
if (types.includes("NodeHTMLBlock")) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
textElement.value = Lute.UnEscapeHTMLStr(renderElement.querySelector("protyle-html").getAttribute("data-content") || "");
|
2022-09-19 10:05:40 +08:00
|
|
|
|
} else if (isInlineMemo) {
|
2022-09-17 23:14:02 +08:00
|
|
|
|
textElement.value = Lute.UnEscapeHTMLStr(renderElement.getAttribute("data-inline-memo-content") || "");
|
2022-05-26 15:18:53 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
const switchElement = this.subElement.querySelector(".b3-switch") as HTMLInputElement;
|
|
|
|
|
|
if (nodeElement.getAttribute("custom-heading-mode") === "1") {
|
|
|
|
|
|
switchElement.checked = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
switchElement.addEventListener("change", () => {
|
|
|
|
|
|
hideElements(["util"], protyle);
|
|
|
|
|
|
nodeElement.setAttribute("custom-heading-mode", switchElement.checked ? "1" : "0");
|
|
|
|
|
|
fetchPost("/api/attr/setBlockAttrs", {
|
|
|
|
|
|
id,
|
|
|
|
|
|
attrs: {"custom-heading-mode": switchElement.checked ? "1" : "0"}
|
|
|
|
|
|
});
|
|
|
|
|
|
renderElement.removeAttribute("data-render");
|
|
|
|
|
|
blockRender(protyle, renderElement);
|
|
|
|
|
|
});
|
|
|
|
|
|
textElement.value = Lute.UnEscapeHTMLStr(renderElement.getAttribute("data-content") || "");
|
|
|
|
|
|
}
|
2022-06-18 09:50:41 +08:00
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
textElement.addEventListener("input", (event) => {
|
|
|
|
|
|
if (!renderElement.parentElement) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (textElement.clientHeight !== textElement.scrollHeight) {
|
|
|
|
|
|
autoHeight();
|
|
|
|
|
|
}
|
2022-06-20 21:47:12 +08:00
|
|
|
|
if (!this.subElement.querySelector('[data-type="refresh"]').classList.contains("block__icon--active")) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-09-20 18:28:09 +08:00
|
|
|
|
if (types.includes("NodeHTMLBlock")) {
|
|
|
|
|
|
renderElement.querySelector("protyle-html").setAttribute("data-content", Lute.EscapeHTMLStr(textElement.value));
|
2022-09-19 10:05:40 +08:00
|
|
|
|
} else if (isInlineMemo) {
|
2022-09-20 18:28:09 +08:00
|
|
|
|
let inlineMemoElements;
|
|
|
|
|
|
if (updateElements) {
|
|
|
|
|
|
inlineMemoElements = updateElements;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
inlineMemoElements = [renderElement];
|
|
|
|
|
|
}
|
|
|
|
|
|
inlineMemoElements.forEach((item) => {
|
|
|
|
|
|
item.setAttribute("data-inline-memo-content", Lute.EscapeHTMLStr(textElement.value));
|
|
|
|
|
|
});
|
2022-05-26 15:18:53 +08:00
|
|
|
|
} else {
|
2022-09-20 18:28:09 +08:00
|
|
|
|
renderElement.setAttribute("data-content", Lute.EscapeHTMLStr(textElement.value));
|
2022-05-26 15:18:53 +08:00
|
|
|
|
renderElement.removeAttribute("data-render");
|
|
|
|
|
|
}
|
2022-09-20 18:28:09 +08:00
|
|
|
|
if (!types.includes("NodeBlockQueryEmbed") || !types.includes("NodeHTMLBlock") || !isInlineMemo) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
processRender(renderElement);
|
|
|
|
|
|
}
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
});
|
2022-09-20 18:28:09 +08:00
|
|
|
|
textElement.addEventListener("keydown", (event: KeyboardEvent) => {
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
// 阻止 ctrl+m 缩小窗口 https://github.com/siyuan-note/siyuan/issues/5541
|
|
|
|
|
|
if (matchHotKey(window.siyuan.config.keymap.editor.insert["inline-math"].custom, event)) {
|
|
|
|
|
|
event.preventDefault();
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-09-20 18:28:09 +08:00
|
|
|
|
if (event.isComposing) {
|
|
|
|
|
|
return;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2022-09-20 18:28:09 +08:00
|
|
|
|
if (event.key === "Escape" || matchHotKey("⌘↩", event)) {
|
|
|
|
|
|
this.subElement.querySelector('[data-type="pin"]').classList.remove("block__icon--active");
|
|
|
|
|
|
hideElements(["util"], protyle);
|
|
|
|
|
|
} else if (event.key === "Tab") {
|
|
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/5270
|
|
|
|
|
|
document.execCommand("insertText", false, "\t");
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
} else if (electronUndo(event)) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
this.subElement.classList.remove("fn__none");
|
|
|
|
|
|
this.subElementCloseCB = () => {
|
|
|
|
|
|
if (!renderElement.parentElement) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-09-20 20:49:40 +08:00
|
|
|
|
let inlineLastNode: Element;
|
2022-09-20 18:28:09 +08:00
|
|
|
|
if (types.includes("NodeHTMLBlock")) {
|
|
|
|
|
|
renderElement.querySelector("protyle-html").setAttribute("data-content", Lute.EscapeHTMLStr(textElement.value));
|
|
|
|
|
|
} else if (isInlineMemo) {
|
2022-09-19 23:56:47 +08:00
|
|
|
|
let inlineMemoElements;
|
2022-09-19 23:49:24 +08:00
|
|
|
|
if (updateElements) {
|
2022-09-19 23:56:47 +08:00
|
|
|
|
inlineMemoElements = updateElements;
|
2022-09-19 10:05:40 +08:00
|
|
|
|
} else {
|
2022-09-19 23:56:47 +08:00
|
|
|
|
inlineMemoElements = [renderElement];
|
2022-09-19 10:05:40 +08:00
|
|
|
|
}
|
2022-09-19 23:49:24 +08:00
|
|
|
|
inlineMemoElements.forEach((item, index) => {
|
2022-09-20 18:28:09 +08:00
|
|
|
|
if (!textElement.value) {
|
2022-09-19 23:49:24 +08:00
|
|
|
|
// https://github.com/siyuan-note/insider/issues/1046
|
2022-09-19 23:56:47 +08:00
|
|
|
|
const currentTypes = item.getAttribute("data-type").split(" ");
|
2022-09-19 23:49:24 +08:00
|
|
|
|
if (currentTypes.length === 1 && currentTypes[0] === "inline-memo") {
|
2022-09-20 20:10:42 +08:00
|
|
|
|
item.outerHTML = item.innerHTML + (index === inlineMemoElements.length - 1 ? "<wbr>" : "");
|
2022-09-19 23:49:24 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
currentTypes.find((typeItem, index) => {
|
|
|
|
|
|
if (typeItem === "inline-memo") {
|
2022-09-19 23:56:47 +08:00
|
|
|
|
currentTypes.splice(index, 1);
|
|
|
|
|
|
return true;
|
2022-09-19 23:49:24 +08:00
|
|
|
|
}
|
2022-09-19 23:56:47 +08:00
|
|
|
|
});
|
2022-09-19 23:49:24 +08:00
|
|
|
|
item.setAttribute("data-type", currentTypes.join(" "));
|
|
|
|
|
|
item.removeAttribute("data-inline-memo-content");
|
|
|
|
|
|
}
|
2022-09-20 20:10:42 +08:00
|
|
|
|
if (index === inlineMemoElements.length - 1) {
|
2022-09-20 20:49:40 +08:00
|
|
|
|
inlineLastNode = item;
|
2022-09-20 20:10:42 +08:00
|
|
|
|
}
|
2022-09-19 23:49:24 +08:00
|
|
|
|
} else {
|
2022-09-20 18:28:09 +08:00
|
|
|
|
item.setAttribute("data-inline-memo-content", Lute.EscapeHTMLStr(textElement.value));
|
2022-09-19 23:49:24 +08:00
|
|
|
|
}
|
2022-09-19 23:56:47 +08:00
|
|
|
|
});
|
2022-09-20 20:49:40 +08:00
|
|
|
|
} else if (types.includes("inline-math")) {
|
|
|
|
|
|
// 行内数学公式不允许换行 https://github.com/siyuan-note/siyuan/issues/2187
|
|
|
|
|
|
if (textElement.value) {
|
|
|
|
|
|
renderElement.setAttribute("data-content", Lute.EscapeHTMLStr(textElement.value.replace(/\n/g, "")));
|
|
|
|
|
|
renderElement.removeAttribute("data-render");
|
|
|
|
|
|
processRender(renderElement);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
inlineLastNode = renderElement;
|
2022-09-23 23:30:01 +08:00
|
|
|
|
renderElement.outerHTML = "<wbr>";
|
2022-09-20 20:49:40 +08:00
|
|
|
|
}
|
2022-09-20 18:28:09 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
renderElement.setAttribute("data-content", Lute.EscapeHTMLStr(textElement.value));
|
|
|
|
|
|
renderElement.removeAttribute("data-render");
|
|
|
|
|
|
if (types.includes("NodeBlockQueryEmbed")) {
|
|
|
|
|
|
blockRender(protyle, renderElement);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
processRender(renderElement);
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2022-09-20 18:28:09 +08:00
|
|
|
|
|
2022-09-20 20:49:40 +08:00
|
|
|
|
// 光标定位
|
2022-09-20 18:28:09 +08:00
|
|
|
|
if (renderElement.tagName === "SPAN") {
|
2022-09-20 20:49:40 +08:00
|
|
|
|
if (inlineLastNode) {
|
|
|
|
|
|
if (inlineLastNode.parentElement) {
|
|
|
|
|
|
this.range.setStartAfter(inlineLastNode);
|
2022-09-20 20:10:42 +08:00
|
|
|
|
this.range.collapse(true);
|
|
|
|
|
|
focusByRange(this.range);
|
|
|
|
|
|
} else {
|
2022-09-23 23:30:01 +08:00
|
|
|
|
focusByWbr(nodeElement, this.range);
|
2022-09-20 20:10:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else if (renderElement.parentElement) {
|
|
|
|
|
|
this.range.setStartAfter(renderElement);
|
|
|
|
|
|
this.range.collapse(true);
|
|
|
|
|
|
focusByRange(this.range);
|
|
|
|
|
|
}
|
2022-09-20 18:28:09 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
focusSideBlock(renderElement);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2022-09-20 20:49:40 +08:00
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
|
2022-08-21 17:38:09 +08:00
|
|
|
|
const newHTML = protyle.lute.SpinBlockDOM(nodeElement.outerHTML);
|
2022-08-28 10:28:47 +08:00
|
|
|
|
// HTML 块中包含多个 <pre> 时只能保存第一个 https://github.com/siyuan-note/siyuan/issues/5732
|
2022-09-20 18:28:09 +08:00
|
|
|
|
if (types.includes("NodeHTMLBlock")) {
|
2022-08-28 10:28:47 +08:00
|
|
|
|
const tempElement = document.createElement("template");
|
|
|
|
|
|
tempElement.innerHTML = newHTML;
|
|
|
|
|
|
if (tempElement.content.childElementCount > 1) {
|
2022-08-28 12:44:28 +08:00
|
|
|
|
showMessage(window.siyuan.languages.htmlBlockTip);
|
2022-08-28 10:28:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-08-19 10:37:26 +08:00
|
|
|
|
updateTransaction(protyle, id, newHTML, html);
|
2022-09-23 23:30:01 +08:00
|
|
|
|
};
|
2022-05-26 15:18:53 +08:00
|
|
|
|
const nodeRect = renderElement.getBoundingClientRect();
|
|
|
|
|
|
this.element.classList.add("fn__none");
|
|
|
|
|
|
if (isPin) {
|
|
|
|
|
|
textElement.style.width = pinData.styleW;
|
|
|
|
|
|
textElement.style.height = pinData.styleH;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
autoHeight();
|
|
|
|
|
|
}
|
|
|
|
|
|
textElement.select();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public showCodeLanguage(protyle: IProtyle, languageElement: HTMLElement) {
|
|
|
|
|
|
const nodeElement = hasClosestBlock(languageElement);
|
|
|
|
|
|
if (!nodeElement) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
this.range = getEditorRange(nodeElement);
|
|
|
|
|
|
const id = nodeElement.getAttribute("data-node-id");
|
|
|
|
|
|
let oldHtml = nodeElement.outerHTML;
|
|
|
|
|
|
let html = "";
|
|
|
|
|
|
Constants.CODE_LANGUAGES.forEach((item, index) => {
|
|
|
|
|
|
html += `<div class="b3-list-item${index === 0 ? " b3-list-item--focus" : ""}">${item}</div>`;
|
|
|
|
|
|
});
|
|
|
|
|
|
this.subElement.style.width = "";
|
|
|
|
|
|
this.subElement.style.padding = "";
|
|
|
|
|
|
this.subElement.innerHTML = `<div class="fn__flex-column" style="max-height:50vh"><input placeholder="${window.siyuan.languages.search}" style="margin: 4px 8px 8px 8px" class="b3-text-field"/>
|
|
|
|
|
|
<div class="b3-list fn__flex-1 b3-list--background" style="position: relative">${html}</div>
|
|
|
|
|
|
</div>`;
|
|
|
|
|
|
|
|
|
|
|
|
const inputElement = this.subElement.querySelector("input");
|
|
|
|
|
|
inputElement.addEventListener("keydown", (event: KeyboardEvent) => {
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
if (event.isComposing) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
upDownHint(this.subElement.lastElementChild.lastElementChild as HTMLElement, event);
|
|
|
|
|
|
if (event.key === "Enter") {
|
|
|
|
|
|
languageElement.textContent = this.subElement.querySelector(".b3-list-item--focus").textContent;
|
|
|
|
|
|
localStorage.setItem(Constants.LOCAL_CODELANG, languageElement.textContent);
|
|
|
|
|
|
const editElement = getContenteditableElement(nodeElement);
|
2022-05-29 10:06:02 +08:00
|
|
|
|
const lineNumber = nodeElement.getAttribute("linenumber");
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if (lineNumber === "true" || (lineNumber !== "false" && window.siyuan.config.editor.codeSyntaxHighlightLineNum)) {
|
|
|
|
|
|
editElement.classList.add("protyle-linenumber");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
editElement.classList.remove("protyle-linenumber");
|
|
|
|
|
|
}
|
|
|
|
|
|
(editElement as HTMLElement).textContent = editElement.textContent;
|
|
|
|
|
|
editElement.removeAttribute("data-render");
|
|
|
|
|
|
highlightRender(nodeElement);
|
|
|
|
|
|
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
|
|
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, oldHtml);
|
|
|
|
|
|
oldHtml = nodeElement.outerHTML;
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
}
|
|
|
|
|
|
if (event.key === "Escape" || event.key === "Enter") {
|
|
|
|
|
|
this.subElement.classList.add("fn__none");
|
|
|
|
|
|
focusByRange(this.range);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
inputElement.addEventListener("input", (event) => {
|
|
|
|
|
|
const mathLanguages: string[] = [];
|
|
|
|
|
|
Constants.CODE_LANGUAGES.forEach((item) => {
|
|
|
|
|
|
if (item.indexOf(inputElement.value.toLowerCase()) > -1) {
|
|
|
|
|
|
mathLanguages.push(item);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
let html = "";
|
|
|
|
|
|
// sort
|
|
|
|
|
|
mathLanguages.sort((a, b) => {
|
|
|
|
|
|
if (a.startsWith(inputElement.value.toLowerCase()) && b.startsWith(inputElement.value.toLowerCase())) {
|
|
|
|
|
|
if (a.length < b.length) {
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
} else if (a.length === b.length) {
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if (a.startsWith(inputElement.value.toLowerCase())) {
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
} else if (b.startsWith(inputElement.value.toLowerCase())) {
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
}).forEach((item) => {
|
|
|
|
|
|
html += `<div class="b3-list-item">${item.replace(inputElement.value.toLowerCase(), "<b>" + inputElement.value.toLowerCase() + "</b>")}</div>`;
|
|
|
|
|
|
});
|
|
|
|
|
|
this.subElement.firstElementChild.lastElementChild.innerHTML = html;
|
|
|
|
|
|
if (html) {
|
|
|
|
|
|
this.subElement.firstElementChild.lastElementChild.firstElementChild.classList.add("b3-list-item--focus");
|
|
|
|
|
|
}
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
});
|
|
|
|
|
|
this.subElement.lastElementChild.lastElementChild.addEventListener("click", (event) => {
|
|
|
|
|
|
const target = event.target as HTMLElement;
|
|
|
|
|
|
const listElement = hasClosestByClassName(target, "b3-list-item");
|
|
|
|
|
|
if (!listElement) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
languageElement.textContent = listElement.textContent;
|
|
|
|
|
|
localStorage.setItem(Constants.LOCAL_CODELANG, languageElement.textContent);
|
|
|
|
|
|
const nodeElement = hasClosestBlock(languageElement);
|
|
|
|
|
|
if (nodeElement) {
|
|
|
|
|
|
const editElement = getContenteditableElement(nodeElement);
|
2022-05-29 10:06:02 +08:00
|
|
|
|
const lineNumber = nodeElement.getAttribute("linenumber");
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if (lineNumber === "true" || (lineNumber !== "false" && window.siyuan.config.editor.codeSyntaxHighlightLineNum)) {
|
|
|
|
|
|
editElement.classList.add("protyle-linenumber");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
editElement.classList.remove("protyle-linenumber");
|
|
|
|
|
|
}
|
|
|
|
|
|
(editElement as HTMLElement).textContent = editElement.textContent;
|
|
|
|
|
|
editElement.removeAttribute("data-render");
|
|
|
|
|
|
highlightRender(nodeElement);
|
|
|
|
|
|
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
|
|
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, oldHtml);
|
|
|
|
|
|
oldHtml = nodeElement.outerHTML;
|
|
|
|
|
|
this.subElement.classList.add("fn__none");
|
|
|
|
|
|
focusByRange(this.range);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
this.subElement.classList.remove("fn__none");
|
2022-09-20 18:28:09 +08:00
|
|
|
|
this.subElementCloseCB = undefined;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
const nodeRect = languageElement.getBoundingClientRect();
|
|
|
|
|
|
setPosition(this.subElement, nodeRect.left, nodeRect.bottom, nodeRect.height);
|
|
|
|
|
|
this.element.classList.add("fn__none");
|
|
|
|
|
|
inputElement.select();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public showTpl(protyle: IProtyle, nodeElement: HTMLElement, range: Range) {
|
|
|
|
|
|
this.range = range;
|
|
|
|
|
|
fetchPost("/api/search/searchTemplate", {
|
|
|
|
|
|
k: "",
|
|
|
|
|
|
}, (response) => {
|
2022-08-18 18:20:49 +08:00
|
|
|
|
let html = "";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
response.data.blocks.forEach((item: { path: string, content: string }, index: number) => {
|
|
|
|
|
|
html += `<div data-value="${item.path}" class="b3-list-item${index === 0 ? " b3-list-item--focus" : ""}">${item.content}</div>`;
|
|
|
|
|
|
});
|
2022-08-18 18:20:49 +08:00
|
|
|
|
if (html === "") {
|
|
|
|
|
|
html = `<li class="b3-list--empty">${window.siyuan.languages.emptyContent}</li>`;
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
this.subElement.style.width = "";
|
|
|
|
|
|
this.subElement.style.padding = "";
|
2022-08-18 20:07:58 +08:00
|
|
|
|
this.subElement.innerHTML = `<div style="max-height:50vh" class="fn__flex">
|
|
|
|
|
|
<div class="fn__flex-column" style="min-width: 260px;max-width: 100vw">
|
|
|
|
|
|
<input style="margin: 4px 8px 8px 8px" class="b3-text-field"/>
|
|
|
|
|
|
<div class="b3-list fn__flex-1 b3-list--background" style="position: relative">${html}</div>
|
|
|
|
|
|
</div>
|
2022-08-18 23:44:45 +08:00
|
|
|
|
<div style="width: 520px;${isMobile() ? "display:none" : ""};overflow: auto;"></div>
|
2022-05-26 15:18:53 +08:00
|
|
|
|
</div>`;
|
2022-08-18 20:07:58 +08:00
|
|
|
|
const listElement = this.subElement.querySelector(".b3-list");
|
|
|
|
|
|
const previewElement = this.subElement.firstElementChild.lastElementChild;
|
2022-08-18 23:47:22 +08:00
|
|
|
|
previewTemplate(listElement.firstElementChild.getAttribute("data-value"), previewElement, protyle.block.parentID);
|
2022-08-18 20:07:58 +08:00
|
|
|
|
listElement.addEventListener("mouseover", (event) => {
|
|
|
|
|
|
const target = event.target as HTMLElement;
|
|
|
|
|
|
const hoverItemElement = hasClosestByClassName(target, "b3-list-item");
|
|
|
|
|
|
if (!hoverItemElement) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-08-18 23:47:22 +08:00
|
|
|
|
previewTemplate(hoverItemElement.getAttribute("data-value"), previewElement, protyle.block.parentID);
|
2022-08-18 20:07:58 +08:00
|
|
|
|
});
|
2022-05-26 15:18:53 +08:00
|
|
|
|
const inputElement = this.subElement.querySelector("input");
|
|
|
|
|
|
inputElement.addEventListener("keydown", (event: KeyboardEvent) => {
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
if (event.isComposing) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-08-18 23:47:22 +08:00
|
|
|
|
const isEmpty = !this.subElement.querySelector(".b3-list-item");
|
2022-08-18 16:59:12 +08:00
|
|
|
|
if (!isEmpty) {
|
2022-08-18 20:07:58 +08:00
|
|
|
|
const currentElement = upDownHint(listElement, event);
|
|
|
|
|
|
if (currentElement) {
|
2022-08-18 23:47:22 +08:00
|
|
|
|
previewTemplate(currentElement.getAttribute("data-value"), previewElement, protyle.block.parentID);
|
2022-08-18 20:07:58 +08:00
|
|
|
|
}
|
2022-08-18 16:59:12 +08:00
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if (event.key === "Enter") {
|
2022-08-18 16:59:12 +08:00
|
|
|
|
if (!isEmpty) {
|
|
|
|
|
|
hintRenderTemplate(decodeURIComponent(this.subElement.querySelector(".b3-list-item--focus").getAttribute("data-value")), protyle, nodeElement);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
focusByRange(this.range);
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
this.subElement.classList.add("fn__none");
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
} else if (event.key === "Escape") {
|
|
|
|
|
|
this.subElement.classList.add("fn__none");
|
|
|
|
|
|
focusByRange(this.range);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
inputElement.addEventListener("input", (event) => {
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
fetchPost("/api/search/searchTemplate", {
|
|
|
|
|
|
k: inputElement.value,
|
|
|
|
|
|
}, (response) => {
|
2022-08-18 18:20:49 +08:00
|
|
|
|
let searchHTML = "";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
response.data.blocks.forEach((item: { path: string, content: string }, index: number) => {
|
|
|
|
|
|
searchHTML += `<div data-value="${item.path}" class="b3-list-item${index === 0 ? " b3-list-item--focus" : ""}">${item.content}</div>`;
|
|
|
|
|
|
});
|
2022-08-18 20:07:58 +08:00
|
|
|
|
listElement.innerHTML = searchHTML || `<li class="b3-list--empty">${window.siyuan.languages.emptyContent}</li>`;
|
2022-08-18 23:44:45 +08:00
|
|
|
|
previewTemplate(response.data.blocks[0]?.path, previewElement, protyle.block.parentID);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
this.subElement.lastElementChild.addEventListener("click", (event) => {
|
|
|
|
|
|
const target = event.target as HTMLElement;
|
2022-08-18 16:59:12 +08:00
|
|
|
|
if (target.classList.contains("b3-list--empty")) {
|
|
|
|
|
|
this.subElement.classList.add("fn__none");
|
|
|
|
|
|
focusByRange(this.range);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
const listElement = hasClosestByClassName(target, "b3-list-item");
|
|
|
|
|
|
if (!listElement) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
hintRenderTemplate(decodeURIComponent(listElement.getAttribute("data-value")), protyle, nodeElement);
|
|
|
|
|
|
});
|
|
|
|
|
|
const rangePosition = getSelectionPosition(nodeElement, range);
|
|
|
|
|
|
this.subElement.classList.remove("fn__none");
|
2022-09-20 18:28:09 +08:00
|
|
|
|
this.subElementCloseCB = undefined;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
setPosition(this.subElement, rangePosition.left, rangePosition.top + 18, Constants.SIZE_TOOLBAR_HEIGHT);
|
|
|
|
|
|
this.element.classList.add("fn__none");
|
|
|
|
|
|
inputElement.select();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public showWidget(protyle: IProtyle, nodeElement: HTMLElement, range: Range) {
|
|
|
|
|
|
this.range = range;
|
|
|
|
|
|
fetchPost("/api/search/searchWidget", {
|
|
|
|
|
|
k: "",
|
|
|
|
|
|
}, (response) => {
|
|
|
|
|
|
let html = "";
|
|
|
|
|
|
response.data.blocks.forEach((item: { content: string }, index: number) => {
|
|
|
|
|
|
html += `<div class="b3-list-item${index === 0 ? " b3-list-item--focus" : ""}">${item.content}</div>`;
|
|
|
|
|
|
});
|
|
|
|
|
|
this.subElement.style.width = "";
|
|
|
|
|
|
this.subElement.style.padding = "";
|
|
|
|
|
|
this.subElement.innerHTML = `<div class="fn__flex-column" style="max-height:50vh"><input style="margin: 4px 8px 8px 8px" class="b3-text-field"/>
|
|
|
|
|
|
<div class="b3-list fn__flex-1 b3-list--background" style="position: relative">${html}</div>
|
|
|
|
|
|
</div>`;
|
|
|
|
|
|
|
|
|
|
|
|
const inputElement = this.subElement.querySelector("input");
|
|
|
|
|
|
inputElement.addEventListener("keydown", (event: KeyboardEvent) => {
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
if (event.isComposing) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
upDownHint(this.subElement.lastElementChild.lastElementChild as HTMLElement, event);
|
|
|
|
|
|
if (event.key === "Enter") {
|
|
|
|
|
|
hintRenderWidget(this.subElement.querySelector(".b3-list-item--focus").textContent, protyle);
|
|
|
|
|
|
this.subElement.classList.add("fn__none");
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
} else if (event.key === "Escape") {
|
|
|
|
|
|
this.subElement.classList.add("fn__none");
|
|
|
|
|
|
focusByRange(this.range);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
inputElement.addEventListener("input", (event) => {
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
fetchPost("/api/search/searchWidget", {
|
|
|
|
|
|
k: inputElement.value,
|
|
|
|
|
|
}, (response) => {
|
|
|
|
|
|
let searchHTML = "";
|
|
|
|
|
|
response.data.blocks.forEach((item: { path: string, content: string }, index: number) => {
|
|
|
|
|
|
searchHTML += `<div data-value="${item.path}" class="b3-list-item${index === 0 ? " b3-list-item--focus" : ""}">${item.content}</div>`;
|
|
|
|
|
|
});
|
|
|
|
|
|
this.subElement.firstElementChild.lastElementChild.innerHTML = searchHTML;
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
this.subElement.lastElementChild.addEventListener("click", (event) => {
|
|
|
|
|
|
const target = event.target as HTMLElement;
|
|
|
|
|
|
const listElement = hasClosestByClassName(target, "b3-list-item");
|
|
|
|
|
|
if (!listElement) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
hintRenderWidget(listElement.textContent, protyle);
|
|
|
|
|
|
});
|
|
|
|
|
|
const rangePosition = getSelectionPosition(nodeElement, range);
|
|
|
|
|
|
this.subElement.classList.remove("fn__none");
|
2022-09-20 18:28:09 +08:00
|
|
|
|
this.subElementCloseCB = undefined;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
setPosition(this.subElement, rangePosition.left, rangePosition.top + 18, Constants.SIZE_TOOLBAR_HEIGHT);
|
|
|
|
|
|
this.element.classList.add("fn__none");
|
|
|
|
|
|
inputElement.select();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public showAssets(protyle: IProtyle, nodeElement: HTMLElement, range: Range) {
|
|
|
|
|
|
this.range = range;
|
|
|
|
|
|
fetchPost("/api/search/searchAsset", {
|
|
|
|
|
|
k: "",
|
|
|
|
|
|
}, (response) => {
|
|
|
|
|
|
let html = "";
|
|
|
|
|
|
response.data.forEach((item: { hName: string, path: string }, index: number) => {
|
2022-07-16 23:20:50 +08:00
|
|
|
|
html += `<div data-value="${item.path}" class="b3-list-item${index === 0 ? " b3-list-item--focus" : ""}">${item.hName}</div>`;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
});
|
2022-08-18 20:07:58 +08:00
|
|
|
|
if (html === "") {
|
|
|
|
|
|
html = `<li class="b3-list--empty">${window.siyuan.languages.emptyContent}</li>`;
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
this.subElement.style.width = "";
|
|
|
|
|
|
this.subElement.style.padding = "";
|
2022-07-16 23:20:50 +08:00
|
|
|
|
this.subElement.innerHTML = `<div style="max-height:50vh" class="fn__flex">
|
2022-07-26 10:48:39 +08:00
|
|
|
|
<div class="fn__flex-column" style="min-width: 260px;max-width: 100vw">
|
2022-07-16 23:20:50 +08:00
|
|
|
|
<input style="margin: 4px 8px 8px 8px" class="b3-text-field"/>
|
|
|
|
|
|
<div class="b3-list fn__flex-1 b3-list--background" style="position: relative">${html}</div>
|
|
|
|
|
|
</div>
|
2022-07-26 10:48:39 +08:00
|
|
|
|
<div style="width: 260px;display: ${isMobile() ? "none" : "flex"};padding: 8px;overflow: auto;justify-content: center;align-items: center;"></div>
|
2022-05-26 15:18:53 +08:00
|
|
|
|
</div>`;
|
2022-07-16 23:20:50 +08:00
|
|
|
|
const listElement = this.subElement.querySelector(".b3-list");
|
|
|
|
|
|
listElement.addEventListener("mouseover", (event) => {
|
|
|
|
|
|
const target = event.target as HTMLElement;
|
|
|
|
|
|
const hoverItemElement = hasClosestByClassName(target, "b3-list-item");
|
|
|
|
|
|
if (!hoverItemElement) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-07-17 11:06:57 +08:00
|
|
|
|
previewElement.innerHTML = renderAssetsPreview(hoverItemElement.getAttribute("data-value"));
|
|
|
|
|
|
});
|
|
|
|
|
|
const previewElement = this.subElement.firstElementChild.lastElementChild;
|
2022-07-16 23:20:50 +08:00
|
|
|
|
previewElement.innerHTML = renderAssetsPreview(listElement.firstElementChild.getAttribute("data-value"));
|
2022-05-26 15:18:53 +08:00
|
|
|
|
const inputElement = this.subElement.querySelector("input");
|
|
|
|
|
|
inputElement.addEventListener("keydown", (event: KeyboardEvent) => {
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
if (event.isComposing) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-08-18 23:47:22 +08:00
|
|
|
|
const isEmpty = !this.subElement.querySelector(".b3-list-item");
|
2022-08-18 20:07:58 +08:00
|
|
|
|
if (!isEmpty) {
|
|
|
|
|
|
const currentElement = upDownHint(listElement, event);
|
|
|
|
|
|
if (currentElement) {
|
|
|
|
|
|
previewElement.innerHTML = renderAssetsPreview(currentElement.getAttribute("data-value"));
|
|
|
|
|
|
}
|
2022-07-16 23:20:50 +08:00
|
|
|
|
}
|
2022-08-18 20:07:58 +08:00
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if (event.key === "Enter") {
|
2022-08-18 20:07:58 +08:00
|
|
|
|
if (!isEmpty) {
|
|
|
|
|
|
hintRenderAssets(this.subElement.querySelector(".b3-list-item--focus").getAttribute("data-value"), protyle);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
focusByRange(this.range);
|
|
|
|
|
|
}
|
|
|
|
|
|
this.subElement.classList.add("fn__none");
|
2022-07-16 16:22:02 +08:00
|
|
|
|
// 空行处插入 mp3 会多一个空的 mp3 块
|
|
|
|
|
|
event.preventDefault();
|
2022-05-26 15:18:53 +08:00
|
|
|
|
} else if (event.key === "Escape") {
|
|
|
|
|
|
this.subElement.classList.add("fn__none");
|
|
|
|
|
|
focusByRange(this.range);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
inputElement.addEventListener("input", (event) => {
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
fetchPost("/api/search/searchAsset", {
|
|
|
|
|
|
k: inputElement.value,
|
|
|
|
|
|
}, (response) => {
|
|
|
|
|
|
let searchHTML = "";
|
|
|
|
|
|
response.data.forEach((item: { path: string, hName: string }, index: number) => {
|
|
|
|
|
|
searchHTML += `<div data-value="${item.path}" class="b3-list-item${index === 0 ? " b3-list-item--focus" : ""}">${item.hName}</div>`;
|
|
|
|
|
|
});
|
2022-08-18 20:07:58 +08:00
|
|
|
|
listElement.innerHTML = searchHTML || `<li class="b3-list--empty">${window.siyuan.languages.emptyContent}</li>`;
|
2022-07-16 23:20:50 +08:00
|
|
|
|
previewElement.innerHTML = renderAssetsPreview(listElement.firstElementChild.getAttribute("data-value"));
|
2022-05-26 15:18:53 +08:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
this.subElement.lastElementChild.addEventListener("click", (event) => {
|
|
|
|
|
|
const target = event.target as HTMLElement;
|
2022-08-18 20:07:58 +08:00
|
|
|
|
if (target.classList.contains("b3-list--empty")) {
|
|
|
|
|
|
this.subElement.classList.add("fn__none");
|
|
|
|
|
|
focusByRange(this.range);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-07-16 23:20:50 +08:00
|
|
|
|
const listItemElement = hasClosestByClassName(target, "b3-list-item");
|
|
|
|
|
|
if (!listItemElement) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-07-16 23:20:50 +08:00
|
|
|
|
hintRenderAssets(listItemElement.getAttribute("data-value"), protyle);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
});
|
|
|
|
|
|
const rangePosition = getSelectionPosition(nodeElement, range);
|
|
|
|
|
|
this.subElement.classList.remove("fn__none");
|
2022-09-20 18:28:09 +08:00
|
|
|
|
this.subElementCloseCB = undefined;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
setPosition(this.subElement, rangePosition.left, rangePosition.top + 18, Constants.SIZE_TOOLBAR_HEIGHT);
|
|
|
|
|
|
this.element.classList.add("fn__none");
|
|
|
|
|
|
inputElement.select();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public showFile(protyle: IProtyle, nodeElements: Element[], range: Range) {
|
|
|
|
|
|
this.range = range;
|
|
|
|
|
|
fetchPost("/api/filetree/searchDocs", {
|
|
|
|
|
|
k: "",
|
|
|
|
|
|
}, (response) => {
|
|
|
|
|
|
let html = "";
|
|
|
|
|
|
response.data.forEach((item: { boxIcon: string, box: string, hPath: string, path: string }) => {
|
|
|
|
|
|
if (item.path === "/") {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
html += `<div class="b3-list-item${html === "" ? " b3-list-item--focus" : ""}" data-path="${item.path}" data-box="${item.box}">
|
|
|
|
|
|
${item.boxIcon ? ('<span class="b3-list-item__icon">' + unicode2Emoji(item.boxIcon) + "</span>") : ""}
|
|
|
|
|
|
<span class="b3-list-item__text">${escapeHtml(item.hPath)}</span>
|
|
|
|
|
|
</div>`;
|
|
|
|
|
|
});
|
|
|
|
|
|
this.subElement.style.width = "";
|
|
|
|
|
|
this.subElement.style.padding = "";
|
|
|
|
|
|
this.subElement.innerHTML = `<div class="fn__flex-column" style="max-height:50vh"><input style="margin: 4px 8px 8px 8px" class="b3-text-field"/>
|
|
|
|
|
|
<div class="b3-list fn__flex-1 b3-list--background" style="position: relative">${html}</div>
|
|
|
|
|
|
</div>`;
|
|
|
|
|
|
|
|
|
|
|
|
const inputElement = this.subElement.querySelector("input");
|
|
|
|
|
|
inputElement.addEventListener("keydown", (event: KeyboardEvent) => {
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
if (event.isComposing) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
upDownHint(this.subElement.lastElementChild.lastElementChild as HTMLElement, event);
|
|
|
|
|
|
if (event.key === "Enter") {
|
|
|
|
|
|
hintMoveBlock(this.subElement.querySelector(".b3-list-item--focus").getAttribute("data-path"), nodeElements, protyle);
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
} else if (event.key === "Escape") {
|
|
|
|
|
|
this.subElement.classList.add("fn__none");
|
|
|
|
|
|
focusByRange(this.range);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
inputElement.addEventListener("input", (event) => {
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
fetchPost("/api/filetree/searchDocs", {
|
|
|
|
|
|
k: inputElement.value,
|
|
|
|
|
|
}, (response) => {
|
|
|
|
|
|
let searchHTML = "";
|
|
|
|
|
|
response.data.forEach((item: { boxIcon: string, box: string, hPath: string, path: string }) => {
|
|
|
|
|
|
if (item.path === "/") {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
searchHTML += `<div class="b3-list-item${searchHTML === "" ? " b3-list-item--focus" : ""}" data-path="${item.path}" data-box="${item.box}">
|
|
|
|
|
|
${item.boxIcon ? ('<span class="b3-list-item__icon">' + unicode2Emoji(item.boxIcon) + "</span>") : ""}
|
|
|
|
|
|
<span class="b3-list-item__text">${escapeHtml(item.hPath)}</span>
|
|
|
|
|
|
</div>`;
|
|
|
|
|
|
});
|
|
|
|
|
|
this.subElement.firstElementChild.lastElementChild.innerHTML = searchHTML;
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
this.subElement.lastElementChild.addEventListener("click", (event) => {
|
|
|
|
|
|
const target = event.target as HTMLElement;
|
|
|
|
|
|
const listElement = hasClosestByClassName(target, "b3-list-item");
|
|
|
|
|
|
if (!listElement) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
hintMoveBlock(listElement.getAttribute("data-path"), nodeElements, protyle);
|
|
|
|
|
|
});
|
|
|
|
|
|
const rangePosition = getSelectionPosition(nodeElements[0], range);
|
|
|
|
|
|
this.subElement.classList.remove("fn__none");
|
2022-09-20 18:28:09 +08:00
|
|
|
|
this.subElementCloseCB = undefined;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
setPosition(this.subElement, rangePosition.left, rangePosition.top + 18, Constants.SIZE_TOOLBAR_HEIGHT);
|
|
|
|
|
|
this.element.classList.add("fn__none");
|
|
|
|
|
|
inputElement.select();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|