@superrouteca/smap-cesium 1.1.5 → 1.1.6

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 CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.1.6 — 2026-10-28
4
+
5
+ - Plan editor: `addPlanEditor(viewer, options)` edits a plan of routes on the globe — the routes, numbered stops,
6
+ vehicle starts and unassigned jobs as entities; drag a stop onto another stop or a route's line to move it, with the
7
+ plan priced as a preview while dragging; the ready-made panel over the viewer in 14 languages. Every edit is a call
8
+ of the Open Platform's plan editing; `remove()` takes it all off the viewer.
9
+
3
10
  ## 1.1.5 — 2026-10-28
4
11
 
5
12
  - The version follows the shared 1.1.5 release; no change of its own.
package/README.md CHANGED
@@ -103,6 +103,38 @@ assets have no sMap counterpart.
103
103
  `czmlToGeoJSON(czml)` gives the converted FeatureCollection with its `skipped` list without a map;
104
104
  `@superrouteca/smap-cesium/data` holds the CZML functions without the cesium package.
105
105
 
106
+ ## Plan editor
107
+
108
+ A person edits a plan of routes on the globe: drag a stop within its route or onto another route, take stops off,
109
+ place new orders, refine, undo and redo. Every edit is a call of the Open Platform's plan editing (the plan editor of
110
+ `@superrouteca/smap`); keep the plan from the `change` event.
111
+
112
+ ```js
113
+ import { addPlanEditor } from '@superrouteca/smap-cesium';
114
+
115
+ const plan = addPlanEditor(viewer, {
116
+ // A short-lived plan editor token your own server mints (POST /v1/plan/token), or { key: 'smk_…' }.
117
+ credentials: { token: () => fetch('/plan-token').then((r) => r.json()) },
118
+ plan: myPlan, // { vehicles, jobs, routes, region?, profile? }
119
+ language: 'fr',
120
+ });
121
+ plan.editor.on('change', (e) => save(e.plan));
122
+ plan.remove(); // entities, events and panel off the viewer
123
+ ```
124
+
125
+ - The plan is a `CustomDataSource` named by `id` (default `plan`): each route's legs on the ground in its colour
126
+ (`colors`), each stop as a numbered pin (bordered dark red with a violation, near black when locked), vehicle
127
+ starts and unassigned jobs as points. Every entity keeps its feature as `smapPlan`.
128
+ - Press a stop or an unassigned job and drag it onto another stop (it goes before that stop) or onto a route's line
129
+ (into that leg); the camera stands still meanwhile, and the plan with the stop there is priced and shown in the
130
+ panel as a preview (`preview: false` turns it off). Release over nothing to leave the plan as it was.
131
+ - The ready-made panel (routes, totals, violations, undo / redo / refine, insert positions, errors, in 14 languages)
132
+ sits over the viewer, top left by default (`panel: { position: 'bottom-left' }`; Cesium's toolbar is top right);
133
+ `panel: false` leaves it out for one of your own built on `plan.editor.state()`. Its stylesheet is added to the
134
+ page unless `css: false`; restyle it through the `.smap-plan-*` classes.
135
+ - `editor` binds a `PlanEditor` you made instead; `remove()` then leaves it running. Destroying the editor removes
136
+ the binding.
137
+
106
138
  ## Example
107
139
 
108
140
  [`examples/index.html`](examples/index.html): serve the repository root (`npx serve .`) and open
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superrouteca/smap-cesium",
3
- "version": "1.1.5",
3
+ "version": "1.1.6",
4
4
  "description": "sMap for CesiumJS: sMap imagery (any scheme, custom style and language) with keys, tokens and node failover, sMap layers as Cesium entities, and Cesium data (CZML, KML, GeoJSON) on sMap maps.",
5
5
  "license": "MIT",
6
6
  "author": "Goodneo Solutions",
@@ -52,7 +52,7 @@
52
52
  "typecheck": "tsc -p test/tsconfig.json"
53
53
  },
54
54
  "dependencies": {
55
- "@superrouteca/smap": "1.1.5"
55
+ "@superrouteca/smap": "1.1.6"
56
56
  },
57
57
  "peerDependencies": {
58
58
  "cesium": "^1.120.0"
@@ -64,6 +64,7 @@
64
64
  },
65
65
  "devDependencies": {
66
66
  "cesium": "1.146.0",
67
+ "jsdom": "30.1.3",
67
68
  "typescript": "6.0.3"
68
69
  }
69
70
  }
