Skip to content

Commit

Permalink
Merge pull request #864 from mermaid-js/fix/dataFlow
Browse files Browse the repository at this point in the history
Fix/data flow
  • Loading branch information
sidharthv96 committed Jun 30, 2022
2 parents ddc1291 + 33e2773 commit 2cff54b
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 106 deletions.
14 changes: 14 additions & 0 deletions cypress/e2e/diagramUpdate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ describe('Auto sync tests', () => {
cy.clearLocalStorage();
cy.visit('/');
});

it('should dim diagram when code is edited', () => {
cy.contains('Auto sync').click();
cy.get('#view').should('not.have.class', 'outOfSync');
Expand Down Expand Up @@ -40,4 +41,17 @@ describe('Auto sync tests', () => {
cy.get('#editor').type(`{uparrow}{${cmd}}/`);
cy.get('#view').contains('Car').should('exist');
});

it('supports editing code when code is incorrect', () => {
cy.visit(
'/edit#pako:eNpljjEKwzAMRa8SNOcEnlt6gK5eVFvYJsgOqkwpIXevg9smEE1PnyfxF3DFExgISW-CczQ2D21cYU7a-SGYXRwyvTp9jUhuKlVP-eHy7zA-leQsMEmg_QOM0BLG5FujZVMsaCQmC6ahR5ks2Lw2r84ela4-aREwKpVGwKrl_s7ut3fnkjAIcg_XDzuaUhs'
);
cy.get('#editor').type(`{enter}branch test`);
cy.get('#editor').contains('branch test').should('exist');
cy.get('#errorContainer')
.contains(
'Error: Trying to checkout branch which is not yet created. (Help try using "branch master")'
)
.should('exist');
});
});
18 changes: 9 additions & 9 deletions src/lib/components/actions.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import Card from '$lib/components/card/card.svelte';
import { krokiRendererUrl, rendererUrl } from '$lib/util/env';
import { pakoSerde } from '$lib/util/serde';
import { serializedState, codeStore } from '$lib/util/state';
import { stateStore } from '$lib/util/state';
import { toBase64 } from 'js-base64';
import moment from 'moment';
Expand Down Expand Up @@ -131,10 +131,10 @@
};
let gistURL = '';
codeStore.subscribe((state) => {
if (state.loader?.type === 'gist') {
stateStore.subscribe(({ loader }) => {
if (loader?.type === 'gist') {
// @ts-ignore Gist will have url
gistURL = state.loader.config.url;
gistURL = loader.config.url;
}
});
Expand All @@ -156,11 +156,11 @@
if (browser && ['mermaid.live', 'netlify'].some((path) => window.location.host.includes(path))) {
isNetlify = true;
}
serializedState.subscribe((encodedState: string) => {
iUrl = `${rendererUrl}/img/${encodedState}`;
svgUrl = `${rendererUrl}/svg/${encodedState}`;
krokiUrl = `${krokiRendererUrl}/mermaid/svg/${pakoSerde.serialize($codeStore.code)}`;
mdCode = `[![](${iUrl})](${window.location.protocol}//${window.location.host}${window.location.pathname}#${encodedState})`;
stateStore.subscribe(({ code, serialized }) => {
iUrl = `${rendererUrl}/img/${serialized}`;
svgUrl = `${rendererUrl}/svg/${serialized}`;
krokiUrl = `${krokiRendererUrl}/mermaid/svg/${pakoSerde.serialize(code)}`;
mdCode = `[![](${iUrl})](${window.location.protocol}//${window.location.host}${window.location.pathname}#${serialized})`;
});
</script>

Expand Down
7 changes: 3 additions & 4 deletions src/lib/components/editor.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import type { EditorEvents } from '$lib/types';
import { codeStore } from '$lib/util/state';
import { stateStore } from '$lib/util/state';
import { themeStore } from '$lib/util/theme';
import type monaco from 'monaco-editor';
import { createEventDispatcher, onMount } from 'svelte';
Expand All @@ -21,17 +21,16 @@
theme: 'mermaid',
overviewRulerLanes: 0
};
export let errorMarkers: monaco.editor.IMarkerData[] = [];
let oldText = text;
$: editor && Monaco?.editor.setModelLanguage(editor.getModel(), language);
$: {
if (text !== oldText) {
if ($codeStore.updateEditor) {
if ($stateStore.updateEditor) {
editor?.setValue(text);
}
oldText = text;
}
editor && Monaco?.editor.setModelMarkers(editor.getModel(), 'test', errorMarkers);
editor && Monaco?.editor.setModelMarkers(editor.getModel(), 'test', $stateStore.errorMarkers);
}
themeStore.subscribe(({ isDark }) => {
Expand Down
6 changes: 3 additions & 3 deletions src/lib/components/history/history.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import Card from '$lib/components/card/card.svelte';
import { codeStore, getStateString } from '$lib/util/state';
import { inputStateStore, getStateString } from '$lib/util/state';
import {
addHistoryEntry,
historyModeStore,
Expand Down Expand Up @@ -37,7 +37,7 @@
const previousState: string = getPreviousState(auto);
if (previousState !== currentState) {
addHistoryEntry({
state: $codeStore,
state: $inputStateStore,
time: Date.now(),
type: auto ? 'auto' : 'manual'
});
Expand All @@ -54,7 +54,7 @@
};
const restoreHistory = (state: State): void => {
codeStore.set({ ...state, updateEditor: true, updateDiagram: true });
inputStateStore.set({ ...state, updateEditor: true, updateDiagram: true });
};
const relativeTime = (time: number) => {
Expand Down
5 changes: 4 additions & 1 deletion src/lib/components/preset.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@
};
const loadSampleDiagram = (diagramType: string): void => {
updateCode(samples[diagramType], true, true);
updateCode(samples[diagramType], {
updateDiagram: true,
updateEditor: true
});
};
</script>

Expand Down
31 changes: 16 additions & 15 deletions src/lib/components/view.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<script lang="ts">
import { errorStore } from '$lib/util/error';
import { codeStore } from '$lib/util/state';
import { inputStateStore, stateStore } from '$lib/util/state';
import { onMount } from 'svelte';
import mermaid from 'mermaid';
Expand All @@ -13,11 +11,16 @@
let outOfSync = false;
let manualUpdate = true;
onMount(() => {
codeStore.subscribe((state) => {
stateStore.subscribe((state) => {
if (state.error !== undefined) {
error = true;
return;
}
error = false;
try {
if (container && state && (state.updateDiagram || state.autoSync)) {
if (!state.autoSync) {
$codeStore.updateDiagram = false;
$inputStateStore.updateDiagram = false;
}
outOfSync = false;
manualUpdate = true;
Expand All @@ -31,7 +34,10 @@
delete container.dataset.processed;
mermaid.initialize(Object.assign({}, JSON.parse(state.mermaid)));
mermaid.render('graph-div', code, (svgCode) => {
container.innerHTML = svgCode;
if (svgCode.length > 0) {
console.log(svgCode);
container.innerHTML = svgCode;
}
});
view.parentElement.scrollTop = scroll;
error = false;
Expand All @@ -45,24 +51,19 @@
error = true;
}
});
errorStore.subscribe((err) => {
if (typeof err === 'undefined') {
error = false;
} else {
error = true;
console.log('Error: ', err);
}
});
});
</script>

{#if error && $stateStore.error instanceof Error}
<div class="p-2 text-red-600" id="errorContainer">{$stateStore.error}</div>
{/if}

<div id="view" bind:this={view} class="p-2" class:error class:outOfSync>
<div id="container" bind:this={container} class="flex-1 overflow-auto" />
</div>

<style>
#view {
border: 1px solor darkred;
flex: 1;
}
.error,
Expand Down
18 changes: 17 additions & 1 deletion src/lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ export interface Locals {
userid: string;
}

export interface MarkerData {
severity: number;
message: string;
source?: string;
startLineNumber: number;
startColumn: number;
endLineNumber: number;
endColumn: number;
}

export interface EditorUpdateEvent {
text: string;
}
Expand All @@ -32,6 +42,12 @@ export interface State {
loader?: LoaderConfig;
}

export interface ValidatedState extends State {
error: any;
errorMarkers: MarkerData[];
serialized: string;
}

export interface GistLoaderConfig {
url: string;
}
Expand Down Expand Up @@ -64,4 +80,4 @@ export interface DocConfig {
};
}

type Loader = (url: string) => Promise<State>;
export type Loader = (url: string) => Promise<State>;
3 changes: 0 additions & 3 deletions src/lib/util/error.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/lib/util/fileLoaders/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,4 @@ export const loadDataFromUrl = async (): Promise<void> => {
updateDiagram: true,
updateEditor: true
});
// window.location.search = '';
};
70 changes: 54 additions & 16 deletions src/lib/util/state.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { writable, get, derived } from 'svelte/store';
import type { Readable } from 'svelte/store';
import { persist, localStorage } from '@macfja/svelte-persistent-store';
import type { State } from '$lib/types';
import { saveStatistics } from './stats';
import { serializeState, deserializeState } from './serde';
import mermaid from 'mermaid';

import type { Readable } from 'svelte/store';
import type { MarkerData, State, ValidatedState } from '$lib/types';

export const defaultState: State = {
code: `graph TD
Expand Down Expand Up @@ -36,9 +38,42 @@ const urlParseFailedState = `graph TD
G --> |"No :("| H(Try using the Timeline tab in History <br/>from same browser you used to create the diagram.)
click D href "https://github.com/mermaid-js/mermaid-live-editor/issues/new?assignees=&labels=bug&template=bug_report.md&title=Broken%20link" "Raise issue"`;

export const codeStore = persist(writable(defaultState), localStorage(), 'codeStore');
export const serializedState: Readable<string> = derived([codeStore], ([code], set) => {
set(serializeState(code));
// inputStateStore handles all updates and is shared externally when exporting via URL, History, etc.
export const inputStateStore = persist(writable(defaultState), localStorage(), 'codeStore');

// All internal reads should be done via stateStore, but it should not be persisted/shared externally.
export const stateStore: Readable<ValidatedState> = derived([inputStateStore], ([state]) => {
const processed: ValidatedState = {
...state,
serialized: '',
errorMarkers: [],
error: undefined
};
// No changes should be done to fields part of `state`.
try {
processed.serialized = serializeState(state);
mermaid.parse(state.code);
JSON.parse(state.mermaid);
} catch (e) {
processed.error = e;
console.error(e);
if (e.hash) {
try {
const marker: MarkerData = {
severity: 8, // Error
startLineNumber: e.hash.loc.first_line,
startColumn: e.hash.loc.first_column,
endLineNumber: e.hash.loc.last_line,
endColumn: (e.hash.loc.last_column as number) + 1,
message: e.str
};
processed.errorMarkers = [marker];
} catch (err) {
console.error('Error without line helper', err);
}
}
}
return processed;
});

export const loadState = (data: string): void => {
Expand All @@ -57,30 +92,33 @@ export const loadState = (data: string): void => {
) {
delete mermaidConfig.securityLevel; // Prevent setting overriding securityLevel when loading state to mitigate possible XSS attack
}

state.mermaid = JSON.stringify(mermaidConfig, null, 2);
} catch (e) {
state = get(codeStore);
state = get(inputStateStore);
if (data) {
console.error('Init error', e);
state.code = urlParseFailedState;
state.mermaid = defaultState.mermaid;
}
}
updateCodeStore({ ...state, updateEditor: true });
};

export const updateCodeStore = (newState: State): void => {
codeStore.update((state) => {
inputStateStore.update((state) => {
return { ...state, ...newState };
});
};

let prompted = false;
export const updateCode = (code: string, updateEditor: boolean, updateDiagram = false): void => {
export const updateCode = (
code: string,
{ updateEditor, updateDiagram = false }: { updateEditor: boolean; updateDiagram?: boolean }
): void => {
saveStatistics(code);
const lines = (code.match(/\n/g) || '').length + 1;

if (lines > 50 && !prompted && get(codeStore).autoSync) {
if (lines > 50 && !prompted && get(stateStore).autoSync) {
const turnOff = confirm(
'Long diagram detected. Turn off Auto Sync? Click the sync logo to manually sync.'
);
Expand All @@ -92,19 +130,19 @@ export const updateCode = (code: string, updateEditor: boolean, updateDiagram =
}
}

codeStore.update((state) => {
inputStateStore.update((state) => {
return { ...state, code, updateEditor, updateDiagram };
});
};

export const updateConfig = (config: string, updateEditor: boolean): void => {
codeStore.update((state) => {
inputStateStore.update((state) => {
return { ...state, mermaid: config, updateEditor };
});
};

export const toggleDarkTheme = (dark: boolean): void => {
codeStore.update((state) => {
inputStateStore.update((state) => {
const config = JSON.parse(state.mermaid);
if (!config.theme || ['dark', 'default'].includes(config.theme)) {
config.theme = dark ? 'dark' : 'default';
Expand All @@ -115,11 +153,11 @@ export const toggleDarkTheme = (dark: boolean): void => {
};

export const initURLSubscription = (): void => {
serializedState.subscribe((state: string) => {
history.replaceState(undefined, undefined, `#${state}`);
stateStore.subscribe(({ serialized }) => {
history.replaceState(undefined, undefined, `#${serialized}`);
});
};

export const getStateString = (): string => {
return JSON.stringify(get(codeStore));
return JSON.stringify(get(inputStateStore));
};

0 comments on commit 2cff54b

Please sign in to comment.