Skip to content

Commit c69f4e8

Browse files
committed
feat(ol-style): define and export OverrideStyleFunction which now also includes the resolution as third parameter
You can now either: - modify an existing style - return a new style - return an array of new styles closes #191
1 parent 7eaafab commit c69f4e8

File tree

5 files changed

+42
-34
lines changed

5 files changed

+42
-34
lines changed

docs/componentsguide/styles/style/index.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,13 @@ import StyleDemo2 from "@demos/StyleDemo2.vue"
4141

4242
- **Type**: `Number`
4343

44-
### overrideStyleFunction
44+
### `overrideStyleFunction`
4545

46-
- **Type**: `Function`
46+
- **Type**: `OverrideStyleFunction`
4747

48-
Change the style forexample in cluster sytem you can change the feature count in text style (see cluster doc)
48+
Change the style for example in cluster you can change the feature count in text style (see cluster doc)
49+
The function has three arguments:
50+
51+
1. `feature: FeatureLike`: The feature the style is related to.
52+
2. `currentStyle: Style`: The current style that's applied (you can override it here)
53+
3. `resolution?: number`: A number representing the view's resolution

src/components/layers/OlLayerGroup.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ defineOptions({
1717
inheritAttrs: false,
1818
});
1919
20-
const props = withDefaults(defineProps<Options>(), layersCommonDefaultProps);
20+
const props = withDefaults(
21+
defineProps<Options & { className?: string }>(),
22+
layersCommonDefaultProps
23+
);
2124
2225
const map = inject<Map>("map");
2326
const { properties } = usePropsAsObjectProperties(props);

src/components/styles/OlStyle.vue

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,27 @@ import Style, { type StyleLike } from "ol/style/Style";
1212
import Draw from "ol/interaction/Draw";
1313
import Modify from "ol/interaction/Modify";
1414
import usePropsAsObjectProperties from "@/composables/usePropsAsObjectProperties";
15+
import type Feature from "ol/Feature";
16+
import type { Layer } from "ol/layer";
17+
import type { Select } from "ol/interaction";
18+
import type { OverrideStyleFunction } from ".";
1519
1620
const props = withDefaults(
1721
defineProps<{
1822
zIndex?: number;
19-
overrideStyleFunction?: (...args: unknown[]) => unknown;
23+
overrideStyleFunction?: OverrideStyleFunction;
2024
}>(),
2125
{}
2226
);
2327
24-
const styledObj = inject<Ref<Draw | Modify | Style> | undefined>(
25-
"stylable",
26-
undefined
27-
);
28+
const styledObj = inject<
29+
Ref<Draw | Modify | Select | Feature | Layer> | undefined
30+
>("stylable", undefined);
2831
const { properties } = usePropsAsObjectProperties(props);
2932
3033
const style = computed(() => new Style(properties));
3134
32-
const setStyle = (val: StyleLike | null) => {
35+
const setStyle = (val: StyleLike) => {
3336
const st = styledObj?.value;
3437
if (!st) {
3538
return;
@@ -42,50 +45,38 @@ const setStyle = (val: StyleLike | null) => {
4245
try {
4346
// @ts-ignore
4447
st.setStyle(val);
45-
// @ts-ignore
4648
st.changed();
47-
// @ts-ignore
4849
st.dispatchEvent("styleChanged");
4950
} catch (error) {
5051
// @ts-ignore
5152
st.style_ = val;
5253
// @ts-ignore
5354
st.values_.style = val;
54-
// @ts-ignore
5555
st.changed();
56-
// @ts-ignore
5756
st.dispatchEvent("styleChanged");
5857
}
5958
}
6059
};
6160
6261
const styleFunc = computed<StyleLike>(() => {
63-
return (feature) => {
64-
if (properties.overrideStyleFunction != null) {
65-
properties.overrideStyleFunction(feature, style.value);
62+
return (feature, resolution) => {
63+
if (properties.overrideStyleFunction) {
64+
properties.overrideStyleFunction(feature, style.value, resolution);
6665
}
6766
return style.value;
6867
};
6968
});
7069
71-
watch(properties, () => {
72-
if (!properties.overrideStyleFunction === null) {
73-
setStyle(style.value);
74-
} else {
75-
setStyle(styleFunc.value);
76-
}
77-
});
70+
watch(properties, () =>
71+
setStyle(properties.overrideStyleFunction ? styleFunc.value : style.value)
72+
);
7873
79-
onMounted(() => {
80-
if (!properties.overrideStyleFunction) {
81-
setStyle(style.value);
82-
} else {
83-
setStyle(styleFunc.value);
84-
}
85-
});
74+
onMounted(() =>
75+
setStyle(properties.overrideStyleFunction ? styleFunc.value : style.value)
76+
);
8677
8778
onUnmounted(() => {
88-
setStyle(null);
79+
setStyle(new Style());
8980
});
9081
9182
provide("style", style);

src/components/styles/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,17 @@ import OlStyleFill from "./OlStyleFill.vue";
66
import OlStyleIcon from "./OlStyleIcon.vue";
77
import OlStyleText from "./OlStyleText.vue";
88
import OlStyleFlowline from "./OlStyleFlowline.vue";
9+
import type { FeatureLike } from "ol/Feature";
10+
import type { Style } from "ol/style";
911

1012
let installed = false;
1113

14+
type OverrideStyleFunction = (
15+
feature: FeatureLike,
16+
currentStyle: Style,
17+
resolution: number
18+
) => Style | Style[] | void;
19+
1220
function install(app: App) {
1321
if (installed) return;
1422
installed = true;
@@ -33,4 +41,5 @@ export {
3341
OlStyleText,
3442
OlStyleFlowline,
3543
OlStyleCircle,
44+
type OverrideStyleFunction,
3645
};

src/demos/ClusterDemo.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ const projection = ref("EPSG:4326");
5555
const zoom = ref(6.8);
5656
const rotation = ref(0);
5757
58-
const overrideStyleFunction = (feature, style) => {
58+
const overrideStyleFunction = (feature, style, resolution) => {
59+
console.log({ feature, style, resolution });
5960
const clusteredFeatures = feature.get("features");
6061
const size = clusteredFeatures.length;
61-
6262
style.getText().setText(size.toString());
6363
};
6464

0 commit comments

Comments
 (0)