diff --git a/src/common/script/env.ts b/src/common/script/env.ts index 13950138..ee42f0f7 100644 --- a/src/common/script/env.ts +++ b/src/common/script/env.ts @@ -283,7 +283,7 @@ export class Environment { } return errors; } - getLoadableState() { + commitLoadableState() { // TODO: visit children? for (let [key, value] of Object.entries(this.obj)) { let loadable = value as io.Loadable; diff --git a/src/common/script/lib/scriptui.ts b/src/common/script/lib/scriptui.ts index 9ceda395..4f825cfc 100644 --- a/src/common/script/lib/scriptui.ts +++ b/src/common/script/lib/scriptui.ts @@ -1,5 +1,4 @@ -import { coerceToArray } from "../../util"; import * as io from "./io"; // sequence counter @@ -173,3 +172,22 @@ export function toggle(name: string) { this.enabled = !this.enabled; }); } + +/// + +export class ScriptUIShortcut extends InteractionRecord implements ScriptUIType, Interactive { + readonly uitype = 'shortcut'; + $$interact: InteractionRecord; + + constructor( + readonly key: string, + callback: InteractCallback + ) { + super(null, callback); + this.$$interact = this; + } +} + +export function key(key: string, callback: InteractCallback) { + return new ScriptUIShortcut(key, callback); +} diff --git a/src/common/script/ui/notebook.ts b/src/common/script/ui/notebook.ts index 03e4529b..e9446ab2 100644 --- a/src/common/script/ui/notebook.ts +++ b/src/common/script/ui/notebook.ts @@ -436,10 +436,23 @@ class UIButtonComponent extends Component { } } +class UIShortcutComponent extends Component { + render(virtualDom, containerNode, replaceNode) { + let shortcut = this.props.uiobject as scriptui.ScriptUIShortcut; + // TODO: needs to fire on container node + return h('div', { + onKeyDown: (e: KeyboardEvent) => { + sendInteraction(shortcut, 'key', e, { }); + }, + }, [ ]) + } +} + const UI_COMPONENTS = { 'slider': UISliderComponent, 'select': UISelectComponent, 'button': UIButtonComponent, + 'shortcut': UIShortcutComponent, } /// diff --git a/src/worker/tools/script.ts b/src/worker/tools/script.ts index 5d6f73cc..4d5f533b 100644 --- a/src/worker/tools/script.ts +++ b/src/worker/tools/script.ts @@ -2,6 +2,7 @@ import { BuildStep, BuildStepResult, emglobal, store } from "../workermain"; import { Environment, RunResult } from "../../common/script/env"; import * as io from "../../common/script/lib/io"; +import { createNewPersistentStore } from "../../ide/project"; // cache environments var environments: { [path: string]: Environment } = {}; @@ -18,13 +19,23 @@ function getEnv(path: string): Environment { export async function runJavascript(step: BuildStep): Promise { var env = getEnv(step.path); var code = store.getFileAsString(step.path); + var lstore = createNewPersistentStore(step.platform + "//items"); + // load items from persistent storage (TODO) + const itemskey = step.path; + if (store.items == null) { + store.items = (await lstore.getItem(itemskey)) || {}; // TODO + console.log(store.items); + } + io.$$setupFS(store); + io.$$loadData(store.items); try { - io.$$setupFS(store); - io.$$loadData(store.items); // TODO: load from file await env.run(code); let cells = env.render(); - let state = env.getLoadableState(); // TODO: doesn't work + let state = env.commitLoadableState(); // TODO: doesn't work let output : RunResult = { cells, state }; + // save items to persistent storage (TODO) + lstore.setItem(itemskey, state); // TODO + store.items = state; // TODO: why???? return { output: output }; } catch (e) { console.log(e); diff --git a/src/worker/workermain.ts b/src/worker/workermain.ts index 70c52af3..7fca1166 100644 --- a/src/worker/workermain.ts +++ b/src/worker/workermain.ts @@ -397,7 +397,7 @@ export interface BuildStep extends WorkerBuildStep { export class FileWorkingStore implements WorkingStore { workfs : {[path:string]:FileEntry} = {}; workerseq : number = 0; - items : {} = {}; + items : {}; constructor() { this.reset();