Skip to content

Commit 8de324c

Browse files
committed
fix: add meta for TextBase and FormattedString
Fixes #65
1 parent 23a4a89 commit 8de324c

File tree

4 files changed

+56
-17
lines changed

4 files changed

+56
-17
lines changed

packages/angular/src/lib/element-registry/common-views.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AbsoluteLayout, ActivityIndicator, Button, ContentView, DatePicker, DockLayout, FlexboxLayout, FormattedString, Frame, GridLayout, HtmlView, Image, Label, ListPicker, ListView, Page, Placeholder, Progress, ProxyViewContainer, Repeater, RootLayout, ScrollView, SearchBar, SegmentedBar, SegmentedBarItem, Slider, Span, StackLayout, Switch, TabView, TextField, TextView, TimePicker, WebView, WrapLayout } from '@nativescript/core';
2-
import { frameMeta } from './metas';
2+
import { formattedStringMeta, frameMeta, textBaseMeta } from './metas';
33
import { registerElement } from './registry';
44

55
// Register default NativeScript components
@@ -9,7 +9,7 @@ export function registerNativeScriptViewComponents() {
99
(<any>global).__ngRegisteredViews = true;
1010
registerElement('AbsoluteLayout', () => AbsoluteLayout);
1111
registerElement('ActivityIndicator', () => ActivityIndicator);
12-
registerElement('Button', () => Button);
12+
registerElement('Button', () => Button, textBaseMeta);
1313
registerElement('ContentView', () => ContentView);
1414
registerElement('DatePicker', () => DatePicker);
1515
registerElement('DockLayout', () => DockLayout);
@@ -19,7 +19,7 @@ export function registerNativeScriptViewComponents() {
1919
registerElement('Image', () => Image);
2020
// Parse5 changes <Image> tags to <img>. WTF!
2121
registerElement('img', () => Image);
22-
registerElement('Label', () => Label);
22+
registerElement('Label', () => Label, textBaseMeta);
2323
registerElement('ListPicker', () => ListPicker);
2424
registerElement('ListView', () => ListView);
2525
registerElement('Page', () => Page);
@@ -37,12 +37,12 @@ export function registerNativeScriptViewComponents() {
3737
registerElement('FlexboxLayout', () => FlexboxLayout);
3838
registerElement('Switch', () => Switch);
3939
registerElement('TabView', () => TabView);
40-
registerElement('TextField', () => TextField);
41-
registerElement('TextView', () => TextView);
40+
registerElement('TextField', () => TextField, textBaseMeta);
41+
registerElement('TextView', () => TextView, textBaseMeta);
4242
registerElement('TimePicker', () => TimePicker);
4343
registerElement('WebView', () => WebView);
4444
registerElement('WrapLayout', () => WrapLayout);
45-
registerElement('FormattedString', () => FormattedString);
45+
registerElement('FormattedString', () => FormattedString, formattedStringMeta);
4646
registerElement('Span', () => Span);
4747
}
4848
}

packages/angular/src/lib/element-registry/metas.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Frame, Page } from '@nativescript/core';
2-
import { NgView, ViewClassMeta } from '../views/view-types';
1+
import { FormattedString, Frame, Page, Span, TextBase } from '@nativescript/core';
32
import { isInvisibleNode } from '../views/utils';
3+
import { NgView, ViewClassMeta } from '../views/view-types';
44

