mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Introduce background shorthand property (#5053)
* Introduce background shorthand style property * Add text property in parser for BackgroundPosition value reason: prevent parsing of background position (pass it as sample string) * Introduce background-shorthand test page * Modify parser unit tests to respect the new BackgroundPosition 'text' property
This commit is contained in:
committed by
Svetoslav
parent
ac3c895c89
commit
fc9a0b7ad8
@@ -254,6 +254,13 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
|
||||
this.style.color = value;
|
||||
}
|
||||
|
||||
get background(): string {
|
||||
return this.style.background;
|
||||
}
|
||||
set background(value: string) {
|
||||
this.style.background = value;
|
||||
}
|
||||
|
||||
get backgroundColor(): Color {
|
||||
return this.style.backgroundColor;
|
||||
}
|
||||
|
||||
5
tns-core-modules/ui/core/view/view.d.ts
vendored
5
tns-core-modules/ui/core/view/view.d.ts
vendored
@@ -163,6 +163,11 @@ export abstract class View extends ViewBase {
|
||||
*/
|
||||
color: Color;
|
||||
|
||||
/**
|
||||
* Gets or sets the background style property.
|
||||
*/
|
||||
background: string;
|
||||
|
||||
/**
|
||||
* Gets or sets the background color of the view.
|
||||
*/
|
||||
|
||||
@@ -54,6 +54,7 @@ export function transformConverter(text: string): TransformFunctionsInfo;
|
||||
export const clipPathProperty: CssProperty<Style, string>;
|
||||
export const colorProperty: InheritedCssProperty<Style, Color>;
|
||||
|
||||
export const backgroundProperty: ShorthandProperty<Style, string>;
|
||||
export const backgroundColorProperty: CssAnimationProperty<Style, Color>;
|
||||
export const backgroundImageProperty: CssProperty<Style, string>;
|
||||
export const backgroundRepeatProperty: CssProperty<Style, BackgroundRepeat>;
|
||||
|
||||
@@ -26,6 +26,8 @@ import {
|
||||
multiplyAffine2d,
|
||||
} from "../../matrix";
|
||||
|
||||
import * as parser from "../../css/parser";
|
||||
|
||||
export type LengthDipUnit = { readonly unit: "dip", readonly value: dip };
|
||||
export type LengthPxUnit = { readonly unit: "px", readonly value: px };
|
||||
export type LengthPercentUnit = { readonly unit: "%", readonly value: percent };
|
||||
@@ -544,6 +546,15 @@ function convertTransformValue(property: string, stringValue: string)
|
||||
}
|
||||
|
||||
// Background properties.
|
||||
const backgroundProperty = new ShorthandProperty<Style, string | Color>({
|
||||
name: "background", cssName: "background",
|
||||
getter: function (this: Style) {
|
||||
return `${this.backgroundColor} ${this.backgroundImage} ${this.backgroundRepeat} ${this.backgroundPosition}`;
|
||||
},
|
||||
converter: convertToBackgrounds
|
||||
});
|
||||
backgroundProperty.register(Style);
|
||||
|
||||
export const backgroundInternalProperty = new CssProperty<Style, Background>({
|
||||
name: "backgroundInternal",
|
||||
cssName: "_backgroundInternal",
|
||||
@@ -603,6 +614,30 @@ export const backgroundPositionProperty = new CssProperty<Style, string>({
|
||||
});
|
||||
backgroundPositionProperty.register(Style);
|
||||
|
||||
function convertToBackgrounds(this: void, value: string): [CssProperty<any, any>, any][] {
|
||||
if (typeof value === "string") {
|
||||
const backgrounds = parser.parseBackground(value).value;
|
||||
const backgroundColor = backgrounds.color ? new Color(backgrounds.color) : unsetValue;
|
||||
const backgroundImage = backgrounds.image || unsetValue;
|
||||
const backgroundRepeat = backgrounds.repeat || unsetValue;
|
||||
const backgroundPosition = backgrounds.position ? backgrounds.position.text : unsetValue;
|
||||
|
||||
return [
|
||||
[backgroundColorProperty, backgroundColor],
|
||||
[backgroundImageProperty, backgroundImage],
|
||||
[backgroundRepeatProperty, backgroundRepeat],
|
||||
[backgroundPositionProperty, backgroundPosition]
|
||||
];
|
||||
} else {
|
||||
return [
|
||||
[backgroundColorProperty, unsetValue],
|
||||
[backgroundImageProperty, unsetValue],
|
||||
[backgroundRepeatProperty, unsetValue],
|
||||
[backgroundPositionProperty, unsetValue]
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
function parseBorderColor(value: string): { top: Color, right: Color, bottom: Color, left: Color } {
|
||||
let result: { top: Color, right: Color, bottom: Color, left: Color } = { top: undefined, right: undefined, bottom: undefined, left: undefined };
|
||||
if (value.indexOf("rgb") === 0) {
|
||||
|
||||
2
tns-core-modules/ui/styling/style/style.d.ts
vendored
2
tns-core-modules/ui/styling/style/style.d.ts
vendored
@@ -62,7 +62,7 @@ export class Style extends Observable {
|
||||
public tintColor: Color;
|
||||
public placeholderColor: Color;
|
||||
|
||||
public background: string | Color;
|
||||
public background: string;
|
||||
public backgroundColor: Color;
|
||||
public backgroundImage: string;
|
||||
public backgroundRepeat: BackgroundRepeat;
|
||||
|
||||
@@ -35,7 +35,7 @@ export class Style extends Observable implements StyleDefinition {
|
||||
public tintColor: Color;
|
||||
public placeholderColor: Color;
|
||||
|
||||
public background: string | Color;
|
||||
public background: string;
|
||||
public backgroundColor: Color;
|
||||
public backgroundImage: string;
|
||||
public backgroundRepeat: BackgroundRepeat;
|
||||
|
||||
Reference in New Issue
Block a user