@rytass/wms-map-react-components 0.1.10 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/components/breadcrumb/breadcrumb-edit-modal.cjs +92 -0
- package/components/breadcrumb/breadcrumb-edit-modal.d.cts +9 -0
- package/components/breadcrumb/breadcrumb-edit-modal.module.scss.cjs +5 -0
- package/components/breadcrumb/breadcrumb.cjs +171 -0
- package/components/breadcrumb/breadcrumb.d.cts +11 -0
- package/components/breadcrumb/breadcrumb.d.ts +1 -1
- package/components/breadcrumb/breadcrumb.module.scss.cjs +5 -0
- package/components/canvas/react-flow-canvas.cjs +321 -0
- package/components/canvas/react-flow-canvas.d.cts +38 -0
- package/components/canvas/react-flow-canvas.d.ts +1 -1
- package/components/canvas/react-flow-canvas.module.scss.cjs +5 -0
- package/components/modal/wms-map-content.cjs +808 -0
- package/components/modal/wms-map-content.d.cts +4 -0
- package/components/modal/wms-map-content.d.ts +1 -1
- package/components/modal/wms-map-modal-container.cjs +130 -0
- package/components/modal/wms-map-modal-container.d.cts +5 -0
- package/components/modal/wms-map-modal-container.d.ts +1 -1
- package/components/modal/wms-map-modal-container.module.scss.cjs +5 -0
- package/components/nodes/base-node.d.cts +12 -0
- package/components/nodes/base-node.d.ts +1 -1
- package/components/nodes/image-node.cjs +236 -0
- package/components/nodes/image-node.d.cts +17 -0
- package/components/nodes/image-node.d.ts +1 -1
- package/components/nodes/image-node.module.scss.cjs +5 -0
- package/components/nodes/path-node.cjs +553 -0
- package/components/nodes/path-node.d.cts +19 -0
- package/components/nodes/path-node.d.ts +1 -1
- package/components/nodes/path-node.module.scss.cjs +5 -0
- package/components/nodes/rectangle-node.cjs +239 -0
- package/components/nodes/rectangle-node.d.cts +11 -0
- package/components/nodes/rectangle-node.d.ts +1 -1
- package/components/nodes/rectangle-node.module.scss.cjs +5 -0
- package/components/ui/context-menu.cjs +171 -0
- package/components/ui/context-menu.d.cts +25 -0
- package/components/ui/context-menu.module.scss.cjs +5 -0
- package/components/ui/toolbar.cjs +286 -0
- package/components/ui/toolbar.d.cts +23 -0
- package/components/ui/toolbar.d.ts +1 -1
- package/components/ui/toolbar.module.scss.cjs +5 -0
- package/components/ui/view-mode-toolbar.cjs +44 -0
- package/components/ui/view-mode-toolbar.d.cts +9 -0
- package/components/ui/view-mode-toolbar.d.ts +1 -1
- package/components/ui/view-mode-toolbar.module.scss.cjs +5 -0
- package/constants/index.cjs +54 -0
- package/constants/index.d.cts +45 -0
- package/constants/index.d.ts +2 -2
- package/constants/text-mappings.cjs +46 -0
- package/constants/text-mappings.d.cts +181 -0
- package/hooks/use-context-menu.cjs +231 -0
- package/hooks/use-context-menu.d.cts +37 -0
- package/hooks/use-context-menu.d.ts +1 -1
- package/hooks/use-direct-state-history.cjs +217 -0
- package/hooks/use-direct-state-history.d.cts +58 -0
- package/hooks/use-direct-state-history.d.ts +2 -2
- package/hooks/use-pen-drawing.cjs +366 -0
- package/hooks/use-pen-drawing.d.cts +30 -0
- package/hooks/use-pen-drawing.d.ts +1 -1
- package/hooks/use-rectangle-drawing.cjs +139 -0
- package/hooks/use-rectangle-drawing.d.cts +19 -0
- package/hooks/use-rectangle-drawing.d.ts +1 -1
- package/hooks/use-text-editing.cjs +156 -0
- package/hooks/use-text-editing.d.cts +19 -0
- package/icons/index.cjs +233 -0
- package/icons/index.d.cts +16 -0
- package/index.cjs +26 -0
- package/index.d.cts +5 -0
- package/index.d.ts +5 -5
- package/package.json +68 -3
- package/types/callback.types.d.cts +7 -0
- package/types/callback.types.d.ts +1 -1
- package/types/canvas.types.d.cts +35 -0
- package/types/canvas.types.d.ts +1 -1
- package/types/component-props.types.d.cts +45 -0
- package/types/component-props.types.d.ts +2 -2
- package/types/history.types.d.cts +24 -0
- package/types/history.types.d.ts +2 -2
- package/types/index.d.cts +5 -0
- package/types/index.d.ts +5 -5
- package/types/react-flow.types.d.cts +30 -0
- package/typings.cjs +44 -0
- package/typings.d.cts +136 -0
- package/utils/api-data-transform.cjs +221 -0
- package/utils/api-data-transform.d.cts +33 -0
- package/utils/api-data-transform.d.ts +1 -1
- package/utils/color-utils.cjs +58 -0
- package/utils/color-utils.d.cts +17 -0
- package/utils/debug-logger.cjs +45 -0
- package/utils/debug-logger.d.cts +33 -0
- package/utils/map-data-transform.cjs +360 -0
- package/utils/map-data-transform.d.cts +18 -0
- package/utils/map-data-transform.d.ts +1 -1
- package/utils/node-operations.cjs +88 -0
- package/utils/node-operations.d.cts +26 -0
- package/utils/node-utils.cjs +40 -0
- package/utils/node-utils.d.cts +10 -0
- package/index.cjs.css +0 -1
- package/index.cjs.js +0 -5128
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 文字對應表
|
|
3
|
+
* 用於管理所有中文文字,避免 Non-ASCII 字符問題
|
|
4
|
+
*/
|
|
5
|
+
export declare const TEXT_MAPPINGS: {
|
|
6
|
+
readonly DEBUG: {
|
|
7
|
+
readonly DATA_LOADING: "data-loading";
|
|
8
|
+
readonly HISTORY: "history";
|
|
9
|
+
readonly REACT_FLOW: "reactFlow";
|
|
10
|
+
readonly NODES: "nodes";
|
|
11
|
+
readonly EVENTS: "events";
|
|
12
|
+
readonly UI: "ui";
|
|
13
|
+
readonly TESTING: "testing";
|
|
14
|
+
};
|
|
15
|
+
readonly OPERATIONS: {
|
|
16
|
+
readonly LOAD_NEW_DATA: "load-new-data";
|
|
17
|
+
readonly CHANGE_COLOR: "change-color";
|
|
18
|
+
readonly MOVE_SHAPE: "move-shape";
|
|
19
|
+
readonly DRAW_RECTANGLE: "draw-rectangle";
|
|
20
|
+
readonly DRAW_PATH: "draw-path";
|
|
21
|
+
readonly TEXT_EDIT: "text-edit";
|
|
22
|
+
readonly PATH_EDIT: "path-edit";
|
|
23
|
+
readonly DATA_CHANGE: "data-change";
|
|
24
|
+
readonly DELETE_IMAGES: "delete-images";
|
|
25
|
+
readonly DELETE_LAYERS: "delete-layers";
|
|
26
|
+
readonly UPLOAD_IMAGE: "upload-image";
|
|
27
|
+
};
|
|
28
|
+
readonly MESSAGES: {
|
|
29
|
+
readonly IMAGE_NODE: "圖片節點";
|
|
30
|
+
readonly RECTANGLE_NODE: "矩形節點";
|
|
31
|
+
readonly PATH_NODE: "路徑節點";
|
|
32
|
+
readonly SELECTABLE: "可選取";
|
|
33
|
+
readonly DRAGGABLE: "可拖曳";
|
|
34
|
+
readonly DELETABLE: "可刪除";
|
|
35
|
+
readonly NODE_COUNT: "節點數";
|
|
36
|
+
readonly EDGE_COUNT: "邊數";
|
|
37
|
+
readonly CURRENT_NODES: "目前節點數";
|
|
38
|
+
readonly CURRENT_EDGES: "目前邊數";
|
|
39
|
+
readonly NEW_DATA_NODES: "新資料節點數";
|
|
40
|
+
readonly NEW_DATA_EDGES: "新資料邊數";
|
|
41
|
+
readonly LOADED_NODES: "載入節點數";
|
|
42
|
+
readonly LOADED_EDGES: "載入邊數";
|
|
43
|
+
readonly DETECT_INITIAL_NODES_CHANGE: "檢測到 initialNodes 變化,更新畫布資料:";
|
|
44
|
+
readonly NODE_ID_CHANGE: "節點ID變化";
|
|
45
|
+
readonly NEW_IDS: "新的";
|
|
46
|
+
readonly OLD_IDS: "舊的";
|
|
47
|
+
readonly DATA_LOADING_COMPLETE: "新資料載入完成:";
|
|
48
|
+
readonly CURRENT_MODE: "當前模式";
|
|
49
|
+
readonly NODE_TYPE_STATS: "節點類型統計";
|
|
50
|
+
readonly NODE_STATE_STATS: "節點狀態統計";
|
|
51
|
+
readonly HISTORY_REINITIALIZE: "歷史記錄已重新初始化:";
|
|
52
|
+
readonly OPERATION: "操作";
|
|
53
|
+
readonly FIRST_HISTORY_INIT: "首次初始化歷史記錄:";
|
|
54
|
+
readonly VIEW_MODE_ALL_NODES_DISABLED: "檢視模式下所有節點都不可選取和拖曳";
|
|
55
|
+
readonly BACKGROUND_NODE_RULE: "底圖節點只能在底圖模式下選取和拖曳";
|
|
56
|
+
readonly BACKGROUND_NODE_NO_DELETE: "底圖節點不可刪除";
|
|
57
|
+
readonly RECTANGLE_NODE_RULE: "矩形節點只能在圖層模式下選取和拖曳";
|
|
58
|
+
readonly RECTANGLE_NODE_NO_DELETE: "矩形節點不可刪除";
|
|
59
|
+
readonly PATH_NODE_RULE: "路徑節點只能在圖層模式下選取、拖曳和刪除";
|
|
60
|
+
readonly ENSURE_NEW_DATA_MANAGED: "重新初始化歷史記錄系統,確保新資料被正確管理";
|
|
61
|
+
readonly DELAY_ENSURE_STATE_UPDATE: "延遲確保 React 狀態更新完成";
|
|
62
|
+
readonly AVOID_DYNAMIC_LOADING_CONFLICT: "避免與動態載入時的明確初始化衝突";
|
|
63
|
+
readonly ONLY_INIT_WHEN_NO_HISTORY: "只有在沒有歷史記錄或節點/邊為空時才初始化";
|
|
64
|
+
readonly ON_NODES_CHANGE_TRIGGER: "onNodesChange 觸發:";
|
|
65
|
+
readonly DETECT_DATA_CHANGE: "檢測到資料變更:";
|
|
66
|
+
readonly CHANGED_NODES: "檢測到資料變更:";
|
|
67
|
+
readonly IMMEDIATE_RECORD_DATA_CHANGE: "立即記錄資料變更歷史:";
|
|
68
|
+
readonly DELAY_RECORD_COLOR_CHANGE: "延遲記錄顏色變更歷史 (800ms 後記錄,避免頻繁切換時產生過多記錄)";
|
|
69
|
+
readonly DELAY_TO_GET_UPDATED_STATE: "使用 setTimeout 確保能獲取到更新後的 nodes 狀態";
|
|
70
|
+
readonly RECORD_TEXT_EDIT_STATE: "保存文字編輯後的狀態:";
|
|
71
|
+
readonly RECORD_PATH_EDIT_STATE: "保存路徑點位編輯後的狀態:";
|
|
72
|
+
readonly UPDATED_POINTS_COUNT: "轉換後節點數:";
|
|
73
|
+
};
|
|
74
|
+
readonly TEST_MESSAGES: {
|
|
75
|
+
readonly SIMPLE_TEST_DATA: "簡單測試資料";
|
|
76
|
+
readonly COMPLEX_WAREHOUSE_DATA: "完整倉庫資料";
|
|
77
|
+
readonly LARGE_TEST_DATA: "大量測試資料";
|
|
78
|
+
readonly DEFAULT_TEST_DATA: "預設測試資料";
|
|
79
|
+
readonly LOAD_SUCCESS: "載入成功";
|
|
80
|
+
readonly LOAD_FAILED: "載入失敗";
|
|
81
|
+
readonly SAVE_SUCCESS: "儲存成功";
|
|
82
|
+
readonly OPEN_MODAL: "開啟 Modal";
|
|
83
|
+
readonly CLOSE_MODAL: "關閉 Modal";
|
|
84
|
+
readonly CLEAR_TEST_RECORD: "清空測試記錄";
|
|
85
|
+
readonly BREADCRUMB_OPEN_MODAL: "透過 breadcrumb 開啟 Modal";
|
|
86
|
+
readonly NODE_CLICK_EVENT: "節點點擊事件:";
|
|
87
|
+
readonly SAVE_EVENT_TRIGGER: "儲存事件觸發,接收到的資料:";
|
|
88
|
+
readonly BREADCRUMB_CLICK_EVENT: "Breadcrumb 點擊事件:";
|
|
89
|
+
readonly BREADCRUMB_LOAD_TRIGGER: "breadcrumb 觸發載入:";
|
|
90
|
+
readonly BREADCRUMB_LOAD_FAILED: "breadcrumb 載入失敗:";
|
|
91
|
+
readonly DATA_TYPE: "資料類型";
|
|
92
|
+
readonly CONVERTED_NODE_COUNT: "轉換後節點數";
|
|
93
|
+
readonly WAREHOUSE_ID: "warehouseId";
|
|
94
|
+
readonly INDEX: "index";
|
|
95
|
+
readonly WMS_MAP_MODAL_COMPREHENSIVE_TEST: "WMS Map Modal - 綜合功能測試";
|
|
96
|
+
readonly TEST_DATA_LOADING_DYNAMIC_SWITCH: "測試資料載入與動態切換";
|
|
97
|
+
readonly LOAD_DIFFERENT_TEST_DATA: "點擊下方按鈕載入不同的測試資料到 WmsMapModal 中,也可以在 Modal 開啟狀態下動態切換:";
|
|
98
|
+
readonly SIMPLE_TEST_DATA_BUTTON: "簡單測試資料";
|
|
99
|
+
readonly SIMPLE_TEST_DATA_DESC: "(1個背景 + 2個區域)";
|
|
100
|
+
readonly COMPLEX_WAREHOUSE_BUTTON: "完整倉庫資料";
|
|
101
|
+
readonly COMPLEX_WAREHOUSE_DESC: "(2個背景 + 8個區域)";
|
|
102
|
+
readonly LARGE_TEST_DATA_BUTTON: "大量測試資料";
|
|
103
|
+
readonly LARGE_TEST_DATA_DESC: "(5個背景 + 30個區域)";
|
|
104
|
+
readonly CLEAR_TEST_RECORD_BUTTON: "清空測試記錄";
|
|
105
|
+
readonly CLEAR_TEST_RECORD_DESC: "(重置記錄)";
|
|
106
|
+
readonly CURRENT_LOADED_DATA: "目前載入的資料";
|
|
107
|
+
readonly DATA_TYPE_LABEL: "資料類型:";
|
|
108
|
+
readonly TOTAL_NODES: "節點總數:";
|
|
109
|
+
readonly BACKGROUND_IMAGES: "背景圖片:";
|
|
110
|
+
readonly RECTANGLE_AREAS: "矩形區域:";
|
|
111
|
+
readonly POLYGON_AREAS: "多邊形區域:";
|
|
112
|
+
readonly TEST_INSTRUCTIONS: "測試說明";
|
|
113
|
+
readonly BUTTON_LOADING: "按鈕載入:";
|
|
114
|
+
readonly BUTTON_LOADING_DESC: "使用上方按鈕可手動載入不同的測試資料";
|
|
115
|
+
readonly DYNAMIC_LOADING: "動態載入:";
|
|
116
|
+
readonly DYNAMIC_LOADING_DESC: "Modal 開啟狀態下點擊不同按鈕,測試資料動態切換功能";
|
|
117
|
+
readonly HISTORY_RECORD_TEST: "歷史記錄測試:";
|
|
118
|
+
readonly HISTORY_RECORD_TEST_DESC: "載入資料後進行編輯操作,測試 Undo/Redo 功能是否正常";
|
|
119
|
+
readonly BREADCRUMB_LOADING: "Breadcrumb 載入:";
|
|
120
|
+
readonly BREADCRUMB_LOADING_DESC: "點擊 Modal 內的 breadcrumb 路徑可自動載入對應資料:";
|
|
121
|
+
readonly MODE_TEST: "模式測試:";
|
|
122
|
+
readonly MODE_TEST_DESC: "切換檢視模式/編輯模式,測試節點狀態規則";
|
|
123
|
+
readonly LAYER_TEST: "圖層測試:";
|
|
124
|
+
readonly LAYER_TEST_DESC: "在編輯模式下切換底圖/圖層模式,驗證節點互動限制";
|
|
125
|
+
readonly INTERACTION_TEST: "互動測試:";
|
|
126
|
+
readonly NODE_CLICK_OUTPUT: "點擊節點會在 console 輸出詳細資訊";
|
|
127
|
+
readonly SAVE_TRIGGER_ALERT: "儲存功能會觸發 alert 和 console 輸出";
|
|
128
|
+
readonly BREADCRUMB_AUTO_LOAD: "Breadcrumb 點擊會自動載入對應測試資料";
|
|
129
|
+
readonly STATE_RULE_VERIFICATION: "狀態規則驗證清單";
|
|
130
|
+
readonly VIEW_MODE_RULE: "檢視模式:";
|
|
131
|
+
readonly VIEW_MODE_RULE_DESC: "所有節點都不可選取、不可拖曳、不可刪除";
|
|
132
|
+
readonly EDIT_BACKGROUND_MODE_RULE: "編輯模式 - 底圖:";
|
|
133
|
+
readonly EDIT_BACKGROUND_MODE_DESC: "只有底圖節點(圖片)可選取拖曳,圖層節點不可互動";
|
|
134
|
+
readonly EDIT_LAYER_MODE_RULE: "編輯模式 - 圖層:";
|
|
135
|
+
readonly EDIT_LAYER_MODE_DESC: "只有圖層節點(矩形/路徑)可選取拖曳,底圖節點不可互動";
|
|
136
|
+
readonly DELETE_PERMISSION_RULE: "刪除權限:";
|
|
137
|
+
readonly DELETE_PERMISSION_DESC: "只有路徑節點在圖層模式下可刪除";
|
|
138
|
+
readonly EXPECTED_BEHAVIOR: "預期行為";
|
|
139
|
+
readonly NODE_STATE_APPLY_ON_LOAD: "每次載入新資料時,節點狀態會立即套用當前模式的規則";
|
|
140
|
+
readonly NODE_STATE_RECALC_ON_MODE_CHANGE: "模式切換時,所有節點狀態會重新計算";
|
|
141
|
+
readonly CONSOLE_DETAILED_STATS: "Console 會顯示詳細的節點狀態統計資訊";
|
|
142
|
+
readonly NO_DATA_MIXING: "動態載入不會出現新舊資料混合或狀態錯誤的情況";
|
|
143
|
+
readonly HISTORY_REINIT_ON_LOAD: "載入新資料後,歷史記錄系統會重新初始化並正常工作";
|
|
144
|
+
readonly EDIT_OPERATIONS_RECORDED: "編輯操作會被正確記錄,Undo/Redo 按鈕狀態正確更新";
|
|
145
|
+
readonly HISTORY_TEST_STEPS: "歷史記錄測試步驟";
|
|
146
|
+
readonly LOAD_DATA_STEP: "載入資料:";
|
|
147
|
+
readonly LOAD_DATA_STEP_DESC: "點擊載入按鈕開啟 Modal";
|
|
148
|
+
readonly PERFORM_EDIT_STEP: "進行編輯:";
|
|
149
|
+
readonly PERFORM_EDIT_STEP_DESC: "移動節點、編輯文字、繪製圖形等";
|
|
150
|
+
readonly TEST_UNDO_STEP: "測試 Undo:";
|
|
151
|
+
readonly TEST_UNDO_STEP_DESC: "使用 Cmd+Z 或工具列 Undo 按鈕";
|
|
152
|
+
readonly TEST_REDO_STEP: "測試 Redo:";
|
|
153
|
+
readonly TEST_REDO_STEP_DESC: "使用 Cmd+Shift+Z 或工具列 Redo 按鈕";
|
|
154
|
+
readonly DYNAMIC_SWITCH_STEP: "動態切換:";
|
|
155
|
+
readonly DYNAMIC_SWITCH_STEP_DESC: "在有編輯歷史時載入其他資料";
|
|
156
|
+
readonly REPEAT_TEST_STEP: "重複測試:";
|
|
157
|
+
readonly REPEAT_TEST_STEP_DESC: "確認新資料也有正確的歷史記錄功能";
|
|
158
|
+
readonly TEST_RECORD_HISTORY_TRACKING: "測試記錄與歷史追蹤";
|
|
159
|
+
readonly NO_TEST_RECORD: "尚無測試記錄 - 開始進行測試操作";
|
|
160
|
+
readonly TEST_RECORD_DESCRIPTION: "此記錄會自動追蹤您的測試操作,包括資料載入、Modal 開關、breadcrumb 點擊等,便於驗證歷史記錄功能是否正常。";
|
|
161
|
+
};
|
|
162
|
+
readonly ALERTS: {
|
|
163
|
+
readonly LOAD_SUCCESS_WITH_NODES: (typeName: string, nodeCount: number) => string;
|
|
164
|
+
readonly SAVE_SUCCESS_WITH_DATA: (backgroundCount: number, rangeCount: number) => string;
|
|
165
|
+
readonly LOAD_FAILED: (error: unknown) => string;
|
|
166
|
+
readonly INVALID_IMAGE_FORMAT: (fileName: string) => string;
|
|
167
|
+
};
|
|
168
|
+
readonly FORMATTERS: {
|
|
169
|
+
readonly NODES_UNIT: "個節點";
|
|
170
|
+
readonly BACKGROUND_COUNT: (count: number) => string;
|
|
171
|
+
readonly RANGE_COUNT: (count: number) => string;
|
|
172
|
+
readonly WAREHOUSE_ID_FORMAT: (id: string) => string;
|
|
173
|
+
readonly INDEX_FORMAT: (index: number) => string;
|
|
174
|
+
readonly CURRENT_INDICATOR: "(當前)";
|
|
175
|
+
};
|
|
176
|
+
};
|
|
177
|
+
export type TextMappingKey = keyof typeof TEXT_MAPPINGS;
|
|
178
|
+
export type DebugKey = keyof typeof TEXT_MAPPINGS.DEBUG;
|
|
179
|
+
export type OperationKey = keyof typeof TEXT_MAPPINGS.OPERATIONS;
|
|
180
|
+
export type MessageKey = keyof typeof TEXT_MAPPINGS.MESSAGES;
|
|
181
|
+
export type TestMessageKey = keyof typeof TEXT_MAPPINGS.TEST_MESSAGES;
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var React = require('react');
|
|
5
|
+
var react = require('@xyflow/react');
|
|
6
|
+
var typings = require('../typings.cjs');
|
|
7
|
+
|
|
8
|
+
const useContextMenu = ({ id, editMode, isEditable, nodeType })=>{
|
|
9
|
+
const { setNodes, getNodes } = react.useReactFlow();
|
|
10
|
+
// Calculate arrange states based on current node position
|
|
11
|
+
const getArrangeStates = React.useCallback(()=>{
|
|
12
|
+
const nodes = getNodes();
|
|
13
|
+
const currentNode = nodes.find((node)=>node.id === id);
|
|
14
|
+
if (!currentNode) {
|
|
15
|
+
return {
|
|
16
|
+
canBringToFront: false,
|
|
17
|
+
canBringForward: false,
|
|
18
|
+
canSendBackward: false,
|
|
19
|
+
canSendToBack: false
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
const currentZ = currentNode.zIndex || 0;
|
|
23
|
+
const allZIndexes = nodes.map((n)=>n.zIndex || 0).sort((a, b)=>a - b);
|
|
24
|
+
const maxZ = Math.max(...allZIndexes);
|
|
25
|
+
const minZ = Math.min(...allZIndexes);
|
|
26
|
+
// Check if there are nodes above (higher zIndex)
|
|
27
|
+
const nodesAbove = nodes.filter((n)=>(n.zIndex || 0) > currentZ);
|
|
28
|
+
const nodesBelow = nodes.filter((n)=>(n.zIndex || 0) < currentZ);
|
|
29
|
+
return {
|
|
30
|
+
canBringToFront: currentZ < maxZ,
|
|
31
|
+
canBringForward: nodesAbove.length > 0,
|
|
32
|
+
canSendBackward: nodesBelow.length > 0,
|
|
33
|
+
canSendToBack: currentZ > minZ
|
|
34
|
+
};
|
|
35
|
+
}, [
|
|
36
|
+
id,
|
|
37
|
+
getNodes
|
|
38
|
+
]);
|
|
39
|
+
const [contextMenu, setContextMenu] = React.useState({
|
|
40
|
+
visible: false,
|
|
41
|
+
x: 0,
|
|
42
|
+
y: 0
|
|
43
|
+
});
|
|
44
|
+
// Handle right click for context menu
|
|
45
|
+
const handleContextMenu = React.useCallback((event)=>{
|
|
46
|
+
// For image nodes, show context menu in BACKGROUND mode
|
|
47
|
+
// For rectangle/path nodes, show context menu in LAYER mode
|
|
48
|
+
const shouldShowMenu = nodeType === 'imageNode' && editMode === typings.EditMode.BACKGROUND || (nodeType === 'rectangleNode' || nodeType === 'pathNode') && editMode === typings.EditMode.LAYER && isEditable;
|
|
49
|
+
if (!shouldShowMenu) return;
|
|
50
|
+
event.preventDefault();
|
|
51
|
+
event.stopPropagation();
|
|
52
|
+
setContextMenu({
|
|
53
|
+
visible: true,
|
|
54
|
+
x: event.clientX,
|
|
55
|
+
y: event.clientY
|
|
56
|
+
});
|
|
57
|
+
}, [
|
|
58
|
+
isEditable,
|
|
59
|
+
editMode,
|
|
60
|
+
nodeType
|
|
61
|
+
]);
|
|
62
|
+
// Handle context menu actions
|
|
63
|
+
const handleCloseContextMenu = React.useCallback(()=>{
|
|
64
|
+
setContextMenu({
|
|
65
|
+
visible: false,
|
|
66
|
+
x: 0,
|
|
67
|
+
y: 0
|
|
68
|
+
});
|
|
69
|
+
}, []);
|
|
70
|
+
const handleDelete = React.useCallback(()=>{
|
|
71
|
+
setNodes((nodes)=>nodes.filter((node)=>node.id !== id));
|
|
72
|
+
handleCloseContextMenu();
|
|
73
|
+
}, [
|
|
74
|
+
id,
|
|
75
|
+
setNodes,
|
|
76
|
+
handleCloseContextMenu
|
|
77
|
+
]);
|
|
78
|
+
// Arrange functionality
|
|
79
|
+
const handleBringToFront = React.useCallback(()=>{
|
|
80
|
+
console.log('Bringing to front:', id);
|
|
81
|
+
setNodes((nodes)=>{
|
|
82
|
+
console.log('Current nodes:', nodes.map((n)=>n.id));
|
|
83
|
+
const nodeToMove = nodes.find((node)=>node.id === id);
|
|
84
|
+
if (!nodeToMove) {
|
|
85
|
+
console.log('Node not found');
|
|
86
|
+
return nodes;
|
|
87
|
+
}
|
|
88
|
+
const otherNodes = nodes.filter((node)=>node.id !== id);
|
|
89
|
+
// Calculate highest zIndex
|
|
90
|
+
const maxZIndex = Math.max(...nodes.map((n)=>n.zIndex || 0), 0);
|
|
91
|
+
const updatedNode = {
|
|
92
|
+
...nodeToMove,
|
|
93
|
+
zIndex: maxZIndex + 1
|
|
94
|
+
};
|
|
95
|
+
const newNodes = [
|
|
96
|
+
...otherNodes,
|
|
97
|
+
updatedNode
|
|
98
|
+
]; // Move to end (front) with higher zIndex
|
|
99
|
+
console.log('New nodes order:', newNodes.map((n)=>`${n.id}(z:${n.zIndex || 0})`));
|
|
100
|
+
return newNodes;
|
|
101
|
+
});
|
|
102
|
+
handleCloseContextMenu();
|
|
103
|
+
}, [
|
|
104
|
+
id,
|
|
105
|
+
setNodes,
|
|
106
|
+
handleCloseContextMenu
|
|
107
|
+
]);
|
|
108
|
+
const handleBringForward = React.useCallback(()=>{
|
|
109
|
+
console.log('Bringing forward:', id);
|
|
110
|
+
setNodes((nodes)=>{
|
|
111
|
+
const currentNode = nodes.find((node)=>node.id === id);
|
|
112
|
+
if (!currentNode) {
|
|
113
|
+
console.log('Node not found');
|
|
114
|
+
return nodes;
|
|
115
|
+
}
|
|
116
|
+
// Find the node immediately in front (higher zIndex)
|
|
117
|
+
const currentZ = currentNode.zIndex || 0;
|
|
118
|
+
const nodesAbove = nodes.filter((n)=>(n.zIndex || 0) > currentZ);
|
|
119
|
+
if (nodesAbove.length === 0) {
|
|
120
|
+
console.log('Already at front');
|
|
121
|
+
return nodes;
|
|
122
|
+
}
|
|
123
|
+
// Find the node with the lowest zIndex above current node
|
|
124
|
+
const nextNode = nodesAbove.reduce((min, node)=>(node.zIndex || 0) < (min.zIndex || 0) ? node : min);
|
|
125
|
+
// Swap zIndex values
|
|
126
|
+
return nodes.map((node)=>{
|
|
127
|
+
if (node.id === id) {
|
|
128
|
+
return {
|
|
129
|
+
...node,
|
|
130
|
+
zIndex: nextNode.zIndex
|
|
131
|
+
};
|
|
132
|
+
} else if (node.id === nextNode.id) {
|
|
133
|
+
return {
|
|
134
|
+
...node,
|
|
135
|
+
zIndex: currentZ
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
return node;
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
handleCloseContextMenu();
|
|
142
|
+
}, [
|
|
143
|
+
id,
|
|
144
|
+
setNodes,
|
|
145
|
+
handleCloseContextMenu
|
|
146
|
+
]);
|
|
147
|
+
const handleSendBackward = React.useCallback(()=>{
|
|
148
|
+
console.log('Sending backward:', id);
|
|
149
|
+
setNodes((nodes)=>{
|
|
150
|
+
const currentNode = nodes.find((node)=>node.id === id);
|
|
151
|
+
if (!currentNode) {
|
|
152
|
+
console.log('Node not found');
|
|
153
|
+
return nodes;
|
|
154
|
+
}
|
|
155
|
+
// Find the node immediately behind (lower zIndex)
|
|
156
|
+
const currentZ = currentNode.zIndex || 0;
|
|
157
|
+
const nodesBelow = nodes.filter((n)=>(n.zIndex || 0) < currentZ);
|
|
158
|
+
if (nodesBelow.length === 0) {
|
|
159
|
+
console.log('Already at back');
|
|
160
|
+
return nodes;
|
|
161
|
+
}
|
|
162
|
+
// Find the node with the highest zIndex below current node
|
|
163
|
+
const prevNode = nodesBelow.reduce((max, node)=>(node.zIndex || 0) > (max.zIndex || 0) ? node : max);
|
|
164
|
+
// Swap zIndex values
|
|
165
|
+
return nodes.map((node)=>{
|
|
166
|
+
if (node.id === id) {
|
|
167
|
+
return {
|
|
168
|
+
...node,
|
|
169
|
+
zIndex: prevNode.zIndex
|
|
170
|
+
};
|
|
171
|
+
} else if (node.id === prevNode.id) {
|
|
172
|
+
return {
|
|
173
|
+
...node,
|
|
174
|
+
zIndex: currentZ
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
return node;
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
handleCloseContextMenu();
|
|
181
|
+
}, [
|
|
182
|
+
id,
|
|
183
|
+
setNodes,
|
|
184
|
+
handleCloseContextMenu
|
|
185
|
+
]);
|
|
186
|
+
const handleSendToBack = React.useCallback(()=>{
|
|
187
|
+
console.log('Sending to back:', id);
|
|
188
|
+
setNodes((nodes)=>{
|
|
189
|
+
const nodeToMove = nodes.find((node)=>node.id === id);
|
|
190
|
+
if (!nodeToMove) {
|
|
191
|
+
console.log('Node not found');
|
|
192
|
+
return nodes;
|
|
193
|
+
}
|
|
194
|
+
const otherNodes = nodes.filter((node)=>node.id !== id);
|
|
195
|
+
// Calculate lowest zIndex
|
|
196
|
+
const minZIndex = Math.min(...nodes.map((n)=>n.zIndex || 0), 0);
|
|
197
|
+
const updatedNode = {
|
|
198
|
+
...nodeToMove,
|
|
199
|
+
zIndex: minZIndex - 1
|
|
200
|
+
};
|
|
201
|
+
const newNodes = [
|
|
202
|
+
updatedNode,
|
|
203
|
+
...otherNodes
|
|
204
|
+
]; // Move to beginning (back) with lower zIndex
|
|
205
|
+
console.log('New nodes order:', newNodes.map((n)=>`${n.id}(z:${n.zIndex || 0})`));
|
|
206
|
+
return newNodes;
|
|
207
|
+
});
|
|
208
|
+
handleCloseContextMenu();
|
|
209
|
+
}, [
|
|
210
|
+
id,
|
|
211
|
+
setNodes,
|
|
212
|
+
handleCloseContextMenu
|
|
213
|
+
]);
|
|
214
|
+
return {
|
|
215
|
+
contextMenu,
|
|
216
|
+
handleContextMenu,
|
|
217
|
+
handleCloseContextMenu,
|
|
218
|
+
handleDelete,
|
|
219
|
+
arrangeActions: {
|
|
220
|
+
onBringToFront: handleBringToFront,
|
|
221
|
+
onBringForward: handleBringForward,
|
|
222
|
+
onSendBackward: handleSendBackward,
|
|
223
|
+
onSendToBack: handleSendToBack
|
|
224
|
+
},
|
|
225
|
+
arrangeStates: getArrangeStates(),
|
|
226
|
+
getNodes,
|
|
227
|
+
setNodes
|
|
228
|
+
};
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
exports.useContextMenu = useContextMenu;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { Node } from '@xyflow/react';
|
|
3
|
+
import { EditMode } from '../typings.cjs';
|
|
4
|
+
interface UseContextMenuProps {
|
|
5
|
+
id: string;
|
|
6
|
+
editMode: EditMode;
|
|
7
|
+
isEditable: boolean;
|
|
8
|
+
nodeType?: 'rectangleNode' | 'pathNode' | 'imageNode';
|
|
9
|
+
}
|
|
10
|
+
interface ArrangeStates {
|
|
11
|
+
canBringToFront: boolean;
|
|
12
|
+
canBringForward: boolean;
|
|
13
|
+
canSendBackward: boolean;
|
|
14
|
+
canSendToBack: boolean;
|
|
15
|
+
}
|
|
16
|
+
interface ContextMenuState {
|
|
17
|
+
visible: boolean;
|
|
18
|
+
x: number;
|
|
19
|
+
y: number;
|
|
20
|
+
}
|
|
21
|
+
interface UseContextMenuReturn {
|
|
22
|
+
contextMenu: ContextMenuState;
|
|
23
|
+
handleContextMenu: (event: React.MouseEvent) => void;
|
|
24
|
+
handleCloseContextMenu: () => void;
|
|
25
|
+
handleDelete: () => void;
|
|
26
|
+
arrangeActions: {
|
|
27
|
+
onBringToFront: () => void;
|
|
28
|
+
onBringForward: () => void;
|
|
29
|
+
onSendBackward: () => void;
|
|
30
|
+
onSendToBack: () => void;
|
|
31
|
+
};
|
|
32
|
+
arrangeStates: ArrangeStates;
|
|
33
|
+
getNodes: () => Node[];
|
|
34
|
+
setNodes: (nodes: Node[] | ((nodes: Node[]) => Node[])) => void;
|
|
35
|
+
}
|
|
36
|
+
export declare const useContextMenu: ({ id, editMode, isEditable, nodeType }: UseContextMenuProps) => UseContextMenuReturn;
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var React = require('react');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 直接狀態歷史系統
|
|
8
|
+
*
|
|
9
|
+
* 核心概念:
|
|
10
|
+
* - 直接記錄 nodes 和 edges 的完整狀態
|
|
11
|
+
* - 每個用戶操作後保存一份狀態快照
|
|
12
|
+
* - undo/redo 就是直接設置 nodes 和 edges
|
|
13
|
+
* - 簡單、直接、可靠
|
|
14
|
+
*/ const useDirectStateHistory = ({ maxHistorySize = 50, debugMode = false } = {})=>{
|
|
15
|
+
const [history, setHistory] = React.useState([]);
|
|
16
|
+
const [currentIndex, setCurrentIndex] = React.useState(-1);
|
|
17
|
+
const isRestoringRef = React.useRef(false);
|
|
18
|
+
const canUndo = currentIndex > 0;
|
|
19
|
+
const canRedo = currentIndex < history.length - 1;
|
|
20
|
+
// 初始化歷史記錄
|
|
21
|
+
const initializeHistory = React.useCallback((initialNodes, initialEdges, editMode)=>{
|
|
22
|
+
const initialState = {
|
|
23
|
+
nodes: JSON.parse(JSON.stringify(initialNodes)),
|
|
24
|
+
edges: JSON.parse(JSON.stringify(initialEdges)),
|
|
25
|
+
editMode,
|
|
26
|
+
operation: 'init',
|
|
27
|
+
timestamp: Date.now()
|
|
28
|
+
};
|
|
29
|
+
setHistory([
|
|
30
|
+
initialState
|
|
31
|
+
]);
|
|
32
|
+
setCurrentIndex(0);
|
|
33
|
+
if (debugMode) {
|
|
34
|
+
console.log('🎯 直接狀態歷史系統初始化:', {
|
|
35
|
+
nodes: initialNodes.length,
|
|
36
|
+
edges: initialEdges.length,
|
|
37
|
+
editMode
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}, [
|
|
41
|
+
debugMode
|
|
42
|
+
]);
|
|
43
|
+
// 保存狀態快照
|
|
44
|
+
const saveState = React.useCallback((nodes, edges, operation, editMode)=>{
|
|
45
|
+
if (isRestoringRef.current) {
|
|
46
|
+
if (debugMode) {
|
|
47
|
+
console.log('🚫 跳過保存 - 正在執行 undo/redo');
|
|
48
|
+
}
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const newState = {
|
|
52
|
+
nodes: JSON.parse(JSON.stringify(nodes)),
|
|
53
|
+
edges: JSON.parse(JSON.stringify(edges)),
|
|
54
|
+
editMode,
|
|
55
|
+
operation,
|
|
56
|
+
timestamp: Date.now()
|
|
57
|
+
};
|
|
58
|
+
setHistory((prevHistory)=>{
|
|
59
|
+
// 如果當前不在最後位置,截斷後續歷史
|
|
60
|
+
const truncatedHistory = currentIndex < prevHistory.length - 1 ? prevHistory.slice(0, currentIndex + 1) : prevHistory;
|
|
61
|
+
// 添加新狀態
|
|
62
|
+
const historyWithNewState = [
|
|
63
|
+
...truncatedHistory,
|
|
64
|
+
newState
|
|
65
|
+
];
|
|
66
|
+
// 限制歷史大小
|
|
67
|
+
const finalHistory = historyWithNewState.length > maxHistorySize ? (()=>{
|
|
68
|
+
const removeCount = historyWithNewState.length - maxHistorySize;
|
|
69
|
+
const trimmedHistory = historyWithNewState.slice(removeCount);
|
|
70
|
+
setCurrentIndex(trimmedHistory.length - 1);
|
|
71
|
+
return trimmedHistory;
|
|
72
|
+
})() : (()=>{
|
|
73
|
+
setCurrentIndex(historyWithNewState.length - 1);
|
|
74
|
+
return historyWithNewState;
|
|
75
|
+
})();
|
|
76
|
+
if (debugMode) {
|
|
77
|
+
console.log(`📸 保存狀態 [${operation}]:`, {
|
|
78
|
+
nodes: nodes.length,
|
|
79
|
+
edges: edges.length,
|
|
80
|
+
editMode,
|
|
81
|
+
historyIndex: finalHistory.length - 1,
|
|
82
|
+
totalHistory: finalHistory.length
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
return finalHistory;
|
|
86
|
+
});
|
|
87
|
+
}, [
|
|
88
|
+
currentIndex,
|
|
89
|
+
maxHistorySize,
|
|
90
|
+
debugMode
|
|
91
|
+
]);
|
|
92
|
+
// Undo 操作
|
|
93
|
+
const undo = React.useCallback(()=>{
|
|
94
|
+
if (!canUndo) {
|
|
95
|
+
if (debugMode) {
|
|
96
|
+
console.log('⚠️ 無法 undo - 已在歷史開始');
|
|
97
|
+
}
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
const targetIndex = currentIndex - 1;
|
|
101
|
+
const targetState = history[targetIndex];
|
|
102
|
+
if (!targetState) return null;
|
|
103
|
+
isRestoringRef.current = true;
|
|
104
|
+
setCurrentIndex(targetIndex);
|
|
105
|
+
if (debugMode) {
|
|
106
|
+
console.log(`↶ Undo 到 [${targetState.operation}]:`, {
|
|
107
|
+
fromIndex: currentIndex,
|
|
108
|
+
toIndex: targetIndex,
|
|
109
|
+
nodes: targetState.nodes.length,
|
|
110
|
+
edges: targetState.edges.length,
|
|
111
|
+
editMode: targetState.editMode
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
// 下一個事件循環解除恢復標記
|
|
115
|
+
setTimeout(()=>{
|
|
116
|
+
isRestoringRef.current = false;
|
|
117
|
+
}, 0);
|
|
118
|
+
return {
|
|
119
|
+
nodes: targetState.nodes,
|
|
120
|
+
edges: targetState.edges,
|
|
121
|
+
editMode: targetState.editMode
|
|
122
|
+
};
|
|
123
|
+
}, [
|
|
124
|
+
canUndo,
|
|
125
|
+
currentIndex,
|
|
126
|
+
history,
|
|
127
|
+
debugMode
|
|
128
|
+
]);
|
|
129
|
+
// Redo 操作
|
|
130
|
+
const redo = React.useCallback(()=>{
|
|
131
|
+
if (!canRedo) {
|
|
132
|
+
if (debugMode) {
|
|
133
|
+
console.log('⚠️ 無法 redo - 已在歷史末端');
|
|
134
|
+
}
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
const targetIndex = currentIndex + 1;
|
|
138
|
+
const targetState = history[targetIndex];
|
|
139
|
+
if (!targetState) return null;
|
|
140
|
+
isRestoringRef.current = true;
|
|
141
|
+
setCurrentIndex(targetIndex);
|
|
142
|
+
if (debugMode) {
|
|
143
|
+
console.log(`↷ Redo 到 [${targetState.operation}]:`, {
|
|
144
|
+
fromIndex: currentIndex,
|
|
145
|
+
toIndex: targetIndex,
|
|
146
|
+
nodes: targetState.nodes.length,
|
|
147
|
+
edges: targetState.edges.length,
|
|
148
|
+
editMode: targetState.editMode
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
// 下一個事件循環解除恢復標記
|
|
152
|
+
setTimeout(()=>{
|
|
153
|
+
isRestoringRef.current = false;
|
|
154
|
+
}, 0);
|
|
155
|
+
return {
|
|
156
|
+
nodes: targetState.nodes,
|
|
157
|
+
edges: targetState.edges,
|
|
158
|
+
editMode: targetState.editMode
|
|
159
|
+
};
|
|
160
|
+
}, [
|
|
161
|
+
canRedo,
|
|
162
|
+
currentIndex,
|
|
163
|
+
history,
|
|
164
|
+
debugMode
|
|
165
|
+
]);
|
|
166
|
+
// 獲取歷史摘要
|
|
167
|
+
const getHistorySummary = React.useCallback(()=>{
|
|
168
|
+
return {
|
|
169
|
+
canUndo,
|
|
170
|
+
canRedo,
|
|
171
|
+
historyLength: history.length,
|
|
172
|
+
currentIndex,
|
|
173
|
+
currentOperation: currentIndex >= 0 ? history[currentIndex]?.operation : undefined,
|
|
174
|
+
operations: debugMode ? history.map((h, index)=>({
|
|
175
|
+
index,
|
|
176
|
+
operation: h.operation,
|
|
177
|
+
nodes: h.nodes.length,
|
|
178
|
+
edges: h.edges.length,
|
|
179
|
+
isCurrent: index === currentIndex
|
|
180
|
+
})) : undefined
|
|
181
|
+
};
|
|
182
|
+
}, [
|
|
183
|
+
canUndo,
|
|
184
|
+
canRedo,
|
|
185
|
+
history,
|
|
186
|
+
currentIndex,
|
|
187
|
+
debugMode
|
|
188
|
+
]);
|
|
189
|
+
// 清除歷史
|
|
190
|
+
const clearHistory = React.useCallback(()=>{
|
|
191
|
+
setHistory([]);
|
|
192
|
+
setCurrentIndex(-1);
|
|
193
|
+
if (debugMode) {
|
|
194
|
+
console.log('🗑️ 清除歷史記錄');
|
|
195
|
+
}
|
|
196
|
+
}, [
|
|
197
|
+
debugMode
|
|
198
|
+
]);
|
|
199
|
+
return {
|
|
200
|
+
// 核心功能
|
|
201
|
+
saveState,
|
|
202
|
+
undo,
|
|
203
|
+
redo,
|
|
204
|
+
// 狀態查詢
|
|
205
|
+
canUndo,
|
|
206
|
+
canRedo,
|
|
207
|
+
// 工具函數
|
|
208
|
+
initializeHistory,
|
|
209
|
+
clearHistory,
|
|
210
|
+
getHistorySummary,
|
|
211
|
+
// 調試信息
|
|
212
|
+
history: debugMode ? history : undefined,
|
|
213
|
+
currentIndex: debugMode ? currentIndex : undefined
|
|
214
|
+
};
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
exports.useDirectStateHistory = useDirectStateHistory;
|