2022-05-26 15:18:53 +08:00
|
|
|
import {Constants} from "../../constants";
|
|
|
|
import {hasClosestBlock, hasClosestByAttribute, hasClosestByClassName, hasClosestByMatchTag} from "../util/hasClosest";
|
|
|
|
import {
|
2022-07-20 19:58:31 +08:00
|
|
|
focusBlock,
|
2022-05-26 15:18:53 +08:00
|
|
|
focusByRange,
|
|
|
|
focusByWbr,
|
|
|
|
getEditorRange,
|
|
|
|
getSelectionOffset,
|
|
|
|
getSelectionPosition
|
|
|
|
} from "../util/selection";
|
2023-07-28 15:20:34 +08:00
|
|
|
import {genHintItemHTML, hintEmbed, hintRef, hintSlash} from "./extend";
|
2022-05-26 15:18:53 +08:00
|
|
|
import {getSavePath} from "../../util/newFile";
|
|
|
|
import {upDownHint} from "../../util/upDownHint";
|
|
|
|
import {setPosition} from "../../util/setPosition";
|
2022-09-10 22:00:21 +08:00
|
|
|
import {getContenteditableElement, hasNextSibling, hasPreviousSibling} from "../wysiwyg/getBlock";
|
2022-05-26 15:18:53 +08:00
|
|
|
import {transaction, updateTransaction} from "../wysiwyg/transaction";
|
|
|
|
import {insertHTML} from "../util/insertHTML";
|
2023-06-07 10:36:20 +08:00
|
|
|
import {highlightRender} from "../render/highlightRender";
|
2022-05-26 15:18:53 +08:00
|
|
|
import {imgMenu} from "../../menus/protyle";
|
|
|
|
import {hideElements} from "../ui/hideElements";
|
|
|
|
import {fetchPost} from "../../util/fetch";
|
|
|
|
import {getDisplayName, pathPosix} from "../../util/pathName";
|
2023-02-10 15:15:02 +08:00
|
|
|
import {addEmoji, filterEmoji, lazyLoadEmoji, lazyLoadEmojiImg, unicode2Emoji} from "../../emoji";
|
2023-06-07 10:36:20 +08:00
|
|
|
import {blockRender} from "../render/blockRender";
|
2022-05-26 15:18:53 +08:00
|
|
|
import {uploadFiles} from "../upload";
|
2022-06-29 20:25:30 +08:00
|
|
|
/// #if !MOBILE
|
2022-05-26 15:18:53 +08:00
|
|
|
import {openFileById} from "../../editor/util";
|
2022-06-29 20:25:30 +08:00
|
|
|
/// #endif
|
2022-05-26 15:18:53 +08:00
|
|
|
import {openMobileFileById} from "../../mobile/editor";
|
2022-08-19 14:39:19 +08:00
|
|
|
import {processRender} from "../util/processCode";
|
2023-03-08 21:57:57 +08:00
|
|
|
import {AIChat} from "../../ai/chat";
|
2023-03-08 23:18:21 +08:00
|
|
|
import {isMobile} from "../../util/functions";
|
2023-09-06 17:19:11 +08:00
|
|
|
import {isCtrl, isIPhone} from "../util/compatibility";
|
2023-06-08 22:55:31 +08:00
|
|
|
import {avRender} from "../render/av/render";
|
2023-07-19 13:55:42 +08:00
|
|
|
import {genIconHTML} from "../render/util";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
|
|
export class Hint {
|
|
|
|
public timeId: number;
|
|
|
|
public element: HTMLDivElement;
|
|
|
|
public enableSlash = true;
|
2023-02-08 17:33:49 +08:00
|
|
|
private enableEmoji = true;
|
|
|
|
public enableExtend = false;
|
2022-05-26 15:18:53 +08:00
|
|
|
public splitChar = "";
|
|
|
|
public lastIndex = -1;
|
2023-07-28 20:16:32 +08:00
|
|
|
private source: THintSource;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
2023-06-01 20:50:49 +08:00
|
|
|
constructor(protyle: IProtyle) {
|
2022-05-26 15:18:53 +08:00
|
|
|
this.element = document.createElement("div");
|
|
|
|
this.element.setAttribute("data-close", "false");
|
|
|
|
// height 402 根据 .emojis max-height+8 得来
|
2022-10-15 12:00:56 +08:00
|
|
|
this.element.setAttribute("style", `width:${Math.max(protyle.element.clientWidth / 2, 320)}px;`);
|
|
|
|
this.element.className = "protyle-hint b3-list b3-list--background fn__none";
|
2022-05-26 15:18:53 +08:00
|
|
|
this.element.addEventListener("click", (event) => {
|
|
|
|
const eventTarget = event.target as HTMLElement;
|
|
|
|
if (eventTarget.tagName === "INPUT") {
|
|
|
|
event.stopPropagation();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const btnElement = hasClosestByMatchTag(eventTarget, "BUTTON");
|
2023-06-22 22:39:41 +08:00
|
|
|
if (btnElement && !btnElement.classList.contains("emojis__item") && !btnElement.classList.contains("emojis__type")) {
|
2023-07-28 20:16:32 +08:00
|
|
|
if (this.source !== "search") {
|
2023-04-20 18:40:29 +08:00
|
|
|
this.fill(decodeURIComponent(btnElement.getAttribute("data-value")), protyle, true, isCtrl(event));
|
2022-05-26 15:18:53 +08:00
|
|
|
} else {
|
|
|
|
// 划选引用点击,需先重置 range
|
|
|
|
setTimeout(() => {
|
|
|
|
this.fill(decodeURIComponent(btnElement.getAttribute("data-value")), protyle);
|
|
|
|
}, 148);
|
|
|
|
}
|
2023-09-12 11:29:11 +08:00
|
|
|
focusByRange(protyle.toolbar.range);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation(); // https://github.com/siyuan-note/siyuan/issues/3710
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const emojisContentElement = this.element.querySelector(".emojis__panel");
|
|
|
|
const typeElement = hasClosestByClassName(eventTarget, "emojis__type");
|
|
|
|
if (typeElement) {
|
|
|
|
const titleElement = emojisContentElement.querySelector(`[data-type="${typeElement.getAttribute("data-type")}"]`) as HTMLElement;
|
|
|
|
if (titleElement) {
|
|
|
|
const index = titleElement.nextElementSibling.getAttribute("data-index");
|
|
|
|
if (index) {
|
|
|
|
let html = "";
|
|
|
|
window.siyuan.emojis[parseInt(index)].items.forEach(emoji => {
|
2023-07-12 11:46:58 +08:00
|
|
|
html += `<button data-unicode="${emoji.unicode}" class="emojis__item ariaLabel" aria-label="${window.siyuan.config.lang === "zh_CN" ? emoji.description_zh_cn : emoji.description}">
|
2023-06-01 22:39:57 +08:00
|
|
|
${unicode2Emoji(emoji.unicode)}</button>`;
|
2022-05-26 15:18:53 +08:00
|
|
|
});
|
|
|
|
titleElement.nextElementSibling.innerHTML = html;
|
|
|
|
titleElement.nextElementSibling.removeAttribute("data-index");
|
|
|
|
}
|
|
|
|
|
|
|
|
emojisContentElement.scrollTo({
|
|
|
|
top: titleElement.offsetTop,
|
|
|
|
// behavior: "smooth" 不能使用,否则无法定位
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const emojiElement = hasClosestByClassName(eventTarget, "emojis__item");
|
|
|
|
if (emojiElement) {
|
|
|
|
const unicode = emojiElement.getAttribute("data-unicode");
|
|
|
|
if (this.element.querySelectorAll(".emojis__title").length > 2) {
|
|
|
|
// /emoji 后会自动添加冒号,导致 range 无法计算,因此不依赖 this.fill
|
|
|
|
const range = getSelection().getRangeAt(0);
|
|
|
|
if (range.endContainer.nodeType !== 3) {
|
|
|
|
range.endContainer.childNodes[range.endOffset - 1]?.remove();
|
2023-06-22 22:39:41 +08:00
|
|
|
} else if (range.endContainer.textContent === ":") {
|
|
|
|
// iphone
|
|
|
|
range.endContainer.textContent = "";
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
addEmoji(unicode);
|
|
|
|
let emoji;
|
|
|
|
if (unicode.indexOf(".") > -1) {
|
|
|
|
emoji = `:${unicode.split(".")[0]}: `;
|
|
|
|
} else {
|
2023-06-01 22:39:57 +08:00
|
|
|
emoji = unicode2Emoji(unicode) + " ";
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
2023-06-22 22:39:41 +08:00
|
|
|
insertHTML(protyle.lute.SpinBlockDOM(emoji), protyle, false, true);
|
2022-05-26 15:18:53 +08:00
|
|
|
this.element.classList.add("fn__none");
|
|
|
|
} else {
|
|
|
|
this.fill(unicode, protyle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public render(protyle: IProtyle) {
|
|
|
|
if (!window.getSelection().focusNode) {
|
|
|
|
this.element.classList.add("fn__none");
|
|
|
|
clearTimeout(this.timeId);
|
|
|
|
return;
|
|
|
|
}
|
2023-02-08 17:33:49 +08:00
|
|
|
if (!this.enableExtend) {
|
|
|
|
clearTimeout(this.timeId);
|
|
|
|
return;
|
|
|
|
}
|
2022-09-30 22:44:39 +08:00
|
|
|
protyle.toolbar.range = getSelection().getRangeAt(0);
|
2023-02-14 09:38:41 +08:00
|
|
|
// 粘贴后 range.startContainer 为空 https://github.com/siyuan-note/siyuan/issues/7360
|
|
|
|
if (protyle.toolbar.range.startContainer.nodeType === 3 && protyle.toolbar.range.startContainer.textContent === "") {
|
|
|
|
const lastSibling = hasPreviousSibling(protyle.toolbar.range.startContainer) as Text;
|
|
|
|
if (lastSibling && lastSibling.nodeType === 3) {
|
|
|
|
if (lastSibling.wholeText !== lastSibling.textContent) {
|
|
|
|
let previousSibling = lastSibling.previousSibling;
|
|
|
|
while (previousSibling && previousSibling.nodeType === 3) {
|
|
|
|
if (previousSibling.textContent === "") {
|
|
|
|
previousSibling = previousSibling.previousSibling;
|
|
|
|
previousSibling.nextSibling.remove();
|
|
|
|
} else {
|
|
|
|
lastSibling.textContent = previousSibling.textContent + lastSibling.textContent;
|
|
|
|
previousSibling.remove();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
protyle.toolbar.range.setStart(lastSibling, lastSibling.textContent.length);
|
|
|
|
protyle.toolbar.range.collapse(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const start = getSelectionOffset(protyle.toolbar.range.startContainer, protyle.wysiwyg.element).start;
|
2022-09-30 22:44:39 +08:00
|
|
|
const currentLineValue = protyle.toolbar.range.startContainer.textContent.substring(0, start) || "";
|
2022-05-26 15:18:53 +08:00
|
|
|
const key = this.getKey(currentLineValue, protyle.options.hint.extend);
|
|
|
|
if (typeof key === "undefined" ||
|
2023-09-12 08:48:38 +08:00
|
|
|
hasClosestByAttribute(protyle.toolbar.range.startContainer, "data-type", "code") ||
|
|
|
|
hasClosestByAttribute(protyle.toolbar.range.startContainer, "data-type", "NodeCodeBlock")) {
|
2022-05-26 15:18:53 +08:00
|
|
|
this.element.classList.add("fn__none");
|
|
|
|
clearTimeout(this.timeId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-09 16:42:50 +08:00
|
|
|
// https://github.com/siyuan-note/siyuan/issues/7933
|
2023-04-20 18:40:29 +08:00
|
|
|
if (this.splitChar === "#") {
|
2023-04-09 16:42:50 +08:00
|
|
|
const blockElement = hasClosestBlock(protyle.toolbar.range.startContainer);
|
|
|
|
if (blockElement && blockElement.getAttribute("data-type") === "NodeHeading") {
|
2023-04-09 19:46:57 +08:00
|
|
|
const blockIndex = getSelectionOffset(protyle.toolbar.range.startContainer, blockElement).start;
|
2023-04-09 16:42:50 +08:00
|
|
|
if (blockElement.textContent.startsWith("#".repeat(blockIndex))) {
|
|
|
|
this.element.classList.add("fn__none");
|
|
|
|
clearTimeout(this.timeId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
if (this.splitChar === ":") {
|
|
|
|
clearTimeout(this.timeId);
|
|
|
|
if (key) {
|
|
|
|
this.genEmojiHTML(protyle, key);
|
|
|
|
} else {
|
|
|
|
this.element.classList.add("fn__none");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2022-06-07 11:37:01 +08:00
|
|
|
// https://github.com/siyuan-note/siyuan/issues/5083
|
2023-03-16 11:19:16 +08:00
|
|
|
if (this.splitChar === "/" || this.splitChar === "、") {
|
2022-06-07 11:37:01 +08:00
|
|
|
clearTimeout(this.timeId);
|
2023-03-16 11:19:16 +08:00
|
|
|
if (this.enableSlash && !isMobile()) {
|
2023-07-28 20:16:32 +08:00
|
|
|
this.genHTML(hintSlash(key, protyle), protyle, false, "hint");
|
2022-06-07 11:37:01 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
protyle.options.hint.extend.forEach((item) => {
|
|
|
|
if (item.key === this.splitChar) {
|
|
|
|
clearTimeout(this.timeId);
|
|
|
|
this.timeId = window.setTimeout(() => {
|
2023-07-28 20:16:32 +08:00
|
|
|
this.genHTML(item.hint(key, protyle, "hint"), protyle, false, "hint");
|
2022-05-26 15:18:53 +08:00
|
|
|
}, protyle.options.hint.delay);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public genLoading(protyle: IProtyle) {
|
|
|
|
if (this.element.classList.contains("fn__none")) {
|
2023-01-27 10:44:31 +08:00
|
|
|
this.element.innerHTML = '<div class="fn__loading" style="height: 128px;position: initial"><img width="64px" src="/stage/loading-pure.svg"></div>';
|
2022-05-26 15:18:53 +08:00
|
|
|
this.element.classList.remove("fn__none");
|
|
|
|
const textareaPosition = getSelectionPosition(protyle.wysiwyg.element);
|
|
|
|
setPosition(this.element, textareaPosition.left, textareaPosition.top + 26, 30);
|
|
|
|
} else {
|
2022-07-06 09:47:23 +08:00
|
|
|
this.element.insertAdjacentHTML("beforeend", '<div class="fn__loading"><img width="64px" src="/stage/loading-pure.svg"></div>');
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 11:19:16 +08:00
|
|
|
public bindUploadEvent(protyle: IProtyle, element: HTMLElement) {
|
2023-03-05 13:01:39 +08:00
|
|
|
const uploadElement = element.querySelector('input[type="file"]');
|
|
|
|
if (uploadElement) {
|
|
|
|
uploadElement.addEventListener("change", (event: InputEvent & { target: HTMLInputElement }) => {
|
|
|
|
if (event.target.files.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const range = getEditorRange(protyle.wysiwyg.element);
|
|
|
|
if (this.lastIndex > -1) {
|
|
|
|
range.setStart(range.startContainer, this.lastIndex);
|
|
|
|
}
|
|
|
|
range.deleteContents();
|
|
|
|
uploadFiles(protyle, event.target.files, event.target);
|
|
|
|
hideElements(["hint", "toolbar"], protyle);
|
|
|
|
});
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
2023-03-05 13:01:39 +08:00
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
2023-07-28 20:16:32 +08:00
|
|
|
private getHTMLByData(data: IHintData[]) {
|
2023-07-28 17:33:35 +08:00
|
|
|
let hintsHTML = '<div style="flex: 1;overflow:auto;">';
|
2023-07-28 20:16:32 +08:00
|
|
|
if (this.source !== "hint") {
|
2023-07-28 17:33:35 +08:00
|
|
|
hintsHTML = '<input style="margin:0 8px 4px 8px" class="b3-text-field"><div style="flex: 1;overflow:auto;">';
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
data.forEach((hintData, i) => {
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/1229 提示时,新建文件不应默认选中
|
|
|
|
let focusClass = "";
|
2023-02-08 17:33:49 +08:00
|
|
|
if ((i === 1 && data[i].focus) ||
|
2023-01-23 10:56:16 +08:00
|
|
|
(i === 0 && (data.length === 1 || !data[1].focus))) {
|
2022-05-26 15:18:53 +08:00
|
|
|
focusClass = " b3-list-item--focus";
|
|
|
|
}
|
|
|
|
if (hintData.html === "separator") {
|
|
|
|
hintsHTML += '<div class="b3-menu__separator"></div>';
|
|
|
|
} else {
|
2022-10-15 11:35:53 +08:00
|
|
|
hintsHTML += `<button style="width: calc(100% - 16px)" class="b3-list-item b3-list-item--two${focusClass}" data-value="${encodeURIComponent(hintData.value)}">${hintData.html}</button>`;
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
});
|
2023-07-28 17:33:35 +08:00
|
|
|
return `${hintsHTML}</div>`;
|
2023-03-05 13:01:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-28 20:16:32 +08:00
|
|
|
public genHTML(data: IHintData[], protyle: IProtyle, hide = false, source: THintSource) {
|
|
|
|
this.source = source;
|
2023-03-05 13:01:39 +08:00
|
|
|
if (data.length === 0) {
|
|
|
|
if (!this.element.querySelector(".fn__loading") || hide) {
|
|
|
|
this.element.classList.add("fn__none");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-28 20:16:32 +08:00
|
|
|
this.element.innerHTML = this.getHTMLByData(data);
|
2022-05-26 15:18:53 +08:00
|
|
|
this.element.classList.remove("fn__none");
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/4575
|
|
|
|
if (data[0].filter) {
|
|
|
|
this.element.classList.add("hint--menu");
|
|
|
|
} else {
|
|
|
|
this.element.classList.remove("hint--menu");
|
|
|
|
}
|
|
|
|
this.element.style.width = Math.max(protyle.element.clientWidth / 2, 320) + "px";
|
2023-07-29 10:34:26 +08:00
|
|
|
if (this.source === "av") {
|
2023-09-28 11:29:58 +08:00
|
|
|
const cellElement = hasClosestByClassName(protyle.toolbar.range.startContainer, "av__cell");
|
|
|
|
if (cellElement) {
|
|
|
|
const cellRect = cellElement.getBoundingClientRect();
|
|
|
|
setPosition(this.element, cellRect.left, cellRect.bottom, cellRect.height);
|
2023-07-29 10:34:26 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const textareaPosition = getSelectionPosition(protyle.wysiwyg.element);
|
|
|
|
setPosition(this.element, textareaPosition.left, textareaPosition.top + 26, 30);
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
this.element.scrollTop = 0;
|
2023-03-05 13:01:39 +08:00
|
|
|
this.bindUploadEvent(protyle, this.element);
|
2023-07-28 20:16:32 +08:00
|
|
|
if (this.source !== "hint") {
|
2022-05-26 15:18:53 +08:00
|
|
|
const searchElement = this.element.querySelector("input.b3-text-field") as HTMLInputElement;
|
2022-09-26 00:14:09 +08:00
|
|
|
const oldValue = this.element.querySelector("mark")?.textContent || "";
|
2022-05-26 15:18:53 +08:00
|
|
|
searchElement.value = oldValue;
|
|
|
|
searchElement.select();
|
|
|
|
searchElement.addEventListener("keydown", (event: KeyboardEvent) => {
|
2023-04-27 15:18:49 +08:00
|
|
|
if (event.key !== "Meta" && event.key !== "Control") {
|
|
|
|
// 需要冒泡以满足光标在块标位置时 ctrl 弹出悬浮层
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
if (event.isComposing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
upDownHint(this.element.lastElementChild, event);
|
|
|
|
if (event.key === "Enter") {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.fill(decodeURIComponent(this.element.querySelector(".b3-list-item--focus").getAttribute("data-value")), protyle);
|
|
|
|
}, 148);
|
|
|
|
focusByRange(protyle.toolbar.range);
|
|
|
|
event.preventDefault();
|
|
|
|
} else if (event.key === "Escape") {
|
|
|
|
this.element.classList.add("fn__none");
|
|
|
|
focusByRange(protyle.toolbar.range);
|
|
|
|
}
|
|
|
|
});
|
2023-07-28 20:16:32 +08:00
|
|
|
const nodeElement = protyle.toolbar.range ? hasClosestBlock(protyle.toolbar.range.startContainer) : false;
|
2022-05-26 15:18:53 +08:00
|
|
|
searchElement.addEventListener("input", (event: InputEvent) => {
|
|
|
|
if (event.isComposing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
event.stopPropagation();
|
|
|
|
this.genSearchHTML(protyle, searchElement, nodeElement, oldValue);
|
|
|
|
});
|
|
|
|
searchElement.addEventListener("compositionend", (event: InputEvent) => {
|
|
|
|
event.stopPropagation();
|
|
|
|
this.genSearchHTML(protyle, searchElement, nodeElement, oldValue);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private genSearchHTML(protyle: IProtyle, searchElement: HTMLInputElement, nodeElement: false | HTMLElement, oldValue: string) {
|
2022-08-01 23:53:35 +08:00
|
|
|
this.element.lastElementChild.innerHTML = '<div class="ft__center"><img style="height:32px;width:32px;" src="/stage/loading-pure.svg"></div>';
|
2022-05-26 15:18:53 +08:00
|
|
|
fetchPost("/api/search/searchRefBlock", {
|
|
|
|
k: searchElement.value,
|
|
|
|
id: nodeElement ? nodeElement.getAttribute("data-node-id") : protyle.block.parentID,
|
|
|
|
beforeLen: Math.floor((Math.max(protyle.element.clientWidth / 2, 320) - 58) / 28.8),
|
|
|
|
rootID: protyle.block.rootID,
|
|
|
|
}, (response) => {
|
|
|
|
let searchHTML = "";
|
|
|
|
if (response.data.newDoc) {
|
2022-08-02 11:15:18 +08:00
|
|
|
const blockRefText = `((newFile "${oldValue}"${Constants.ZWSP}'${response.data.k}${Lute.Caret}'))`;
|
2023-07-28 15:20:34 +08:00
|
|
|
searchHTML += `<button style="width: calc(100% - 16px)" class="b3-list-item b3-list-item--two${response.data.blocks.length === 0 ? " b3-list-item--focus" : ""}" data-value="${encodeURIComponent(blockRefText)}"><div class="b3-list-item__first"><svg class="b3-list-item__graphic"><use xlink:href="#iconFile"></use></svg>
|
2022-05-26 15:18:53 +08:00
|
|
|
<span class="b3-list-item__text">${window.siyuan.languages.newFile} <mark>${response.data.k}</mark></span></div></button>`;
|
|
|
|
}
|
|
|
|
response.data.blocks.forEach((item: IBlock, index: number) => {
|
2022-08-02 11:15:18 +08:00
|
|
|
const blockRefHTML = `<span data-type="block-ref" data-id="${item.id}" data-subtype="s">${oldValue}</span>`;
|
2023-07-28 15:20:34 +08:00
|
|
|
searchHTML += `<button style="width: calc(100% - 16px)" class="b3-list-item b3-list-item--two${index === 0 ? " b3-list-item--focus" : ""}" data-value="${encodeURIComponent(blockRefHTML)}">
|
|
|
|
${genHintItemHTML(item)}
|
|
|
|
</button>`;
|
2022-05-26 15:18:53 +08:00
|
|
|
});
|
|
|
|
if (searchHTML === "") {
|
2023-07-28 15:20:34 +08:00
|
|
|
searchHTML = `<button style="width: calc(100% - 16px)" class="b3-list-item b3-list-item--two" data-value="">${window.siyuan.languages.emptyContent}</button>`;
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
this.element.lastElementChild.innerHTML = searchHTML;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private genEmojiHTML(protyle: IProtyle, value = "") {
|
|
|
|
if (value && !this.enableEmoji) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const panelElement = this.element.querySelector(".emojis__panel");
|
|
|
|
if (panelElement) {
|
2023-06-01 22:39:57 +08:00
|
|
|
panelElement.innerHTML = filterEmoji(value, 256);
|
2022-05-26 15:18:53 +08:00
|
|
|
if (value) {
|
|
|
|
panelElement.nextElementSibling.classList.add("fn__none");
|
|
|
|
} else {
|
|
|
|
panelElement.nextElementSibling.classList.remove("fn__none");
|
|
|
|
}
|
2023-02-10 15:15:02 +08:00
|
|
|
lazyLoadEmojiImg(panelElement);
|
2022-05-26 15:18:53 +08:00
|
|
|
} else {
|
2023-02-08 17:33:49 +08:00
|
|
|
this.element.innerHTML = `<div class="emojis">
|
2023-06-01 22:39:57 +08:00
|
|
|
<div class="emojis__panel">${filterEmoji(value, 256)}</div>
|
2022-05-26 15:18:53 +08:00
|
|
|
<div class="fn__flex${value ? " fn__none" : ""}">
|
2023-07-12 11:46:58 +08:00
|
|
|
<button data-type="0" class="emojis__type ariaLabel" aria-label="${window.siyuan.languages.recentEmoji}">${unicode2Emoji("2b50")}</button>
|
|
|
|
<button data-type="1" class="emojis__type ariaLabel" aria-label="${window.siyuan.emojis[0][window.siyuan.config.lang === "zh_CN" ? "title_zh_cn" : "title"]}">${unicode2Emoji("1f527")}</button>
|
|
|
|
<button data-type="2" class="emojis__type ariaLabel" aria-label="${window.siyuan.emojis[1][window.siyuan.config.lang === "zh_CN" ? "title_zh_cn" : "title"]}">${unicode2Emoji("1f60d")}</button>
|
|
|
|
<button data-type="3" class="emojis__type ariaLabel" aria-label="${window.siyuan.emojis[2][window.siyuan.config.lang === "zh_CN" ? "title_zh_cn" : "title"]}">${unicode2Emoji("1f433")}</button>
|
|
|
|
<button data-type="4" class="emojis__type ariaLabel" aria-label="${window.siyuan.emojis[3][window.siyuan.config.lang === "zh_CN" ? "title_zh_cn" : "title"]}">${unicode2Emoji("1f96a")}</button>
|
|
|
|
<button data-type="5" class="emojis__type ariaLabel" aria-label="${window.siyuan.emojis[4][window.siyuan.config.lang === "zh_CN" ? "title_zh_cn" : "title"]}">${unicode2Emoji("1f3a8")}</button>
|
|
|
|
<button data-type="6" class="emojis__type ariaLabel" aria-label="${window.siyuan.emojis[5][window.siyuan.config.lang === "zh_CN" ? "title_zh_cn" : "title"]}">${unicode2Emoji("1f3dd")}</button>
|
|
|
|
<button data-type="7" class="emojis__type ariaLabel" aria-label="${window.siyuan.emojis[6][window.siyuan.config.lang === "zh_CN" ? "title_zh_cn" : "title"]}">${unicode2Emoji("1f52e")}</button>
|
|
|
|
<button data-type="8" class="emojis__type ariaLabel" aria-label="${window.siyuan.emojis[7][window.siyuan.config.lang === "zh_CN" ? "title_zh_cn" : "title"]}">${unicode2Emoji("267e")}</button>
|
|
|
|
<button data-type="9" class="emojis__type ariaLabel" aria-label="${window.siyuan.emojis[8][window.siyuan.config.lang === "zh_CN" ? "title_zh_cn" : "title"]}">${unicode2Emoji("1f6a9")}</button>
|
2022-05-26 15:18:53 +08:00
|
|
|
</div>
|
|
|
|
</div>`;
|
2023-06-01 22:39:57 +08:00
|
|
|
lazyLoadEmoji(this.element);
|
2023-02-10 15:15:02 +08:00
|
|
|
lazyLoadEmojiImg(this.element);
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
const firstEmojiElement = this.element.querySelector(".emojis__item");
|
|
|
|
if (firstEmojiElement) {
|
|
|
|
firstEmojiElement.classList.add("emojis__item--current");
|
|
|
|
this.element.classList.remove("fn__none");
|
|
|
|
this.element.style.width = Math.max(protyle.element.clientWidth / 2, 320) + "px";
|
|
|
|
const textareaPosition = getSelectionPosition(protyle.wysiwyg.element);
|
|
|
|
setPosition(this.element, textareaPosition.left, textareaPosition.top + 26, 30);
|
|
|
|
this.element.querySelector(".emojis__panel").scrollTop = 0;
|
|
|
|
} else {
|
|
|
|
this.element.classList.add("fn__none");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-20 18:40:29 +08:00
|
|
|
public fill(value: string, protyle: IProtyle, updateRange = true, refIsS = false) {
|
2022-05-26 15:18:53 +08:00
|
|
|
hideElements(["hint", "toolbar"], protyle);
|
2023-07-28 20:16:32 +08:00
|
|
|
if (updateRange && this.source !== "av") {
|
2023-04-01 17:36:34 +08:00
|
|
|
protyle.toolbar.range = getEditorRange(protyle.wysiwyg.element);
|
|
|
|
}
|
2022-09-30 22:44:39 +08:00
|
|
|
const range = protyle.toolbar.range;
|
|
|
|
let nodeElement = hasClosestBlock(protyle.toolbar.range.startContainer) as HTMLElement;
|
2022-05-26 15:18:53 +08:00
|
|
|
if (!nodeElement) {
|
|
|
|
return;
|
|
|
|
}
|
2023-07-28 20:16:32 +08:00
|
|
|
if (this.source === "av") {
|
2023-09-28 11:29:58 +08:00
|
|
|
const cellElement = hasClosestByClassName(protyle.toolbar.range.startContainer, "av__cell");
|
|
|
|
if (!cellElement) {
|
|
|
|
return;
|
|
|
|
}
|
2023-09-28 23:41:44 +08:00
|
|
|
const previousID = cellElement.dataset.blockId;
|
2023-07-28 20:16:32 +08:00
|
|
|
const avID = nodeElement.getAttribute("data-av-id");
|
|
|
|
let tempElement = document.createElement("div");
|
|
|
|
tempElement.innerHTML = value.replace(/<mark>/g, "").replace(/<\/mark>/g, "");
|
|
|
|
tempElement = tempElement.firstElementChild as HTMLDivElement;
|
|
|
|
if (value.startsWith("((newFile ") && value.endsWith(`${Lute.Caret}'))`)) {
|
|
|
|
const fileNames = value.substring(11, value.length - 4).split(`"${Constants.ZWSP}'`);
|
|
|
|
const realFileName = fileNames.length === 1 ? fileNames[0] : fileNames[1];
|
|
|
|
getSavePath(protyle.path, protyle.notebookId, (pathString) => {
|
|
|
|
fetchPost("/api/filetree/createDocWithMd", {
|
|
|
|
notebook: protyle.notebookId,
|
|
|
|
path: pathPosix().join(pathString, realFileName),
|
|
|
|
parentID: protyle.block.rootID,
|
|
|
|
markdown: ""
|
|
|
|
}, response => {
|
|
|
|
transaction(protyle, [{
|
2023-09-28 11:29:58 +08:00
|
|
|
action: "replaceAttrViewBlock",
|
2023-07-28 20:16:32 +08:00
|
|
|
avID,
|
|
|
|
previousID,
|
2023-09-28 11:29:58 +08:00
|
|
|
nextID: response.data,
|
|
|
|
isDetached: false,
|
2023-07-28 20:16:32 +08:00
|
|
|
}], [{
|
2023-09-28 11:29:58 +08:00
|
|
|
action: "replaceAttrViewBlock",
|
2023-07-28 20:16:32 +08:00
|
|
|
avID,
|
2023-09-28 11:29:58 +08:00
|
|
|
previousID: response.data,
|
|
|
|
nextID: previousID,
|
|
|
|
isDetached: true,
|
2023-07-28 20:16:32 +08:00
|
|
|
}]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
2023-07-31 11:32:36 +08:00
|
|
|
const sourceId = tempElement.getAttribute("data-id");
|
2023-07-28 20:16:32 +08:00
|
|
|
transaction(protyle, [{
|
2023-09-28 11:29:58 +08:00
|
|
|
action: "replaceAttrViewBlock",
|
2023-07-28 20:16:32 +08:00
|
|
|
avID,
|
|
|
|
previousID,
|
2023-09-28 11:29:58 +08:00
|
|
|
nextID: sourceId,
|
|
|
|
isDetached: false,
|
2023-07-28 20:16:32 +08:00
|
|
|
}], [{
|
2023-09-28 11:29:58 +08:00
|
|
|
action: "replaceAttrViewBlock",
|
2023-07-28 20:16:32 +08:00
|
|
|
avID,
|
2023-09-28 11:29:58 +08:00
|
|
|
previousID: sourceId,
|
|
|
|
nextID: previousID,
|
|
|
|
isDetached: true,
|
2023-07-28 20:16:32 +08:00
|
|
|
}]);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-02-08 21:43:16 +08:00
|
|
|
this.enableExtend = false;
|
2022-05-26 15:18:53 +08:00
|
|
|
let id = "";
|
|
|
|
if (nodeElement) {
|
|
|
|
id = nodeElement.getAttribute("data-node-id");
|
|
|
|
}
|
2022-09-27 00:13:11 +08:00
|
|
|
const html = nodeElement.outerHTML;
|
2022-05-26 15:18:53 +08:00
|
|
|
// 自顶向下法新建文档后光标定位问题 https://github.com/siyuan-note/siyuan/issues/299
|
|
|
|
// QQ 拼音输入法自动补全需移除补全内容 https://github.com/siyuan-note/siyuan/issues/320
|
|
|
|
// 前后有标记符的情况 https://github.com/siyuan-note/siyuan/issues/2511
|
|
|
|
const endSplit = Constants.BLOCK_HINT_CLOSE_KEYS[this.splitChar];
|
|
|
|
if (Constants.BLOCK_HINT_KEYS.includes(this.splitChar) && endSplit && range.startContainer.nodeType === 3
|
2023-03-29 11:09:28 +08:00
|
|
|
&& (range.startContainer as Text).wholeText.indexOf(endSplit) > -1
|
|
|
|
// 在包含 )) 的块中引用时会丢失字符 https://ld246.com/article/1679980200782
|
|
|
|
&& (range.startContainer as Text).wholeText.indexOf(this.splitChar) > -1) {
|
2022-05-26 15:18:53 +08:00
|
|
|
let matchEndChar = 0;
|
|
|
|
let textNode = range.startContainer;
|
|
|
|
while (textNode && matchEndChar < 2) {
|
|
|
|
const index = textNode.textContent.indexOf(endSplit);
|
|
|
|
const startIndex = textNode.textContent.indexOf(this.splitChar);
|
|
|
|
if (index > -1 && (index < startIndex || startIndex < 0)) {
|
|
|
|
matchEndChar = 2;
|
|
|
|
range.setEnd(textNode, index + 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const indexOne = textNode.textContent.indexOf(endSplit.substr(1));
|
|
|
|
if (indexOne > -1) {
|
|
|
|
matchEndChar += 1;
|
|
|
|
}
|
|
|
|
if (matchEndChar === 2) {
|
|
|
|
range.setEnd(textNode, indexOne + 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
textNode = textNode.nextSibling;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.lastIndex > -1) {
|
|
|
|
range.setStart(range.startContainer, this.lastIndex);
|
2023-09-06 17:19:11 +08:00
|
|
|
if (isIPhone()) {
|
2023-06-22 22:39:41 +08:00
|
|
|
focusByRange(range);
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
// 新建文件
|
2022-08-01 23:53:35 +08:00
|
|
|
if (Constants.BLOCK_HINT_KEYS.includes(this.splitChar) && value.startsWith("((newFile ") && value.endsWith(`${Lute.Caret}'))`)) {
|
2022-05-26 15:18:53 +08:00
|
|
|
focusByRange(range);
|
2022-08-02 11:15:18 +08:00
|
|
|
const fileNames = value.substring(11, value.length - 4).split(`"${Constants.ZWSP}'`);
|
2022-08-01 23:53:35 +08:00
|
|
|
const realFileName = fileNames.length === 1 ? fileNames[0] : fileNames[1];
|
2022-05-26 15:18:53 +08:00
|
|
|
getSavePath(protyle.path, protyle.notebookId, (pathString) => {
|
|
|
|
fetchPost("/api/filetree/createDocWithMd", {
|
|
|
|
notebook: protyle.notebookId,
|
2022-08-01 23:53:35 +08:00
|
|
|
path: pathPosix().join(pathString, realFileName),
|
2023-05-01 11:52:49 +08:00
|
|
|
parentID: protyle.block.rootID,
|
2022-05-26 15:18:53 +08:00
|
|
|
markdown: ""
|
|
|
|
}, response => {
|
2022-09-26 00:58:40 +08:00
|
|
|
protyle.toolbar.setInlineMark(protyle, "block-ref", "range", {
|
|
|
|
type: "id",
|
2023-04-20 19:13:30 +08:00
|
|
|
color: `${response.data}${Constants.ZWSP}${(fileNames.length === 2 || refIsS) ? "s" : "d"}${Constants.ZWSP}${(fileNames.length === 2 ? fileNames[0] : realFileName).substring(0, window.siyuan.config.editor.blockRefDynamicAnchorTextMaxLen)}`
|
2022-09-26 00:58:40 +08:00
|
|
|
});
|
2022-05-26 15:18:53 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Constants.BLOCK_HINT_KEYS.includes(this.splitChar)) {
|
|
|
|
if (value === "") {
|
|
|
|
const editElement = getContenteditableElement(nodeElement);
|
|
|
|
if (editElement.textContent === "") {
|
|
|
|
editElement.innerHTML = "<wbr>";
|
|
|
|
focusByWbr(editElement, range);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2022-09-18 22:16:15 +08:00
|
|
|
let tempElement = document.createElement("div");
|
2022-05-26 15:18:53 +08:00
|
|
|
tempElement.innerHTML = value.replace(/<mark>/g, "").replace(/<\/mark>/g, "");
|
2022-09-26 00:58:40 +08:00
|
|
|
tempElement = tempElement.firstElementChild as HTMLDivElement;
|
2023-04-20 18:40:29 +08:00
|
|
|
if (refIsS) {
|
2023-04-22 15:12:38 +08:00
|
|
|
const staticText = range.toString().replace(this.splitChar, "");
|
2023-04-20 18:43:14 +08:00
|
|
|
if (staticText) {
|
|
|
|
tempElement.setAttribute("data-subtype", "s");
|
|
|
|
tempElement.innerText = staticText;
|
|
|
|
}
|
2023-04-20 18:40:29 +08:00
|
|
|
}
|
2022-09-26 00:58:40 +08:00
|
|
|
protyle.toolbar.setInlineMark(protyle, "block-ref", "range", {
|
|
|
|
type: "id",
|
|
|
|
color: `${tempElement.getAttribute("data-id")}${Constants.ZWSP}${tempElement.getAttribute("data-subtype")}${Constants.ZWSP}${tempElement.textContent}`
|
|
|
|
});
|
2022-05-26 15:18:53 +08:00
|
|
|
return;
|
|
|
|
} else if (this.splitChar === ":") {
|
|
|
|
addEmoji(value);
|
|
|
|
let emoji;
|
|
|
|
if (value.indexOf(".") > -1) {
|
|
|
|
emoji = `:${value.split(".")[0]}: `;
|
|
|
|
} else {
|
2023-06-01 22:39:57 +08:00
|
|
|
emoji = unicode2Emoji(value) + " ";
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
insertHTML(protyle.lute.SpinBlockDOM(emoji), protyle);
|
2023-09-27 15:26:22 +08:00
|
|
|
} else if (["「「", "「『", "『「", "『『", "{{"].includes(this.splitChar) || this.splitChar === "#" || this.splitChar === ":") {
|
2022-05-26 15:18:53 +08:00
|
|
|
if (value === "") {
|
|
|
|
const editElement = getContenteditableElement(nodeElement);
|
|
|
|
if (editElement.textContent === "") {
|
|
|
|
editElement.innerHTML = "<wbr>";
|
|
|
|
focusByWbr(editElement, range);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-03-08 23:18:21 +08:00
|
|
|
insertHTML(protyle.lute.SpinBlockDOM(value), protyle, false, isMobile());
|
2022-05-26 15:18:53 +08:00
|
|
|
blockRender(protyle, protyle.wysiwyg.element);
|
|
|
|
return;
|
|
|
|
} else if (this.splitChar === "/" || this.splitChar === "、") {
|
2023-02-08 17:33:49 +08:00
|
|
|
this.enableExtend = true;
|
2022-05-26 15:18:53 +08:00
|
|
|
if (value === "((" || value === "{{") {
|
|
|
|
if (value === "((") {
|
2023-07-28 20:16:32 +08:00
|
|
|
hintRef("", protyle, "hint");
|
2022-05-26 15:18:53 +08:00
|
|
|
} else {
|
|
|
|
hintEmbed("", protyle);
|
|
|
|
}
|
|
|
|
this.splitChar = value;
|
|
|
|
this.lastIndex = 0;
|
2022-09-05 22:01:57 +08:00
|
|
|
range.deleteContents();
|
2022-05-26 15:18:53 +08:00
|
|
|
const textNode = document.createTextNode(value);
|
|
|
|
range.insertNode(textNode);
|
|
|
|
range.setEnd(textNode, value.length);
|
|
|
|
range.collapse(false);
|
|
|
|
return;
|
|
|
|
} else if (value === Constants.ZWSP) {
|
2022-09-05 22:01:57 +08:00
|
|
|
range.deleteContents();
|
2022-09-10 22:00:21 +08:00
|
|
|
this.fixImageCursor(range);
|
2022-05-26 15:18:53 +08:00
|
|
|
protyle.toolbar.showTpl(protyle, nodeElement, range);
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, html);
|
|
|
|
return;
|
|
|
|
} else if (value === Constants.ZWSP + 1) {
|
2022-09-05 22:01:57 +08:00
|
|
|
range.deleteContents();
|
2022-09-10 22:00:21 +08:00
|
|
|
this.fixImageCursor(range);
|
2022-05-26 15:18:53 +08:00
|
|
|
protyle.toolbar.showWidget(protyle, nodeElement, range);
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, html);
|
|
|
|
return;
|
|
|
|
} else if (value === Constants.ZWSP + 2) {
|
2022-09-05 22:01:57 +08:00
|
|
|
range.deleteContents();
|
2022-09-10 22:00:21 +08:00
|
|
|
this.fixImageCursor(range);
|
2023-10-01 10:26:31 +08:00
|
|
|
protyle.toolbar.range = range;
|
|
|
|
const rangePosition = getSelectionPosition(nodeElement, range);
|
|
|
|
protyle.toolbar.showAssets(protyle, {x: rangePosition.left, y: rangePosition.top + 26, w: 0, h: 26});
|
2022-05-26 15:18:53 +08:00
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, html);
|
|
|
|
return;
|
|
|
|
} else if (value === Constants.ZWSP + 3) {
|
2022-09-05 22:01:57 +08:00
|
|
|
range.deleteContents();
|
2022-05-26 15:18:53 +08:00
|
|
|
return;
|
|
|
|
} else if (value === Constants.ZWSP + 4) {
|
|
|
|
const newSubDocId = Lute.NewNodeID();
|
|
|
|
fetchPost("/api/filetree/createDoc", {
|
|
|
|
notebook: protyle.notebookId,
|
|
|
|
path: pathPosix().join(getDisplayName(protyle.path, false, true), newSubDocId + ".sy"),
|
2022-06-16 20:23:19 +08:00
|
|
|
title: "Untitled",
|
2022-05-26 15:18:53 +08:00
|
|
|
md: ""
|
|
|
|
}, () => {
|
2022-11-14 23:14:44 +08:00
|
|
|
insertHTML(`<span data-type="block-ref" data-id="${newSubDocId}" data-subtype="d">Untitled</span>`, protyle);
|
2022-06-29 20:25:30 +08:00
|
|
|
/// #if MOBILE
|
2023-06-01 20:50:49 +08:00
|
|
|
openMobileFileById(protyle.app, newSubDocId, [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT]);
|
2022-06-29 20:25:30 +08:00
|
|
|
/// #else
|
|
|
|
openFileById({
|
2023-06-01 20:50:49 +08:00
|
|
|
app: protyle.app,
|
2022-06-29 20:25:30 +08:00
|
|
|
id: newSubDocId,
|
2022-08-07 09:26:08 +08:00
|
|
|
action: [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT]
|
2022-06-29 20:25:30 +08:00
|
|
|
});
|
|
|
|
/// #endif
|
2022-05-26 15:18:53 +08:00
|
|
|
});
|
|
|
|
return;
|
2023-03-03 22:50:45 +08:00
|
|
|
} else if (value === Constants.ZWSP + 5) {
|
2023-03-31 20:07:29 +08:00
|
|
|
range.deleteContents();
|
2023-03-08 23:35:36 +08:00
|
|
|
AIChat(protyle, nodeElement);
|
2023-03-03 22:50:45 +08:00
|
|
|
return;
|
2022-05-26 15:18:53 +08:00
|
|
|
} else if (Constants.INLINE_TYPE.includes(value)) {
|
2022-09-05 22:01:57 +08:00
|
|
|
range.deleteContents();
|
2022-07-21 22:23:08 +08:00
|
|
|
focusByRange(range);
|
2022-09-26 00:14:09 +08:00
|
|
|
if (["a", "block-ref", "inline-math", "inline-memo", "text"].includes(value)) {
|
2023-03-04 11:45:36 +08:00
|
|
|
protyle.toolbar.element.querySelector(`[data-type="${value}"]`).dispatchEvent(new CustomEvent("click"));
|
2022-09-26 00:14:09 +08:00
|
|
|
return;
|
|
|
|
}
|
2022-09-15 20:19:56 +08:00
|
|
|
protyle.toolbar.setInlineMark(protyle, value, "range");
|
2022-05-26 15:18:53 +08:00
|
|
|
return;
|
|
|
|
} else if (value === "emoji") {
|
2022-09-05 22:01:57 +08:00
|
|
|
range.deleteContents();
|
2022-05-26 15:18:53 +08:00
|
|
|
range.insertNode(document.createTextNode(":"));
|
|
|
|
range.collapse(false);
|
|
|
|
this.genEmojiHTML(protyle);
|
|
|
|
return;
|
|
|
|
} else if (value.indexOf("style") > -1) {
|
2022-09-05 22:01:57 +08:00
|
|
|
range.deleteContents();
|
2022-09-10 22:00:21 +08:00
|
|
|
this.fixImageCursor(range);
|
2022-05-26 15:18:53 +08:00
|
|
|
nodeElement.setAttribute("style", value.split(Constants.ZWSP)[1] || "");
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, html);
|
|
|
|
return;
|
2023-06-23 16:27:54 +08:00
|
|
|
} else if (value.startsWith("plugin")) {
|
|
|
|
protyle.app.plugins.find((plugin) => {
|
2023-06-28 22:57:18 +08:00
|
|
|
const ids = value.split(Constants.ZWSP);
|
2023-06-23 16:27:54 +08:00
|
|
|
if (ids[1] === plugin.name) {
|
|
|
|
plugin.protyleSlash.find((slash) => {
|
2023-07-28 20:16:32 +08:00
|
|
|
if (slash.id === ids[2]) {
|
2023-06-23 16:27:54 +08:00
|
|
|
slash.callback(protyle.getInstance());
|
|
|
|
return true;
|
|
|
|
}
|
2023-06-28 22:57:18 +08:00
|
|
|
});
|
2023-07-28 20:16:32 +08:00
|
|
|
return true;
|
2023-06-23 16:27:54 +08:00
|
|
|
}
|
2023-06-28 22:57:18 +08:00
|
|
|
});
|
2023-06-23 16:27:54 +08:00
|
|
|
return;
|
2022-05-26 15:18:53 +08:00
|
|
|
} else {
|
2022-09-05 22:01:57 +08:00
|
|
|
range.deleteContents();
|
2022-09-10 22:00:21 +08:00
|
|
|
if (value !== "![]()") {
|
|
|
|
this.fixImageCursor(range);
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
let textContent = value;
|
|
|
|
if (value === "```") {
|
2022-12-31 22:14:42 +08:00
|
|
|
textContent = value + window.siyuan.storage[Constants.LOCAL_CODELANG] + Lute.Caret + "\n```";
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
const editableElement = getContenteditableElement(nodeElement);
|
|
|
|
if (value === "![]()") { // https://github.com/siyuan-note/siyuan/issues/4586 1
|
|
|
|
let newHTML = "";
|
|
|
|
range.insertNode(document.createElement("wbr"));
|
|
|
|
range.insertNode(document.createTextNode(value));
|
|
|
|
newHTML = protyle.lute.SpinBlockDOM(nodeElement.outerHTML);
|
|
|
|
nodeElement.outerHTML = newHTML;
|
|
|
|
nodeElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${id}"]`);
|
|
|
|
focusByWbr(nodeElement, range);
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, html);
|
|
|
|
let imgElement: HTMLElement = range.startContainer.childNodes[range.startOffset - 1] as HTMLElement || range.startContainer as HTMLElement;
|
|
|
|
if (imgElement && imgElement.nodeType !== 3 && imgElement.classList.contains("img")) {
|
|
|
|
// 已经找到图片
|
2023-03-02 11:06:33 +08:00
|
|
|
} else if (imgElement.previousSibling?.nodeType !== 3 && (imgElement.previousSibling as HTMLElement).classList.contains("img")) {
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/7540
|
|
|
|
imgElement = imgElement.previousSibling as HTMLElement;
|
2022-05-26 15:18:53 +08:00
|
|
|
} else {
|
2023-03-02 11:06:33 +08:00
|
|
|
Array.from(nodeElement.querySelectorAll(".img")).find((item: HTMLElement) => {
|
|
|
|
if (item.querySelector("img").getAttribute("data-src") === "") {
|
|
|
|
imgElement = item;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
const rect = imgElement.getBoundingClientRect();
|
2023-06-01 20:50:49 +08:00
|
|
|
imgMenu(protyle, range, imgElement, {
|
2022-05-26 15:18:53 +08:00
|
|
|
clientX: rect.left,
|
|
|
|
clientY: rect.top
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
} else if (editableElement.textContent === "" && nodeElement.getAttribute("data-type") === "NodeParagraph") {
|
|
|
|
let newHTML = "";
|
|
|
|
if (value === "<div>") {
|
2023-07-19 13:55:42 +08:00
|
|
|
newHTML = `<div data-node-id="${id}" data-type="NodeHTMLBlock" class="render-node" data-subtype="block">${genIconHTML()}<div><protyle-html data-content=""></protyle-html><span style="position: absolute">${Constants.ZWSP}</span></div><div class="protyle-attr" contenteditable="false"></div></div>`;
|
2022-05-26 15:18:53 +08:00
|
|
|
} else {
|
|
|
|
editableElement.textContent = textContent;
|
|
|
|
newHTML = protyle.lute.SpinBlockDOM(nodeElement.outerHTML);
|
|
|
|
}
|
|
|
|
nodeElement.outerHTML = newHTML;
|
|
|
|
nodeElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${id}"]`);
|
2022-12-26 20:13:36 +08:00
|
|
|
// https://github.com/siyuan-note/siyuan/issues/6864
|
|
|
|
if (nodeElement.getAttribute("data-type") === "NodeTable") {
|
|
|
|
nodeElement.querySelectorAll("colgroup col").forEach((item: HTMLElement) => {
|
2022-12-29 15:13:37 +08:00
|
|
|
item.style.minWidth = "60px";
|
2022-12-26 20:13:36 +08:00
|
|
|
});
|
|
|
|
newHTML = nodeElement.outerHTML;
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
updateTransaction(protyle, id, newHTML, html);
|
|
|
|
} else {
|
|
|
|
let newHTML = protyle.lute.SpinBlockDOM(textContent);
|
|
|
|
if (value === "<div>") {
|
2023-07-19 13:55:42 +08:00
|
|
|
newHTML = `<div data-node-id="${Lute.NewNodeID()}" data-type="NodeHTMLBlock" class="render-node" data-subtype="block">${genIconHTML()}<div><protyle-html data-content=""></protyle-html><span style="position: absolute">${Constants.ZWSP}</span></div><div class="protyle-attr" contenteditable="false"></div></div>`;
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
nodeElement.insertAdjacentHTML("afterend", newHTML);
|
2022-12-26 20:13:36 +08:00
|
|
|
const oldHTML = nodeElement.outerHTML;
|
2022-05-26 15:18:53 +08:00
|
|
|
const newId = newHTML.substr(newHTML.indexOf('data-node-id="') + 14, 22);
|
2022-12-26 20:13:36 +08:00
|
|
|
nodeElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${newId}"]`);
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/6864
|
|
|
|
if (nodeElement.getAttribute("data-type") === "NodeTable") {
|
|
|
|
nodeElement.querySelectorAll("colgroup col").forEach((item: HTMLElement) => {
|
2022-12-29 15:13:37 +08:00
|
|
|
item.style.minWidth = "60px";
|
2022-12-26 20:13:36 +08:00
|
|
|
});
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
transaction(protyle, [{
|
2022-12-26 20:13:36 +08:00
|
|
|
data: oldHTML,
|
2022-05-26 15:18:53 +08:00
|
|
|
id,
|
|
|
|
action: "update"
|
|
|
|
}, {
|
2022-12-26 20:13:36 +08:00
|
|
|
data: nodeElement.outerHTML,
|
2022-05-26 15:18:53 +08:00
|
|
|
id: newId,
|
|
|
|
previousID: id,
|
|
|
|
action: "insert"
|
|
|
|
}], [{
|
|
|
|
id: newId,
|
|
|
|
action: "delete"
|
|
|
|
}, {
|
|
|
|
data: html,
|
|
|
|
id,
|
|
|
|
action: "update"
|
|
|
|
}]);
|
|
|
|
}
|
|
|
|
if (value === "<div>" || value === "$$" || (value.indexOf("```") > -1 && value.length > 3)) {
|
|
|
|
protyle.toolbar.showRender(protyle, nodeElement);
|
2022-08-19 14:39:19 +08:00
|
|
|
processRender(nodeElement);
|
2022-05-26 15:18:53 +08:00
|
|
|
} else if (value.startsWith("```")) {
|
|
|
|
highlightRender(nodeElement);
|
|
|
|
} else if (value.startsWith("<iframe") || value.startsWith("<video") || value.startsWith("<audio")) {
|
|
|
|
protyle.gutter.renderMenu(protyle, nodeElement);
|
|
|
|
const rect = nodeElement.getBoundingClientRect();
|
2022-08-02 11:13:11 +08:00
|
|
|
window.siyuan.menus.menu.popup({
|
|
|
|
x: rect.left,
|
|
|
|
y: rect.top
|
2022-08-02 11:15:18 +08:00
|
|
|
}, true);
|
2022-08-15 10:26:49 +08:00
|
|
|
const itemElement = window.siyuan.menus.menu.element.querySelector('[data-id="assetSubMenu"]');
|
2022-08-15 09:29:34 +08:00
|
|
|
itemElement.classList.add("b3-menu__item--show");
|
|
|
|
window.siyuan.menus.menu.showSubMenu(itemElement.querySelector(".b3-menu__submenu"));
|
2023-09-02 19:45:34 +08:00
|
|
|
window.siyuan.menus.menu.element.querySelector("textarea").focus();
|
2022-05-26 15:18:53 +08:00
|
|
|
} else if (value === "---") {
|
2022-07-20 19:58:31 +08:00
|
|
|
focusBlock(nodeElement);
|
2023-06-08 12:09:12 +08:00
|
|
|
} else if (nodeElement.classList.contains("av")) {
|
2023-09-09 23:21:46 +08:00
|
|
|
avRender(nodeElement, protyle);
|
2022-05-26 15:18:53 +08:00
|
|
|
} else {
|
|
|
|
focusByWbr(nodeElement, range);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public select(event: KeyboardEvent, protyle: IProtyle) {
|
|
|
|
const isEmojiPanel = this.element.firstElementChild.classList.contains("emojis");
|
|
|
|
if (this.element.querySelectorAll("button").length === 0 && !isEmojiPanel) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (event.key === "Enter") {
|
|
|
|
if (isEmojiPanel) {
|
|
|
|
const currentElement = this.element.querySelector(".emojis__item--current");
|
|
|
|
if (!currentElement) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const unicode = currentElement.getAttribute("data-unicode");
|
|
|
|
if (this.element.querySelectorAll(".emojis__title").length > 2) {
|
|
|
|
// /emoji 后会自动添加冒号,导致 range 无法计算,因此不依赖 this.fill
|
|
|
|
const range = getSelection().getRangeAt(0);
|
|
|
|
if (range.endContainer.nodeType !== 3) {
|
|
|
|
range.endContainer.childNodes[range.endOffset - 1]?.remove();
|
|
|
|
}
|
|
|
|
addEmoji(unicode);
|
|
|
|
let emoji;
|
|
|
|
if (unicode.indexOf(".") > -1) {
|
|
|
|
emoji = `:${unicode.split(".")[0]}: `;
|
|
|
|
} else {
|
2023-06-01 22:39:57 +08:00
|
|
|
emoji = unicode2Emoji(unicode) + " ";
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
insertHTML(protyle.lute.SpinBlockDOM(emoji), protyle);
|
|
|
|
this.element.classList.add("fn__none");
|
|
|
|
} else {
|
|
|
|
this.fill(unicode, protyle);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const mark = decodeURIComponent(this.element.querySelector(".b3-list-item--focus").getAttribute("data-value"));
|
|
|
|
if (mark === Constants.ZWSP + 3) {
|
|
|
|
(this.element.querySelector(".b3-list-item--focus input") as HTMLElement).click();
|
|
|
|
} else {
|
2023-04-20 18:40:29 +08:00
|
|
|
this.fill(mark, protyle, true, isCtrl(event));
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (isEmojiPanel) {
|
|
|
|
const currentElement = this.element.querySelector(".emojis__item--current");
|
|
|
|
if (!currentElement) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let newCurrentElement: HTMLElement;
|
|
|
|
if (event.key === "ArrowLeft" || event.key === "ArrowUp") {
|
|
|
|
if (currentElement.previousElementSibling) {
|
|
|
|
currentElement.classList.remove("emojis__item--current");
|
|
|
|
newCurrentElement = currentElement.previousElementSibling as HTMLElement;
|
|
|
|
} else if (currentElement.parentElement.previousElementSibling?.previousElementSibling) {
|
|
|
|
currentElement.classList.remove("emojis__item--current");
|
|
|
|
newCurrentElement = currentElement.parentElement.previousElementSibling.previousElementSibling.lastElementChild as HTMLElement;
|
|
|
|
}
|
|
|
|
} else if (event.key === "ArrowRight" || event.key === "ArrowDown") {
|
|
|
|
if (currentElement.nextElementSibling) {
|
|
|
|
currentElement.classList.remove("emojis__item--current");
|
|
|
|
newCurrentElement = currentElement.nextElementSibling as HTMLElement;
|
|
|
|
} else if (currentElement.parentElement.nextElementSibling?.nextElementSibling) {
|
|
|
|
currentElement.classList.remove("emojis__item--current");
|
|
|
|
newCurrentElement = currentElement.parentElement.nextElementSibling.nextElementSibling.firstElementChild as HTMLElement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (newCurrentElement) {
|
|
|
|
newCurrentElement.classList.add("emojis__item--current");
|
|
|
|
const topHeight = 4;
|
|
|
|
const emojisContentElement = this.element.querySelector(".emojis__panel");
|
|
|
|
if (newCurrentElement.offsetTop - topHeight < emojisContentElement.scrollTop) {
|
|
|
|
emojisContentElement.scrollTop = newCurrentElement.offsetTop - topHeight;
|
|
|
|
} else if (newCurrentElement.offsetTop - topHeight - emojisContentElement.clientHeight + newCurrentElement.clientHeight > emojisContentElement.scrollTop) {
|
|
|
|
emojisContentElement.scrollTop = newCurrentElement.offsetTop - topHeight - emojisContentElement.clientHeight + newCurrentElement.clientHeight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
return true;
|
|
|
|
} else if (event.key === "ArrowDown" || event.key === "ArrowUp") {
|
2023-08-02 11:08:35 +08:00
|
|
|
upDownHint(this.element.firstElementChild, event);
|
2022-05-26 15:18:53 +08:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
return true;
|
|
|
|
}
|
2023-01-22 18:51:23 +08:00
|
|
|
if (event.key === "ArrowLeft" || event.key === "ArrowRight") {
|
|
|
|
hideElements(["hint"], protyle);
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
return true;
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:00:21 +08:00
|
|
|
private fixImageCursor(range: Range) {
|
|
|
|
const previous = hasPreviousSibling(range.startContainer);
|
|
|
|
if (previous && previous.nodeType !== 3 && (previous as HTMLElement).classList.contains("img")) {
|
|
|
|
if (!hasNextSibling(previous)) {
|
2022-09-14 11:18:44 +08:00
|
|
|
range.insertNode(document.createTextNode(Constants.ZWSP));
|
2022-09-10 22:00:21 +08:00
|
|
|
range.collapse(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
private getKey(currentLineValue: string, extend: IHintExtend[]) {
|
|
|
|
this.lastIndex = -1;
|
|
|
|
this.splitChar = "";
|
|
|
|
extend.forEach((item) => {
|
|
|
|
const currentLastIndex = currentLineValue.lastIndexOf(item.key);
|
|
|
|
if (this.lastIndex < currentLastIndex) {
|
|
|
|
if (Constants.BLOCK_HINT_KEYS.includes(this.splitChar) &&
|
|
|
|
(item.key === ":" || item.key === "#" || item.key === "/" || item.key === "、")) {
|
|
|
|
// 块搜索中忽略以上符号
|
|
|
|
} else if (this.splitChar === "#" &&
|
|
|
|
(item.key === "/" || item.key === "、")) {
|
|
|
|
// 标签中忽略以上符号
|
|
|
|
} else {
|
|
|
|
this.splitChar = item.key;
|
|
|
|
this.lastIndex = currentLastIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (this.lastIndex === -1) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
// 冒号前为数字或冒号不进行emoji提示
|
2023-02-08 17:33:49 +08:00
|
|
|
if (this.splitChar === ":") {
|
|
|
|
this.enableEmoji = !(/\d/.test(currentLineValue.substr(this.lastIndex - 1, 1)) ||
|
|
|
|
currentLineValue.substr(this.lastIndex - 1, 2) === "::");
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
const lineArray = currentLineValue.split(this.splitChar);
|
|
|
|
const lastItem = lineArray[lineArray.length - 1];
|
|
|
|
if (lineArray.length > 1 && lastItem.trim() === lastItem && lastItem.length < Constants.SIZE_TITLE) {
|
|
|
|
// 输入法自动补全 https://github.com/siyuan-note/insider/issues/100
|
|
|
|
if (this.splitChar === "【【" && currentLineValue.endsWith("【【】")) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return lastItem;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|