@metagl/smartearth-base 0.0.0-stage → 0.3.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/CHANGELOG.md +78 -0
- package/README.md +320 -3
- package/dist/chunk-24R3PPPA.js +2116 -0
- package/dist/chunk-POH7K3F4.js +705 -0
- package/dist/chunk-UENQT22J.js +885 -0
- package/dist/createEarth-BY0dtgQQ.d.ts +647 -0
- package/dist/errors-cB7IQlHy.d.ts +15 -0
- package/dist/index.d.ts +345 -0
- package/dist/index.js +2 -0
- package/dist/presets.d.ts +312 -0
- package/dist/presets.js +2 -0
- package/dist/vite.d.ts +20 -0
- package/dist/vite.js +89 -0
- package/dist/vue.d.ts +108 -0
- package/dist/vue.js +291 -0
- package/package.json +75 -6
|
@@ -0,0 +1,705 @@
|
|
|
1
|
+
import { ENVIRONMENT_PRESETS, SmartEarthError, requireSnapshot, PRESET_DOCUMENT_SCHEMA, withoutClock, readSdkRenderVersion, BASE_VERSION, describeLayerCause, describeCause } from './chunk-UENQT22J.js';
|
|
2
|
+
|
|
3
|
+
// src/presets/builtin.ts
|
|
4
|
+
function builtinSource(options = {}) {
|
|
5
|
+
const ids = new Set(ENVIRONMENT_PRESETS.map((preset) => preset.id));
|
|
6
|
+
return {
|
|
7
|
+
id: "builtin",
|
|
8
|
+
label: options.label ?? "SDK \u5185\u7F6E",
|
|
9
|
+
writable: false,
|
|
10
|
+
async list() {
|
|
11
|
+
return ENVIRONMENT_PRESETS.map(
|
|
12
|
+
(preset) => ({ id: `builtin:${preset.id}`, source: "builtin", name: preset.name, writable: false })
|
|
13
|
+
);
|
|
14
|
+
},
|
|
15
|
+
async get() {
|
|
16
|
+
throw new SmartEarthError("INVALID_OPTIONS", "SDK \u5185\u7F6E\u9884\u8BBE\u6CA1\u6709\u73B0\u6210\u5FEB\u7167\uFF1A\u5148\u5957\u7528\uFF0C\u518D\u5B58\u6210\u81EA\u5DF1\u7684\u9884\u8BBE\u3002");
|
|
17
|
+
},
|
|
18
|
+
async apply(earth, id) {
|
|
19
|
+
if (!ids.has(id)) throw new SmartEarthError("INVALID_OPTIONS", `\u6CA1\u6709\u8FD9\u4E2A SDK \u5185\u7F6E\u9884\u8BBE\uFF1A${id}`);
|
|
20
|
+
earth.environment.setPreset(id, { transition: options.transition ?? 1.5 });
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// src/presets/document.ts
|
|
26
|
+
function requireDocument(value, what = "\u9884\u8BBE") {
|
|
27
|
+
requireSnapshot(value?.snapshot, what);
|
|
28
|
+
const doc = value;
|
|
29
|
+
if (doc.schema !== PRESET_DOCUMENT_SCHEMA || typeof doc.name !== "string" || typeof doc.id !== "string") {
|
|
30
|
+
throw new SmartEarthError("INVALID_OPTIONS", `${what}\u4E0D\u662F ${PRESET_DOCUMENT_SCHEMA} \u683C\u5F0F\u3002`);
|
|
31
|
+
}
|
|
32
|
+
return value;
|
|
33
|
+
}
|
|
34
|
+
function wrapLegacyConfig(config, meta) {
|
|
35
|
+
const settings = typeof config.settings === "object" && config.settings !== null ? config.settings : {};
|
|
36
|
+
const time = typeof settings.clockIsoTime === "string" ? settings.clockIsoTime : void 0;
|
|
37
|
+
const snapshot = {
|
|
38
|
+
environment: withoutClock(config),
|
|
39
|
+
...time ? { time } : {},
|
|
40
|
+
quality: "high",
|
|
41
|
+
preset: null
|
|
42
|
+
};
|
|
43
|
+
return {
|
|
44
|
+
schema: PRESET_DOCUMENT_SCHEMA,
|
|
45
|
+
id: meta.id,
|
|
46
|
+
name: meta.name,
|
|
47
|
+
...meta.desc ? { desc: meta.desc } : {},
|
|
48
|
+
...meta.adcode ? { adcode: meta.adcode } : {},
|
|
49
|
+
createdAt: 0,
|
|
50
|
+
producedBy: "legacy",
|
|
51
|
+
snapshot
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function compatibilityNote(producedBy, sdkRenderVersion) {
|
|
55
|
+
if (!producedBy || !sdkRenderVersion) return null;
|
|
56
|
+
if (producedBy === "legacy") return "\u8FD9\u662F\u6837\u677F\u95F4\u7684\u8001\u683C\u5F0F\u9884\u8BBE\uFF08\u88F8\u914D\u7F6E\uFF0C\u6CA1\u8BB0\u751F\u6210\u65F6\u7684 SDK \u7248\u672C\uFF09\uFF0C\u6548\u679C\u53EF\u80FD\u548C\u5F53\u65F6\u4E0D\u5B8C\u5168\u4E00\u6837\u3002";
|
|
57
|
+
const major = (version) => version.split(".")[0];
|
|
58
|
+
return major(producedBy.sdkRender) === major(sdkRenderVersion) ? null : `\u8FD9\u4E2A\u9884\u8BBE\u662F sdk-render ${producedBy.sdkRender} \u751F\u6210\u7684\uFF0C\u5F53\u524D\u662F ${sdkRenderVersion}\uFF0C\u6548\u679C\u53EF\u80FD\u6709\u51FA\u5165\u3002`;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// src/presets/bucket.ts
|
|
62
|
+
var DEFAULT_CITY_CONFIG_BASE_URL = "https://io-qos.geovisearth.com/getfile/46/brainsim-cdn/CityConfig";
|
|
63
|
+
var HTTP_NOT_FOUND = 404;
|
|
64
|
+
function requireAdcode(adcode) {
|
|
65
|
+
const text = typeof adcode === "number" ? String(adcode) : adcode;
|
|
66
|
+
if (typeof text !== "string" || !/^\d{6}$/.test(text)) {
|
|
67
|
+
throw new SmartEarthError("INVALID_OPTIONS", `\u57CE\u5E02 adcode \u8981\u662F 6 \u4F4D\u884C\u653F\u533A\u5212\u4EE3\u7801\uFF08\u5982\u5317\u4EAC 110000\uFF09\uFF0C\u6536\u5230\uFF1A${String(adcode)}\u3002`);
|
|
68
|
+
}
|
|
69
|
+
return text;
|
|
70
|
+
}
|
|
71
|
+
function requireFile(file) {
|
|
72
|
+
if (typeof file !== "string" || !/^[\w.-]+\.json$/.test(file)) {
|
|
73
|
+
throw new SmartEarthError("INVALID_OPTIONS", `\u914D\u7F6E\u6587\u4EF6\u540D\u4E0D\u5408\u6CD5\uFF1A${String(file)}\uFF08\u5E94\u4E3A\u6E05\u5355\u91CC\u7684 *.json \u6587\u4EF6\u540D\uFF09\u3002`);
|
|
74
|
+
}
|
|
75
|
+
return file;
|
|
76
|
+
}
|
|
77
|
+
async function fetchJson(url, what) {
|
|
78
|
+
let response;
|
|
79
|
+
try {
|
|
80
|
+
response = await fetch(url, { cache: "no-store" });
|
|
81
|
+
} catch (error) {
|
|
82
|
+
throw new SmartEarthError("LAYER_LOAD_FAILED", `${what}\u8BFB\u53D6\u5931\u8D25\uFF08\u7F51\u7EDC\u4E0D\u901A\uFF1F\uFF09\uFF1A${describeLayerCause(error)}`, error);
|
|
83
|
+
}
|
|
84
|
+
if (response.status === HTTP_NOT_FOUND) return void 0;
|
|
85
|
+
if (!response.ok) throw new SmartEarthError("LAYER_LOAD_FAILED", `${what}\u8BFB\u53D6\u5931\u8D25\uFF1AHTTP ${response.status}\u3002`);
|
|
86
|
+
try {
|
|
87
|
+
return await response.json();
|
|
88
|
+
} catch (error) {
|
|
89
|
+
throw new SmartEarthError("LAYER_LOAD_FAILED", `${what}\u4E0D\u662F\u5408\u6CD5\u7684 JSON\u3002`, error);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function toIndex(raw) {
|
|
93
|
+
const configs = raw?.configs;
|
|
94
|
+
if (!Array.isArray(configs)) return { configs: [] };
|
|
95
|
+
return {
|
|
96
|
+
configs: configs.filter(
|
|
97
|
+
(item) => typeof item === "object" && item !== null && typeof item.file === "string" && typeof item.name === "string"
|
|
98
|
+
)
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
function cityConfigFilename(date) {
|
|
102
|
+
return `render-config-${date.toISOString().slice(0, 19).replace(/[T:]/g, "-")}.json`;
|
|
103
|
+
}
|
|
104
|
+
function createCityConfigLibrary(baseUrl) {
|
|
105
|
+
const base = baseUrl.replace(/\/+$/, "");
|
|
106
|
+
const fetchIndex = async (adcode) => toIndex(await fetchJson(`${base}/${adcode}/index.json`, `\u57CE\u5E02 ${adcode} \u7684\u9884\u8BBE\u6E05\u5355`));
|
|
107
|
+
return {
|
|
108
|
+
async listCityConfigs(adcode) {
|
|
109
|
+
const code = requireAdcode(adcode);
|
|
110
|
+
const index = await fetchIndex(code);
|
|
111
|
+
return index.configs.map((item) => ({
|
|
112
|
+
file: item.file,
|
|
113
|
+
name: item.name,
|
|
114
|
+
...item.desc ? { desc: item.desc } : {},
|
|
115
|
+
...item.thumb ? { thumbUrl: `${base}/${code}/${item.thumb}` } : {}
|
|
116
|
+
}));
|
|
117
|
+
},
|
|
118
|
+
async fetchCityConfig(adcode, file) {
|
|
119
|
+
const code = requireAdcode(adcode);
|
|
120
|
+
const name = requireFile(file);
|
|
121
|
+
const config = await fetchJson(`${base}/${code}/${name}`, `\u57CE\u5E02\u9884\u8BBE ${code}/${name}`);
|
|
122
|
+
if (typeof config !== "object" || config === null || !("systems" in config)) {
|
|
123
|
+
throw new SmartEarthError("LAYER_LOAD_FAILED", `\u57CE\u5E02\u9884\u8BBE ${code}/${name} \u4E0D\u5B58\u5728\u6216\u4E0D\u662F\u73AF\u5883\u914D\u7F6E\u3002`);
|
|
124
|
+
}
|
|
125
|
+
return config;
|
|
126
|
+
},
|
|
127
|
+
async draftCityConfig(adcode, entry, config, now) {
|
|
128
|
+
const code = requireAdcode(adcode);
|
|
129
|
+
if (typeof entry?.name !== "string" || entry.name.trim() === "") {
|
|
130
|
+
throw new SmartEarthError("INVALID_OPTIONS", "\u57CE\u5E02\u9884\u8BBE\u8981\u6709\u540D\u79F0 name\u3002");
|
|
131
|
+
}
|
|
132
|
+
const filename = cityConfigFilename(now);
|
|
133
|
+
const existing = await fetchIndex(code);
|
|
134
|
+
const newEntry = { file: filename, name: entry.name.trim(), ...entry.desc ? { desc: entry.desc } : {} };
|
|
135
|
+
const others = existing.configs.filter((item) => item.file !== filename);
|
|
136
|
+
return { adcode: code, filename, config, index: { configs: [...others, newEntry] }, uploadDir: `${base}/${code}/` };
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
function bucketSource(options = {}) {
|
|
141
|
+
const library = createCityConfigLibrary(options.baseUrl ?? DEFAULT_CITY_CONFIG_BASE_URL);
|
|
142
|
+
return {
|
|
143
|
+
id: "bucket",
|
|
144
|
+
label: options.label ?? "\u6837\u677F\u95F4",
|
|
145
|
+
writable: false,
|
|
146
|
+
async list({ adcode }) {
|
|
147
|
+
if (!adcode) return [];
|
|
148
|
+
const entries = await library.listCityConfigs(adcode);
|
|
149
|
+
return entries.map(
|
|
150
|
+
(item) => ({
|
|
151
|
+
id: `bucket:${adcode}/${item.file}`,
|
|
152
|
+
source: "bucket",
|
|
153
|
+
name: item.name,
|
|
154
|
+
adcode,
|
|
155
|
+
writable: false,
|
|
156
|
+
producedBy: "legacy",
|
|
157
|
+
...item.desc ? { desc: item.desc } : {},
|
|
158
|
+
...item.thumbUrl ? { thumb: item.thumbUrl } : {}
|
|
159
|
+
})
|
|
160
|
+
);
|
|
161
|
+
},
|
|
162
|
+
async get(id) {
|
|
163
|
+
const [adcode, file] = id.split("/");
|
|
164
|
+
const config = await library.fetchCityConfig(adcode ?? "", file ?? "");
|
|
165
|
+
const name = (await library.listCityConfigs(adcode ?? "")).find((item) => item.file === file)?.name ?? file ?? id;
|
|
166
|
+
return wrapLegacyConfig(config, { id: `bucket:${id}`, name, ...adcode ? { adcode } : {} });
|
|
167
|
+
},
|
|
168
|
+
draftUpload: library.draftCityConfig
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// src/presets/cities.ts
|
|
173
|
+
var DEFAULT_CITY_LIST_URL = "https://metaverseds.geovisearth.com/api/v1/city/list";
|
|
174
|
+
var REGION_RULES = [
|
|
175
|
+
{ id: "north", name: "\u534E\u5317", prefixes: [11, 12, 13, 14, 15] },
|
|
176
|
+
{ id: "northeast", name: "\u4E1C\u5317", prefixes: [21, 22, 23] },
|
|
177
|
+
{ id: "east", name: "\u534E\u4E1C", prefixes: [31, 32, 33, 34, 35, 36, 37] },
|
|
178
|
+
{ id: "central", name: "\u534E\u4E2D", prefixes: [41, 42, 43] },
|
|
179
|
+
{ id: "south", name: "\u534E\u5357", prefixes: [44, 45, 46] },
|
|
180
|
+
{ id: "southwest", name: "\u897F\u5357", prefixes: [50, 51, 52, 53, 54] },
|
|
181
|
+
{ id: "northwest", name: "\u897F\u5317", prefixes: [61, 62, 63, 64, 65] }
|
|
182
|
+
];
|
|
183
|
+
var REGIONS = [
|
|
184
|
+
...REGION_RULES.map(({ id, name }) => ({ id, name })),
|
|
185
|
+
{ id: "other", name: "\u5176\u4ED6" }
|
|
186
|
+
];
|
|
187
|
+
function regionOf(adcode) {
|
|
188
|
+
const prefix = Number.parseInt(adcode.slice(0, 2), 10);
|
|
189
|
+
return REGION_RULES.find((rule) => rule.prefixes.includes(prefix))?.id ?? "other";
|
|
190
|
+
}
|
|
191
|
+
function toCities(raw) {
|
|
192
|
+
const list = Array.isArray(raw) ? raw : raw?.data ?? [];
|
|
193
|
+
if (!Array.isArray(list)) return [];
|
|
194
|
+
return list.flatMap((item) => {
|
|
195
|
+
const adcode = typeof item.adcode === "number" ? String(item.adcode) : item.adcode;
|
|
196
|
+
if (typeof adcode !== "string" || !/^\d{6}$/.test(adcode) || typeof item.name !== "string") return [];
|
|
197
|
+
const province = typeof item.p_name === "string" && item.p_name !== "\u4E2D\u56FD" ? item.p_name : void 0;
|
|
198
|
+
return [{ adcode, name: item.name, ...province ? { province } : {}, regionId: regionOf(adcode) }];
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
function createCityList(url) {
|
|
202
|
+
let cached;
|
|
203
|
+
return () => {
|
|
204
|
+
cached ??= (async () => {
|
|
205
|
+
let response;
|
|
206
|
+
try {
|
|
207
|
+
response = await fetch(url);
|
|
208
|
+
} catch (error) {
|
|
209
|
+
throw new SmartEarthError("LAYER_LOAD_FAILED", `\u57CE\u5E02\u5217\u8868\u8BFB\u53D6\u5931\u8D25\uFF08\u7F51\u7EDC\u4E0D\u901A\uFF1F\uFF09\uFF1A${describeLayerCause(error)}`, error);
|
|
210
|
+
}
|
|
211
|
+
if (!response.ok) throw new SmartEarthError("LAYER_LOAD_FAILED", `\u57CE\u5E02\u5217\u8868\u8BFB\u53D6\u5931\u8D25\uFF1AHTTP ${response.status}\u3002`);
|
|
212
|
+
return toCities(await response.json());
|
|
213
|
+
})();
|
|
214
|
+
cached.catch(() => {
|
|
215
|
+
cached = void 0;
|
|
216
|
+
});
|
|
217
|
+
return cached;
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// src/presets/local.ts
|
|
222
|
+
var LOCAL_STORAGE_KEY = "smartearth-base.presets";
|
|
223
|
+
function createLocalStorageStorage(key = LOCAL_STORAGE_KEY) {
|
|
224
|
+
return {
|
|
225
|
+
load() {
|
|
226
|
+
try {
|
|
227
|
+
const raw = globalThis.localStorage?.getItem(key);
|
|
228
|
+
const parsed = raw ? JSON.parse(raw) : [];
|
|
229
|
+
return Array.isArray(parsed) ? parsed : [];
|
|
230
|
+
} catch {
|
|
231
|
+
return [];
|
|
232
|
+
}
|
|
233
|
+
},
|
|
234
|
+
save(records) {
|
|
235
|
+
globalThis.localStorage.setItem(key, JSON.stringify(records));
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
function localSource(storage, label = "\u672C\u5730") {
|
|
240
|
+
const load = async () => {
|
|
241
|
+
try {
|
|
242
|
+
return [...await storage.load()];
|
|
243
|
+
} catch (error) {
|
|
244
|
+
throw new SmartEarthError("LAYER_LOAD_FAILED", `\u672C\u5730\u9884\u8BBE\u8BFB\u53D6\u5931\u8D25\uFF1A${describeCause(error)}`, error);
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
const write = async (records) => {
|
|
248
|
+
try {
|
|
249
|
+
await storage.save(records);
|
|
250
|
+
} catch (error) {
|
|
251
|
+
throw new SmartEarthError("LAYER_LOAD_FAILED", `\u672C\u5730\u9884\u8BBE\u4FDD\u5B58\u5931\u8D25\uFF08\u5B58\u50A8\u6EE1\u4E86\uFF1F\uFF09\uFF1A${describeCause(error)}`, error);
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
const summary = (record) => ({
|
|
255
|
+
id: `local:${record.document.id}`,
|
|
256
|
+
source: "local",
|
|
257
|
+
name: record.document.name,
|
|
258
|
+
writable: true,
|
|
259
|
+
createdAt: record.document.createdAt,
|
|
260
|
+
producedBy: record.document.producedBy,
|
|
261
|
+
...record.document.desc ? { desc: record.document.desc } : {},
|
|
262
|
+
...record.document.adcode ? { adcode: record.document.adcode } : {},
|
|
263
|
+
...record.thumb ? { thumb: record.thumb } : {}
|
|
264
|
+
});
|
|
265
|
+
return {
|
|
266
|
+
id: "local",
|
|
267
|
+
label,
|
|
268
|
+
writable: true,
|
|
269
|
+
async list({ adcode }) {
|
|
270
|
+
return (await load()).filter((record) => !adcode || record.document.adcode === adcode).map(summary);
|
|
271
|
+
},
|
|
272
|
+
async get(id) {
|
|
273
|
+
const found = (await load()).find((record) => record.document.id === id);
|
|
274
|
+
if (!found) throw new SmartEarthError("INVALID_OPTIONS", `\u672C\u5730\u9884\u8BBE ${id} \u5DF2\u4E0D\u5B58\u5728\u3002`);
|
|
275
|
+
return found.document;
|
|
276
|
+
},
|
|
277
|
+
async save(draft) {
|
|
278
|
+
const id = `l-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;
|
|
279
|
+
const record = { document: { ...draft.document, id }, ...draft.thumb ? { thumb: draft.thumb } : {} };
|
|
280
|
+
await write([...await load(), record]);
|
|
281
|
+
return summary(record);
|
|
282
|
+
},
|
|
283
|
+
async remove(id) {
|
|
284
|
+
await write((await load()).filter((record) => record.document.id !== id));
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// src/presets/viewRegion.ts
|
|
290
|
+
var RADIANS_TO_DEGREES = 180 / Math.PI;
|
|
291
|
+
var FALLBACK_HALF_SPAN = 0.5;
|
|
292
|
+
var COORD_DECIMALS = 4;
|
|
293
|
+
function round(value) {
|
|
294
|
+
const factor = 10 ** COORD_DECIMALS;
|
|
295
|
+
return Math.round(value * factor) / factor;
|
|
296
|
+
}
|
|
297
|
+
function clampBox(west, south, east, north) {
|
|
298
|
+
const box = [Math.max(-180, west), Math.max(-90, south), Math.min(180, east), Math.min(90, north)].map(round);
|
|
299
|
+
return box.every(Number.isFinite) && box[0] < box[2] && box[1] < box[3] ? box : void 0;
|
|
300
|
+
}
|
|
301
|
+
function viewRegion(viewer, name) {
|
|
302
|
+
const camera = viewer?.camera;
|
|
303
|
+
if (!camera) return void 0;
|
|
304
|
+
try {
|
|
305
|
+
const rect = camera.computeViewRectangle?.();
|
|
306
|
+
if (rect) {
|
|
307
|
+
const box2 = clampBox(
|
|
308
|
+
rect.west * RADIANS_TO_DEGREES,
|
|
309
|
+
rect.south * RADIANS_TO_DEGREES,
|
|
310
|
+
rect.east * RADIANS_TO_DEGREES,
|
|
311
|
+
rect.north * RADIANS_TO_DEGREES
|
|
312
|
+
);
|
|
313
|
+
if (box2) return { name, bbox: box2 };
|
|
314
|
+
}
|
|
315
|
+
} catch {
|
|
316
|
+
}
|
|
317
|
+
const position = camera.positionCartographic;
|
|
318
|
+
if (!position) return void 0;
|
|
319
|
+
const lon = position.longitude * RADIANS_TO_DEGREES;
|
|
320
|
+
const lat = position.latitude * RADIANS_TO_DEGREES;
|
|
321
|
+
const box = clampBox(lon - FALLBACK_HALF_SPAN, lat - FALLBACK_HALF_SPAN, lon + FALLBACK_HALF_SPAN, lat + FALLBACK_HALF_SPAN);
|
|
322
|
+
return box ? { name, bbox: box } : void 0;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// src/presets/httpStore.ts
|
|
326
|
+
var SOURCE_ID = "cluster";
|
|
327
|
+
var PRESET_SCHEMA = "render-preset/1";
|
|
328
|
+
var FIRST_VERSION = "1.0.0";
|
|
329
|
+
var NAME_MAX = 40;
|
|
330
|
+
var CLUSTER_ID = /^[A-Za-z0-9_-]{1,64}$/;
|
|
331
|
+
var REF = /^[a-z0-9][a-z0-9-]{0,63}@\d+\.\d+\.\d+(\+[A-Za-z0-9_-]{1,64})?$/;
|
|
332
|
+
var DEFAULT_UTC_OFFSET = 8;
|
|
333
|
+
var MS_PER_HOUR = 36e5;
|
|
334
|
+
var HTTP_UNAUTHORIZED = 401;
|
|
335
|
+
var HTTP_NOT_FOUND2 = 404;
|
|
336
|
+
var HTTP_CONFLICT = 409;
|
|
337
|
+
var HTTP_METHOD_NOT_ALLOWED = 405;
|
|
338
|
+
var HTTP_TOO_MANY = 429;
|
|
339
|
+
var REGION_SUFFIX = /(维吾尔自治区|壮族自治区|回族自治区|特别行政区|自治区|自治州|地区|盟|省|市)$/;
|
|
340
|
+
function normalizeRegionName(name) {
|
|
341
|
+
return name.trim().replace(REGION_SUFFIX, "");
|
|
342
|
+
}
|
|
343
|
+
function pad(value) {
|
|
344
|
+
return String(value).padStart(2, "0");
|
|
345
|
+
}
|
|
346
|
+
function toPresetTime(iso, utcOffset = DEFAULT_UTC_OFFSET) {
|
|
347
|
+
const ms = Date.parse(iso);
|
|
348
|
+
if (!Number.isFinite(ms)) return void 0;
|
|
349
|
+
const local = new Date(ms + utcOffset * MS_PER_HOUR);
|
|
350
|
+
return {
|
|
351
|
+
date: `${local.getUTCFullYear()}-${pad(local.getUTCMonth() + 1)}-${pad(local.getUTCDate())}`,
|
|
352
|
+
time: `${pad(local.getUTCHours())}:${pad(local.getUTCMinutes())}`
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
function fromPresetTime(value, utcOffset = DEFAULT_UTC_OFFSET) {
|
|
356
|
+
if (!value?.date || !value.time || !/^\d{4}-\d{2}-\d{2}$/.test(value.date) || !/^\d{2}:\d{2}$/.test(value.time)) {
|
|
357
|
+
return void 0;
|
|
358
|
+
}
|
|
359
|
+
const ms = Date.parse(`${value.date}T${value.time}:00Z`) - utcOffset * MS_PER_HOUR;
|
|
360
|
+
return Number.isFinite(ms) ? new Date(ms).toISOString() : void 0;
|
|
361
|
+
}
|
|
362
|
+
function newPresetId() {
|
|
363
|
+
const random = Math.floor(Math.random() * 4294967295).toString(16).padStart(8, "0");
|
|
364
|
+
return `p-${Date.now().toString(36)}-${random}`;
|
|
365
|
+
}
|
|
366
|
+
function dataUrlToBlob(dataUrl) {
|
|
367
|
+
const match = /^data:(image\/(?:jpeg|png|webp));base64,(.+)$/.exec(dataUrl);
|
|
368
|
+
if (!match) throw new SmartEarthError("INVALID_OPTIONS", "\u7F29\u7565\u56FE\u8981\u662F JPEG / PNG / WebP \u7684 dataURL\u3002");
|
|
369
|
+
const binary = atob(match[2]);
|
|
370
|
+
const bytes = new Uint8Array(binary.length);
|
|
371
|
+
for (let i = 0; i < binary.length; i += 1) bytes[i] = binary.charCodeAt(i);
|
|
372
|
+
return new Blob([bytes], { type: match[1] });
|
|
373
|
+
}
|
|
374
|
+
function requireRef(ref) {
|
|
375
|
+
if (!REF.test(ref)) throw new SmartEarthError("INVALID_OPTIONS", `\u4E1A\u52A1\u96C6\u7FA4\u9884\u8BBE\u5F15\u7528\u4E0D\u5408\u6CD5\uFF1A${ref}`);
|
|
376
|
+
return ref;
|
|
377
|
+
}
|
|
378
|
+
function describeRejection(body) {
|
|
379
|
+
const rules = (body.report?.items ?? []).filter((item) => item.level === "error" && item.rule).map((item) => item.path ? `${item.rule}\uFF08${item.path}\uFF09` : item.rule);
|
|
380
|
+
return rules.length ? `${body.message ?? "\u6CA1\u901A\u8FC7\u68C0\u67E5"}\uFF1A${rules.join("\u3001")}` : body.message ?? "\u6CA1\u901A\u8FC7\u68C0\u67E5";
|
|
381
|
+
}
|
|
382
|
+
function httpPresetStore(options) {
|
|
383
|
+
if (typeof options?.baseUrl !== "string" || !/^https?:\/\//.test(options.baseUrl)) {
|
|
384
|
+
throw new SmartEarthError("INVALID_OPTIONS", "httpPresetStore \u8981\u7ED9 baseUrl\uFF08http/https\uFF0C\u5230 /presets \u4E3A\u6B62\uFF09\u3002");
|
|
385
|
+
}
|
|
386
|
+
if (options.clusterId !== void 0 && !CLUSTER_ID.test(options.clusterId)) {
|
|
387
|
+
throw new SmartEarthError("INVALID_OPTIONS", "clusterId \u53EA\u80FD\u662F 1\u201364 \u4F4D\u5B57\u6BCD\u3001\u6570\u5B57\u3001- \u6216 _\u3002");
|
|
388
|
+
}
|
|
389
|
+
const base = options.baseUrl.replace(/\/+$/, "");
|
|
390
|
+
const utcOffset = options.utcOffset ?? DEFAULT_UTC_OFFSET;
|
|
391
|
+
const doFetch = options.fetch ?? ((input, init) => fetch(input, init));
|
|
392
|
+
const thumbs = /* @__PURE__ */ new Map();
|
|
393
|
+
const authHeaders = async () => {
|
|
394
|
+
const token = options.getToken ? await options.getToken() : void 0;
|
|
395
|
+
return token ? { Authorization: `Bearer ${token}` } : {};
|
|
396
|
+
};
|
|
397
|
+
const request = async (path, init, what) => {
|
|
398
|
+
let response;
|
|
399
|
+
try {
|
|
400
|
+
response = await doFetch(`${base}${path}`, {
|
|
401
|
+
cache: "no-store",
|
|
402
|
+
...init,
|
|
403
|
+
headers: { ...init.headers, ...await authHeaders() }
|
|
404
|
+
});
|
|
405
|
+
} catch (error2) {
|
|
406
|
+
throw new SmartEarthError("LAYER_LOAD_FAILED", `${what}\u5931\u8D25\uFF08\u4E1A\u52A1\u96C6\u7FA4\u8FDE\u4E0D\u4E0A\uFF1F\uFF09\uFF1A${describeLayerCause(error2)}`, error2);
|
|
407
|
+
}
|
|
408
|
+
let body;
|
|
409
|
+
try {
|
|
410
|
+
body = await response.json();
|
|
411
|
+
} catch {
|
|
412
|
+
body = void 0;
|
|
413
|
+
}
|
|
414
|
+
if (response.ok && body?.ok) return body.data;
|
|
415
|
+
const error = body?.error ?? {};
|
|
416
|
+
const message = `${what}\u5931\u8D25\uFF1A${error.code === "UPLOAD_REJECTED" ? describeRejection(error) : error.message ?? `HTTP ${response.status}`}`;
|
|
417
|
+
if (response.status === HTTP_UNAUTHORIZED) throw new SmartEarthError("AUTH_FAILED", message);
|
|
418
|
+
const method = (init.method ?? "GET").toUpperCase();
|
|
419
|
+
if (method !== "GET" && (response.status === HTTP_METHOD_NOT_ALLOWED || error.code === "NOT_FOUND")) {
|
|
420
|
+
throw new SmartEarthError("ENVIRONMENT_UNSUPPORTED", `${what}\u5931\u8D25\uFF1A\u4E1A\u52A1\u96C6\u7FA4\u670D\u52A1\u8FD8\u4E0D\u652F\u6301\u5BA2\u6237\u7AEF\u5199\u5165\u9884\u8BBE\uFF08\u9700\u8981\u90E8\u7F72\u5E26\u5BA2\u6237\u7AEF\u5199\u5165\u7684\u7248\u672C\uFF09\u3002`);
|
|
421
|
+
}
|
|
422
|
+
const code = response.status === HTTP_NOT_FOUND2 || response.status === HTTP_CONFLICT || response.status === HTTP_TOO_MANY || response.status === 422 || response.status === 400 ? "INVALID_OPTIONS" : "LAYER_LOAD_FAILED";
|
|
423
|
+
throw new SmartEarthError(code, message);
|
|
424
|
+
};
|
|
425
|
+
const refOf = (entry) => entry.ref ?? `${entry.id}@${entry.version}${entry.clusterId ? `+${entry.clusterId}` : ""}`;
|
|
426
|
+
const thumbnailOf = (entry) => {
|
|
427
|
+
if (!entry.thumbnailUrl || typeof URL.createObjectURL !== "function") return Promise.resolve(void 0);
|
|
428
|
+
const cached = thumbs.get(entry.ref);
|
|
429
|
+
if (cached) return cached;
|
|
430
|
+
const loading = (async () => {
|
|
431
|
+
try {
|
|
432
|
+
const response = await doFetch(`${base}/${entry.ref}/thumbnail`, { headers: await authHeaders() });
|
|
433
|
+
return response.ok ? URL.createObjectURL(await response.blob()) : void 0;
|
|
434
|
+
} catch {
|
|
435
|
+
return void 0;
|
|
436
|
+
}
|
|
437
|
+
})();
|
|
438
|
+
thumbs.set(entry.ref, loading);
|
|
439
|
+
return loading;
|
|
440
|
+
};
|
|
441
|
+
const summaryOf = async (raw, adcode) => {
|
|
442
|
+
const entry = { ...raw, ref: refOf(raw) };
|
|
443
|
+
const own = entry.visibility === "cluster";
|
|
444
|
+
const thumb = await thumbnailOf(entry);
|
|
445
|
+
const desc = [entry.region?.name, entry.author, own ? "\u4EC5\u672C\u96C6\u7FA4" : void 0].filter(Boolean).join(" \xB7 ");
|
|
446
|
+
const createdAt = entry.updatedAt ? Date.parse(entry.updatedAt) : Number.NaN;
|
|
447
|
+
return {
|
|
448
|
+
id: `${SOURCE_ID}:${entry.ref}`,
|
|
449
|
+
source: SOURCE_ID,
|
|
450
|
+
name: entry.name,
|
|
451
|
+
writable: own,
|
|
452
|
+
...desc ? { desc } : {},
|
|
453
|
+
...adcode ? { adcode } : {},
|
|
454
|
+
...thumb ? { thumb } : {},
|
|
455
|
+
...Number.isFinite(createdAt) ? { createdAt } : {},
|
|
456
|
+
...entry.sdkRender ? { producedBy: { base: "unknown", sdkRender: entry.sdkRender } } : {}
|
|
457
|
+
};
|
|
458
|
+
};
|
|
459
|
+
const matchesCity = (entry, city) => {
|
|
460
|
+
if (!city) return true;
|
|
461
|
+
const region = entry.region?.name ? normalizeRegionName(entry.region.name) : "";
|
|
462
|
+
if (!region) return false;
|
|
463
|
+
return [city.name, city.province].some((name) => name !== void 0 && normalizeRegionName(name) === region);
|
|
464
|
+
};
|
|
465
|
+
return {
|
|
466
|
+
id: SOURCE_ID,
|
|
467
|
+
label: options.label ?? "\u4E1A\u52A1\u96C6\u7FA4",
|
|
468
|
+
writable: true,
|
|
469
|
+
async list({ adcode, city }) {
|
|
470
|
+
const query = options.clusterId ? `?cluster=${encodeURIComponent(options.clusterId)}` : "";
|
|
471
|
+
const data = await request(`${query}`, {}, "\u8BFB\u53D6\u4E1A\u52A1\u96C6\u7FA4\u9884\u8BBE");
|
|
472
|
+
const all = [...data.clusterEntries ?? [], ...data.entries ?? []].filter(
|
|
473
|
+
(entry) => typeof entry?.id === "string" && typeof entry.version === "string" && typeof entry.name === "string"
|
|
474
|
+
);
|
|
475
|
+
if (adcode && !city) return [];
|
|
476
|
+
return Promise.all(all.filter((entry) => matchesCity(entry, city)).map((entry) => summaryOf(entry, adcode)));
|
|
477
|
+
},
|
|
478
|
+
async get(ref) {
|
|
479
|
+
const detail = await request(`/${requireRef(ref)}`, {}, `\u8BFB\u53D6\u4E1A\u52A1\u96C6\u7FA4\u9884\u8BBE ${ref}`);
|
|
480
|
+
const time = fromPresetTime(detail.time, utcOffset);
|
|
481
|
+
const camera = detail.camera;
|
|
482
|
+
if (typeof detail.render !== "object" || detail.render === null) {
|
|
483
|
+
throw new SmartEarthError("LAYER_LOAD_FAILED", `\u4E1A\u52A1\u96C6\u7FA4\u9884\u8BBE ${ref} \u6CA1\u6709\u6E32\u67D3\u914D\u7F6E\u3002`);
|
|
484
|
+
}
|
|
485
|
+
const document = {
|
|
486
|
+
schema: PRESET_DOCUMENT_SCHEMA,
|
|
487
|
+
id: `${SOURCE_ID}:${ref}`,
|
|
488
|
+
name: detail.name,
|
|
489
|
+
...detail.author ? { author: detail.author } : {},
|
|
490
|
+
createdAt: detail.updatedAt ? Date.parse(detail.updatedAt) || 0 : 0,
|
|
491
|
+
producedBy: { base: "unknown", sdkRender: detail.sdkRender ?? "unknown" },
|
|
492
|
+
source: { kind: SOURCE_ID, id: ref },
|
|
493
|
+
snapshot: {
|
|
494
|
+
environment: detail.render,
|
|
495
|
+
...time ? { time } : {},
|
|
496
|
+
...camera && typeof camera.lon === "number" && typeof camera.lat === "number" && typeof camera.height === "number" ? { camera: { lon: camera.lon, lat: camera.lat, height: camera.height, heading: camera.heading ?? 0, pitch: camera.pitch ?? -90 } } : {},
|
|
497
|
+
quality: "high",
|
|
498
|
+
preset: null
|
|
499
|
+
}
|
|
500
|
+
};
|
|
501
|
+
return document;
|
|
502
|
+
},
|
|
503
|
+
async save(draft) {
|
|
504
|
+
if (!draft.region) throw new SmartEarthError("INVALID_OPTIONS", "\u5B58\u5230\u4E1A\u52A1\u96C6\u7FA4\u7684\u9884\u8BBE\u8981\u6709\u5730\u533A\uFF08region.name \u4E0E bbox\uFF09\u3002");
|
|
505
|
+
if (!draft.thumb) throw new SmartEarthError("INVALID_OPTIONS", "\u5B58\u5230\u4E1A\u52A1\u96C6\u7FA4\u7684\u9884\u8BBE\u8981\u6709\u7F29\u7565\u56FE\uFF08thumbnail \u4E0D\u80FD\u5173\uFF09\u3002");
|
|
506
|
+
const name = draft.name.trim();
|
|
507
|
+
if (!name || name.length > NAME_MAX) throw new SmartEarthError("INVALID_OPTIONS", `\u9884\u8BBE\u540D\u79F0\u8981\u6709\uFF0C\u6700\u591A ${NAME_MAX} \u5B57\u3002`);
|
|
508
|
+
const { snapshot, producedBy } = draft.document;
|
|
509
|
+
const id = newPresetId();
|
|
510
|
+
const time = snapshot.time ? toPresetTime(snapshot.time, utcOffset) : void 0;
|
|
511
|
+
const preset = {
|
|
512
|
+
schema: PRESET_SCHEMA,
|
|
513
|
+
id,
|
|
514
|
+
version: FIRST_VERSION,
|
|
515
|
+
name,
|
|
516
|
+
region: { name: draft.region.name.slice(0, NAME_MAX), bbox: [...draft.region.bbox] },
|
|
517
|
+
render: snapshot.environment,
|
|
518
|
+
...time ? { time } : {},
|
|
519
|
+
...snapshot.camera ? { camera: { ...snapshot.camera, roll: 0 } } : {},
|
|
520
|
+
sdkRender: (producedBy === "legacy" ? "unknown" : producedBy.sdkRender).slice(0, NAME_MAX)
|
|
521
|
+
};
|
|
522
|
+
const form = new FormData();
|
|
523
|
+
form.append("preset", JSON.stringify(preset));
|
|
524
|
+
form.append("thumbnail", dataUrlToBlob(draft.thumb), "thumbnail.jpg");
|
|
525
|
+
if (options.clusterId) form.append("clusterId", options.clusterId);
|
|
526
|
+
const data = await request(`/${id}/versions`, { method: "POST", body: form }, "\u4FDD\u5B58\u9884\u8BBE\u5230\u4E1A\u52A1\u96C6\u7FA4");
|
|
527
|
+
const saved = data.version;
|
|
528
|
+
const ref = saved?.ref ?? (options.clusterId ? `${id}@${FIRST_VERSION}+${options.clusterId}` : `${id}@${FIRST_VERSION}`);
|
|
529
|
+
const summary = await summaryOf(
|
|
530
|
+
{ id, version: FIRST_VERSION, ref, name, region: preset.region, sdkRender: preset.sdkRender, visibility: saved?.visibility ?? "cluster" },
|
|
531
|
+
draft.adcode
|
|
532
|
+
);
|
|
533
|
+
return { ...summary, thumb: draft.thumb };
|
|
534
|
+
},
|
|
535
|
+
async remove(ref) {
|
|
536
|
+
await request(`/${requireRef(ref)}`, { method: "DELETE" }, `\u5220\u9664\u4E1A\u52A1\u96C6\u7FA4\u9884\u8BBE ${ref}`);
|
|
537
|
+
const thumb = await thumbs.get(ref);
|
|
538
|
+
thumbs.delete(ref);
|
|
539
|
+
if (thumb) URL.revokeObjectURL(thumb);
|
|
540
|
+
}
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
// src/presets/index.ts
|
|
545
|
+
function splitCardId(id) {
|
|
546
|
+
const at = id.indexOf(":");
|
|
547
|
+
if (at <= 0) throw new SmartEarthError("INVALID_OPTIONS", `\u9884\u8BBE\u5361 id \u4E0D\u5408\u6CD5\uFF1A${id}`);
|
|
548
|
+
return { source: id.slice(0, at), inner: id.slice(at + 1) };
|
|
549
|
+
}
|
|
550
|
+
function requireName(name) {
|
|
551
|
+
if (typeof name !== "string" || name.trim() === "") throw new SmartEarthError("INVALID_OPTIONS", "\u9884\u8BBE\u8981\u6709\u540D\u79F0 name\u3002");
|
|
552
|
+
return name.trim();
|
|
553
|
+
}
|
|
554
|
+
function createPresetLibrary(earth, options = {}) {
|
|
555
|
+
const bucket = options.bucket === false ? void 0 : bucketSource(options.bucket ?? {});
|
|
556
|
+
const sources = [
|
|
557
|
+
...options.builtin === false ? [] : [builtinSource()],
|
|
558
|
+
...options.store ? [options.store] : [],
|
|
559
|
+
...options.storage ? [localSource(options.storage)] : [],
|
|
560
|
+
...bucket ? [bucket] : [],
|
|
561
|
+
...options.sources ?? []
|
|
562
|
+
];
|
|
563
|
+
const sourceOf = (id) => {
|
|
564
|
+
const found = sources.find((source) => source.id === id);
|
|
565
|
+
if (!found) throw new SmartEarthError("INVALID_OPTIONS", `\u6CA1\u6709\u8FD9\u4E2A\u9884\u8BBE\u6570\u636E\u6E90\uFF1A${id}`);
|
|
566
|
+
return found;
|
|
567
|
+
};
|
|
568
|
+
const fetchCities = createCityList(options.cityListUrl ?? DEFAULT_CITY_LIST_URL);
|
|
569
|
+
const cityOf = async (adcode) => {
|
|
570
|
+
try {
|
|
571
|
+
const city = (await fetchCities()).find((item) => item.adcode === adcode);
|
|
572
|
+
return city ? { city: { name: city.name, ...city.province ? { province: city.province } : {} } } : {};
|
|
573
|
+
} catch {
|
|
574
|
+
return {};
|
|
575
|
+
}
|
|
576
|
+
};
|
|
577
|
+
const seen = /* @__PURE__ */ new Map();
|
|
578
|
+
const sdkRenderVersion = readSdkRenderVersion();
|
|
579
|
+
let beforePreview;
|
|
580
|
+
const applyCard = async (card, camera = false) => {
|
|
581
|
+
const { source, inner } = splitCardId(card.id);
|
|
582
|
+
const from = sourceOf(source);
|
|
583
|
+
if (from.apply) {
|
|
584
|
+
await from.apply(earth, inner);
|
|
585
|
+
return;
|
|
586
|
+
}
|
|
587
|
+
const doc = await from.get(inner);
|
|
588
|
+
await earth.environment.restoreSnapshot(doc.snapshot, { time: true, camera });
|
|
589
|
+
};
|
|
590
|
+
const buildDocument = async (opts) => {
|
|
591
|
+
const name = requireName(opts.name);
|
|
592
|
+
const snapshot = await earth.environment.captureSnapshot({ time: true, camera: opts.camera !== false });
|
|
593
|
+
return {
|
|
594
|
+
schema: PRESET_DOCUMENT_SCHEMA,
|
|
595
|
+
id: "",
|
|
596
|
+
name,
|
|
597
|
+
...opts.desc ? { desc: opts.desc } : {},
|
|
598
|
+
...opts.adcode ? { adcode: String(opts.adcode) } : {},
|
|
599
|
+
...opts.tags?.length ? { tags: [...opts.tags] } : {},
|
|
600
|
+
createdAt: Date.now(),
|
|
601
|
+
producedBy: { base: BASE_VERSION, sdkRender: sdkRenderVersion ?? "unknown" },
|
|
602
|
+
snapshot
|
|
603
|
+
};
|
|
604
|
+
};
|
|
605
|
+
const thumbnail = (enabled) => enabled === false ? Promise.resolve(void 0) : earth.captureImage().catch(() => void 0);
|
|
606
|
+
return {
|
|
607
|
+
sources,
|
|
608
|
+
regions: REGIONS,
|
|
609
|
+
canSave: sources.some((source) => source.writable && source.save),
|
|
610
|
+
listCities: () => fetchCities(),
|
|
611
|
+
async searchCities(query) {
|
|
612
|
+
const cities = await fetchCities();
|
|
613
|
+
const text = query.trim();
|
|
614
|
+
if (!text) return cities;
|
|
615
|
+
const regionIds = new Set(REGIONS.filter((region) => region.name.includes(text)).map((region) => region.id));
|
|
616
|
+
const hits = new Set(
|
|
617
|
+
[...seen.entries()].filter(([, cards]) => cards.some((card) => card.name.includes(text) || card.desc?.includes(text))).map(([adcode]) => adcode)
|
|
618
|
+
);
|
|
619
|
+
return cities.filter(
|
|
620
|
+
(city) => city.name.includes(text) || city.province?.includes(text) || regionIds.has(city.regionId) || hits.has(city.adcode)
|
|
621
|
+
);
|
|
622
|
+
},
|
|
623
|
+
async list(adcode) {
|
|
624
|
+
const code = adcode === void 0 ? void 0 : String(adcode);
|
|
625
|
+
const query = code ? { adcode: code, ...await cityOf(code) } : {};
|
|
626
|
+
const lists = await Promise.all(sources.map((source) => source.list(query)));
|
|
627
|
+
const cards = lists.flat();
|
|
628
|
+
if (code) seen.set(code, cards);
|
|
629
|
+
return cards;
|
|
630
|
+
},
|
|
631
|
+
async getDocument(card) {
|
|
632
|
+
const { source, inner } = splitCardId(card.id);
|
|
633
|
+
return sourceOf(source).get(inner);
|
|
634
|
+
},
|
|
635
|
+
compatibility: (card) => compatibilityNote(card.producedBy, sdkRenderVersion),
|
|
636
|
+
async preview(card) {
|
|
637
|
+
const snapshot = beforePreview ?? await earth.environment.captureSnapshot({ time: true });
|
|
638
|
+
await applyCard(card);
|
|
639
|
+
beforePreview = snapshot;
|
|
640
|
+
},
|
|
641
|
+
confirmPreview() {
|
|
642
|
+
beforePreview = void 0;
|
|
643
|
+
},
|
|
644
|
+
async cancelPreview() {
|
|
645
|
+
const snapshot = beforePreview;
|
|
646
|
+
if (!snapshot) return;
|
|
647
|
+
await earth.environment.restoreSnapshot(snapshot, { time: true });
|
|
648
|
+
beforePreview = void 0;
|
|
649
|
+
},
|
|
650
|
+
get isPreviewing() {
|
|
651
|
+
return beforePreview !== void 0;
|
|
652
|
+
},
|
|
653
|
+
async apply(card, applyOptions) {
|
|
654
|
+
await applyCard(card, applyOptions?.camera === true);
|
|
655
|
+
beforePreview = void 0;
|
|
656
|
+
},
|
|
657
|
+
async save(opts) {
|
|
658
|
+
const target = opts.target ? sourceOf(opts.target) : sources.find((source) => source.writable && source.save);
|
|
659
|
+
if (!target?.save) {
|
|
660
|
+
throw new SmartEarthError(
|
|
661
|
+
"INVALID_OPTIONS",
|
|
662
|
+
"\u6CA1\u6709\u53EF\u5199\u7684\u9884\u8BBE\u6570\u636E\u6E90\uFF1A\u7531\u4E0A\u5C42\u4F20 createPresetLibrary(earth, { store: httpPresetStore({ baseUrl, getToken, clusterId }) })\u3002"
|
|
663
|
+
);
|
|
664
|
+
}
|
|
665
|
+
const document = await buildDocument(opts);
|
|
666
|
+
const thumb = await thumbnail(opts.thumbnail);
|
|
667
|
+
const city = document.adcode ? (await cityOf(document.adcode)).city : void 0;
|
|
668
|
+
const region = document.adcode ? viewRegion(earth.viewer, city?.name ?? document.adcode) : void 0;
|
|
669
|
+
const draft = {
|
|
670
|
+
name: document.name,
|
|
671
|
+
document,
|
|
672
|
+
...document.desc ? { desc: document.desc } : {},
|
|
673
|
+
...document.adcode ? { adcode: document.adcode } : {},
|
|
674
|
+
...document.tags ? { tags: document.tags } : {},
|
|
675
|
+
...thumb ? { thumb } : {},
|
|
676
|
+
...region ? { region } : {}
|
|
677
|
+
};
|
|
678
|
+
return target.save(draft);
|
|
679
|
+
},
|
|
680
|
+
async remove(card) {
|
|
681
|
+
const { source, inner } = splitCardId(card.id);
|
|
682
|
+
const from = sourceOf(source);
|
|
683
|
+
if (!from.writable || !from.remove) throw new SmartEarthError("INVALID_OPTIONS", `${from.label}\u7684\u9884\u8BBE\u662F\u53EA\u8BFB\u7684\u3002`);
|
|
684
|
+
await from.remove(inner);
|
|
685
|
+
},
|
|
686
|
+
documentFromCurrent: (opts) => buildDocument(opts),
|
|
687
|
+
async bucketUploadPackage(adcode, opts) {
|
|
688
|
+
if (!bucket) throw new SmartEarthError("INVALID_OPTIONS", "\u6CA1\u5F00\u6837\u677F\u95F4\u50A8\u5B58\u6876\u6570\u636E\u6E90\u3002");
|
|
689
|
+
const name = requireName(opts.name);
|
|
690
|
+
const snapshot = await earth.environment.captureSnapshot({ time: true });
|
|
691
|
+
const config = {
|
|
692
|
+
...snapshot.environment,
|
|
693
|
+
settings: { ...snapshot.environment.settings ?? {}, clockIsoTime: snapshot.time }
|
|
694
|
+
};
|
|
695
|
+
const draft = await bucket.draftUpload(adcode, { name, ...opts.desc ? { desc: opts.desc } : {} }, config, /* @__PURE__ */ new Date());
|
|
696
|
+
const dataUrl = await thumbnail(opts.thumbnail);
|
|
697
|
+
if (!dataUrl) return draft;
|
|
698
|
+
const filename = draft.filename.replace(/\.json$/, ".jpg");
|
|
699
|
+
const configs = draft.index.configs.map((item) => item.file === draft.filename ? { ...item, thumb: filename } : item);
|
|
700
|
+
return { ...draft, index: { configs }, thumb: { filename, dataUrl } };
|
|
701
|
+
}
|
|
702
|
+
};
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
export { DEFAULT_CITY_CONFIG_BASE_URL, DEFAULT_CITY_LIST_URL, LOCAL_STORAGE_KEY, REGIONS, bucketSource, builtinSource, compatibilityNote, createLocalStorageStorage, createPresetLibrary, httpPresetStore, localSource, normalizeRegionName, regionOf, requireDocument, wrapLegacyConfig };
|