Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

feat(diagram): place architecture shapes by drag

tianma-ife691453aac81
diff --git a/apps/web/src/components/DiagramEditorPane.test.mjs b/apps/web/src/components/DiagramEditorPane.test.mjs
index cc18906c..bf694f3f 100644
--- a/apps/web/src/components/DiagramEditorPane.test.mjs
+++ b/apps/web/src/components/DiagramEditorPane.test.mjs
@@ -163,7 +163,7 @@ describe("diagram editor canvas surface", () => {
expect(source).toContain('className="grid grid-cols-7 gap-1 px-1 pb-2"');
expect(source).toContain('text-xs font-semibold');
expect(source).not.toContain('{category.items.length}</span>');
- expect(source).toContain('<TooltipContent side="top">');
+ expect(source).toContain('<TooltipContent side="top"');
expect(source).toContain('aria-label={label}');
expect(source).toContain('options.label ??');
expect(source).toContain('resourceIcon: architectureResourceIcon(item)');
@@ -174,6 +174,21 @@ describe("diagram editor canvas surface", () => {
expect(source).not.toContain('className="line-clamp-2"');
});
+ test("places architecture resources at an explicit canvas position", () => {
+ expect(source).toContain('const ARCHITECTURE_LIBRARY_DRAG_TYPE = "application/x-edgeever-architecture-resource"');
+ expect(source).toContain('<DropdownMenu modal={false}');
+ expect(source).toContain("draggable");
+ expect(source).toContain("event.dataTransfer.setData(ARCHITECTURE_LIBRARY_DRAG_TYPE");
+ expect(source).toContain("onDragOver={handleArchitectureDragOver}");
+ expect(source).toContain("onDrop={handleArchitectureDrop}");
+ expect(source).toContain("graph.clientToLocal({ x: event.clientX, y: event.clientY })");
+ expect(source).toContain("position: { x: number; y: number }");
+ expect(source).toContain("x: options.position.x - authoredSize.width / 2");
+ expect(source).toContain("onPick={setPendingArchitectureItem}");
+ expect(source).toContain("onPointerDownCapture={handlePendingArchitecturePlacement}");
+ expect(source).toContain('t("diagram.placeShapeHint"');
+ });
+
test("opens every diagram insertion library immediately on pointer hover", () => {
expect(source).toContain("const DiagramInsertMenu = ({");
expect(source.match(/onPointerEnter=\{\(\) => setOpen\(true\)\}/g)?.length).toBe(2);
diff --git a/apps/web/src/components/DiagramEditorPane.tsx b/apps/web/src/components/DiagramEditorPane.tsx
index da1211fc..57cca4c4 100644
--- a/apps/web/src/components/DiagramEditorPane.tsx
+++ b/apps/web/src/components/DiagramEditorPane.tsx
@@ -1,4 +1,4 @@
-import { useCallback, useEffect, useRef, useState } from "react";
+import { useCallback, useEffect, useRef, useState, type DragEvent as ReactDragEvent, type PointerEvent as ReactPointerEvent } from "react";
import { Export, Graph, History, Keyboard, Selection, type Edge, type Node } from "@antv/x6";
import * as m from "motion/react-m";
import {
@@ -170,6 +170,8 @@ type ArchitectureLibraryItem = {
shape: DiagramNodeShape;
};
+const ARCHITECTURE_LIBRARY_DRAG_TYPE = "application/x-edgeever-architecture-resource";
+
const ARCHITECTURE_LIBRARY_CATEGORIES: Array<{
id: string;
labelKey: string;
@@ -305,10 +307,10 @@ const inferArchitectureResourceIcon = (
};
const ArchitectureComponentLibrary = ({
- onAdd,
+ onPick,
t,
}: {
- onAdd: (item: ArchitectureLibraryItem) => void;
+ onPick: (item: ArchitectureLibraryItem) => void;
t: (key: string) => string;
}) => {
const [open, setOpen] = useState(false);
@@ -320,7 +322,7 @@ const ArchitectureComponentLibrary = ({
})).filter((category) => category.items.length > 0);
return (
- <DropdownMenu open={open} onOpenChange={(nextOpen) => {
+ <DropdownMenu modal={false} open={open} onOpenChange={(nextOpen) => {
setOpen(nextOpen);
if (!nextOpen) setQuery("");
}}>
@@ -359,17 +361,24 @@ const ArchitectureComponentLibrary = ({
<TooltipTrigger asChild>
<DropdownMenuItem
aria-label={label}
- className={cn("flex h-10 w-10 cursor-pointer justify-center rounded-lg p-0 hover:bg-current/10 focus:bg-current/10", category.tone)}
+ className={cn("flex h-10 w-10 cursor-grab justify-center rounded-lg p-0 hover:bg-current/10 focus:bg-current/10 active:cursor-grabbing", category.tone)}
+ draggable
+ onDragStart={(event) => {
+ event.dataTransfer.effectAllowed = "copy";
+ event.dataTransfer.setData(ARCHITECTURE_LIBRARY_DRAG_TYPE, architectureResourceIcon(item));
+ }}
+ onDragEnd={() => setOpen(false)}
onSelect={() => {
- onAdd(item);
+ onPick(item);
setOpen(false);
}}
>
<Icon className="h-5 w-5" />
</DropdownMenuItem>
</TooltipTrigger>
- <TooltipContent side="top">
- {label}
+ <TooltipContent side="top" className="max-w-48">
+ <div>{label}</div>
+ <div className="text-[11px] text-slate-300">{t("diagram.placeShapeHelp")}</div>
</TooltipContent>
</Tooltip>
);
@@ -987,10 +996,26 @@ export const DiagramEditorPane = ({
const [historyState, setHistoryState] = useState({ undo: false, redo: false });
const [nodeEditor, setNodeEditor] = useState<NodeEditorState | null>(null);
const [flowQuickCreate, setFlowQuickCreate] = useState<FlowQuickCreateState | null>(null);
+ const [pendingArchitectureItem, setPendingArchitectureItem] = useState<ArchitectureLibraryItem | null>(null);
const flowQuickCreateRef = useRef<FlowQuickCreateState | null>(null);
const flowPointerDragRef = useRef<FlowPointerDragState | null>(null);
const nodeEditorRef = useRef<NodeEditorState | null>(null);
+ useEffect(() => {
+ setPendingArchitectureItem(null);
+ }, [memo.id]);
+
+ useEffect(() => {
+ if (!pendingArchitectureItem) return;
+ const cancelPlacement = (event: KeyboardEvent) => {
+ if (event.key !== "Escape") return;
+ event.preventDefault();
+ setPendingArchitectureItem(null);
+ };
+ window.addEventListener("keydown", cancelPlacement);
+ return () => window.removeEventListener("keydown", cancelPlacement);
+ }, [pendingArchitectureItem]);
+
const beginNodeEdit = useCallback((node: Node) => {
const graph = graphRef.current;
if (!graph || readOnly) return;
@@ -1554,6 +1579,7 @@ export const DiagramEditorPane = ({
baseNodeId?: string;
beginEditing?: boolean;
label?: string;
+ position?: { x: number; y: number };
resourceIcon?: ArchitectureResourceIcon;
} = {},
) => {
@@ -1580,7 +1606,46 @@ export const DiagramEditorPane = ({
const childNodes = isMindMap && selected?.isNode()
? graph.getNodes().filter((node) => node.getData<NodeData>()?.parentId === selected.id)
: [];
- const nextPosition = requestedSibling
+ const authoredSize = isArchitecture
+ ? compactArchitectureNodeSize(shape)
+ : { width: shape === "decision" ? 132 : 140, height: shape === "decision" ? 84 : 52 };
+ const dropBoundary = isArchitecture && shape !== "boundary" && options.position
+ ? graph.getNodes()
+ .filter((node) => node.getData<NodeData>()?.shape === "boundary")
+ .filter((node) => {
+ const bounds = node.getBBox();
+ return options.position!.x >= bounds.x
+ && options.position!.x <= bounds.x + bounds.width
+ && options.position!.y >= bounds.y
+ && options.position!.y <= bounds.y + bounds.height;
+ })
+ .sort((left, right) => {
+ const leftBounds = left.getBBox();
+ const rightBounds = right.getBBox();
+ return leftBounds.width * leftBounds.height - rightBounds.width * rightBounds.height;
+ })[0]
+ : undefined;
+ const requestedPosition = options.position
+ ? {
+ x: options.position.x - authoredSize.width / 2,
+ y: options.position.y - authoredSize.height / 2,
+ }
+ : undefined;
+ if (requestedPosition && dropBoundary) {
+ const bounds = dropBoundary.getBBox();
+ const horizontalPadding = 18;
+ const topPadding = 40;
+ const bottomPadding = 18;
+ requestedPosition.x = Math.min(
+ Math.max(requestedPosition.x, bounds.x + horizontalPadding),
+ Math.max(bounds.x + horizontalPadding, bounds.x + bounds.width - authoredSize.width - horizontalPadding),
+ );
+ requestedPosition.y = Math.min(
+ Math.max(requestedPosition.y, bounds.y + topPadding),
+ Math.max(bounds.y + topPadding, bounds.y + bounds.height - authoredSize.height - bottomPadding),
+ );
+ }
+ const nextPosition = requestedPosition ?? (requestedSibling
? {
x: selectedPosition.x,
y: Math.max(selectedPosition.y, ...siblings.map((node) => node.getPosition().y)) + 52,
@@ -1590,14 +1655,11 @@ export const DiagramEditorPane = ({
y: childNodes.length > 0
? Math.max(...childNodes.map((node) => node.getPosition().y)) + 52
: selectedPosition.y,
- };
+ });
const id = createId(isMindMap ? "topic" : "node");
const architectureParentId = isArchitecture && shape !== "boundary"
- ? selectedData?.shape === "boundary" ? selected?.id : selectedData?.parentId
+ ? dropBoundary?.id ?? (options.position ? undefined : selectedData?.shape === "boundary" ? selected?.id : selectedData?.parentId)
: undefined;
- const authoredSize = isArchitecture
- ? compactArchitectureNodeSize(shape)
- : { width: shape === "decision" ? 132 : 140, height: shape === "decision" ? 84 : 52 };
graph.startBatch("add");
const node = graph.addNode(nodeMetadata({
id,
@@ -1650,6 +1712,47 @@ export const DiagramEditorPane = ({
}
}, [beginNodeEdit, document, readOnly, selectedNodeId, t]);
+ const placeArchitectureItem = useCallback((
+ item: ArchitectureLibraryItem,
+ position: { x: number; y: number },
+ ) => {
+ addNode(item.shape, {
+ label: t(item.labelKey),
+ position,
+ resourceIcon: architectureResourceIcon(item),
+ });
+ setPendingArchitectureItem(null);
+ requestAnimationFrame(() => containerRef.current?.focus({ preventScroll: true }));
+ }, [addNode, t]);
+
+ const handleArchitectureDragOver = (event: ReactDragEvent<HTMLDivElement>) => {
+ if (document?.kind !== "architecture" || readOnly || !Array.from(event.dataTransfer.types).includes(ARCHITECTURE_LIBRARY_DRAG_TYPE)) return;
+ event.preventDefault();
+ event.dataTransfer.dropEffect = "copy";
+ };
+
+ const handleArchitectureDrop = (event: ReactDragEvent<HTMLDivElement>) => {
+ if (document?.kind !== "architecture" || readOnly) return;
+ const resourceIcon = event.dataTransfer.getData(ARCHITECTURE_LIBRARY_DRAG_TYPE) as ArchitectureResourceIcon;
+ const item = ARCHITECTURE_LIBRARY_ITEMS.find((candidate) => architectureResourceIcon(candidate) === resourceIcon);
+ const graph = graphRef.current;
+ if (!item || !graph) return;
+ event.preventDefault();
+ event.stopPropagation();
+ placeArchitectureItem(item, graph.clientToLocal({ x: event.clientX, y: event.clientY }));
+ };
+
+ const handlePendingArchitecturePlacement = (event: ReactPointerEvent<HTMLDivElement>) => {
+ if (!pendingArchitectureItem || document?.kind !== "architecture" || readOnly || event.button !== 0) return;
+ const target = event.target instanceof Element ? event.target : null;
+ if (target?.closest(".x6-node, .x6-edge")) return;
+ const graph = graphRef.current;
+ if (!graph) return;
+ event.preventDefault();
+ event.stopPropagation();
+ placeArchitectureItem(pendingArchitectureItem, graph.clientToLocal({ x: event.clientX, y: event.clientY }));
+ };
+
insertNodeRef.current = (relation, baseNodeId) => {
addNode("topic", { relation, baseNodeId, beginEditing: true });
};
@@ -2144,10 +2247,7 @@ export const DiagramEditorPane = ({
/>
) : document.kind === "architecture" ? (
<ArchitectureComponentLibrary
- onAdd={(item) => addNode(item.shape, {
- label: t(item.labelKey),
- resourceIcon: architectureResourceIcon(item),
- })}
+ onPick={setPendingArchitectureItem}
t={t}
/>
) : (
@@ -2192,7 +2292,24 @@ export const DiagramEditorPane = ({
theme={theme}
/>
<div className="relative min-h-0 flex-1">
- <div ref={containerRef} className="edgeever-diagram-canvas absolute inset-0 touch-none outline-none" data-diagram-appearance={resolvedTheme} data-diagram-kind={document.kind} data-diagram-theme={theme} tabIndex={0} aria-label={t("diagram.canvas", { type: kindLabel })} />
+ <div
+ ref={containerRef}
+ className={cn("edgeever-diagram-canvas absolute inset-0 touch-none outline-none", pendingArchitectureItem && "cursor-crosshair")}
+ data-architecture-placement={pendingArchitectureItem ? "active" : undefined}
+ data-diagram-appearance={resolvedTheme}
+ data-diagram-kind={document.kind}
+ data-diagram-theme={theme}
+ tabIndex={0}
+ aria-label={t("diagram.canvas", { type: kindLabel })}
+ onDragOver={handleArchitectureDragOver}
+ onDrop={handleArchitectureDrop}
+ onPointerDownCapture={handlePendingArchitecturePlacement}
+ />
+ {pendingArchitectureItem ? (
+ <div className="pointer-events-none absolute left-1/2 top-3 z-20 -translate-x-1/2 rounded-md border border-slate-200 bg-white/95 px-3 py-1.5 text-xs font-medium text-slate-600 shadow-sm" role="status">
+ {t("diagram.placeShapeHint", { shape: t(pendingArchitectureItem.labelKey) })}
+ </div>
+ ) : null}
{flowQuickCreate ? (
<div
className="absolute z-30 w-[330px] max-w-[calc(100%-24px)] rounded-xl border border-slate-200 bg-white p-2 shadow-xl"
diff --git a/packages/shared/src/i18n/en-US.ts b/packages/shared/src/i18n/en-US.ts
index 456a2ada..f3d3352d 100644
--- a/packages/shared/src/i18n/en-US.ts
+++ b/packages/shared/src/i18n/en-US.ts
@@ -268,6 +268,8 @@ export const enUS = {
addBoundary: "Add system boundary",
components: "Components",
componentLibrary: "Shape library",
+ placeShapeHelp: "Drag to the canvas, or select then click to place",
+ placeShapeHint: "Click the canvas to place “{{shape}}” · Esc to cancel",
componentSearch: "Search architecture components",
componentCategoryExperience: "Apps & clients",
componentCategoryServices: "Compute & services",
diff --git a/packages/shared/src/i18n/zh-CN.ts b/packages/shared/src/i18n/zh-CN.ts
index 62ebf344..454b38e9 100644
--- a/packages/shared/src/i18n/zh-CN.ts
+++ b/packages/shared/src/i18n/zh-CN.ts
@@ -268,6 +268,8 @@ export const zhCN = {
addBoundary: "添加系统边界",
components: "组件",
componentLibrary: "图形库",
+ placeShapeHelp: "拖到画布,或选中后点击放置",
+ placeShapeHint: "点击画布放置“{{shape}}” · Esc 取消",
componentSearch: "搜索架构组件",
componentCategoryExperience: "应用与客户端",
componentCategoryServices: "计算与服务",