Skip to content

Commit

Permalink
Fix Editor Init. Fixes #85
Browse files Browse the repository at this point in the history
  • Loading branch information
sidharthv96 committed Jan 23, 2021
1 parent 18e3f8b commit 0a27e93
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 43 deletions.
4 changes: 1 addition & 3 deletions src/code-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ export const fromUrl = (data) => {
console.error('Init error', e);
state = defaultState;
}

codeStore.set(state);
codeStore.set({...state, updateEditor: true});
};
export const updateCodeStore = (newState) => {
codeStore.set(newState);
replace('/edit/' + Base64.encodeURI(JSON.stringify(newState)));
};
export const updateCode = (code, updateEditor) => {
const state = get(codeStore);
Expand Down
68 changes: 28 additions & 40 deletions src/components/Editor.svelte
Original file line number Diff line number Diff line change
@@ -1,44 +1,40 @@
<script>
import { codeStore, updateCodeStore } from '../code-store.js';
import { codeStore, updateCode } from '../code-store.js';
import { codeErrorStore } from '../code-error-store.js';
import { onMount } from 'svelte';
import { replace } from 'svelte-spa-router';
import { Base64 } from 'js-base64';
// import mermaid from '@mermaid-js/mermaid';
import mermaid from '@mermaid';
import Error from './Error.svelte';
import { getResizeHandler, initEditor } from './editor-utils';
import * as monaco from 'monaco-editor';
import { watchResize } from 'svelte-watch-resize';
export let code = '';
const isDarkMode =
window.matchMedia('(prefers-color-scheme: dark)').matches && false;
export let conf = { theme: isDarkMode ? 'dark' : 'default' };
let edit;
export let error = false;
let decorations = [];
const decArr = [];
let editorElem = null;
let resizeHandler = () => {};
const handleCodeUpdate = (code) => {
const handleCodeUpdate = (updatedCode, updateEditor) => {
try {
mermaid.parse(code);
let newState = { code, mermaid: conf, updateEditor: false };
updateCodeStore(newState);
mermaid.parse(updatedCode);
if(updateEditor){
edit.setValue(updatedCode);
}
decArr.forEach((decor) => {
edit.deltaDecorations(decor, []);
});
updateCode(updatedCode, false)
codeErrorStore.set(undefined);
const model = edit.getModel();
} catch (e) {
if (e) {
codeErrorStore.set(e);
console.log('Error in parsed', e.hash);
const str = JSON.stringify({ code: code, mermaid: conf });
replace('/edit/' + Base64.encodeURI(str));
const l = e.hash.line;
decArr.push(
edit.deltaDecorations(
Expand All @@ -61,35 +57,29 @@
};
onMount(async () => {
initEditor(monaco);
editorElem = document.getElementById('editor');
edit = monaco.editor.create(editorElem, {
value: [code].join('\n'),
theme: 'myCoolTheme',
language: 'mermaid',
});
resizeHandler = getResizeHandler(edit);
edit.onDidChangeModelContent(function (e) {
handleCodeUpdate(edit.getValue(), false);
});
const unsubscribe = codeStore.subscribe((state) => {
console.log('Code change');
if (editorElem === null) {
editorElem = document.getElementById('editor');
}
if (!code && state) {
code = state.code;
}
console.log(`Code change ${JSON.stringify(state)}`);
if (state) {
conf = state.mermaid;
code = state.code || code;
}
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);
if(state.updateEditor){
handleCodeUpdate(code, true);
}
});
Expand All @@ -100,8 +90,6 @@
error = false;
}
});
initEditor(monaco);
});
</script>

Expand Down

0 comments on commit 0a27e93

Please sign in to comment.