@emuanalytics/flow-rdf-core 4.1.8 → 4.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.
Files changed (27) hide show
  1. package/dist/index.d.mts +322 -4
  2. package/dist/index.d.ts +322 -4
  3. package/dist/index.mjs +6 -0
  4. package/dist/lib/map/flow-core-map.mjs +120 -13
  5. package/dist/lib/map/plugins/index.d.mts +252 -223
  6. package/dist/lib/map/plugins/index.d.ts +252 -223
  7. package/dist/lib/map/plugins/selection/selection.plugin.mjs +73 -7
  8. package/dist/lib/map/plugins/selection/source-selection.mjs +12 -0
  9. package/dist/lib/map-style/composite/builder.mjs +31 -0
  10. package/dist/lib/map-style/composite/editing.mjs +123 -0
  11. package/dist/lib/map-style/composite/expand.mjs +182 -0
  12. package/dist/lib/map-style/composite/stored-format.mjs +18 -0
  13. package/dist/lib/map-style/composite/validate.mjs +376 -0
  14. package/dist/lib/map-style/flow-style/builder.mjs +97 -0
  15. package/dist/lib/map-style/flow-style/data-driven.mjs +164 -0
  16. package/dist/lib/map-style/flow-style/expand.mjs +145 -0
  17. package/dist/lib/map-style/flow-style/literals.mjs +58 -0
  18. package/dist/lib/map-style/flow-style/stored-format.mjs +93 -0
  19. package/dist/lib/map-style/flow-style/validate.mjs +117 -0
  20. package/dist/lib/map-style/flow-style/values.mjs +249 -0
  21. package/dist/lib/map-style/map-data-source.mjs +9 -0
  22. package/dist/lib/map-style/map-layer.mjs +555 -17
  23. package/dist/lib/map-style/map-style.mjs +250 -39
  24. package/dist/package.json.mjs +1 -1
  25. package/dist/shared/{flow-rdf-core.1ee62df9.d.mts → flow-rdf-core.62a36d8b.d.mts} +1077 -234
  26. package/dist/shared/{flow-rdf-core.1ee62df9.d.ts → flow-rdf-core.62a36d8b.d.ts} +1077 -234
  27. package/package.json +1 -1
@@ -1,5 +1,6 @@
1
- import { makeObservable, observable, computed, action } from 'mobx';
1
+ import { makeObservable, observable, computed, action, comparer } from 'mobx';
2
2
  import { isNil } from '../utils/type-checks.mjs';
3
+ import { MapDataSource } from './map-data-source.mjs';
3
4
  import { FlowCoreError } from '../errors/flow-error.mjs';
4
5
 
5
6
  var __defProp = Object.defineProperty;
@@ -9,22 +10,39 @@ var __publicField = (obj, key, value) => {
9
10
  return value;
10
11
  };
