Skip to content

Commit

Permalink
Fix Init error #77
Browse files Browse the repository at this point in the history
  • Loading branch information
sidharthv96 committed Jan 14, 2021
1 parent fedb119 commit b5d5aeb
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 110 deletions.
41 changes: 17 additions & 24 deletions src/code-store.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
91 changes: 43 additions & 48 deletions src/components/Editor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -62,64 +62,59 @@
}
};
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 = {
getWorkerUrl: function (moduleId, label) {
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);
});
</script>

<style>
#editor-container {
}
#editor {
width: 100%;
height: 400px;
Expand Down
76 changes: 38 additions & 38 deletions src/components/View.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,44 @@
let container;
onMount(async () => {
element = document.querySelector('graph-div');
const unsubscribe = codeStore.subscribe((state) => {
try {
if (container && state) {
code = state.code;
// mermaid.parse(code)
// Replacing special characters '<' and '>' with encoded '&lt;' and '&gt;'
let _code = code;
_code = _code.replace(/</g, '&lt;');
_code = _code.replace(/>/g, '&gt;');
container.innerHTML = _code;
saveStatistcs(detectType(code));
delete container.dataset.processed;
mermaid.initialize(Object.assign({}, state.mermaid));
mermaid.init(undefined, container);
if (code) mermaid.render('graph-div', code, insertSvg);
}
} catch (e) {
console.log('view fail', e);
}
});
const unsubscribeError = codeErrorStore.subscribe((_error) => {
if (typeof _error === 'undefined') {
codeClasses = '';
} else {
codeClasses = 'error';
console.log('code error: ', _error);
}
});
const unsubscribeConfigError = configErrorStore.subscribe((_error) => {
if (typeof _error === 'undefined') {
configClasses = '';
} else {
configClasses = 'error';
console.log('conf error: ', _error);
}
});
});
let insertSvg = function (svgCode, bindFunctions) {
Expand All @@ -66,44 +104,6 @@
export let code = '';
export let configClasses = '';
export let codeClasses = '';
const unsubscribe = codeStore.subscribe((state) => {
try {
if (container && state) {
code = state.code;
// mermaid.parse(code)
// Replacing special characters '<' and '>' with encoded '&lt;' and '&gt;'
let _code = code;
_code = _code.replace(/</g, '&lt;');
_code = _code.replace(/>/g, '&gt;');
container.innerHTML = _code;
saveStatistcs(detectType(code));
delete container.dataset.processed;
mermaid.initialize(Object.assign({}, state.mermaid));
mermaid.init(undefined, container);
if (code) mermaid.render('graph-div', code, insertSvg);
}
} catch (e) {
console.log('view fail', e);
}
});
const unsubscribeError = codeErrorStore.subscribe((_error) => {
if (typeof _error === 'undefined') {
codeClasses = '';
} else {
codeClasses = 'error';
console.log('code error: ', _error);
}
});
const unsubscribeConfigError = configErrorStore.subscribe((_error) => {
if (typeof _error === 'undefined') {
configClasses = '';
} else {
configClasses = 'error';
console.log('conf error: ', _error);
}
});
</script>

<style>
Expand Down

0 comments on commit b5d5aeb

Please sign in to comment.