Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: Improve embed block toolbar visibility while editing https://github.com/siyuan-note/siyuan/issues/19794

Danield00bd8e41087
diff --git a/app/src/assets/scss/protyle/_wysiwyg.scss b/app/src/assets/scss/protyle/_wysiwyg.scss
index e7e0bfd723..03213de87e 100644
--- a/app/src/assets/scss/protyle/_wysiwyg.scss
+++ b/app/src/assets/scss/protyle/_wysiwyg.scss
@@ -528,6 +528,11 @@
// https://github.com/siyuan-note/siyuan/issues/14678
z-index: 2;
+ &.protyle-icons--editing-obscured {
+ opacity: 0;
+ pointer-events: none;
+ }
+
& + .protyle-wysiwyg__embed {
border-top: 0;
}
diff --git a/app/src/protyle/wysiwyg/embedToolbarVisibility.ts b/app/src/protyle/wysiwyg/embedToolbarVisibility.ts
new file mode 100644
index 0000000000..a00dcfd8c8
--- /dev/null
+++ b/app/src/protyle/wysiwyg/embedToolbarVisibility.ts
@@ -0,0 +1,55 @@
+const EMBED_SELECTOR = '[data-type="NodeBlockQueryEmbed"]';
+const HIDDEN_CLASS = "protyle-icons--editing-obscured";
+const PROXIMITY = 12;
+
+const getToolbar = (element: Element | null): HTMLElement | null => {
+ const embed = element?.closest(EMBED_SELECTOR);
+ return embed?.querySelector<HTMLElement>(":scope > .protyle-icons") || null;
+};
+
+const isNearToolbar = (rect: DOMRect, toolbar: HTMLElement): boolean => {
+ const toolbarRect = toolbar.getBoundingClientRect();
+ return rect.right >= toolbarRect.left - PROXIMITY && rect.left <= toolbarRect.right + PROXIMITY &&
+ rect.bottom >= toolbarRect.top - PROXIMITY && rect.top <= toolbarRect.bottom + PROXIMITY;
+};
+
+export const bindEmbedToolbarVisibility = (root: HTMLElement): (() => void) => {
+ let caretToolbar: HTMLElement | null = null;
+
+ const onSelectionChange = () => {
+ const previous = caretToolbar;
+ caretToolbar = null;
+ const selection = document.getSelection();
+ if (selection?.rangeCount && selection.focusNode && root.contains(selection.focusNode) &&
+ root.contains(document.activeElement)) {
+ const focusElement = selection.focusNode.nodeType === Node.ELEMENT_NODE ?
+ selection.focusNode as Element : selection.focusNode.parentElement;
+ if (focusElement?.closest(".protyle-wysiwyg__embed")) {
+ const toolbar = getToolbar(focusElement);
+ if (toolbar) {
+ const range = document.createRange();
+ range.setStart(selection.focusNode, selection.focusOffset);
+ range.collapse(true);
+ const rect = range.getClientRects()[0] || range.getBoundingClientRect();
+ if (isNearToolbar(rect, toolbar)) {
+ caretToolbar = toolbar;
+ }
+ }
+ }
+ }
+ if (previous !== caretToolbar) {
+ previous?.classList.remove(HIDDEN_CLASS);
+ caretToolbar?.classList.add(HIDDEN_CLASS);
+ }
+ };
+
+ const onFocusOut = () => queueMicrotask(onSelectionChange);
+
+ document.addEventListener("selectionchange", onSelectionChange);
+ root.addEventListener("focusout", onFocusOut);
+ return () => {
+ document.removeEventListener("selectionchange", onSelectionChange);
+ root.removeEventListener("focusout", onFocusOut);
+ caretToolbar?.classList.remove(HIDDEN_CLASS);
+ };
+};
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index a30c5449b1..2f101635ce 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -1,4 +1,5 @@
import {recordReplacementUndo} from "./replacementInput";
+import {bindEmbedToolbarVisibility} from "./embedToolbarVisibility";
import {bindSpellcheckFocus} from "../util/spellcheckFocus";
import {isTableLikeView} from "../render/av/viewType";
import {visibleTabsSelectionHTML} from "../render/tabsVisibility";
@@ -397,6 +398,7 @@ export class WYSIWYG {
public tableControl: TableControl;
private largeListVirtualizer?: LargeListVirtualizer;
private disposeSpellcheckFocus?: () => void;
+ private disposeEmbedToolbarVisibility?: () => void;
private scheduleInput(callback: () => void | Promise<void>, delay = 0, replace = true) {
if (replace && this.inputTimeout) {
@@ -511,6 +513,7 @@ export class WYSIWYG {
}
this.bindCommonEvent(protyle);
this.bindEvent(protyle);
+ this.disposeEmbedToolbarVisibility = bindEmbedToolbarVisibility(this.element);
/// #if BROWSER
if (!isMobile() && !isPhablet() && navigator.userAgent.includes("Chrome/")) {
this.disposeSpellcheckFocus = bindSpellcheckFocus(this.element, () =>
@@ -539,6 +542,7 @@ export class WYSIWYG {
}
public destroy() {
+ this.disposeEmbedToolbarVisibility?.();
this.disposeSpellcheckFocus?.();
this.largeListVirtualizer?.destroy();
}