From abb50a86ea070b1e76509bfd74f11207421f221b Mon Sep 17 00:00:00 2001 From: ojack Date: Fri, 21 Jul 2023 11:08:25 +0200 Subject: [PATCH] update url when code is evaluated --- src/stores/editor-store.js | 4 ++-- src/stores/extension-store.js | 6 +++--- src/stores/gallery-store.js | 14 +++++++------- src/stores/store.js | 3 ++- src/views/editor/editor.js | 2 ++ 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/stores/editor-store.js b/src/stores/editor-store.js index 45d87ab..f53e443 100644 --- a/src/stores/editor-store.js +++ b/src/stores/editor-store.js @@ -9,7 +9,7 @@ export default function editorStore(state, emitter) { } else { editor.mutator.mutate({ reroll: false, changeTransform: evt.metaKey }); editor.formatCode() - emitter.emit('gallery: save locally', editor.getValue()) + emitter.emit('gallery: save to URL', editor.getValue()) } }) @@ -31,7 +31,7 @@ export default function editorStore(state, emitter) { // }) emitter.emit('repl: eval', code, (string, err) => { editor.flashCode() - if (!err) emitter.emit('gallery: save locally', code) + if (!err) emitter.emit('gallery: save to URL', code) // sketches.saveLocally(code) }) }) diff --git a/src/stores/extension-store.js b/src/stores/extension-store.js index 477b633..3422d8a 100644 --- a/src/stores/extension-store.js +++ b/src/stores/extension-store.js @@ -93,10 +93,10 @@ export default async function store(state, emitter) { emitter.on('extensions: load example', (extensionIndex, exampleIndex) => { const {categories, selectedCategoryIndex } = state.extensions const path = categories[selectedCategoryIndex].entries[extensionIndex].examples[exampleIndex] - const url = new URL(path); - console.log(url) + const newURL= new URL(path); + console.log(newURL, state.gallery) - state.gallery.setSketchFromURL(url.search, (code) => { + state.gallery.setSketchFromURL(newURL.search, (code) => { emitter.emit('load and eval code', code) }) }) diff --git a/src/stores/gallery-store.js b/src/stores/gallery-store.js index a404649..fde4f50 100644 --- a/src/stores/gallery-store.js +++ b/src/stores/gallery-store.js @@ -5,7 +5,7 @@ export default function galleryStore(state, emitter) { emitter.on('DOMContentLoaded', function () { sketches = new Gallery((code, sketchFromURL) => { - emitter.emit('load and eval code', code) + emitter.emit('load and eval code', code, false) if(sketchFromURL) { emitter.emit('ui: hide info') } else { @@ -20,14 +20,14 @@ export default function galleryStore(state, emitter) { }) // redundant with below - emitter.on('gallery:saveToURL', function () { - let editor = state.editor.editor - const editorText = editor.getValue() - sketches.saveLocally(editorText) - }) + // emitter.on('gallery:saveToURL', function () { + // let editor = state.editor.editor + // const editorText = editor.getValue() + // sketches.saveLocally(editorText) + // }) // save to url - emitter.on('gallery: save locally', function (code) { + emitter.on('gallery: save to URL', function (code) { // let editor = state.editor.editor // const editorText = editor.getValue() sketches.saveLocally(code) diff --git a/src/stores/store.js b/src/stores/store.js index e8c41dd..3a3a754 100644 --- a/src/stores/store.js +++ b/src/stores/store.js @@ -11,9 +11,10 @@ export default function store(state, emitter) { state.serverURL = SERVER_URL !== undefined ? SERVER_URL : null - emitter.on('load and eval code', (code) => { + emitter.on('load and eval code', (code, shouldUpdateURL = true) => { emitter.emit('editor: load code', code) emitter.emit('repl: eval', code) + if(shouldUpdateURL) emitter.emit('gallery: save to URL', code) }) emitter.on('repl: eval', (code = '', callback) => { diff --git a/src/views/editor/editor.js b/src/views/editor/editor.js index df9b054..675842d 100755 --- a/src/views/editor/editor.js +++ b/src/views/editor/editor.js @@ -41,6 +41,8 @@ export default class Editor extends EventEmitter { this.emit(e, this) } else if (e === 'editor:formatCode') { this.formatCode() + } else if (e == 'gallery:saveToURL') { + this.emit('editor: save to URL', this.cm.getValue()) } else { this.emit(e, this) }