From 92844692beccc84c2687745ed1c164aead21f342 Mon Sep 17 00:00:00 2001 From: Dongkyuuuu Date: Sat, 28 Aug 2021 15:31:43 +0900 Subject: [PATCH] feat(map): add feature map components --- playground/App.vue | 67 ++++++++++++++++++++++++++++++--------- playground/vite.config.js | 1 + src/apis/useMap.ts | 2 +- src/components/Map.vue | 57 ++++++++++++++++++++++----------- src/types/index.ts | 2 +- src/useApi.ts | 12 +++---- 6 files changed, 100 insertions(+), 41 deletions(-) diff --git a/playground/App.vue b/playground/App.vue index f819c2e..279ca6a 100644 --- a/playground/App.vue +++ b/playground/App.vue @@ -1,44 +1,81 @@ diff --git a/playground/vite.config.js b/playground/vite.config.js index 309425a..7818255 100644 --- a/playground/vite.config.js +++ b/playground/vite.config.js @@ -6,6 +6,7 @@ import { resolve } from "path"; // https://vitejs.dev/config/ export default defineConfig({ root: resolve(process.cwd(), "playground"), + hmr: { overlay: false }, build: { outDir: "../playground_dist", rollupOptions: { diff --git a/src/apis/useMap.ts b/src/apis/useMap.ts index d67b08b..b5ec476 100644 --- a/src/apis/useMap.ts +++ b/src/apis/useMap.ts @@ -85,7 +85,7 @@ export interface useMapSetterMethods { setOptions( optionsOrKey: naver.maps.MapOptions | string, - value: naver.maps.MapOptions | null + value?: naver.maps.MapOptions | null ): void; setSize(size: naver.maps.Size | naver.maps.SizeLiteral): void; diff --git a/src/components/Map.vue b/src/components/Map.vue index 75eb2f2..2ef9c3f 100644 --- a/src/components/Map.vue +++ b/src/components/Map.vue @@ -1,5 +1,5 @@ @@ -10,8 +10,9 @@ import { PropType, toRefs, ref, - onBeforeMount, onUnmounted, + onMounted, + watch, } from "vue"; import { useMapInitOptions } from "../utils"; import type { naverV3 } from "../types"; @@ -30,38 +31,58 @@ export default defineComponent({ default: [], }, }, + emits: ["updateMap"], setup: (props, { emit }) => { const map = ref(null); const { width, height, mapOptions, initLayers } = toRefs(props); const { mapSettings } = useMapInitOptions(); const initNaverMap = () => { - map.value = null; const settings = mapSettings(mapOptions.value, initLayers.value); - map.value = new window.naver.maps.Map("vue-naver-maps", { + map.value = new window.naver.maps.Map("naver-maps", { ...settings, ...mapOptions.value, }); - emit("updateMap", map.value); }; - onBeforeMount(() => { - document.getElementById("vue3-naver-maps")!.onload = () => { - window.naver.maps.onJSContentLoaded = () => initNaverMap(); - }; + /** + * Props mapOptions watch + */ + watch( + () => mapOptions.value, + (newMaptions: naverV3.mapOptions, oldMaptions: naverV3.mapOptions) => { + map.value!.setOptions(newMaptions); + }, + { deep: true, immediate: false } + ); + + /** + * Props initLayers watch + */ + watch( + () => initLayers.value, + ( + newInitLayers: naverV3.initLayer[], + oldInitLayers: naverV3.initLayer[] + ) => { + const options = mapSettings(map.value!.getOptions(), newInitLayers); + map.value!.setOptions(options); + }, + { deep: true, immediate: false } + ); + + onMounted(() => { + if (!window.naver) { + document.getElementById("vue3-naver-maps")!.onload = () => { + window.naver.maps.onJSContentLoaded = () => initNaverMap(); + }; + } else { + initNaverMap(); + } }); onUnmounted(() => (map.value = null)); - // watch( - // () => mapOptions.value, - // () => { - // console.log("hello"); - // initNaverMap(); - // }, - // { deep: true, immediate: false } - // ); - return { width, height, diff --git a/src/types/index.ts b/src/types/index.ts index f34af1b..be33fe6 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -40,7 +40,7 @@ export namespace naverV3 { | "CHINESE" | "JAPANESE"; - export interface mapOptions { + export interface mapOptions extends naver.maps.MapOptions { zoom?: number; lat?: number; lng?: number; diff --git a/src/useApi.ts b/src/useApi.ts index f34ce9c..980806e 100644 --- a/src/useApi.ts +++ b/src/useApi.ts @@ -1,7 +1,7 @@ -import { inject, InjectionKey } from "vue"; -import type { useMap as useMapType } from "./apis"; +// import { inject, InjectionKey } from "vue"; +// import type { useMap as useMapType } from "./apis"; -export function useMap() { - const mapSymbol: InjectionKey = Symbol("[vue3-naver-maps]useMap"); - return inject(mapSymbol); -} +// export function useMap() { +// const mapSymbol: InjectionKey = Symbol("[vue3-naver-maps]useMap"); +// return inject(mapSymbol); +// }