my-openlayer 2.1.0 → 2.1.1
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/SelectHandler.d.ts +11 -2
- package/core/SelectHandler.js +52 -2
- package/package.json +1 -1
package/core/SelectHandler.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import Map from "ol/Map";
|
|
2
2
|
import { FeatureLike } from "ol/Feature";
|
|
3
|
+
import { Style } from "ol/style";
|
|
3
4
|
import { SelectOptions, SelectMode } from "../types";
|
|
4
5
|
/**
|
|
5
6
|
* 要素选择处理器类
|
|
@@ -80,17 +81,19 @@ export default class SelectHandler {
|
|
|
80
81
|
* 通过要素ID选择要素
|
|
81
82
|
* @param featureIds 要素ID数组
|
|
82
83
|
* @param layerName 图层名称(可选)
|
|
84
|
+
* @param selectStyle 选中样式(可选,仅作用于此次选择)
|
|
83
85
|
* @returns SelectHandler 实例(支持链式调用)
|
|
84
86
|
*/
|
|
85
|
-
selectByIds(featureIds: string[], layerName?: string): this;
|
|
87
|
+
selectByIds(featureIds: string[], layerName?: string, selectStyle?: Style | Style[] | ((feature: FeatureLike) => Style | Style[])): this;
|
|
86
88
|
/**
|
|
87
89
|
* 通过属性选择要素
|
|
88
90
|
* @param propertyName 属性名称
|
|
89
91
|
* @param propertyValue 属性值
|
|
90
92
|
* @param layerName 图层名称(可选)
|
|
93
|
+
* @param selectStyle 选中样式(可选,仅作用于此次选择)
|
|
91
94
|
* @returns SelectHandler 实例(支持链式调用)
|
|
92
95
|
*/
|
|
93
|
-
selectByProperty(propertyName: string, propertyValue: any, layerName?: string): this;
|
|
96
|
+
selectByProperty(propertyName: string, propertyValue: any, layerName?: string, selectStyle?: Style | Style[] | ((feature: FeatureLike) => Style | Style[])): this;
|
|
94
97
|
/**
|
|
95
98
|
* 判断选择是否已启用
|
|
96
99
|
* @returns 是否已启用
|
|
@@ -130,6 +133,12 @@ export default class SelectHandler {
|
|
|
130
133
|
* @private
|
|
131
134
|
*/
|
|
132
135
|
private createSelectStyle;
|
|
136
|
+
/**
|
|
137
|
+
* 更新选择样式
|
|
138
|
+
* @param selectStyle 新的选择样式
|
|
139
|
+
* @returns SelectHandler 实例(支持链式调用)
|
|
140
|
+
*/
|
|
141
|
+
updateSelectStyle(selectStyle: Style | Style[] | ((feature: FeatureLike) => Style | Style[])): this;
|
|
133
142
|
/**
|
|
134
143
|
* 附加事件监听器
|
|
135
144
|
* @private
|
package/core/SelectHandler.js
CHANGED
|
@@ -69,6 +69,8 @@ export default class SelectHandler {
|
|
|
69
69
|
this.map = map;
|
|
70
70
|
this.eventManager = new EventManager(map);
|
|
71
71
|
this.errorHandler = ErrorHandler.getInstance();
|
|
72
|
+
// 默认启用点击选择模式,支持多选
|
|
73
|
+
this.enableSelect('click', { multi: true });
|
|
72
74
|
}
|
|
73
75
|
/**
|
|
74
76
|
* 启用要素选择
|
|
@@ -145,9 +147,10 @@ export default class SelectHandler {
|
|
|
145
147
|
* 通过要素ID选择要素
|
|
146
148
|
* @param featureIds 要素ID数组
|
|
147
149
|
* @param layerName 图层名称(可选)
|
|
150
|
+
* @param selectStyle 选中样式(可选,仅作用于此次选择)
|
|
148
151
|
* @returns SelectHandler 实例(支持链式调用)
|
|
149
152
|
*/
|
|
150
|
-
selectByIds(featureIds, layerName) {
|
|
153
|
+
selectByIds(featureIds, layerName, selectStyle) {
|
|
151
154
|
try {
|
|
152
155
|
if (!this.selectInteraction) {
|
|
153
156
|
console.warn('选择交互未启用,无法选择要素');
|
|
@@ -174,6 +177,15 @@ export default class SelectHandler {
|
|
|
174
177
|
for (const featureId of featureIds) {
|
|
175
178
|
const feature = source.getFeatureById(featureId);
|
|
176
179
|
if (feature) {
|
|
180
|
+
// 如果传入了自定义样式,仅为该要素设置样式
|
|
181
|
+
if (selectStyle) {
|
|
182
|
+
if (typeof selectStyle === 'function') {
|
|
183
|
+
feature.setStyle(selectStyle(feature));
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
feature.setStyle(selectStyle);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
177
189
|
this.selectInteraction.getFeatures().push(feature);
|
|
178
190
|
}
|
|
179
191
|
}
|
|
@@ -191,9 +203,10 @@ export default class SelectHandler {
|
|
|
191
203
|
* @param propertyName 属性名称
|
|
192
204
|
* @param propertyValue 属性值
|
|
193
205
|
* @param layerName 图层名称(可选)
|
|
206
|
+
* @param selectStyle 选中样式(可选,仅作用于此次选择)
|
|
194
207
|
* @returns SelectHandler 实例(支持链式调用)
|
|
195
208
|
*/
|
|
196
|
-
selectByProperty(propertyName, propertyValue, layerName) {
|
|
209
|
+
selectByProperty(propertyName, propertyValue, layerName, selectStyle) {
|
|
197
210
|
try {
|
|
198
211
|
if (!this.selectInteraction) {
|
|
199
212
|
console.warn('选择交互未启用,无法选择要素');
|
|
@@ -219,6 +232,15 @@ export default class SelectHandler {
|
|
|
219
232
|
const features = source.getFeatures();
|
|
220
233
|
for (const feature of features) {
|
|
221
234
|
if (feature.get(propertyName) === propertyValue) {
|
|
235
|
+
// 如果传入了自定义样式,仅为该要素设置样式
|
|
236
|
+
if (selectStyle) {
|
|
237
|
+
if (typeof selectStyle === 'function') {
|
|
238
|
+
feature.setStyle(selectStyle(feature));
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
feature.setStyle(selectStyle);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
222
244
|
this.selectInteraction.getFeatures().push(feature);
|
|
223
245
|
}
|
|
224
246
|
}
|
|
@@ -357,6 +379,34 @@ export default class SelectHandler {
|
|
|
357
379
|
}
|
|
358
380
|
};
|
|
359
381
|
}
|
|
382
|
+
/**
|
|
383
|
+
* 更新选择样式
|
|
384
|
+
* @param selectStyle 新的选择样式
|
|
385
|
+
* @returns SelectHandler 实例(支持链式调用)
|
|
386
|
+
*/
|
|
387
|
+
updateSelectStyle(selectStyle) {
|
|
388
|
+
if (!this.selectInteraction) {
|
|
389
|
+
console.warn('选择交互未启用,无法更新样式');
|
|
390
|
+
return this;
|
|
391
|
+
}
|
|
392
|
+
try {
|
|
393
|
+
// 更新选择交互的样式
|
|
394
|
+
this.selectInteraction.getStyle = () => {
|
|
395
|
+
if (typeof selectStyle === 'function') {
|
|
396
|
+
return selectStyle;
|
|
397
|
+
}
|
|
398
|
+
return selectStyle;
|
|
399
|
+
};
|
|
400
|
+
// 触发样式更新
|
|
401
|
+
const features = this.selectInteraction.getFeatures();
|
|
402
|
+
features.changed();
|
|
403
|
+
return this;
|
|
404
|
+
}
|
|
405
|
+
catch (error) {
|
|
406
|
+
this.errorHandler.handleError(new MyOpenLayersError(`更新选择样式失败: ${error instanceof Error ? error.message : '未知错误'}`, ErrorType.COMPONENT_ERROR, { selectStyle }));
|
|
407
|
+
throw error;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
360
410
|
/**
|
|
361
411
|
* 附加事件监听器
|
|
362
412
|
* @private
|