diff --git a/src/global.d.ts b/src/global.d.ts index 7cf91818..1e5eeed4 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -6,3 +6,18 @@ interface EditorUpdateEvent { interface EditorEvents { update: EditorUpdateEvent; } + +interface TabEvents { + select: Tab; +} + +interface Tab { + id: string; + title: string; +} + +interface State { + code: string; + mermaid: any; + updateEditor: boolean; +} diff --git a/src/lib/Card/index.svelte b/src/lib/Card/index.svelte index fbfee5cf..449e282b 100644 --- a/src/lib/Card/index.svelte +++ b/src/lib/Card/index.svelte @@ -1,8 +1,8 @@ -
-
+
+
-
- +
+
diff --git a/src/lib/Editor/index.svelte b/src/lib/Editor/index.svelte index af5747ff..76572691 100644 --- a/src/lib/Editor/index.svelte +++ b/src/lib/Editor/index.svelte @@ -12,15 +12,12 @@ export let editorOptions: monaco.editor.IStandaloneEditorConstructionOptions = { value: ['function x() {', '\tconsole.log("Hello world!");', '}'].join('\n'), language: 'javascript' + // automaticLayout: true }; - const dispatch = createEventDispatcher(); - let resizeHandler: any = () => {}; - export const getResizeHandler = (editor) => { - return (node) => editor && editor.layout(); - }; + const dispatch = createEventDispatcher(); + onMount(async () => { - resizeHandler = getResizeHandler(divEl); // @ts-ignore self.MonacoEnvironment = { getWorker: function (_moduleId: any, label: string) { @@ -40,14 +37,14 @@ } }; Monaco = await import('monaco-editor'); + // divEl = document.getElementById('editor') as HTMLDivElement; editor = Monaco.editor.create(divEl, editorOptions); - editor.onDidChangeModelContent(async () => { dispatch('update', { text: editor.getValue() }); }); - editor.layout(); + // editor.layout(); return () => { editor.dispose(); }; diff --git a/src/lib/Tabs/index.svelte b/src/lib/Tabs/index.svelte new file mode 100644 index 00000000..66db7aef --- /dev/null +++ b/src/lib/Tabs/index.svelte @@ -0,0 +1,30 @@ + + +
+ +
diff --git a/src/lib/Util/state.ts b/src/lib/Util/state.ts new file mode 100644 index 00000000..c335e413 --- /dev/null +++ b/src/lib/Util/state.ts @@ -0,0 +1,51 @@ +import { writable } from 'svelte/store'; +import { encode, decode } from 'js-base64'; + +const defaultState: State = { + 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: 'default', + updateEditor: false +}; + +export const codeStore = writable(defaultState); + +export const loadState = (data: string): void => { + let state: State; + try { + const stateStr = decode(data); + console.log('state from url', stateStr); + state = JSON.parse(stateStr); + } catch (e) { + console.error('Init error', e); + state = defaultState; + } + codeStore.set({ ...state, updateEditor: true }); +}; + +export const updateCodeStore = (newState: State): void => { + codeStore.set(newState); +}; + +export const updateCode = (code: string, updateEditor: boolean): void => { + codeStore.update((state) => { + return { ...state, code, updateEditor }; + }); +}; + +export const updateConfig = (config: any, updateEditor: boolean): void => { + codeStore.update((state) => { + return { ...state, mermaid: config, updateEditor }; + }); +}; + +export const initURLSubscription = (): void => { + codeStore.subscribe((state: State) => { + window.location.hash = encode(JSON.stringify(state), true); + }); +}; diff --git a/src/lib/Util/util.ts b/src/lib/Util/util.ts new file mode 100644 index 00000000..6288d9f4 --- /dev/null +++ b/src/lib/Util/util.ts @@ -0,0 +1,6 @@ +import { loadState } from './state'; + +export const loadStateFromURL = (): void => { + debugger; + loadState(window.location.hash); +}; diff --git a/src/routes/__layout.svelte b/src/routes/__layout.svelte index b7cf7021..e688deaa 100644 --- a/src/routes/__layout.svelte +++ b/src/routes/__layout.svelte @@ -1,12 +1,11 @@
-
+
diff --git a/src/routes/edit.svelte b/src/routes/edit.svelte index 001cd5ee..96f18fe3 100644 --- a/src/routes/edit.svelte +++ b/src/routes/edit.svelte @@ -1,11 +1,35 @@ + + @@ -13,13 +37,17 @@
-
- -

Hello

- -
-
-
- -
+ +
+ +
+ + {#if selectedTab == 'code'} + + {:else} + + {/if} +
+ +