An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:art: Improve appearance panel interaction in the selection toolbar https://github.com/siyuan-note/siyuan/issues/14268
Danielb82821b3b3db
diff --git a/app/src/boot/globalEvent/click.ts b/app/src/boot/globalEvent/click.tsindex bb47f28a83..23cfe1990d 100644--- a/app/src/boot/globalEvent/click.ts+++ b/app/src/boot/globalEvent/click.ts@@ -9,6 +9,7 @@ import {showMessage} from "../../dialog/message";import {cancelDrag} from "./dragover";import {nbsp2space, removeZWJ} from "../../protyle/util/normalizeText";import {getDockByType} from "../../layout/tabUtil";+import {SELECTION_TOOLBAR_SUB_ELEMENT_SOURCE} from "../../protyle/toolbar/subElementLifecycle";export const globalClickHideMenu = (element: HTMLElement) => {if (!window.siyuan.menus.menu.element.contains(element) && !hasClosestByAttribute(element, "data-menu", "true")) {@@ -34,10 +35,14 @@ export const globalClick = (event: MouseEvent & { target: HTMLElement }) => {}}- if (!hasTopClosestByClassName(event.target, "protyle-util") &&- !hasTopClosestByClassName(event.target, "protyle-toolbar")) {+ const toolbarElement = hasTopClosestByClassName(event.target, "protyle-toolbar");+ const appearanceTrigger = toolbarElement && hasClosestByAttribute(event.target, "data-type", "text");+ if (!hasTopClosestByClassName(event.target, "protyle-util") && !appearanceTrigger) {document.querySelectorAll(".protyle-font").forEach((item: HTMLElement) => {item.parentElement.classList.add("fn__none");+ if (item.parentElement.dataset.subElementSource === SELECTION_TOOLBAR_SUB_ELEMENT_SOURCE) {+ delete item.parentElement.dataset.subElementSource;+ }});}diff --git a/app/src/boot/globalEvent/event.ts b/app/src/boot/globalEvent/event.tsindex 548a4f5081..5737efc143 100644--- a/app/src/boot/globalEvent/event.ts+++ b/app/src/boot/globalEvent/event.ts@@ -18,6 +18,7 @@ import {fetchPost} from "../../util/fetch";import {initHarmonyTextSelectionMenu} from "../../util/harmonyTextSelectionMenu";import {clearDragTipGhost, hideDragTip} from "../../protyle/util/dragTip";import {formatPainter} from "../../protyle/toolbar/FormatPainter";+import {SELECTION_TOOLBAR_SUB_ELEMENT_SOURCE} from "../../protyle/toolbar/subElementLifecycle";const KANBAN_GROUP_DRAG_TYPE = `${Constants.SIYUAN_DROP_GUTTER}NodeAttributeView${Constants.ZWSP}Group${Constants.ZWSP}`.toLowerCase();@@ -241,7 +242,8 @@ export const initWindowEvent = (app: App) => {event.preventDefault();}// protyle.toolbar 点击空白处时进行隐藏- if (!hasClosestByClassName(event.target as Element, "protyle-toolbar")) {+ if (!hasClosestByClassName(event.target as Element, "protyle-toolbar") &&+ !hasClosestByAttribute(event.target, "data-sub-element-source", SELECTION_TOOLBAR_SUB_ELEMENT_SOURCE)) {hideAllElements(["toolbar"]);}});diff --git a/app/src/protyle/toolbar/Font.ts b/app/src/protyle/toolbar/Font.tsindex 6074dbbe95..7eb2a0e9fb 100644--- a/app/src/protyle/toolbar/Font.ts+++ b/app/src/protyle/toolbar/Font.ts@@ -5,7 +5,11 @@ import {Constants} from "../../constants";import {hasClosestBlock, hasClosestByAttribute} from "../util/hasClosest";import {updateBatchTransaction} from "../wysiwyg/transaction";import {lineNumberRender} from "../render/highlightRender";-import {closeSubElement} from "./subElementLifecycle";+import {+ closeSubElement,+ SELECTION_TOOLBAR_SUB_ELEMENT_SOURCE,+ setSubElementSource,+} from "./subElementLifecycle";import {escapeAttr} from "../../util/escape";import {decodeStyle1,@@ -61,15 +65,31 @@ export class Font extends ToolbarItem {constructor(protyle: IProtyle, menuItem: IMenuItem) {super(protyle, menuItem);this.element.addEventListener("click", () => {+ if (protyle.toolbar.subElement.dataset.subElementSource === SELECTION_TOOLBAR_SUB_ELEMENT_SOURCE &&+ !protyle.toolbar.subElement.classList.contains("fn__none")) {+ protyle.toolbar.subElement.classList.add("fn__none");+ closeSubElement(protyle.toolbar);+ focusByRange(protyle.toolbar.range);+ return;+ }+ closeSubElement(protyle.toolbar);+ /// #if !MOBILE+ if (protyle.toolbar.element.classList.contains("fn__none")) {+ protyle.toolbar.render(protyle, protyle.toolbar.range);+ }+ /// #else+ protyle.toolbar.element.classList.add("fn__none");+ /// #endifconst triggerRect = this.element.getBoundingClientRect();const visibleTriggerRect = triggerRect.width > 0 && triggerRect.height > 0 ? triggerRect : undefined;- protyle.toolbar.element.classList.add("fn__none");- closeSubElement(protyle.toolbar);protyle.toolbar.subElement.innerHTML = "";protyle.toolbar.subElement.style.width = "";protyle.toolbar.subElement.style.padding = "";const appearanceElement = appearanceMenu(protyle, getFontNodeElements(protyle));protyle.toolbar.subElement.append(appearanceElement);+ /// #if !MOBILE+ setSubElementSource(protyle.toolbar, SELECTION_TOOLBAR_SUB_ELEMENT_SOURCE);+ /// #endifprotyle.toolbar.subElement.style.zIndex = (++window.siyuan.zIndex).toString();protyle.toolbar.subElement.classList.remove("fn__none");limitRecentFontStyleRows(appearanceElement);@@ -230,6 +250,15 @@ export const appearanceMenu = (protyle: IProtyle, nodeElements?: Element[],const applyFontStyle = (type: string, color?: string) => {fontEvent(protyle, nodeElements, type, color, true, onChange);};+ const closeSelectionToolbarAppearance = () => {+ if (protyle.toolbar.subElement.dataset.subElementSource !== SELECTION_TOOLBAR_SUB_ELEMENT_SOURCE) {+ return;+ }+ protyle.toolbar.subElement.classList.add("fn__none");+ closeSubElement(protyle.toolbar);+ protyle.toolbar.render(protyle, protyle.toolbar.range);+ focusByRange(protyle.toolbar.range);+ };element.innerHTML = `${lastColorHTML}<div class="fn__hr"></div><div data-id="color">${window.siyuan.languages.color}</div>@@ -296,17 +325,23 @@ export const appearanceMenu = (protyle: IProtyle, nodeElements?: Element[],if (target.dataset.action === "manageInlineStyle") {closeSubElement(protyle.toolbar);protyle.toolbar.subElement.classList.add("fn__none");+ protyle.toolbar.element.classList.add("fn__none");openInlineStyleDialog(target.dataset.inlineStyleType as TInlineStyleType);} else if (dataType === "style1") {applyFontStyle(dataType, encodeStyle1(target.style.backgroundColor, target.style.color));+ closeSelectionToolbarAppearance();} else if (dataType === "fontSize") {applyFontStyle(dataType, target.getAttribute("data-value"));+ closeSelectionToolbarAppearance();} else if (dataType === "backgroundColor") {applyFontStyle(dataType, target.style.backgroundColor);+ closeSelectionToolbarAppearance();} else if (dataType === "color") {applyFontStyle(dataType, target.style.color);+ closeSelectionToolbarAppearance();} else {applyFontStyle(dataType);+ closeSelectionToolbarAppearance();}break;}diff --git a/app/src/protyle/toolbar/index.ts b/app/src/protyle/toolbar/index.tsindex 2ddf303c1e..21bc0f652c 100644--- a/app/src/protyle/toolbar/index.ts+++ b/app/src/protyle/toolbar/index.ts@@ -59,11 +59,12 @@ import {activeBlur, updateMobilePluginToolbar} from "../../mobile/util/keyboardTimport {FormatPainter} from "./FormatPainter";import {IFormatPainterSnapshot} from "./formatPainterCore";import {clearDisallowedTextInputHotkey} from "../../util/hotKeyPolicy";-import {closeSubElement} from "./subElementLifecycle";+import {closeSubElement, SELECTION_TOOLBAR_SUB_ELEMENT_SOURCE} from "./subElementLifecycle";import {getDefaultToolbar, getToolbarEntryId, markPluginToolbarEntries} from "./defaults";import {applyToolbarEntryVisibility} from "../../config/entryVisibility/runtime";import {refreshToolbarCatalog} from "../../config/entryVisibility/catalog";import {emitToPlugins, forEachPluginSubscriber, hasPluginSubscriber} from "../../plugin/EventBusCore";+import {getSelectionElementAvailableHeight, getSelectionElementY} from "./selectionElementPosition";const filterPluginToolbar = (toolbar: Array<string | IMenuItem>, lite: boolean) => {if (!lite) {@@ -186,24 +187,28 @@ export class Toolbar {(this.rangePosition.isBottom ? rangeRects[rangeRects.length - 1] : rangeRects[0]) ||this.range.getBoundingClientRect();const gap = this.isMultipleClick ? 2 : 4;+ const toolbarRect = element.dataset.subElementSource === SELECTION_TOOLBAR_SUB_ELEMENT_SOURCE &&+ !this.element.classList.contains("fn__none") ? this.element.getBoundingClientRect() : undefined;+ const positionOptions = {+ elementHeight: element.offsetHeight,+ rangeTop: rangeRect.top,+ rangeBottom: rangeRect.bottom,+ topBoundary,+ bottomBoundary,+ gap,+ isBottom: this.rangePosition.isBottom,+ toolbarTop: toolbarRect?.top,+ toolbarBottom: toolbarRect?.bottom,+ };if (scrollElement) {- const availableHeight = Math.max(- rangeRect.top - topBoundary - gap,- bottomBoundary - rangeRect.bottom - gap,- 0- );+ const availableHeight = getSelectionElementAvailableHeight(positionOptions);if (element.offsetHeight > availableHeight) {const outerHeight = element.offsetHeight - scrollElement.offsetHeight;scrollElement.style.maxHeight = `${Math.max(0, availableHeight - outerHeight)}px`;+ positionOptions.elementHeight = element.offsetHeight;}}- const above = rangeRect.top - element.offsetHeight - gap;- const below = rangeRect.bottom + gap;- const y = this.rangePosition.isBottom ?- (below + element.offsetHeight <= bottomBoundary ?- below : Math.max(above, topBoundary)) :- (above >= topBoundary ?- above : Math.min(below, bottomBoundary - element.offsetHeight));+ const y = getSelectionElementY(positionOptions);const horizontalDivisor = this.isMultipleClick ? 3 : 4;// 子面板与触发按钮水平居中,垂直方向继续避让选区const left = triggerRect ?diff --git a/app/src/protyle/toolbar/selectionElementPosition.test.ts b/app/src/protyle/toolbar/selectionElementPosition.test.tsnew file mode 100644index 0000000000..b0d97d58d1--- /dev/null+++ b/app/src/protyle/toolbar/selectionElementPosition.test.ts@@ -0,0 +1,37 @@+import {describe, it} from "node:test";+import * as assert from "node:assert/strict";+import {getSelectionElementAvailableHeight, getSelectionElementY} from "./selectionElementPosition";++const options = {+ elementHeight: 120,+ rangeTop: 300,+ rangeBottom: 320,+ topBoundary: 20,+ bottomBoundary: 700,+ gap: 4,+ isBottom: false,+};++describe("selection element position", () => {+ it("places a child panel outside a toolbar above the selection", () => {+ assert.equal(getSelectionElementY({...options, toolbarTop: 260, toolbarBottom: 290}), 136);+ });++ it("places a child panel outside a toolbar below the selection", () => {+ assert.equal(getSelectionElementY({...options, toolbarTop: 330, toolbarBottom: 360}), 364);+ });++ it("falls back to the opposite side when the toolbar side has insufficient space", () => {+ assert.equal(getSelectionElementY({+ ...options,+ rangeTop: 130,+ rangeBottom: 150,+ toolbarTop: 90,+ toolbarBottom: 120,+ }), 154);+ });++ it("reserves the toolbar space when limiting a child panel", () => {+ assert.equal(getSelectionElementAvailableHeight({...options, toolbarTop: 260, toolbarBottom: 290}), 376);+ });+});diff --git a/app/src/protyle/toolbar/selectionElementPosition.ts b/app/src/protyle/toolbar/selectionElementPosition.tsnew file mode 100644index 0000000000..96d58440f2--- /dev/null+++ b/app/src/protyle/toolbar/selectionElementPosition.ts@@ -0,0 +1,45 @@+interface ISelectionElementPositionOptions {+ elementHeight: number;+ rangeTop: number;+ rangeBottom: number;+ topBoundary: number;+ bottomBoundary: number;+ gap: number;+ isBottom: boolean;+ toolbarTop?: number;+ toolbarBottom?: number;+}++export const getSelectionElementAvailableHeight = (options: ISelectionElementPositionOptions) => {+ const toolbarAbove = typeof options.toolbarBottom === "number" && options.toolbarBottom <= options.rangeTop;+ const toolbarBelow = typeof options.toolbarTop === "number" && options.toolbarTop >= options.rangeBottom;+ const top = toolbarAbove ? options.toolbarTop : options.rangeTop;+ const bottom = toolbarBelow ? options.toolbarBottom : options.rangeBottom;+ return Math.max(+ top - options.topBoundary - options.gap,+ options.bottomBoundary - bottom - options.gap,+ 0+ );+};++export const getSelectionElementY = (options: ISelectionElementPositionOptions) => {+ const aboveRange = options.rangeTop - options.elementHeight - options.gap;+ const belowRange = options.rangeBottom + options.gap;+ if (typeof options.toolbarTop === "number" && typeof options.toolbarBottom === "number") {+ if (options.toolbarBottom <= options.rangeTop) {+ const aboveToolbar = options.toolbarTop - options.elementHeight - options.gap;+ return aboveToolbar >= options.topBoundary ?+ aboveToolbar : Math.min(belowRange, options.bottomBoundary - options.elementHeight);+ }+ if (options.toolbarTop >= options.rangeBottom) {+ const belowToolbar = options.toolbarBottom + options.gap;+ return belowToolbar + options.elementHeight <= options.bottomBoundary ?+ belowToolbar : Math.max(aboveRange, options.topBoundary);+ }+ }+ return options.isBottom ?+ (belowRange + options.elementHeight <= options.bottomBoundary ?+ belowRange : Math.max(aboveRange, options.topBoundary)) :+ (aboveRange >= options.topBoundary ?+ aboveRange : Math.min(belowRange, options.bottomBoundary - options.elementHeight));+};diff --git a/app/src/protyle/toolbar/subElementLifecycle.test.ts b/app/src/protyle/toolbar/subElementLifecycle.test.tsindex cec1725a86..25cc158851 100644--- a/app/src/protyle/toolbar/subElementLifecycle.test.ts+++ b/app/src/protyle/toolbar/subElementLifecycle.test.ts@@ -1,6 +1,11 @@import {describe, it} from "node:test";import * as assert from "node:assert/strict";-import {closeSubElement, ISubElementLifecycleState} from "./subElementLifecycle";+import {+ closeSubElement,+ ISubElementLifecycleState,+ SELECTION_TOOLBAR_SUB_ELEMENT_SOURCE,+ setSubElementSource,+} from "./subElementLifecycle";describe("closeSubElement", () => {it("runs a pending close callback exactly once", () => {@@ -60,4 +65,34 @@ describe("closeSubElement", () => {assert.equal(state.subElementCloseCB, undefined);assert.equal(state.subElementResizeCB, undefined);});++ it("clears the sub-element source before invoking its close handler", () => {+ const subElement = {dataset: {}} as HTMLElement;+ const state: ISubElementLifecycleState = {+ subElement,+ subElementCloseCB: () => {+ assert.equal(subElement.dataset.subElementSource, undefined);+ },+ };+ setSubElementSource(state, SELECTION_TOOLBAR_SUB_ELEMENT_SOURCE);++ closeSubElement(state);++ assert.equal(subElement.dataset.subElementSource, undefined);+ });++ it("preserves a replacement sub-element source installed while closing", () => {+ const subElement = {dataset: {}} as HTMLElement;+ const state: ISubElementLifecycleState = {+ subElement,+ subElementCloseCB: () => {+ setSubElementSource(state, "replacement");+ },+ };+ setSubElementSource(state, SELECTION_TOOLBAR_SUB_ELEMENT_SOURCE);++ closeSubElement(state);++ assert.equal(subElement.dataset.subElementSource, "replacement");+ });});diff --git a/app/src/protyle/toolbar/subElementLifecycle.ts b/app/src/protyle/toolbar/subElementLifecycle.tsindex 123ff53df6..ce604e9280 100644--- a/app/src/protyle/toolbar/subElementLifecycle.ts+++ b/app/src/protyle/toolbar/subElementLifecycle.ts@@ -1,11 +1,26 @@export interface ISubElementLifecycleState {+ subElement?: HTMLElement;subElementCloseCB?: (() => void) | null;subElementResizeCB?: (() => void) | null;}+export const SELECTION_TOOLBAR_SUB_ELEMENT_SOURCE = "selection-toolbar";++export const setSubElementSource = (state: ISubElementLifecycleState, source?: string) => {+ if (!state.subElement) {+ return;+ }+ if (source) {+ state.subElement.dataset.subElementSource = source;+ } else {+ delete state.subElement.dataset.subElementSource;+ }+};+export const closeSubElement = (state: ISubElementLifecycleState) => {const closeCallback = state.subElementCloseCB;state.subElementCloseCB = undefined;state.subElementResizeCB = undefined;+ setSubElementSource(state);closeCallback?.();};