Skip to content

Commit

Permalink
refactor(Style): supp getTextFromProperties() ad it's done with getCo…
Browse files Browse the repository at this point in the history
…ntext()
  • Loading branch information
ftoromanoff committed Dec 13, 2023
1 parent 17bbe88 commit 565dd63
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/Core/Label.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class Label extends THREE.Object3D {

if (typeof content === 'string') {
this.content = document.createElement('div');
this.content.textContent = content;
this.content.textContent = style.text.field;
} else {
this.content = content.cloneNode(true);
}
Expand Down
20 changes: 2 additions & 18 deletions src/Core/Style.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export function readExpression(property, ctx) {
// In this proposal, metadata will be accessed in the callee by the
// `context.properties` property.
return property(ctx.properties, ctx);
} else if (typeof property === 'string' || property instanceof String) {
return property.replace(/\{(.+?)\}/g, (a, b) => (ctx.properties[b] || '')).trim();
} else {
return property;
}
Expand Down Expand Up @@ -718,24 +720,6 @@ class Style {
return new Style(style);
}

/**
* Returns a string, associating `style.text.field` and properties to use to
* replace the keys in `style.text.field`.
*
* @param {FeatureContext} context The context linked to the feature
*
* @return {string|undefined} The formatted string if `style.text.field` is defined, nothing otherwise.
*/
getTextFromProperties(context) {
if (!this.text.field) { return; }

if (this.text.field.expression) {
return readExpression(this.text.field, context);
} else {
return this.text.field.replace(/\{(.+?)\}/g, (a, b) => (context.properties()[b] || '')).trim();
}
}

/**
* set Style from (geojson-like) properties.
* @param {Object} properties (geojson-like) properties.
Expand Down
6 changes: 0 additions & 6 deletions src/Layer/LabelLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,6 @@ class LabelLayer extends GeometryLayer {
&& !(this.style && this.style.icon && (this.style.icon.source || this.style.icon.key))) {
return;
}
} else if (geometryField) {
content = new Style(g.properties.style).getTextFromProperties(context);
} else if (featureField) {
content = new Style(f.style).getTextFromProperties(context);
} else if (layerField) {
content = new Style(this.style).getTextFromProperties(context);
}

const style = Style.applyContext(context);
Expand Down
28 changes: 17 additions & 11 deletions test/unit/label.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,21 @@ describe('Label', function () {
let label;
let style;
const c = new Coordinates('EPSG:4326');
const layerVT = {
type: 'symbol',
paint: {},
layout: {
'icon-image': 'icon',
'icon-size': 1,
'text-field': 'label',
},
};
const sprites = {
img: '',
icon: { x: 0, y: 0, width: 10, height: 10 },
};

before('init style', function () {
const layerVT = {
type: 'symbol',
paint: {},
layout: {
'icon-image': 'icon',
'icon-size': 1,
},
};
style = new Style(Style.setFromVectorTileLayer(layerVT, sprites));
});

Expand All @@ -72,9 +73,14 @@ describe('Label', function () {
assert.throws(() => { label = new Label('content'); });
});

it('should correctly create Labels', function () {
assert.doesNotThrow(() => { label = new Label('', c); });
assert.doesNotThrow(() => { label = new Label(document.createElement('div'), c); });
describe('should correctly create Labels', function () {
it('with label from style', function () {
assert.doesNotThrow(() => { label = new Label('', c, style); });
assert.equal(label.content.textContent, layerVT.layout['text-field']);
});
it('from a DomElement', function () {
assert.doesNotThrow(() => { label = new Label(document.createElement('div'), c); });
});
});

it('should hide the DOM', function () {
Expand Down

0 comments on commit 565dd63

Please sign in to comment.