my-openlayer 2.4.6 → 2.4.8

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 (34) hide show
  1. package/CHANGELOG.md +101 -0
  2. package/MyOl.js +1 -1
  3. package/core/Point.js +10 -2
  4. package/docs/.vitepress/cache/deps/@theme_index.js +275 -0
  5. package/docs/.vitepress/cache/deps/@theme_index.js.map +7 -0
  6. package/docs/.vitepress/cache/deps/_metadata.json +40 -0
  7. package/docs/.vitepress/cache/deps/chunk-LW4I4DCF.js +12542 -0
  8. package/docs/.vitepress/cache/deps/chunk-LW4I4DCF.js.map +7 -0
  9. package/docs/.vitepress/cache/deps/chunk-Z5QSWKN2.js +9719 -0
  10. package/docs/.vitepress/cache/deps/chunk-Z5QSWKN2.js.map +7 -0
  11. package/docs/.vitepress/cache/deps/package.json +3 -0
  12. package/docs/.vitepress/cache/deps/vitepress___@vue_devtools-api.js +4505 -0
  13. package/docs/.vitepress/cache/deps/vitepress___@vue_devtools-api.js.map +7 -0
  14. package/docs/.vitepress/cache/deps/vitepress___@vueuse_core.js +583 -0
  15. package/docs/.vitepress/cache/deps/vitepress___@vueuse_core.js.map +7 -0
  16. package/docs/.vitepress/cache/deps/vue.js +343 -0
  17. package/docs/.vitepress/cache/deps/vue.js.map +7 -0
  18. package/docs/.vitepress/config.mts +57 -0
  19. package/docs/ConfigManager.md +57 -26
  20. package/docs/ErrorHandler.md +88 -27
  21. package/docs/EventManager.md +120 -30
  22. package/docs/Line.md +153 -18
  23. package/docs/MapBaseLayers.md +171 -21
  24. package/docs/MapTools.md +139 -15
  25. package/docs/MeasureHandler.md +71 -31
  26. package/docs/MyOl.md +220 -35
  27. package/docs/Point.md +141 -34
  28. package/docs/Polygon.md +204 -28
  29. package/docs/RiverLayerManager.md +156 -28
  30. package/docs/SelectHandler.md +110 -89
  31. package/docs/ValidationUtils.md +65 -20
  32. package/docs/VueTemplatePoint.md +155 -26
  33. package/docs/index.md +78 -0
  34. package/package.json +7 -3
