mapcard 0.0.0-stage → 0.1.0
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/dist/config.d.ts +41 -0
- package/dist/config.js +32 -0
- package/dist/index.d.ts +38 -0
- package/dist/index.js +112 -0
- package/dist/style.css +25 -0
- package/package.json +15 -4
- package/README.md +0 -3
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type AdapterOptions, type CardActionMapping, type MethodRule, type EventRule } from '@neogis-auxiliary/adapters';
|
|
2
|
+
import { type HostPort, type MapEnginePort } from '@neogis-auxiliary/core';
|
|
3
|
+
type BindingRead = MethodRule | {
|
|
4
|
+
readonly call: string;
|
|
5
|
+
readonly args?: readonly unknown[];
|
|
6
|
+
readonly extract?: string;
|
|
7
|
+
};
|
|
8
|
+
export interface CardBindingRule {
|
|
9
|
+
readonly inputs?: readonly string[];
|
|
10
|
+
readonly read: BindingRead;
|
|
11
|
+
readonly refreshOn?: EventRule;
|
|
12
|
+
}
|
|
13
|
+
export type MapCardsEngineRules = Readonly<Partial<Record<Exclude<keyof MapEnginePort, 'on'>, MethodRule>>> & {
|
|
14
|
+
readonly preset?: string;
|
|
15
|
+
readonly events?: Readonly<Partial<Record<'camera' | 'resize', EventRule>>>;
|
|
16
|
+
};
|
|
17
|
+
type PlatformMethod = Exclude<keyof HostPort, 'onDataChanged' | 'getBindings' | 'syncCards' | 'onBindingsChanged' | 'refreshBindings' | 'whenBindingsReady'>;
|
|
18
|
+
export type MapCardsPlatformRules = Readonly<Partial<Record<PlatformMethod, MethodRule | null>>> & {
|
|
19
|
+
readonly preset?: string;
|
|
20
|
+
readonly events?: {
|
|
21
|
+
readonly dataChanged?: EventRule | null;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
/** JSON supplied by the platform. Business binding names are chosen by the integrator. */
|
|
25
|
+
export interface MapCardsConfig {
|
|
26
|
+
readonly version: 1;
|
|
27
|
+
readonly map?: string | MapCardsEngineRules;
|
|
28
|
+
readonly platform?: string | MapCardsPlatformRules;
|
|
29
|
+
readonly actions?: Readonly<Record<string, CardActionMapping>>;
|
|
30
|
+
readonly bindings?: Readonly<Record<string, CardBindingRule>>;
|
|
31
|
+
}
|
|
32
|
+
export interface MapCardsExtensions {
|
|
33
|
+
readonly converters?: AdapterOptions['converters'];
|
|
34
|
+
readonly handlers?: AdapterOptions['handlers'];
|
|
35
|
+
readonly presets?: AdapterOptions['presets'];
|
|
36
|
+
}
|
|
37
|
+
export declare function readMapCardsConfig(config: unknown): MapCardsConfig;
|
|
38
|
+
export declare function makeAdapterOptions(config: MapCardsConfig, targets: AdapterOptions['targets'], extensions: MapCardsExtensions): AdapterOptions;
|
|
39
|
+
/** Use the same configuration for platform document access outside a mounted card session. */
|
|
40
|
+
export declare function createMapCardsHost(platform: object, config: unknown, extensions?: MapCardsExtensions): HostPort;
|
|
41
|
+
export {};
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { createPlatformHostAdapter } from '@neogis-auxiliary/adapters';
|
|
2
|
+
import { isRecord } from '@neogis-auxiliary/core';
|
|
3
|
+
import { mapLibrePreset } from '@neogis-auxiliary/maplibre';
|
|
4
|
+
export function readMapCardsConfig(config) {
|
|
5
|
+
if (!isRecord(config) || config.version !== 1)
|
|
6
|
+
throw new Error('地图卡片配置需要 version: 1');
|
|
7
|
+
for (const key of Object.keys(config)) {
|
|
8
|
+
if (!['version', 'map', 'platform', 'bindings', 'actions'].includes(key))
|
|
9
|
+
throw new Error('未知地图卡片配置项:' + key);
|
|
10
|
+
}
|
|
11
|
+
return config;
|
|
12
|
+
}
|
|
13
|
+
export function makeAdapterOptions(config, targets, extensions) {
|
|
14
|
+
return {
|
|
15
|
+
config: {
|
|
16
|
+
version: 1,
|
|
17
|
+
engine: config.map ?? 'maplibre',
|
|
18
|
+
...(config.platform === undefined ? {} : { host: config.platform }),
|
|
19
|
+
},
|
|
20
|
+
targets,
|
|
21
|
+
presets: { maplibre: mapLibrePreset, ...extensions.presets },
|
|
22
|
+
converters: extensions.converters,
|
|
23
|
+
handlers: extensions.handlers,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/** Use the same configuration for platform document access outside a mounted card session. */
|
|
27
|
+
export function createMapCardsHost(platform, config, extensions = {}) {
|
|
28
|
+
const parsed = readMapCardsConfig(config);
|
|
29
|
+
if (parsed.platform === undefined)
|
|
30
|
+
throw new Error('需要配置 platform 才能独立创建宿主端口');
|
|
31
|
+
return createPlatformHostAdapter(makeAdapterOptions(parsed, { services: platform }, extensions));
|
|
32
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type CardFile, type CardInstanceOptions, type HostPort, type MapCard, type MapEnginePort } from '@neogis-auxiliary/core';
|
|
2
|
+
import { type MountMapCardsOptions } from '@neogis-auxiliary/dom';
|
|
3
|
+
import { type MapCardsExtensions } from './config.js';
|
|
4
|
+
export type { CardActionMapping } from '@neogis-auxiliary/adapters';
|
|
5
|
+
export { createMapCardsHost } from './config.js';
|
|
6
|
+
export type { CardBindingRule, MapCardsConfig, MapCardsEngineRules, MapCardsPlatformRules, MapCardsExtensions } from './config.js';
|
|
7
|
+
export { importCardFile, createMapCard } from '@neogis-auxiliary/core';
|
|
8
|
+
export type { CardFile, CardInstanceOptions, HostPort, MapCard, MapEnginePort } from '@neogis-auxiliary/core';
|
|
9
|
+
export interface CreateMapCardsOptions extends Pick<MountMapCardsOptions, 'container' | 'onSelect' | 'onUpdate' | 'onCardChange'>, MapCardsExtensions {
|
|
10
|
+
/** Existing MapLibre map by default, or the object referenced by custom map rules. */
|
|
11
|
+
map?: object;
|
|
12
|
+
platform?: object;
|
|
13
|
+
config?: unknown;
|
|
14
|
+
/** Advanced integrations can supply ports directly. */
|
|
15
|
+
engine?: MapEnginePort;
|
|
16
|
+
host?: HostPort;
|
|
17
|
+
}
|
|
18
|
+
export interface AddCardOptions extends CardInstanceOptions {
|
|
19
|
+
file: Pick<File, 'name' | 'text'>;
|
|
20
|
+
}
|
|
21
|
+
/** Initialize a map-card session without taking ownership of the platform or map. */
|
|
22
|
+
export declare function createMapCards(options: CreateMapCardsOptions): {
|
|
23
|
+
add: ({ file, ...values }: AddCardOptions) => Promise<{
|
|
24
|
+
card: MapCard;
|
|
25
|
+
cards: MapCard[];
|
|
26
|
+
context: import("@neogis-auxiliary/core").CardContext;
|
|
27
|
+
}>;
|
|
28
|
+
addMany(template: CardFile, instances: readonly CardInstanceOptions[]): MapCard[];
|
|
29
|
+
sync(nextCards: readonly MapCard[], nextSelectedId?: string): void;
|
|
30
|
+
getCards: () => MapCard[];
|
|
31
|
+
getDocuments: () => import("@neogis-auxiliary/core").MapCardDocument[];
|
|
32
|
+
restoreDocuments(value: unknown): Promise<MapCard[]>;
|
|
33
|
+
destroy(): void;
|
|
34
|
+
runtime: import("@neogis-auxiliary/core").MapCardRuntime;
|
|
35
|
+
overlay: import("@neogis-auxiliary/dom").CardOverlay;
|
|
36
|
+
refresh(): Promise<void>;
|
|
37
|
+
};
|
|
38
|
+
export type MapCardsSession = ReturnType<typeof createMapCards>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { createCardActions, createCardBindings, createMapEngineAdapter, createPlatformHostAdapter } from '@neogis-auxiliary/adapters';
|
|
2
|
+
import { createMapCard, createMapCardDocuments, importCardFile, restoreMapCardDocuments, } from '@neogis-auxiliary/core';
|
|
3
|
+
import { mountMapCards } from '@neogis-auxiliary/dom';
|
|
4
|
+
import { makeAdapterOptions, readMapCardsConfig } from './config.js';
|
|
5
|
+
export { createMapCardsHost } from './config.js';
|
|
6
|
+
export { importCardFile, createMapCard } from '@neogis-auxiliary/core';
|
|
7
|
+
/** Initialize a map-card session without taking ownership of the platform or map. */
|
|
8
|
+
export function createMapCards(options) {
|
|
9
|
+
const config = readMapCardsConfig(options.config === undefined ? { version: 1 } : options.config);
|
|
10
|
+
const targets = {
|
|
11
|
+
...(options.map ? { map: options.map } : {}),
|
|
12
|
+
...(options.platform ? { services: options.platform } : {}),
|
|
13
|
+
};
|
|
14
|
+
const adapterOptions = makeAdapterOptions(config, targets, options);
|
|
15
|
+
const engine = options.engine ?? createMapEngineAdapter(adapterOptions);
|
|
16
|
+
const host = options.host ?? (config.platform === undefined ? undefined : createPlatformHostAdapter(adapterOptions));
|
|
17
|
+
const bindings = config.bindings === undefined ? undefined : createCardBindings({ ...adapterOptions, config: config.bindings });
|
|
18
|
+
const actions = config.actions === undefined ? undefined : createCardActions({ ...adapterOptions, config: config.actions });
|
|
19
|
+
let cards = [];
|
|
20
|
+
let selectedId = '';
|
|
21
|
+
let generation = 0;
|
|
22
|
+
let disposed = false;
|
|
23
|
+
const connection = mountMapCards({
|
|
24
|
+
engine, host, bindings, actions, container: options.container, onUpdate: options.onUpdate,
|
|
25
|
+
onSelect(id) {
|
|
26
|
+
selectedId = id;
|
|
27
|
+
connection.sync(cards, selectedId);
|
|
28
|
+
options.onSelect?.(id);
|
|
29
|
+
},
|
|
30
|
+
onCardChange(moved) {
|
|
31
|
+
cards = cards.map(card => card.id === moved.id ? moved : card);
|
|
32
|
+
options.onCardChange?.(moved);
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
function assertCurrent(expectedGeneration = generation) {
|
|
36
|
+
if (disposed)
|
|
37
|
+
throw new Error('地图卡片连接已销毁');
|
|
38
|
+
if (expectedGeneration !== generation)
|
|
39
|
+
throw new Error('卡片操作已被新的状态替换');
|
|
40
|
+
}
|
|
41
|
+
function instanceOptions(template, values) {
|
|
42
|
+
return {
|
|
43
|
+
id: crypto.randomUUID(),
|
|
44
|
+
...(template.instance?.position ? {} : { position: [...engine.getCamera().center] }),
|
|
45
|
+
...values,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function append(added) {
|
|
49
|
+
const first = added[0];
|
|
50
|
+
if (!first)
|
|
51
|
+
return;
|
|
52
|
+
const next = [...cards, ...added];
|
|
53
|
+
connection.sync(next, first.id);
|
|
54
|
+
cards = next;
|
|
55
|
+
selectedId = first.id;
|
|
56
|
+
}
|
|
57
|
+
async function add({ file, ...values }) {
|
|
58
|
+
assertCurrent();
|
|
59
|
+
const token = generation;
|
|
60
|
+
const content = await file.text();
|
|
61
|
+
assertCurrent(token);
|
|
62
|
+
const template = importCardFile(file.name, content);
|
|
63
|
+
const added = await connection.runtime.createCards(template, instanceOptions(template, values));
|
|
64
|
+
assertCurrent(token);
|
|
65
|
+
const card = added[0];
|
|
66
|
+
if (!card)
|
|
67
|
+
throw new Error('图层没有点要素,未生成卡片');
|
|
68
|
+
append(added);
|
|
69
|
+
await Promise.all(added.map(item => connection.runtime.prepareCard(item)));
|
|
70
|
+
assertCurrent(token);
|
|
71
|
+
return { card, cards: added, context: connection.runtime.getContext(card) };
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
...connection,
|
|
75
|
+
add,
|
|
76
|
+
addMany(template, instances) {
|
|
77
|
+
assertCurrent();
|
|
78
|
+
const added = instances.map(values => createMapCard(template, instanceOptions(template, values)));
|
|
79
|
+
append(added);
|
|
80
|
+
return added;
|
|
81
|
+
},
|
|
82
|
+
sync(nextCards, nextSelectedId = '') {
|
|
83
|
+
assertCurrent();
|
|
84
|
+
const next = [...nextCards];
|
|
85
|
+
connection.sync(next, nextSelectedId);
|
|
86
|
+
generation++;
|
|
87
|
+
cards = next;
|
|
88
|
+
selectedId = nextSelectedId;
|
|
89
|
+
},
|
|
90
|
+
getCards: () => [...cards],
|
|
91
|
+
getDocuments: () => createMapCardDocuments(cards),
|
|
92
|
+
async restoreDocuments(value) {
|
|
93
|
+
assertCurrent();
|
|
94
|
+
const restored = restoreMapCardDocuments(value);
|
|
95
|
+
const nextSelectedId = restored[0]?.id ?? '';
|
|
96
|
+
connection.sync(restored, nextSelectedId);
|
|
97
|
+
const token = ++generation;
|
|
98
|
+
cards = restored;
|
|
99
|
+
selectedId = nextSelectedId;
|
|
100
|
+
await Promise.all(restored.map(card => connection.runtime.prepareCard(card)));
|
|
101
|
+
assertCurrent(token);
|
|
102
|
+
return [...cards];
|
|
103
|
+
},
|
|
104
|
+
destroy() {
|
|
105
|
+
if (disposed)
|
|
106
|
+
return;
|
|
107
|
+
disposed = true;
|
|
108
|
+
generation++;
|
|
109
|
+
connection.destroy();
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
}
|
package/dist/style.css
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* DOM 包拥有的卡片外框样式,由接入方单独导入 @neogis-auxiliary/dom/style.css。
|
|
3
|
+
* renderer.ts 在覆盖层容器上添加 mapcard-overlay,在每张卡片上添加 map-card。
|
|
4
|
+
* 这里管理外框、选中态和地理锚点;用户 HTML 的内容样式位于各自 Shadow DOM 中。
|
|
5
|
+
*
|
|
6
|
+
* 接入条件:覆盖层的定位父容器应设置 position: relative 等定位方式,
|
|
7
|
+
* 其尺寸和原点需要与地图绘制区域一致,避免投影坐标与卡片位置产生偏移。
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/* 容器覆盖父区域;空白处不拦截鼠标,让用户继续拖动地图;超出区域的部分被裁掉。 */
|
|
11
|
+
.mapcard-overlay { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
|
|
12
|
+
/* 卡片恢复鼠标交互。左上角从原点开始,真实位置由 renderer.ts 写入 transform。 */
|
|
13
|
+
.mapcard-overlay .map-card { position: absolute; left: 0; top: 0; padding-bottom: 30px; pointer-events: auto; cursor: pointer; will-change: transform; outline: none; }
|
|
14
|
+
/* html-content 自身是外框;其内部的 Shadow DOM 由 html-view.ts 创建。 */
|
|
15
|
+
.mapcard-overlay .html-content { position: relative; border: 1px solid #ffffffc9; border-radius: 9px; background: #fffffff5; box-shadow: 0 5px 22px #24453617, 0 1px 3px #24453610; }
|
|
16
|
+
/* selected 由 sync 根据选中 id 切换;focus-visible 为键盘操作提供可见焦点。 */
|
|
17
|
+
.mapcard-overlay .selected > .html-content { border: 1.5px solid #379c83; box-shadow: 0 6px 22px #214f3e1a, 0 0 0 3px #ffffff45; }
|
|
18
|
+
.mapcard-overlay .map-card:focus-visible > .html-content { outline: 2px solid #16816f; outline-offset: 3px; }
|
|
19
|
+
/* 卡片底部连线与圆点;底部留白来自 map-card 的 padding-bottom。 */
|
|
20
|
+
.mapcard-overlay .card-stem { position: absolute; bottom: 11px; left: 50%; width: 1px; height: 19px; background: #92b4a1; }
|
|
21
|
+
.mapcard-overlay .card-anchor { position: absolute; bottom: 0; left: 50%; width: 19px; height: 19px; transform: translateX(-50%); display: grid; place-items: center; background: #fff; color: #79a78f; border: 1px solid #e1ebe4; border-radius: 50%; box-shadow: 0 1px 6px #1c523626; }
|
|
22
|
+
.mapcard-overlay .selected .card-anchor { background: #16816f; color: white; border-color: white; box-shadow: 0 0 0 3px #16816f1a; }
|
|
23
|
+
.mapcard-overlay .mapcard-drag-handle { display: block; width: 100%; border: 1px solid #cbded6; border-radius: 6px 6px 0 0; padding: 7px 10px; background: #eaf4f0; color: #247b68; font: 12px sans-serif; text-align: left; cursor: grab; touch-action: none; user-select: none; }
|
|
24
|
+
.mapcard-overlay .dragging { z-index: 10 !important; }
|
|
25
|
+
.mapcard-overlay .dragging .mapcard-drag-handle { cursor: grabbing; background: #d6ece3; }
|
package/package.json
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mapcard",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"publishConfig": { "registry": "https://registry.npmjs.org/", "access": "public" },
|
|
5
|
+
"type": "module",
|
|
6
|
+
"files": ["dist"],
|
|
7
|
+
"exports": {
|
|
8
|
+
".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" },
|
|
9
|
+
"./style.css": "./dist/style.css"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"@neogis-auxiliary/core": "0.1.0",
|
|
13
|
+
"@neogis-auxiliary/adapters": "0.1.0",
|
|
14
|
+
"@neogis-auxiliary/dom": "0.1.0",
|
|
15
|
+
"@neogis-auxiliary/maplibre": "0.1.0"
|
|
16
|
+
}
|
|
17
|
+
}
|
package/README.md
DELETED