package/src/index.js CHANGED
@@ -3,4 +3,5 @@ export { Session, createSession, FLAVORS, ATTRIBUTION } from './session.js';
3
3
  export { createImageryProvider, createWmtsImageryProvider, createBaseLayer, setBasemap, providerViewModels, smapResource, smapCredit } from './imagery.js';
4
4
  export { addSMapData, entitiesToGeoJSON, simplestyleFor, toFeatures } from './entities.js';
5
5
  export { czmlToGeoJSON, czmlPositions, cartesianToDegrees, addCesiumData } from './data.js';
6
- export const version = '1.1.5';
6
+ export { addPlanEditor } from './plan.js';
7
+ export const version = '1.1.6';
package/src/plan.js ADDED
@@ -0,0 +1,172 @@
1
+ // The sMap plan editor (OPEN-ROUTE-EDIT E10) in a Cesium viewer: the plan's routes as entities of a data source, a
2
+ // stop dragged within or between routes with Cesium's screen-space events (the camera stands still meanwhile), and the
3
+ // ready-made panel over the viewer. Every edit is a plan.* call of the Open Platform made by the SDK's PlanEditor.
4
+ import {
5
+ CustomDataSource, Cartesian2, Cartesian3, Color, PinBuilder, VerticalOrigin, ScreenSpaceEventHandler, ScreenSpaceEventType,
6
+ } from 'cesium';
7
+ import { PlanEditor, planEditorParts } from '@superrouteca/smap';
8
+
9
+ const EDITOR_OPTIONS = ['credentials', 'baseUrl', 'fetch', 'app', 'region', 'profile', 'snapshot', 'geometry', 'insertLimit', 'objective'];
10
+ const POSITIONS = ['top-left', 'top-right', 'bottom-left', 'bottom-right'];
11
+
12
+ function pointOf(position) { return position ? { x: position.x, y: position.y } : null; }
13
+
14
+ /**
15
+ * A plan editor in a Cesium viewer.
16
+ *
17
+ * const plan = addPlanEditor(viewer, { credentials: { token: getPlanToken }, plan: myPlan, language: 'fr' });
18
+ * plan.editor.on('change', (e) => save(e.plan));
19
+ *
20
+ * Options: every PlanEditor option (credentials, baseUrl, fetch, app, plan, region, profile, snapshot, geometry,
21
+ * insertLimit, objective) or `editor` (a PlanEditor of your own), plus `id` (the data source's name, default 'plan'),
22
+ * `colors`, `preview` (price the drop target while dragging, default true), `language`, `labels`, `panel` (true,
23
+ * false, or { position: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right', className, labels }) and `css`
24
+ * (add the panel's stylesheet to the page, default true).
25
+ *
26
+ * @returns {{ editor, dataSource, handler, panel, features(), remove() }}
27
+ */
28
+ export function addPlanEditor(viewer, options = {}) {
29
+ if (!viewer || !viewer.scene || !viewer.dataSources) { throw new Error('sMap: pass a Cesium Viewer: addPlanEditor(viewer, options)'); }
30
+ const P = planEditorParts;
31
+ const id = options.id || 'plan';
32
+ const own = !options.editor;
33
+ let editor = options.editor;
34
+ if (own) {
35
+ const o = { plan: null };
36
+ EDITOR_OPTIONS.forEach((k) => { if (options[k] !== undefined) { o[k] = options[k]; } });
37
+ editor = new PlanEditor(o);
38
+ }
39
+ const dataSource = new CustomDataSource(id);
40
+ void viewer.dataSources.add(dataSource);
41
+ const pins = new PinBuilder();
42
+ const scene = viewer.scene;
43
+ const drag = P.planDrag(editor, { preview: options.preview !== false });
44
+ let dragEntity = null;
45
+ let removed = false;
46
+
47
+ function color(css) { return Color.fromCssColorString(css); }
48
+ function draw() {
49
+ if (removed) { return; }
50
+ const f = P.planFeatures(editor, id, options.colors);
51
+ dataSource.entities.removeAll();
52
+ dragEntity = null;
53
+ f.legs.forEach((leg) => {
54
+ const e = dataSource.entities.add({
55
+ polyline: { positions: Cartesian3.fromDegreesArray(leg.geometry.coordinates.flat()), width: 4, material: color(leg.properties.color), clampToGround: true },
56
+ });
57
+ e.smapPlan = leg;
58
+ });
59
+ const point = (feature, fill, size) => {
60
+ const [lng, lat] = feature.geometry.coordinates;
61
+ const e = dataSource.entities.add({
62
+ position: Cartesian3.fromDegrees(lng, lat),
63
+ point: { pixelSize: size, color: color(fill), outlineColor: Color.WHITE, outlineWidth: 2 },
64
+ });
65
+ e.smapPlan = feature;
66
+ return e;
67
+ };
68
+ f.starts.forEach((s) => point(s, s.properties.color, 12));
69
+ f.unassigned.forEach((u) => point(u, '#9ca3af', 12));
70
+ f.stops.forEach((s) => {
71
+ const [lng, lat] = s.geometry.coordinates;
72
+ const e = dataSource.entities.add({
73
+ position: Cartesian3.fromDegrees(lng, lat),
74
+ billboard: { image: pins.fromText(s.properties.label, color(s.properties.border), 36).toDataURL(), verticalOrigin: VerticalOrigin.BOTTOM },
75
+ });
76
+ e.smapPlan = s;
77
+ });
78
+ }
79
+
80
+ // The plan features under a screen position, nearest first as Cesium picks them.
81
+ function hitsAt(position) {
82
+ const picked = scene.drillPick(new Cartesian2(position.x, position.y)) || [];
83
+ const out = [];
84
+ picked.forEach((p) => {
85
+ const e = p && p.id;
86
+ if (e && e.smapPlan && dataSource.entities.values.indexOf(e) >= 0 && out.indexOf(e.smapPlan) < 0) { out.push(e.smapPlan); }
87
+ });
88
+ return out;
89
+ }
90
+ function setCamera(on) { if (scene.screenSpaceCameraController) { scene.screenSpaceCameraController.enableInputs = on; } }
91
+ function showDrag(position) {
92
+ const at = viewer.camera && viewer.camera.pickEllipsoid ? viewer.camera.pickEllipsoid(new Cartesian2(position.x, position.y)) : null;
93
+ if (!at) { return; }
94
+ if (!dragEntity) {
95
+ dragEntity = dataSource.entities.add({ position: at, point: { pixelSize: 14, color: color('#111827'), outlineColor: Color.WHITE, outlineWidth: 2 } });
96
+ } else {
97
+ dragEntity.position = at;
98
+ }
99
+ }
100
+
101
+ const handler = new ScreenSpaceEventHandler(scene.canvas);
102
+ handler.setInputAction((e) => {
103
+ if (removed) { return; }
104
+ const hit = hitsAt(e.position).find((f) => f.properties.kind === 'stop' || f.properties.kind === 'unassigned');
105
+ if (!hit) { return; }
106
+ drag.start(hit.properties.id, pointOf(e.position));
107
+ setCamera(false);
108
+ }, ScreenSpaceEventType.LEFT_DOWN);
109
+ handler.setInputAction((e) => {
110
+ if (!drag.dragging() || !e.endPosition) { return; }
111
+ drag.move(hitsAt(e.endPosition), pointOf(e.endPosition));
112
+ if (drag.moved()) { showDrag(e.endPosition); }
113
+ }, ScreenSpaceEventType.MOUSE_MOVE);
114
+ handler.setInputAction((e) => {
115
+ if (!drag.dragging()) { return; }
116
+ setCamera(true);
117
+ const moved = drag.moved();
118
+ void drag.end(e && e.position ? hitsAt(e.position) : null);
119
+ if (moved) { draw(); }
120
+ }, ScreenSpaceEventType.LEFT_UP);
121
+
122
+ // The panel, over the viewer (Cesium's own toolbar sits top right).
123
+ let panel = null;
124
+ let wrapper = null;
125
+ const container = viewer.container;
126
+ const doc = container && container.ownerDocument;
127
+ if (options.panel !== false && doc) {
128
+ const po = options.panel && typeof options.panel === 'object' ? options.panel : {};
129
+ const position = POSITIONS.indexOf(po.position) >= 0 ? po.position : 'top-left';
130
+ if (options.css !== false) { P.injectCss(doc); }
131
+ panel = P.createPanel(editor, { language: options.language || 'en', labels: po.labels || options.labels, document: doc, colors: options.colors, className: po.className });
132
+ wrapper = doc.createElement('div');
133
+ wrapper.className = 'smap-plan-control smap-plan-' + position;
134
+ const [v, h] = position.split('-');
135
+ wrapper.style.position = 'absolute';
136
+ wrapper.style[v] = '10px';
137
+ wrapper.style[h] = '10px';
138
+ wrapper.style.zIndex = '1';
139
+ wrapper.appendChild(panel.element);
140
+ container.appendChild(wrapper);
141
+ }
142
+
143
+ const onChange = () => draw();
144
+ editor.on('change', onChange);
145
+ const gone = () => handle.remove();
146
+ editor.on('destroy', gone);
147
+ draw();
148
+
149
+ const handle = {
150
+ editor,
151
+ dataSource,
152
+ handler,
153
+ panel,
154
+ features: () => P.planFeatures(editor, id, options.colors),
155
+ remove() {
156
+ if (removed) { return; }
157
+ removed = true;
158
+ drag.cancel();
159
+ setCamera(true);
160
+ editor.off('change', onChange);
161
+ editor.off('destroy', gone);
162
+ handler.destroy();
163
+ viewer.dataSources.remove(dataSource, true);
164
+ if (panel) { panel.destroy(); }
165
+ if (wrapper && wrapper.parentNode) { wrapper.parentNode.removeChild(wrapper); }
166
+ if (own) { editor.destroy(); }
167
+ },
168
+ };
169
+ if (own && options.plan) { editor.setPlan(options.plan).catch(() => {}); }
170
+ return handle;
171
+ }
172
+
package/types/index.d.ts CHANGED
@@ -1,10 +1,12 @@
1
1
  // Types of @superrouteca/smap-cesium.