@@ -0,0 +1,343 @@
1
+ import {
2
+ BaseTransition,
3
+ BaseTransitionPropsValidators,
4
+ Comment,
5
+ DeprecationTypes,
6
+ EffectScope,
7
+ ErrorCodes,
8
+ ErrorTypeStrings,
9
+ Fragment,
10
+ KeepAlive,
11
+ ReactiveEffect,
12
+ Static,
13
+ Suspense,
14
+ Teleport,
15
+ Text,
16
+ TrackOpTypes,
17
+ Transition,
18
+ TransitionGroup,
19
+ TriggerOpTypes,
20
+ VueElement,
21
+ assertNumber,
22
+ callWithAsyncErrorHandling,
23
+ callWithErrorHandling,
24
+ camelize,
25
+ capitalize,
26
+ cloneVNode,
27
+ compatUtils,
28
+ compile,
29
+ computed,
30
+ createApp,
31
+ createBaseVNode,
32
+ createBlock,
33
+ createCommentVNode,
34
+ createElementBlock,
35
+ createHydrationRenderer,
36
+ createPropsRestProxy,
37
+ createRenderer,
38
+ createSSRApp,
39
+ createSlots,
40
+ createStaticVNode,
41
+ createTextVNode,
42
+ createVNode,
43
+ customRef,
44
+ defineAsyncComponent,
45
+ defineComponent,
46
+ defineCustomElement,
47
+ defineEmits,
48
+ defineExpose,
49
+ defineModel,
50
+ defineOptions,
51
+ defineProps,
52
+ defineSSRCustomElement,
53
+ defineSlots,
54
+ devtools,
55
+ effect,
56
+ effectScope,
57
+ getCurrentInstance,
58
+ getCurrentScope,
59
+ getCurrentWatcher,
60
+ getTransitionRawChildren,
61
+ guardReactiveProps,
62
+ h,
63
+ handleError,
64
+ hasInjectionContext,
65
+ hydrate,
66
+ hydrateOnIdle,
67
+ hydrateOnInteraction,
68
+ hydrateOnMediaQuery,
69
+ hydrateOnVisible,
70
+ initCustomFormatter,
71
+ initDirectivesForSSR,
72
+ inject,
73
+ isMemoSame,
74
+ isProxy,
75
+ isReactive,
76
+ isReadonly,
77
+ isRef,
78
+ isRuntimeOnly,
79
+ isShallow,
80
+ isVNode,
81
+ markRaw,
82
+ mergeDefaults,
83
+ mergeModels,
84
+ mergeProps,
85
+ nextTick,
86
+ normalizeClass,
87
+ normalizeProps,
88
+ normalizeStyle,
89
+ onActivated,
90
+ onBeforeMount,
91
+ onBeforeUnmount,
92
+ onBeforeUpdate,
93
+ onDeactivated,
94
+ onErrorCaptured,
95
+ onMounted,
96
+ onRenderTracked,
97
+ onRenderTriggered,
98
+ onScopeDispose,
99
+ onServerPrefetch,
100
+ onUnmounted,
101
+ onUpdated,
102
+ onWatcherCleanup,
103
+ openBlock,
104
+ popScopeId,
105
+ provide,
106
+ proxyRefs,
107
+ pushScopeId,
108
+ queuePostFlushCb,
109
+ reactive,
110
+ readonly,
111
+ ref,
112
+ registerRuntimeCompiler,
113
+ render,
114
+ renderList,
115
+ renderSlot,
116
+ resolveComponent,
117
+ resolveDirective,
118
+ resolveDynamicComponent,
119
+ resolveFilter,
120
+ resolveTransitionHooks,
121
+ setBlockTracking,
122
+ setDevtoolsHook,
123
+ setTransitionHooks,
124
+ shallowReactive,
125
+ shallowReadonly,
126
+ shallowRef,
127
+ ssrContextKey,
128
+ ssrUtils,
129
+ stop,
130
+ toDisplayString,
131
+ toHandlerKey,
132
+ toHandlers,
133
+ toRaw,
134
+ toRef,
135
+ toRefs,
136
+ toValue,
137
+ transformVNodeArgs,
138
+ triggerRef,
139
+ unref,
140
+ useAttrs,
141
+ useCssModule,
142
+ useCssVars,
143
+ useHost,
144
+ useId,
145
+ useModel,
146
+ useSSRContext,
147
+ useShadowRoot,
148
+ useSlots,
149
+ useTemplateRef,
150
+ useTransitionState,
151
+ vModelCheckbox,
152
+ vModelDynamic,
153
+ vModelRadio,
154
+ vModelSelect,
155
+ vModelText,
156
+ vShow,
157
+ version,
158
+ warn,
159
+ watch,
160
+ watchEffect,
161
+ watchPostEffect,
162
+ watchSyncEffect,
163
+ withAsyncContext,
164
+ withCtx,
165
+ withDefaults,
166
+ withDirectives,
167
+ withKeys,
168
+ withMemo,
169
+ withModifiers,
170
+ withScopeId
171
+ } from "./chunk-LW4I4DCF.js";
172
+ export {
173
+ BaseTransition,
174
+ BaseTransitionPropsValidators,
175
+ Comment,
176
+ DeprecationTypes,
177
+ EffectScope,
178
+ ErrorCodes,
179
+ ErrorTypeStrings,
180
+ Fragment,
181
+ KeepAlive,
182
+ ReactiveEffect,
183
+ Static,
184
+ Suspense,
185
+ Teleport,
186
+ Text,
187
+ TrackOpTypes,
188
+ Transition,
189
+ TransitionGroup,
190
+ TriggerOpTypes,
191
+ VueElement,
192
+ assertNumber,
193
+ callWithAsyncErrorHandling,
194
+ callWithErrorHandling,
195
+ camelize,
196
+ capitalize,
197
+ cloneVNode,
198
+ compatUtils,
199
+ compile,
200
+ computed,
201
+ createApp,
202
+ createBlock,
203
+ createCommentVNode,
204
+ createElementBlock,
205
+ createBaseVNode as createElementVNode,
206
+ createHydrationRenderer,
207
+ createPropsRestProxy,
208
+ createRenderer,
209
+ createSSRApp,
210
+ createSlots,
211
+ createStaticVNode,
212
+ createTextVNode,
213
+ createVNode,
214
+ customRef,
215
+ defineAsyncComponent,
216
+ defineComponent,
217
+ defineCustomElement,
218
+ defineEmits,
219
+ defineExpose,
220
+ defineModel,
221
+ defineOptions,
222
+ defineProps,
223
+ defineSSRCustomElement,
224
+ defineSlots,
225
+ devtools,
226
+ effect,
227
+ effectScope,
228
+ getCurrentInstance,
229
+ getCurrentScope,
230
+ getCurrentWatcher,
231
+ getTransitionRawChildren,
232
+ guardReactiveProps,
233
+ h,
234
+ handleError,
235
+ hasInjectionContext,
236
+ hydrate,
237
+ hydrateOnIdle,
238
+ hydrateOnInteraction,
239
+ hydrateOnMediaQuery,
240
+ hydrateOnVisible,
241
+ initCustomFormatter,
242
+ initDirectivesForSSR,
243
+ inject,
244
+ isMemoSame,
245
+ isProxy,
246
+ isReactive,
247
+ isReadonly,
248
+ isRef,
249
+ isRuntimeOnly,
250
+ isShallow,
251
+ isVNode,
252
+ markRaw,
253
+ mergeDefaults,
254
+ mergeModels,
255
+ mergeProps,
256
+ nextTick,
257
+ normalizeClass,
258
+ normalizeProps,
259
+ normalizeStyle,
260
+ onActivated,
261
+ onBeforeMount,
262
+ onBeforeUnmount,
263
+ onBeforeUpdate,
264
+ onDeactivated,
265
+ onErrorCaptured,
266
+ onMounted,
267
+ onRenderTracked,
268
+ onRenderTriggered,
269
+ onScopeDispose,
270
+ onServerPrefetch,
271
+ onUnmounted,
272
+ onUpdated,
273
+ onWatcherCleanup,
274
+ openBlock,
275
+ popScopeId,
276
+ provide,
277
+ proxyRefs,
278
+ pushScopeId,
279
+ queuePostFlushCb,
280
+ reactive,
281
+ readonly,
282
+ ref,
283
+ registerRuntimeCompiler,
284
+ render,
285
+ renderList,
286
+ renderSlot,
287
+ resolveComponent,
288
+ resolveDirective,
289
+ resolveDynamicComponent,
290
+ resolveFilter,
291
+ resolveTransitionHooks,
292
+ setBlockTracking,
293
+ setDevtoolsHook,
294
+ setTransitionHooks,
295
+ shallowReactive,
296
+ shallowReadonly,
297
+ shallowRef,
298
+ ssrContextKey,
299
+ ssrUtils,
300
+ stop,
301
+ toDisplayString,
302
+ toHandlerKey,
303
+ toHandlers,
304
+ toRaw,
305
+ toRef,
306
+ toRefs,
307
+ toValue,
308
+ transformVNodeArgs,
309
+ triggerRef,
310
+ unref,
311
+ useAttrs,
312
+ useCssModule,
313
+ useCssVars,
314
+ useHost,
315
+ useId,
316
+ useModel,
317
+ useSSRContext,
318
+ useShadowRoot,
319
+ useSlots,
320
+ useTemplateRef,
321
+ useTransitionState,
322
+ vModelCheckbox,
323
+ vModelDynamic,
324
+ vModelRadio,
325
+ vModelSelect,
326
+ vModelText,
327
+ vShow,
328
+ version,
329
+ warn,
330
+ watch,
331
+ watchEffect,
332
+ watchPostEffect,
333
+ watchSyncEffect,
334
+ withAsyncContext,
335
+ withCtx,
336
+ withDefaults,
337
+ withDirectives,
338
+ withKeys,
339
+ withMemo,
340
+ withModifiers,
341
+ withScopeId
342
+ };
343
+ //# sourceMappingURL=vue.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": [],
4
+ "sourcesContent": [],
5
+ "mappings": "",
6
+ "names": []
7
+ }
@@ -0,0 +1,57 @@
1
+ import { defineConfig } from 'vitepress'
2
+
3
+ export default defineConfig({
4
+ base: '/my-openlayer/',
5
+ title: "MyOpenLayer",
6
+ description: "基于 OpenLayers 的现代地图组件库",
7
+ lang: 'zh-CN',
8
+ themeConfig: {
9
+ nav: [
10
+ { text: '首页', link: '/' },
11
+ { text: '指南', link: '/MyOl' },
12
+ { text: 'API', link: '/MyOl' },
13
+ { text: 'GitHub', link: 'https://github.com/cuteyuchen/my-openlayer' }
14
+ ],
15
+
16
+ sidebar: [
17
+ {
18
+ text: '核心类库',
19
+ items: [
20
+ { text: 'MyOl 地图入口', link: '/MyOl' },
21
+ { text: 'MapBaseLayers 底图管理', link: '/MapBaseLayers' },
22
+ { text: 'ConfigManager 配置管理', link: '/ConfigManager' },
23
+ { text: 'EventManager 事件管理', link: '/EventManager' },
24
+ { text: 'ErrorHandler 错误处理', link: '/ErrorHandler' }
25
+ ]
26
+ },
27
+ {
28
+ text: '要素操作',
29
+ items: [
30
+ { text: 'Point 点要素', link: '/Point' },
31
+ { text: 'Line 线要素', link: '/Line' },
32
+ { text: 'Polygon 面要素', link: '/Polygon' },
33
+ { text: 'VueTemplatePoint Vue点位', link: '/VueTemplatePoint' },
34
+ { text: 'RiverLayerManager 河流图层', link: '/RiverLayerManager' }
35
+ ]
36
+ },
37
+ {
38
+ text: '交互与工具',
39
+ items: [
40
+ { text: 'SelectHandler 要素选择', link: '/SelectHandler' },
41
+ { text: 'MeasureHandler 测量工具', link: '/MeasureHandler' },
42
+ { text: 'MapTools 地图工具', link: '/MapTools' },
43
+ { text: 'ValidationUtils 验证工具', link: '/ValidationUtils' }
44
+ ]
45
+ }
46
+ ],
47
+
48
+ socialLinks: [
49
+ { icon: 'github', link: 'https://github.com/cuteyuchen/my-openlayer' }
50
+ ],
51
+
52
+ footer: {
53
+ message: 'Released under the MIT License.',
54
+ copyright: 'Copyright © 2024-present'
55
+ }
56
+ }
57
+ })
@@ -1,40 +1,71 @@
1
- # ConfigManager 配置管理类
1
+ # ConfigManager 配置管理
2
2
 
