Skip to content

Commit 2d200bc

Browse files
committed
fix: support latest text plugin
1 parent 893e63f commit 2d200bc

File tree

4 files changed

+53
-47
lines changed

4 files changed

+53
-47
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"@angular/router": "~10.1.0",
5757
"@commitlint/cli": "^9.1.2",
5858
"@commitlint/config-conventional": "^9.1.2",
59-
"@nativescript-community/text": "^1.3.2",
59+
"@nativescript-community/text": "^1.3.4",
6060
"@nativescript/angular": "10.1.0",
6161
"@nativescript/core": "7.0.0",
6262
"@nativescript/types-android": "7.0.2",
@@ -83,4 +83,4 @@
8383
"@commitlint/config-conventional"
8484
]
8585
}
86-
}
86+
}

plugin/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@
2828
"homepage": "https://github.com/nativescript-community/ui-label",
2929
"readmeFilename": "README.md",
3030
"dependencies": {
31-
"@nativescript-community/text": "^1.3.2"
31+
"@nativescript-community/text": "^1.3.4"
3232
}
33-
}
33+
}

src/label-common.ts

+20-28
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,46 @@
11
import {
22
CSSType,
3+
ChangedData,
34
Color,
45
CssProperty,
56
FormattedString,
67
InheritedCssProperty,
78
Observable,
9+
ObservableArray,
810
Property,
11+
PropertyChangeData,
912
Span,
1013
Style,
1114
Label as TNLabel,
1215
booleanConverter,
1316
makeParser,
1417
makeValidator,
1518
} from '@nativescript/core';
16-
import { dip } from '@nativescript/core/ui/core/view';
17-
import { TextAlignment } from '@nativescript/core/ui/text-base';
19+
import { View, dip } from '@nativescript/core/ui/core/view';
20+
import { TextAlignment, TextDecoration } from '@nativescript/core/ui/text-base';
1821
import { layout } from '@nativescript/core/utils/utils';
1922
import { Label as LabelViewDefinition, LineBreak, TextShadow } from './label';
20-
import { VerticalTextAlignment, cssProperty } from '@nativescript-community/text';
23+
import { LightFormattedString, VerticalTextAlignment, cssProperty } from '@nativescript-community/text';
24+
import { FontStyle, FontWeight } from '@nativescript/core/ui/styling/font';
2125

2226
declare module '@nativescript/core/ui/text-base/formatted-string' {
2327
interface FormattedString {
2428
addPropertyChangeHandler(span: Span);
2529
removePropertyChangeHandler(span: Span);
2630
}
2731
}
28-
// FormattedString.prototype.onPropertyChange = function(data: PropertyChangeData) {
29-
// this.notifyPropertyChange(data.propertyName, this);
30-
// }
31-
FormattedString.prototype.addPropertyChangeHandler = function (span: Span) {
32-
span.on(Observable.propertyChangeEvent, this.onPropertyChange, this);
33-
// const style = span.style;
34-
// style.on('fontFamilyChange', this.onPropertyChange, this);
35-
// style.on('fontSizeChange', this.onPropertyChange, this);
36-
// style.on('fontStyleChange', this.onPropertyChange, this);
37-
// style.on('fontWeightChange', this.onPropertyChange, this);
38-
// style.on('textDecorationChange', this.onPropertyChange, this);
39-
// style.on('colorChange', this.onPropertyChange, this);
40-
};
41-
FormattedString.prototype.removePropertyChangeHandler = function (span: Span) {
42-
span.off(Observable.propertyChangeEvent, this.onPropertyChange, this);
43-
// const style = span.style;
44-
// style.off('fontFamilyChange', this.onPropertyChange, this);
45-
// style.off('fontSizeChange', this.onPropertyChange, this);
46-
// style.off('fontStyleChange', this.onPropertyChange, this);
47-
// style.off('fontWeightChange', this.onPropertyChange, this);
48-
// style.off('textDecorationChange', this.onPropertyChange, this);
49-
// style.off('colorChange', this.onPropertyChange, this);
50-
};
51-
// FormattedString.prototype.eachChild = function(callback: (child: ViewBase) => boolean): void {
52-
// this.spans.forEach((v, i, arr) => callback(v));
32+
33+
const CHILD_SPAN = 'Span';
34+
const CHILD_FORMATTED_TEXT = 'formattedText';
35+
const CHILD_FORMATTED_STRING = 'FormattedString';
36+
// FormattedString.prototype.addPropertyChangeHandler = function (span: Span) {
37+
// span.on(Observable.propertyChangeEvent, this.onPropertyChange, this);
5338
// };
39+
// FormattedString.prototype.removePropertyChangeHandler = function (span: Span) {
40+
// span.off(Observable.propertyChangeEvent, this.onPropertyChange, this);
41+
// };
42+
43+
5444