11
12
  const _MapStyle = class _MapStyle {
12
- constructor() {
13
+ /**
14
+ * Each map creates its own map style, with hooks of its own.
15
+ *
16
+ * @param hooks - What the map does as the style adds and removes the data sources that layers bring.
17
+ *
18
+ * @internal
19
+ */
20
+ constructor(hooks = {}) {
13
21
  __publicField(this, "_state", null);
14
22
  __publicField(this, "_baseStyle");
23
+ __publicField(this, "_hooks");
24
+ // Sources the app added
15
25
  __publicField(this, "_sources", {});
26
+ // Sources that layers brought, by ID, with the options each was made from
27
+ __publicField(this, "_ownedSources", {});
28
+ __publicField(this, "_ownedSourceOptions", /* @__PURE__ */ new Map());
29
+ // MapLayer ID to the function that stops following the layer's owned sources
30
+ __publicField(this, "_ownedSourceDisposers", /* @__PURE__ */ new Map());
16
31
  __publicField(this, "_layers", {});
17
32
  __publicField(this, "_layerOrder", []);
18
33
  this._baseStyle = _MapStyle._defaultBaseStyle;
34
+ this._hooks = hooks;
19
35
  makeObservable(
20
36
  this,
21
37
  {
22
38
  //observable
23
39
  _sources: observable,
40
+ _ownedSources: observable.shallow,
24
41
  _layers: observable,
25
42
  _layerOrder: observable,
26
43
  _baseStyle: observable.ref,
27
44
  //computed
45
+ renderedStyle: computed,
28
46
  styleSpecification: computed,
29
47
  // actions
30
48
  bindState: action,
@@ -32,48 +50,118 @@ const _MapStyle = class _MapStyle {
32
50
  addSource: action,
33
51
  removeSource: action,
34
52
  addLayer: action,
35
- removeLayer: action
53
+ removeLayer: action,
54
+ syncOwnedSources: action
36
55
  },
37
56
  { name: this.formattedName }
38
57
  );
39
58
  }
40
59
  get styleSpecification() {
60
+ return this.renderedStyle.spec;
61
+ }
62
+ /**
63
+ * Problems found while building {@link styleSpecification}: each names a MapLibre layer that was left out, because
64
+ * another layer or the base style already has its ID, or its source isn't on the map.
65
+ *
66
+ * @internal
67
+ */
68
+ get styleProblems() {
69
+ return this.renderedStyle.problems;
70
+ }
71
+ /**
72
+ * The ID of the {@link MapLayer} that rendered each MapLibre layer in {@link styleSpecification}, by MapLibre layer
73
+ * ID. Base style layers aren't included, unless a MapLayer restyled them.
74
+ *
75
+ * @internal
76
+ */
77
+ get maplibreLayerOwners() {
78
+ return this.renderedStyle.owners;
79
+ }
80
+ /** The style specification, the problems found while building it, and which MapLayer rendered each MapLibre layer. */
81
+ get renderedStyle() {
41
82
  const spec = {
42
83
  ...this._baseStyle,
43
84
  sources: { ...this._baseStyle.sources },
44
85
  layers: [...this._baseStyle.layers]
45
86
  };
46
- Object.values(this._sources).forEach((source) => {
87
+ const problems = [];
88
+ const baseLayerIds = new Set(this._baseStyle.layers.map((l) => l.id));
89
+ const ownIds = new Set(this._layerOrder.filter((id) => this._layers[id].maplibreLayerIds.includes(id)));
90
+ const owners = /* @__PURE__ */ new Map();
91
+ const placed = /* @__PURE__ */ new Map();
92
+ [...Object.values(this._sources), ...Object.values(this._ownedSources)].forEach((source) => {
47
93
  spec.sources[source.id] = source.dataSourceSpecification;
48
94
  });
95
+ problems.push(...this.ownedSourceConflicts());
49
96
  this._layerOrder.forEach((id) => {
50
97
  const layer = this._layers[id];
51
- const layerSpec = layer.layerSpecification;
52
- const baseLayerIndex = spec.layers.findIndex((l) => l.id === id);
53
- if (baseLayerIndex >= 0) {
54
- const baseLayer = spec.layers[baseLayerIndex];
55
- const mergedBaseLayer = {
56
- ...baseLayer,
57
- paint: {
58
- ...baseLayer.paint,
59
- ...layerSpec.paint
60
- },
61
- layout: {
62
- ...baseLayer.layout,
63
- ...layerSpec.layout
98
+ if (!layer.subLayers) {
99
+ owners.set(id, id);
100
+ const layerSpec = layer.layerSpecification;
101
+ const baseLayerIndex = baseLayerIds.has(id) ? spec.layers.findIndex((l) => l.id === id) : -1;
102
+ if (baseLayerIndex >= 0) {
103
+ const baseLayer = spec.layers[baseLayerIndex];
104
+ const mergedBaseLayer = {
105
+ ...baseLayer,
106
+ paint: {
107
+ ...baseLayer.paint,
108
+ ...layerSpec.paint
109
+ },
110
+ layout: {
111
+ ...baseLayer.layout,
112
+ ...layerSpec.layout
113
+ },
114
+ ...layerSpec.metadata && { metadata: { ...baseLayer.metadata, ...layerSpec.metadata } }
115
+ };
116
+ spec.layers.splice(baseLayerIndex, 1, mergedBaseLayer);
117
+ } else {
118
+ const layerIndex = this.layerInsertionPoint(layer, layerSpec.type, spec, placed);
119
+ spec.layers.splice(layerIndex, 0, this.withDefaultSourceLayer(layerSpec, spec));
120
+ }
121
+ placed.set(id, [id]);
122
+ return;
123
+ }
124
+ const layerSpecs = layer.layerSpecifications.filter((layerSpec) => {
125
+ if (baseLayerIds.has(layerSpec.id)) {
126
+ problems.push(this.duplicateIdProblem(layer, layerSpec.id, "the base style"));
127
+ return false;
128
+ } else if (owners.has(layerSpec.id)) {
129
+ problems.push(this.duplicateIdProblem(layer, layerSpec.id, owners.get(layerSpec.id)));
130
+ return false;
131
+ } else if (layerSpec.id !== id && ownIds.has(layerSpec.id)) {
132
+ problems.push(this.duplicateIdProblem(layer, layerSpec.id, layerSpec.id));
133
+ return false;
134
+ } else if (!spec.sources[layerSpec.source]) {
135
+ if (layer.type === "composite") {
136
+ return false;
64
137
  }
65
- };
66
- spec.layers.splice(baseLayerIndex, 1, mergedBaseLayer);
67
- } else {
68
- const source = spec.sources[layer.source];
69
- if (source.type === "vector" && isNil(layerSpec["source-layer"])) {
70
- layerSpec["source-layer"] = "geometry";
138
+ problems.push(
139
+ `${this.formattedName}: layer '${id}' leaves out MapLibre layer '${layerSpec.id}', whose source '${layerSpec.source}' is not on the map`
140
+ );
141
+ return false;
71
142
  }
72
- const layerIndex = this.layerInsertionPoint(layer, spec);
73
- spec.layers.splice(layerIndex, 0, layer.layerSpecification);
143
+ return true;
144
+ });
145
+ if (layer.above || layer.below) {
146
+ const layerIndex = this.layerInsertionPoint(layer, void 0, spec, placed);
147
+ spec.layers.splice(
148
+ layerIndex,
149
+ 0,
150
+ ...layerSpecs.map((layerSpec) => this.withDefaultSourceLayer(layerSpec, spec))
151
+ );
152
+ } else {
153
+ layerSpecs.forEach((layerSpec) => {
154
+ const layerIndex = this.layerInsertionPoint(layer, layerSpec.type, spec, placed);
155
+ spec.layers.splice(layerIndex, 0, this.withDefaultSourceLayer(layerSpec, spec));
156
+ });
74
157
  }
158
+ layerSpecs.forEach((layerSpec) => owners.set(layerSpec.id, id));
159
+ placed.set(
160
+ id,
161
+ layerSpecs.map((layerSpec) => layerSpec.id)
162
+ );
75
163
  });
76
- return spec;
164
+ return { spec, problems, owners };
77
165
  }
78
166
  get formattedName() {
79
167
  return `MapStyle<${this._baseStyle.id}>`;
@@ -81,25 +169,38 @@ const _MapStyle = class _MapStyle {
81
169
  bindState(state) {
82
170
  this._state = state;
83
171
  state.transaction(() => {
84
- Object.values(this._sources).forEach((source) => source.bindState(state));
172
+ this.getSources().forEach((source) => source.bindState(state));
85
173
  Object.values(this._layers).forEach((layer) => layer.bindState(state));
86
174
  });
87
175
  return this;
88
176
  }
89
177
  setBaseStyle(style) {
90
178
  this._baseStyle = style ? Object.freeze({ ...style }) : _MapStyle._defaultBaseStyle;
179
+ this.syncOwnedSources();
91
180
  return this;
92
181
  }
182
+ /**
183
+ * Adds a data source. A source that layers brought with the same ID is removed: the app's takes its place.
184
+ */
93
185
  addSource(source) {
94
186
  this._sources[source.id] = source;
95
187
  source.bindState(this._state);
188
+ this.syncOwnedSources();
96
189
  return this;
97
190
  }
191
+ /**
192
+ * Removes a data source, the app's or one that layers brought.
193
+ *
194
+ * @param id - Data source ID.
195
+ * @param removeLayers - Also remove the layers that display or bring the source.
196
+ * @throws {@link FlowCoreError} if there's no source with the ID, or, unless `removeLayers`, a layer displays or
197
+ * brings it.
198
+ */
98
199
  removeSource(id, removeLayers = false) {
99
- if (!this._sources[id]) {
200
+ if (!this._sources[id] && !this._ownedSources[id]) {
100
201
  throw new FlowCoreError(`${this.formattedName}: no source with id '${id}'`);
101
202
  }
102
- const linkedLayers = Object.values(this._layers).filter((layer) => layer.source === id).map((layer) => layer.id);
203
+ const linkedLayers = Object.values(this._layers).filter((layer) => layer.sourceIds.includes(id) || layer.ownedSources?.some((source) => source.id === id)).map((layer) => layer.id);
103
204
  if (removeLayers) {
104
205
  linkedLayers.forEach((id2) => this.removeLayer(id2));
105
206
  } else if (linkedLayers.length) {
@@ -110,32 +211,69 @@ const _MapStyle = class _MapStyle {
110
211
  delete this._sources[id];
111
212
  return this;
112
213
  }
214
+ /**
215
+ * Whether the source with the ID is one that layers brought (see {@link MapLayer.setOwnedSources}), rather than
216
+ * one the app added.
217
+ *
218
+ * @internal
219
+ */
220
+ isOwnedSource(id) {
221
+ return !!this._ownedSources[id];
222
+ }
113
223
  getSource(id) {
114
- const source = this._sources[id];
224
+ const source = this._sources[id] ?? this._ownedSources[id];
115
225
  if (!source) {
116
226
  throw new FlowCoreError(`${this.formattedName}: no source with id '${id}'`);
117
227
  }
118
228
  return source;
119
229
  }
120
230
  getSources(filter) {
121
- return Object.values(this._sources).filter((source) => filter ? filter(source) : true);
231
+ return [...Object.values(this._sources), ...Object.values(this._ownedSources)].filter(
232
+ (source) => filter ? filter(source) : true
233
+ );
122
234
  }
123
235
  addLayer(layer) {
124
- if (!this._sources[layer.source] && !this._baseStyle.sources[layer.source]) {
125
- throw new FlowCoreError(`${this.formattedName}: no source with id '${layer.source}'`);
236
+ const brought = new Set(layer.ownedSources?.map((source) => source.id));
237
+ const sourceIds = layer.type === "composite" ? [] : layer.sourceIds;
238
+ sourceIds.forEach((sourceId) => {
239
+ if (!this._sources[sourceId] && !this._ownedSources[sourceId] && !this._baseStyle.sources[sourceId] && !brought.has(sourceId)) {
240
+ throw new FlowCoreError(`${this.formattedName}: no source with id '${sourceId}'`);
241
+ }
242
+ });
243
+ if (this._layers[layer.id]) {
244
+ throw new FlowCoreError(`${this.formattedName}: a layer with id '${layer.id}' is already on the map`);
126
245
  }
246
+ const maplibreIds = new Set(layer.maplibreLayerIds);
247
+ Object.values(this._layers).forEach((other) => {
248
+ const shared = other.maplibreLayerIds.find((maplibreId) => maplibreIds.has(maplibreId));
249
+ if (shared) {
250
+ throw new FlowCoreError(
251
+ `${this.formattedName}: layer '${layer.id}' uses MapLibre layer id '${shared}', which layer '${other.id}' already uses`
252
+ );
253
+ }
254
+ });
127
255
  this._layers[layer.id] = layer;
128
256
  this._layerOrder.push(layer.id);
129
257
  layer.bindState(this._state);
258
+ layer.setMapSources((id) => !!(this._sources[id] || this._ownedSources[id] || this._baseStyle.sources[id]));
259
+ this._ownedSourceDisposers.set(
260
+ layer.id,
261
+ layer.observeOwnedSources(() => this.syncOwnedSources())
262
+ );
263
+ this.syncOwnedSources();
130
264
  return this;
131
265
  }
132
266
  removeLayer(id) {
133
267
  if (!this._layers[id]) {
134
268
  throw new FlowCoreError(`${this.formattedName}: no layer with id '${id}'`);
135
269
  }
270
+ this._layers[id].setMapSources(null);
136
271
  delete this._layers[id];
137
272
  const index = this._layerOrder.indexOf(id);
138
273
  this._layerOrder.splice(index, 1);
274
+ this._ownedSourceDisposers.get(id)?.();
275
+ this._ownedSourceDisposers.delete(id);
276
+ this.syncOwnedSources();
139
277
  return this;
140
278
  }
141
279
  getLayer(id) {
@@ -148,16 +286,89 @@ const _MapStyle = class _MapStyle {
148
286
  getLayers(filter) {
149
287
  return this._layerOrder.map((layerId) => this._layers[layerId]).filter((layer) => filter ? filter(layer) : true);
150
288
  }
151
- layerInsertionPoint(layer, spec) {
289
+ /**
290
+ * Adds and removes the sources that layers bring, so that each ID that a layer on the map brings, and that neither
291
+ * the app nor the base style has, is on the map once, with the options of the first layer added that brings it. A
292
+ * source whose options are unchanged keeps its instance, so MapLibre doesn't reload it.
293
+ */
294
+ syncOwnedSources() {
295
+ const wanted = /* @__PURE__ */ new Map();
296
+ this._layerOrder.forEach(
297
+ (layerId) => this._layers[layerId].ownedSources?.forEach((options) => {
298
+ if (!wanted.has(options.id) && !this._sources[options.id] && !this._baseStyle.sources[options.id]) {
299
+ wanted.set(options.id, options);
300
+ }
301
+ })
302
+ );
303
+ Object.keys(this._ownedSources).forEach((id) => {
304
+ const options = wanted.get(id);
305
+ if (!options || !comparer.structural(options, this._ownedSourceOptions.get(id))) {
306
+ this._hooks.event?.("beforeSourceRemoved", id);
307
+ delete this._ownedSources[id];
308
+ this._ownedSourceOptions.delete(id);
309
+ this._hooks.event?.("sourceRemoved", id);
310
+ }
311
+ });
312
+ wanted.forEach((options, id) => {
313
+ if (!this._ownedSources[id]) {
314
+ this._hooks.event?.("beforeSourceAdded", id);
315
+ const source = new MapDataSource(options);
316
+ this._hooks.prepare?.(source);
317
+ source.bindState(this._state);
318
+ this._ownedSources[id] = source;
319
+ this._ownedSourceOptions.set(id, options);
320
+ this._hooks.event?.("sourceAdded", id);
321
+ }
322
+ });
323
+ }
324
+ /** A problem for each layer that brings a source with options other than those of the source on the map. */
325
+ ownedSourceConflicts() {
326
+ const problems = [];
327
+ const first = /* @__PURE__ */ new Map();
328
+ this._layerOrder.forEach(
329
+ (layerId) => this._layers[layerId].ownedSources?.forEach((options) => {
330
+ if (this._sources[options.id] || this._baseStyle.sources[options.id]) {
331
+ return;
332
+ }
333
+ const used = first.get(options.id);
334
+ if (!used) {
335
+ first.set(options.id, { layer: layerId, options });
336
+ } else if (!comparer.structural(used.options, options)) {
337
+ problems.push(
338
+ `${this.formattedName}: layer '${layerId}' brings data source '${options.id}' with options that differ from those of layer '${used.layer}', which are used`
339
+ );
340
+ }
341
+ })
342
+ );
343
+ return problems;
344
+ }
345
+ duplicateIdProblem(layer, maplibreId, owner) {
346
+ const usedBy = owner === "the base style" ? owner : `layer '${owner}'`;
347
+ return `${this.formattedName}: layer '${layer.id}' leaves out MapLibre layer '${maplibreId}', whose id ${usedBy} already uses`;
348
+ }
349
+ /** Defaults the source layer to 'geometry' if missing and the source type is 'vector'. */
350
+ withDefaultSourceLayer(layerSpec, spec) {
351
+ if (spec.sources[layerSpec.source]?.type === "vector" && isNil(layerSpec["source-layer"])) {
352
+ return { ...layerSpec, "source-layer": "geometry" };
353
+ }
354
+ return layerSpec;
355
+ }
356
+ /**
357
+ * Where to insert a MapLibre layer of a type for a layer: above or below the layer's `above` or `below` layer, or
358
+ * else at the base style's insertion point for the type. `above` and `below` name a MapLibre layer or a MapLayer;
359
+ * for a MapLayer with sub-layers, the group's top or bottom MapLibre layer.
360
+ */
361
+ layerInsertionPoint(layer, type, spec, placed) {
152
362
  let insertionIndex;
363
+ const indexes = (id) => (placed.get(id) ?? [id]).map((layerId) => spec.layers.findIndex((l) => l.id === layerId)).filter((i) => i >= 0);
153
364
  if (layer.above) {
154
- insertionIndex = spec.layers.findIndex((l) => l.id === layer.above);
155
- if (insertionIndex >= 0)
156
- insertionIndex++;
365
+ const found = indexes(layer.above);
366
+ insertionIndex = found.length ? Math.max(...found) + 1 : -1;
157
367
  } else if (layer.below) {
158
- insertionIndex = spec.layers.findIndex((l) => l.id === layer.below);
368
+ const found = indexes(layer.below);
369
+ insertionIndex = found.length ? Math.min(...found) : -1;
159
370
  } else {
160
- const below = spec.metadata?.insertionPoints?.[layer.type];
371
+ const below = spec.metadata?.insertionPoints?.[type ?? layer.type];
161
372
  insertionIndex = spec.layers.findIndex((l) => l.id === below);
162
373
  }
163
374
  if (insertionIndex === -1) {
@@ -1,3 +1,3 @@
1
- const version = "4.1.8";
1
+ const version = "4.3.0";
2
2
 
3
3
  export { version };