3
- `ConfigManager` 用于统一管理库的默认配置项,提供配置合并和 ID 生成工具。
3
+ `ConfigManager` 类用于统一管理地图组件的默认配置和常量。它提供了一系列静态只读属性,确保整个应用中使用一致的配置默认值。
4
4
 
5
- ## 静态属性 (默认配置)
5
+ ## 导入
6
6
 
7
- - **DEFAULT_POINT_OPTIONS**: 默认点位配置。
8
- - **DEFAULT_LINE_OPTIONS**: 默认线要素配置。
9
- - **DEFAULT_POLYGON_OPTIONS**: 默认面要素配置。
10
- - **DEFAULT_CLUSTER_OPTIONS**: 默认聚合配置。
11
- - **DEFAULT_HEATMAP_OPTIONS**: 默认热力图配置。
12
- - **DEFAULT_IMAGE_OPTIONS**: 默认图片图层配置。
13
- - **DEFAULT_MASK_OPTIONS**: 默认遮罩配置。
14
- - **TIANDITU_CONFIG**: 天地图服务配置。
7
+ ```typescript
8
+ import { ConfigManager } from 'my-openlayer';
9
+ ```
10
+
11
+ ## 静态属性
12
+
13
+ `ConfigManager` 包含以下类别的默认配置:
14
+
15
+ ### 点位配置 (Point Options)
16
+
17
+ | 属性名 | 类型 | 描述 | 默认值 |
18
+ | :--- | :--- | :--- | :--- |
19
+ | `DEFAULT_POINT_OPTIONS` | `object` | 默认点位基础配置 | `{ visible: true, zIndex: 21 }` |
20
+ | `DEFAULT_POINT_TEXT_OPTIONS` | `object` | 默认点位文本配置 | `{ textFont: '12px Calibri,sans-serif', textFillColor: '#FFF', textStrokeColor: '#000', textStrokeWidth: 3, textOffsetY: 20 }` |
21
+ | `DEFAULT_POINT_ICON_SCALE` | `number` | 默认图标缩放比例 | `1` |
22
+ | `DEFAULT_CLUSTER_OPTIONS` | `object` | 默认聚合配置 | `{ distance: 40, minDistance: 0, zIndex: 21 }` |
23
+ | `DEFAULT_DOM_POINT_OVERLAY_OPTIONS` | `object` | 默认 DOM 点覆盖物配置 | `{ positioning: 'center-center', stopEvent: false }` |
24
+
25
+ ### 线配置 (Line Options)
15
26
 
