|
| 1 | +import { get } from "svelte/store"; |
1 | 2 | import type { DatabaseNode } from "../../../../core/nodes/DatabaseNode/databaseNode"; |
| 3 | +import { FieldDatabaseNode } from "../../../../core/nodes/DatabaseNode/fieldDatabaseNode"; |
| 4 | +import { TextDatabaseNode } from "../../../../core/nodes/DatabaseNode/textDatabaseNode"; |
| 5 | +import { getNodeDatabase } from "../../../../core/nodes/db"; |
| 6 | +import { nodeUpdater } from "../../../../core/nodes/NodeUpdater/nodeUpdater"; |
| 7 | +import { selectedNodeIds } from "../canvasStore"; |
| 8 | +import { CANVAS_HEIGHT, CANVAS_WIDTH, GRID_SNAP_SIZE } from "../const"; |
| 9 | +import { clamp } from "../util"; |
| 10 | +import { close } from "../contextMenu.svelte"; |
2 | 11 |
|
3 | 12 | export class CanvasNode { |
4 | 13 | id: number; |
5 | 14 | type: string; |
6 | 15 | element: HTMLElement | null; |
7 | 16 | active: boolean; |
| 17 | + isDragging: boolean; |
| 18 | + dragOffsetX: number; |
| 19 | + dragOffsetY: number; |
8 | 20 |
|
9 | 21 | constructor(id: number, type: string) { |
10 | 22 | this.id = id; |
11 | 23 | this.type = type; |
12 | 24 | this.element = null; |
13 | 25 | this.active = false; |
| 26 | + this.isDragging = false; |
| 27 | + this.dragOffsetX = 0; |
| 28 | + this.dragOffsetY = 0; |
14 | 29 | } |
15 | 30 |
|
16 | 31 | render(DatabaseNode: DatabaseNode): void { |
17 | 32 | throw new Error("Method not implemented"); |
18 | 33 | } |
19 | 34 |
|
20 | | - click(e: MouseEvent): void { |
| 35 | + _pointerDown(e: MouseEvent): void { |
21 | 36 | throw new Error("Method not implemented"); |
22 | 37 | } |
23 | 38 |
|
24 | | - dblClick(e: MouseEvent): void { |
25 | | - throw new Error("Method not implemented"); |
| 39 | + _dblClick(e: MouseEvent): void { |
| 40 | + e.stopPropagation(); |
26 | 41 | } |
27 | 42 |
|
28 | | - rightClick(e: MouseEvent): void { |
29 | | - throw new Error("Method not implemented"); |
| 43 | + // 共通の右クリック処理 |
| 44 | + protected handleRightClick(e: MouseEvent): void { |
| 45 | + e.stopPropagation(); |
| 46 | + e.preventDefault(); |
| 47 | + |
| 48 | + // 右クリック時の選択処理 |
| 49 | + if (e.ctrlKey) { |
| 50 | + // Ctrlキーが押されている場合は追加選択 |
| 51 | + const currentSelection = get(selectedNodeIds); |
| 52 | + if (currentSelection.includes(this.id)) { |
| 53 | + selectedNodeIds.set(currentSelection.filter(id => id !== this.id)); |
| 54 | + } else { |
| 55 | + selectedNodeIds.set([...currentSelection, this.id]); |
| 56 | + } |
| 57 | + } else { |
| 58 | + // Ctrlキーが押されていない場合 |
| 59 | + const currentSelection = get(selectedNodeIds); |
| 60 | + // 既に選択されていない場合のみ、このノードのみを選択 |
| 61 | + if (!currentSelection.includes(this.id)) { |
| 62 | + selectedNodeIds.set([this.id]); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // コンテキストメニューを開くためのイベントを発行 |
| 67 | + const contextMenuEvent = new CustomEvent('openNodeContextMenu', { |
| 68 | + detail: { |
| 69 | + x: e.clientX, |
| 70 | + y: e.clientY, |
| 71 | + id: this.id |
| 72 | + } |
| 73 | + }); |
| 74 | + document.dispatchEvent(contextMenuEvent); |
| 75 | + } |
| 76 | + |
| 77 | + // 共通のマウスダウン処理 |
| 78 | + protected handleMouseDown(e: MouseEvent): void { |
| 79 | + if (e.button !== 0) return; |
| 80 | + e.stopPropagation(); |
| 81 | + |
| 82 | + close(); |
| 83 | + |
| 84 | + const currentSelection = get(selectedNodeIds); |
| 85 | + |
| 86 | + if (e.ctrlKey) { |
| 87 | + if (currentSelection.includes(this.id)) { |
| 88 | + selectedNodeIds.set(currentSelection.filter(id => id !== this.id)); |
| 89 | + } else { |
| 90 | + selectedNodeIds.set([...currentSelection, this.id]); |
| 91 | + } |
| 92 | + } else { |
| 93 | + // 既に選択されている場合は何もしない |
| 94 | + if (!currentSelection.includes(this.id)) { |
| 95 | + selectedNodeIds.set([this.id]); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + this.isDragging = true; |
| 100 | + const parent = (this.element as HTMLElement).parentElement; |
| 101 | + const parentRect = parent?.getBoundingClientRect(); |
| 102 | + const scale = |
| 103 | + parent?.style.transform?.match(/scale\(([\d.]+)\)/)?.[1] |
| 104 | + ? parseFloat(parent.style.transform.match(/scale\(([\d.]+)\)/)![1]) |
| 105 | + : 1; |
| 106 | + |
| 107 | + // 基準点: マウスの押した位置を記録 |
| 108 | + this.dragOffsetX = (e.clientX - (parentRect?.left ?? 0)) / scale; |
| 109 | + this.dragOffsetY = (e.clientY - (parentRect?.top ?? 0)) / scale; |
| 110 | + |
| 111 | + document.body.style.cursor = "grabbing"; |
| 112 | + document.addEventListener("mousemove", this.handleDragMove); |
| 113 | + document.addEventListener("mouseup", this.handleDragEnd); |
| 114 | + } |
| 115 | + |
| 116 | + // 共通のドラッグ移動処理 |
| 117 | + private handleDragMove = async (e: MouseEvent) => { |
| 118 | + if (!this.isDragging || !this.element) return; |
| 119 | + const parent = this.element.parentElement; |
| 120 | + if (!parent) return; |
| 121 | + const parentRect = parent.getBoundingClientRect(); |
| 122 | + const scale = |
| 123 | + parent.style.transform?.match(/scale\(([\d.]+)\)/)?.[1] |
| 124 | + ? parseFloat(parent.style.transform.match(/scale\(([\d.]+)\)/)![1]) |
| 125 | + : 1; |
| 126 | + // 現在のマウス位置を取得 |
| 127 | + const currentMouseX = (e.clientX - parentRect.left) / scale; |
| 128 | + const currentMouseY = (e.clientY - parentRect.top) / scale; |
| 129 | + |
| 130 | + // ドラッグ開始からの移動量を計算 |
| 131 | + const deltaX = currentMouseX - this.dragOffsetX; |
| 132 | + const deltaY = currentMouseY - this.dragOffsetY; |
| 133 | + |
| 134 | + // 実際に適用された移動量を記録するための変数 |
| 135 | + let actualDeltaX = 0; |
| 136 | + let actualDeltaY = 0; |
| 137 | + |
| 138 | + // 選択されているすべてのノードを移動 |
| 139 | + const selectedIds = get(selectedNodeIds); |
| 140 | + |
| 141 | + // 選択されているノードを一括更新(異なる種類のノードも含む) |
| 142 | + for (const nodeId of selectedIds) { |
| 143 | + const node = getNodeDatabase(nodeId); |
| 144 | + if (node && this.isMovableNode(node)) { |
| 145 | + // 各ノードの元の位置 |
| 146 | + const oldX = node.x!; |
| 147 | + const oldY = node.y!; |
| 148 | + |
| 149 | + // 各ノードの新しい位置を計算 |
| 150 | + const newX = clamp(node.x! + deltaX, 0, CANVAS_WIDTH); |
| 151 | + const newY = clamp(node.y! + deltaY, 0, CANVAS_HEIGHT); |
| 152 | + const snappedX = Math.round(newX / GRID_SNAP_SIZE) * GRID_SNAP_SIZE; |
| 153 | + const snappedY = Math.round(newY / GRID_SNAP_SIZE) * GRID_SNAP_SIZE; |
| 154 | + |
| 155 | + // 実際の移動量を計算(最初のノードの移動量を基準にする) |
| 156 | + if (actualDeltaX === 0 && actualDeltaY === 0) { |
| 157 | + actualDeltaX = snappedX - oldX; |
| 158 | + actualDeltaY = snappedY - oldY; |
| 159 | + } |
| 160 | + |
| 161 | + // ノードの種類に応じて適切な更新を行う |
| 162 | + await this.updateNodePosition(node, nodeId, snappedX, snappedY); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + // 実際に適用された移動量分だけオフセットを更新(残りの移動量は蓄積される) |
| 167 | + this.dragOffsetX += actualDeltaX; |
| 168 | + this.dragOffsetY += actualDeltaY; |
| 169 | + }; |
| 170 | + |
| 171 | + // 共通のドラッグ終了処理 |
| 172 | + private handleDragEnd = (e: MouseEvent) => { |
| 173 | + this.isDragging = false; |
| 174 | + document.body.style.cursor = ""; |
| 175 | + document.removeEventListener("mousemove", this.handleDragMove); |
| 176 | + document.removeEventListener("mouseup", this.handleDragEnd); |
| 177 | + }; |
| 178 | + |
| 179 | + protected handleClick(e: MouseEvent): void { |
| 180 | + if (e.button !== 0) return; |
| 181 | + e.stopPropagation(); |
| 182 | + |
| 183 | + // Ctrlキーが押されていない場合は、このノードのみを選択 |
| 184 | + if (!e.ctrlKey) { |
| 185 | + selectedNodeIds.set([this.id]); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + // ノードが移動可能かどうかを判定 |
| 190 | + private isMovableNode(node: DatabaseNode): node is FieldDatabaseNode | TextDatabaseNode { |
| 191 | + return node instanceof FieldDatabaseNode || node instanceof TextDatabaseNode; |
| 192 | + } |
| 193 | + |
| 194 | + // ノードタイプに応じた位置更新 |
| 195 | + private async updateNodePosition(node: DatabaseNode, nodeId: number, x: number, y: number): Promise<void> { |
| 196 | + if (node instanceof FieldDatabaseNode) { |
| 197 | + await get(nodeUpdater)!.update(new FieldDatabaseNode(nodeId, x, y, undefined, undefined)); |
| 198 | + } else if (node instanceof TextDatabaseNode) { |
| 199 | + await get(nodeUpdater)!.update(new TextDatabaseNode(nodeId, x, y, undefined, undefined, undefined, undefined)); |
| 200 | + } |
| 201 | + // 新しいノードタイプを追加する場合は、ここに条件を追加するだけ |
30 | 202 | } |
31 | 203 |
|
32 | 204 | } |
0 commit comments