From 8171eeaccc7a5cb3444cd4277ad52fd4a1b6424b Mon Sep 17 00:00:00 2001 From: Danny Koppenhagen Date: Wed, 22 May 2024 08:11:34 +0200 Subject: [PATCH] refactor(composables): move prop handling and class creation into `useLayer` composable --- docs/pluginsguide/index.md | 28 +++++++++++---- .../layers/OlAnimatedClusterLayer.vue | 34 +++++++------------ .../layers/OlAnimatedClusterlayer.vue | 34 +++++++------------ src/components/layers/OlHeatmapLayer.vue | 13 +++---- src/components/layers/OlImageLayer.vue | 12 +++---- src/components/layers/OlTileLayer.vue | 12 +++---- src/components/layers/OlVectorImageLayer.vue | 14 +++----- src/components/layers/OlVectorLayer.vue | 14 +++----- src/components/layers/OlVectorTileLayer.vue | 14 +++----- src/components/layers/OlWebglTileLayer.vue | 12 +++---- src/components/layers/OlWebglVectorLayer.vue | 20 ++++------- src/composables/__tests__/useLayer.spec.ts | 10 ++---- src/composables/useLayer.ts | 32 ++++++++++++----- 13 files changed, 112 insertions(+), 137 deletions(-) diff --git a/docs/pluginsguide/index.md b/docs/pluginsguide/index.md index ad963b8..31aa46a 100644 --- a/docs/pluginsguide/index.md +++ b/docs/pluginsguide/index.md @@ -41,15 +41,31 @@ const props = withDefaults( }, ); -const properties = usePropsAsObjectProperties(props); - -const layer = shallowRef(new FooLayer(properties)); -useLayer(layer, props); +// Create the layer +// Changes will be applied and the layer will be removed on unmount. +// The last parameter will receive the event names which should be handled by the target component. +// Check out the sources of the composable for more details. +const { layer } = useLayer(FooLayer, props, ['change:opacity']); -provide("layer", layer); +// source components will rely on the layer (depends on the source type) +provide("vectorLayer", layer); +// provide("heatmapLayer", layer); +// provide("imageLayer", layer); +// provide("tileLayer", layer); +// provide("vectorImageLayer", layer); +// provide("vectorTileLayer", layer); +// provide("webglVectorLayer", layer); defineExpose({ - layer, + // see above ("provide") + vectorLayer: layer + // vectorLayer + // heatmapLayer + // imageLayer + // tileLayer + // vectorImageLayer + // vectorTileLayer + // webglVectorLayer }); ``` diff --git a/src/components/layers/OlAnimatedClusterLayer.vue b/src/components/layers/OlAnimatedClusterLayer.vue index 45563d1..799da18 100644 --- a/src/components/layers/OlAnimatedClusterLayer.vue +++ b/src/components/layers/OlAnimatedClusterLayer.vue @@ -5,11 +5,10 @@ diff --git a/src/components/layers/OlAnimatedClusterlayer.vue b/src/components/layers/OlAnimatedClusterlayer.vue index 45563d1..799da18 100644 --- a/src/components/layers/OlAnimatedClusterlayer.vue +++ b/src/components/layers/OlAnimatedClusterlayer.vue @@ -5,11 +5,10 @@ diff --git a/src/components/layers/OlHeatmapLayer.vue b/src/components/layers/OlHeatmapLayer.vue index 823b8dd..785845d 100644 --- a/src/components/layers/OlHeatmapLayer.vue +++ b/src/components/layers/OlHeatmapLayer.vue @@ -5,10 +5,9 @@ diff --git a/src/components/layers/OlImageLayer.vue b/src/components/layers/OlImageLayer.vue index 274d1ef..3bd40dd 100644 --- a/src/components/layers/OlImageLayer.vue +++ b/src/components/layers/OlImageLayer.vue @@ -5,9 +5,8 @@ diff --git a/src/components/layers/OlTileLayer.vue b/src/components/layers/OlTileLayer.vue index 7e5a83e..7634c28 100644 --- a/src/components/layers/OlTileLayer.vue +++ b/src/components/layers/OlTileLayer.vue @@ -5,9 +5,8 @@ diff --git a/src/components/layers/OlVectorImageLayer.vue b/src/components/layers/OlVectorImageLayer.vue index 54c9232..002de73 100644 --- a/src/components/layers/OlVectorImageLayer.vue +++ b/src/components/layers/OlVectorImageLayer.vue @@ -5,10 +5,9 @@ diff --git a/src/components/layers/OlVectorLayer.vue b/src/components/layers/OlVectorLayer.vue index 66a1eea..7ae9568 100644 --- a/src/components/layers/OlVectorLayer.vue +++ b/src/components/layers/OlVectorLayer.vue @@ -5,9 +5,8 @@ diff --git a/src/components/layers/OlVectorTileLayer.vue b/src/components/layers/OlVectorTileLayer.vue index 0a51bd6..28ec988 100644 --- a/src/components/layers/OlVectorTileLayer.vue +++ b/src/components/layers/OlVectorTileLayer.vue @@ -5,11 +5,10 @@ diff --git a/src/components/layers/OlWebglTileLayer.vue b/src/components/layers/OlWebglTileLayer.vue index cd36b18..df0c171 100644 --- a/src/components/layers/OlWebglTileLayer.vue +++ b/src/components/layers/OlWebglTileLayer.vue @@ -5,22 +5,18 @@ diff --git a/src/components/layers/OlWebglVectorLayer.vue b/src/components/layers/OlWebglVectorLayer.vue index 6646b86..dca272c 100644 --- a/src/components/layers/OlWebglVectorLayer.vue +++ b/src/components/layers/OlWebglVectorLayer.vue @@ -5,9 +5,7 @@ diff --git a/src/composables/__tests__/useLayer.spec.ts b/src/composables/__tests__/useLayer.spec.ts index 69e011e..e686552 100644 --- a/src/composables/__tests__/useLayer.spec.ts +++ b/src/composables/__tests__/useLayer.spec.ts @@ -1,7 +1,6 @@ import { describe, expect, it, vi } from "vitest"; import useLayer from "../useLayer"; -import usePropsAsObjectProperties from "../usePropsAsObjectProperties"; -import { defineComponent, ref, shallowRef } from "vue"; +import { defineComponent, ref } from "vue"; import { shallowMount } from "@vue/test-utils"; import { Layer } from "ol/layer"; import { layersCommonDefaultProps } from "@components/layers/LayersCommonProps"; @@ -15,12 +14,7 @@ describe("useLayer", () => { template: "", props: Object.keys(layersCommonDefaultProps), setup(props) { - const properties = usePropsAsObjectProperties( - props, - ) as typeof layersCommonDefaultProps; - const layer = shallowRef(new Layer(properties)); - useLayer(layer, properties); - return { layer }; + return useLayer(Layer, props); }, }); diff --git a/src/composables/useLayer.ts b/src/composables/useLayer.ts index a29f288..f51334c 100644 --- a/src/composables/useLayer.ts +++ b/src/composables/useLayer.ts @@ -1,17 +1,30 @@ -import { inject, onUnmounted, ref, type Ref, watch } from "vue"; +import { inject, onUnmounted, ref, type Ref, shallowRef, watch } from "vue"; import type { Map } from "ol"; import type LayerGroup from "ol/layer/Group"; import type { Layer } from "ol/layer"; -import type usePropsAsObjectProperties from "./usePropsAsObjectProperties"; +import usePropsAsObjectProperties from "./usePropsAsObjectProperties"; +import { useOpenLayersEvents } from "./useOpenLayersEvents"; import type { OverviewMap } from "ol/control"; -import type { layersCommonDefaultProps } from "@/components/layers/LayersCommonProps"; -export default function useLayer( - layer: Ref, - properties: typeof layersCommonDefaultProps & - ReturnType, +/** + * Create a Layer + * @param LayerClass The Class of the source which should be created + * @param props The properties which should be passed to the LayerClass + * @param eventsToHandle The event names list for events that should be passed from the layer through the component + */ +export default function useLayer( + // eslint-disable-next-line + LayerClass: new (...args: any[]) => T, + props: ConstructorParameters[0], + eventsToHandle: string[] = [], ) { + const properties = usePropsAsObjectProperties(props); + + const layer = shallowRef(new LayerClass(properties)); + + useOpenLayersEvents(layer, eventsToHandle); + const map = inject("map"); const layerGroup = inject("layerGroup", null); const overviewMap = inject | null>( @@ -71,5 +84,8 @@ export default function useLayer( }); } - return {}; + return { + layer, + map, + }; }