From 0f8fe1765c35ff777cdefad0e332374fc55c768e Mon Sep 17 00:00:00 2001 From: ojack Date: Wed, 19 Jul 2023 15:03:31 +0200 Subject: [PATCH] added beautify --- src/stores/editor-store.js | 11 ++++++++--- src/stores/gallery-store.js | 8 +++++++- src/stores/store.js | 23 ++++++++++++++++------- src/views/editor/editor.js | 6 ++++-- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/stores/editor-store.js b/src/stores/editor-store.js index 83e913f..cb4c116 100644 --- a/src/stores/editor-store.js +++ b/src/stores/editor-store.js @@ -6,7 +6,7 @@ export default function editorStore(state, emitter) { } else { editor.mutator.mutate({ reroll: false, changeTransform: evt.metaKey }); editor.formatCode() - sketches.saveLocally(editor.getValue()) + emitter.emit('gallery: save locally', editor.getValue()) } }) @@ -17,9 +17,14 @@ export default function editorStore(state, emitter) { emitter.on('editor: eval all', function () { const editor = state.editor.editor const code = editor.getValue() - repl.eval(code, (string, err) => { + // repl.eval(code, (string, err) => { + // editor.flashCode() + // if (!err) sketches.saveLocally(code) + // }) + emitter.emit('repl: eval', code, (string, err) => { editor.flashCode() - if (!err) sketches.saveLocally(code) + if (!err) emitter.emit('gallery: save locally', code) + // sketches.saveLocally(code) }) }) diff --git a/src/stores/gallery-store.js b/src/stores/gallery-store.js index 3b88461..939b20d 100644 --- a/src/stores/gallery-store.js +++ b/src/stores/gallery-store.js @@ -23,7 +23,13 @@ export default function galleryStore(state, emitter) { let editor = state.editor.editor const editorText = editor.getValue() sketches.saveLocally(editorText) - }) + }) + + emitter.on('gallery: save locally', function (code) { + // let editor = state.editor.editor + // const editorText = editor.getValue() + sketches.saveLocally(code) + }) emitter.on('gallery: clear', () => { sketches.clear() diff --git a/src/stores/store.js b/src/stores/store.js index 9f48dfd..0ff2ed8 100644 --- a/src/stores/store.js +++ b/src/stores/store.js @@ -9,13 +9,12 @@ export default function store(state, emitter) { // if backend gallery endpoint supplied, then enable gallery functionality const SERVER_URL = import.meta.env.VITE_SERVER_URL state.serverURL = SERVER_URL !== undefined ? SERVER_URL : null - let sketches emitter.on('load and eval code', (code) => { const editor = state.editor.editor editor.setValue(code) - repl.eval(code) + emitter.emit('repl: eval', code) }) emitter.on('repl: eval', (code = '', callback) => { @@ -52,8 +51,6 @@ export default function store(state, emitter) { }) - - emitter.on('hideAll', function () { state.showUI = !state.showUI emitter.emit('render') @@ -61,12 +58,24 @@ export default function store(state, emitter) { emitter.on('toggle info', function (count) { if (state.showInfo) { - state.showInfo = false - state.showExtensions = false + // state.showInfo = false + // state.showExtensions = false + emitter.emit('ui: hide info') } else { - state.showInfo = true + emitter.emit('ui: show info') } // state.showInfo = !state.showInfo + //emitter.emit('render') + }) + + emitter.on('ui: show info', () => { + state.showInfo = true + emitter.emit('render') + }) + + emitter.on('ui: hide info', () => { + state.showInfo = false + state.showExtensions = false emitter.emit('render') }) diff --git a/src/views/editor/editor.js b/src/views/editor/editor.js index 8a4fadd..a37ffd4 100755 --- a/src/views/editor/editor.js +++ b/src/views/editor/editor.js @@ -10,7 +10,9 @@ import 'codemirror-minified/addon/comment/comment' import EventEmitter from 'nanobus' import keymaps from './keymaps.js' import Mutator from './randomizer/Mutator.js' -import { beautify_js } from 'js-beautify' +import beautify from 'js-beautify' + +console.log('BEAUTIFY', beautify) var isShowing = true @@ -82,7 +84,7 @@ export default class Editor extends EventEmitter { } formatCode() { - const formatted = beautify_js(this.cm.getValue(), { indent_size: 2, "break_chained_methods": true, "indent_with_tabs": true}) + const formatted = beautify(this.cm.getValue(), { indent_size: 2, "break_chained_methods": true, "indent_with_tabs": true}) this.cm.setValue(formatted) }