@mapbox/mapbox-gl-style-spec 14.0.0-beta.4 → 14.0.0-rc.2

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/diff.js CHANGED
@@ -134,14 +134,24 @@ export const operations: {[_: string]: string} = {
134
134
  setProjection: 'setProjection',
135
135
 
136
136
  /*
137
- * { command: 'addImport', args: [importProperties] }
137
+ * { command: 'addImport', args: [import] }
138
138
  */
139
139
  addImport: 'addImport',
140
140
 
141
141
  /*
142
142
  * { command: 'removeImport', args: [importId] }
143
143
  */
144
- removeImport: 'removeImport'
144
+ removeImport: 'removeImport',
145
+
146
+ /*
147
+ * { command: 'setImportUrl', args: [importId, styleUrl] }
148
+ */
149
+ setImportUrl: 'setImportUrl',
150
+
151
+ /*
152
+ * { command: 'setImportData', args: [importId, stylesheet] }
153
+ */
154
+ setImportData: 'setImportData'
145
155
  };
146
156
 
147
157
  function addSource(sourceId: string, after: Sources, commands: Array<Command>) {
@@ -226,12 +236,12 @@ function diffLayerPropertyChanges(before: any, after: any, commands: Array<Comma
226
236
  }
227
237
  }
228
238
 
229
- function pluckId(layer: LayerSpecification) {
230
- return layer.id;
239
+ function pluckId<T: {id: string}>(item: T): string {
240
+ return item.id;
231
241
  }
232
242
 
233
- function indexById(group: {[string]: LayerSpecification}, layer: LayerSpecification) {
234
- group[layer.id] = layer;
243
+ function indexById<T: {id: string}>(group: {[string]: T}, item: T): {[id: string]: T} {
244
+ group[item.id] = item;
235
245
  return group;
236
246
  }
237
247
 
@@ -345,14 +355,72 @@ function diffLayers(before: Array<LayerSpecification>, after: Array<LayerSpecifi
345
355
  }
346
356
  }
347
357
 
348
- function diffImports(before: Array<ImportSpecification> = [], after: Array<ImportSpecification> = [], commands: Array<Command>) {
349
- // no diff for the imports, must remove then add
350
- for (const beforeImport of before) {
351
- commands.push({command: operations.removeImport, args: [beforeImport.id]});
358
+ export function diffImports(before: Array<ImportSpecification> = [], after: Array<ImportSpecification> = [], commands: Array<Command>) {
359
+ before = before || [];
360
+ after = after || [];
361
+
362
+ // order imports by id
363
+ const beforeOrder = before.map(pluckId);
364
+ const afterOrder = after.map(pluckId);
365
+
366
+ // index imports by id
367
+ const beforeIndex = before.reduce(indexById, {});
368
+ const afterIndex = after.reduce(indexById, {});
369
+
370
+ // track order of imports as if they have been mutated
371
+ const tracker = beforeOrder.slice();
372
+
373
+ let i, d, importId, insertBefore;
374
+
375
+ // remove imports
376
+ for (i = 0, d = 0; i < beforeOrder.length; i++) {
377
+ importId = beforeOrder[i];
378
+ if (!afterIndex.hasOwnProperty(importId)) {
379
+ commands.push({command: operations.removeImport, args: [importId]});
380
+ tracker.splice(tracker.indexOf(importId, d), 1);
381
+ } else {
382
+ // limit where in tracker we need to look for a match
383
+ d++;
384
+ }
385
+ }
386
+
387
+ // add/reorder imports
388
+ for (i = 0, d = 0; i < afterOrder.length; i++) {
389
+ // work backwards as insert is before an existing import
390
+ importId = afterOrder[afterOrder.length - 1 - i];
391
+
392
+ if (tracker[tracker.length - 1 - i] === importId) continue;
393
+
394
+ if (beforeIndex.hasOwnProperty(importId)) {
395
+ // remove the import before we insert at the correct position
396
+ commands.push({command: operations.removeImport, args: [importId]});
397
+ tracker.splice(tracker.lastIndexOf(importId, tracker.length - d), 1);
398
+ } else {
399
+ // limit where in tracker we need to look for a match
400
+ d++;
401
+ }
402
+
403
+ // add import at correct position
404
+ insertBefore = tracker[tracker.length - i];
405
+ commands.push({command: operations.addImport, args: [afterIndex[importId], insertBefore]});
406
+ tracker.splice(tracker.length - i, 0, importId);
352
407
  }
353
408
 
409
+ // update imports
354
410
  for (const afterImport of after) {
355
- commands.push({command: operations.addImport, args: [afterImport]});
411
+ const beforeImport = beforeIndex[afterImport.id];
412
+ if (!beforeImport || isEqual(beforeImport, afterImport)) continue;
413
+
414
+ if (!isEqual(beforeImport.url, afterImport.url)) {
415
+ commands.push({command: operations.setImportUrl, args: [afterImport.id, afterImport.url]});
416
+ }
417
+
418
+ const beforeData = beforeImport && beforeImport.data;
419
+ const afterData = afterImport.data;
420
+
421
+ if (!isEqual(beforeData, afterData)) {
422
+ commands.push({command: operations.setImportData, args: [afterImport.id, afterData]});
423
+ }
356
424
  }
357
425
  }
358
426