feat(style-scope): Resolve css sheets from tns_modules

If the css sheet is not in the app directory, try to find it in the
tns_modules folder.
This commit is contained in:
sis0k0
2016-11-07 11:32:10 +02:00
committed by Stanimira Vlaeva
parent 8e2af03e8f
commit 414ebc67dd
3 changed files with 83 additions and 14 deletions

View File

@@ -32,5 +32,6 @@ declare module "ui/styling/style-scope" {
public getAnimations(ruleset: RuleSet): KeyframeAnimationInfo[];
}
export function resolveFileNameFromUrl(url: string, appDirectory: string, fileExists: (string) => boolean): string;
export function applyInlineSyle(view: view.View, style: string): void;
}

View File

@@ -149,13 +149,11 @@ export class StyleScope {
if (utils.isFileOrResourcePath(url)) {
ensureFS();
let fileName = types.isString(url) ? url.trim() : "";
if (fileName.indexOf("~/") === 0) {
fileName = fs.path.join(fs.knownFolders.currentApp().path, fileName.replace("~/", ""));
}
let appDirectory = fs.knownFolders.currentApp().path;
let fileName = resolveFileNameFromUrl(url, appDirectory, fs.File.exists);
if (fs.File.exists(fileName)) {
let file = fs.File.fromPath(fileName);
if (fileName !== null) {
let file: fileSystemModule.File = fs.File.fromPath(fileName);
let text = file.readTextSync();
if (text) {
selectors = selectors.concat(StyleScope.createSelectorsFromCss(text, fileName, keyframes));
@@ -197,7 +195,7 @@ export class StyleScope {
}
public applySelectors(view: view.View): void {
this.ensureSelectors();
this.ensureSelectors();
let state = this._selectors.query(view);
@@ -245,6 +243,26 @@ export class StyleScope {
}
}
export function resolveFileNameFromUrl(url: string, appDirectory: string, fileExists: (string) => boolean): string {
let fileName: string = types.isString(url) ? url.trim() : "";
if (fileName.indexOf("~/") === 0) {
fileName = fileName.replace("~/", "");
}
let local = fs.path.join(appDirectory, fileName);
if (fileExists(local)) {
return local;
}
let external = fs.path.join(appDirectory, "tns_modules", fileName);
if (fileExists(external)) {
return external;
}
return null;
}
export function applyInlineSyle(view: view.View, style: string) {
try {
let syntaxTree = cssParser.parse("local { " + style + " }", undefined);