16
- ## 静态方法
27
+ | 属性名 | 类型 | 描述 | 默认值 |
28
+ | :--- | :--- | :--- | :--- |
29
+ | `DEFAULT_LINE_OPTIONS` | `object` | 默认线样式配置 | `{ type: 'line', strokeColor: 'rgba(3, 122, 255, 1)', strokeWidth: 2, visible: true, layerName: 'lineLayer', zIndex: 15 }` |
17
30
 
18
- - **mergeOptions<T>(defaultOptions: T, userOptions?: Partial<T>)**: 合并用户配置与默认配置。
19
- - **generateId(prefix?: string)**: 生成带前缀的唯一 ID。
20
- - **deepClone<T>(obj: T)**: 深度克隆对象。
31
+ ### 面配置 (Polygon Options)
32
+
33
+ | 属性名 | 类型 | 描述 | 默认值 |
34
+ | :--- | :--- | :--- | :--- |
35
+ | `DEFAULT_POLYGON_OPTIONS` | `object` | 默认面样式配置 | `{ zIndex: 11, visible: true, textFont: '14px Calibri,sans-serif', textFillColor: '#FFF', textStrokeColor: '#409EFF', textStrokeWidth: 2 }` |
36
+ | `DEFAULT_POLYGON_COLOR_MAP` | `object` | 默认面颜色映射 (用于分级渲染) | `{ '0': 'rgba(255, 0, 0, 0.6)', '1': 'rgba(245, 154, 35, 0.6)', ... }` |
21
37
 
