Skip to content

Commit

Permalink
feat(Source): add propertie 'warn' for avoiding multiple console.warn
Browse files Browse the repository at this point in the history
  • Loading branch information
ftoromanoff committed Jan 6, 2025
1 parent f581056 commit c41e18a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
27 changes: 22 additions & 5 deletions src/Core/Style.js
Original file line number Diff line number Diff line change
Expand Up @@ -761,10 +761,11 @@ class Style {
* @param {Object} layer vector tile layer.
* @param {Object} sprites vector tile layer.
* @param {Boolean} [symbolToCircle=false]
* @param {Set} warn Set storing all warnings encountered by the source.
*
* @returns {StyleOptions} containing all properties for itowns.Style
*/
static setFromVectorTileLayer(layer, sprites, symbolToCircle = false) {
static setFromVectorTileLayer(layer, sprites, symbolToCircle = false, warn) {
const style = {
fill: {},
stroke: {},
Expand Down Expand Up @@ -875,13 +876,21 @@ class Style {
cropValues = function _(p) {
const id = stop[1].replace(/\{(.+?)\}/g, (a, b) => (p[b] || '')).trim();
if (cropValues === undefined) {
// const warning = `WARNING: "${id}" not found in sprite file`;
const warning = `WARNING: "${id}" not found in sprite file`;
if (!warn.has(warning)) {
warn.add(warning);
console.warn(warning);
}
sprites[id] = cropValueDefault;// or return cropValueDefault;
}
return sprites[id];
};
} else if (cropValues === undefined) {
// const warning = `WARNING: "${stop[1]}" not found in sprite file`;
const warning = `WARNING: "${stop[1]}" not found in sprite file`;
if (!warn.has(warning)) {
warn.add(warning);
console.warn(warning);
}
cropValues = cropValueDefault;
}
return [stop[0], cropValues];
Expand All @@ -894,13 +903,21 @@ class Style {
style.icon.cropValues = function _(p) {
const id = iconImg.replace(/\{(.+?)\}/g, (a, b) => (p[b] || '')).trim();
if (sprites[id] === undefined) {
// const warning = `WARNING: "${id}" not found in sprite file`;
const warning = `WARNING: "${id}" not found in sprite file`;
if (!warn.has(warning)) {
warn.add(warning);
console.warn(warning);
}
sprites[id] = cropValueDefault;// or return cropValueDefault;
}
return sprites[id];
};
} else if (sprites[iconImg] === undefined) {
// const warning = `WARNING: "${iconImg}" not found in sprite file`;
const warning = `WARNING: "${iconImg}" not found in sprite file`;
if (!warn.has(warning)) {
warn.add(warning);
console.warn(warning);
}
style.icon.cropValues = cropValueDefault;
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/Layer/LabelLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,16 @@ class LabelLayer extends GeometryLayer {
}
}

// TODO: add support for LINE and POLYGON
if (f.type !== FEATURE_TYPES.POINT) {
const warn = `Type label ${Object.keys(FEATURE_TYPES).filter(ft => FEATURE_TYPES[ft] === f.type)} not well supported`;
if (!this.source.warn.has(warn)) {
this.source.warn.add(warn);
console.warn(warn);
}
return;
}

context.setFeature(f);

const featureField = f.style?.text?.field;
Expand Down
1 change: 1 addition & 0 deletions src/Source/Source.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class Source extends InformationsData {
constructor(source) {
super(source);
this.isSource = true;
this.warn = new Set();

if (!source.url) {
throw new Error('New Source: url is required');
Expand Down
2 changes: 1 addition & 1 deletion src/Source/VectorTilesSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class VectorTilesSource extends TMSSource {
if (layer['source-layer'] === undefined) {
getPropertiesFromRefLayer(mvtStyle.layers, layer);
}
const style = Style.setFromVectorTileLayer(layer, this.sprites, this.symbolToCircle);
const style = Style.setFromVectorTileLayer(layer, this.sprites, this.symbolToCircle, this.warn);
this.styles[layer.id] = style;

if (!this.layers[layer['source-layer']]) {
Expand Down

0 comments on commit c41e18a

Please sign in to comment.