codepad extension is now stored on user home

This commit is contained in:
lxsang 2020-12-21 18:22:34 +01:00
parent 9fe744fde7
commit f70686d9ca
3 changed files with 92 additions and 60 deletions

Binary file not shown.

View File

@ -14,7 +14,6 @@ namespace OS {
constructor(app: application.CodePad) { constructor(app: application.CodePad) {
super(app); super(app);
} }
// public functions // public functions
/** /**
* *
@ -134,6 +133,10 @@ namespace OS {
}); });
} }
get ext_dir(): string
{
return "home://.codepad";
}
/** /**
* *
* *
@ -408,7 +411,7 @@ namespace OS {
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const file = files.splice(0, 1)[0]; const file = files.splice(0, 1)[0];
const path = `${this.basedir()}/${file}`; const path = `${this.ext_dir}/${file}`;
return zip return zip
.file(file) .file(file)
.async("uint8array") .async("uint8array")
@ -441,8 +444,7 @@ namespace OS {
*/ */
private installMeta(meta: GenericObject<any>): Promise<void> { private installMeta(meta: GenericObject<any>): Promise<void> {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
const file = `${this.app.meta().path const file = `${this.ext_dir}/extensions.json`.asFileHandle();
}/extensions.json`.asFileHandle();
try { try {
const data = await file.read("json"); const data = await file.read("json");
const names = []; const names = [];
@ -461,7 +463,13 @@ namespace OS {
return reject(__e(e)); return reject(__e(e));
} }
} catch (e_1) { } catch (e_1) {
return reject(__e(e_1)); // try to create new file
try {
await file.setCache([meta]).write("object");
return resolve();
} catch (e_2) {
return reject(__e(e_2));
}
} }
}); });
} }
@ -483,8 +491,8 @@ namespace OS {
.then((data) => { .then((data) => {
JSZip.loadAsync(data) JSZip.loadAsync(data)
.then((zip: any) => { .then((zip: any) => {
const pth = this.basedir(); const pth = this.ext_dir;
const dir = []; const dir = [this.ext_dir];
const files = []; const files = [];
for (let name in zip.files) { for (let name in zip.files) {
const file = zip.files[name]; const file = zip.files[name];

View File

@ -428,7 +428,7 @@ namespace OS {
stat.column + 1, stat.column + 1,
stat.line stat.line
); );
if(stat.langmode) if (stat.langmode)
this.langstat.text = stat.langmode.text; this.langstat.text = stat.langmode.text;
this.filestat.text = stat.file this.filestat.text = stat.file
} }
@ -448,8 +448,8 @@ namespace OS {
this.trigger("resize"); this.trigger("resize");
} }
showOutput(toggle:boolean = false): void { showOutput(toggle: boolean = false): void {
if(toggle) if (toggle)
this.showBottomBar(true); this.showBottomBar(true);
this.bottombar.selectedIndex = 0; this.bottombar.selectedIndex = 0;
} }
@ -494,15 +494,13 @@ namespace OS {
this.showBottomBar(!this.setting.showBottomBar); this.showBottomBar(!this.setting.showBottomBar);
} }
private toggleSplitMode():void { private toggleSplitMode(): void {
const right_pannel = this.find("right-panel"); const right_pannel = this.find("right-panel");
const right_editor = this.eum.editors[1]; const right_editor = this.eum.editors[1];
const left_editor = this.eum.editors[0]; const left_editor = this.eum.editors[0];
if(this.split_mode) if (this.split_mode) {
{
// before hide check if there is dirty files // before hide check if there is dirty files
if(right_editor.isDirty()) if (right_editor.isDirty()) {
{
this.notify(__("Unable to disable split view: Please save changes of modified files on the right panel")); this.notify(__("Unable to disable split view: Please save changes of modified files on the right panel"));
return; return;
} }
@ -511,8 +509,7 @@ namespace OS {
this.split_mode = false; this.split_mode = false;
left_editor.focus(); left_editor.focus();
} }
else else {
{
$(right_pannel).show(); $(right_pannel).show();
this.split_mode = true; this.split_mode = true;
right_editor.openFile("Untitled".asFileHandle() as CodePadFileHandle); right_editor.openFile("Untitled".asFileHandle() as CodePadFileHandle);
@ -587,46 +584,71 @@ namespace OS {
this.addAction(CMDMenu.fromMenu(this.fileMenu())); this.addAction(CMDMenu.fromMenu(this.fileMenu()));
} }
/**
* Load extension meta-data from specific file
*
* @private
* @param {string} path
* @return {*} {Promise<void>}
* @memberof CodePad
*/
private loadExtensionMetaFromFile(path: string| API.VFS.BaseFileHandle): Promise<void> {
return new Promise((resolve, reject) => {
path
.asFileHandle()
.read("json")
.then((d: GenericObject<any>[]) => {
for (var ext of Array.from(d)) {
if (this.extensions[ext.name]) {
this.extensions[ext.name].nodes = [];
for (let v of Array.from(ext.actions)) {
this.extensions[ext.name].addAction(v);
}
} else {
this.extensions[ext.name] = new CMDMenu(
ext.text
);
this.extensions[ext.name].name = ext.name;
for (let v of Array.from(ext.actions)) {
this.extensions[ext.name].addAction(v);
}
this.spotlight.addAction(
this.extensions[ext.name]
);
this.extensions[ext.name].onchildselect(
(
e: GUI.TagEventType<
GUI.tag.ListItemEventData
>
) => {
return this.loadAndRunExtensionAction(
e.data.item.data as any
);
}
);
this.extensions[ext.name].rootpath = path.asFileHandle().parent().path;
}
}
resolve();
})
.catch((e) => {
reject(__e(e));
});
});
}
/** /**
* Load the extension meta data from `extension.json` file * Load the extension meta data from `extension.json` file
* *
* @memberof CodePad * @memberof CodePad
*/ */
loadExtensionMetaData(): void { loadExtensionMetaData(): void {
`${this.meta().path}/extensions.json` this.loadExtensionMetaFromFile(`${this.meta().path}/extensions.json`)
.asFileHandle() .then(() => {
.read("json") // try to load local extension
.then((d: GenericObject<any>[]) => { this.loadExtensionMetaFromFile("home://.codepad/extensions.json")
for (var ext of Array.from(d)) { .catch((e)=>{
if (this.extensions[ext.name]) { // ignore any error
this.extensions[ext.name].nodes = []; });
for (let v of Array.from(ext.actions)) {
this.extensions[ext.name].addAction(v);
}
} else {
this.extensions[ext.name] = new CMDMenu(
ext.text
);
this.extensions[ext.name].name = ext.name;
for (let v of Array.from(ext.actions)) {
this.extensions[ext.name].addAction(v);
}
this.spotlight.addAction(
this.extensions[ext.name]
);
this.extensions[ext.name].onchildselect(
(
e: GUI.TagEventType<
GUI.tag.ListItemEventData
>
) => {
return this.loadAndRunExtensionAction(
e.data.item.data as any
);
}
);
}
}
}) })
.catch((e) => { .catch((e) => {
return this.error( return this.error(
@ -684,9 +706,9 @@ namespace OS {
/** /**
* Parent context of the current action * Parent context of the current action
* *
* @type {{ name: any, ext: any }} * @type {{ name: any, ext: any, rootpath?:string }}
*/ */
parent: { name: any, ext: any }; parent: { name: any, ext: any, rootpath?:string };
/** /**
* Action name * Action name
@ -700,7 +722,9 @@ namespace OS {
//verify if the extension is load //verify if the extension is load
if (!CodePad.extensions[name]) { if (!CodePad.extensions[name]) {
//load the extension //load the extension
const path = `${this.meta().path}/${name}.js`; let path = `${this.meta().path}/${name}.js`;
if(data.parent.rootpath)
path = `${data.parent.rootpath}/${name}.js`;
this._api this._api
.requires(path, true) .requires(path, true)
.then(() => this.runExtensionAction(data.parent, action)) .then(() => this.runExtensionAction(data.parent, action))
@ -942,10 +966,8 @@ namespace OS {
const dirties = this.eum.dirties(); const dirties = this.eum.dirties();
if (dirties.length === 0) { if (dirties.length === 0) {
// cleanup all extension // cleanup all extension
for(let k in this.extensions) for (let k in this.extensions) {
{ if (this.extensions[k].ext && this.extensions[k].ext.cleanup) {
if(this.extensions[k].ext && this.extensions[k].ext.cleanup)
{
this.extensions[k].ext.cleanup(); this.extensions[k].ext.cleanup();
} }
} }
@ -1010,7 +1032,7 @@ namespace OS {
case "bottombar": case "bottombar":
return this.toggleBottomBar(); return this.toggleBottomBar();
case "splitview": case "splitview":
return this.toggleSplitMode(); return this.toggleSplitMode();
break; break;
@ -1035,6 +1057,7 @@ namespace OS {
private shortcut: string; private shortcut: string;
nodes: GenericObject<any>[]; nodes: GenericObject<any>[];
parent: CMDMenu; parent: CMDMenu;
rootpath: string;
private select: ( private select: (
e: GUI.TagEventType<GUI.tag.ListItemEventData>, e: GUI.TagEventType<GUI.tag.ListItemEventData>,
r: CodePad r: CodePad
@ -1052,6 +1075,7 @@ namespace OS {
this.shortcut = shortcut; this.shortcut = shortcut;
this.nodes = []; this.nodes = [];
this.parent = undefined; this.parent = undefined;
this.rootpath = undefined;
this.select = function (e) { }; this.select = function (e) { };
} }
@ -1161,7 +1185,7 @@ namespace OS {
this.models = []; this.models = [];
} }
get editors(): CodePadBaseEditorModel[]{ get editors(): CodePadBaseEditorModel[] {
return this.models; return this.models;
} }
set contextmenuHandle(cb: (e: any, m: any) => void) { set contextmenuHandle(cb: (e: any, m: any) => void) {