2
2
  import type {
3
3
  UrlTemplateImageryProvider, WebMapTileServiceImageryProvider, ImageryLayer, ProviderViewModel, Resource, Credit,
4
- GeoJsonDataSource, Viewer, Entity, EntityCollection, DataSource, JulianDate,
4
+ GeoJsonDataSource, Viewer, Entity, EntityCollection, DataSource, JulianDate, CustomDataSource, ScreenSpaceEventHandler,
5
5
  } from 'cesium';
6
6
  import type SMapClient from '@superrouteca/smap/client';
7
- import type { LayerData, PointOptions, LineOptions, PolygonOptions, GeoJSONFeature } from '@superrouteca/smap';
7
+ import type {
8
+ LayerData, PointOptions, LineOptions, PolygonOptions, GeoJSONFeature, PlanEditor, PlanEditorOptions, PlanFeatures, PlanPanel, PlanTextKey,
9
+ } from '@superrouteca/smap';
8
10
 
9
11
  export * from './data.js';
10
12
 
@@ -100,3 +102,36 @@ export declare function entitiesToGeoJSON(entities: EntityCollection | DataSourc
100
102
  };
101
103
  export declare function simplestyleFor(kind: 'points' | 'lines' | 'areas', properties: Record<string, unknown>, options?: Record<string, unknown>): Record<string, string | number>;
