feat: improved css-shadow parser

This commit is contained in:
Igor Randjelovic
2021-02-20 00:20:35 +01:00
committed by Nathan Walker
parent 673387cf99
commit d2f50e50bb
7 changed files with 94 additions and 77 deletions

View File

@@ -3,6 +3,7 @@ import { FormattedString } from './formatted-string';
import { Style } from '../styling/style';
import { Length } from '../styling/style-properties';
import { Property, CssProperty, InheritedCssProperty } from '../core/properties';
import { CSSShadow } from '../styling/css-shadow';
export class TextBase extends View implements AddChildFromBuilder {
/**
@@ -54,7 +55,7 @@ export class TextBase extends View implements AddChildFromBuilder {
/**
* Gets or sets text shadow style property.
*/
textShadow: TextShadow;
textShadow: CSSShadow;
/**
* Gets or sets white space style property.
@@ -125,12 +126,6 @@ export type WhiteSpace = 'initial' | 'normal' | 'nowrap';
export type TextAlignment = 'initial' | 'left' | 'center' | 'right';
export type TextTransform = 'initial' | 'none' | 'capitalize' | 'uppercase' | 'lowercase';
export type TextDecoration = 'none' | 'underline' | 'line-through' | 'underline line-through';
export type TextShadow = {
offsetX: Length;
offsetY: Length;
blurRadius: Length;
color: Color;
};
export const textAlignmentProperty: InheritedCssProperty<Style, TextAlignment>;
export const textDecorationProperty: CssProperty<Style, TextDecoration>;

View File

@@ -1,5 +1,6 @@
// Types
import { TextDecoration, TextAlignment, TextTransform, TextShadow, getClosestPropertyValue } from './text-base-common';
import { TextDecoration, TextAlignment, TextTransform, getClosestPropertyValue } from './text-base-common';
import { CSSShadow } from '../styling/css-shadow';
// Requires
import { Font } from '../styling/font';
@@ -8,7 +9,7 @@ import { Color } from '../../color';
import { FormattedString } from './formatted-string';
import { Span } from './span';
import { colorProperty, fontInternalProperty, Length, VerticalAlignment } from '../styling/style-properties';
import { isString, isDefined, isNullOrUndefined } from '../../utils/types';
import { isString, isNullOrUndefined } from '../../utils/types';
import { iOSNativeHelper } from '../../utils';
import { Trace } from '../../trace';
@@ -152,8 +153,7 @@ export class TextBase extends TextBaseCommon {
if (!(value instanceof Font) || !this.formattedText) {
let nativeView = this.nativeTextViewProtected;
nativeView = nativeView instanceof UIButton ? nativeView.titleLabel : nativeView;
const font = value instanceof Font ? value.getUIFont(nativeView.font) : value;
nativeView.font = font;
nativeView.font = value instanceof Font ? value.getUIFont(nativeView.font) : value;
}
}
@@ -189,7 +189,7 @@ export class TextBase extends TextBaseCommon {
this._setNativeText();
}
[textShadowProperty.setNative](value: TextShadow) {
[textShadowProperty.setNative](value: CSSShadow) {
this._setShadow(value);
}
@@ -348,7 +348,7 @@ export class TextBase extends TextBaseCommon {
}
}
_setShadow(value: TextShadow): void {
_setShadow(value: CSSShadow): void {
const layer = getShadowLayer(this);
if (!layer) {
Trace.write('text-shadow not applied, no layer.', Trace.categories.Style, Trace.messageType.info);
@@ -377,7 +377,7 @@ export class TextBase extends TextBaseCommon {
// layer.shadowOffset = CGSizeMake(Length.toDevicePixels(value.offsetX), Length.toDevicePixels(value.offsetY));
layer.masksToBounds = false;
// NOTE: generally should not need shouldRaterize
// NOTE: generally should not need shouldRasterize
// however for various detailed animation work which involves text-shadow applicable layers, we may want to give users the control of enabling this with text-shadow
// if (!(this.nativeTextViewProtected instanceof UITextView)) {
// layer.shouldRasterize = true;
@@ -504,6 +504,7 @@ export function getTransformedText(text: string, textTransform: TextTransform):
}
}
// todo: clean up nesting & logs
export function getShadowLayer(view: TextBase): CALayer {
let layer: CALayer;
const name = 'shadow-layer';

View File

@@ -9,13 +9,13 @@ import { Span } from './span';
import { View } from '../core/view';
import { Property, CssProperty, InheritedCssProperty, makeValidator, makeParser } from '../core/properties';
import { Style } from '../styling/style';
import { Length, parseShadowProperites } from '../styling/style-properties';
import { Length } from '../styling/style-properties';
import { Observable } from '../../data/observable';
import { TextAlignment, TextDecoration, TextTransform, WhiteSpace } from './text-base-interfaces';
import { TextBase as TextBaseDefinition } from '.';
import { Color } from '../../color';
import { CSSShadow } from '../styling/css-shadow';
import { CSSShadow, parseCSSShadow } from '../styling/css-shadow';
export * from './text-base-interfaces';
@@ -115,10 +115,10 @@ export abstract class TextBaseCommon extends View implements TextBaseDefinition
this.style.textTransform = value;
}
get textShadow(): TextShadow {
get textShadow(): CSSShadow {
return this.style.textShadow;
}
set textShadow(value: TextShadow) {
set textShadow(value: CSSShadow) {
this.style.textShadow = value;
}
@@ -273,7 +273,7 @@ export const textShadowProperty = new CssProperty<Style, string | CSSShadow>({
cssName: 'text-shadow',
affectsLayout: global.isIOS,
valueConverter: (value) => {
return parseShadowProperites(value);
return parseCSSShadow(value);
},
});
textShadowProperty.register(Style);