@rytass/wms-map-react-components 0.2.0 → 0.3.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.
Files changed (41) hide show
  1. package/CHANGELOG.md +181 -0
  2. package/README.md +30 -0
  3. package/components/canvas/react-flow-canvas.cjs +7 -1
  4. package/components/canvas/react-flow-canvas.d.cts +2 -0
  5. package/components/canvas/react-flow-canvas.d.ts +2 -0
  6. package/components/canvas/react-flow-canvas.js +7 -1
  7. package/components/modal/wms-map-content.cjs +29 -2
  8. package/components/modal/wms-map-content.js +30 -3
  9. package/components/modal/wms-map-modal-container.cjs +6 -4
  10. package/components/modal/wms-map-modal-container.d.cts +1 -1
  11. package/components/modal/wms-map-modal-container.d.ts +1 -1
  12. package/components/modal/wms-map-modal-container.js +6 -4
  13. package/components/nodes/image-node.cjs +0 -2
  14. package/components/nodes/image-node.js +0 -2
  15. package/components/nodes/path-node.cjs +5 -4
  16. package/components/nodes/path-node.js +6 -5
  17. package/components/nodes/rectangle-node.cjs +5 -4
  18. package/components/nodes/rectangle-node.js +6 -5
  19. package/components/preview/wms-map-preview.cjs +201 -0
  20. package/components/preview/wms-map-preview.d.cts +11 -0
  21. package/components/preview/wms-map-preview.d.ts +11 -0
  22. package/components/preview/wms-map-preview.js +199 -0
  23. package/hooks/use-fit-view-when-ready.cjs +74 -0
  24. package/hooks/use-fit-view-when-ready.d.cts +18 -0
  25. package/hooks/use-fit-view-when-ready.d.ts +18 -0
  26. package/hooks/use-fit-view-when-ready.js +72 -0
  27. package/index.cjs +2 -0
  28. package/index.d.cts +1 -0
  29. package/index.d.ts +1 -0
  30. package/index.js +1 -0
  31. package/package.json +1 -1
  32. package/types/component-props.types.d.cts +31 -0
  33. package/types/component-props.types.d.ts +31 -0
  34. package/utils/fit-trigger.cjs +41 -0
  35. package/utils/fit-trigger.d.cts +37 -0
  36. package/utils/fit-trigger.d.ts +37 -0
  37. package/utils/fit-trigger.js +36 -0
  38. package/utils/preview-highlight.cjs +38 -0
  39. package/utils/preview-highlight.d.cts +12 -0
  40. package/utils/preview-highlight.d.ts +12 -0
  41. package/utils/preview-highlight.js +34 -0
@@ -10,13 +10,14 @@ var useContextMenu = require('../../hooks/use-context-menu.cjs');
10
10
  var useTextEditing = require('../../hooks/use-text-editing.cjs');
11
11
  var nodeOperations = require('../../utils/node-operations.cjs');
12
12
  var colorUtils = require('../../utils/color-utils.cjs');
13
+ var previewHighlight = require('../../utils/preview-highlight.cjs');
13
14
  var contextMenu = require('../ui/context-menu.cjs');
14
15
  var rectangleNode_module = require('./rectangle-node.module.scss.cjs');
15
16
 
