From b5d5aeb122168deb3de4f3effcc2f1250ae3b267 Mon Sep 17 00:00:00 2001 From: Sidharth Vinod Date: Thu, 14 Jan 2021 20:18:58 +0530 Subject: [PATCH] Fix Init error #77 --- src/code-store.js | 41 +++++++--------- src/components/Editor.svelte | 91 +++++++++++++++++------------------- src/components/View.svelte | 76 +++++++++++++++--------------- 3 files changed, 98 insertions(+), 110 deletions(-) diff --git a/src/code-store.js b/src/code-store.js index d31cb18fc9..972d7825ef 100644 --- a/src/code-store.js +++ b/src/code-store.js @@ -1,35 +1,28 @@ import { writable, get } from 'svelte/store'; -// import mermaid from '@mermaid-js/mermaid'; -import mermaid from '@mermaid'; import { Base64 } from 'js-base64'; -import { push, pop, replace } from 'svelte-spa-router'; - -export const codeStore = writable({}); +import { replace } from 'svelte-spa-router'; +const isDarkMode = + window.matchMedia('(prefers-color-scheme: dark)').matches && false; +const defaultState = { + code: `graph TD +A[Christmas] -->|Get money| B(Go shopping) +B --> C{Let me think} +C -->|One| D[Laptop] +C -->|Two| E[iPhone] +C -->|Three| F[fa:fa-car Car] + `, + mermaid: { theme: isDarkMode ? 'dark' : 'default' }, +}; +export const codeStore = writable(defaultState); export const fromUrl = (data) => { - let code; let state; - const isDarkMode = - window.matchMedia('(prefers-color-scheme: dark)').matches && false; try { - let stateStr = Base64.decode(data); + const stateStr = Base64.decode(data); console.log('state from url', stateStr); state = JSON.parse(stateStr); - - if (state.code === undefined) { - // not valid json - // state = { code: '', mermaid: { theme: themeFromUrl } } - } - code = state.code; } catch (e) { - // console.error('Init error', e); - code = `graph TD - A[Christmas] -->|Get money| B(Go shopping) - B --> C{Let me think} - C -->|One| D[Laptop] - C -->|Two| E[iPhone] - C -->|Three| F[fa:fa-car Car] - `; - state = { code, mermaid: { theme: isDarkMode ? 'dark' : 'default' } }; + console.error('Init error', e); + state = defaultState; } codeStore.set(state); diff --git a/src/components/Editor.svelte b/src/components/Editor.svelte index d7a9c70e97..2e58acfd1e 100644 --- a/src/components/Editor.svelte +++ b/src/components/Editor.svelte @@ -2,7 +2,7 @@ import { codeStore, updateCodeStore } from '../code-store.js'; import { codeErrorStore } from '../code-error-store.js'; import { onMount } from 'svelte'; - import { push, pop, replace } from 'svelte-spa-router'; + import { replace } from 'svelte-spa-router'; import { Base64 } from 'js-base64'; // import mermaid from '@mermaid-js/mermaid'; import mermaid from '@mermaid'; @@ -62,48 +62,6 @@ } }; - const unsubscribe = codeStore.subscribe((state) => { - console.log('Code change'); - if (editorElem === null) { - editorElem = document.getElementById('editor'); - } - if (!code && state) { - code = state.code; - } - if (state) { - conf = state.mermaid; - } - if (!edit && code && editorElem !== null) { - edit = monaco.editor.create(editorElem, { - value: [code].join('\n'), - theme: 'myCoolTheme', - language: 'mermaid', - }); - resizeHandler = getResizeHandler(edit); - - let decorations = []; - edit.onDidChangeModelContent(function (e) { - const code = edit.getValue(); - handleCodeUpdate(code); - }); - handleCodeUpdate(code); - } - if (state && state.updateEditor && edit && code && editorElem !== null) { - edit.setValue(state.code); - handleCodeUpdate(state.code); - } - }); - - const unsubscribeError = codeErrorStore.subscribe((_error) => { - if (_error) { - error = true; - } else { - error = false; - } - }); - - initEditor(monaco); - onMount(async () => { // editorElem = document.querySelector('#editor') self.MonacoEnvironment = { @@ -111,15 +69,52 @@ return './editor.worker.bundle.js'; }, }; - }); - // export let name; - // export let params = {}; + const unsubscribe = codeStore.subscribe((state) => { + console.log('Code change'); + if (editorElem === null) { + editorElem = document.getElementById('editor'); + } + if (!code && state) { + code = state.code; + } + if (state) { + conf = state.mermaid; + } + if (!edit && code && editorElem !== null) { + edit = monaco.editor.create(editorElem, { + value: [code].join('\n'), + theme: 'myCoolTheme', + language: 'mermaid', + }); + resizeHandler = getResizeHandler(edit); + + let decorations = []; + edit.onDidChangeModelContent(function (e) { + const code = edit.getValue(); + handleCodeUpdate(code); + }); + handleCodeUpdate(code); + } + if (state && state.updateEditor && edit && code && editorElem !== null) { + edit.setValue(state.code); + handleCodeUpdate(state.code); + } + }); + + const unsubscribeError = codeErrorStore.subscribe((_error) => { + if (_error) { + error = true; + } else { + error = false; + } + }); + + initEditor(monaco); + });