An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:sparkles: Support switching tabs while dragging blocks https://github.com/siyuan-note/siyuan/issues/12088
Daniel861c205d90a6
diff --git a/app/src/boot/globalEvent/event.ts b/app/src/boot/globalEvent/event.tsindex ece99fee9a..548a4f5081 100644--- a/app/src/boot/globalEvent/event.ts+++ b/app/src/boot/globalEvent/event.ts@@ -9,6 +9,7 @@ import {hasClosestByAttribute, hasClosestByClassName, isInEmbedBlock} from "../.import {hideTooltip} from "../../dialog/tooltip";import {hideAllElements} from "../../protyle/ui/hideElements";import {dragOverScroll, stopScrollAnimation} from "./dragover";+import {clearTabHoverSwitch} from "../../layout/tabDrag";import {setWebViewFocusable} from "../../mobile/util/mobileAppUtil";import {cancelManualTouch, initTouchDragBridge, isLastPointerMouse} from "../../util/touchDragBridge";import {isWindow} from "../../util/functions";@@ -99,6 +100,9 @@ export const initWindowEvent = (app: App) => {window.addEventListener("dragover", (event: DragEvent & { target: HTMLElement }) => {const isDocumentTab = event.dataTransfer.types.includes(Constants.SIYUAN_DROP_DOCUMENT_TAB);const tabBarElement = hasClosestByClassName(event.target, "layout-tab-bar");+ if (!tabBarElement && event.dataTransfer.types.includes(Constants.SIYUAN_DROP_BLOCK)) {+ clearTabHoverSwitch();+ }if (event.dataTransfer.types.includes(Constants.SIYUAN_DROP_TAB) && (!isDocumentTab || tabBarElement)) {if (!tabBarElement) {stopScrollAnimation();@@ -203,6 +207,7 @@ export const initWindowEvent = (app: App) => {}});window.addEventListener("dragend", () => {+ clearTabHoverSwitch();stopScrollAnimation();hideDragTip();clearDragTipGhost();diff --git a/app/src/constants.ts b/app/src/constants.tsindex 339f23ea0b..8a9d8e8b3c 100644--- a/app/src/constants.ts+++ b/app/src/constants.ts@@ -26,6 +26,7 @@ export abstract class Constants {// drop 事件public static readonly SIYUAN_DROP_FILE: string = "application/siyuan-file";public static readonly SIYUAN_DROP_GUTTER: string = "application/siyuan-gutter";+ public static readonly SIYUAN_DROP_BLOCK: string = "application/siyuan-block";public static readonly SIYUAN_DROP_BLOCK_REF: string = "application/siyuan-block-ref";public static readonly SIYUAN_DROP_TAB: string = "application/siyuan-tab";public static readonly SIYUAN_DROP_DOCUMENT_TAB: string = "application/siyuan-document-tab";@@ -330,6 +331,7 @@ export abstract class Constants {public static readonly TIMEOUT_RESIZE = 200;public static readonly TIMEOUT_INPUT = 256;public static readonly TIMEOUT_LOAD = 300;+ public static readonly TIMEOUT_TAB_SWITCH = 500;public static readonly TIMEOUT_SNIPPET_LOAD = 5000;public static readonly TIMEOUT_LONGPRESS = 460;public static readonly TIMEOUT_VIBRATION_DURATION = 20;diff --git a/app/src/layout/Wnd.ts b/app/src/layout/Wnd.tsindex bab20d05fc..5a840c5599 100644--- a/app/src/layout/Wnd.ts+++ b/app/src/layout/Wnd.ts@@ -53,7 +53,13 @@ import {recordBeforeResizeTop} from "../protyle/util/resize";import {sanitizeClosedTabs, setStorageVal} from "../protyle/util/compatibility";import {setTitle} from "../util/processTitle";import {dragOverScroll} from "../boot/globalEvent/dragover";-import {clearTabDragPreview, findNextTabId, reorderTabItems} from "./tabDrag";+import {+ clearTabDragPreview,+ clearTabHoverSwitch,+ findNextTabId,+ reorderTabItems,+ scheduleTabHoverSwitch+} from "./tabDrag";const createDragTabPlaceholder = () => {const dragTab = window.siyuan.dragTab;@@ -184,7 +190,9 @@ export class Wnd {});const tabHeadersElement = this.headersElement.parentElement;tabHeadersElement.addEventListener("dragleave", (event: DragEvent) => {- if (!event.dataTransfer.types.includes(Constants.SIYUAN_DROP_TAB)) {+ const isBlockDrag = event.dataTransfer.types.includes(Constants.SIYUAN_DROP_BLOCK);+ const isTabDrag = event.dataTransfer.types.includes(Constants.SIYUAN_DROP_TAB);+ if (!isBlockDrag && !isTabDrag) {return;}const relatedTarget = event.relatedTarget;@@ -194,13 +202,34 @@ export class Wnd {const rect = tabHeadersElement.getBoundingClientRect();if (event.clientX < rect.left || event.clientX > rect.right ||event.clientY < rect.top || event.clientY > rect.bottom) {- clearTabDragPreview(tabHeadersElement);+ if (isBlockDrag) {+ clearTabHoverSwitch();+ }+ if (isTabDrag) {+ clearTabDragPreview(tabHeadersElement);+ }}});- tabHeadersElement.addEventListener("dragover", function (event: DragEvent & {+ tabHeadersElement.addEventListener("dragover", (event: DragEvent & {target: HTMLElement- }) {- const it = this as HTMLElement;+ }) => {+ const it = event.currentTarget as HTMLElement;+ if (event.dataTransfer.types.includes(Constants.SIYUAN_DROP_BLOCK)) {+ const tabHeaderElement = hasClosestByAttribute(event.target, "data-type", "tab-header");+ if (!tabHeaderElement || !this.headersElement.contains(tabHeaderElement) ||+ tabHeaderElement.classList.contains("item--focus")) {+ clearTabHoverSwitch();+ return;+ }+ scheduleTabHoverSwitch(tabHeaderElement.dataset.id, () => {+ if (this.headersElement.contains(tabHeaderElement) &&+ !tabHeaderElement.classList.contains("item--focus") && !pdfIsLoading(this.element)) {+ this.switchTab(tabHeaderElement, true);+ }+ }, Constants.TIMEOUT_TAB_SWITCH);+ return;+ }+ clearTabHoverSwitch();const isFileDrag = event.dataTransfer.types.includes(Constants.SIYUAN_DROP_FILE);const isTabDrag = event.dataTransfer.types.includes(Constants.SIYUAN_DROP_TAB);if (!isFileDrag && !isTabDrag) {@@ -275,6 +304,7 @@ export class Wnd {tabHeadersElement.addEventListener("drop", function (event: DragEvent & {target: HTMLElement}) {+ clearTabHoverSwitch();const it = this as HTMLElement;if (event.dataTransfer.types.includes(Constants.SIYUAN_DROP_FILE)) {// 文档树拖拽diff --git a/app/src/layout/tabDrag.test.ts b/app/src/layout/tabDrag.test.tsindex 41f5c370ef..45a537e11d 100644--- a/app/src/layout/tabDrag.test.ts+++ b/app/src/layout/tabDrag.test.ts@@ -1,6 +1,11 @@import {describe, it} from "node:test";import * as assert from "node:assert/strict";-import {findNextTabId, reorderTabItems} from "./tabDrag";+import {+ clearTabHoverSwitch,+ findNextTabId,+ reorderTabItems,+ scheduleTabHoverSwitch+} from "./tabDrag";const createItems = () => [{id: "a"},@@ -44,3 +49,55 @@ describe("tab drag ordering", () => {assert.deepEqual(items.map((item) => item.id), ["a", "b", "c", "d"]);});});++describe("tab hover switching", () => {+ it("keeps the original delay when hovering the same tab", () => {+ let callback: () => void;+ let scheduleCount = 0;+ let switchCount = 0;+ const scheduler = (nextCallback: () => void): (() => void) => {+ callback = nextCallback;+ scheduleCount++;+ return () => undefined;+ };++ scheduleTabHoverSwitch("a", () => switchCount++, 500, scheduler);+ scheduleTabHoverSwitch("a", () => switchCount++, 500, scheduler);+ callback();++ assert.equal(scheduleCount, 1);+ assert.equal(switchCount, 1);+ clearTabHoverSwitch();+ });++ it("switches only the last hovered tab", () => {+ const callbacks: Array<() => void> = [];+ const switchedTabs: string[] = [];+ const scheduler = (callback: () => void): (() => void) => {+ callbacks.push(callback);+ return () => undefined;+ };++ scheduleTabHoverSwitch("a", () => switchedTabs.push("a"), 500, scheduler);+ scheduleTabHoverSwitch("b", () => switchedTabs.push("b"), 500, scheduler);+ callbacks.forEach((callback) => callback());++ assert.deepEqual(switchedTabs, ["b"]);+ clearTabHoverSwitch();+ });++ it("does not switch after clearing the hover", () => {+ let callback: () => void;+ let switchCount = 0;+ const scheduler = (nextCallback: () => void): (() => void) => {+ callback = nextCallback;+ return () => undefined;+ };++ scheduleTabHoverSwitch("a", () => switchCount++, 500, scheduler);+ clearTabHoverSwitch();+ callback();++ assert.equal(switchCount, 0);+ });+});diff --git a/app/src/layout/tabDrag.ts b/app/src/layout/tabDrag.tsindex 3a62da9459..d70a0a601d 100644--- a/app/src/layout/tabDrag.ts+++ b/app/src/layout/tabDrag.ts@@ -36,3 +36,38 @@ export const findNextTabId = <T extends { id: string }>(items: T[], candidateIdsconst existingIds = new Set(items.map((item) => item.id));return candidateIds.find((id) => existingIds.has(id));};++type TTabHoverScheduler = (callback: () => void, delay: number) => () => void;++let tabHoverId: string;+let cancelTabHover: () => void;+let tabHoverGeneration = 0;++const defaultTabHoverScheduler: TTabHoverScheduler = (callback, delay) => {+ const timeoutId = setTimeout(callback, delay);+ return () => clearTimeout(timeoutId);+};++export const clearTabHoverSwitch = () => {+ tabHoverGeneration++;+ cancelTabHover?.();+ cancelTabHover = undefined;+ tabHoverId = undefined;+};++export const scheduleTabHoverSwitch = (tabId: string, callback: () => void, delay: number,+ scheduler = defaultTabHoverScheduler) => {+ if (tabHoverId === tabId) {+ return;+ }+ clearTabHoverSwitch();+ tabHoverId = tabId;+ const generation = tabHoverGeneration;+ cancelTabHover = scheduler(() => {+ if (generation !== tabHoverGeneration || tabHoverId !== tabId) {+ return;+ }+ cancelTabHover = undefined;+ callback();+ }, delay);+};diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.tsindex fe17ae5613..84f6e165f6 100644--- a/app/src/protyle/gutter/index.ts+++ b/app/src/protyle/gutter/index.ts@@ -294,6 +294,7 @@ export class Gutter {});}if (isBlockDrag) {+ event.dataTransfer.setData(Constants.SIYUAN_DROP_BLOCK, Constants.SIYUAN_DROP_BLOCK);const text = getContenteditableElement(selectElements[0] as HTMLElement)?.textContent?.trim() || "";// 数据库块若无标题,优先用当前视图名,最后兜底为"数据库"let title = text;diff --git a/app/src/protyle/util/editorCommonEvent.ts b/app/src/protyle/util/editorCommonEvent.tsindex b7c40bc4db..98be6843d4 100644--- a/app/src/protyle/util/editorCommonEvent.ts+++ b/app/src/protyle/util/editorCommonEvent.ts@@ -1033,6 +1033,7 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {window.siyuan.dragTitle = getContenteditableElement(target.parentElement)?.textContent?.trim() || "";window.siyuan.dragElement = protyle.wysiwyg.element;+ event.dataTransfer.setData(Constants.SIYUAN_DROP_BLOCK, Constants.SIYUAN_DROP_BLOCK);event.dataTransfer.setData(`${Constants.SIYUAN_DROP_GUTTER}NodeListItem${Constants.ZWSP}${target.parentElement.getAttribute("data-subtype")}${Constants.ZWSP}${[target.parentElement.getAttribute("data-node-id")]}`,protyle.wysiwyg.element.innerHTML);return;