16
17
  const RectangleNode = ({ data, selected, id, editMode, viewMode, onTextEditComplete, isHovered = false })=>{
17
18
  const { setNodes } = react.useReactFlow();
18
19
  const updateNodeInternals = react.useUpdateNodeInternals();
19
- const { width = index.DEFAULT_RECTANGLE_WIDTH, height = index.DEFAULT_RECTANGLE_HEIGHT, color = index.DEFAULT_RECTANGLE_COLOR, label = index.DEFAULT_RECTANGLE_LABEL } = data;
20
+ const { width = index.DEFAULT_RECTANGLE_WIDTH, height = index.DEFAULT_RECTANGLE_HEIGHT, color = index.DEFAULT_RECTANGLE_COLOR, label = index.DEFAULT_RECTANGLE_LABEL, previewHighlighted = false, previewDimmed = false } = data;
20
21
  const [currentSize, setCurrentSize] = React.useState({
21
22
  width,
22
23
  height
@@ -179,9 +180,9 @@ const RectangleNode = ({ data, selected, id, editMode, viewMode, onTextEditCompl
179
180
  style: {
180
181
  width: `${currentSize.width}px`,
181
182
  height: `${currentSize.height}px`,
182
- backgroundColor: `${displayColor}33`,
183
- opacity: opacity,
184
- border: selected && isEditable ? '2px solid #3b82f6' : `2px solid ${displayColor}`,
183
+ backgroundColor: previewHighlighted ? `${displayColor}66` : `${displayColor}33`,
184
+ opacity: previewDimmed ? previewHighlight.PREVIEW_DIMMED_OPACITY : opacity,
185
+ border: selected && isEditable ? '2px solid #3b82f6' : `${previewHighlighted ? previewHighlight.PREVIEW_HIGHLIGHT_STROKE_WIDTH : 2}px solid ${displayColor}`,
185
186
  borderRadius: '4px',
186
187
  display: 'flex',
187
188
  alignItems: 'center',
@@ -3,18 +3,19 @@ import { jsxs, jsx } from 'react/jsx-runtime';
3
3
  import { useState, useEffect, useCallback } from 'react';
4
4
  import { useReactFlow, useUpdateNodeInternals, NodeResizer } from '@xyflow/react';
5
5
  import { ViewMode, EditMode } from '../../typings.js';
6
- import { DEFAULT_RECTANGLE_LABEL, DEFAULT_RECTANGLE_WIDTH, DEFAULT_RECTANGLE_HEIGHT, DEFAULT_RECTANGLE_COLOR, ACTIVE_OPACITY, RECTANGLE_INACTIVE_OPACITY, MIN_RESIZE_HEIGHT, MIN_RESIZE_WIDTH } from '../../constants/index.js';
6
+ import { DEFAULT_RECTANGLE_WIDTH, DEFAULT_RECTANGLE_HEIGHT, DEFAULT_RECTANGLE_COLOR, DEFAULT_RECTANGLE_LABEL, ACTIVE_OPACITY, RECTANGLE_INACTIVE_OPACITY, MIN_RESIZE_HEIGHT, MIN_RESIZE_WIDTH } from '../../constants/index.js';
7
7
  import { useContextMenu } from '../../hooks/use-context-menu.js';
8
8
  import { useTextEditing } from '../../hooks/use-text-editing.js';
9
9
  import { createRectangleCopy } from '../../utils/node-operations.js';
10
10
  import { createHoverColor } from '../../utils/color-utils.js';
11
+ import { PREVIEW_HIGHLIGHT_STROKE_WIDTH, PREVIEW_DIMMED_OPACITY } from '../../utils/preview-highlight.js';
11
12
  import ContextMenu from '../ui/context-menu.js';
12
13
  import styles from './rectangle-node.module.scss.js';
13
14
 
14
15
  const RectangleNode = ({ data, selected, id, editMode, viewMode, onTextEditComplete, isHovered = false })=>{
15
16
  const { setNodes } = useReactFlow();
16
17
  const updateNodeInternals = useUpdateNodeInternals();
17
- const { width = DEFAULT_RECTANGLE_WIDTH, height = DEFAULT_RECTANGLE_HEIGHT, color = DEFAULT_RECTANGLE_COLOR, label = DEFAULT_RECTANGLE_LABEL } = data;
18
+ const { width = DEFAULT_RECTANGLE_WIDTH, height = DEFAULT_RECTANGLE_HEIGHT, color = DEFAULT_RECTANGLE_COLOR, label = DEFAULT_RECTANGLE_LABEL, previewHighlighted = false, previewDimmed = false } = data;
18
19
  const [currentSize, setCurrentSize] = useState({
19
20
  width,
20
21
  height
@@ -177,9 +178,9 @@ const RectangleNode = ({ data, selected, id, editMode, viewMode, onTextEditCompl
177
178
  style: {
178
179
  width: `${currentSize.width}px`,
179
180
  height: `${currentSize.height}px`,
180
- backgroundColor: `${displayColor}33`,
181
- opacity: opacity,
182
- border: selected && isEditable ? '2px solid #3b82f6' : `2px solid ${displayColor}`,
181
+ backgroundColor: previewHighlighted ? `${displayColor}66` : `${displayColor}33`,
182
+ opacity: previewDimmed ? PREVIEW_DIMMED_OPACITY : opacity,
183
+ border: selected && isEditable ? '2px solid #3b82f6' : `${previewHighlighted ? PREVIEW_HIGHLIGHT_STROKE_WIDTH : 2}px solid ${displayColor}`,
183
184
  borderRadius: '4px',
184
185
  display: 'flex',
185
186
  alignItems: 'center',
@@ -0,0 +1,201 @@
1
+ "use client";
2
+ 'use strict';
3
+
4
+ var jsxRuntime = require('react/jsx-runtime');
5
+ var React = require('react');
6
+ var react = require('@xyflow/react');
7
+ var typings = require('../../typings.cjs');
8
+ var imageNode = require('../nodes/image-node.cjs');
9
+ var rectangleNode = require('../nodes/rectangle-node.cjs');
10
+ var pathNode = require('../nodes/path-node.cjs');
11
+ var useFitViewWhenReady = require('../../hooks/use-fit-view-when-ready.cjs');
12
+ var previewHighlight = require('../../utils/preview-highlight.cjs');
13
+ var fitTrigger = require('../../utils/fit-trigger.cjs');
14
+
15
+ /** 與 WMSMapModal 的檢視模式一致:VIEW 不可編輯,BACKGROUND 讓底圖全不透明。 */ const PREVIEW_EDIT_MODE = typings.EditMode.BACKGROUND;
16
+ const PREVIEW_VIEW_MODE = typings.ViewMode.VIEW;
17
+ /** 模組層級常數,維持穩定參照,React Flow 才不會每次 render 重建節點。 */ const PREVIEW_NODE_TYPES = {
18
+ imageNode: (props)=>/*#__PURE__*/ jsxRuntime.jsx(imageNode, {
19
+ ...props,
20
+ editMode: PREVIEW_EDIT_MODE,
21
+ viewMode: PREVIEW_VIEW_MODE
22
+ }),
23
+ rectangleNode: (props)=>/*#__PURE__*/ jsxRuntime.jsx(rectangleNode, {
24
+ ...props,
25
+ editMode: PREVIEW_EDIT_MODE,
26
+ viewMode: PREVIEW_VIEW_MODE
27
+ }),
28
+ pathNode: (props)=>/*#__PURE__*/ jsxRuntime.jsx(pathNode, {
29
+ ...props,
30
+ editMode: PREVIEW_EDIT_MODE,
31
+ viewMode: PREVIEW_VIEW_MODE
32
+ })
33
+ };
34
+ /** 預覽容器可能很窄(例如 200px),minZoom 需夠低,否則 fit 會被夾住而框不住整張圖。 */ const PREVIEW_MIN_ZOOM = 0.01;
35
+ const PREVIEW_MAX_ZOOM = 4;
36
+ const NO_NODES_DELETE_KEY = null;
37
+ const getFileName = (node)=>{
38
+ const { fileName } = node.data;
39
+ return node.type === 'imageNode' && typeof fileName === 'string' && fileName.length > 0 ? fileName : null;
40
+ };
41
+ /** 以 getFilenameFQDN 解析背景圖網址;未提供時沿用 data.imageUrl。 */ const useResolvedImageUrls = (nodes, getFilenameFQDN)=>{
42
+ const [resolved, setResolved] = React.useState({});
43
+ const fileNamesKey = React.useMemo(()=>Array.from(new Set(nodes.map(getFileName).filter((name)=>name !== null))).sort().join('\u0000'), [
44
+ nodes
45
+ ]);
46
+ React.useEffect(()=>{
47
+ if (!getFilenameFQDN || fileNamesKey === '') {
48
+ setResolved({});
49
+ return undefined;
50
+ }
51
+ let cancelled = false;
52
+ const fileNames = fileNamesKey.split('\u0000');
53
+ void Promise.all(fileNames.map(async (fileName)=>{
54
+ try {
55
+ return [
56
+ fileName,
57
+ await Promise.resolve(getFilenameFQDN(fileName))
58
+ ];
59
+ } catch (error) {
60
+ console.error(`無法取得底圖網址: ${fileName}`, error);
61
+ return null;
62
+ }
63
+ })).then((entries)=>{
64
+ if (!cancelled) {
65
+ setResolved(Object.fromEntries(entries.filter((entry)=>entry !== null)));
66
+ }
67
+ });
68
+ return ()=>{
69
+ cancelled = true;
70
+ };
71
+ }, [
72
+ fileNamesKey,
73
+ getFilenameFQDN
74
+ ]);
75
+ return resolved;
76
+ };
77
+ const WMSMapPreviewCanvas = ({ initialNodes, highlightIds, dimOthers = true, fitPadding = 0.15, interactive = false, getFilenameFQDN })=>{
78
+ const resolvedImageUrls = useResolvedImageUrls(initialNodes, getFilenameFQDN);
79
+ const nodes = React.useMemo(()=>previewHighlight.applyPreviewHighlight(initialNodes, highlightIds, dimOthers).map((node)=>{
80
+ const fileName = getFileName(node);
81
+ const imageUrl = fileName !== null ? resolvedImageUrls[fileName] : undefined;
82
+ return {
83
+ ...node,
84
+ data: imageUrl !== undefined ? {
85
+ ...node.data,
86
+ imageUrl
87
+ } : node.data,
88
+ selectable: false,
89
+ draggable: false,
90
+ deletable: false,
91
+ connectable: false,
92
+ focusable: false
93
+ };
94
+ }), [
95
+ initialNodes,
96
+ highlightIds,
97
+ dimOthers,
98
+ resolvedImageUrls
99
+ ]);
100
+ // React Flow 以自己的 ResizeObserver 量測容器並寫入 store;尺寸改變時重新 fit
101
+ const width = react.useStore((state)=>state.width);
102
+ const height = react.useStore((state)=>state.height);
103
+ const nodeIds = React.useMemo(()=>nodes.map((node)=>node.id), [
104
+ nodes
105
+ ]);
106
+ const fitKey = [
107
+ fitTrigger.getNodeIdSignature(nodes),
108
+ highlightIds ? [
109
+ ...highlightIds
110
+ ].sort().join('\u0000') : '',
111
+ fitPadding,
112
+ width,
113
+ height
114
+ ].join('|');
115
+ const [fitRequestToken, setFitRequestToken] = React.useState(0);
116
+ React.useEffect(()=>{
117
+ if (nodeIds.length > 0 && width > 0 && height > 0) {
118
+ setFitRequestToken((token)=>token + 1);
119
+ }
120
+ }, [
121
+ fitKey,
122
+ nodeIds.length,
123
+ width,
124
+ height
125
+ ]);
126
+ useFitViewWhenReady.useFitViewWhenReady({
127
+ requestToken: fitRequestToken,
128
+ nodeIds,
129
+ focusIds: highlightIds,
130
+ padding: fitPadding
131
+ });
132
+ return /*#__PURE__*/ jsxRuntime.jsx(react.ReactFlow, {
133
+ nodes: nodes,
134
+ edges: [],
135
+ nodeTypes: PREVIEW_NODE_TYPES,
136
+ style: {
137
+ background: '#F5F5F5'
138
+ },
139
+ defaultViewport: {
140
+ x: 0,
141
+ y: 0,
142
+ zoom: 1
143
+ },
144
+ minZoom: PREVIEW_MIN_ZOOM,
145
+ maxZoom: PREVIEW_MAX_ZOOM,
146
+ nodeOrigin: [
147
+ 0,
148
+ 0
149
+ ],
150
+ nodesDraggable: false,
151
+ nodesConnectable: false,
152
+ nodesFocusable: false,
153
+ edgesFocusable: false,
154
+ elementsSelectable: false,
155
+ selectNodesOnDrag: false,
156
+ deleteKeyCode: NO_NODES_DELETE_KEY,
157
+ selectionKeyCode: null,
158
+ multiSelectionKeyCode: null,
159
+ panOnDrag: interactive,
160
+ panOnScroll: false,
161
+ zoomOnScroll: interactive,
162
+ zoomOnPinch: interactive,
163
+ zoomOnDoubleClick: false,
164
+ preventScrolling: interactive
165
+ });
166
+ };
167
+ /**
168
+ * 唯讀的地圖預覽:不含工具列、麵包屑與右鍵選單,節點就緒後自動 fit,
169
+ * 有 highlightIds 時框住並高亮這些範圍。
170
+ *
171
+ * 需要 React Flow 的樣式表;與 WMSMapModal 相同,由宿主在 app 層 import:
172
+ * `import '@xyflow/react/dist/style.css';`
173
+ */ const WMSMapPreview = (props)=>{
174
+ const { className, style } = props;
175
+ return /*#__PURE__*/ jsxRuntime.jsxs("div", {
176
+ className: className,
177
+ style: {
178
+ position: 'relative',
179
+ width: '100%',
180
+ height: '100%',
181
+ ...style
182
+ },
183
+ children: [
184
+ /*#__PURE__*/ jsxRuntime.jsx(react.ReactFlowProvider, {
185
+ children: /*#__PURE__*/ jsxRuntime.jsx(WMSMapPreviewCanvas, {
186
+ ...props
187
+ })
188
+ }),
189
+ !props.interactive && /*#__PURE__*/ jsxRuntime.jsx("div", {
190
+ "aria-hidden": true,
191
+ style: {
192
+ position: 'absolute',
193
+ inset: 0,
194
+ zIndex: 10
195
+ }
196
+ })
197
+ ]
198
+ });
199
+ };
200
+
201
+ module.exports = WMSMapPreview;
@@ -0,0 +1,11 @@
1
+ import { FC } from 'react';
2
+ import type { WMSMapPreviewProps } from '../../types/index.cjs';
3
+ /**
4
+ * 唯讀的地圖預覽:不含工具列、麵包屑與右鍵選單,節點就緒後自動 fit,
5
+ * 有 highlightIds 時框住並高亮這些範圍。
6
+ *
7
+ * 需要 React Flow 的樣式表;與 WMSMapModal 相同,由宿主在 app 層 import:
8
+ * `import '@xyflow/react/dist/style.css';`
9
+ */
10
+ declare const WMSMapPreview: FC<WMSMapPreviewProps>;
11
+ export default WMSMapPreview;
@@ -0,0 +1,11 @@
1
+ import { FC } from 'react';
2
+ import type { WMSMapPreviewProps } from '../../types/index.js';
3
+ /**
4
+ * 唯讀的地圖預覽:不含工具列、麵包屑與右鍵選單,節點就緒後自動 fit,
5
+ * 有 highlightIds 時框住並高亮這些範圍。
6
+ *
7
+ * 需要 React Flow 的樣式表;與 WMSMapModal 相同,由宿主在 app 層 import:
8
+ * `import '@xyflow/react/dist/style.css';`
9
+ */
10
+ declare const WMSMapPreview: FC<WMSMapPreviewProps>;
11
+ export default WMSMapPreview;
@@ -0,0 +1,199 @@
1
+ "use client";
2
+ import { jsxs, jsx } from 'react/jsx-runtime';
3
+ import { useMemo, useState, useEffect } from 'react';
4
+ import { ReactFlowProvider, useStore, ReactFlow } from '@xyflow/react';
5
+ import { ViewMode, EditMode } from '../../typings.js';
6
+ import ImageNode from '../nodes/image-node.js';
7
+ import RectangleNode from '../nodes/rectangle-node.js';
8
+ import PathNode from '../nodes/path-node.js';
9
+ import { useFitViewWhenReady } from '../../hooks/use-fit-view-when-ready.js';
10
+ import { applyPreviewHighlight } from '../../utils/preview-highlight.js';
11
+ import { getNodeIdSignature } from '../../utils/fit-trigger.js';
12
+
13
+ /** 與 WMSMapModal 的檢視模式一致:VIEW 不可編輯,BACKGROUND 讓底圖全不透明。 */ const PREVIEW_EDIT_MODE = EditMode.BACKGROUND;
14
+ const PREVIEW_VIEW_MODE = ViewMode.VIEW;
15
+ /** 模組層級常數,維持穩定參照,React Flow 才不會每次 render 重建節點。 */ const PREVIEW_NODE_TYPES = {
16
+ imageNode: (props)=>/*#__PURE__*/ jsx(ImageNode, {
17
+ ...props,
18
+ editMode: PREVIEW_EDIT_MODE,
19
+ viewMode: PREVIEW_VIEW_MODE
20
+ }),
21
+ rectangleNode: (props)=>/*#__PURE__*/ jsx(RectangleNode, {
22
+ ...props,
23
+ editMode: PREVIEW_EDIT_MODE,
24
+ viewMode: PREVIEW_VIEW_MODE
25
+ }),
26
+ pathNode: (props)=>/*#__PURE__*/ jsx(PathNode, {
27
+ ...props,
28
+ editMode: PREVIEW_EDIT_MODE,
29
+ viewMode: PREVIEW_VIEW_MODE
30
+ })
31
+ };
32
+ /** 預覽容器可能很窄(例如 200px),minZoom 需夠低,否則 fit 會被夾住而框不住整張圖。 */ const PREVIEW_MIN_ZOOM = 0.01;
33
+ const PREVIEW_MAX_ZOOM = 4;
34
+ const NO_NODES_DELETE_KEY = null;
35
+ const getFileName = (node)=>{
36
+ const { fileName } = node.data;
37
+ return node.type === 'imageNode' && typeof fileName === 'string' && fileName.length > 0 ? fileName : null;
38
+ };
39
+ /** 以 getFilenameFQDN 解析背景圖網址;未提供時沿用 data.imageUrl。 */ const useResolvedImageUrls = (nodes, getFilenameFQDN)=>{
40
+ const [resolved, setResolved] = useState({});
41
+ const fileNamesKey = useMemo(()=>Array.from(new Set(nodes.map(getFileName).filter((name)=>name !== null))).sort().join('\u0000'), [
42
+ nodes
43
+ ]);
44
+ useEffect(()=>{
45
+ if (!getFilenameFQDN || fileNamesKey === '') {
46
+ setResolved({});
47
+ return undefined;
48
+ }
49
+ let cancelled = false;
50
+ const fileNames = fileNamesKey.split('\u0000');
51
+ void Promise.all(fileNames.map(async (fileName)=>{
52
+ try {
53
+ return [
54
+ fileName,
55
+ await Promise.resolve(getFilenameFQDN(fileName))
56
+ ];
57
+ } catch (error) {
58
+ console.error(`無法取得底圖網址: ${fileName}`, error);
59
+ return null;
60
+ }
61
+ })).then((entries)=>{
62
+ if (!cancelled) {
63
+ setResolved(Object.fromEntries(entries.filter((entry)=>entry !== null)));
64
+ }
65
+ });
66
+ return ()=>{
67
+ cancelled = true;
68
+ };
69
+ }, [
70
+ fileNamesKey,
71
+ getFilenameFQDN
72
+ ]);
73
+ return resolved;
74
+ };
75
+ const WMSMapPreviewCanvas = ({ initialNodes, highlightIds, dimOthers = true, fitPadding = 0.15, interactive = false, getFilenameFQDN })=>{
76
+ const resolvedImageUrls = useResolvedImageUrls(initialNodes, getFilenameFQDN);
77
+ const nodes = useMemo(()=>applyPreviewHighlight(initialNodes, highlightIds, dimOthers).map((node)=>{
78
+ const fileName = getFileName(node);
79
+ const imageUrl = fileName !== null ? resolvedImageUrls[fileName] : undefined;
80
+ return {
81
+ ...node,
82
+ data: imageUrl !== undefined ? {
83
+ ...node.data,
84
+ imageUrl
85
+ } : node.data,
86
+ selectable: false,
87
+ draggable: false,
88
+ deletable: false,
89
+ connectable: false,
90
+ focusable: false
91
+ };
92
+ }), [
93
+ initialNodes,
94
+ highlightIds,
95
+ dimOthers,
96
+ resolvedImageUrls
97
+ ]);
98
+ // React Flow 以自己的 ResizeObserver 量測容器並寫入 store;尺寸改變時重新 fit
99
+ const width = useStore((state)=>state.width);
100
+ const height = useStore((state)=>state.height);
101
+ const nodeIds = useMemo(()=>nodes.map((node)=>node.id), [
102
+ nodes
103
+ ]);
104
+ const fitKey = [
105
+ getNodeIdSignature(nodes),
106
+ highlightIds ? [
107
+ ...highlightIds
108
+ ].sort().join('\u0000') : '',
109
+ fitPadding,
110
+ width,
111
+ height
112
+ ].join('|');
113
+ const [fitRequestToken, setFitRequestToken] = useState(0);
114
+ useEffect(()=>{
115
+ if (nodeIds.length > 0 && width > 0 && height > 0) {
116
+ setFitRequestToken((token)=>token + 1);
117
+ }
118
+ }, [
119
+ fitKey,
120
+ nodeIds.length,
121
+ width,
122
+ height
123
+ ]);
124
+ useFitViewWhenReady({
125
+ requestToken: fitRequestToken,
126
+ nodeIds,
127
+ focusIds: highlightIds,
128
+ padding: fitPadding
129
+ });
130
+ return /*#__PURE__*/ jsx(ReactFlow, {
131
+ nodes: nodes,
132
+ edges: [],
133
+ nodeTypes: PREVIEW_NODE_TYPES,
134
+ style: {
135
+ background: '#F5F5F5'
136
+ },
137
+ defaultViewport: {
138
+ x: 0,
139
+ y: 0,
140
+ zoom: 1
141
+ },
142
+ minZoom: PREVIEW_MIN_ZOOM,
143
+ maxZoom: PREVIEW_MAX_ZOOM,
144
+ nodeOrigin: [
145
+ 0,
146
+ 0
147
+ ],
148
+ nodesDraggable: false,
149
+ nodesConnectable: false,
150
+ nodesFocusable: false,
151
+ edgesFocusable: false,
152
+ elementsSelectable: false,
153
+ selectNodesOnDrag: false,
154
+ deleteKeyCode: NO_NODES_DELETE_KEY,
155
+ selectionKeyCode: null,
156
+ multiSelectionKeyCode: null,
157
+ panOnDrag: interactive,
158
+ panOnScroll: false,
159
+ zoomOnScroll: interactive,
160
+ zoomOnPinch: interactive,
161
+ zoomOnDoubleClick: false,
162
+ preventScrolling: interactive
163
+ });
164
+ };
165
+ /**
166
+ * 唯讀的地圖預覽:不含工具列、麵包屑與右鍵選單,節點就緒後自動 fit,
167
+ * 有 highlightIds 時框住並高亮這些範圍。
168
+ *
169
+ * 需要 React Flow 的樣式表;與 WMSMapModal 相同,由宿主在 app 層 import:
170
+ * `import '@xyflow/react/dist/style.css';`
171
+ */ const WMSMapPreview = (props)=>{
172
+ const { className, style } = props;
173
+ return /*#__PURE__*/ jsxs("div", {
174
+ className: className,
175
+ style: {
176
+ position: 'relative',
177
+ width: '100%',
178
+ height: '100%',
179
+ ...style
180
+ },
181
+ children: [
182
+ /*#__PURE__*/ jsx(ReactFlowProvider, {
183
+ children: /*#__PURE__*/ jsx(WMSMapPreviewCanvas, {
184
+ ...props
185
+ })
186
+ }),
187
+ !props.interactive && /*#__PURE__*/ jsx("div", {
188
+ "aria-hidden": true,
189
+ style: {
190
+ position: 'absolute',
191
+ inset: 0,
192
+ zIndex: 10
193
+ }
194
+ })
195
+ ]
196
+ });
197
+ };
198
+
199
+ export { WMSMapPreview as default };
@@ -0,0 +1,74 @@
1
+ "use client";
2
+ 'use strict';
3
+
4
+ var React = require('react');
5
+ var react = require('@xyflow/react');
6
+
7
+ /** 等待節點量測完成的 rAF 重試上限(約 2 秒);超過即放棄,避免無限輪詢。 */ const MAX_READY_FRAMES = 120;
8
+ const isMeasured = (width, height)=>(width ?? 0) > 0 && (height ?? 0) > 0;
9
+ /**
10
+ * 在 requestToken 變動時,等節點全部量測完成後直接設定 viewport 框住節點。
11
+ *
12
+ * 不使用 React Flow 的 `fitView`:它只會在下一次 `setNodes` 或 `updateNodeInternals` 時才執行,
13
+ * 換入新節點集合時若未觸發這兩者會永遠不 fit。這裡改為自行輪詢 store,就緒後直接呼叫 panZoom。
14
+ * 只有 requestToken 變動時才會 fit,之後使用者的手動縮放或平移不會被覆寫。
15
+ */ const useFitViewWhenReady = (options)=>{
16
+ const store = react.useStoreApi();
17
+ const latestRef = React.useRef(options);
18
+ React.useEffect(()=>{
19
+ latestRef.current = options;
20
+ });
21
+ const { requestToken } = options;
22
+ React.useEffect(()=>{
23
+ if (requestToken === 0) {
24
+ return undefined;
25
+ }
26
+ let frame = null;
27
+ let attempts = 0;
28
+ const tryFit = ()=>{
29
+ frame = null;
30
+ const { nodeIds, focusIds, padding } = latestRef.current;
31
+ if (nodeIds.length === 0) {
32
+ return;
33
+ }
34
+ const { nodeLookup, width, height, panZoom, minZoom, maxZoom, nodeOrigin } = store.getState();
35
+ const ready = width > 0 && height > 0 && panZoom !== null && nodeLookup.size === nodeIds.length && nodeIds.every((id)=>{
36
+ const node = nodeLookup.get(id);
37
+ return node !== undefined && isMeasured(node.measured.width, node.measured.height);
38
+ });
39
+ if (!ready) {
40
+ attempts += 1;
41
+ if (attempts < MAX_READY_FRAMES) {
42
+ frame = requestAnimationFrame(tryFit);
43
+ }
44
+ return;
45
+ }
46
+ const focused = focusIds?.filter((id)=>nodeLookup.has(id)) ?? [];
47
+ const targetIds = focused.length > 0 ? focused : nodeIds;
48
+ const bounds = react.getNodesBounds([
49
+ ...targetIds
50
+ ], {
51
+ nodeLookup,
52
+ nodeOrigin
53
+ });
54
+ if (bounds.width <= 0 || bounds.height <= 0) {
55
+ return;
56
+ }
57
+ const viewport = react.getViewportForBounds(bounds, width, height, minZoom, maxZoom, padding);
58
+ void panZoom?.setViewport(viewport, {
59
+ duration: 0
60
+ });
61
+ };
62
+ frame = requestAnimationFrame(tryFit);
63
+ return ()=>{
64
+ if (frame !== null) {
65
+ cancelAnimationFrame(frame);
66
+ }
67
+ };
68
+ }, [
69
+ requestToken,
70
+ store
71
+ ]);
72
+ };
73
+
74
+ exports.useFitViewWhenReady = useFitViewWhenReady;
@@ -0,0 +1,18 @@
1
+ export interface FitViewWhenReadyOptions {
2
+ /** 每次要求 fit 時遞增;0 代表尚未要求,不會 fit。 */
3
+ requestToken: number;
4
+ /** 目前節點集合的全部 id;全部節點量測完成後才會 fit。 */
5
+ nodeIds: readonly string[];
6
+ /** 要框住的節點 id(例如 highlightIds);未提供或皆不存在時框住全部節點。 */
7
+ focusIds?: readonly string[];
8
+ /** 比例 padding,語意與 React Flow `fitView` 相同(0.1 = 10%)。 */
9
+ padding: number;
10
+ }
11
+ /**
12
+ * 在 requestToken 變動時,等節點全部量測完成後直接設定 viewport 框住節點。
13
+ *
14
+ * 不使用 React Flow 的 `fitView`:它只會在下一次 `setNodes` 或 `updateNodeInternals` 時才執行,
15
+ * 換入新節點集合時若未觸發這兩者會永遠不 fit。這裡改為自行輪詢 store,就緒後直接呼叫 panZoom。
16
+ * 只有 requestToken 變動時才會 fit,之後使用者的手動縮放或平移不會被覆寫。
17
+ */
18
+ export declare const useFitViewWhenReady: (options: FitViewWhenReadyOptions) => void;
@@ -0,0 +1,18 @@
1
+ export interface FitViewWhenReadyOptions {
2
+ /** 每次要求 fit 時遞增;0 代表尚未要求,不會 fit。 */
3
+ requestToken: number;
4
+ /** 目前節點集合的全部 id;全部節點量測完成後才會 fit。 */
5
+ nodeIds: readonly string[];
6
+ /** 要框住的節點 id(例如 highlightIds);未提供或皆不存在時框住全部節點。 */
7
+ focusIds?: readonly string[];
8
+ /** 比例 padding,語意與 React Flow `fitView` 相同(0.1 = 10%)。 */
9
+ padding: number;
10
+ }
11
+ /**
12
+ * 在 requestToken 變動時,等節點全部量測完成後直接設定 viewport 框住節點。
13
+ *
14
+ * 不使用 React Flow 的 `fitView`:它只會在下一次 `setNodes` 或 `updateNodeInternals` 時才執行,
15
+ * 換入新節點集合時若未觸發這兩者會永遠不 fit。這裡改為自行輪詢 store,就緒後直接呼叫 panZoom。
16
+ * 只有 requestToken 變動時才會 fit,之後使用者的手動縮放或平移不會被覆寫。
17
+ */
18
+ export declare const useFitViewWhenReady: (options: FitViewWhenReadyOptions) => void;
@@ -0,0 +1,72 @@
1
+ "use client";
2
+ import { useRef, useEffect } from 'react';
3
+ import { useStoreApi, getNodesBounds, getViewportForBounds } from '@xyflow/react';
4
+
5
+ /** 等待節點量測完成的 rAF 重試上限(約 2 秒);超過即放棄,避免無限輪詢。 */ const MAX_READY_FRAMES = 120;
6
+ const isMeasured = (width, height)=>(width ?? 0) > 0 && (height ?? 0) > 0;
7
+ /**
8
+ * 在 requestToken 變動時,等節點全部量測完成後直接設定 viewport 框住節點。
9
+ *
10
+ * 不使用 React Flow 的 `fitView`:它只會在下一次 `setNodes` 或 `updateNodeInternals` 時才執行,
11
+ * 換入新節點集合時若未觸發這兩者會永遠不 fit。這裡改為自行輪詢 store,就緒後直接呼叫 panZoom。
12
+ * 只有 requestToken 變動時才會 fit,之後使用者的手動縮放或平移不會被覆寫。
13
+ */ const useFitViewWhenReady = (options)=>{
14
+ const store = useStoreApi();
15
+ const latestRef = useRef(options);
16
+ useEffect(()=>{
17
+ latestRef.current = options;
18
+ });
19
+ const { requestToken } = options;
20
+ useEffect(()=>{
21
+ if (requestToken === 0) {
22
+ return undefined;
23
+ }
24
+ let frame = null;
25
+ let attempts = 0;
26
+ const tryFit = ()=>{
27
+ frame = null;
28
+ const { nodeIds, focusIds, padding } = latestRef.current;
29
+ if (nodeIds.length === 0) {
30
+ return;
31
+ }
32
+ const { nodeLookup, width, height, panZoom, minZoom, maxZoom, nodeOrigin } = store.getState();
33
+ const ready = width > 0 && height > 0 && panZoom !== null && nodeLookup.size === nodeIds.length && nodeIds.every((id)=>{
34
+ const node = nodeLookup.get(id);
35
+ return node !== undefined && isMeasured(node.measured.width, node.measured.height);
36
+ });
37
+ if (!ready) {
38
+ attempts += 1;
39
+ if (attempts < MAX_READY_FRAMES) {
40
+ frame = requestAnimationFrame(tryFit);
41
+ }
42
+ return;
43
+ }
44
+ const focused = focusIds?.filter((id)=>nodeLookup.has(id)) ?? [];
45
+ const targetIds = focused.length > 0 ? focused : nodeIds;
46
+ const bounds = getNodesBounds([
47
+ ...targetIds
48
+ ], {
49
+ nodeLookup,
50
+ nodeOrigin
51
+ });
52
+ if (bounds.width <= 0 || bounds.height <= 0) {
53
+ return;
54
+ }
55
+ const viewport = getViewportForBounds(bounds, width, height, minZoom, maxZoom, padding);
56
+ void panZoom?.setViewport(viewport, {
57
+ duration: 0
58
+ });
59
+ };
60
+ frame = requestAnimationFrame(tryFit);
61
+ return ()=>{
62
+ if (frame !== null) {
63
+ cancelAnimationFrame(frame);
64
+ }
65
+ };
66
+ }, [
67
+ requestToken,
68
+ store
69
+ ]);
70
+ };
71
+
72
+ export { useFitViewWhenReady };