22
38
  ## 使用示例
23
39
 
40
+ ### 获取默认配置
41
+
24
42
  ```typescript
25
- import { ConfigManager, PointOptions } from 'my-openlayer';
43
+ import { ConfigManager } from 'my-openlayer';
26
44
 
27
- // 1. 合并用户配置与默认配置
28
- const userOptions: PointOptions = {
29
- layerName: 'my-points',
30
- visible: true
45
+ // 获取默认点位配置
46
+ const pointOptions = {
47
+ ...ConfigManager.DEFAULT_POINT_OPTIONS,
48
+ ...ConfigManager.DEFAULT_POINT_TEXT_OPTIONS
31
49
  };
32
50
 
33
- const finalOptions = ConfigManager.mergeOptions(
34
- ConfigManager.DEFAULT_POINT_OPTIONS,
35
- userOptions
36
- );
51
+ // 获取默认线配置
52
+ const lineOptions = ConfigManager.DEFAULT_LINE_OPTIONS;
53
+ ```
54
+
55
+ ### 在自定义组件中使用
56
+
57
+ ```typescript
58
+ import { ConfigManager } from 'my-openlayer';
59
+
60
+ class MyComponent {
61
+ private options: any;
37
62
 
38
- // 2. 生成唯一 ID
39
- const pointId = ConfigManager.generateId('point'); // 例如: point_169...
63
+ constructor(options: any) {
64
+ // 合并用户配置和默认配置
65
+ this.options = {
66
+ ...ConfigManager.DEFAULT_POINT_OPTIONS,
67
+ ...options
68
+ };
69
+ }
70
+ }
40
71
  ```
