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 cross-window tab dragging https://github.com/siyuan-note/siyuan/issues/14736
Daniel995410f2b6dc
diff --git a/app/src/assets/scss/business/_layout.scss b/app/src/assets/scss/business/_layout.scssindex 40255351db..024f6ab402 100644--- a/app/src/assets/scss/business/_layout.scss+++ b/app/src/assets/scss/business/_layout.scss@@ -234,6 +234,11 @@margin: 6px 4px;border-radius: var(--b3-border-radius);+ &[data-clone="true"] {+ opacity: .38;+ pointer-events: none;+ }+&--full {max-width: none;flex: 1;diff --git a/app/src/assets/scss/util/_reset.scss b/app/src/assets/scss/util/_reset.scssindex 896fdf5476..b42f1ac9b6 100644--- a/app/src/assets/scss/util/_reset.scss+++ b/app/src/assets/scss/util/_reset.scss@@ -98,10 +98,6 @@ body {}}- &.body--blur .layout-tab-bar {- opacity: .38;- }-.layout__center {border-radius: 0;}diff --git a/app/src/constants.ts b/app/src/constants.tsindex ac81a83d3e..66947f61bf 100644--- a/app/src/constants.ts+++ b/app/src/constants.ts@@ -42,7 +42,7 @@ export abstract class Constants {public static readonly SIYUAN_HOTKEY: string = "siyuan-hotkey";public static readonly SIYUAN_INIT: string = "siyuan-init";public static readonly SIYUAN_READY_TO_SHOW: string = "siyuan-ready-to-show";- public static readonly SIYUAN_SEND_WINDOWS: string = "siyuan-send-windows"; // 主窗口和各新窗口之间的通信,{cmd: "closetab"|"lockscreen"|"lockscreenByMode", data: {}})+ public static readonly SIYUAN_SEND_WINDOWS: string = "siyuan-send-windows"; // 主窗口和各新窗口之间的通信public static readonly SIYUAN_SAVE_CLOSE: string = "siyuan-save-close";public static readonly SIYUAN_AUTO_LAUNCH: string = "siyuan-auto-launch";diff --git a/app/src/layout/Tab.ts b/app/src/layout/Tab.tsindex 550ae23ac4..23c95dffdb 100644--- a/app/src/layout/Tab.ts+++ b/app/src/layout/Tab.ts@@ -12,6 +12,48 @@ import {ipcRenderer} from "electron";/// #endifimport {layoutToJSON, saveLayout} from "./util";import {setTitle} from "../util/processTitle";+import {clearTabDragPreview} from "./tabDrag";++type TTabHeaderOptions = Pick<ITab, "title" | "icon" | "docIcon"> & {+ id?: string,+ draggable?: boolean,+ focus?: boolean,+ pin?: boolean,+ unupdate?: boolean,+};++export const createTabHeaderElement = (options: TTabHeaderOptions) => {+ const element = document.createElement("li");+ element.setAttribute("data-type", "tab-header");+ if (options.draggable) {+ element.setAttribute("draggable", "true");+ }+ if (options.id) {+ element.setAttribute("data-id", options.id);+ }+ element.classList.add("item");+ if (options.focus) {+ element.classList.add("item--focus");+ }+ if (options.pin) {+ element.classList.add("item--pin");+ }+ if (options.unupdate) {+ element.classList.add("item--unupdate");+ }+ let iconHTML = "";+ if (options.icon) {+ iconHTML = `<svg class="item__graphic"><use xlink:href="#${escapeAttr(escapeHtml(options.icon))}"></use></svg>`;+ } else if (options.docIcon) {+ iconHTML = `<span class="item__icon">${unicode2Emoji(options.docIcon)}</span>`;+ }+ element.innerHTML = `${iconHTML}<span class="item__text">${escapeHtml(options.title || "")}</span>+<span class="item__close"><svg><use xlink:href="#iconClose"></use></svg></span>`;+ if (options.pin && (options.icon || options.docIcon)) {+ element.querySelector(".item__text").classList.add("fn__none");+ }+ return element;+};export class Tab {public parent: Wnd;@@ -31,19 +73,14 @@ export class Tab {this.title = options.title;this.icon = options.icon;this.docIcon = options.docIcon;- this.headElement = document.createElement("li");- this.headElement.setAttribute("data-type", "tab-header");- this.headElement.setAttribute("draggable", "true");- this.headElement.setAttribute("data-id", this.id);- this.headElement.classList.add("item", "item--focus");- let iconHTML = "";- if (options.icon) {- iconHTML = `<svg class="item__graphic"><use xlink:href="#${escapeAttr(escapeHtml(options.icon))}"></use></svg>`;- } else if (options.docIcon) {- iconHTML = `<span class="item__icon">${unicode2Emoji(options.docIcon)}</span>`;- }- this.headElement.innerHTML = `${iconHTML}<span class="item__text">${escapeHtml(options.title)}</span>-<span class="item__close"><svg><use xlink:href="#iconClose"></use></svg></span>`;+ this.headElement = createTabHeaderElement({+ id: this.id,+ title: options.title,+ icon: options.icon,+ docIcon: options.docIcon,+ draggable: true,+ focus: true,+ });this.headElement.addEventListener("dragstart", (event: DragEvent & { target: HTMLElement }) => {window.getSelection().removeAllRanges();hideTooltip();@@ -67,6 +104,18 @@ export class Tab {event.dataTransfer.dropEffect = "move";tabElement.style.opacity = "0.38";window.siyuan.dragElement = this.headElement;+ const dragTabData: ITabDragData = {+ title: this.title,+ icon: this.icon,+ docIcon: this.docIcon,+ pin: tabElement.classList.contains("item--pin"),+ focus: tabElement.classList.contains("item--focus"),+ unupdate: tabElement.classList.contains("item--unupdate"),+ };+ window.siyuan.dragTab = dragTabData;+ /// #if !BROWSER+ ipcRenderer.send(Constants.SIYUAN_SEND_WINDOWS, {cmd: "setTabDragData", data: dragTabData});+ /// #endif}/// #if !BROWSERipcRenderer.send(Constants.SIYUAN_SEND_WINDOWS, {cmd: "resetTabsStyle", data: "removeRegionStyle"});@@ -87,14 +136,9 @@ export class Tab {}}, Constants.TIMEOUT_LOAD); // 等待主进程发送关闭消息ipcRenderer.send(Constants.SIYUAN_SEND_WINDOWS, {cmd: "resetTabsStyle", data: "rmDragStyle"});- /// #else- document.querySelectorAll(".layout-tab-bars--drag").forEach(item => {- item.classList.remove("layout-tab-bars--drag");- });- document.querySelectorAll(".layout-tab-bar li[data-clone='true']").forEach(tabItem => {- tabItem.remove();- });/// #endif+ clearTabDragPreview();+ window.siyuan.dragTab = undefined;window.siyuan.dragElement = undefined;if (event.dataTransfer.dropEffect === "none") {// 按 esc 取消的时候应该还原在 dragover 时交换的 tabdiff --git a/app/src/layout/Wnd.ts b/app/src/layout/Wnd.tsindex b55f31b293..668f2a94c0 100644--- a/app/src/layout/Wnd.ts+++ b/app/src/layout/Wnd.ts@@ -11,7 +11,7 @@ import {saveLayout,setPanelFocus,} from "./util";-import {Tab} from "./Tab";+import {createTabHeaderElement, Tab} from "./Tab";import {Model} from "./Model";import {Editor} from "../editor";import {Graph} from "./dock/Graph";@@ -54,6 +54,24 @@ 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, reorderTabItems} from "./tabDrag";++const createDragTabPlaceholder = () => {+ const dragTab = window.siyuan.dragTab;+ const element = createTabHeaderElement({+ title: dragTab?.title,+ icon: dragTab?.icon || (dragTab ? undefined : "iconFile"),+ docIcon: dragTab?.docIcon,+ focus: dragTab?.focus,+ pin: dragTab?.pin,+ unupdate: dragTab?.unupdate,+ });+ element.setAttribute("data-clone", "true");+ if (!dragTab) {+ element.setAttribute("data-drag-fallback", "true");+ }+ return element;+};export class Wnd {private app: App;@@ -163,56 +181,76 @@ export class Wnd {target = target.parentElement;}});- this.headersElement.parentElement.addEventListener("dragover", function (event: DragEvent & {+ const tabHeadersElement = this.headersElement.parentElement;+ tabHeadersElement.addEventListener("dragleave", (event: DragEvent) => {+ if (!event.dataTransfer.types.includes(Constants.SIYUAN_DROP_TAB)) {+ return;+ }+ const relatedTarget = event.relatedTarget;+ if (relatedTarget instanceof Node && tabHeadersElement.contains(relatedTarget)) {+ return;+ }+ const rect = tabHeadersElement.getBoundingClientRect();+ if (event.clientX < rect.left || event.clientX > rect.right ||+ event.clientY < rect.top || event.clientY > rect.bottom) {+ clearTabDragPreview(tabHeadersElement);+ }+ });+ tabHeadersElement.addEventListener("dragover", function (event: DragEvent & {target: HTMLElement}) {const it = this as HTMLElement;- if (!window.siyuan.currentDragOverTabHeadersElement) {- window.siyuan.currentDragOverTabHeadersElement = it;- } else {- if (window.siyuan.currentDragOverTabHeadersElement !== it) {- window.siyuan.currentDragOverTabHeadersElement.classList.remove("layout-tab-bars--drag");- window.siyuan.currentDragOverTabHeadersElement.querySelectorAll(".layout-tab-bar li[data-clone='true']").forEach(item => {- item.remove();- });- window.siyuan.currentDragOverTabHeadersElement = it;+ const isFileDrag = event.dataTransfer.types.includes(Constants.SIYUAN_DROP_FILE);+ const isTabDrag = event.dataTransfer.types.includes(Constants.SIYUAN_DROP_TAB);+ if (!isFileDrag && !isTabDrag) {+ return;+ }+ if (window.siyuan.currentDragOverTabHeadersElement !== it) {+ if (window.siyuan.currentDragOverTabHeadersElement) {+ clearTabDragPreview(window.siyuan.currentDragOverTabHeadersElement);}+ window.siyuan.currentDragOverTabHeadersElement = it;}- if (event.dataTransfer.types.includes(Constants.SIYUAN_DROP_FILE)) {+ if (isFileDrag) {event.preventDefault();it.classList.add("layout-tab-bars--drag");return;}- // 不能使用 !window.siyuan.dragElement,因为移动页签到新窗口后,再把主窗口页签拖拽新窗口页签上时,该值为空- if (!event.dataTransfer.types.includes(Constants.SIYUAN_DROP_TAB)) {- return;- }event.preventDefault();const tabBarElement = it.firstElementChild as HTMLElement;dragOverScroll(event, tabBarElement.getBoundingClientRect(), tabBarElement, "x");- let oldTabHeaderElement = window.siyuan.dragElement;- let exitDrag = false;- Array.from(it.firstElementChild.childNodes).find((item: HTMLElement) => {- if (item.style?.opacity === "0.38") {- oldTabHeaderElement = item;- exitDrag = true;- return true;- }- });- if (!exitDrag && oldTabHeaderElement) {- if (oldTabHeaderElement.classList.contains("item--pin")) {+ let oldTabHeaderElement = tabBarElement.querySelector("li[data-clone='true']") as HTMLElement;+ if (oldTabHeaderElement?.hasAttribute("data-drag-fallback") && window.siyuan.dragTab) {+ const placeholderElement = createDragTabPlaceholder();+ oldTabHeaderElement.replaceWith(placeholderElement);+ oldTabHeaderElement = placeholderElement;+ }+ if (!oldTabHeaderElement && window.siyuan.dragElement && tabBarElement.contains(window.siyuan.dragElement)) {+ oldTabHeaderElement = window.siyuan.dragElement;+ } else if (!oldTabHeaderElement && window.siyuan.dragElement) {+ if (window.siyuan.dragElement.classList.contains("item--pin")) {return;}- oldTabHeaderElement = oldTabHeaderElement.cloneNode(true) as HTMLElement;+ oldTabHeaderElement = window.siyuan.dragElement.cloneNode(true) as HTMLElement;oldTabHeaderElement.setAttribute("data-clone", "true");- it.firstElementChild.append(oldTabHeaderElement);- return;- } else if (!exitDrag && !oldTabHeaderElement) { // 拖拽到新窗口- oldTabHeaderElement = document.createElement("li");- oldTabHeaderElement.style.opacity = "0.38";- oldTabHeaderElement.innerHTML = '<svg class="svg"><use xlink:href="#iconFile"></use></svg>';- oldTabHeaderElement.setAttribute("data-clone", "true");- it.firstElementChild.append(oldTabHeaderElement);+ oldTabHeaderElement.removeAttribute("data-id");+ oldTabHeaderElement.removeAttribute("draggable");+ oldTabHeaderElement.style.removeProperty("opacity");+ delete oldTabHeaderElement.dataset.dragDocumentId;+ tabBarElement.append(oldTabHeaderElement);+ } else if (!oldTabHeaderElement) {+ oldTabHeaderElement = createDragTabPlaceholder();+ if (oldTabHeaderElement.classList.contains("item--pin")) {+ const firstUnpinnedElement = Array.from(tabBarElement.children).find((item: HTMLElement) =>+ !item.classList.contains("item--pin"));+ if (firstUnpinnedElement) {+ firstUnpinnedElement.before(oldTabHeaderElement);+ } else {+ tabBarElement.append(oldTabHeaderElement);+ }+ } else {+ tabBarElement.append(oldTabHeaderElement);+ }}const newTabHeaderElement = hasClosestByAttribute(event.target, "data-type", "tab-header");if (!newTabHeaderElement) {@@ -233,12 +271,9 @@ export class Wnd {}}});- this.headersElement.parentElement.addEventListener("drop", function (event: DragEvent & {+ tabHeadersElement.addEventListener("drop", function (event: DragEvent & {target: HTMLElement}) {- document.querySelectorAll(".layout-tab-bars--drag").forEach(item => {- item.classList.remove("layout-tab-bars--drag");- });const it = this as HTMLElement;if (event.dataTransfer.types.includes(Constants.SIYUAN_DROP_FILE)) {// 文档树拖拽@@ -253,67 +288,72 @@ export class Wnd {}});window.siyuan.dragElement = undefined;+ clearTabDragPreview(it);+ return;+ }+ if (!event.dataTransfer.types.includes(Constants.SIYUAN_DROP_TAB)) {+ return;+ }+ event.preventDefault();+ const tabBarElement = it.firstElementChild as HTMLElement;+ const cloneTabElement = tabBarElement.querySelector("li[data-clone='true']") as HTMLElement;+ const dragTabHeaderElement = cloneTabElement ||+ (window.siyuan.dragElement && tabBarElement.contains(window.siyuan.dragElement) ? window.siyuan.dragElement : undefined);+ if (!dragTabHeaderElement) {+ clearTabDragPreview(it);return;}+ const nextTabHeaderElement = dragTabHeaderElement.nextElementSibling as HTMLElement;+ const nextTabId = nextTabHeaderElement?.getAttribute("data-id") || undefined;const tabData = JSON.parse(event.dataTransfer.getData(Constants.SIYUAN_DROP_TAB));let oldTab = getInstanceById(tabData.id) as Tab;const wnd = getInstanceById(it.parentElement.getAttribute("data-id")) as Wnd;+ if (!(wnd instanceof Wnd)) {+ clearTabDragPreview(it);+ return;+ }+ let isCrossWindow = false;/// #if !BROWSERif (!oldTab) { // 从主窗口拖拽到页签新窗口- if (wnd instanceof Wnd) {- JSONToCenter(app, tabData, wnd);- oldTab = wnd.children[wnd.children.length - 1];- ipcRenderer.send(Constants.SIYUAN_SEND_WINDOWS, {cmd: "closetab", data: tabData.id});- it.querySelector("li[data-clone='true']").remove();- wnd.switchTab(oldTab.headElement);- ipcRenderer.send(Constants.SIYUAN_CMD, "focus");- }+ const existingTabs = new Set(wnd.children);+ JSONToCenter(app, tabData, wnd);+ oldTab = wnd.children.find((item) => !existingTabs.has(item));+ isCrossWindow = true;}/// #endifif (!oldTab) {+ clearTabDragPreview(it);return;}-- let nextTabHeaderElement: HTMLElement;- Array.from(it.firstElementChild.childNodes).find((item: HTMLElement) => {- if (item.style?.opacity === "0.38") {- nextTabHeaderElement = item.nextElementSibling as HTMLElement;- return true;- }- });-- if (!it.contains(oldTab.headElement)) {- // 从其他 Wnd 拖动过来- const cloneTabElement = it.querySelector("[data-clone='true']");- if (!cloneTabElement) {- return;- }+ const oldWnd = oldTab.parent;+ if (cloneTabElement) {cloneTabElement.before(oldTab.headElement);cloneTabElement.remove();- // 对象顺序- wnd.moveTab(oldTab, nextTabHeaderElement ? nextTabHeaderElement.getAttribute("data-id") : undefined);- resizeTabs();- return;}-- let tempTab: Tab;- oldTab.parent.children.find((item, index) => {- if (item.id === oldTab.id) {- tempTab = oldTab.parent.children.splice(index, 1)[0];- return true;- }- });- if (nextTabHeaderElement) {- oldTab.parent.children.find((item, index) => {- if (item.id === nextTabHeaderElement.getAttribute("data-id")) {- oldTab.parent.children.splice(index, 0, tempTab);- return true;- }- });+ if (oldWnd !== wnd) {+ // 从其他 Wnd 拖动过来+ wnd.moveTab(oldTab, nextTabId);+ resizeTabs();} else {- oldTab.parent.children.push(tempTab);+ if (!reorderTabItems(wnd.children, oldTab, nextTabId)) {+ clearTabDragPreview(it);+ return;+ }+ if (isCrossWindow) {+ wnd.switchTab(oldTab.headElement);+ } else {+ saveLayout();+ }}- saveLayout();+ clearTabDragPreview(it);+ /// #if !BROWSER+ if (isCrossWindow) {+ ipcRenderer.send(Constants.SIYUAN_SEND_WINDOWS, {cmd: "resetTabsStyle", data: "rmDragStyle"});+ ipcRenderer.send(Constants.SIYUAN_SEND_WINDOWS, {cmd: "resetTabsStyle", data: "addRegionStyle"});+ ipcRenderer.send(Constants.SIYUAN_SEND_WINDOWS, {cmd: "closetab", data: tabData.id});+ ipcRenderer.send(Constants.SIYUAN_CMD, "focus");+ }+ /// #endif});let elementDragCounter = 0;this.element.addEventListener("dragenter", (event: DragEvent & { target: HTMLElement }) => {diff --git a/app/src/layout/tabDrag.test.ts b/app/src/layout/tabDrag.test.tsnew file mode 100644index 0000000000..f5e18c6143--- /dev/null+++ b/app/src/layout/tabDrag.test.ts@@ -0,0 +1,33 @@+import {describe, it} from "node:test";+import * as assert from "node:assert/strict";+import {reorderTabItems} from "./tabDrag";++const createItems = () => [+ {id: "a"},+ {id: "b"},+ {id: "c"},+ {id: "d"},+];++describe("tab drag ordering", () => {+ it("moves a tab before the requested successor", () => {+ const items = createItems();++ assert.equal(reorderTabItems(items, items[3], "b"), true);+ assert.deepEqual(items.map((item) => item.id), ["a", "d", "b", "c"]);+ });++ it("moves a tab to the end when no successor exists", () => {+ const items = createItems();++ assert.equal(reorderTabItems(items, items[0]), true);+ assert.deepEqual(items.map((item) => item.id), ["b", "c", "d", "a"]);+ });++ it("does not change the list when the dragged tab is missing", () => {+ const items = createItems();++ assert.equal(reorderTabItems(items, {id: "missing"}, "b"), false);+ assert.deepEqual(items.map((item) => item.id), ["a", "b", "c", "d"]);+ });+});diff --git a/app/src/layout/tabDrag.ts b/app/src/layout/tabDrag.tsnew file mode 100644index 0000000000..68ff28dff4--- /dev/null+++ b/app/src/layout/tabDrag.ts@@ -0,0 +1,33 @@+export const clearTabDragPreview = (tabHeadersElement?: HTMLElement) => {+ if (tabHeadersElement) {+ tabHeadersElement.classList.remove("layout-tab-bars--drag");+ tabHeadersElement.querySelectorAll(".layout-tab-bar li[data-clone='true']").forEach((item) => {+ item.remove();+ });+ } else {+ document.querySelectorAll(".layout-tab-bars--drag").forEach((item) => {+ item.classList.remove("layout-tab-bars--drag");+ });+ document.querySelectorAll(".layout-tab-bar li[data-clone='true']").forEach((item) => {+ item.remove();+ });+ }+ if (!tabHeadersElement || window.siyuan.currentDragOverTabHeadersElement === tabHeadersElement) {+ window.siyuan.currentDragOverTabHeadersElement = undefined;+ }+};++export const reorderTabItems = <T extends { id: string }>(items: T[], item: T, nextId?: string) => {+ const currentIndex = items.indexOf(item);+ if (currentIndex === -1) {+ return false;+ }+ items.splice(currentIndex, 1);+ const nextIndex = nextId ? items.findIndex((currentItem) => currentItem.id === nextId) : -1;+ if (nextIndex === -1) {+ items.push(item);+ } else {+ items.splice(nextIndex, 0, item);+ }+ return true;+};diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.tsindex 8b5afa0838..e4a5f48c1c 100644--- a/app/src/types/index.d.ts+++ b/app/src/types/index.d.ts@@ -649,6 +649,7 @@ interface ISiyuan {},dragElement?: HTMLElement,dragTitle?: string,+ dragTab?: ITabDragData,currentDragOverTabHeadersElement?: HTMLElementtouchDragActive?: boolean,touchDragGhost?: HTMLElement | null,@@ -855,6 +856,15 @@ interface ITab {callback?: (tab: import("../layout/Tab").Tab) => void;}+interface ITabDragData {+ title?: string;+ icon?: string;+ docIcon?: string;+ pin: boolean;+ focus: boolean;+ unupdate: boolean;+}+interface IWebSocketData {cmd?: string;callback?: string;diff --git a/app/src/window/onWindowsMsg.ts b/app/src/window/onWindowsMsg.tsindex cbb338f2ee..4b3bbcbf58 100644--- a/app/src/window/onWindowsMsg.ts+++ b/app/src/window/onWindowsMsg.ts@@ -2,6 +2,7 @@ import {getInstanceById} from "../layout/util";import {Tab} from "../layout/Tab";import {lockScreen} from "../dialog/processSystem";import type {App} from "../index";+import {clearTabDragPreview} from "../layout/tabDrag";const closeTab = (ipcData: IWebSocketData) => {const tab = getInstanceById(ipcData.data);@@ -14,15 +15,14 @@ export const onWindowsMsg = (ipcData: IWebSocketData, app: App) => {case "closetab":closeTab(ipcData);break;+ case "setTabDragData":+ window.siyuan.dragTab = ipcData.data as ITabDragData;+ break;case "resetTabsStyle":// data: addRegionStyle, rmDragStyle, rmDragStyleRegionStyleif (ipcData.data === "rmDragStyle") {- document.querySelectorAll(".layout-tab-bars--drag").forEach(item => {- item.classList.remove("layout-tab-bars--drag");- });- document.querySelectorAll(".layout-tab-bar li[data-clone='true']").forEach(tabItem => {- tabItem.remove();- });+ clearTabDragPreview();+ window.siyuan.dragTab = undefined;} else {document.querySelectorAll(".layout-tab-bar--readonly .fn__flex-1").forEach((item: HTMLElement) => {if (item.getBoundingClientRect().top <= 6) {