@cn-map/gaode-shared 0.0.0 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2024-present Jungzl.
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2024-present Jungzl.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,6 +1,41 @@
1
1
  # @cn-map/gaode-shared
2
2
 
3
- 高德地图 多框架共享代码。
3
+ 高德地图 monorepo 共享代码包,包含事件常量、补丁集合等多个模块。
4
4
 
5
- - 仓库:https://github.com/Jungzl/amap
6
- - 许可证:MIT
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install @cn-map/gaode-shared
9
+ ```
10
+
11
+ ```bash
12
+ yarn add @cn-map/gaode-shared
13
+ ```
14
+
15
+ ```bash
16
+ pnpm add @cn-map/gaode-shared
17
+ ```
18
+
19
+ ```bash
20
+ bun add @cn-map/gaode-shared
21
+ ```
22
+
23
+ ## 模块
24
+
25
+ ### Patch 补丁模块
26
+
27
+ 高德地图 JavaScript API 补丁集,解决原始 API 存在的问题。
28
+
29
+ 详细说明请查看 [Patch 补丁文档](./src/patch/README.md)
30
+
31
+ ### Const 常量模块
32
+
33
+ 事件常量和其他共享常量。
34
+
35
+ ```typescript
36
+ import { complete, dragStart, hotspotClick } from '@cn-map/gaode-shared/const'
37
+ ```
38
+
39
+ ## 许可证
40
+
41
+ [MIT](https://github.com/Jungzl/amap/blob/main/LICENSE)
@@ -0,0 +1,55 @@
1
+ export declare const hotspotClick = "hotspotClick";
2
+ export declare const hotspotOver = "hotspotOver";
3
+ export declare const hotspotOut = "hotspotOut";
4
+ export declare const dragStart = "dragStart";
5
+ export declare const dragging = "dragging";
6
+ export declare const dragEnd = "dragEnd";
7
+ export declare const click = "click";
8
+ export declare const dblClick = "dblClick";
9
+ export declare const rightClick = "rightClick";
10
+ export declare const mouseDown = "mouseDown";
11
+ export declare const mouseUp = "mouseUp";
12
+ export declare const mouseOver = "mouseOver";
13
+ export declare const mouseOut = "mouseOut";
14
+ export declare const mouseMove = "mouseMove";
15
+ export declare const mouseWheel = "mouseWheel";
16
+ export declare const touchStart = "touchStart";
17
+ export declare const touchMove = "touchMove";
18
+ export declare const touchEnd = "touchEnd";
19
+ export declare const hide = "hide";
20
+ export declare const show = "show";
21
+ export declare const zoomStart = "zoomStart";
22
+ export declare const zoomEnd = "zoomEnd";
23
+ export declare const zoomChange = "zoomChange";
24
+ export declare const pitchStart = "pitchStart";
25
+ export declare const pitchEnd = "pitchEnd";
26
+ export declare const pitchChange = "pitchChange";
27
+ export declare const rotateStart = "rotateStart";
28
+ export declare const rotateEnd = "rotateEnd";
29
+ export declare const rotateChange = "rotateChange";
30
+ export declare const mapMove = "mapMove";
31
+ export declare const viewChange = "viewChange";
32
+ export declare const resize = "resize";
33
+ export declare const complete = "complete";
34
+ export declare const error = "error";
35
+ export declare const open = "open";
36
+ export declare const close = "close";
37
+ export declare const moveStart = "moveStart";
38
+ export declare const moveEnd = "moveEnd";
39
+ export declare const moving = "moving";
40
+ export declare const moveAlong = "moveAlong";
41
+ export declare const draw = "draw";
42
+ export declare const select = "select";
43
+ export declare const choose = "choose";
44
+ export declare const add = "add";
45
+ export declare const end = "end";
46
+ export declare const addNode = "addNode";
47
+ export declare const adjust = "adjust";
48
+ export declare const removeNode = "removeNode";
49
+ export declare const move = "move";
50
+ export declare const selectChanged = "selectChanged";
51
+ export declare const markerClick = "markerClick";
52
+ export declare const listElementClick = "listElementClick";
53
+ export declare const addWay = "addWay";
54
+ export declare const floorChange = "floorChange";
55
+ export declare const loaded = "loaded";
@@ -0,0 +1,57 @@
1
+ // src/const/index.ts
2
+ const hotspotClick = "hotspotClick";
3
+ const hotspotOver = "hotspotOver";
4
+ const hotspotOut = "hotspotOut";
5
+ const dragStart = "dragStart";
6
+ const dragging = "dragging";
7
+ const dragEnd = "dragEnd";
8
+ const click = "click";
9
+ const dblClick = "dblClick";
10
+ const rightClick = "rightClick";
11
+ const mouseDown = "mouseDown";
12
+ const mouseUp = "mouseUp";
13
+ const mouseOver = "mouseOver";
14
+ const mouseOut = "mouseOut";
15
+ const mouseMove = "mouseMove";
16
+ const mouseWheel = "mouseWheel";
17
+ const touchStart = "touchStart";
18
+ const touchMove = "touchMove";
19
+ const touchEnd = "touchEnd";
20
+ const hide = "hide";
21
+ const show = "show";
22
+ const zoomStart = "zoomStart";
23
+ const zoomEnd = "zoomEnd";
24
+ const zoomChange = "zoomChange";
25
+ const pitchStart = "pitchStart";
26
+ const pitchEnd = "pitchEnd";
27
+ const pitchChange = "pitchChange";
28
+ const rotateStart = "rotateStart";
29
+ const rotateEnd = "rotateEnd";
30
+ const rotateChange = "rotateChange";
31
+ const mapMove = "mapMove";
32
+ const viewChange = "viewChange";
33
+ const resize = "resize";
34
+ const complete = "complete";
35
+ const error = "error";
36
+ const open = "open";
37
+ const close = "close";
38
+ const moveStart = "moveStart";
39
+ const moveEnd = "moveEnd";
40
+ const moving = "moving";
41
+ const moveAlong = "moveAlong";
42
+ const draw = "draw";
43
+ const select = "select";
44
+ const choose = "choose";
45
+ const add = "add";
46
+ const end = "end";
47
+ const addNode = "addNode";
48
+ const adjust = "adjust";
49
+ const removeNode = "removeNode";
50
+ const move = "move";
51
+ const selectChanged = "selectChanged";
52
+ const markerClick = "markerClick";
53
+ const listElementClick = "listElementClick";
54
+ const addWay = "addWay";
55
+ const floorChange = "floorChange";
56
+ const loaded = "loaded";
57
+ export { add, addNode, addWay, adjust, choose, click, close, complete, dblClick, dragEnd, dragStart, dragging, draw, end, error, floorChange, hide, hotspotClick, hotspotOut, hotspotOver, listElementClick, loaded, mapMove, markerClick, mouseDown, mouseMove, mouseOut, mouseOver, mouseUp, mouseWheel, move, moveAlong, moveEnd, moveStart, moving, open, pitchChange, pitchEnd, pitchStart, removeNode, resize, rightClick, rotateChange, rotateEnd, rotateStart, select, selectChanged, show, touchEnd, touchMove, touchStart, viewChange, zoomChange, zoomEnd, zoomStart };
@@ -0,0 +1 @@
1
+ export {}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ // src/index.ts
2
+ console.warn(`Please import from the subpath`);
@@ -0,0 +1,2 @@
1
+ declare const patchContextMenu: () => void;
2
+ export { patchContextMenu as default };
@@ -0,0 +1,11 @@
1
+ import { createPatchOnce } from "./utils.js";
2
+ // src/patch/context-menu.ts
3
+ let originalYy;
4
+ function patch() {
5
+ originalYy = AMap.ContextMenu.prototype.Yy;
6
+ AMap.ContextMenu.prototype.Yy = function() {
7
+ return this.dom ?? originalYy.call(this);
8
+ };
9
+ }
10
+ const patchContextMenu = createPatchOnce("contextMenu", patch);
11
+ export { patchContextMenu as default };
@@ -0,0 +1,2 @@
1
+ declare const patchGeoBufferSource: () => void;
2
+ export { patchGeoBufferSource as default };
@@ -0,0 +1,57 @@
1
+ import { createPatchOnce } from "./utils.js";
2
+ // src/patch/geobuffer-source.ts
3
+ let originalLoad;
4
+ function patch() {
5
+ originalLoad = Loca.GeoBufferSource.prototype.load;
6
+ Loca.GeoBufferSource.prototype.load = function() {
7
+ if (this.status === "loaded" || this.status === "loading") return;
8
+ this._ac = new AbortController();
9
+ const signal = this._ac.signal;
10
+ this.status = "loading";
11
+ if (this.options.data) {
12
+ if (this.valid(this.options.data)) {
13
+ const u = this;
14
+ Promise.resolve().then(() => {
15
+ if (signal.aborted) return;
16
+ u.status = "unload";
17
+ originalLoad.call(u);
18
+ }).catch(() => {
19
+ if (!signal.aborted) u.status = "loaded";
20
+ });
21
+ }
22
+ } else {
23
+ const f = this;
24
+ const l = this.url;
25
+ if (!l) return;
26
+ fetch(l, { signal }).then((c) => {
27
+ if (signal.aborted) throw new DOMException("Aborted", "AbortError");
28
+ return c.arrayBuffer();
29
+ }).then((c) => {
30
+ if (signal.aborted) throw new DOMException("Aborted", "AbortError");
31
+ try {
32
+ delete f.options.url;
33
+ f.url = void 0;
34
+ f.options.data = c;
35
+ f.status = "unload";
36
+ originalLoad.call(f);
37
+ } catch (e) {
38
+ console.error("Error in processing data:", e);
39
+ if (!signal.aborted) f.status = "loaded";
40
+ } finally {
41
+ f.options.url = l;
42
+ f.url = l;
43
+ delete f.options.data;
44
+ }
45
+ }).catch((error) => {
46
+ if (error.name !== "AbortError" && !signal.aborted) f.status = "loaded";
47
+ });
48
+ }
49
+ };
50
+ Loca.GeoBufferSource.prototype.abort = function() {
51
+ this._ac?.abort();
52
+ delete this._ac;
53
+ this.status = "unload";
54
+ };
55
+ }
56
+ const patchGeoBufferSource = createPatchOnce("geoBufferSource", patch);
57
+ export { patchGeoBufferSource as default };
@@ -0,0 +1,2 @@
1
+ declare const patchGeoJSONSource: () => void;
2
+ export { patchGeoJSONSource as default };
@@ -0,0 +1,45 @@
1
+ import { createPatchOnce } from "./utils.js";
2
+ // src/patch/geojson-source.ts
3
+ function patch() {
4
+ Loca.GeoJSONSource.prototype.load = function() {
5
+ if (this.status === "loaded" || this.status === "loading") return;
6
+ this._ac = new AbortController();
7
+ const signal = this._ac.signal;
8
+ this.status = "loading";
9
+ if (this.options.data) {
10
+ if (this.valid(this.options.data)) {
11
+ const o = this;
12
+ Promise.resolve().then(() => {
13
+ if (signal.aborted) return;
14
+ o.dataset = o.bf(this.options.data);
15
+ o.nf();
16
+ }).catch(() => {
17
+ if (!signal.aborted) o.status = "loaded";
18
+ });
19
+ }
20
+ } else {
21
+ const l = this;
22
+ const u = this.url;
23
+ if (!u) return;
24
+ fetch(u, { signal }).then((f) => {
25
+ if (signal.aborted) throw new DOMException("Aborted", "AbortError");
26
+ return f.json();
27
+ }).then((f) => {
28
+ if (signal.aborted) throw new DOMException("Aborted", "AbortError");
29
+ if (l.valid(f)) {
30
+ l.dataset = l.bf(f);
31
+ l.nf();
32
+ }
33
+ }).catch((error) => {
34
+ if (error.name !== "AbortError" && !signal.aborted) l.status = "loaded";
35
+ });
36
+ }
37
+ };
38
+ Loca.GeoJSONSource.prototype.abort = function() {
39
+ this._ac?.abort();
40
+ delete this._ac;
41
+ this.status = "unload";
42
+ };
43
+ }
44
+ const patchGeoJSONSource = createPatchOnce("geoJSONSource", patch);
45
+ export { patchGeoJSONSource as default };
@@ -0,0 +1,8 @@
1
+ import patchContextMenu from "./context-menu.js";
2
+ import patchGeoBufferSource from "./geobuffer-source.js";
3
+ import patchGeoJSONSource from "./geojson-source.js";
4
+ import patchLocaLayer from "./loca-layer.js";
5
+ import patchMap from "./map.js";
6
+ import patchPolygonEditor from "./polygon-editor.js";
7
+ import { PatchConfig, getPatchConfig, setPatchConfig } from "./utils.js";
8
+ export { type PatchConfig, getPatchConfig, patchContextMenu, patchGeoBufferSource, patchGeoJSONSource, patchLocaLayer, patchMap, patchPolygonEditor, setPatchConfig };
@@ -0,0 +1,8 @@
1
+ import patchContextMenu from "./context-menu.js";
2
+ import patchGeoBufferSource from "./geobuffer-source.js";
3
+ import patchGeoJSONSource from "./geojson-source.js";
4
+ import patchLocaLayer from "./loca-layer.js";
5
+ import patchMap from "./map.js";
6
+ import patchPolygonEditor from "./polygon-editor.js";
7
+ import { getPatchConfig, setPatchConfig } from "./utils.js";
8
+ export { getPatchConfig, patchContextMenu, patchGeoBufferSource, patchGeoJSONSource, patchLocaLayer, patchMap, patchPolygonEditor, setPatchConfig };
@@ -0,0 +1,2 @@
1
+ declare const patchLocaLayer: () => void;
2
+ export { patchLocaLayer as default };
@@ -0,0 +1,13 @@
1
+ import { createPatchOnce } from "./utils.js";
2
+ // src/patch/loca-layer.ts
3
+ function patch() {
4
+ Loca.Layer.prototype.setLoca = function() {
5
+ const a = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : null;
6
+ const o = this.loca;
7
+ this.loca = a;
8
+ this.map = a ? a == null ? void 0 : a.map : null;
9
+ o && o.requestRender();
10
+ };
11
+ }
12
+ const patchLocaLayer = createPatchOnce("locaLayer", patch);
13
+ export { patchLocaLayer as default };
@@ -0,0 +1,2 @@
1
+ declare const patchMap: () => void;
2
+ export { patchMap as default };
@@ -0,0 +1,15 @@
1
+ import { createPatchOnce } from "./utils.js";
2
+ // src/patch/map.ts
3
+ function patch() {
4
+ AMap.Map.prototype.addControl = function(t) {
5
+ t.addTo(this);
6
+ };
7
+ AMap.Map.prototype.removeControl = function(t) {
8
+ if (typeof t.remove === "function") t.remove();
9
+ else if (typeof t.removeFrom === "function") t.removeFrom(this);
10
+ const index = this.controls.indexOf(t);
11
+ if (index !== -1) this.controls.splice(index, 1);
12
+ };
13
+ }
14
+ const patchMap = createPatchOnce("map", patch);
15
+ export { patchMap as default };
@@ -0,0 +1,2 @@
1
+ declare const patchPolygonEditor: () => void;
2
+ export { patchPolygonEditor as default };
@@ -0,0 +1,14 @@
1
+ import { createPatchOnce } from "./utils.js";
2
+ // src/patch/polygon-editor.ts
3
+ function patch() {
4
+ AMap.PolygonEditor.prototype.destroy = function() {
5
+ this.singleRingListHandle.forEach((ring) => {
6
+ return ring.destroy();
7
+ });
8
+ this.singleRingListHandle.clear();
9
+ if (this.editingPolyObj.poly) this.map.remove(this.editingPolyObj.poly);
10
+ delete this.editingPolyObj.poly;
11
+ };
12
+ }
13
+ const patchPolygonEditor = createPatchOnce("polygonEditor", patch);
14
+ export { patchPolygonEditor as default };
@@ -0,0 +1,18 @@
1
+ export interface PatchConfig {
2
+ map?: boolean;
3
+ contextMenu?: boolean;
4
+ locaLayer?: boolean;
5
+ geoJSONSource?: boolean;
6
+ geoBufferSource?: boolean;
7
+ polygonEditor?: boolean;
8
+ }
9
+ /**
10
+ * 获取当前补丁配置
11
+ */
12
+ export declare function getPatchConfig(): Readonly<PatchConfig>;
13
+ /**
14
+ * 设置补丁配置
15
+ * 注意:必须在任何组件加载或补丁应用之前调用
16
+ * 一旦某个补丁被检查,该补丁的配置即被锁定
17
+ */
18
+ export declare function setPatchConfig(config: Partial<PatchConfig>): void;
@@ -0,0 +1,49 @@
1
+ // src/patch/utils.ts
2
+ let globalPatchConfig = {
3
+ map: true,
4
+ contextMenu: true,
5
+ locaLayer: true,
6
+ geoJSONSource: true,
7
+ geoBufferSource: true,
8
+ polygonEditor: true
9
+ };
10
+ /**
11
+ * 获取当前补丁配置
12
+ */
13
+ function getPatchConfig() {
14
+ return { ...globalPatchConfig };
15
+ }
16
+ /**
17
+ * 设置补丁配置
18
+ * 注意:必须在任何组件加载或补丁应用之前调用
19
+ * 一旦某个补丁被检查,该补丁的配置即被锁定
20
+ */
21
+ function setPatchConfig(config) {
22
+ globalPatchConfig = {
23
+ ...globalPatchConfig,
24
+ ...config
25
+ };
26
+ }
27
+ /**
28
+ * 创建补丁函数,确保补丁只会应用一次
29
+ * 配置只在首次调用时读取,随后即锁定
30
+ * @param patchName - PatchConfig 中定义的补丁名称
31
+ * @param patchFn - 实际执行补丁逻辑的函数
32
+ */
33
+ function createPatchOnce(patchName, patchFn) {
34
+ let checked = false;
35
+ return function applyPatch() {
36
+ if (checked) return;
37
+ if (globalPatchConfig[patchName] === false) {
38
+ checked = true;
39
+ return;
40
+ }
41
+ if (typeof AMap === "undefined") {
42
+ console.warn(`[gaode-patch] 高德地图未加载,${patchName} 补丁将被跳过`);
43
+ return;
44
+ }
45
+ patchFn();
46
+ checked = true;
47
+ };
48
+ }
49
+ export { createPatchOnce, getPatchConfig, setPatchConfig };
@@ -0,0 +1,36 @@
1
+ /**
2
+ * 可能 `null` 类型
3
+ */
4
+ export type Nullable<T> = T | null;
5
+ /**
6
+ * 访问器
7
+ */
8
+ export type Accessor<T> = () => T;
9
+ /**
10
+ * 可能是访问器
11
+ */
12
+ export type AccessorLike<T> = Accessor<T> | T;
13
+ /**
14
+ * 地图事件转换为 prop 属性
15
+ * @example
16
+ * ```ts
17
+ * type Props = EventToProp<'click'>; // 'onClick'
18
+ * ```
19
+ */
20
+ export type EventToProp<T extends AMap.EventType> = `on${Capitalize<T>}`;
21
+ /**
22
+ * prop 属性转换为地图事件
23
+ * @example
24
+ * ```ts
25
+ * type Event = EventFromProp<'onClick'>; // 'click'
26
+ * ```
27
+ */
28
+ export type EventFromProp<T extends EventToProp<AMap.EventType>> = T extends `on${infer U}` ? Uncapitalize<U> extends AMap.EventType ? Uncapitalize<U> : never : never;
29
+ /**
30
+ * 地图事件属性
31
+ * @example
32
+ * ```ts
33
+ * type Props = MapEventProps<'click', any>; // { onClick?: AMap.EventCallback<'click', any> }
34
+ * ```
35
+ */
36
+ export type MapEventProps<T extends AMap.EventType, U> = { [K in EventToProp<T>]?: AMap.EventCallback<EventFromProp<K>, U>; };
File without changes
@@ -0,0 +1,10 @@
1
+ /**
2
+ * 纯拷贝工具,不解代理:
3
+ * - 默认浅拷贝(拷贝顶层普通对象/数组,嵌套值保持引用)
4
+ * - `deep = true` 深拷贝(递归拷贝普通对象/数组)
5
+ * 类实例(LngLat / Pixel / Icon / HTMLElement / AMap 实例)与函数保持引用;
6
+ * 稀疏数组保留空槽;`seen` 处理循环引用。
7
+ * 需要解代理时请先调用各框架原生解代理 API(Vue `toRaw`/`toDefinedRaw`、
8
+ * Solid `unwrap`/`snapshot`、Svelte `snapshot`)。
9
+ */
10
+ export declare function clone<T>(value: T, deep?: boolean): T;
@@ -0,0 +1,48 @@
1
+ // src/utils/clone.ts
2
+ function isPlainObject(value) {
3
+ const proto = Object.getPrototypeOf(value);
4
+ return proto === Object.prototype || proto === null;
5
+ }
6
+ function isClassInstance(value) {
7
+ const proto = Object.getPrototypeOf(value);
8
+ return !!proto && proto !== Object.prototype && proto !== Array.prototype;
9
+ }
10
+ /**
11
+ * 纯拷贝工具,不解代理:
12
+ * - 默认浅拷贝(拷贝顶层普通对象/数组,嵌套值保持引用)
13
+ * - `deep = true` 深拷贝(递归拷贝普通对象/数组)
14
+ * 类实例(LngLat / Pixel / Icon / HTMLElement / AMap 实例)与函数保持引用;
15
+ * 稀疏数组保留空槽;`seen` 处理循环引用。
16
+ * 需要解代理时请先调用各框架原生解代理 API(Vue `toRaw`/`toDefinedRaw`、
17
+ * Solid `unwrap`/`snapshot`、Svelte `snapshot`)。
18
+ */
19
+ function clone(value, deep = false) {
20
+ return cloneInternal(value, deep, /* @__PURE__ */ new Map());
21
+ }
22
+ function cloneInternal(value, deep, seen) {
23
+ if (value == null || typeof value !== "object") return value;
24
+ if (isClassInstance(value)) return value;
25
+ if (Array.isArray(value)) {
26
+ const existing = seen.get(value);
27
+ if (existing !== void 0) return existing;
28
+ const copy = value.slice();
29
+ seen.set(value, copy);
30
+ if (deep) {
31
+ for (let i = 0; i < copy.length; i++) if (i in copy) copy[i] = cloneInternal(copy[i], true, seen);
32
+ }
33
+ return copy;
34
+ }
35
+ if (isPlainObject(value)) {
36
+ const existing = seen.get(value);
37
+ if (existing !== void 0) return existing;
38
+ const copy = {};
39
+ seen.set(value, copy);
40
+ for (const key of Object.keys(value)) {
41
+ const item = value[key];
42
+ copy[key] = deep ? cloneInternal(item, true, seen) : item;
43
+ }
44
+ return copy;
45
+ }
46
+ return value;
47
+ }
48
+ export { clone };
@@ -0,0 +1,12 @@
1
+ export declare class Logger {
2
+ silent: boolean;
3
+ LABEL: string;
4
+ SHARED_STYLES: string;
5
+ setSilent(value: boolean): void;
6
+ bgColor(color: string): string;
7
+ log(...args: any[]): void;
8
+ info(...args: any[]): void;
9
+ warn(...args: any[]): void;
10
+ error(...args: any[]): void;
11
+ }
12
+ export declare const logger: Logger;
@@ -0,0 +1,26 @@
1
+ // src/utils/console.ts
2
+ var Logger = class {
3
+ silent = false;
4
+ LABEL = "🌏 %c@cn-map/gaode";
5
+ SHARED_STYLES = "padding:1px 4px;color:white;border-radius:2px;";
6
+ setSilent(value) {
7
+ this.silent = value;
8
+ }
9
+ bgColor(color) {
10
+ return `${this.SHARED_STYLES}background:${color};`;
11
+ }
12
+ log(...args) {
13
+ if (!this.silent) console.log(this.LABEL, this.bgColor("silver"), ...args);
14
+ }
15
+ info(...args) {
16
+ if (!this.silent) console.info(this.LABEL, this.bgColor("steelblue"), ...args);
17
+ }
18
+ warn(...args) {
19
+ if (!this.silent) console.warn(this.LABEL, this.bgColor("darkorange"), ...args);
20
+ }
21
+ error(...args) {
22
+ if (!this.silent) console.error(this.LABEL, this.bgColor("crimson"), ...args);
23
+ }
24
+ };
25
+ const logger = new Logger();
26
+ export { Logger, logger };
@@ -0,0 +1,32 @@
1
+ import { EventToProp } from "../types/index.js";
2
+ /**
3
+ * Converts a string to lowercase.
4
+ * @param {string} str - The input string.
5
+ * @example
6
+ * lowercase('dblClick') // 'dblclick'
7
+ * @returns {string} The lowercase string.
8
+ */
9
+ export declare function lowercase<T extends AMap.EventType>(str: T): Lowercase<T>;
10
+ /**
11
+ * Converts an event type to a prop name.
12
+ * @param {AMap.EventType} eventType - The event type.
13
+ * @example
14
+ * eventToProp('click') // 'onClick'
15
+ * eventToProp('dblClick') // 'onDblClick'
16
+ * @returns {EventToProp<T>} The prop name.
17
+ */
18
+ export declare function eventToProp<T extends AMap.EventType>(eventType: T): EventToProp<T>;
19
+ /**
20
+ * Returns an array of event prop names for given events.
21
+ * @template T - The type of the event.
22
+ * @param {AMap.EventType[]} events - An array of event types.
23
+ * @example
24
+ * eventsToProps(['click', 'dblClick']) // ['onClick', 'onDblClick']
25
+ * @returns {EventToProp<T>[]} An array of event prop names.
26
+ */
27
+ export declare function eventsToProps<T extends AMap.EventType>(events: T[]): EventToProp<T>[];
28
+ /**
29
+ * Creates an error indicating that a required context is missing.
30
+ * @param {string} name - The name of the context that is missing.
31
+ */
32
+ export declare function lostContextError(name: string): Error;
@@ -0,0 +1,51 @@
1
+ // src/utils/helpers.ts
2
+ /**
3
+ * Converts a string to lowercase.
4
+ * @param {string} str - The input string.
5
+ * @example
6
+ * lowercase('dblClick') // 'dblclick'
7
+ * @returns {string} The lowercase string.
8
+ */
9
+ function lowercase(str) {
10
+ return str.toLowerCase();
11
+ }
12
+ /**
13
+ * Capitalizes the first letter of a string.
14
+ * @param {string} str - The input string.
15
+ * @example
16
+ * capitalize('dblclick') // 'Dblclick'
17
+ * @returns {string} The capitalized string.
18
+ */
19
+ function capitalize(str) {
20
+ return str.charAt(0).toUpperCase() + str.slice(1);
21
+ }
22
+ /**
23
+ * Converts an event type to a prop name.
24
+ * @param {AMap.EventType} eventType - The event type.
25
+ * @example
26
+ * eventToProp('click') // 'onClick'
27
+ * eventToProp('dblClick') // 'onDblClick'
28
+ * @returns {EventToProp<T>} The prop name.
29
+ */
30
+ function eventToProp(eventType) {
31
+ return `on${capitalize(eventType)}`;
32
+ }
33
+ /**
34
+ * Returns an array of event prop names for given events.
35
+ * @template T - The type of the event.
36
+ * @param {AMap.EventType[]} events - An array of event types.
37
+ * @example
38
+ * eventsToProps(['click', 'dblClick']) // ['onClick', 'onDblClick']
39
+ * @returns {EventToProp<T>[]} An array of event prop names.
40
+ */
41
+ function eventsToProps(events) {
42
+ return events.map(eventToProp);
43
+ }
44
+ /**
45
+ * Creates an error indicating that a required context is missing.
46
+ * @param {string} name - The name of the context that is missing.
47
+ */
48
+ function lostContextError(name) {
49
+ return /* @__PURE__ */ new Error(`缺少 ${name} 上下文,请确保在正确的上下文中使用组件或钩子`);
50
+ }
51
+ export { eventToProp, eventsToProps, lostContextError, lowercase };
@@ -0,0 +1,4 @@
1
+ import { clone } from "./clone.js";
2
+ import { Logger, logger } from "./console.js";
3
+ import { eventToProp, eventsToProps, lostContextError, lowercase } from "./helpers.js";
4
+ export { Logger, clone, eventToProp, eventsToProps, logger, lostContextError, lowercase };
@@ -0,0 +1,4 @@
1
+ import { clone } from "./clone.js";
2
+ import { Logger, logger } from "./console.js";
3
+ import { eventToProp, eventsToProps, lostContextError, lowercase } from "./helpers.js";
4
+ export { Logger, clone, eventToProp, eventsToProps, logger, lostContextError, lowercase };
package/package.json CHANGED
@@ -1,23 +1,89 @@
1
1
  {
2
2
  "name": "@cn-map/gaode-shared",
3
- "version": "0.0.0",
3
+ "version": "0.0.1",
4
4
  "description": "高德地图 多框架共享代码",
5
5
  "keywords": [
6
6
  "amap",
7
7
  "高德地图"
8
8
  ],
9
+ "bugs": {
10
+ "url": "https://gtihub.com/Jungzl/amap/issues"
11
+ },
9
12
  "license": "MIT",
10
- "author": "jungzl",
11
13
  "repository": {
12
14
  "type": "git",
13
15
  "url": "git+https://github.com/Jungzl/amap.git",
14
16
  "directory": "packages/shared"
15
17
  },
16
- "bugs": {
17
- "url": "https://github.com/Jungzl/amap/issues"
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "type": "module",
22
+ "sideEffects": false,
23
+ "main": "./dist/index.js",
24
+ "module": "./dist/index.js",
25
+ "types": "./dist/index.d.ts",
26
+ "typesVersions": {
27
+ "*": {
28
+ "const": [
29
+ "./dist/const/index.d.ts"
30
+ ],
31
+ "types": [
32
+ "./dist/types/index.d.ts"
33
+ ],
34
+ "patch": [
35
+ "./dist/patch/index.d.ts"
36
+ ],
37
+ "utils": [
38
+ "./dist/utils/index.d.ts"
39
+ ]
40
+ }
41
+ },
42
+ "exports": {
43
+ ".": {
44
+ "types": "./dist/index.d.ts",
45
+ "default": "./dist/index.js"
46
+ },
47
+ "./const": {
48
+ "types": "./dist/const/index.d.ts",
49
+ "default": "./dist/const/index.js"
50
+ },
51
+ "./types": {
52
+ "types": "./dist/types/index.d.ts",
53
+ "default": "./dist/types/index.js"
54
+ },
55
+ "./patch": {
56
+ "types": "./dist/patch/index.d.ts",
57
+ "default": "./dist/patch/index.js"
58
+ },
59
+ "./patch/*": {
60
+ "types": "./dist/patch/*.d.ts",
61
+ "default": "./dist/patch/*.js"
62
+ },
63
+ "./utils": {
64
+ "types": "./dist/utils/index.d.ts",
65
+ "default": "./dist/utils/index.js"
66
+ },
67
+ "./package.json": "./package.json"
68
+ },
69
+ "devDependencies": {
70
+ "@cn-map/gaode-types": "^2.1.0",
71
+ "vitest": "^5.1.0"
72
+ },
73
+ "peerDependencies": {
74
+ "@cn-map/gaode-types": "^2.0.0"
18
75
  },
19
- "publishConfig": {
20
- "access": "public"
76
+ "peerDependenciesMeta": {
77
+ "@cn-map/gaode-types": {
78
+ "optional": true
79
+ }
21
80
  },
22
- "files": []
81
+ "scripts": {
82
+ "build": "tsdown",
83
+ "dev": "tsdown --watch",
84
+ "check:type": "tsc",
85
+ "test": "vitest run",
86
+ "test:unit": "vitest run -p unit",
87
+ "test:coverage": "vitest run --coverage --bail=1"
88
+ }
23
89
  }