@@ -1,45 +1,106 @@
1
- # ErrorHandler 错误处理类
1
+ # ErrorHandler 错误处理
2
2
 
3
- `ErrorHandler` 是一个单例类,提供全局的错误捕获、日志记录和回调分发机制。
3
+ `ErrorHandler` 类是一个单例工具类,用于统一管理和分发应用程序中的错误。它支持不同的日志级别,并允许注册自定义错误回调。
4
+
5
+ ## 导入
6
+
7
+ ```typescript
8
+ import { ErrorHandler, ErrorType, MyOpenLayersError } from 'my-openlayer';
9
+ ```
10
+
11
+ ## 枚举与类定义
12
+
13
+ ### ErrorType 枚举
14
+
15
+ 定义了系统中可能的错误类型:
16
+
17
+ | 成员 | 值 | 描述 |
18
+ | :--- | :--- | :--- |
19
+ | `VALIDATION_ERROR` | `'VALIDATION_ERROR'` | 参数验证错误 |
20
+ | `MAP_ERROR` | `'MAP_ERROR'` | 地图相关错误 |
21
+ | `LAYER_ERROR` | `'LAYER_ERROR'` | 图层相关错误 |
22
+ | `COORDINATE_ERROR` | `'COORDINATE_ERROR'` | 坐标无效错误 |
23
+ | `DATA_ERROR` | `'DATA_ERROR'` | 数据格式错误 |
24
+ | `COMPONENT_ERROR` | `'COMPONENT_ERROR'` | 组件内部错误 |
25
+
26
+ ### MyOpenLayersError 类
27
+
28
+ 自定义错误类,继承自 `Error`。
29
+
30
+ | 属性 | 类型 | 描述 |
31
+ | :--- | :--- | :--- |
32
+ | `message` | `string` | 错误消息 |
33
+ | `type` | `ErrorType` | 错误类型 |
34
+ | `timestamp` | `Date` | 错误发生的时间戳 |
35
+ | `context` | `any` | 错误相关的上下文数据 |
4
36
 
5
37
  ## 方法
6
38
 
7
- - **getInstance()**: 获取单例实例。
8
- - **addErrorCallback(callback)**: 添加全局错误监听回调。
9
- - **handleError(error: MyOpenLayersError)**: 手动触发错误处理。
10
- - **validate(condition, message, context)**: 断言验证,失败则抛出错误。
11
- - **setEnabled(enabled: boolean)**: 启用/禁用日志。
12
- - **setLogLevel(level)**: 设置日志级别 (`debug`, `info`, `warn`, `error`)。
39
+ ### 静态方法
13
40
 
