From edbffeb2b8716842c19f1ea8bbf282e822e94afa Mon Sep 17 00:00:00 2001 From: lxsang Date: Sat, 6 Jun 2020 18:41:43 +0200 Subject: [PATCH] Enhance core API - Add trim function to core API - Minor bug fix --- src/core/core.ts | 63 +++++++++++++-------------- src/core/tags/TreeViewTag.ts | 1 + src/core/vfs.ts | 5 +-- src/packages/CodePad/BaseExtension.ts | 14 +++--- 4 files changed, 42 insertions(+), 41 deletions(-) diff --git a/src/core/core.ts b/src/core/core.ts index 7c3e2ac..dca3a0e 100644 --- a/src/core/core.ts +++ b/src/core/core.ts @@ -43,6 +43,9 @@ interface String { f(...args: any[]): OS.FormatedString; __(): string; l(): string; + trimLeft(arg: string): string; + trimRight(arg: string): string; + trimBy(arg: string): string; } /** @@ -378,8 +381,7 @@ namespace OS { Object.defineProperty(Object.prototype, "__", { value() { - if(this) - return this.toString(); + if (this) return this.toString(); }, enumerable: false, writable: true, @@ -459,7 +461,20 @@ namespace OS { } return API.lang[this]; }; + String.prototype.trimLeft = function (charlist: string): string { + if (charlist === undefined) charlist = "s"; + return this.replace(new RegExp("^[" + charlist + "]+"), ""); + }; + String.prototype.trimRight = function (charlist: string): string { + if (charlist === undefined) charlist = "s"; + + return this.replace(new RegExp("[" + charlist + "]+$"), ""); + }; + + String.prototype.trimBy = function (charlist: string) { + return this.trimLeft(charlist).trimRight(charlist); + }; Date.prototype.toString = function (): string { let dd = this.getDate(); let mm = this.getMonth() + 1; @@ -490,7 +505,7 @@ namespace OS { return (this.getTime() / 1000) | 0; }; - export const VERSION:Version = "1.0.0-a".__v(); + export const VERSION: Version = "1.0.0-a".__v(); /** * * @@ -500,10 +515,10 @@ namespace OS { * @returns {*} */ export function register(name: string, x: PM.ProcessTypeClass): void { - if ((x as any as typeof BaseModel).type === ModelType.SubWindow) { + if (((x as any) as typeof BaseModel).type === ModelType.SubWindow) { GUI.dialog[name] = x; } else { - (application[name] = x); + application[name] = x; } } @@ -573,9 +588,7 @@ namespace OS { .setting() .then(function (r: any) { cleanup(); - return API.handle - .logout() - .then((d: any) => boot()); + return API.handle.logout().then((d: any) => boot()); }) .catch((e: Error) => console.error(e)); } @@ -625,7 +638,6 @@ namespace OS { export var lang: GenericObject = {}; - /** * * @@ -695,9 +707,7 @@ namespace OS { resolve(this.response); } else { API.loaded(q, p, "FAIL"); - reject( - API.throwe(__("Unable to get blob: {0}", p)) - ); + reject(API.throwe(__("Unable to get blob: {0}", p))); } }; API.loading(q, p); @@ -887,10 +897,7 @@ namespace OS { }).appendTo("head"); API.shared[l] = true; console.log("Loaded :", l); - announcer.trigger( - "sharedlibraryloaded", - l - ); + announcer.trigger("sharedlibraryloaded", l); return resolve(undefined); }) .catch((e: Error) => reject(__e(e))); @@ -899,10 +906,7 @@ namespace OS { .then(function (data: any) { API.shared[l] = true; console.log("Loaded :", l); - announcer.trigger( - "sharedlibraryloaded", - l - ); + announcer.trigger("sharedlibraryloaded", l); return resolve(data); }) .catch((e: Error) => reject(__e(e))); @@ -931,22 +935,18 @@ namespace OS { if (!(libs.length > 0)) { return resolve(); } - announcer.observable.one( - "sharedlibraryloaded", - function (l) { - libs.splice(0, 1); - return API.require(libs) - .catch((e: Error) => reject(__e(e))) - .then((r: any) => resolve(r)); - } - ); + announcer.observable.one("sharedlibraryloaded", function (l) { + libs.splice(0, 1); + return API.require(libs) + .catch((e: Error) => reject(__e(e))) + .then((r: any) => resolve(r)); + }); return API.requires(libs[0]).catch((e: Error) => reject(__e(e)) ); }); } export namespace packages { - /** * * @@ -992,7 +992,6 @@ namespace OS { } } - /** * * @@ -1130,7 +1129,7 @@ namespace OS { .catch((e) => reject(__e(e))); }); } - + /** * * diff --git a/src/core/tags/TreeViewTag.ts b/src/core/tags/TreeViewTag.ts index 8f0c866..f17be2e 100644 --- a/src/core/tags/TreeViewTag.ts +++ b/src/core/tags/TreeViewTag.ts @@ -668,6 +668,7 @@ namespace OS { * @memberof TreeViewTag */ set data(v: TreeViewDataType) { + this._selectedItem = undefined if (!v) { return; } diff --git a/src/core/vfs.ts b/src/core/vfs.ts index ca0493e..77e7e99 100644 --- a/src/core/vfs.ts +++ b/src/core/vfs.ts @@ -1147,10 +1147,7 @@ namespace OS { */ _rd(t: string): Promise { return new Promise((resolve, reject) => { - return resolve({ - result: this.cache, - error: false, - }); + return resolve(this.cache); }); } diff --git a/src/packages/CodePad/BaseExtension.ts b/src/packages/CodePad/BaseExtension.ts index d501873..50503c3 100644 --- a/src/packages/CodePad/BaseExtension.ts +++ b/src/packages/CodePad/BaseExtension.ts @@ -169,7 +169,7 @@ namespace OS { // copy file return file .read("binary") - .then(async (data: BlobPart) => { + .then(async (data: ArrayBuffer) => { const d = await tof .setCache( new Blob([data], { @@ -246,15 +246,19 @@ namespace OS { } else { return file .read("binary") - .then((d: any) => { + .then(async (d: any) => { const zpath = `${base}${file.basename}`.replace( /^\/+|\/+$/g, "" ); zip.file(zpath, d, { binary: true }); - return this.aradd(list, zip, base) - .then(() => resolve(zip)) - .catch((e) => reject(__e(e))); + try { + await this.aradd(list, zip, base); + return resolve(zip); + } + catch (e) { + return reject(__e(e)); + } }) .catch((e: Error) => reject(__e(e))); }