From f4c1687d4d61eebcf43b5299f92f2d0ca527575c Mon Sep 17 00:00:00 2001 From: Vanessa Date: Mon, 26 Sep 2022 00:14:09 +0800 Subject: [PATCH] :recycle: set inline type --- app/src/block/popover.ts | 4 ++-- app/src/protyle/hint/index.ts | 7 ++++++- app/src/protyle/toolbar/BlockRef.ts | 3 +++ app/src/protyle/toolbar/Font.ts | 3 +++ app/src/protyle/toolbar/index.ts | 4 ---- app/src/protyle/wysiwyg/keydown.ts | 6 +++++- 6 files changed, 19 insertions(+), 8 deletions(-) diff --git a/app/src/block/popover.ts b/app/src/block/popover.ts index 8f9338346..05f3a64b4 100644 --- a/app/src/block/popover.ts +++ b/app/src/block/popover.ts @@ -49,7 +49,7 @@ export const initBlockPopover = () => { } let popoverTargetElement = hasClosestByAttribute(event.target, "data-type", "block-ref") as HTMLElement || hasClosestByAttribute(event.target, "data-type", "virtual-block-ref") as HTMLElement; - if (popoverTargetElement && popoverTargetElement.classList.contains("protyle-toolbar")) { + if (popoverTargetElement && popoverTargetElement.classList.contains("b3-tooltips")) { popoverTargetElement = undefined; } if (!popoverTargetElement) { @@ -107,7 +107,7 @@ export const initBlockPopover = () => { } let popoverTargetElement = hasClosestByAttribute(event.target, "data-type", "block-ref") as HTMLElement || hasClosestByAttribute(event.target, "data-type", "virtual-block-ref") as HTMLElement; - if (popoverTargetElement && popoverTargetElement.classList.contains("protyle-toolbar")) { + if (popoverTargetElement && popoverTargetElement.classList.contains("b3-tooltips")) { popoverTargetElement = undefined; } if (!popoverTargetElement) { diff --git a/app/src/protyle/hint/index.ts b/app/src/protyle/hint/index.ts index f9dd1190c..8e6332438 100644 --- a/app/src/protyle/hint/index.ts +++ b/app/src/protyle/hint/index.ts @@ -31,6 +31,7 @@ import {openFileById} from "../../editor/util"; import {openMobileFileById} from "../../mobile/editor"; import {getIconByType} from "../../editor/getIcon"; import {processRender} from "../util/processCode"; +import {getEventName} from "../util/compatibility"; export class Hint { public timeId: number; @@ -245,7 +246,7 @@ ${unicode2Emoji(emoji.unicode, true)}`; } if (hasSearch) { const searchElement = this.element.querySelector("input.b3-text-field") as HTMLInputElement; - const oldValue = this.element.querySelector("mark").textContent; + const oldValue = this.element.querySelector("mark")?.textContent || ""; searchElement.value = oldValue; searchElement.select(); searchElement.addEventListener("keydown", (event: KeyboardEvent) => { @@ -531,6 +532,10 @@ ${unicode2Emoji(emoji.unicode, true)}`; range.deleteContents(); focusByRange(range); protyle.toolbar.range = range; + if (["a", "block-ref", "inline-math", "inline-memo", "text"].includes(value)) { + protyle.toolbar.element.querySelector(`[data-type="${value}"]`).dispatchEvent(new CustomEvent("block-ref" === value? getEventName() : "click")); + return; + } protyle.toolbar.setInlineMark(protyle, value, "range"); return; } else if (value === "emoji") { diff --git a/app/src/protyle/toolbar/BlockRef.ts b/app/src/protyle/toolbar/BlockRef.ts index f1291f3d7..6c00f6c40 100644 --- a/app/src/protyle/toolbar/BlockRef.ts +++ b/app/src/protyle/toolbar/BlockRef.ts @@ -9,6 +9,9 @@ export class BlockRef extends ToolbarItem { super(protyle, menuItem); // 不能用 getEventName,否则会导致光标位置变动到点击的文档中 this.element.addEventListener("click", (event: MouseEvent & { changedTouches: MouseEvent[] }) => { + if (protyle.toolbar.range.toString() === "") { + return; + } fixTableRange(protyle.toolbar.range); hintRef(protyle.toolbar.range.toString(), protyle, true); protyle.toolbar.element.classList.add("fn__none"); diff --git a/app/src/protyle/toolbar/Font.ts b/app/src/protyle/toolbar/Font.ts index 852911fd2..8ea3c97fe 100644 --- a/app/src/protyle/toolbar/Font.ts +++ b/app/src/protyle/toolbar/Font.ts @@ -11,6 +11,9 @@ export class Font extends ToolbarItem { constructor(protyle: IProtyle, menuItem: IMenuItem) { super(protyle, menuItem); this.element.addEventListener(getEventName(), () => { + if (protyle.toolbar.range.toString() === "") { + return; + } protyle.toolbar.element.classList.add("fn__none"); protyle.toolbar.subElement.innerHTML = ""; protyle.toolbar.subElement.style.width = ""; diff --git a/app/src/protyle/toolbar/index.ts b/app/src/protyle/toolbar/index.ts index 02d372eb1..dbd549e5c 100644 --- a/app/src/protyle/toolbar/index.ts +++ b/app/src/protyle/toolbar/index.ts @@ -237,10 +237,6 @@ export class Toolbar { } public setInlineMark(protyle: IProtyle, type: string, action: "range" | "toolbar", textObj?: ITextOption) { - if (["a", "block-ref", "inline-math", "inline-memo"].includes(type) || (type === "text" && !textObj)) { - protyle.toolbar.element.querySelector(`[data-type="${type}"]`).dispatchEvent(new CustomEvent("block-ref" === type ? getEventName() : "click")); - return; - } const nodeElement = hasClosestBlock(this.range.startContainer); if (!nodeElement) { return; diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.ts index 7d2c73d67..ae44da9ce 100644 --- a/app/src/protyle/wysiwyg/keydown.ts +++ b/app/src/protyle/wysiwyg/keydown.ts @@ -1,5 +1,5 @@ import {hideElements} from "../ui/hideElements"; -import {isCtrl, isMac, writeText} from "../util/compatibility"; +import {getEventName, isCtrl, isMac, writeText} from "../util/compatibility"; import { focusBlock, focusByRange, @@ -1197,6 +1197,10 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => { } if (matchHotKey(menuItem.hotkey, event)) { protyle.toolbar.range = getEditorRange(protyle.wysiwyg.element); + if (["a", "block-ref", "inline-math", "inline-memo", "text"].includes(menuItem.name)) { + protyle.toolbar.element.querySelector(`[data-type="${menuItem.name}"]`).dispatchEvent(new CustomEvent("block-ref" === menuItem.name ? getEventName() : "click")); + return true; + } protyle.toolbar.setInlineMark(protyle, menuItem.name, "range"); return true; }