102
104
  export declare function toFeatures(data: LayerData): GeoJSONFeature[];
105
+
106
+ /** Where the plan editor's panel sits over the viewer (Cesium's own toolbar is top right). */
107
+ export type PlanPanelPosition = 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';
108
+ export interface CesiumPlanEditorOptions extends Partial<PlanEditorOptions> {
109
+ /** A plan editor of your own to show; without it, one is made from the PlanEditor options (`credentials` needed). */
110
+ editor?: PlanEditor;
111
+ /** The data source's name and the features' `smap_plan` (default 'plan'). */
112
+ id?: string;
113
+ /** Route colours, one per vehicle in the plan's order (repeated). */
114
+ colors?: string[];
115
+ /** Price the drop target while a stop is dragged (default true). */
116
+ preview?: boolean;
117
+ /** The panel's language: any BCP-47 tag, English when none of the 14 fits (default English). */
118
+ language?: string;
119
+ labels?: Partial<Record<PlanTextKey, string>>;
120
+ /** The ready-made panel (default true): false for none, or where it sits. */
121
+ panel?: boolean | { position?: PlanPanelPosition; className?: string; labels?: Partial<Record<PlanTextKey, string>> };
122
+ /** Add the panel's stylesheet to the page (default true). */
123
+ css?: boolean;
124
+ }
125
+ export interface CesiumPlanEditor {
126
+ readonly editor: PlanEditor;
127
+ /** The plan's routes, stops, starts and unassigned jobs as entities (each with `smapPlan`, its feature). */
128
+ readonly dataSource: CustomDataSource;
129
+ /** The screen-space events that drag stops. */
130
+ readonly handler: ScreenSpaceEventHandler;
131
+ readonly panel: PlanPanel | null;
132
+ features(): PlanFeatures;
133
+ /** Takes the entities, the events and the panel away; ends the editor when this binding made it. */
134
+ remove(): void;
135
+ }
136
+ /** A plan editor in a Cesium viewer: the plan as entities, stops dragged within and between routes, the panel over the viewer. */
137
+ export declare function addPlanEditor(viewer: Pick<Viewer, 'dataSources' | 'scene' | 'camera' | 'container'>, options: CesiumPlanEditorOptions): CesiumPlanEditor;