55
export const frameMeta: ViewClassMeta = {
66
insertChild: (parent: Frame, child: NgView) => {
@@ -14,3 +14,41 @@ export const frameMeta: ViewClassMeta = {
1414
}
1515
},
1616
};
17+
18+
export const formattedStringMeta: ViewClassMeta = {
19+
insertChild(parent: FormattedString, child: Span, next: Span) {
20+
const index = parent.spans.indexOf(next);
21+
if (index > -1) {
22+
parent.spans.splice(index, 0, child);
23+
} else {
24+
parent.spans.push(child);
25+
}
26+
},
27+
removeChild(parent: FormattedString, child: Span) {
28+
const index = parent.spans.indexOf(child);
29+
if (index > -1) {
30+
parent.spans.splice(index, 1);
31+
}
32+
},
33+
};
34+
35+
export const textBaseMeta: ViewClassMeta = {
36+
insertChild(parent: TextBase, child, next) {
37+
if (child instanceof FormattedString) {
38+
parent.formattedText = child;
39+
} else if (child instanceof Span) {
40+
parent.formattedText ??= new FormattedString();
41+
formattedStringMeta.insertChild(parent.formattedText, child, next);
42+
}
43+
},
44+
removeChild(parent: TextBase, child: NgView) {
45+
if (!parent.formattedText) return;
46+
if (child instanceof FormattedString) {
47+
if (parent.formattedText === child) {
48+
parent.formattedText = null;
49+
}
50+
} else if (child instanceof Span) {
51+
formattedStringMeta.removeChild(parent.formattedText, child);
52+
}
53+
},
54+
};

packages/angular/src/lib/view-util.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { View, unsetValue, Placeholder, ContentView, LayoutBase, ProxyViewContainer } from '@nativescript/core';
1+
import { unsetValue, View } from '@nativescript/core';
22
import { getViewClass, getViewMeta, isKnownView } from './element-registry';
3-
import { CommentNode, NgView, TextNode, ViewExtensions, isDetachedElement, isInvisibleNode, isView, isContentView, isLayout } from './views';
43
import { NamespaceFilter } from './property-filter';
4+
import { CommentNode, isContentView, isDetachedElement, isInvisibleNode, isLayout, isView, NgView, TextNode } from './views';
55

66
import { NativeScriptDebug } from './trace';
77
import { NgLayoutBase } from './views/view-types';
@@ -61,6 +61,7 @@ function printSiblingsTree(view: NgView) {
6161
console.log(`${view} previousSiblings: ${previousSiblings} nextSiblings: ${nextSiblings}`);
6262
}
6363

64+
// eslint-disable-next-line @typescript-eslint/ban-types
6465
const propertyMaps: Map<Function, Map<string, string>> = new Map<Function, Map<string, string>>();
6566

6667
export class ViewUtil {
@@ -371,10 +372,10 @@ export class ViewUtil {
371372
}
372373

373374
private ensureNgViewExtensions(view: View): NgView {
374-
if (view.hasOwnProperty('meta')) {
375+
if (Object.hasOwnProperty.call(view, 'meta')) {
375376
return view as NgView;
376377
} else {
377-
const name = view.cssType;
378+
const name = view.cssType || view.typeName;
378379
const ngView = this.setNgViewExtensions(view, name);
379380

380381
return ngView;
@@ -501,8 +502,8 @@ export class ViewUtil {
501502
}
502503

503504
if (!propertyMaps.has(type)) {
504-
let propMap = new Map<string, string>();
505-
for (let propName in instance) {
505+
const propMap = new Map<string, string>();
506+
for (const propName in instance) {
506507
// tslint:disable:forin
507508
propMap.set(propName.toLowerCase(), propName);
508509
}
@@ -532,14 +533,14 @@ export class ViewUtil {
532533
}
533534

534535
private setClasses(view: NgView, classesValue: string): void {
535-
let classes = classesValue.split(whiteSpaceSplitter);
536+
const classes = classesValue.split(whiteSpaceSplitter);
536537
this.cssClasses(view).clear();
537538
classes.forEach((className) => this.cssClasses(view).set(className, true));
538539
this.syncClasses(view);
539540
}
540541

541542
private syncClasses(view: NgView): void {
542-
let classValue = (<any>Array).from(this.cssClasses(view).keys()).join(' ');
543+
const classValue = (<any>Array).from(this.cssClasses(view).keys()).join(' ');
543544
view.className = classValue;
544545
}
545546

packages/angular/src/lib/views/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ContentView, LayoutBase, ProxyViewContainer, View } from '@nativescript/core';
1+
import { ContentView, LayoutBase, ProxyViewContainer, View, ViewBase } from '@nativescript/core';
22
import { InvisibleNode } from './invisible-nodes';
33
import type { NgContentView, NgLayoutBase, NgView } from './view-types';
44

0 commit comments

Comments
 (0)