Skip to content

Commit 6fddf6a

Browse files
committed
add better handling for initial no plugins state
1 parent de9bc61 commit 6fddf6a

File tree

3 files changed

+79
-69
lines changed

3 files changed

+79
-69
lines changed

src/js/classes/storage.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ export const DBStorage = function(
1515
store.put({ key, value });
1616
return tx.complete;
1717
},
18-
load: async function(valueKey) {
18+
load: async function(valueKey, fallBackValue) {
1919
const tx = this.db.transaction(this.objectStoreName, 'readonly');
2020
const store = tx.objectStore(this.objectStoreName);
21+
2122
const data = await store.get(valueKey);
23+
if(!data || data.value == null) return fallBackValue;
2224
return data && data.value;
2325
},
2426
openDatabase: function() {
@@ -28,10 +30,10 @@ export const DBStorage = function(
2830
},
2931
});
3032
},
31-
getDbValue: function(key = this.dbName) {
33+
getDbValue: function(key = this.dbName, fallBackValue) {
3234
return this.openDatabase().then(_db => {
3335
this.db = _db;
34-
return this.load(key);
36+
return this.load(key, fallBackValue);
3537
});
3638
},
3739
};

src/public/plugins/index.js

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,78 @@
11
import { Runner } from './runner';
22
import { PluginEditor } from './plugin-editor';
33
import { ResourcesEditor } from './resources-editor';
4+
export const EXAMPLE = `
5+
() => {
6+
return {
7+
// use the modules option to import modules from addresses or from the plugins gist
8+
modules: [
9+
// if a module is a web address, it can be included this way
10+
"https://unpkg.com/[email protected]/dist/kaplay.js",
11+
// this is how you can include the contents of other files from the list
12+
//'kaplay-dist.js', // <-- copy of kaplay.js can also be stored in the gist and imported like so
13+
// you can host a module on the same gist
14+
],
15+
// if passed, inkjs or bondagejs will be included in the output - can be ink or yarn
16+
parser: 'ink',
17+
// Use the script option to write your code that uses any imported modules
18+
script: () => {
19+
// since we imported kaplayjs in the modules option, we can now use it like this to render to the Test tab
20+
const k = kaplay();
21+
k.loadSprite("bean", "public/icon.png")
22+
k.scene("main", () => {
23+
k.add([
24+
k.pos(200, 100),
25+
k.sprite("bean"),
26+
]);
27+
k.add([
28+
k.text("ohhimark fdfg"),
29+
])
430
31+
});
32+
k.go("main");
33+
// inkjs becomes available because we used parser: 'ink' option
34+
}
35+
}
36+
}
37+
`
38+
export const INTERNAL_EXAMPLE = `
39+
() => {
40+
return {
41+
name: 'ExamplePlugin',
42+
Constructor: function( {
43+
app,
44+
createButton,
45+
createToggle,
46+
getPluginStore,
47+
setPluginStore,
48+
addSettingsItem,
49+
onYarnLoadedData,
50+
onYarnEditorOpen,
51+
onYarnInPreviewMode,
52+
onYarnSavedNode,
53+
onYarnSetLanguage,
54+
onYarnLoadedStateFromLocalStorage,
55+
onYarnSavedStateToLocalStorage,
56+
onYarnSetDocumentType,
57+
onKeyUp,
58+
onKeyDown,
59+
onLoad,
60+
}) {
61+
const self = this;
62+
this.name = 'ExamplePlugin';
63+
this.onOpenPluginEditor = ()=> {
64+
alert("YAY!")
65+
}
66+
createButton(self.name, {
67+
name: 'ExamplePlugin',
68+
attachTo: app.settings.developmentModeEnabled() ? 'appHeader': 'fileMenuDropdown',
69+
onClick: 'onOpenPluginEditor()',
70+
iconName: 'cog',
71+
});
72+
}
73+
}
74+
}
75+
`
576
const PLUGINS = [Runner, PluginEditor, ResourcesEditor];
677

778
const PublicLibs = {}
@@ -230,7 +301,7 @@ export var Plugins = function (app) {
230301
};
231302

232303
const dbStorage = app.data.db;
233-
const getVloatilePlugins = () => dbStorage.getDbValue('volatilePlugins');
304+
const getVloatilePlugins = () => dbStorage.getDbValue('volatilePlugins', { ['example.js']: {content: EXAMPLE}});
234305
const setVloatilePlugins = value => dbStorage.save('volatilePlugins', value);
235306
const setVloatilePlugin = (key, value) => {
236307
return getVloatilePlugins().then(prev => {
@@ -337,7 +408,7 @@ export var Plugins = function (app) {
337408
const getExtensionScriptData = (fileContents) => {
338409
try {
339410
const extension = new Function("parameters", `return ${fileContents}`);//();
340-
//console.log({isFunction: typeof extension === 'function', fileContents})
411+
//console.log({isFunction: typeof extension === 'function', fileContents})
341412
if (typeof extension === 'function') {
342413
const data = extension();
343414
if (data) {

src/public/plugins/plugin-editor.js

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import AceDiff from './ace-diff/ace-diff.min.js';
22
import 'ace-builds/webpack-resolver'; // needed for the webworker to work (syntax highlighting)
3+
import { EXAMPLE, INTERNAL_EXAMPLE } from './';
34

45
const addEsVersionToEditor = editor => {
56
editor.session.$worker.send("setOptions", [{
@@ -21,70 +22,6 @@ const removeStyleSheet = (path, root = document) => {
2122
if (el) root.getElementById(path).remove()
2223
}
2324

24-
const EXAMPLE = `
25-
() => {
26-
return {
27-
modules: [
28-
// if a module is a web address, it can be included this way
29-
"https://unpkg.com/[email protected]/dist/kaplay.js",
30-
// you can host a module on the same gist
31-
// "test-local-module.js",
32-
],
33-
script: () => {
34-
const k = kaplay();
35-
k.loadSprite("bean", "public/icon.png")
36-
k.scene("main", () => {
37-
k.add([
38-
k.pos(200, 100),
39-
k.sprite("bean"),
40-
]);
41-
k.add([
42-
k.text("yarr"),
43-
])
44-
});
45-
k.go("main");
46-
}
47-
}
48-
}
49-
`
50-
const INTERNAL_EXAMPLE = `
51-
() => {
52-
return {
53-
name: 'ExamplePlugin',
54-
Constructor: function( {
55-
app,
56-
createButton,
57-
createToggle,
58-
getPluginStore,
59-
setPluginStore,
60-
addSettingsItem,
61-
onYarnLoadedData,
62-
onYarnEditorOpen,
63-
onYarnInPreviewMode,
64-
onYarnSavedNode,
65-
onYarnSetLanguage,
66-
onYarnLoadedStateFromLocalStorage,
67-
onYarnSavedStateToLocalStorage,
68-
onYarnSetDocumentType,
69-
onKeyUp,
70-
onKeyDown,
71-
onLoad,
72-
}) {
73-
const self = this;
74-
this.name = 'ExamplePlugin';
75-
this.onOpenPluginEditor = ()=> {
76-
alert("YAY!")
77-
}
78-
createButton(self.name, {
79-
name: 'ExamplePlugin',
80-
attachTo: app.settings.developmentModeEnabled() ? 'appHeader': 'fileMenuDropdown',
81-
onClick: 'onOpenPluginEditor()',
82-
iconName: 'cog',
83-
});
84-
}
85-
}
86-
}
87-
`
8825

8926
const getExampleOutputFunction = (error = "") => `
9027
<head>

0 commit comments

Comments
 (0)