Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 565dd63

Browse files
committed
refactor(Style): supp getTextFromProperties() ad it's done with getContext()
1 parent 17bbe88 commit 565dd63

File tree

4 files changed

+20
-36
lines changed

4 files changed

+20
-36
lines changed

src/Core/Label.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class Label extends THREE.Object3D {
8383

8484
if (typeof content === 'string') {
8585
this.content = document.createElement('div');
86-
this.content.textContent = content;
86+
this.content.textContent = style.text.field;
8787
} else {
8888
this.content = content.cloneNode(true);
8989
}

src/Core/Style.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ export function readExpression(property, ctx) {
4949
// In this proposal, metadata will be accessed in the callee by the
5050
// `context.properties` property.
5151
return property(ctx.properties, ctx);
52+
} else if (typeof property === 'string' || property instanceof String) {
53+
return property.replace(/\{(.+?)\}/g, (a, b) => (ctx.properties[b] || '')).trim();
5254
} else {
5355
return property;
5456
}
@@ -718,24 +720,6 @@ class Style {
718720
return new Style(style);
719721
}
720722

721-
/**
722-
* Returns a string, associating `style.text.field` and properties to use to
723-
* replace the keys in `style.text.field`.
724-
*
725-
* @param {FeatureContext} context The context linked to the feature
726-
*
727-
* @return {string|undefined} The formatted string if `style.text.field` is defined, nothing otherwise.
728-
*/
729-
getTextFromProperties(context) {
730-
if (!this.text.field) { return; }
731-
732-
if (this.text.field.expression) {
733-
return readExpression(this.text.field, context);
734-
} else {
735-
return this.text.field.replace(/\{(.+?)\}/g, (a, b) => (context.properties()[b] || '')).trim();
736-
}
737-
}
738-
739723
/**
740724
* set Style from (geojson-like) properties.
741725
* @param {Object} properties (geojson-like) properties.

src/Layer/LabelLayer.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,12 +288,6 @@ class LabelLayer extends GeometryLayer {
288288
&& !(this.style && this.style.icon && (this.style.icon.source || this.style.icon.key))) {
289289
return;
290290
}
291-
} else if (geometryField) {
292-
content = new Style(g.properties.style).getTextFromProperties(context);
293-
} else if (featureField) {
294-
content = new Style(f.style).getTextFromProperties(context);
295-
} else if (layerField) {
296-
content = new Style(this.style).getTextFromProperties(context);
297291
}
298292

299293
const style = Style.applyContext(context);

test/unit/label.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,21 @@ describe('Label', function () {
5050
let label;
5151
let style;
5252
const c = new Coordinates('EPSG:4326');
53+
const layerVT = {
54+
type: 'symbol',
55+
paint: {},
56+
layout: {
57+
'icon-image': 'icon',
58+
'icon-size': 1,
59+
'text-field': 'label',
60+
},
61+
};
5362
const sprites = {
5463
img: '',
5564
icon: { x: 0, y: 0, width: 10, height: 10 },
5665
};
5766

5867
before('init style', function () {
59-
const layerVT = {
60-
type: 'symbol',
61-
paint: {},
62-
layout: {
63-
'icon-image': 'icon',
64-
'icon-size': 1,
65-
},
66-
};
6768
style = new Style(Style.setFromVectorTileLayer(layerVT, sprites));
6869
});
6970

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

75-
it('should correctly create Labels', function () {
76-
assert.doesNotThrow(() => { label = new Label('', c); });
77-
assert.doesNotThrow(() => { label = new Label(document.createElement('div'), c); });
76+
describe('should correctly create Labels', function () {
77+
it('with label from style', function () {
78+
assert.doesNotThrow(() => { label = new Label('', c, style); });
79+
assert.equal(label.content.textContent, layerVT.layout['text-field']);
80+
});
81+
it('from a DomElement', function () {
82+
assert.doesNotThrow(() => { label = new Label(document.createElement('div'), c); });
83+
});
7884
});
7985

8086
it('should hide the DOM', function () {

0 commit comments

Comments
 (0)