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 tab drag sorting with live position previews https://github.com/siyuan-note/siyuan/issues/17642
Daniel762bcdecd248
diff --git a/app/src/assets/scss/protyle/_tabs.scss b/app/src/assets/scss/protyle/_tabs.scssindex 0142c4496c..f3a1e9170d 100644--- a/app/src/assets/scss/protyle/_tabs.scss+++ b/app/src/assets/scss/protyle/_tabs.scss@@ -73,6 +73,10 @@pointer-events: none;}+ .tabs-tab--dragging {+ opacity: .36;+ }+.tabs-control svg {display: block;width: 1em;diff --git a/app/src/protyle/render/tabsDrag.ts b/app/src/protyle/render/tabsDrag.tsnew file mode 100644index 0000000000..810c0cfbff--- /dev/null+++ b/app/src/protyle/render/tabsDrag.ts@@ -0,0 +1,128 @@+interface ITabsDragOptions {+ tabs: HTMLElement;+ readonly: () => boolean;+ move: (source: HTMLElement, target: HTMLElement, after: boolean) => void;+ render: () => void;+}++interface ITabsDrag {+ item: HTMLElement;+ button: HTMLElement;+ list: HTMLElement;+ next: Element;+ width: string;+ options: ITabsDragOptions;+ renders: Set<() => void>;+}++let dragging: ITabsDrag;++export const isDraggingTabs = (root: Element) => !!dragging &&+ (root.contains(dragging.item) || root.contains(dragging.button));++const restorePosition = (state: ITabsDrag) => {+ state.list.insertBefore(state.button, state.next?.parentElement === state.list ? state.next : null);+};++export const cancelTabsDrag = (root?: Element) => {+ if (!dragging || (root && !root.contains(dragging.item) && !root.contains(dragging.button))) {+ return;+ }+ const state = dragging;+ dragging = undefined;+ restorePosition(state);+ state.button.classList.remove("tabs-tab--dragging");+ state.button.style.width = state.width;+ state.renders.forEach(render => render());+};++// 拖动期间只调整导航按钮,正文顺序在落下时由一次事务更新。+export const bindTabsDrag = (list: HTMLElement, options: ITabsDragOptions) => {+ const canDrop = () => dragging && dragging.item.isConnected && !options.readonly() &&+ !dragging.options.readonly() && !dragging.item.contains(options.tabs);+ list.addEventListener("dragstart", (event: DragEvent) => {+ const button = (event.target as Element).closest<HTMLElement>(".tabs-tab");+ if (!button || button.parentElement !== list || options.readonly()) {+ event.preventDefault();+ return;+ }+ cancelTabsDrag();+ const item = Array.from(options.tabs.children).find(child =>+ child.getAttribute("data-node-id") === button.dataset.tabId) as HTMLElement;+ if (!item) {+ event.preventDefault();+ return;+ }+ dragging = {item, button, list, next: button.nextElementSibling, width: button.style.width,+ options, renders: new Set([options.render])};+ button.style.width = button.getBoundingClientRect().width + "px";+ button.classList.add("tabs-tab--dragging");+ event.dataTransfer.setData("application/x-siyuan-tab", button.dataset.tabId);+ event.dataTransfer.effectAllowed = "move";+ event.stopPropagation();+ });+ list.addEventListener("dragover", (event: DragEvent) => {+ if (!canDrop()) {+ return;+ }+ event.preventDefault();+ event.stopPropagation();+ event.dataTransfer.dropEffect = "move";+ const vertical = options.tabs.getAttribute("data-tabs-orientation") === "vertical";+ const bounds = list.getBoundingClientRect();+ const position = vertical ? event.clientY : event.clientX;+ const start = vertical ? bounds.top : bounds.left;+ const end = vertical ? bounds.bottom : bounds.right;+ const scroll = position < start + 24 ? -16 : position > end - 24 ? 16 : 0;+ if (vertical) {+ list.scrollTop += scroll;+ } else {+ list.scrollLeft += scroll;+ }+ const next = Array.from(list.children).find(child => {+ if (child === dragging.button) {+ return false;+ }+ const rect = child.getBoundingClientRect();+ return vertical ? event.clientY < rect.top + rect.height / 2 : event.clientX < rect.left + rect.width / 2;+ });+ dragging.renders.add(options.render);+ if (dragging.button.parentElement !== list || dragging.button.nextElementSibling !== (next || null)) {+ list.insertBefore(dragging.button, next || null);+ }+ });+ list.addEventListener("dragenter", event => {+ if (canDrop()) {+ event.preventDefault();+ event.stopPropagation();+ }+ });+ list.addEventListener("dragleave", (event: DragEvent) => {+ if (dragging && !list.contains(event.relatedTarget as Node)) {+ restorePosition(dragging);+ event.stopPropagation();+ }+ });+ list.addEventListener("drop", (event: DragEvent) => {+ if (!canDrop() || dragging.button.parentElement !== list) {+ return;+ }+ event.preventDefault();+ event.stopPropagation();+ const state = dragging;+ const next = state.button.nextElementSibling;+ const adjacent = next || state.button.previousElementSibling;+ const target = Array.from(options.tabs.children).find(child =>+ child.getAttribute("data-node-id") === (adjacent as HTMLElement)?.dataset.tabId) as HTMLElement;+ const changed = list !== state.list || next !== state.next;+ cancelTabsDrag();+ if (changed && target) {+ options.move(state.item, target, !next);+ }+ });+ // 跨组移动后 dragend 会在目标导航栏冒泡,两个入口都执行同一清理。+ list.addEventListener("dragend", event => {+ event.stopPropagation();+ cancelTabsDrag();+ });+};diff --git a/app/src/protyle/render/tabsRender.ts b/app/src/protyle/render/tabsRender.tsindex a74d69b89c..528f63d598 100644--- a/app/src/protyle/render/tabsRender.ts+++ b/app/src/protyle/render/tabsRender.ts@@ -1,4 +1,5 @@import {resolveTabID, tabKeyboardTarget} from "./tabsState";+import {bindTabsDrag, cancelTabsDrag, isDraggingTabs} from "./tabsDrag";export interface ITabsRenderOptions {readonly?: (tabs?: Element) => boolean;@@ -36,7 +37,6 @@ const roots = new WeakMap<Element, ITabsRoot>();const states = new WeakMap<HTMLElement, ITabState>();const boundHeaders = new WeakSet<Element>();let instanceID = 0;-let draggedTab: HTMLElement;export const getTabItems = (tabs: Element): HTMLElement[] =>Array.from(tabs.children).filter(item => item.classList.contains("tab-item")) as HTMLElement[];@@ -102,6 +102,9 @@ export const tabsRender = (element: Element, options: ITabsRenderOptions = {}) =controller.render();},render() {+ if (isDraggingTabs(element)) {+ return;+ }controller.observer.disconnect();controller.resize.disconnect();const shown: HTMLElement[] = [];@@ -159,6 +162,12 @@ export const tabsRender = (element: Element, options: ITabsRenderOptions = {}) =list.setAttribute("role", "tablist");list.setAttribute("aria-label", controller.options.label || "Tabs");header.appendChild(list);+ bindTabsDrag(list, {+ tabs,+ readonly: () => (controller.options.readonly?.(tabs) ?? true) || !controller.options.move,+ move: (source, target, after) => controller.options.move?.(source, target, after),+ render: schedule,+ });items.forEach((item, index) => {const button = document.createElement("button");button.type = "button";@@ -218,30 +227,6 @@ export const tabsRender = (element: Element, options: ITabsRenderOptions = {}) =(Array.from(list.children).find(child => (child as HTMLElement).dataset.tabId === target) as HTMLElement)?.focus();});button.draggable = !readonly && !!controller.options.move;- button.addEventListener("dragstart", event => {- draggedTab = item;- event.dataTransfer.setData("application/x-siyuan-tab", itemID(item));- event.dataTransfer.effectAllowed = "move";- event.stopPropagation();- });- button.addEventListener("dragover", event => {- if (draggedTab && !readonly) {- event.preventDefault();- event.stopPropagation();- }- });- button.addEventListener("drop", event => {- if (draggedTab && !readonly) {- event.preventDefault();- event.stopPropagation();- const rect = button.getBoundingClientRect();- const after = tabs.getAttribute("data-tabs-orientation") === "vertical" ?- event.clientY > rect.top + rect.height / 2 : event.clientX > rect.left + rect.width / 2;- controller.options.move?.(draggedTab, item, after);- draggedTab = undefined;- }- });- button.addEventListener("dragend", () => draggedTab = undefined);list.appendChild(button);});if (!readonly) {@@ -334,6 +319,7 @@ export const tabsRender = (element: Element, options: ITabsRenderOptions = {}) =}}),destroy() {+ cancelTabsDrag(element);destroyed = true;controller.observer.disconnect();controller.resize.disconnect();diff --git a/docs/TABS.md b/docs/TABS.mdindex e619d17792..33ae423a8a 100644--- a/docs/TABS.md+++ b/docs/TABS.md@@ -84,6 +84,7 @@ Linux 下的说明。- 各层嵌套独立保存选择。定位隐藏内容时逐层激活所属页签;单独打开页签项展示标题与全部正文。- 普通文字选区不隐式包含隐藏正文;选中整组块后,复制和删除涵盖全部页签。正文首尾的退格和删除不能自动合并相邻页签。- 页签项拖动移动整页,正文块拖动移动内容。跨组移动及两组选择状态调整在同一事务中完成。+- 拖动导航页签时实时调整按钮位置并淡化被拖动的按钮,相邻页签让出落点;横向和纵向布局分别按对应方向判断位置,靠近导航边缘时滚动。预览不调整正文,松开后提交一次排序事务,取消拖动或拖回原位不提交排序。跨组拖动采用相同反馈,只读目标和自身后代不接受落入。- 删除当前页后优先选中后一个页签,否则选前一个。删除最后一页时移除容器,必要时补空段落作为光标落点。- 取消页签布局时,两层容器转换为纵向超级块,标题转换为段落,保留容器、页签项和原正文 ID。