cesium-xs-sdk 1.0.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/src/index.d.ts ADDED
@@ -0,0 +1,358 @@
1
+ // 基础类型别名
2
+ type LonLat = [number, number, number?];
3
+ type LonLatArray = LonLat[];
4
+
5
+ // 用 any 占位 Cesium 类型,避免对 cesium 包类型的强依赖
6
+ // 这样既能提供完整的 SDK 方法自动补全,又不会在 JS/HTML 项目中出现类型解析错误
7
+ type CesiumColor = any;
8
+ type CesiumCartesian2 = any;
9
+ type CesiumCartesian3 = any;
10
+ type CesiumCartographic = any;
11
+ type CesiumHeightReference = any;
12
+ type CesiumViewer = any;
13
+ type CesiumEntity = any;
14
+ type CesiumMaterial = any;
15
+
16
+ // ==================== 枚举 ====================
17
+
18
+ /**
19
+ * 图层类型
20
+ */
21
+ export type LayerType = 'xyz' | 'wmts' | 'wms' | 'tms' | 'single_image' | 'terrain' | '3dtiles';
22
+
23
+ /**
24
+ * 标绘图元类型
25
+ */
26
+ export type PlotGraphicType =
27
+ | 'triangle'
28
+ | 'sector'
29
+ | 'ellipse'
30
+ | 'fineArrow'
31
+ | 'assaultDirection'
32
+ | 'attackArrow'
33
+ | 'swallowtailAttackArrow'
34
+ | 'squadCombat'
35
+ | 'swallowtailSquadCombat'
36
+ | 'doubleArrow'
37
+ | 'straightArrow'
38
+ | 'curvedArrow'
39
+ | 'curve';
40
+
41
+ /**
42
+ * 绘制工具类型
43
+ */
44
+ export type DrawType =
45
+ | 'point'
46
+ | 'line'
47
+ | 'polygon'
48
+ | 'fineArrow'
49
+ | 'straightArrow'
50
+ | 'assaultDirection'
51
+ | 'triangle'
52
+ | 'sector'
53
+ | 'ellipse'
54
+ | 'attackArrow'
55
+ | 'swallowtailAttackArrow'
56
+ | 'squadCombat'
57
+ | 'swallowtailSquadCombat'
58
+ | 'doubleArrow'
59
+ | 'curvedArrow'
60
+ | 'curve';
61
+
62
+ /**
63
+ * 编辑模式
64
+ */
65
+ export type EditMode = 'vertex' | 'move';
66
+
67
+ /**
68
+ * 选择类型
69
+ */
70
+ export type SelectType = 'point' | 'box';
71
+
72
+ /**
73
+ * 删除模式
74
+ */
75
+ export type DeleteMode = 'single' | 'multiple';
76
+
77
+ /**
78
+ * 工具名称
79
+ */
80
+ export type ToolName = 'draw' | 'edit' | 'select' | 'delete';
81
+
82
+ // ==================== 包装器 ====================
83
+
84
+ /**
85
+ * 点图元包装器
86
+ */
87
+ export class PointWrapper {
88
+ setPosition(coords: LonLat): this;
89
+ setColor(color: CesiumColor): this;
90
+ setPixelSize(size: number): this;
91
+ setVisible(visible: boolean): this;
92
+ setHeightReference(ref: CesiumHeightReference): this;
93
+ setDisableDepthTestDistance(distance: number): this;
94
+ setOutlineColor(color: CesiumColor): this;
95
+ setOutlineWidth(width: number): this;
96
+ setName(name: string): this;
97
+ setNameVisible(visible: boolean): this;
98
+ setText(text: string, options?: Record<string, any>): this;
99
+ setFont(font: string): this;
100
+ setTextColor(color: CesiumColor): this;
101
+ setTextOutlineColor(color: CesiumColor): this;
102
+ setTextOutlineWidth(width: number): this;
103
+ setAlignType(alignType: number): this;
104
+ setTextPixelOffset(offset: CesiumCartesian2): this;
105
+ setTextScale(scale: number): this;
106
+ remove(): this;
107
+ }
108
+
109
+ /**
110
+ * 线图元包装器
111
+ */
112
+ export class LineWrapper {
113
+ setPositions(coords: LonLatArray): this;
114
+ setColor(color: CesiumColor): this;
115
+ setWidth(width: number): this;
116
+ setMaterial(material: CesiumMaterial | CesiumColor): this;
117
+ setVisible(visible: boolean): this;
118
+ setClampToGround(clamp: boolean): this;
119
+ setName(name: string): this;
120
+ setLabel(text: string, options?: Record<string, any>): this;
121
+ remove(): this;
122
+ }
123
+
124
+ /**
125
+ * 面图元包装器
126
+ */
127
+ export class PolygonWrapper {
128
+ setPositions(coords: LonLatArray): this;
129
+ setColor(color: CesiumColor): this;
130
+ setOutlineColor(color: CesiumColor): this;
131
+ setOutlineWidth(width: number): this;
132
+ setVisible(visible: boolean): this;
133
+ setClampToGround(clamp: boolean): this;
134
+ setHeightReference(ref: CesiumHeightReference): this;
135
+ setName(name: string): this;
136
+ setLabel(text: string, options?: Record<string, any>): this;
137
+ remove(): this;
138
+ }
139
+
140
+ /**
141
+ * 模型图元包装器
142
+ */
143
+ export class ModelWrapper {
144
+ setPosition(coords: LonLat): this;
145
+ setUri(uri: string): this;
146
+ setScale(scale: number): this;
147
+ setMinimumPixelSize(size: number): this;
148
+ setVisible(visible: boolean): this;
149
+ setHeightReference(ref: CesiumHeightReference): this;
150
+ setName(name: string): this;
151
+ remove(): this;
152
+ }
153
+
154
+ /**
155
+ * 文本图元包装器
156
+ */
157
+ export class TextWrapper {
158
+ setPosition(coords: LonLat): this;
159
+ setText(text: string): this;
160
+ setFont(font: string): this;
161
+ setColor(color: CesiumColor): this;
162
+ setOutlineColor(color: CesiumColor): this;
163
+ setOutlineWidth(width: number): this;
164
+ setPixelOffset(offset: CesiumCartesian2): this;
165
+ setVisible(visible: boolean): this;
166
+ setHeightReference(ref: CesiumHeightReference): this;
167
+ setName(name: string): this;
168
+ remove(): this;
169
+ }
170
+
171
+ /**
172
+ * 图层包装器
173
+ */
174
+ export interface LayerWrapper {
175
+ setVisible(visible: boolean): this;
176
+ setAlpha?(alpha: number): this;
177
+ setBrightness?(brightness: number): this;
178
+ setContrast?(contrast: number): this;
179
+ setHue?(hue: number): this;
180
+ setSaturation?(saturation: number): this;
181
+ setGamma?(gamma: number): this;
182
+ getExtent?(): number[];
183
+ locateTo?(): void;
184
+ remove(): this;
185
+ [key: string]: any;
186
+ }
187
+
188
+ // ==================== 模块 ====================
189
+
190
+ /**
191
+ * 场景模块
192
+ */
193
+ export class SceneModule {
194
+ lonLatToWorld(lon: number, lat: number, height?: number): CesiumCartesian3;
195
+ worldToLonLat(cartesian: CesiumCartesian3): LonLat;
196
+ screenToWorld(x: number, y: number): CesiumCartesian3 | null;
197
+ worldToScreen(cartesian: CesiumCartesian3): CesiumCartesian2;
198
+ screenToLonLat(x: number, y: number): LonLat | null;
199
+ lonLatToScreen(lon: number, lat: number, height?: number): CesiumCartesian2;
200
+ lonLatToCartographic(lon: number, lat: number, height?: number): CesiumCartographic;
201
+ cartographicToLonLat(cartographic: CesiumCartographic): LonLat;
202
+ createLayer(url: string, options?: Record<string, any>): Promise<LayerWrapper>;
203
+ removeLayer(layerId: string): boolean;
204
+ getLayer(layerId: string): LayerWrapper | undefined;
205
+ flyToLonLat(params?: { lon?: number; lat?: number; height?: number; duration?: number }): void;
206
+ setView(params?: { lon?: number; lat?: number; height?: number; heading?: number; pitch?: number; roll?: number }): void;
207
+ }
208
+
209
+ /**
210
+ * 图元模块
211
+ */
212
+ export class GraphicModule {
213
+ createPoint(graphicId: string): PointWrapper;
214
+ createLine(graphicId: string): LineWrapper;
215
+ createPolygon(graphicId: string): PolygonWrapper;
216
+ createModel(graphicId: string): ModelWrapper;
217
+ createText(graphicId: string): TextWrapper;
218
+ createPlotGraphic(graphicId: string, plotType: PlotGraphicType, controlPoints?: LonLatArray): LineWrapper | PolygonWrapper;
219
+ createFineArrow(graphicId: string, controlPoints?: LonLatArray): PolygonWrapper;
220
+ createAssaultDirection(graphicId: string, controlPoints?: LonLatArray): PolygonWrapper;
221
+ createStraightArrow(graphicId: string, controlPoints?: LonLatArray): LineWrapper;
222
+ createTriangle(graphicId: string, controlPoints?: LonLatArray): PolygonWrapper;
223
+ createSector(graphicId: string, center: LonLat, radius: number, startAngle: number, endAngle: number): PolygonWrapper;
224
+ createEllipse(graphicId: string, controlPoints?: LonLatArray): PolygonWrapper;
225
+ createAttackArrow(graphicId: string, controlPoints?: LonLatArray): PolygonWrapper;
226
+ createSwallowtailAttackArrow(graphicId: string, controlPoints?: LonLatArray): PolygonWrapper;
227
+ createSquadCombat(graphicId: string, controlPoints?: LonLatArray): PolygonWrapper;
228
+ createSwallowtailSquadCombat(graphicId: string, controlPoints?: LonLatArray): PolygonWrapper;
229
+ createDoubleArrow(graphicId: string, controlPoints?: LonLatArray): PolygonWrapper;
230
+ createCurvedArrow(graphicId: string, controlPoints?: LonLatArray): LineWrapper;
231
+ createCurve(graphicId: string, controlPoints?: LonLatArray): LineWrapper;
232
+ removeGraphic(graphicId: string): boolean;
233
+ getGraphic(graphicId: string): CesiumEntity | undefined;
234
+ }
235
+
236
+ /**
237
+ * 事件模块
238
+ */
239
+ export class EventModule {
240
+ registerObserver(eventType: string, callback: (...args: any[]) => void): string;
241
+ unregisterObserver(eventType: string, observerId: string): void;
242
+ unregisterAllObservers(): void;
243
+ getZoomLevel(): number;
244
+ }
245
+
246
+ /**
247
+ * 编辑器模块
248
+ */
249
+ export class EditorModule {
250
+ activateDrawTool(type?: DrawType): this;
251
+ activateSelectTool(type?: SelectType): this;
252
+ activateEditTool(mode?: EditMode): this;
253
+ activateDeleteTool(mode?: DeleteMode): this;
254
+ deactivateTool(): this;
255
+ getDrawTool(): any;
256
+ getSelectTool(): any;
257
+ getEditTool(): any;
258
+ onDrawComplete(callback: (data: any) => void): this;
259
+ onDrawProgress(callback: (data: any) => void): this;
260
+ onDrawCancel(callback: () => void): this;
261
+ onEditComplete(callback: (data: any) => void): this;
262
+ onEditSelect(callback: (data: any) => void): this;
263
+ onSelect(callback: (data: any) => void): this;
264
+ onSelectionChange(callback: (data: any) => void): this;
265
+ onSelectionComplete(callback: (data: any) => void): this;
266
+ onBeforeDelete(callback: (data: any) => void): this;
267
+ onDelete(callback: (data: any) => void): this;
268
+ onDeleteMultiple(callback: (data: any) => void): this;
269
+ }
270
+
271
+ /**
272
+ * 模块管理器
273
+ */
274
+ export class ModuleManager {
275
+ /** Cesium Viewer 实例 */
276
+ readonly cesiumViewer: CesiumViewer;
277
+ /** Cesium Viewer 快捷别名 */
278
+ readonly viewer: CesiumViewer;
279
+ /** 获取任意模块(保留向后兼容) */
280
+ getModule(moduleName: string): any;
281
+ /** 场景模块 */
282
+ readonly sceneModule: SceneModule;
283
+ /** 图元模块 */
284
+ readonly graphicModule: GraphicModule;
285
+ /** 事件模块 */
286
+ readonly eventModule: EventModule;
287
+ /** 编辑器模块 */
288
+ readonly editorModule: EditorModule;
289
+ /** 销毁 SDK */
290
+ destroy(): void;
291
+ /** 重置单例 */
292
+ static reset(): void;
293
+ }
294
+
295
+ /**
296
+ * 全局配置类
297
+ */
298
+ export class Config {
299
+ static getInstance(): Config;
300
+ setConfig(options: Record<string, any>): void;
301
+ getConfig(): Record<string, any>;
302
+ }
303
+
304
+ /**
305
+ * 坐标工具类
306
+ */
307
+ export class CoordinateUtil {
308
+ // 具体方法可按需补充
309
+ }
310
+
311
+ /**
312
+ * 地理计算工具类
313
+ */
314
+ export class GeoCalcUtil {
315
+ // 具体方法可按需补充
316
+ }
317
+
318
+ // ==================== SDK 入口 ====================
319
+
320
+ interface Xs3dSDK {
321
+ /**
322
+ * 初始化 SDK
323
+ * @param container 容器元素 ID 或 HTMLElement
324
+ * @param config 初始化配置
325
+ */
326
+ init(container: string | HTMLElement, config?: Record<string, any>): ModuleManager;
327
+ /**
328
+ * 获取模块(保留向后兼容)
329
+ * @deprecated 推荐通过 init 返回的实例访问模块
330
+ */
331
+ getModule(moduleName: string): any;
332
+ /** 销毁 SDK */
333
+ destroy(): void;
334
+
335
+ Config: typeof Config;
336
+ ModuleManager: typeof ModuleManager;
337
+ SceneModule: typeof SceneModule;
338
+ GraphicModule: typeof GraphicModule;
339
+ EventModule: typeof EventModule;
340
+ EditorModule: typeof EditorModule;
341
+ CoordinateUtil: typeof CoordinateUtil;
342
+ GeoCalcUtil: typeof GeoCalcUtil;
343
+
344
+ DrawType: Record<string, DrawType>;
345
+ EditMode: Record<string, EditMode>;
346
+ SelectType: Record<string, SelectType>;
347
+ DeleteMode: Record<string, DeleteMode>;
348
+ ToolName: Record<string, ToolName>;
349
+ LayerType: Record<string, LayerType>;
350
+ PlotGraphicType: Record<string, PlotGraphicType>;
351
+ }
352
+
353
+ declare const xs3d: Xs3dSDK;
354
+ export default xs3d;
355
+
356
+ // 命名导出(与 index.js 保持一致)
357
+ export { Config, ModuleManager, SceneModule, GraphicModule, EventModule, EditorModule, CoordinateUtil, GeoCalcUtil };
358
+ export { DrawType, EditMode, SelectType, DeleteMode, ToolName, LayerType, PlotGraphicType };
package/src/index.js ADDED
@@ -0,0 +1,59 @@
1
+ // src/index.js
2
+ import { Config } from './core/Config.js';
3
+ import { ModuleManager } from './core/ModuleManager.js';
4
+ import { SceneModule, LayerType } from './modules/SceneModule.js';
5
+ import { GraphicModule } from './modules/GraphicModule.js';
6
+ import { EventModule } from './modules/EventModule.js';
7
+ import { EditorModule, DrawType, EditMode, SelectType, DeleteMode, ToolName } from './modules/EditorModule.js';
8
+ import { PlotGraphicType } from './modules/PlotGraphicType.js';
9
+ import { CoordinateUtil } from './utils/CoordinateUtil.js';
10
+ import { GeoCalcUtil } from './utils/GeoCalcUtil.js';
11
+
12
+ // 核心 SDK 实例(直接导出,无嵌套)
13
+ const xs3d = {
14
+ // 初始化 SDK
15
+ init: (container, config = {}) => {
16
+ // 设置 Cesium Token(提前处理,避免内部模块重复设置)
17
+ if (config.cesiumToken) {
18
+ window.Cesium.Ion.defaultAccessToken = config.cesiumToken;
19
+ }
20
+ // 设置全局配置
21
+ Config.getInstance().setConfig(config);
22
+ // 初始化模块管理器
23
+ const moduleManager = ModuleManager.getInstance();
24
+ moduleManager.initViewer(container);
25
+ return moduleManager;
26
+ },
27
+ // 获取模块
28
+ getModule: (moduleName) => {
29
+ return ModuleManager.getInstance().getModule(moduleName);
30
+ },
31
+ // 销毁 SDK
32
+ destroy: () => {
33
+ ModuleManager.reset();
34
+ Config.getInstance().setConfig({});
35
+ if (window.Cesium?.Ion) {
36
+ window.Cesium.Ion.defaultAccessToken = '';
37
+ }
38
+ },
39
+ // 暴露核心类(方便高级使用)
40
+ Config,
41
+ ModuleManager,
42
+ SceneModule,
43
+ GraphicModule,
44
+ EventModule,
45
+ EditorModule,
46
+ CoordinateUtil,
47
+ GeoCalcUtil,
48
+ // 导出工具枚举
49
+ DrawType,
50
+ EditMode,
51
+ SelectType,
52
+ DeleteMode,
53
+ ToolName,
54
+ LayerType,
55
+ PlotGraphicType
56
+ };
57
+
58
+ // 仅默认导出(Rollup 会自动处理 UMD 全局变量)
59
+ export default xs3d;