Skip to content

Commit

Permalink
rework plugins init
Browse files Browse the repository at this point in the history
  • Loading branch information
blurymind committed Aug 13, 2024
1 parent b853589 commit 14bb6e8
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 40 deletions.
129 changes: 91 additions & 38 deletions src/public/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,10 @@ export var Plugins = function (app) {
const getVloatilePlugins = () => dbStorage.getDbValue('volatilePlugins');
const setVloatilePlugins = value => dbStorage.save('volatilePlugins', value);
const setVloatilePlugin = (key, value) => {
console.log({key, value: value.content.toString()})
getVloatilePlugins().then(prev => {
prev = prev || {};
console.log({key, prev})
dbStorage.save('volatilePlugins', {
...prev,
[key]: { ...prev[key], ...value },
Expand All @@ -249,14 +251,19 @@ export var Plugins = function (app) {
const isGistTokenInvalid = () => {
return app.data.storage.getIsTokenInvalid();
}
const urlParams = new URLSearchParams(window.location.href.split('?')[1]);
const gistPluginsId = urlParams.get('gistPlugins');

const getGistPluginFiles = () => {
return new Promise((resolve) => {
// if (!app.settings.gistPluginsFile()) reject("No");
const gistId = gistPluginsId || app.settings.gistPluginsFile();
console.log({gistId})
app.data.storage
.getGist(app.settings.gistPluginsFile())
.getGist(gistId)
.then(({ filesInGist }) => {
const promises = Object.values(filesInGist)
.filter(gistFile => gistFile.language === 'JavaScript')
.filter(gistFile => gistFile.language === 'JavaScript' || gistFile.filename.endsWith(".js"))
.map(gistFile => {
return app.data.storage
.getContentOrRaw(gistFile.content, gistFile.raw_url)
Expand All @@ -271,6 +278,23 @@ export var Plugins = function (app) {
console.log({gistId: app.settings.gistPluginsFile(), fileName, contents})
return app.data.storage.editGist(app.settings.gistPluginsFile(), fileName, contents)
}
const getPluginsList = () => {
return new Promise(resolve=> {
return getVloatilePlugins().then(volatilePlugins => {
return getGistPluginFiles().then(gistPlugins => {
console.log({volatilePlugins, gistPlugins})
// if(!gistPlugins && volatilePlugins) resolve(volatilePlugins || {});

const result = {}
gistPlugins.forEach(item=> {
result[item.filename] = item
})
resolve({...result,...volatilePlugins})
})
})
})
}


const pluginApiMethods = {
app,
Expand All @@ -295,7 +319,9 @@ export var Plugins = function (app) {
setVloatilePlugins,
getGistPluginFiles,
saveGistPlugin,
isGistTokenInvalid
isGistTokenInvalid,
urlParams,
getPluginsList
};

// built in plugin initiation
Expand Down Expand Up @@ -334,7 +360,7 @@ export var Plugins = function (app) {

const loadPluginWithDependencies = (content, filename) => {
importModuleWeb(content, filename).then(importedPlugin => {
const newPlugin = importedPlugin(pluginApiMethods);
const newPlugin = new importedPlugin(pluginApiMethods);
console.log({ importedPlugin, newPlugin });
newPlugin.name = newPlugin.name || filename;

Expand All @@ -352,49 +378,76 @@ export var Plugins = function (app) {
});
});
}
registerPlugin(newPlugin);
window.addEventListener('DOMContentLoaded', e => {
registerPlugin(newPlugin);
});
// registerPlugin(newPlugin);
});
};

const volatileGistPlugins = {};
// const volatileGistPlugins = {};
const loadPluginsFromCacheOrGist = () => {
getGistPluginFiles().then(plugins =>
plugins.forEach(gistFile => {
console.log({ gistFile, volatilePlugins });
if (volatilePlugins && !(gistFile.filename in volatilePlugins)) {
loadPluginWithDependencies(gistFile.content, gistFile.filename);

volatileGistPlugins[gistFile.filename] = gistFile;
setVloatilePlugin(
gistFile.filename,
volatileGistPlugins[gistFile.filename]
);
} else {
// do not set volatilePlugin from gist if its already in cache
}
getPluginsList().then(pluginsList=>{
console.log("----->",{pluginsList})

setVloatilePlugins(pluginsList);
Object.values(pluginsList).forEach(pluginFile => {
// volatileGistPlugins[pluginFile.filename] = pluginFile;
// setVloatilePlugin(
// pluginFile.filename,
// pluginFile
// );

loadPluginWithDependencies(pluginFile.content, pluginFile.filename);

importModuleWeb(pluginFile.content, pluginFile.filename).then(
importedPlugin => {
const initializedPlugin = new importedPlugin(pluginApiMethods);
console.log({ importedPlugin, initializedPlugin });
window.addEventListener('DOMContentLoaded', e => {
registerPlugin(initializedPlugin);
});
}
);
})
);
};
})
// getGistPluginFiles().then(plugins =>
// plugins.forEach(gistFile => {
// console.log({ gistFile, volatilePlugins });
// if (volatilePlugins && !(gistFile.filename in volatilePlugins)) {
// loadPluginWithDependencies(gistFile.content, gistFile.filename);

const onLoadPluginsFromVolatile = () => {
Object.values(volatilePlugins).forEach(volatilePlugin => {
if (volatilePlugin.type === 'builtin') return; // todo for now built in ones dont
importModuleWeb(volatilePlugin.content, volatilePlugin.name).then(
importedPlugin => {
const initializedPlugin = new importedPlugin(pluginApiMethods);
console.log({ importedPlugin, initializedPlugin });
window.addEventListener('DOMContentLoaded', e => {
registerPlugin(initializedPlugin);
});
}
);
});
// volatileGistPlugins[gistFile.filename] = gistFile;
// setVloatilePlugin(
// gistFile.filename,
// volatileGistPlugins[gistFile.filename]
// );
// } else {
// // do not set volatilePlugin from gist if its already in cache
// }
// })
// );
};

if (app.settings.gistPluginsFile() !== null) {
// const onLoadPluginsFromVolatile = () => {
// Object.values(volatilePlugins).forEach(volatilePlugin => {
// if (volatilePlugin.type === 'builtin') return; // todo for now built in ones dont
// importModuleWeb(volatilePlugin.content, volatilePlugin.name).then(
// importedPlugin => {
// const initializedPlugin = new importedPlugin(pluginApiMethods);
// console.log({ importedPlugin, initializedPlugin });
// window.addEventListener('DOMContentLoaded', e => {
// registerPlugin(initializedPlugin);
// });
// }
// );
// });
// };

// if (app.settings.gistPluginsFile() !== null) {
loadPluginsFromCacheOrGist(); // writes gist data to volatile cache
}
// }
onLoadBuiltInPlugins();
onLoadPluginsFromVolatile();
// onLoadPluginsFromVolatile();
});
};
21 changes: 19 additions & 2 deletions src/public/plugins/plugin-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ export var PluginEditor = function ({
setVloatilePlugins,
getGistPluginFiles,
saveGistPlugin,
isGistTokenInvalid
isGistTokenInvalid,
urlParams,
getPluginsList
}) {
const self = this;
this.name = 'PluginEditor';
Expand All @@ -103,6 +105,16 @@ export var PluginEditor = function ({
this.mode = 'edit';
this.theme = app.settings.theme() === 'dracula' ? 'ace/theme/monokai' : undefined;

this.onUpdatePluginsList = () => {
// initialize file menu
getPluginsList().then(fileList=>{
this.volatilePlugins = fileList;
console.log({fileList: Object.values(fileList)})
document.getElementById("edited-plugin-file").innerHTML = Object.keys(fileList || {}).map(
key => `<option value="${key}">${key}</option>`
);
})
}
this.onCommitChanges = () => {
const contents = this.differ.getEditors().right.getValue();
saveGistPlugin(this.editingFile, contents).then(data=>{
Expand Down Expand Up @@ -221,7 +233,7 @@ export var PluginEditor = function ({
<div>
<select id="edited-plugin-file" class="settings-value" onchange="app.plugins.${self.name
}.onSetEditingFile()">
${Object.keys(this.volatilePlugins).map(
${Object.keys(this.volatilePlugins || {}).map(
key => `<option value="${key}">${key}</option>`
)}
</select>
Expand Down Expand Up @@ -290,6 +302,8 @@ export var PluginEditor = function ({
} else {
addStyleSheet('public/plugins/ace-diff/ace-diff.min.css');
}


},
onAfterClose: () => {
removeStyleSheet('public/plugins/ace-diff/ace-diff-dark.min.css');
Expand Down Expand Up @@ -351,6 +365,9 @@ export var PluginEditor = function ({

// initialize data on both editor and differ
this.onSetEditingFile();
setTimeout(()=>{
this.onUpdatePluginsList();
}, 300)
},
preConfirm: () => {
setPluginStore(self.name, 'pluginEditorOpen', false);
Expand Down

0 comments on commit 14bb6e8

Please sign in to comment.