5545
export const needFormattedStringComputation = function (
5646
target: any,
@@ -74,6 +64,8 @@ export abstract class LabelBase extends TNLabel implements LabelViewDefinition {
7464
@cssProperty verticalTextAlignment: VerticalTextAlignment;
7565
@cssProperty lineBreak: LineBreak;
7666
html: string;
67+
//@ts-ignore
68+
formattedText: FormattedString;
7769

7870
_canChangeText = true;
7971
_needFormattedStringComputation = false;

src/label.android.ts

+29-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import {
2+
LightFormattedString,
23
VerticalTextAlignment,
34
createNativeAttributedString,
45
cssProperty,
6+
overrideSpanAndFormattedStringEnabled,
57
verticalTextAlignmentProperty,
68
} from '@nativescript-community/text';
79
import {
@@ -128,8 +130,9 @@ abstract class LabelBase extends View implements LabelViewDefinition {
128130

129131
public _isSingleLine: boolean;
130132
public text: string;
131-
public formattedText: FormattedString;
132133
public spannableStringBuilder: globalAndroid.text.SpannableStringBuilder;
134+
//@ts-ignore
135+
formattedText: FormattedString;
133136

134137
get nativeTextViewProtected() {
135138
return this.nativeViewProtected;
@@ -190,28 +193,35 @@ abstract class LabelBase extends View implements LabelViewDefinition {
190193
public _addChildFromBuilder(name: string, value: any): void {
191194
if (name === CHILD_SPAN) {
192195
if (!this.formattedText) {
193-
const formattedText = new FormattedString();
196+
let formattedText: FormattedString;
197+
if (overrideSpanAndFormattedStringEnabled) {
198+
formattedText = new LightFormattedString() as any;
199+
} else {
200+
formattedText = new FormattedString();
201+
}
194202
formattedText.spans.push(value);
195203
this.formattedText = formattedText;
204+
(formattedText as any).parent = this;
196205
} else {
197206
this.formattedText.spans.push(value);
198207
}
199208
} else if (name === CHILD_FORMATTED_TEXT || name === CHILD_FORMATTED_STRING) {
200209
this.formattedText = value;
210+
value.parent = this;
201211
}
202212
}
203213

204214
_requestLayoutOnTextChanged(): void {
205215
this.requestLayout();
206216
}
207217

208-
// without this spans class wont work :s
209-
eachChild(callback: (child: ViewBase) => boolean): void {
210-
const text = this.formattedText;
211-
if (text) {
212-
callback(text);
213-
}
214-
}
218+
// // without this spans class wont work :s
219+
// eachChild(callback: (child: ViewBase) => boolean): void {
220+
// const text = this.formattedText;
221+
// if (text) {
222+
// callback(text);
223+
// }
224+
// }
215225

216226
abstract _setNativeText(reset?: boolean): void;
217227

@@ -446,7 +456,7 @@ export class Label extends LabelBase {
446456
@profile
447457
createSpannableStringBuilder() {
448458
const formattedText = this.formattedText;
449-
const result = createNativeAttributedString(formattedText);
459+
const result = createNativeAttributedString(formattedText as any);
450460
let indexSearch = 0;
451461
let str: string ;
452462
formattedText.spans.forEach(s=>{
@@ -578,16 +588,20 @@ formattedTextProperty.register(Label);
578588
function onFormattedTextPropertyChanged(textBase: Label, oldValue: FormattedString, newValue: FormattedString) {
579589
if (oldValue) {
580590
oldValue.off(Observable.propertyChangeEvent, textBase._onFormattedTextContentsChanged, textBase);
581-
textBase._removeView(oldValue);
591+
if (oldValue instanceof FormattedString){
592+
textBase._removeView(oldValue);
593+
}
582594
}
583595

584596
if (newValue) {
585-
const oldParent = newValue.parent;
586597
// In case formattedString is attached to new TextBase
587-
if (oldParent) {
588-
oldParent._removeView(newValue);
598+
if (newValue instanceof FormattedString){
599+
const oldParent = newValue.parent;
600+
if (oldParent) {
601+
oldParent._removeView(newValue);
602+
}
603+
textBase._addView(newValue);
589604
}
590-
textBase._addView(newValue);
591605
newValue.on(Observable.propertyChangeEvent, textBase._onFormattedTextContentsChanged, textBase);
592606
}
593607
}

0 commit comments

Comments
 (0)