Skip to content

Commit

Permalink
various bugs with updating volatile resources list
Browse files Browse the repository at this point in the history
  • Loading branch information
blurymind committed Sep 13, 2024
1 parent 99ad65e commit e37ddce
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 22 deletions.
14 changes: 13 additions & 1 deletion src/public/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ export var Plugins = function (app) {
});
});
}
const getVolatileResourcesList = () => dbStorage.getDbValue(`volatileResources`);
const getVloatileResource = (name = '') => dbStorage.getDbValue(`volatileResources-${name || this.selectedResourcesJson}`);


const isGistTokenInvalid = () => {
return app.data.storage.getIsTokenInvalid();
Expand Down Expand Up @@ -361,8 +364,14 @@ export var Plugins = function (app) {
const entire = func.toString();
const body = func.toString().slice(entire.indexOf("{") + 1, entire.lastIndexOf("}"));
return body;
}
const getVolatileResources = () => { ///todo

}
const getPreviewHtml = (data, otherFiles, yarnData = {}) => {

// todo get volatile resources

// includes: ['some-other-file.js'] - with moduleName (can be used to create an instance) or no moduleName (just dump script body)
const localModules = (data.modules || []).filter(item => !item.includes('/') && item in otherFiles && otherFiles[item].content).map(item => {
try {
Expand Down Expand Up @@ -398,6 +407,7 @@ export var Plugins = function (app) {
<body>
<script id="yarnDataJson">
const yarnData = ${yarnData};
const y = {yarnData: ${yarnData}};
</script>
${data.html || data.body || ''}
${getStoryParserModuleCode(data.parser)}
Expand Down Expand Up @@ -525,6 +535,8 @@ export var Plugins = function (app) {
isGistTokenInvalid,
urlParams,
updateUrlParams,
getVolatileResourcesList,
getVloatileResource,
getGistPluginsFileUrl,
getGistPluginsId,
pluginModeUrl,
Expand All @@ -534,7 +546,7 @@ export var Plugins = function (app) {
getExtensionScriptData,
getPreviewHtml,
getGistFileUrl,
getSelectedResourceUrl
getSelectedResourceUrl,
};

// built in plugin initiation
Expand Down
66 changes: 45 additions & 21 deletions src/public/plugins/resources-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@ export var ResourcesEditor = function({
getGistFileUrl,
getSelectedResourceUrl,
updateUrlParams,

getVolatileResourcesList,
getVloatileResource
}) {
const self = this;
this.name = 'ResourcesEditor';
this.selectedResourcesJson = 'resources.res.json';
this.resourcesFileUrl = '';// todo this should be written in the file itself instead. It doesnt persist between reloads atm
this.resourcesFileContent = '';
const dbStorage = app.data.db;
this.getVloatileResource = (name = '') => dbStorage.getDbValue(`volatileResources-${name || this.selectedResourcesJson}`);
this.setVolatileResource = (value, name = '') => dbStorage.save(`volatileResources-${name || this.selectedResourcesJson}`, value);
this.deleteVolatileResource = (name = '') => dbStorage.save(`volatileResources-${name || this.selectedResourcesJson}`, undefined);
this.getVolatileResourcesList = () => dbStorage.getDbValue(`volatileResources`);
this.setVolatileResourcesList = (key, add = true) => {
return this.getVolatileResourcesList().then(data => {
return getVolatileResourcesList().then(data => {
if(!data) data = new Set([]);
if(add) {
data.add(key)
Expand Down Expand Up @@ -76,10 +75,8 @@ export var ResourcesEditor = function({
this.initResourcesFile = (createVolatile = false) => {
return new Promise((resolve, reject) => {
const getOrCreateVolatile = () => {
return this.getVloatileResource().then(volatile => {
if(volatile && volatile.content) {
console.log({volatile})
this.onUpdateResourcesList();
return getVloatileResource().then(volatile => {
if(volatile && volatile.content) {
resolve(volatile);
return
}
Expand All @@ -95,13 +92,13 @@ export var ResourcesEditor = function({
return this.createOrEditGistFile(resolve, reject);
})
}
this.getVolatileResourcesList().then(volatileResourcesList => {
console.log({volatileResourcesList})
getVolatileResourcesList().then(volatileResourcesList => {
if(!volatileResourcesList) {
this.setVolatileResourcesList(this.selectedResourcesJson).then(()=> {
return getOrCreateVolatile()
})
}
this.onUpdateResourcesList();
return getOrCreateVolatile();
})
});
Expand Down Expand Up @@ -140,19 +137,44 @@ export var ResourcesEditor = function({

this.onGetFromGist = () => {
// todo make this less hideous
this.getVolatileResourcesList().then(volatileResourcesList => {
app.data.storage.getGistFiles().then(({ filesInGist }) => {
getVolatileResourcesList().then(volatileResourcesList => {
this.isBusy('☁️ Requesting new resource files in gist');
app.data.storage.getGistFiles(error => {
ToastWc.show({
type: 'error',
content: error,
time: 3000,
});
this.isBusy('');
}).then(({ filesInGist }) => {
this.isBusy('☁️ Looking for new resource files in gist');

// try to detect json resource map files in the gist
const filesFromGist = [];
const promises = [];
Object.values(filesInGist).forEach(
item => {
if(item.filename.endsWith('.res.json') && !volatileResourcesList.has(item.filename)) {
if(item.filename === this.selectedResourcesJson){
console.log('Skip --', item.filename)
return;
}
if(item.filename.endsWith('.res.json') && (!volatileResourcesList.has(item.filename) || !volatileResourcesList[item.filename])) {
filesFromGist.push(item);
this.isBusy(`☁️ Found ${item.filename}.. Trying to load it..`);
promises.push(app.data.storage.getContentOrRaw(item.content, item.raw_url, console.error));
}
}
);
console.log({filesInGist, promises, filesFromGist})
if(promises.length === 0) {
ToastWc.show({
type: 'info',
content: 'No new resources found.',
time: 3000,
});
this.isBusy('')
return;
}
Promise.all(promises).then((files) => {
files.forEach((content, index) => {
const file = {...filesFromGist[index], content}
Expand All @@ -162,13 +184,14 @@ export var ResourcesEditor = function({
this.setVolatileResource(file, newFileName).then(() => {
if(index === files.length -1) {
this.setVolatileResourcesList(newFileName).then(()=> {
this.onUpdateResourcesList(newFileName);
this.onUpdateResourcesList();
this.onSetEditingFile(newFileName);
ToastWc.show({
type: 'success',
content: `Added ${files.length} files..\n${filesFromGist.map(item=>item.filename).join('\n')}`,
time: 3000,
});
this.isBusy('')
})
}
})
Expand All @@ -178,14 +201,15 @@ export var ResourcesEditor = function({
})
}
this.onUpdateResourcesList = () => {
this.getVolatileResourcesList().then(resourcesList => {
getVolatileResourcesList().then(resourcesList => {
console.log({resourcesList})
document.getElementById("edited-resources").innerHTML = Array.from(resourcesList).map(key => `<option value="${key}">${key}</option>`).join('')
})
}
this.onSetEditingFile = (newFileName = '') => {
const fileName = newFileName || document.getElementById('edited-resources').value;
this.selectedResourcesJson = fileName;
this.getVloatileResource(fileName).then(file => {
getVloatileResource(fileName).then(file => {
console.log('Set', {fileName, file})
document.querySelector('resources-component').updateResourcesList(file.content);
document.getElementById("edited-resources").value = this.selectedResourcesJson;
Expand All @@ -198,15 +222,15 @@ export var ResourcesEditor = function({
if (newFileName) {
newFileName = newFileName.replace(/\s+/g, '').replace(/\//g, '').trim();
newFileName = newFileName.endsWith('.res.json') ? newFileName : `${newFileName}.res.json`;
this.getVolatileResourcesList().then(volatileResources => {
getVolatileResourcesList().then(volatileResources => {
if (volatileResources.has(newFileName)) {
alert(`${newFileName} already exists.\nPlease choose another name..`)
return;
}
const newFileData = { content: this.getNewresourceFileContent(), filename: newFileName }
this.setVolatileResource(newFileData, newFileName).then(() => {
this.setVolatileResourcesList(newFileName).then(()=> {
this.onUpdateResourcesList(newFileName);
this.onUpdateResourcesList();
this.onSetEditingFile(newFileName);
})
})
Expand All @@ -221,7 +245,7 @@ export var ResourcesEditor = function({
}
const willDelete = confirm(`Are you sure you want to delete this resources file:\n${fileName}?`)
if (willDelete) {
this.getVolatileResourcesList().then(oldList =>{
getVolatileResourcesList().then(oldList =>{
const resourcesList = Array.from(oldList);
let nextFileIndex = resourcesList.indexOf(fileName) - 1;
if(nextFileIndex < 0) nextFileIndex = 0;
Expand Down Expand Up @@ -260,7 +284,7 @@ export var ResourcesEditor = function({
<div class="button-group-rounded" id="add-remove-resource-file" style="flex-wrap:nowrap">
<button id="add-resource-file" onclick="${domPath}.onAddNewFile()" title="Add">+</button>
<button id="remove-resource-file" onclick="${domPath}.onRemoveSelectedFile()" title="remove">─</button>
<button id="remove-resource-file" onclick="${domPath}.onGetFromGist()" title="search for new in gist"> ⟳ </button>
<button id="remove-resource-file" onclick="${domPath}.onGetFromGist()" title="find new *.res.json files in gist"> ⟳ </button>
</div>
</span>
</div>
Expand Down Expand Up @@ -313,7 +337,7 @@ export var ResourcesEditor = function({
})
}
if(action === 'push'){
this.getVloatileResource().then(result=> this.onCommitResourceFiles(result.content))
getVloatileResource().then(result=> this.onCommitResourceFiles(result.content))
}
});
this.initResourcesComponent = (file) => {
Expand Down

0 comments on commit e37ddce

Please sign in to comment.