@ruckc/truss-ol 1.55.1 → 1.57.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/README.md +64 -4
- package/dist/Map.svelte +498 -59
- package/dist/Map.svelte.d.ts +82 -5
- package/dist/contextmenu.d.ts +9 -0
- package/dist/contextmenu.js +23 -0
- package/dist/edit.d.ts +42 -0
- package/dist/edit.js +116 -0
- package/dist/index.d.ts +1 -1
- package/dist/locate.d.ts +9 -0
- package/dist/locate.js +29 -0
- package/dist/types.d.ts +138 -1
- package/package.json +2 -2
package/dist/Map.svelte.d.ts
CHANGED
|
@@ -29,10 +29,27 @@ export type MapLabels = {
|
|
|
29
29
|
legendRange: (title: string, from: string, to: string) => string;
|
|
30
30
|
/** Shown when the map could not be created; `reason` is the error message. */
|
|
31
31
|
loadFailed: (reason: string) => string;
|
|
32
|
+
/** Tooltip of the attribution's show/hide button. The provider credits themselves come from the tile sources and are not translated here. */
|
|
33
|
+
attributions: string;
|
|
34
|
+
/** Accessible name of a popup opened with `openPopup` when it passes no `label`. */
|
|
35
|
+
details: string;
|
|
36
|
+
/** Accessible name of the locate button (`controls={{ locate: true }}`). */
|
|
37
|
+
locate: string;
|
|
38
|
+
/** Announced when the locate control first finds the reader; `accuracy` in metres. */
|
|
39
|
+
located: (accuracy: number) => string;
|
|
40
|
+
/** Shown when the reader refused location access. */
|
|
41
|
+
locateDenied: string;
|
|
42
|
+
/** Shown when the position could not be found (no fix, or it took too long). */
|
|
43
|
+
locateUnavailable: string;
|
|
44
|
+
/** Shown when the browser has no Geolocation API. */
|
|
45
|
+
locateUnsupported: string;
|
|
46
|
+
/** Announced when `drawMode` changes: how to draw or edit with the pointer and the keyboard. */
|
|
47
|
+
drawing: (mode: 'Point' | 'LineString' | 'Polygon' | 'modify' | 'move') => string;
|
|
32
48
|
};
|
|
33
49
|
import { type Snippet } from 'svelte';
|
|
34
50
|
import 'ol/ol.css';
|
|
35
|
-
import type { Basemap, FitOptions, FitTarget, MapApi, MapFeatureInfo, MapMarker, MapSelection, MapViewChange, MapViewState } from './types.js';
|
|
51
|
+
import type { Basemap, FitOptions, FitTarget, MapApi, MapAttribution, MapControls, MapFeatureInfo, MapInitialView, MapMarker, MapSelection, MapViewChange, MapViewState } from './types.js';
|
|
52
|
+
import type { MapContextMenuEvent, MapDrawEnd, MapDrawMode, MapEditEnd, MapLocateError, MapLocation, MapPopupOptions } from './types.js';
|
|
36
53
|
import { type ClusterStyle, type FeatureStyle, type HeatmapOptions, type MapStyle, type PointsInput, type PointsMode, type Props } from './util.js';
|
|
37
54
|
type $$ComponentProps = {
|
|
38
55
|
/** [lon, lat] */
|
|
@@ -104,10 +121,28 @@ type $$ComponentProps = {
|
|
|
104
121
|
selected?: string;
|
|
105
122
|
/** The visible lon/lat extent `[west, south, east, north]`, updated whenever the map moves. Bindable, read-only output: setting it does nothing. */
|
|
106
123
|
extent?: [number, number, number, number];
|
|
107
|
-
/** Map height: px, or any CSS length. Default 420
|
|
124
|
+
/** Map height: px, or any CSS length (`'100%'`, `'60vh'`, `'calc(100dvh - 56px)'`). Default 420, or `'100%'` with `variant="bare"`. */
|
|
108
125
|
height?: number | string;
|
|
109
|
-
/**
|
|
126
|
+
/** `bare`: no border and no corner radius, and (unless `height` says otherwise) the full height of its container, for an edge-to-edge map. */
|
|
127
|
+
variant?: 'default' | 'bare';
|
|
128
|
+
/** Fit the view to the data (`fitTarget`, else markers/features/points) on first load, once the container has a size. It wins over `initialView` when there is data. */
|
|
110
129
|
fitOnLoad?: boolean;
|
|
130
|
+
/**
|
|
131
|
+
* Where the map starts, applied before anything is reported: `{ center: [lon, lat], zoom }`, or `{ extent: [west, south, east, north], padding?, maxZoom? }`
|
|
132
|
+
* (fitted as soon as the container has a size). Wins over `center` and `zoom`; read once, on mount. A saved view restored here never makes `onviewchange` fire.
|
|
133
|
+
*/
|
|
134
|
+
initialView?: MapInitialView;
|
|
135
|
+
/** Which built-in toolbar buttons show: `{ zoom: false }` hides zoom in/out, `{ fit: false }` the fit button. Both show by default; the `toolbar` snippet always does. */
|
|
136
|
+
controls?: MapControls;
|
|
137
|
+
/**
|
|
138
|
+
* What "the data" is for the fit button, `fitOnLoad`, `fitOnDataChange`, `refitOnResize` and `fit()` without an argument: anything `fit()` accepts,
|
|
139
|
+
* or a function returning it (called at each fit; `undefined` means nothing to fit). Default: the markers, GeoJSON, points and `trussInteractive` layers.
|
|
140
|
+
*/
|
|
141
|
+
fitTarget?: FitTarget | (() => FitTarget | undefined);
|
|
142
|
+
/** Replaces what the fit button does (for example a fit with your own options). `fitOnLoad` and `fit()` are unaffected. */
|
|
143
|
+
onfit?: () => void;
|
|
144
|
+
/** OpenLayers' attribution: `collapsed` `false` (default: expanded), `true`, or `'auto'` (collapsed while the map is narrower than 640px, measured on the map, not the viewport). */
|
|
145
|
+
attribution?: MapAttribution;
|
|
111
146
|
/** Fit the view again whenever `markers`, `geojson` or `points` change. Default false. */
|
|
112
147
|
fitOnDataChange?: boolean;
|
|
113
148
|
/** Re-measure the map when its container resizes (always), and also refit to the data. Default false. */
|
|
@@ -122,11 +157,47 @@ type $$ComponentProps = {
|
|
|
122
157
|
featurePopup?: Snippet<[feature: MapFeatureInfo]>;
|
|
123
158
|
/** Extra controls appended to the toolbar. */
|
|
124
159
|
toolbar?: Snippet;
|
|
160
|
+
/**
|
|
161
|
+
* A click or the keyboard selected a marker, GeoJSON feature or point (or cleared the selection: `null`). Layers your app adds to `api.map`
|
|
162
|
+
* take no part unless they carry the `trussInteractive: true` property; their features then report the layer's `id` property as `layer`.
|
|
163
|
+
*/
|
|
125
164
|
onselect?: (s: MapSelection | null) => void;
|
|
126
|
-
/**
|
|
165
|
+
/**
|
|
166
|
+
* Fired when the map has moved (`moveend`), including the load-time fit of `fitOnLoad` when it moved the view: the centre `[lon, lat]`, zoom and
|
|
167
|
+
* visible extent `[west, south, east, north]`. Not fired for the starting view (`center`/`zoom` or `initialView`) nor for a move that changed nothing.
|
|
168
|
+
*/
|
|
127
169
|
onviewchange?: (v: MapViewChange) => void;
|
|
128
|
-
/** Called once the map exists, with the imperative API (`map`, `fit`, `setView`, `getView`, `updateSize`). */
|
|
170
|
+
/** Called once the map exists, with the imperative API (`map`, `fit`, `setView`, `getView`, `updateSize`, `select`, `openPopup`, `closePopup`, `finishDrawing`, `abortDrawing`, `undoLastPoint`). */
|
|
129
171
|
onready?: (api: MapApi) => void;
|
|
172
|
+
/**
|
|
173
|
+
* Draw or edit (`null`, the default, does neither): `'Point'`, `'LineString'` or `'Polygon'` draws a new feature (`ondrawend`), `'modify'` drags
|
|
174
|
+
* vertices of existing ones (`onmodifyend`), `'move'` drags whole features (`onmoveend`). The target is `editLayer`, else the map's own `geojson`.
|
|
175
|
+
* While drawing, clicks do not select, and the focused map takes Space (add a vertex at the centre), Enter (finish), Backspace and Escape.
|
|
176
|
+
*/
|
|
177
|
+
drawMode?: MapDrawMode | null;
|
|
178
|
+
/**
|
|
179
|
+
* The vector layer `drawMode` works on, by its `id` property: a layer your app added to `api.map` (`properties: { id: 'places' }`).
|
|
180
|
+
* Drawn features are added to its source. Without it, `modify` and `move` edit the map's own GeoJSON features and a drawn feature is only
|
|
181
|
+
* reported (add it to `geojson`). A layer that is not on the map yet is waited for.
|
|
182
|
+
*/
|
|
183
|
+
editLayer?: string;
|
|
184
|
+
/** Snap the pointer to the vertices and edges of the features being edited. Default true. */
|
|
185
|
+
snap?: boolean;
|
|
186
|
+
/** A point, line or polygon was drawn: its GeoJSON (lon/lat) and the OpenLayers feature. */
|
|
187
|
+
ondrawend?: (e: MapDrawEnd) => void;
|
|
188
|
+
/** Vertices were dragged (`drawMode: 'modify'`): the changed features with their new geometry. */
|
|
189
|
+
onmodifyend?: (e: MapEditEnd) => void;
|
|
190
|
+
/** Features were dragged (`drawMode: 'move'`). */
|
|
191
|
+
onmoveend?: (e: MapEditEnd) => void;
|
|
192
|
+
/** The locate control (`controls.locate`) has a position: on the first fix and every update while it is on. */
|
|
193
|
+
onlocate?: (l: MapLocation) => void;
|
|
194
|
+
/** The locate control failed: permission denied, no position, a timeout, or no Geolocation API. The map shows `message` too. */
|
|
195
|
+
onlocateerror?: (e: MapLocateError) => void;
|
|
196
|
+
/**
|
|
197
|
+
* A right-click, a touch long-press, or the ContextMenu key / Shift+F10 on the focused map (at the selected feature, else the centre): where
|
|
198
|
+
* (`lonLat`, map `pixel`, viewport `x`/`y`) and the feature there. Setting it suppresses the browser's own menu on the map.
|
|
199
|
+
*/
|
|
200
|
+
oncontextmenu?: (e: MapContextMenuEvent) => void;
|
|
130
201
|
/** Replaces the built-in texts (translation). Missing keys keep the defaults. */
|
|
131
202
|
labels?: Partial<MapLabels>;
|
|
132
203
|
};
|
|
@@ -137,6 +208,12 @@ declare const Map: import("svelte").Component<$$ComponentProps, {
|
|
|
137
208
|
}) => void;
|
|
138
209
|
getView: () => MapViewState | undefined;
|
|
139
210
|
updateSize: () => void;
|
|
211
|
+
select: (layer: string | null, id: string | number | null | undefined) => boolean;
|
|
212
|
+
openPopup: (lonLat: [number, number], options: MapPopupOptions) => void;
|
|
213
|
+
closePopup: () => void;
|
|
214
|
+
finishDrawing: () => void;
|
|
215
|
+
abortDrawing: () => void;
|
|
216
|
+
undoLastPoint: () => void;
|
|
140
217
|
}, "center" | "selected" | "zoom" | "extent">;
|
|
141
218
|
type Map = ReturnType<typeof Map>;
|
|
142
219
|
export default Map;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @internal The Map's context-menu gestures (#303): a right-click (`contextmenu`) and a touch long-press (the core's `longPress`, the gesture
|
|
3
|
+
* the tooltips use). The keyboard (ContextMenu key, Shift+F10) is handled by the Map's own key handler, which calls `keyboard()` so the
|
|
4
|
+
* native `contextmenu` event some platforms send after the key is not reported twice.
|
|
5
|
+
*/
|
|
6
|
+
export declare function wireContextMenu(el: HTMLElement, fire: (x: number, y: number, source: 'mouse' | 'touch') => void, touchOk: () => boolean): {
|
|
7
|
+
keyboard(): void;
|
|
8
|
+
dispose(): void;
|
|
9
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { longPress } from '@ruckc/truss-ui';
|
|
2
|
+
/**
|
|
3
|
+
* @internal The Map's context-menu gestures (#303): a right-click (`contextmenu`) and a touch long-press (the core's `longPress`, the gesture
|
|
4
|
+
* the tooltips use). The keyboard (ContextMenu key, Shift+F10) is handled by the Map's own key handler, which calls `keyboard()` so the
|
|
5
|
+
* native `contextmenu` event some platforms send after the key is not reported twice.
|
|
6
|
+
*/
|
|
7
|
+
export function wireContextMenu(el, fire, touchOk) {
|
|
8
|
+
let kb = -Infinity;
|
|
9
|
+
const menu = (e) => {
|
|
10
|
+
e.preventDefault();
|
|
11
|
+
// a long-press in progress reports itself; a key press just did
|
|
12
|
+
if (el.hasAttribute('data-tr-pressing') || performance.now() - kb < 800)
|
|
13
|
+
return;
|
|
14
|
+
fire(e.clientX, e.clientY, 'mouse');
|
|
15
|
+
};
|
|
16
|
+
el.addEventListener('contextmenu', menu);
|
|
17
|
+
const press = longPress(el, ({ x, y }) => { if (touchOk())
|
|
18
|
+
fire(x, y, 'touch'); }, () => { });
|
|
19
|
+
return {
|
|
20
|
+
keyboard() { kb = performance.now(); },
|
|
21
|
+
dispose() { el.removeEventListener('contextmenu', menu); press.dispose(); }
|
|
22
|
+
};
|
|
23
|
+
}
|
package/dist/edit.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type OLMap from 'ol/Map.js';
|
|
2
|
+
import type { GeoJSONFeature, MapDrawEnd, MapDrawMode, MapEditEnd } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* @internal Drawing and editing for the Map (#303): OpenLayers' Draw, Modify, Translate and Snap, loaded on first use.
|
|
5
|
+
* The target is a vector layer found by its `id` property (`editLayer`, an app layer or, later, a `layers` entry of #302), or the
|
|
6
|
+
* Map's own GeoJSON layer. Kept out of Map.svelte so the component only wires props to it.
|
|
7
|
+
*/
|
|
8
|
+
export type EditDeps = {
|
|
9
|
+
map: OLMap;
|
|
10
|
+
/** The Map's own GeoJSON layer: the target without `editLayer`. */
|
|
11
|
+
own: any;
|
|
12
|
+
/** A vector layer by its `id` property. */
|
|
13
|
+
find(id: string): any;
|
|
14
|
+
/** The sketch style (themed; read at each call). */
|
|
15
|
+
style: () => any;
|
|
16
|
+
toGeoJSON(f: any): GeoJSONFeature;
|
|
17
|
+
/** The current callbacks (props can change after the editor was created). */
|
|
18
|
+
events(): {
|
|
19
|
+
drawend?: (e: MapDrawEnd) => void;
|
|
20
|
+
modifyend?: (e: MapEditEnd) => void;
|
|
21
|
+
moveend?: (e: MapEditEnd) => void;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
export declare const isDrawMode: (m: MapDrawMode | null | undefined) => m is "Point" | "LineString" | "Polygon";
|
|
25
|
+
/** Real vertices of a sketch (the last coordinate follows the pointer; a polygon's ring is closed back to its first vertex as well). */
|
|
26
|
+
export declare function sketchVertices(type: string, coords: any): number;
|
|
27
|
+
export declare function createEditor(d: EditDeps): {
|
|
28
|
+
set: (m: MapDrawMode | null, layer: string | undefined, snap: boolean) => Promise<void>;
|
|
29
|
+
finish: () => void;
|
|
30
|
+
abort: () => void;
|
|
31
|
+
undo: () => void;
|
|
32
|
+
/** Whether a draw mode (point, line, polygon) is active. */
|
|
33
|
+
readonly drawing: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* The keyboard on the focused map while drawing: Space adds a vertex at the map centre (`center`), Enter finishes (or places a point),
|
|
36
|
+
* Backspace removes the last vertex, Escape drops the sketch. Returns whether the key was used.
|
|
37
|
+
*/
|
|
38
|
+
key(e: KeyboardEvent, center: number[]): boolean;
|
|
39
|
+
/** The theme changed: redraw the sketch and the vertex handles (their style reads the tokens when drawn). */
|
|
40
|
+
restyle(): void;
|
|
41
|
+
dispose(): void;
|
|
42
|
+
};
|
package/dist/edit.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
export const isDrawMode = (m) => m === 'Point' || m === 'LineString' || m === 'Polygon';
|
|
2
|
+
/** Real vertices of a sketch (the last coordinate follows the pointer; a polygon's ring is closed back to its first vertex as well). */
|
|
3
|
+
export function sketchVertices(type, coords) {
|
|
4
|
+
return type === 'Polygon' ? Math.max(0, coords[0].length - 2) : coords.length - 1;
|
|
5
|
+
}
|
|
6
|
+
export function createEditor(d) {
|
|
7
|
+
let mode = null;
|
|
8
|
+
let ints = [];
|
|
9
|
+
let draw;
|
|
10
|
+
let sketch;
|
|
11
|
+
let token = 0;
|
|
12
|
+
let unwait;
|
|
13
|
+
const teardown = () => {
|
|
14
|
+
for (const i of ints)
|
|
15
|
+
d.map.removeInteraction(i);
|
|
16
|
+
ints = [];
|
|
17
|
+
draw = undefined;
|
|
18
|
+
sketch = undefined;
|
|
19
|
+
unwait?.();
|
|
20
|
+
unwait = undefined;
|
|
21
|
+
};
|
|
22
|
+
/** Switch to `m` on the layer `layer` (`undefined`: the Map's own GeoJSON); `null` stops. A layer that is not on the map yet is waited for. */
|
|
23
|
+
async function set(m, layer, snap) {
|
|
24
|
+
const t = ++token;
|
|
25
|
+
teardown();
|
|
26
|
+
mode = m;
|
|
27
|
+
if (!m)
|
|
28
|
+
return;
|
|
29
|
+
const l = layer === undefined ? d.own : d.find(layer);
|
|
30
|
+
if (!l) {
|
|
31
|
+
// a layer the app adds a moment later (created, then added in its own effect): start once it is there
|
|
32
|
+
const coll = d.map.getLayers();
|
|
33
|
+
const on = () => { if (d.find(layer))
|
|
34
|
+
void set(m, layer, snap); };
|
|
35
|
+
coll.on('add', on);
|
|
36
|
+
unwait = () => coll.un('add', on);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const [{ default: Draw }, { default: Modify }, { default: Translate }, { default: Snap }] = await Promise.all([
|
|
40
|
+
import('ol/interaction/Draw.js'), import('ol/interaction/Modify.js'), import('ol/interaction/Translate.js'), import('ol/interaction/Snap.js')
|
|
41
|
+
]);
|
|
42
|
+
if (t !== token)
|
|
43
|
+
return;
|
|
44
|
+
const source = l.getSource();
|
|
45
|
+
const tag = layer === undefined ? {} : { layer };
|
|
46
|
+
const report = (fs) => fs.map((f) => ({ ...(f.getId() !== undefined && { id: f.getId() }), feature: d.toGeoJSON(f), olFeature: f }));
|
|
47
|
+
if (isDrawMode(m)) {
|
|
48
|
+
// with an editLayer the drawn feature goes into its source; without one it is only reported (the app adds it to `geojson`)
|
|
49
|
+
draw = new Draw({ ...(layer !== undefined && { source }), type: m, style: d.style });
|
|
50
|
+
draw.on('drawstart', (e) => { sketch = e.feature; });
|
|
51
|
+
draw.on('drawabort', () => { sketch = undefined; });
|
|
52
|
+
draw.on('drawend', (e) => { sketch = undefined; d.events().drawend?.({ feature: d.toGeoJSON(e.feature), olFeature: e.feature, ...tag }); });
|
|
53
|
+
ints.push(draw);
|
|
54
|
+
}
|
|
55
|
+
else if (m === 'modify') {
|
|
56
|
+
const mod = new Modify({ source, style: d.style });
|
|
57
|
+
mod.on('modifyend', (e) => d.events().modifyend?.({ features: report(e.features.getArray()), ...tag }));
|
|
58
|
+
ints.push(mod);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
const tr = new Translate({ layers: [l], hitTolerance: 6 });
|
|
62
|
+
tr.on('translateend', (e) => d.events().moveend?.({ features: report(e.features.getArray()), ...tag }));
|
|
63
|
+
ints.push(tr);
|
|
64
|
+
}
|
|
65
|
+
// Snap goes last, so it sees the pointer before the interactions above
|
|
66
|
+
if (snap)
|
|
67
|
+
ints.push(new Snap({ source }));
|
|
68
|
+
for (const i of ints)
|
|
69
|
+
d.map.addInteraction(i);
|
|
70
|
+
}
|
|
71
|
+
const enough = () => {
|
|
72
|
+
const g = sketch?.getGeometry();
|
|
73
|
+
return !!g && sketchVertices(g.getType(), g.getCoordinates()) >= (mode === 'Polygon' ? 3 : 2);
|
|
74
|
+
};
|
|
75
|
+
const finish = () => { if (draw && sketch && enough())
|
|
76
|
+
draw.finishDrawing(); };
|
|
77
|
+
const abort = () => { draw?.abortDrawing(); };
|
|
78
|
+
const undo = () => { if (sketch)
|
|
79
|
+
draw.removeLastPoint(); };
|
|
80
|
+
/** Add a vertex at `at` (map coordinates), from the keyboard: a point is placed at once, a line or polygon grows. */
|
|
81
|
+
const add = (at) => {
|
|
82
|
+
draw.appendCoordinates([at]);
|
|
83
|
+
if (mode === 'Point')
|
|
84
|
+
draw.finishDrawing();
|
|
85
|
+
};
|
|
86
|
+
return {
|
|
87
|
+
set,
|
|
88
|
+
finish, abort, undo,
|
|
89
|
+
/** Whether a draw mode (point, line, polygon) is active. */
|
|
90
|
+
get drawing() { return !!draw; },
|
|
91
|
+
/**
|
|
92
|
+
* The keyboard on the focused map while drawing: Space adds a vertex at the map centre (`center`), Enter finishes (or places a point),
|
|
93
|
+
* Backspace removes the last vertex, Escape drops the sketch. Returns whether the key was used.
|
|
94
|
+
*/
|
|
95
|
+
key(e, center) {
|
|
96
|
+
if (!draw)
|
|
97
|
+
return false;
|
|
98
|
+
if (e.key === ' ' || (e.key === 'Enter' && mode === 'Point'))
|
|
99
|
+
add(center);
|
|
100
|
+
else if (e.key === 'Enter')
|
|
101
|
+
finish();
|
|
102
|
+
else if (e.key === 'Backspace' && sketch)
|
|
103
|
+
undo();
|
|
104
|
+
else if (e.key === 'Escape' && sketch)
|
|
105
|
+
abort();
|
|
106
|
+
else
|
|
107
|
+
return false;
|
|
108
|
+
e.preventDefault();
|
|
109
|
+
return true;
|
|
110
|
+
},
|
|
111
|
+
/** The theme changed: redraw the sketch and the vertex handles (their style reads the tokens when drawn). */
|
|
112
|
+
restyle() { for (const i of ints)
|
|
113
|
+
i.getOverlay?.().changed(); },
|
|
114
|
+
dispose() { token++; teardown(); mode = null; }
|
|
115
|
+
};
|
|
116
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ export { default as ScaleBar } from './ScaleBar.svelte';
|
|
|
8
8
|
export type { ScaleBarLabels } from './ScaleBar.svelte';
|
|
9
9
|
export { default as Attribution } from './Attribution.svelte';
|
|
10
10
|
export type { AttributionLabels } from './Attribution.svelte';
|
|
11
|
-
export type { MapMarker, MarkerKind, Basemap, MapSelection, MapFeatureInfo, MapViewState, MapViewChange, FitOptions, FitTarget, MapApi } from './types.js';
|
|
11
|
+
export type { MapMarker, MarkerKind, Basemap, MapSelection, MapFeatureInfo, MapViewState, MapViewChange, FitOptions, FitTarget, MapApi, MapInitialView, MapControls, MapAttribution, MapPopupOptions, MapDrawMode, GeoJSONFeature, MapDrawEnd, MapEditEnd, MapLocateOptions, MapLocation, MapLocateError, MapContextMenuEvent } from './types.js';
|
|
12
12
|
export type { MapStyle, SizeSpec, ColorSpec, FeatureStyle, PieStroke, LegendInfo, ClusterStyle, ClusterSizeSpec, ClusterColorSpec, ClusterBy, ClusterAgg, ClusterInfo } from './style.js';
|
|
13
13
|
export type { PointsInput, PointInput, PointsMode, HeatmapOptions } from './points.js';
|
|
14
14
|
export type { ControlPosition, ScaleUnit, HeadingStatus } from './util.js';
|
package/dist/locate.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { MapLocateError, MapLocateOptions, MapLocation } from './types.js';
|
|
2
|
+
/** @internal The Geolocation API behind the Map's locate control (#303). */
|
|
3
|
+
/** A `GeolocationPositionError` code as the Map reports it. */
|
|
4
|
+
export declare function locateErrorCode(code: number): MapLocateError['code'];
|
|
5
|
+
export declare function toLocation(p: GeolocationPosition): MapLocation;
|
|
6
|
+
/** Resolved `controls.locate` options; `undefined` when the control is off. */
|
|
7
|
+
export declare function locateOptions(v: boolean | MapLocateOptions | undefined): Required<MapLocateOptions> | undefined;
|
|
8
|
+
/** Watch the position: `onfix` on each update, `onerror` once (the watch stops). Returns the stop function. */
|
|
9
|
+
export declare function watchLocation(o: Required<MapLocateOptions>, onfix: (l: MapLocation) => void, onerror: (code: MapLocateError['code']) => void): () => void;
|
package/dist/locate.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** @internal The Geolocation API behind the Map's locate control (#303). */
|
|
2
|
+
/** A `GeolocationPositionError` code as the Map reports it. */
|
|
3
|
+
export function locateErrorCode(code) {
|
|
4
|
+
return code === 1 ? 'denied' : code === 3 ? 'timeout' : 'unavailable';
|
|
5
|
+
}
|
|
6
|
+
export function toLocation(p) {
|
|
7
|
+
const c = p.coords;
|
|
8
|
+
return { lonLat: [c.longitude, c.latitude], accuracy: c.accuracy, heading: c.heading ?? null, speed: c.speed ?? null, timestamp: p.timestamp };
|
|
9
|
+
}
|
|
10
|
+
/** Resolved `controls.locate` options; `undefined` when the control is off. */
|
|
11
|
+
export function locateOptions(v) {
|
|
12
|
+
if (!v)
|
|
13
|
+
return undefined;
|
|
14
|
+
const o = v === true ? {} : v;
|
|
15
|
+
return { follow: o.follow ?? false, zoom: o.zoom ?? 15, enableHighAccuracy: o.enableHighAccuracy ?? true, timeout: o.timeout ?? 15000, maximumAge: o.maximumAge ?? 30000 };
|
|
16
|
+
}
|
|
17
|
+
/** Watch the position: `onfix` on each update, `onerror` once (the watch stops). Returns the stop function. */
|
|
18
|
+
export function watchLocation(o, onfix, onerror) {
|
|
19
|
+
// `Object(undefined)` is `{}`: no navigator (SSR) and no Geolocation API are the same case
|
|
20
|
+
const geo = Object(globalThis.navigator).geolocation;
|
|
21
|
+
let id;
|
|
22
|
+
const stop = () => { if (id !== undefined)
|
|
23
|
+
geo.clearWatch(id); id = undefined; };
|
|
24
|
+
if (!geo)
|
|
25
|
+
onerror('unsupported');
|
|
26
|
+
else
|
|
27
|
+
id = geo.watchPosition((p) => onfix(toLocation(p)), (e) => { stop(); onerror(locateErrorCode(e.code)); }, { enableHighAccuracy: o.enableHighAccuracy, timeout: o.timeout, maximumAge: o.maximumAge });
|
|
28
|
+
return stop;
|
|
29
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type OLMap from 'ol/Map.js';
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
2
3
|
export type MarkerKind = 'neutral' | 'primary' | 'success' | 'warning' | 'danger';
|
|
3
4
|
export type MapMarker = {
|
|
4
5
|
id: string;
|
|
@@ -42,8 +43,11 @@ export type MapSelection = {
|
|
|
42
43
|
} | {
|
|
43
44
|
type: 'feature';
|
|
44
45
|
id?: string | number;
|
|
45
|
-
properties: Record<string, unknown>;
|
|
46
|
+
properties: Record<string, unknown>;
|
|
47
|
+
/** `Point`, `LineString`, `Polygon`, `MultiPolygon`, ... */
|
|
46
48
|
geometryType: string;
|
|
49
|
+
/** The `id` property of the app layer the feature belongs to (a layer added with `trussInteractive: true`); absent for the map's own features. */
|
|
50
|
+
layer?: string;
|
|
47
51
|
};
|
|
48
52
|
/** A GeoJSON or point feature as the popup and selection report it. */
|
|
49
53
|
export type MapFeatureInfo = {
|
|
@@ -54,6 +58,8 @@ export type MapFeatureInfo = {
|
|
|
54
58
|
geometryType: string;
|
|
55
59
|
/** Where the popup is anchored. */
|
|
56
60
|
lonLat: [number, number];
|
|
61
|
+
/** The `id` property of the app layer the feature belongs to (see `trussInteractive`); absent for the map's own features. */
|
|
62
|
+
layer?: string;
|
|
57
63
|
};
|
|
58
64
|
/** The view as `getView()` reports it and `setView()` accepts. */
|
|
59
65
|
export type MapViewState = {
|
|
@@ -87,4 +93,135 @@ export type MapApi = {
|
|
|
87
93
|
getView(): MapViewState;
|
|
88
94
|
/** Re-measure the container (call after resizing it yourself; the map already does this when its container resizes). */
|
|
89
95
|
updateSize(): void;
|
|
96
|
+
/**
|
|
97
|
+
* Select a feature as a click would, without firing `onselect` (like setting `selected`): `layer` is `null` for the map's own markers,
|
|
98
|
+
* GeoJSON features and points, or the `id` property of a vector layer your app added (`new VectorLayer({ properties: { id: 'roads' } })`).
|
|
99
|
+
* `id` `null`/`undefined` clears the selection. Returns false (and changes nothing) when the layer or the feature is not found.
|
|
100
|
+
*/
|
|
101
|
+
select(layer: string | null, id: string | number | null | undefined): boolean;
|
|
102
|
+
/** Open the map's popup at `[lon, lat]` with your content (#303). It replaces the selection popup: one popup shows at a time. */
|
|
103
|
+
openPopup(lonLat: [number, number], options: MapPopupOptions): void;
|
|
104
|
+
/** Close the popup `openPopup` opened (its `onclose` runs). Does nothing when none is open. */
|
|
105
|
+
closePopup(): void;
|
|
106
|
+
/** Finish the line or polygon being drawn (`drawMode`), as Enter does. */
|
|
107
|
+
finishDrawing(): void;
|
|
108
|
+
/** Drop the sketch being drawn, as Escape does. */
|
|
109
|
+
abortDrawing(): void;
|
|
110
|
+
/** Remove the last vertex of the sketch being drawn, as Backspace does. */
|
|
111
|
+
undoLastPoint(): void;
|
|
112
|
+
};
|
|
113
|
+
/** Where the Map starts (`initialView`): a centre and zoom, or a lon/lat extent fitted once the container has a size. */
|
|
114
|
+
export type MapInitialView = {
|
|
115
|
+
/** `[lon, lat]` */
|
|
116
|
+
center?: [number, number];
|
|
117
|
+
zoom?: number;
|
|
118
|
+
/** `[west, south, east, north]`: fitted instead of `center`/`zoom` once the map has a size. */
|
|
119
|
+
extent?: [number, number, number, number];
|
|
120
|
+
/** Pixels kept free around `extent` (a number for all four sides). Default 0. */
|
|
121
|
+
padding?: number | [number, number, number, number];
|
|
122
|
+
/** Do not zoom closer than this when fitting `extent`. Default: the map's `maxZoom`. */
|
|
123
|
+
maxZoom?: number;
|
|
124
|
+
};
|
|
125
|
+
/** The built-in toolbar buttons of the Map (`controls`): each shows unless set to `false`. */
|
|
126
|
+
export type MapControls = {
|
|
127
|
+
/** Zoom in and zoom out. */
|
|
128
|
+
zoom?: boolean;
|
|
129
|
+
/** Fit to data (see `fitTarget` and `onfit`). */
|
|
130
|
+
fit?: boolean;
|
|
131
|
+
/** "Show my location" (off by default): `true`, or options. See `onlocate` / `onlocateerror`. */
|
|
132
|
+
locate?: boolean | MapLocateOptions;
|
|
133
|
+
};
|
|
134
|
+
/** OpenLayers' attribution control of the Map (`attribution`). */
|
|
135
|
+
export type MapAttribution = {
|
|
136
|
+
/** `false` (expanded, the default), `true` (collapsed behind its button), or `'auto'`: collapsed while the map is narrower than 640px. */
|
|
137
|
+
collapsed?: boolean | 'auto';
|
|
138
|
+
};
|
|
139
|
+
/** What `openPopup` takes. */
|
|
140
|
+
export type MapPopupOptions = {
|
|
141
|
+
/** The popup body: a snippet, or an element you rendered (it is moved into the popup, and detached again when the popup closes). */
|
|
142
|
+
content: Snippet | HTMLElement;
|
|
143
|
+
/** Pan the map so the whole popup shows. Default true. */
|
|
144
|
+
autoPan?: boolean;
|
|
145
|
+
/** Show the × button. Default true; Escape closes the popup either way. */
|
|
146
|
+
closeButton?: boolean;
|
|
147
|
+
/** The popup's accessible name (it is a `role="dialog"`). Default: the `details` label. */
|
|
148
|
+
label?: string;
|
|
149
|
+
/** Runs once when the popup closes: × button, Escape, a click on the map, a new selection, another `openPopup`, or `closePopup()`. */
|
|
150
|
+
onclose?: () => void;
|
|
151
|
+
};
|
|
152
|
+
/** What the map draws or edits (`drawMode`): a new point, line or polygon, the vertices of existing features (`modify`), or whole features (`move`). */
|
|
153
|
+
export type MapDrawMode = 'Point' | 'LineString' | 'Polygon' | 'modify' | 'move';
|
|
154
|
+
/** A GeoJSON Feature in lon/lat (EPSG:4326). */
|
|
155
|
+
export type GeoJSONFeature = {
|
|
156
|
+
type: 'Feature';
|
|
157
|
+
id?: string | number;
|
|
158
|
+
geometry: {
|
|
159
|
+
type: string;
|
|
160
|
+
coordinates: unknown;
|
|
161
|
+
} | null;
|
|
162
|
+
properties: Record<string, unknown> | null;
|
|
163
|
+
};
|
|
164
|
+
/** What `ondrawend` receives. */
|
|
165
|
+
export type MapDrawEnd = {
|
|
166
|
+
/** The new feature as GeoJSON (lon/lat). */
|
|
167
|
+
feature: GeoJSONFeature;
|
|
168
|
+
/**
|
|
169
|
+
* The OpenLayers feature. With `editLayer` it is added to that layer's source right after this call: give it an id (`olFeature.setId(saved.id)`)
|
|
170
|
+
* once you have saved it, or remove it again (`source.removeFeature(olFeature)`). Without `editLayer` it is not added anywhere; add `feature` to your `geojson`.
|
|
171
|
+
*/
|
|
172
|
+
olFeature: any;
|
|
173
|
+
/** The `editLayer` it was drawn into; absent for the map's own GeoJSON. */
|
|
174
|
+
layer?: string;
|
|
175
|
+
};
|
|
176
|
+
/** What `onmodifyend` and `onmoveend` receive: every feature the gesture changed, with its new geometry. */
|
|
177
|
+
export type MapEditEnd = {
|
|
178
|
+
features: {
|
|
179
|
+
id?: string | number;
|
|
180
|
+
feature: GeoJSONFeature;
|
|
181
|
+
olFeature: any;
|
|
182
|
+
}[];
|
|
183
|
+
/** The `editLayer`; absent for the map's own GeoJSON. */
|
|
184
|
+
layer?: string;
|
|
185
|
+
};
|
|
186
|
+
/** The `locate` control's options (`controls={{ locate: { ... } }}`). */
|
|
187
|
+
export type MapLocateOptions = {
|
|
188
|
+
/** Keep the view centred on each new position until the reader pans the map; pressing the button again re-centres and resumes. Default false. */
|
|
189
|
+
follow?: boolean;
|
|
190
|
+
/** Zoom in to at least this level on the first fix. Default 15. */
|
|
191
|
+
zoom?: number;
|
|
192
|
+
/** `PositionOptions.enableHighAccuracy`. Default true. */
|
|
193
|
+
enableHighAccuracy?: boolean;
|
|
194
|
+
/** `PositionOptions.timeout` in ms. Default 15000. */
|
|
195
|
+
timeout?: number;
|
|
196
|
+
/** `PositionOptions.maximumAge` in ms. Default 30000. */
|
|
197
|
+
maximumAge?: number;
|
|
198
|
+
};
|
|
199
|
+
/** What `onlocate` receives, on every position update while the locate control is on. */
|
|
200
|
+
export type MapLocation = {
|
|
201
|
+
/** `[lon, lat]` */
|
|
202
|
+
lonLat: [number, number];
|
|
203
|
+
/** Radius of the accuracy circle, in metres. */
|
|
204
|
+
accuracy: number;
|
|
205
|
+
heading: number | null;
|
|
206
|
+
speed: number | null;
|
|
207
|
+
timestamp: number;
|
|
208
|
+
};
|
|
209
|
+
/** What `onlocateerror` receives: `denied` (permission refused), `unavailable`, `timeout` or `unsupported` (no Geolocation API). `message` is the text the map shows. */
|
|
210
|
+
export type MapLocateError = {
|
|
211
|
+
code: 'denied' | 'unavailable' | 'timeout' | 'unsupported';
|
|
212
|
+
message: string;
|
|
213
|
+
};
|
|
214
|
+
/** What `oncontextmenu` receives: a right-click, a touch long-press, or the ContextMenu key / Shift+F10 on the focused map. */
|
|
215
|
+
export type MapContextMenuEvent = {
|
|
216
|
+
/** `[lon, lat]` of the point (the map centre, or the selected feature, from the keyboard). */
|
|
217
|
+
lonLat: [number, number];
|
|
218
|
+
/** The point in map pixels, from the map's top-left corner. */
|
|
219
|
+
pixel: [number, number];
|
|
220
|
+
/** The point in viewport (client) pixels: where to open a menu (`ContextMenu.open(x, y)`). */
|
|
221
|
+
x: number;
|
|
222
|
+
y: number;
|
|
223
|
+
/** The feature under the point (the selected one, from the keyboard): the map's own or a `trussInteractive` layer's. */
|
|
224
|
+
feature?: MapFeatureInfo;
|
|
225
|
+
/** What opened it. */
|
|
226
|
+
source: 'mouse' | 'touch' | 'keyboard';
|
|
90
227
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ruckc/truss-ol",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.57.0",
|
|
4
4
|
"description": "OpenLayers map styled with Truss UI",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|
|
26
26
|
"svelte": "^5.20.0",
|
|
27
|
-
"@ruckc/truss-ui": "^1.
|
|
27
|
+
"@ruckc/truss-ui": "^1.57.0",
|
|
28
28
|
"ol": "^10.0.0"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|