Skip to content

Commit b476468

Browse files
committed
fix(ol-source-vector): make property changes reactive
1 parent c00d992 commit b476468

File tree

4 files changed

+60
-17
lines changed

4 files changed

+60
-17
lines changed

docs/.vitepress/theme/demo.css

+3-1
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,17 @@ main form {
7676
align-items: baseline;
7777
gap: 1rem;
7878
margin-bottom: 1rem;
79+
flex-flow: column;
7980
}
8081

8182
main form fieldset {
8283
margin: 0;
8384
padding: 0;
8485
display: flex;
85-
align-items: baseline;
86+
align-items: center;
8687
gap: 1rem;
8788
border: none;
89+
flex-wrap: wrap;
8890
}
8991

9092
main form label {

src/components/sources/OlSourceVector.vue

+27-11
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ import type VectorLayer from "ol/layer/Vector";
1010
import type HeatmapLayer from "ol/layer/Heatmap";
1111
import type { WebGLVectorLayer } from "../layers/WebGLVectorLayerClass";
1212
import type { Ref } from "vue";
13-
import { inject, watch, onMounted, onUnmounted, provide, ref } from "vue";
13+
import {
14+
inject,
15+
watch,
16+
onMounted,
17+
onUnmounted,
18+
provide,
19+
shallowRef,
20+
} from "vue";
1421
import type Geometry from "ol/geom/Geometry";
1522
import usePropsAsObjectProperties from "@/composables/usePropsAsObjectProperties";
1623
import {
@@ -43,36 +50,45 @@ const layer = heatmapLayer || vectorLayer || webglVectorLayer;
4350
4451
const { properties } = usePropsAsObjectProperties(props);
4552
46-
const createSource = () => new VectorSource(properties);
47-
48-
let source = createSource();
53+
const source = shallowRef(new VectorSource(properties));
4954
5055
useOpenLayersEvents(source, FEATURE_EVENTS);
5156
5257
const applySource = () => {
53-
layer?.value?.setSource(null);
54-
source = createSource();
55-
layer?.value?.setSource(source);
58+
const existingSource = layer?.value.getSource();
59+
if (existingSource) {
60+
for (const key in properties) {
61+
const keyInObj = key as keyof typeof properties;
62+
if (properties[keyInObj]) {
63+
existingSource.set(key, properties[keyInObj]);
64+
}
65+
}
66+
} else {
67+
layer?.value.setSource(source.value);
68+
}
5669
};
57-
watch(properties, () => applySource());
70+
watch(
71+
() => properties,
72+
() => applySource(),
73+
);
5874
5975
watch(
6076
() => layer?.value,
6177
() => applySource(),
6278
);
6379
6480
onMounted(() => {
65-
layer?.value.setSource(source);
81+
applySource();
6682
});
6783
6884
onUnmounted(() => {
6985
layer?.value.setSource(null);
7086
});
7187
72-
provide("vectorSource", ref(source));
88+
provide("vectorSource", source);
7389
7490
defineExpose({
7591
layer,
76-
source: ref(source),
92+
source,
7793
});
7894
</script>

src/demos/CircleDemo.vue

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
<template>
2+
<form>
3+
<label for="radius">Radius:</label>
4+
<input type="number" id="radius" step="0.01" min="0.01" v-model="radius" />
5+
</form>
26
<ol-map
37
:loadTilesWhileAnimating="true"
48
:loadTilesWhileInteracting="true"
@@ -18,7 +22,7 @@
1822
<ol-vector-layer>
1923
<ol-source-vector>
2024
<ol-feature>
21-
<ol-geom-circle :center="[30, 40]" :radius="0.2"></ol-geom-circle>
25+
<ol-geom-circle :center="[30, 40]" :radius="radius"></ol-geom-circle>
2226
<ol-style>
2327
<ol-style-stroke color="red" :width="3"></ol-style-stroke>
2428
<ol-style-fill color="rgba(255,200,0,0.2)"></ol-style-fill>
@@ -35,4 +39,5 @@ import { ref } from "vue";
3539
const center = ref([30, 40]);
3640
const projection = ref("EPSG:4326");
3741
const zoom = ref(10);
42+
const radius = ref(0.2);
3843
</script>

src/demos/GeomPoint.vue

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11
<template>
2+
<form>
3+
<fieldset>
4+
<label for="fill">Fill:</label>
5+
<input type="color" id="fill" v-model="fill" />
6+
<label for="stroke">Stroke:</label>
7+
<input type="color" id="stroke" v-model="stroke" />
8+
</fieldset>
9+
<fieldset>
10+
<label for="strokeWidth">StrokeWidth:</label>
11+
<input
12+
type="number"
13+
id="strokeWidth"
14+
step="1"
15+
min="0"
16+
v-model="strokeWidth"
17+
/>
18+
<label for="radius">Radius:</label>
19+
<input type="number" id="radius" step="1" min="1" v-model="radius" />
20+
</fieldset>
21+
</form>
222
<button class="btn-default" @click="changeCoordinate" type="button">
323
change coordinates
424
</button>
@@ -24,9 +44,9 @@
2444
<ol-geom-point :coordinates="coordinate"></ol-geom-point>
2545
<ol-style>
2646
<ol-style-circle :radius="radius">
27-
<ol-style-fill :color="fillColor"></ol-style-fill>
47+
<ol-style-fill :color="fill"></ol-style-fill>
2848
<ol-style-stroke
29-
:color="strokeColor"
49+
:color="stroke"
3050
:width="strokeWidth"
3151
></ol-style-stroke>
3252
</ol-style-circle>
@@ -45,8 +65,8 @@ const projection = ref("EPSG:4326");
4565
const zoom = ref(8);
4666
const radius = ref(40);
4767
const strokeWidth = ref(10);
48-
const strokeColor = ref("red");
49-
const fillColor = ref("white");
68+
const stroke = ref("#ff0000");
69+
const fill = ref("#ffffff");
5070
const coordinate = ref([40, 40]);
5171
5272
function changeCoordinate() {

0 commit comments

Comments
 (0)