Skip to content

Commit 1ff9cf4

Browse files
committed
refactor: make all provided styleable's a computed
closes #178 BREAKING CHANGE: If you may have used `inject("stylable")` to access the style for `ol-interaction-draw` or `ol-interaction-modify`, you will now receive a `computed` instead of the direct value. You can access the value by using `.value`. **BEFORE**: ```vue const drawStyleable = inject("stylable") ``` **AFTER**: ```vue const drawStyleable = inject("stylable").value ```
1 parent 4318ec8 commit 1ff9cf4

File tree

3 files changed

+32
-32
lines changed

3 files changed

+32
-32
lines changed

src/components/interaction/DrawInteraction.vue

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import {
1010
onMounted,
1111
onUnmounted,
1212
toRefs,
13-
//computed
13+
computed,
1414
} from "vue";
1515
1616
import Draw from "ol/interaction/Draw";
17+
import { Style } from "ol/style";
1718
//import Style from 'ol/style/Style';
1819
1920
export default {
@@ -40,8 +41,8 @@ export default {
4041
wrapX,
4142
} = toRefs(props);
4243
43-
const createDraw = () => {
44-
const draw = new Draw({
44+
const draw = computed(() => {
45+
const d = new Draw({
4546
source: source.value,
4647
type: type.value,
4748
clickTolerance: clickTolerance.value,
@@ -59,18 +60,16 @@ export default {
5960
wrapX: wrapX.value,
6061
});
6162
62-
draw.on("drawstart", (event) => {
63+
d.on("drawstart", (event) => {
6364
emit("drawstart", event);
6465
});
6566
66-
draw.on("drawend", (event) => {
67+
d.on("drawend", (event) => {
6768
emit("drawend", event);
6869
});
6970
70-
return draw;
71-
};
72-
73-
let draw = createDraw();
71+
return d;
72+
});
7473
7574
watch(
7675
[
@@ -90,21 +89,20 @@ export default {
9089
wrapX,
9190
],
9291
() => {
93-
map.removeInteraction(draw);
94-
draw = createDraw();
95-
map.addInteraction(draw);
96-
draw.changed();
92+
map.removeInteraction(draw.value);
93+
map.addInteraction(draw.value);
94+
draw.value.changed();
9795
9896
map.changed();
9997
}
10098
);
10199
102100
onMounted(() => {
103-
map.addInteraction(draw);
101+
map.addInteraction(draw.value);
104102
});
105103
106104
onUnmounted(() => {
107-
map.removeInteraction(draw);
105+
map.removeInteraction(draw.value);
108106
});
109107
110108
provide("stylable", draw);

src/components/interaction/ModifyInteraction.vue

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import {
1010
onMounted,
1111
onUnmounted,
1212
toRefs,
13-
// computed
13+
computed,
1414
} from "vue";
1515
1616
import Collection from "ol/Collection";
1717
import Modify from "ol/interaction/Modify";
18+
import { Style } from "ol/style";
1819
//import Style from 'ol/style/Style';
1920
//import usePropsAsObjectProperties from '@/composables/usePropsAsObjectProperties'
2021
@@ -35,8 +36,8 @@ export default {
3536
hitDetection,
3637
} = toRefs(props);
3738
38-
const createModify = () => {
39-
const modify = new Modify({
39+
const modify = computed(() => {
40+
const m = new Modify({
4041
source: source.value,
4142
features: features.value,
4243
condition: condition.value,
@@ -47,18 +48,16 @@ export default {
4748
hitDetection: hitDetection.value,
4849
});
4950
50-
modify.on("modifystart", (event) => {
51+
m.on("modifystart", (event) => {
5152
emit("modifystart", event);
5253
});
5354
54-
modify.on("modifyend", (event) => {
55+
m.on("modifyend", (event) => {
5556
emit("modifyend", event);
5657
});
5758
58-
return modify;
59-
};
60-
61-
let modify = createModify();
59+
return m;
60+
});
6261
6362
watch(
6463
[
@@ -70,21 +69,20 @@ export default {
7069
hitDetection,
7170
],
7271
() => {
73-
map.removeInteraction(modify);
74-
modify = createModify();
75-
map.addInteraction(modify);
76-
modify.changed();
72+
map.removeInteraction(modify.value);
73+
map.addInteraction(modify.value);
74+
modify.value.changed();
7775
7876
map.changed();
7977
}
8078
);
8179
8280
onMounted(() => {
83-
map.addInteraction(modify);
81+
map.addInteraction(modify.value);
8482
});
8583
8684
onUnmounted(() => {
87-
map.removeInteraction(modify);
85+
map.removeInteraction(modify.value);
8886
});
8987
9088
provide("stylable", modify);

src/components/styles/Style.vue

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@ export default {
2222
const style = computed(() => new Style(properties));
2323
2424
const setStyle = (val) => {
25-
if (styledObj instanceof Draw || styledObj instanceof Modify) {
26-
styledObj.getOverlay().setStyle(val);
25+
if (
26+
styledObj.value instanceof Draw ||
27+
styledObj.value instanceof Modify
28+
) {
29+
styledObj.value.getOverlay().setStyle(val);
30+
styledObj.value.changed();
2731
styledObj.value.dispatchEvent("styleChanged");
2832
return;
2933
}

0 commit comments

Comments
 (0)