@ruckc/truss-ol 1.56.0 → 1.58.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 +101 -7
- package/dist/Map.svelte +419 -53
- package/dist/Map.svelte.d.ts +78 -2
- package/dist/basemaps.d.ts +106 -0
- package/dist/basemaps.js +50 -0
- 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 +4 -1
- package/dist/index.js +2 -0
- package/dist/layers-ol.d.ts +47 -0
- package/dist/layers-ol.js +185 -0
- package/dist/locate.d.ts +9 -0
- package/dist/locate.js +29 -0
- package/dist/ol-style.d.ts +71 -0
- package/dist/ol-style.js +107 -0
- package/dist/style.d.ts +27 -4
- package/dist/style.js +36 -7
- package/dist/types.d.ts +167 -10
- package/dist/util.d.ts +1 -0
- package/dist/util.js +1 -0
- package/package.json +7 -2
package/README.md
CHANGED
|
@@ -46,7 +46,9 @@ npm install @ruckc/truss-ol @ruckc/truss-ui ol
|
|
|
46
46
|
| Prop | Purpose |
|
|
47
47
|
| --- | --- |
|
|
48
48
|
| `center` (bindable), `zoom` (bindable), `minZoom`, `maxZoom` | The view. `center` is `[lon, lat]`; zoom limits default to 1 and 19. |
|
|
49
|
-
| `basemap` | `'osm'`, `'none'` (themed graticule, works offline), `{ url, attribution?, maxZoom? }
|
|
49
|
+
| `basemap` | `'osm'`, `'none'` (themed graticule, works offline), `{ url, attribution?, maxZoom?, minZoom?, opacity?, themed? }`, `{ osm: true, attribution?, maxZoom?, opacity?, themed? }`, or a stack `{ layers: [...], themed? }` drawn bottom to top. See [Basemaps](#basemaps). |
|
|
50
|
+
| `basemaps`, `basemapId` (bindable), `onbasemapchange` | A list of `{ id, label?, ...basemap }` (`{ none: true }` for the graticule) with a switcher in the toolbar; wins over `basemap`. |
|
|
51
|
+
| `layers` | Data layers, each a managed vector layer: `{ id, geojson \| source \| loader, style?, simplestyle?, visible?, zIndex?, interactive?, label?, opacity?, declutter?, onload? }[]`. See [Layers](#layers). |
|
|
50
52
|
| `markers` | `{ id, lon, lat, label?, description?, kind?, colors?, radius?, stroke? }[]`. Invalid coordinates are skipped. |
|
|
51
53
|
| `geojson` | A GeoJSON Feature or FeatureCollection. |
|
|
52
54
|
| `points` | A large point set: `{ lon, lat, ...properties }[]` or a GeoJSON FeatureCollection of points. |
|
|
@@ -59,19 +61,85 @@ npm install @ruckc/truss-ol @ruckc/truss-ui ol
|
|
|
59
61
|
| `featureStyle` | Per-feature styling by function (`fill`, `opacity`, `stroke`, `strokeWidth`, `radius`, `label`, `colors`). Features may also carry `style` / simplestyle properties. `colors: string[]` draws a pie marker, see below. |
|
|
60
62
|
| `onviewchange`, `extent` (bindable) | `onviewchange({ center, zoom, extent })` fires on `moveend`, including a load-time fit that moved the view, but never for the starting view (`center`/`zoom`, `initialView`); `extent` is `[west, south, east, north]`. `bind:extent` is a read-only output that holds the starting view too. Neither fires when the view did not change, so applying `center`/`zoom` does not loop. |
|
|
61
63
|
| `initialView` | Where the map starts, applied before anything is reported: `{ center: [lon, lat], zoom }` or `{ extent: [west, south, east, north], padding?, maxZoom? }` (fitted once the container has a size). Wins over `center`/`zoom`; `fitOnLoad` wins over it when there is data. Read once, on mount. |
|
|
62
|
-
| `controls`, `fitTarget`, `onfit` | `controls={{ zoom: false }}` / `{{ fit: false }}` hide built-in buttons (both show by default; the `toolbar` snippet always shows). `fitTarget` (anything `fit()` takes, or a function returning it) is "the data" for the fit button, `fitOnLoad`, `fitOnDataChange`, `refitOnResize` and `fit()`; by default the markers, GeoJSON, points and `trussInteractive` layers. `onfit` replaces what the fit button does. |
|
|
64
|
+
| `controls`, `fitTarget`, `onfit` | `controls={{ zoom: false }}` / `{{ fit: false }}` hide built-in buttons (both show by default; the `toolbar` snippet always shows); `{{ locate: true }}` adds "Show my location". `fitTarget` (anything `fit()` takes, or a function returning it) is "the data" for the fit button, `fitOnLoad`, `fitOnDataChange`, `refitOnResize` and `fit()`; by default the markers, GeoJSON, points and `trussInteractive` layers. `onfit` replaces what the fit button does. |
|
|
65
|
+
| `controls`, `fitTarget`, `onfit` | `controls={{ zoom: false }}` / `{{ fit: false }}` / `{{ basemap: false }}` hide built-in buttons (all show by default, the basemap switcher once `basemaps` lists two or more; the `toolbar` snippet always shows). `fitTarget` (anything `fit()` takes, or a function returning it) is "the data" for the fit button, `fitOnLoad`, `fitOnDataChange`, `refitOnResize` and `fit()`; by default the markers, GeoJSON, points and `trussInteractive` layers. `onfit` replaces what the fit button does. |
|
|
63
66
|
| `attribution` | `{ collapsed: false }` (default, expanded), `true`, or `'auto'`: collapsed while the map is narrower than 640px (the map's width, not the viewport's; a reader's own open/close stands until the map crosses 640px). The scale line always stacks above the attribution's measured height. |
|
|
64
67
|
| `variant` | `'bare'`: no border, no radius, and the full height of its container unless `height` is set (an edge-to-edge map). |
|
|
65
68
|
| `selected` (bindable), `onselect` | Selection of a marker, GeoJSON feature or point (or a feature of a `trussInteractive` layer, see below), by click or keyboard. `onselect` and `featurePopup` receive the `geometryType` (`Point`, `Polygon`, ...). |
|
|
66
69
|
| `popup`, `featurePopup`, `toolbar` | Snippets: marker popup body, feature popup body, extra toolbar controls. |
|
|
70
|
+
| `drawMode`, `editLayer`, `snap`, `ondrawend`, `onmodifyend`, `onmoveend` | Draw a point, line or polygon, drag vertices (`'modify'`) or whole features (`'move'`) on an app layer (by its `id` property) or the map's GeoJSON. See below. |
|
|
71
|
+
| `onlocate`, `onlocateerror` | The `controls.locate` button's positions and errors. |
|
|
72
|
+
| `oncontextmenu` | Right-click, touch long-press, ContextMenu key / Shift+F10: `{ lonLat, pixel, x, y, feature?, source }`. |
|
|
67
73
|
| `fitOnLoad`, `fitOnDataChange`, `refitOnResize` | When the view fits the data. `center` and `zoom` are applied only when you change them, so their defaults never undo a fit. |
|
|
68
|
-
| `height`, `onready` | Map height: px or any CSS length (`'100%'`, `'calc(100dvh - 56px)'`), server-rendered as a nonced rule (no `style` attribute); `onready(api)` receives `{ map, fit, setView, getView, updateSize, select }`. |
|
|
74
|
+
| `height`, `onready` | Map height: px or any CSS length (`'100%'`, `'calc(100dvh - 56px)'`), server-rendered as a nonced rule (no `style` attribute); `onready(api)` receives `{ map, fit, setView, getView, updateSize, select, openPopup, closePopup, finishDrawing, abortDrawing, undoLastPoint }`. |
|
|
69
75
|
|
|
70
|
-
Methods (via `bind:this`): `fit(target?, { padding, duration, maxZoom })`, `setView({ center, zoom, duration })`, `getView()`, `updateSize()`, `select(layer, id)`.
|
|
76
|
+
Methods (via `bind:this`): `fit(target?, { padding, duration, maxZoom })`, `setView({ center, zoom, duration })`, `getView()`, `updateSize()`, `select(layer, id)`, `openPopup(lonLat, options)`, `closePopup()`, `finishDrawing()`, `abortDrawing()`, `undoLastPoint()`.
|
|
77
|
+
| `height`, `onready` | Map height: px or any CSS length (`'100%'`, `'calc(100dvh - 56px)'`), server-rendered as a nonced rule (no `style` attribute); `onready(api)` receives `{ map, fit, setView, getView, updateSize, select, layer, reloadLayer }`. |
|
|
78
|
+
|
|
79
|
+
Methods (via `bind:this`): `fit(target?, { padding, duration, maxZoom })`, `setView({ center, zoom, duration })`, `getView()`, `updateSize()`, `select(layer, id)`, `layer(id)` (the OpenLayers layer of a `layers` entry), `reloadLayer(id)`.
|
|
80
|
+
|
|
81
|
+
### Layers
|
|
82
|
+
|
|
83
|
+
Each `layers` entry is an OpenLayers vector layer the Map creates and keeps in step with the list, matched by `id`: only what changed is applied, so an unchanged layer (and its features) is never rebuilt. Features come from `geojson` (lon/lat, replaced when you pass a new object), your own `source` (EPSG:3857; never cleared), or a bbox `loader`. Feature ids are the GeoJSON `id`, else `properties.id`, else the index.
|
|
84
|
+
|
|
85
|
+
- **Style:** `style` is a `FeatureStyle` for every feature or `(properties, { id, type }) => FeatureStyle`. A feature's own `style` object and simplestyle properties win over it (`simplestyle: false` turns that off); whatever neither sets takes the theme tokens.
|
|
86
|
+
- **Order and visibility:** `zIndex` defaults to `30 + index` (above the built-in GeoJSON, points and markers; later entries on top); `visible`, `opacity`, `declutter` (read when the layer is created) and `label` (kept as the OpenLayers layer's `label` property, for your layer list).
|
|
87
|
+
- **Selection:** layers are `interactive` by default: click, hover, Enter/Shift+Enter, `onselect` (`layer: id`), `select(id, featureId)`, the popup and the default fit all include them. `interactive: false` leaves a layer out of hit testing (the fit still covers it while visible).
|
|
88
|
+
- **Loader:** `loader({ bbox, zoom, signal })` is called after the map moves (debounced 250ms) with the view padded by a quarter, unless the last answer covered the view. Resolve to GeoJSON, or `{ geojson, truncated: true }` when the box held more than you sent (the next move loads again). A request the view outran is aborted (`signal`) and its answer ignored. `onload({ loading, count, truncated, error? })` reports progress; `reloadLayer(id)` loads again now.
|
|
89
|
+
- Pass a plain array (or `$state.raw`), as for `geojson`; each entry's own fields are still watched, so a change inside a `$state` list applies too.
|
|
90
|
+
|
|
91
|
+
```svelte
|
|
92
|
+
<Map {layers} onselect={(s) => s?.type === 'feature' && openFeature(s.layer, s.id)} />
|
|
93
|
+
<script>
|
|
94
|
+
const layers = $derived(appLayers.map((l, i) => ({
|
|
95
|
+
id: l.id, label: l.name, visible: l.visible, zIndex: 100 - i,
|
|
96
|
+
style: (p) => ({ markerColor: l.color, stroke: l.color, fill: l.color, opacity: 0.25, label: l.labelProperty && p[l.labelProperty] }),
|
|
97
|
+
...(l.count > 5000
|
|
98
|
+
? { loader: ({ bbox, signal }) => fetchFeatures(l.id, { bbox, signal }).then((r) => ({ geojson: r.collection, truncated: r.hasMore })), onload: (s) => (loadState[l.id] = s) }
|
|
99
|
+
: { geojson: l.features })
|
|
100
|
+
})));
|
|
101
|
+
</script>
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Styles and simplestyle
|
|
105
|
+
|
|
106
|
+
`propertyStyle(properties)` reads [simplestyle-spec 1.1](https://github.com/mapbox/simplestyle-spec): `marker-color`, `marker-size` (`small` 5px, `medium` 7px, `large` 10px radius, or a radius in px), `marker-symbol`, `stroke`, `stroke-width`, `stroke-opacity`, `fill`, `fill-opacity`, and `title` as the label. Points use `marker-color` (then `fill`). The spec's grey defaults are not applied: what a feature leaves out takes the layer style, then the theme.
|
|
107
|
+
|
|
108
|
+
`marker-symbol` is drawn as text inside the marker when it is one or two characters (a letter, a digit, an emoji) or a number up to 999, in black or white for contrast (the `on-primary` token on the default marker); the marker grows to a 9px radius to hold it. Maki icon names (`rail`, `cafe`) are not drawn, because no icon set is bundled: draw those with your own `ol/style` icon in a style of your own.
|
|
109
|
+
|
|
110
|
+
For layers you build yourself, the same builder is exported from `@ruckc/truss-ol/ol-style` (it imports `ol`, so load it from client-only code; the Map loads it lazily):
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
import { createFeatureStyle, createOlStyle, readMapTokens } from '@ruckc/truss-ol/ol-style';
|
|
114
|
+
import { onThemeChange } from '@ruckc/truss-ui';
|
|
115
|
+
|
|
116
|
+
const style = createFeatureStyle({ style: { markerColor: '#d33' }, tokens: () => readMapTokens(el), selected: (f) => f.getId() === sel });
|
|
117
|
+
layer.setStyle(style); // the Map's look: tokens, selection halo, labels, simplestyle, cached per look
|
|
118
|
+
onThemeChange(el, () => { style.reset(); layer.changed(); });
|
|
119
|
+
createOlStyle({ markerColor: '#d33', symbol: 'A', label: 'Depot' }, { geometryType: 'Point', selected: true, tokens }); // Style[]
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Basemaps
|
|
123
|
+
|
|
124
|
+
```svelte
|
|
125
|
+
<script>
|
|
126
|
+
import { Map, esriBasemaps } from '@ruckc/truss-ol';
|
|
127
|
+
let basemapId = $state(localStorage.getItem('basemap') ?? 'esri-streets');
|
|
128
|
+
</script>
|
|
129
|
+
<Map basemaps={[esriBasemaps.streets, esriBasemaps.imagery, esriBasemaps.hybrid, esriBasemaps.topo]} bind:basemapId
|
|
130
|
+
onbasemapchange={(id) => localStorage.setItem('basemap', id)} />
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
- **Stacks:** `{ layers: [{ url, attribution?, maxZoom?, minZoom?, opacity?, themed? }, ...] }` draws its layers bottom to top: imagery first, then label or boundary overlays. Changing `basemap` swaps only the tile layers (identical tiles are kept); the map and the view stay.
|
|
134
|
+
- **Switcher:** with two or more `basemaps`, a layers button in the toolbar opens a menu of them (checkable items, arrow keys, Enter, Escape returns focus). Names come from `label`, else `MapLabels.basemapName(id)` (which names the presets, `osm` and `none`); its accessible name is `MapLabels.basemap`. Hide it with `controls={{ basemap: false }}`. `basemapId` unset or unknown shows the first entry.
|
|
135
|
+
- **Dark mode (`themed`):** in dark mode a themed tile layer is inverted and hue-rotated to match the theme. Only OpenStreetMap tiles (`'osm'`, `{ osm: true }`) are themed by default; tile URLs, stacks and the Esri presets are not, so aerial imagery and hillshades keep their real colours. Set `themed: true` on a basemap, a stack, or one layer of a stack (a layer's own value wins). Unthemed layers carry the `tr-basemap-plain` class.
|
|
136
|
+
- **Esri presets:** `esriBasemaps.imagery` (World_Imagery), `.streets` (World_Street_Map), `.topo` (World_Topo_Map) and `.hybrid` (World_Imagery under Reference/World_Boundaries_and_Places, also exported alone as `esriLabels`), each with the credits Esri's service publishes ("Powered by Esri" and the data sources). They are `basemaps` entries, and each also works as `basemap`. Override `id`, `label` or the layers by spreading: `{ ...esriBasemaps.imagery, id: 'imagery', label: t('Imagery') }`.
|
|
137
|
+
|
|
138
|
+
**Esri's terms.** These tiles are Esri's, under Esri's terms (the Esri Master License Agreement and the ArcGIS Online terms of use linked from each service, e.g. https://goto.arcgisonline.com/maps/World_Imagery), not under this package's MIT licence. They load without an API key, but whether your use is allowed (commercial use, volume, whether you need an ArcGIS account) is for you to check with Esri. Keep the attribution visible, and do not bulk-download or cache the tiles for offline use (Esri publishes separate export services for that).
|
|
71
139
|
|
|
72
140
|
### Your own layers
|
|
73
141
|
|
|
74
|
-
Layers your app adds through `onready(api) → api.map` are left alone: clicks, the pointer cursor, the tooltip and keyboard selection only see the
|
|
142
|
+
Prefer `layers` (below), which the Map creates, styles and hit-tests for you. Layers your app adds through `onready(api) → api.map` are left alone: clicks, the pointer cursor, the tooltip and keyboard selection only see the
|
|
75
143
|
map's own markers, GeoJSON features and points. A vector layer takes part when it opts in with the `trussInteractive: true` property; its `id`
|
|
76
144
|
property names it in `onselect`, in `featurePopup`'s `feature.layer`, and in `select()`. Its features need ids (`feature.setId`).
|
|
77
145
|
|
|
@@ -96,6 +164,32 @@ api.select(null, null); // clear
|
|
|
96
164
|
|
|
97
165
|
`onviewchange` only reports moves, so saving every change never overwrites the saved view with the default one.
|
|
98
166
|
|
|
167
|
+
### Your own popup, drawing, my location and a context menu (#303)
|
|
168
|
+
|
|
169
|
+
```svelte
|
|
170
|
+
<script lang="ts">
|
|
171
|
+
import { Map, type MapApi, type MapDrawMode } from '@ruckc/truss-ol';
|
|
172
|
+
let api: MapApi;
|
|
173
|
+
let mode = $state<MapDrawMode | null>(null);
|
|
174
|
+
</script>
|
|
175
|
+
|
|
176
|
+
{#snippet place()}<PlaceCard feature={hit} />{/snippet}
|
|
177
|
+
|
|
178
|
+
<Map onready={(a) => (api = a)}
|
|
179
|
+
drawMode={mode} editLayer="places" ondrawend={saveNew} onmodifyend={saveEdits} onmoveend={saveEdits}
|
|
180
|
+
controls={{ locate: { follow: true } }} onlocateerror={(e) => console.warn(e.code)}
|
|
181
|
+
oncontextmenu={(e) => menu.open(e.x, e.y)} />
|
|
182
|
+
|
|
183
|
+
<!-- anywhere: api.openPopup(lonLat, { content: place, label: hit.name, onclose }) / api.closePopup() -->
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
- **Popup:** `openPopup(lonLat, { content: Snippet | HTMLElement, autoPan?, closeButton?, label?, onclose? })` and `closePopup()` (on the api and the component) use the selection popup's card and arrow. One popup at a time: it replaces the selection popup until the selection changes. A `role="dialog"`; focus moves in (first control, else the card) and back on close; `Escape`, the × button, a click on the map, a new selection or another `openPopup` close it.
|
|
187
|
+
- **Draw and edit:** `drawMode` `'Point' | 'LineString' | 'Polygon' | 'modify' | 'move' | null` on `editLayer` (the `id` property of a vector layer you added; drawn features go into its source) or the map's own `geojson` (drawn features are only reported). `ondrawend({ feature, olFeature, layer })`, `onmodifyend` / `onmoveend({ features: [{ id, feature, olFeature }], layer })`, with GeoJSON in lon/lat. `snap` (default true), a sketch in the primary colour, clicks do not select while drawing. Keyboard on the focused map: Space adds a vertex at the centre, Enter finishes (or places a point), Backspace undoes, Escape cancels; `finishDrawing()`, `abortDrawing()`, `undoLastPoint()` on the api. The interactions load on first use.
|
|
188
|
+
- **Locate:** `controls={{ locate: true }}` or `{ follow, zoom, enableHighAccuracy, timeout, maximumAge }` adds a "Show my location" button: an accuracy circle and a dot in the primary colour, `onlocate({ lonLat, accuracy, heading, speed, timestamp })` on every update, an `onlocateerror({ code: 'denied' | 'unavailable' | 'timeout' | 'unsupported', message })` plus a `role="alert"` notice. `follow` keeps the view on the position until the reader pans; the next press re-centres, a press while centred stops.
|
|
189
|
+
- **Context menu:** `oncontextmenu({ lonLat, pixel, x, y, feature?, source })` on right-click, touch long-press, and the ContextMenu key or Shift+F10 on the focused map (at the selected feature, else the centre). It only reports where; open truss-ui's `ContextMenu` with `open(x, y)` (#304).
|
|
190
|
+
|
|
191
|
+
All new texts are `MapLabels` keys: `details`, `locate`, `located`, `locateDenied`, `locateUnavailable`, `locateUnsupported`, `drawing`.
|
|
192
|
+
|
|
99
193
|
### Pie markers
|
|
100
194
|
|
|
101
195
|
`featureStyle` may return `{ colors, radius?, stroke?: { color, width } }` for a point. One colour is a filled circle; two to six are equal slices from 12 o'clock, clockwise (more than six are ignored); colours are any CSS colours, so hex strings from your data work. The outline defaults to black, 1px. Icons are drawn once to a canvas and cached by colours, radius and outline. A selected pie keeps its slices and gains a ring. `markers` accept `colors`, `radius` and `stroke` directly.
|
|
@@ -106,9 +200,9 @@ Supported: `geojson` points, `markers`, `points` in `'points'` mode, and the sin
|
|
|
106
200
|
<Map {points} pointsMode="points" idBy="code" featureStyle={(p) => ({ colors: p.status.map((s) => COLORS[s]), radius: 9 })} />
|
|
107
201
|
```
|
|
108
202
|
|
|
109
|
-
###
|
|
203
|
+
### Selection
|
|
110
204
|
|
|
111
|
-
|
|
205
|
+
A click on empty map clears the selection and calls `onselect(null)`; so does removing the `layers` entry that holds the selected feature.
|
|
112
206
|
|
|
113
207
|
### Large point sets
|
|
114
208
|
|