my-openlayer 1.0.8 → 1.0.9

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/core/Point.d.ts CHANGED
@@ -79,11 +79,12 @@ export default class Point {
79
79
  * 添加vue组件为点位
80
80
  * @param pointDataList 点位信息列表
81
81
  * @param template vue组件模板
82
- * @param Vue Vue实例
82
+ * @param options 选项,包含Vue实例
83
83
  * @returns 返回控制对象,包含显示、隐藏、移除方法
84
84
  * @throws 当参数无效时抛出错误
85
85
  */
86
86
  addVueTemplatePoint(pointDataList: PointData[], template: any, options?: {
87
+ Vue?: any;
87
88
  positioning?: 'bottom-left' | 'bottom-center' | 'bottom-right' | 'center-left' | 'center-center' | 'center-right' | 'top-left' | 'top-center' | 'top-right';
88
89
  stopEvent?: boolean;
89
90
  }): {
package/core/Point.js CHANGED
@@ -321,7 +321,7 @@ export default class Point {
321
321
  * 添加vue组件为点位
322
322
  * @param pointDataList 点位信息列表
323
323
  * @param template vue组件模板
324
- * @param Vue Vue实例
324
+ * @param options 选项,包含Vue实例
325
325
  * @returns 返回控制对象,包含显示、隐藏、移除方法
326
326
  * @throws 当参数无效时抛出错误
327
327
  */
@@ -332,6 +332,9 @@ export default class Point {
332
332
  if (!template) {
333
333
  throw new Error('Vue template is required');
334
334
  }
335
+ if (!options?.Vue) {
336
+ throw new Error('Vue instance is required in options');
337
+ }
335
338
  try {
336
339
  const vueTemplatePoint = new VueTemplatePoint(this.map);
337
340
  return vueTemplatePoint.addVueTemplatePoint(pointDataList, template, options);
@@ -21,6 +21,7 @@ export default class VueTemplatePoint {
21
21
  * @returns 点位控制器
22
22
  */
23
23
  addVueTemplatePoint(pointDataList: any[], template: any, options?: {
24
+ Vue?: any;
24
25
  positioning?: 'bottom-left' | 'bottom-center' | 'bottom-right' | 'center-left' | 'center-center' | 'center-right' | 'top-left' | 'top-center' | 'top-right';
25
26
  stopEvent?: boolean;
26
27
  }): {
@@ -2,52 +2,14 @@ import Overlay from 'ol/Overlay';
2
2
  import { VueTemplatePointState } from '../types';
3
3
  import { ErrorHandler, ErrorType } from '../utils/ErrorHandler';
4
4
  import { ValidationUtils } from '../utils/ValidationUtils';
5
- // 动态导入Vue
6
- let Vue = null;
7
- let isVue3 = false;
8
- // 检测Vue版本并导入
9
- async function detectAndImportVue() {
10
- try {
11
- // 尝试动态导入Vue
12
- const vueModule = await import('vue');
13
- Vue = vueModule.default || vueModule;
14
- // 检测Vue版本
15
- if (Vue && (Vue.version?.startsWith('3') || Vue.createApp)) {
16
- isVue3 = true;
17
- }
18
- else {
19
- isVue3 = false;
20
- }
21
- }
22
- catch (e) {
23
- console.warn('Vue not found. Please ensure Vue is installed in your project.');
24
- Vue = null;
25
- }
26
- }
27
- // 浏览器环境的Vue检测
28
- function detectVueSync() {
29
- try {
30
- // 从全局对象获取Vue(仅浏览器环境)
31
- if (typeof window !== 'undefined' && window.Vue) {
32
- Vue = window.Vue;
33
- isVue3 = !!(Vue.version?.startsWith('3') || Vue.createApp);
34
- }
35
- else {
36
- console.warn('Vue not found in global scope. Please ensure Vue is loaded before this library.');
37
- Vue = null;
38
- }
39
- }
40
- catch (e) {
41
- console.warn('Failed to detect Vue:', e);
42
- Vue = null;
43
- }
5
+ /**
6
+ * 检测Vue版本
7
+ * @param Vue Vue实例
8
+ * @returns 是否为Vue3
9
+ */
10
+ function isVue3(Vue) {
11
+ return !!(Vue && (Vue.version?.startsWith('3') || Vue.createApp));
44
12
  }
45
- // 初始化Vue导入
46
- detectVueSync();
47
- // 注意:不在模块顶层调用异步函数,避免模块变成异步的
48
- // if (!Vue) {
49
- // detectAndImportVue();
50
- // }
51
13
  /**
52
14
  * Vue模板点位管理类
53
15
  * 用于在地图上添加和管理Vue组件覆盖物
@@ -77,7 +39,12 @@ export default class VueTemplatePoint {
77
39
  if (!template) {
78
40
  throw new Error('Vue template is required');
79
41
  }
42
+ if (!options?.Vue) {
43
+ throw new Error('Vue instance is required in options');
44
+ }
80
45
  try {
46
+ const Vue = options.Vue;
47
+ const vueIsVue3 = isVue3(Vue);
81
48
  const instances = [];
82
49
  pointDataList.forEach((pointData) => {
83
50
  if (!ValidationUtils.validateLngLat(pointData.lgtd, pointData.lttd)) {
@@ -96,7 +63,7 @@ export default class VueTemplatePoint {
96
63
  positioning: options?.positioning,
97
64
  stopEvent: options?.stopEvent,
98
65
  };
99
- const instance = new VueTemplatePointInstanceImpl(this.map, pointOptions, this.errorHandler);
66
+ const instance = new VueTemplatePointInstanceImpl(this.map, pointOptions, this.errorHandler, Vue, vueIsVue3);
100
67
  instances.push(instance);
101
68
  this.vuePoints.set(instance.id, instance);
102
69
  });
@@ -156,11 +123,13 @@ export default class VueTemplatePoint {
156
123
  * @internal
157
124
  */
158
125
  class VueTemplatePointInstanceImpl {
159
- constructor(map, options, errorHandler) {
126
+ constructor(map, options, errorHandler, Vue, isVue3) {
160
127
  this.app = null;
161
128
  this.state = VueTemplatePointState.CREATED;
162
129
  this.map = map;
163
130
  this.errorHandler = errorHandler;
131
+ this.Vue = Vue;
132
+ this.isVue3 = isVue3;
164
133
  this.options = this.mergeDefaultOptions(options);
165
134
  this.id = this.generateUniqueId();
166
135
  this.position = [this.options.lgtd, this.options.lttd];
@@ -169,31 +138,18 @@ class VueTemplatePointInstanceImpl {
169
138
  this.validateConstructorParams(map, options);
170
139
  // 创建DOM元素
171
140
  this.dom = this.createDomElement();
141
+ // 创建Vue应用实例
142
+ this.createVueApp();
172
143
  // 创建覆盖层
173
144
  this.anchor = this.createOverlay();
174
145
  // 添加到地图
175
146
  this.map.addOverlay(this.anchor);
176
- // 异步创建Vue应用实例
177
- this.initializeVueApp();
178
- }
179
- catch (error) {
180
- this.handleError('Failed to create Vue template point', error, { map, options });
181
- throw error;
182
- }
183
- }
184
- /**
185
- * 异步初始化Vue应用
186
- * @private
187
- */
188
- async initializeVueApp() {
189
- try {
190
- await this.createVueApp();
191
147
  // 设置初始状态
192
148
  this.state = VueTemplatePointState.MOUNTED;
193
149
  this.setVisible(this.options.visible ?? true);
194
150
  }
195
151
  catch (error) {
196
- this.handleError('Failed to initialize Vue app', error);
152
+ this.handleError('Failed to create Vue template point', error, { map, options });
197
153
  throw error;
198
154
  }
199
155
  }
@@ -256,24 +212,15 @@ class VueTemplatePointInstanceImpl {
256
212
  * 创建Vue应用实例
257
213
  * @private
258
214
  */
259
- async createVueApp() {
215
+ createVueApp() {
260
216
  const { Template, props } = this.options;
261
- // 如果Vue不可用,尝试异步导入
262
- if (!Vue) {
263
- try {
264
- await detectAndImportVue();
265
- }
266
- catch (error) {
267
- throw new Error('Vue is not available. Please ensure Vue is installed in your project.');
268
- }
269
- }
270
- if (!Vue) {
271
- throw new Error('Vue is not available. Please ensure Vue is installed in your project.');
217
+ if (!this.Vue) {
218
+ throw new Error('Vue is not available. Please ensure Vue instance is provided.');
272
219
  }
273
220
  try {
274
- if (isVue3) {
221
+ if (this.isVue3) {
275
222
  // Vue 3
276
- this.app = Vue.createApp({
223
+ this.app = this.Vue.createApp({
277
224
  ...Template,
278
225
  props: props || {}
279
226
  });
@@ -281,7 +228,7 @@ class VueTemplatePointInstanceImpl {
281
228
  }
282
229
  else {
283
230
  // Vue 2
284
- this.app = new Vue({
231
+ this.app = new this.Vue({
285
232
  el: this.dom,
286
233
  render: (h) => h(Template, { props: props || {} })
287
234
  });
@@ -373,7 +320,7 @@ class VueTemplatePointInstanceImpl {
373
320
  * @param newProps 新的属性对象
374
321
  * @throws 当操作失败时抛出错误
375
322
  */
376
- async updateProps(newProps) {
323
+ updateProps(newProps) {
377
324
  if (this.state === VueTemplatePointState.DESTROYED) {
378
325
  throw new Error('Cannot update props on destroyed DOM point');
379
326
  }
@@ -381,7 +328,7 @@ class VueTemplatePointInstanceImpl {
381
328
  // 重新创建Vue应用实例
382
329
  this.destroyVueApp();
383
330
  this.options.props = { ...this.options.props, ...newProps };
384
- await this.createVueApp();
331
+ this.createVueApp();
385
332
  }
386
333
  catch (error) {
387
334
  this.handleError('Failed to update props', error, { newProps });
@@ -448,7 +395,7 @@ class VueTemplatePointInstanceImpl {
448
395
  destroyVueApp() {
449
396
  if (this.app) {
450
397
  try {
451
- if (isVue3) {
398
+ if (this.isVue3) {
452
399
  // Vue 3: 使用 unmount
453
400
  this.app.unmount();
454
401
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "my-openlayer",
3
3
  "private": false,
4
- "version": "1.0.8",
4
+ "version": "1.0.9",
5
5
  "type": "module",
6
6
  "main": "index.js",
7
7
  "types": "index.d.ts",
package/types.d.ts CHANGED
@@ -290,7 +290,7 @@ export interface VueTemplatePointInstance {
290
290
  options: VueTemplatePointOptions;
291
291
  setVisible(visible: boolean): void;
292
292
  updatePosition(lgtd: number, lttd: number): void;
293
- updateProps(newProps: Record<string, any>): Promise<void>;
293
+ updateProps(newProps: Record<string, any>): void;
294
294
  setStyle(styles: Partial<CSSStyleDeclaration>): void;
295
295
  addClass(className: string): void;
296
296
  removeClass(className: string): void;