Skip to content

Commit 8161c8a

Browse files
committed
add ability to use resources in yarn playground
1 parent 47ca7b8 commit 8161c8a

File tree

4 files changed

+53
-12
lines changed

4 files changed

+53
-12
lines changed

src/public/plugins/index.js

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,8 @@ export var Plugins = function (app) {
365365
const body = func.toString().slice(entire.indexOf("{") + 1, entire.lastIndexOf("}"));
366366
return body;
367367
}
368-
const getVolatileResources = () => { ///todo
369-
370-
}
371-
const getPreviewHtml = (data, otherFiles, yarnData = {}) => {
372368

369+
const getPreviewHtmlRender = (data, otherFiles, yarnData = {}, volatileResources) => {
373370
// todo get volatile resources
374371

375372
// includes: ['some-other-file.js'] - with moduleName (can be used to create an instance) or no moduleName (just dump script body)
@@ -406,8 +403,16 @@ export var Plugins = function (app) {
406403
</head>
407404
<body>
408405
<script id="yarnDataJson">
409-
const yarnData = ${yarnData};
410-
const y = {yarnData: ${yarnData}};
406+
const yarnData = ${JSON.stringify(yarnData)};
407+
const y = {
408+
yarnData, resources: ${JSON.stringify(volatileResources)},
409+
getRes: function(resMapKey, resKey) {
410+
if(resMapKey in this.resources && resKey in this.resources[resMapKey] && 'src' in this.resources[resMapKey][resKey]) {
411+
return this.resources[resMapKey][resKey].src;
412+
}
413+
return ''
414+
},
415+
};
411416
</script>
412417
${data.html || data.body || ''}
413418
${getStoryParserModuleCode(data.parser)}
@@ -508,6 +513,36 @@ export var Plugins = function (app) {
508513
</body>
509514
`}
510515

516+
const getVolatileResources = async () => {
517+
return new Promise((resolve,reject)=> {
518+
getVolatileResourcesList().then(resourcesList => {
519+
const result = {}
520+
try{
521+
const resourcesListArray = Array.from(resourcesList)
522+
resourcesListArray.forEach((resourceName, index) => {
523+
getVloatileResource(resourceName).then(data=> {
524+
result[resourceName.split('.')[0]] = JSON.parse(data.content);
525+
if(index === resourcesListArray.length - 1) resolve(result);
526+
})
527+
})
528+
} catch(e) {
529+
console.error(e);
530+
reject(e);
531+
}
532+
})
533+
})
534+
}
535+
536+
const getPreviewHtml = async (data, otherFiles, yarnData = {}) => {
537+
return getVolatileResources().then(volatileResources => {
538+
console.log("GOT resources --- ", {volatileResources})
539+
return getPreviewHtmlRender(data, otherFiles, yarnData = {}, volatileResources);
540+
}).catch(()=> {
541+
return getPreviewHtmlRender(data, otherFiles, yarnData = {}, {});
542+
})
543+
544+
}
545+
511546
const pluginApiMethods = {
512547
app,
513548
createButton,

src/public/plugins/plugin-editor.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,10 @@ export var PluginEditor = function ({
294294
console.log({ outputData: data })
295295
// just in case to prevent getting stuck with bad code
296296
if(!this.hasTestedOnce) return;
297-
document.getElementById('plugin-output-previewer').srcdoc = getPreviewHtml(data, this.volatilePlugins, this.yarnData);
297+
getPreviewHtml(data, this.volatilePlugins, this.yarnData).then(output => {
298+
document.getElementById('plugin-output-previewer').srcdoc = output;
299+
})
300+
// document.getElementById('plugin-output-previewer').srcdoc = getPreviewHtml(data, this.volatilePlugins, this.yarnData);
298301
document.getElementById('plugin-output-previewer').onload = () => {
299302
console.log("LOADED")
300303
onLoad();

src/public/plugins/resources-editor.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export var ResourcesEditor = function({
114114
};
115115

116116
this.onCommitResourceFiles = newContent => {
117-
this.isBusy('Uploading changes to gist...');
117+
this.isBusy(`${this.selectedResourcesJson} Uploading changes to gist...`);
118118
document.querySelector('resources-component').setIsLocked(true);
119119
app.data.storage.editGistFile(this.selectedResourcesJson, newContent).then(({ok, gistId, file}) => {
120120
if(!gistId) {
@@ -130,8 +130,10 @@ export var ResourcesEditor = function({
130130
time: 1000,
131131
});
132132
this.resourcesFileUrl = file.raw_url;
133+
console.log({file})
133134
document.querySelector('resources-component').setIsNew(false);
134-
document.querySelector('resources-component').setFileContent(file);
135+
document.querySelector('resources-component').setFileContent({...file, content: newContent});
136+
this.isBusy('');
135137
} else {
136138
ToastWc.show({
137139
type: 'error',
@@ -309,7 +311,7 @@ export var ResourcesEditor = function({
309311
width: `${window.innerWidth - 20}px`,
310312
onBeforeOpen: () => {},
311313
onAfterClose: () => {
312-
// updateUrlParams('selectedResource', '')
314+
updateUrlParams('selectedResource', '')
313315
},
314316
onOpen: () => {
315317
this.isBusy = (message) => {
@@ -349,7 +351,9 @@ export var ResourcesEditor = function({
349351
})
350352
}
351353
if(action === 'push'){
352-
getVloatileResource().then(result=> this.onCommitResourceFiles(result.content))
354+
getVloatileResource(this.selectedResourcesJson).then(result=> {
355+
this.onCommitResourceFiles(result.content)
356+
})
353357
}
354358
});
355359
this.initResourcesComponent = (file) => {

src/public/web-components.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,6 @@ class ResourcesComponent extends HTMLElement {
533533
this.isPointerDown = true;
534534
shadowRoot.getElementById('resources-editor-select').focus();
535535
if(evt.target.className !== 'select-option') return;
536-
console.log('is control down --> ',this.isControlDown)
537536
if(!this.isControlDown) shadowRoot.getElementById('resources-editor-select').childNodes.forEach(item => item.removeAttribute('data-selected'));
538537
// evt.target.setAttribute('data-selected', true)
539538
toggleItemSelected(evt.target);

0 commit comments

Comments
 (0)