14
- ## 静态验证方法
41
+ | 方法名 | 参数 | 返回值 | 描述 |
42
+ | :--- | :--- | :--- | :--- |
43
+ | `getInstance()` | - | `ErrorHandler` | 获取 `ErrorHandler` 的单例实例 |
44
+ | `validate(condition, message, context?)` | `condition: boolean`<br>`message: string`<br>`context?: any` | `void` | 验证条件,如果为 `false` 则抛出 `VALIDATION_ERROR` |
45
+ | `validateMap(map)` | `map: any` | `void` | 验证地图实例是否存在 |
46
+ | `validateCoordinates(longitude, latitude)` | `longitude: number`<br>`latitude: number` | `void` | 验证经纬度坐标是否有效 |
47
+ | `validateLayerName(layerName)` | `layerName: string` | `void` | 验证图层名称是否有效 |
48
+ | `validateData(data, dataType)` | `data: any`<br>`dataType: string` | `void` | 验证数据是否存在 |
49
+ | `safeExecute<T>(fn, errorMessage, errorType?, context?)` | `fn: () => T`<br>`errorMessage: string`<br>`errorType?: ErrorType`<br>`context?: any` | `T` | 安全执行函数,捕获并处理异常 |
15
50
 
16
- - **validateMap(map)**: 验证地图实例。
17
- - **validateCoordinates(lng, lat)**: 验证经纬度有效性。
18
- - **validateLayerName(name)**: 验证图层名称。
51
+ ### 实例方法
52
+
53
+ | 方法名 | 参数 | 返回值 | 描述 |
54
+ | :--- | :--- | :--- | :--- |
55
+ | `setLogLevel(level)` | `level: 'debug' \| 'info' \| 'warn' \| 'error'` | `void` | 设置日志级别 |
56
+ | `debug(...args)` | `...args: any[]` | `void` | 输出调试日志 |
57
+ | `info(...args)` | `...args: any[]` | `void` | 输出信息日志 |
58
+ | `warn(...args)` | `...args: any[]` | `void` | 输出警告日志 |
59
+ | `createAndHandleError(message, type, context?)` | `message: string`<br>`type: ErrorType`<br>`context?: any` | `MyOpenLayersError` | 创建并处理错误,通知监听器 |
19
60
 
20
61
  ## 使用示例
21
62
 
63
+ ### 基本验证
64
+
22
65
  ```typescript
23
- import { ErrorHandler, MyOpenLayersError } from 'my-openlayer';
66
+ import { ErrorHandler } from 'my-openlayer';
67
+
68
+ function setCenter(longitude: number, latitude: number) {
69
+ // 验证坐标,如果不合法会自动抛出异常
70
+ ErrorHandler.validateCoordinates(longitude, latitude);
71
+
72
+ // ...设置中心点逻辑
73
+ }
74
+ ```
24
75
 
25
- // 1. 获取实例
26
- const errorHandler = ErrorHandler.getInstance();
76
+ ### 捕获和处理错误
27
77
 
28
- // 2. 注册全局错误监听
29
- errorHandler.addErrorCallback((error) => {
30
- console.error('地图组件发生错误:', error.message);
31
- // 上报错误监控系统
32
- });
78
+ ```typescript
79
+ import { ErrorHandler, ErrorType } from 'my-openlayer';
33
80
 
34
- // 3. 手动验证数据
35
81
  try {
36
- ErrorHandler.validateCoordinates(200, 0); // 将抛出错误,因为经度超出范围
37
- } catch (e) {
38
- // 处理验证错误
82
+ // 可能出错的代码
83
+ throw new Error('Something went wrong');
84
+ } catch (error) {
85
+ ErrorHandler.getInstance().createAndHandleError(
86
+ 'Operation failed',
87
+ ErrorType.COMPONENT_ERROR,
88
+ { originalError: error }
89
+ );
39
90
  }
91
+ ```
92
+
93
+ ### 安全执行函数
94
+
95
+ ```typescript
96
+ import { ErrorHandler } from 'my-openlayer';
40
97
 
41
- // 4. 手动触发错误
42
- errorHandler.handleError(
43
- new MyOpenLayersError('自定义错误消息', 'CUSTOM_ERROR', { detail: '...' })
98
+ const result = ErrorHandler.safeExecute(
99
+ () => {
100
+ // 危险操作
101
+ return JSON.parse(someJsonString);
102
+ },
103
+ 'Failed to parse JSON',
104
+ ErrorType.DATA_ERROR
44
105
  );
45
106
  ```