Compare commits
13
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
83ca1c5438 | ||
|
|
fee0addc6b | ||
|
|
d375b5cfea | ||
|
|
ce1dc1f3af | ||
|
|
0f3ce99659 | ||
|
|
e6ab76a5d1 | ||
|
|
40bd944597 | ||
|
|
831d1e384d | ||
|
|
4c86d95f21 | ||
|
|
64e8ed09ba | ||
|
|
70b67afb87 | ||
|
|
38824e8bab | ||
|
|
63869b8d11 |
@@ -48,3 +48,4 @@ jobs:
|
|||||||
parallel: true
|
parallel: true
|
||||||
env:
|
env:
|
||||||
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
|
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
|
||||||
|
MERMAID_DISABLE_DEBOUNCE: 'true'
|
||||||
|
|||||||
Vendored
+2
-1
@@ -3,5 +3,6 @@
|
|||||||
"cSpell.words": ["pako", "Serde", "serdes"],
|
"cSpell.words": ["pako", "Serde", "serdes"],
|
||||||
"vitest.commandLine": "yarn test:unit",
|
"vitest.commandLine": "yarn test:unit",
|
||||||
"vitest.enable": true,
|
"vitest.enable": true,
|
||||||
"testing.autoRun.mode": "rerun"
|
"testing.autoRun.mode": "rerun",
|
||||||
|
"svelte.enable-ts-plugin": true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
import { disableDebounce, verifyFileSize } from './util';
|
import { verifyFileSize } from './util';
|
||||||
describe('Check actions', () => {
|
describe('Check actions', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
cy.clearLocalStorage();
|
cy.clearLocalStorage();
|
||||||
cy.visit('/edit');
|
cy.visit('/edit');
|
||||||
disableDebounce();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should update markdown code', () => {
|
it('should update markdown code', () => {
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
import { getEditor, cmd, disableDebounce } from './util';
|
import { getEditor, cmd } from './util';
|
||||||
|
|
||||||
describe('Auto sync tests', () => {
|
describe('Auto sync tests', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
cy.clearLocalStorage();
|
cy.clearLocalStorage();
|
||||||
cy.visit('/');
|
cy.visit('/');
|
||||||
disableDebounce();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should dim diagram when code is edited', () => {
|
it('should dim diagram when code is edited', () => {
|
||||||
@@ -72,7 +71,6 @@ describe.only('Pan and Zoom', () => {
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
cy.clearLocalStorage();
|
cy.clearLocalStorage();
|
||||||
cy.visit('/');
|
cy.visit('/');
|
||||||
disableDebounce();
|
|
||||||
});
|
});
|
||||||
it('should toggle pan and zoom', () => {
|
it('should toggle pan and zoom', () => {
|
||||||
cy.get('#svg-pan-zoom-reset-pan-zoom').should('not.exist');
|
cy.get('#svg-pan-zoom-reset-pan-zoom').should('not.exist');
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import { getEditor, disableDebounce, verifyFileSnapshot } from './util';
|
import { getEditor, verifyFileSnapshot } from './util';
|
||||||
|
|
||||||
describe('Save History', () => {
|
describe('Save History', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
cy.clock(new Date(2022, 0, 1).getTime());
|
cy.clock(new Date(2022, 0, 1).getTime());
|
||||||
cy.clearLocalStorage();
|
cy.clearLocalStorage();
|
||||||
cy.visit('/edit');
|
cy.visit('/edit');
|
||||||
disableDebounce();
|
|
||||||
cy.contains('Actions').click();
|
cy.contains('Actions').click();
|
||||||
cy.contains('History').click();
|
cy.contains('History').click();
|
||||||
});
|
});
|
||||||
@@ -17,11 +17,11 @@ describe('Save History', () => {
|
|||||||
it('should load history from localstorage', () => {
|
it('should load history from localstorage', () => {
|
||||||
cy.setLocalStorage(
|
cy.setLocalStorage(
|
||||||
'manualHistoryStore',
|
'manualHistoryStore',
|
||||||
'[{"state":{"code":"graph TD\\n A[Halloween] -->|Get money| B(Go shopping)","mermaid":"{\\n \\"theme\\": \\"dark\\"\\n}","updateEditor":false,"autoSync":true,"updateDiagram":false},"time":0,"type":"manual","id":"d7ea820e-21dd-418a-b984-fd58acde09df","name":"hollow-art"},{"state":{"code":"graph TD\\n A[Christmas] -->|Get money| B(Go shopping)","mermaid":"{\\n \\"theme\\": \\"dark\\"\\n}","updateEditor":true,"autoSync":true,"updateDiagram":true},"time":0,"type":"manual","id":"b749ffc6-522b-4a44-86cf-7c1ffc3146b3","name":"helpful-ocean"}]'
|
'[{"state":{"code":"graph TD\\n A[Halloween] -->|Get money| B(Go shopping)","mermaid":"{\\n \\"theme\\": \\"dark\\"\\n}","autoSync":true,"updateDiagram":false},"time":0,"type":"manual","id":"d7ea820e-21dd-418a-b984-fd58acde09df","name":"hollow-art"},{"state":{"code":"graph TD\\n A[Christmas] -->|Get money| B(Go shopping)","mermaid":"{\\n \\"theme\\": \\"dark\\"\\n}","autoSync":true,"updateDiagram":true},"time":0,"type":"manual","id":"b749ffc6-522b-4a44-86cf-7c1ffc3146b3","name":"helpful-ocean"}]'
|
||||||
);
|
);
|
||||||
cy.setLocalStorage(
|
cy.setLocalStorage(
|
||||||
'autoHistoryStore',
|
'autoHistoryStore',
|
||||||
'[{"state":{"code":"graph TD\\n A[New Year] -->|Get money| B(Go shopping)","mermaid":"{\\n \\"theme\\": \\"dark\\"\\n}","updateEditor":false,"autoSync":true,"updateDiagram":false},"time":0,"type":"auto","id":"69ea820e-522b-4a44-86cf-fd58acde09df","name":"barking-dog"},{"state":{"code":"graph TD\\n A[Christmas] -->|Get money| B(Go shopping)","mermaid":"{\\n \\"theme\\": \\"dark\\"\\n}","updateEditor":true,"autoSync":true,"updateDiagram":true},"time":0,"type":"manual","id":"x749ffc6-21dd-418a-b984-7c1ffc3146b3","name":"needy-mosquito"}]'
|
'[{"state":{"code":"graph TD\\n A[New Year] -->|Get money| B(Go shopping)","mermaid":"{\\n \\"theme\\": \\"dark\\"\\n}","autoSync":true,"updateDiagram":false},"time":0,"type":"auto","id":"69ea820e-522b-4a44-86cf-fd58acde09df","name":"barking-dog"},{"state":{"code":"graph TD\\n A[Christmas] -->|Get money| B(Go shopping)","mermaid":"{\\n \\"theme\\": \\"dark\\"\\n}","autoSync":true,"updateDiagram":true},"time":0,"type":"manual","id":"x749ffc6-21dd-418a-b984-7c1ffc3146b3","name":"needy-mosquito"}]'
|
||||||
);
|
);
|
||||||
cy.reload();
|
cy.reload();
|
||||||
cy.contains('Actions').click();
|
cy.contains('Actions').click();
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
import { toBase64 } from 'js-base64';
|
import { toBase64 } from 'js-base64';
|
||||||
import { disableDebounce } from './util';
|
|
||||||
|
|
||||||
describe('Site Loads', () => {
|
describe('Site Loads', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
cy.clearLocalStorage();
|
cy.clearLocalStorage();
|
||||||
cy.visit('/');
|
cy.visit('/');
|
||||||
disableDebounce();
|
|
||||||
});
|
});
|
||||||
it('Check Home page load', () => {
|
it('Check Home page load', () => {
|
||||||
cy.url().should('include', '/edit');
|
cy.url().should('include', '/edit');
|
||||||
@@ -60,7 +58,7 @@ describe('Site Loads', () => {
|
|||||||
// Disabled temporarily. Should be enabled after the issue is fixed in Mermaid.
|
// Disabled temporarily. Should be enabled after the issue is fixed in Mermaid.
|
||||||
// it('should prevent setting the "securityLevel" option via URL', () => {
|
// it('should prevent setting the "securityLevel" option via URL', () => {
|
||||||
// const b64State = toBase64(
|
// const b64State = toBase64(
|
||||||
// `{"code":"graph TD\\nA[\\"<img src='https://via.placeholder.com/64' width=64 />\\"]","mermaid":"{\\"securityLevel\\": \\"loose\\", \\"theme\\": \\"forest\\"}","updateEditor":true,"autoSync":true,"updateDiagram":true}`,
|
// `{"code":"graph TD\\nA[\\"<img src='https://via.placeholder.com/64' width=64 />\\"]","mermaid":"{\\"securityLevel\\": \\"loose\\", \\"theme\\": \\"forest\\"}","autoSync":true,"updateDiagram":true}`,
|
||||||
// true
|
// true
|
||||||
// );
|
// );
|
||||||
// cy.on('window:confirm', () => true);
|
// cy.on('window:confirm', () => true);
|
||||||
@@ -75,7 +73,7 @@ describe('Site Loads', () => {
|
|||||||
|
|
||||||
it('should allow persisting "securityLevel" using confirm dialogue', () => {
|
it('should allow persisting "securityLevel" using confirm dialogue', () => {
|
||||||
const b64State = toBase64(
|
const b64State = toBase64(
|
||||||
`{"code":"graph TD\\nA[\\"<img src='https://dummyimage.com/64' width=64/>\\"]","mermaid":"{\\"securityLevel\\": \\"loose\\", \\"theme\\": \\"forest\\"}","updateEditor":true,"autoSync":true,"updateDiagram":true}`,
|
`{"code":"graph TD\\nA[\\"<img src='https://dummyimage.com/64' width=64/>\\"]","mermaid":"{\\"securityLevel\\": \\"loose\\", \\"theme\\": \\"forest\\"}","autoSync":true,"updateDiagram":true}`,
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
cy.on('window:confirm', () => false);
|
cy.on('window:confirm', () => false);
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ export const getEditor = ({ bottom = true, newline = false } = {}) =>
|
|||||||
.type(`${bottom ? '{pageDown}' : cmd}`)
|
.type(`${bottom ? '{pageDown}' : cmd}`)
|
||||||
.type(`${newline ? '{enter}' : cmd}`);
|
.type(`${newline ? '{enter}' : cmd}`);
|
||||||
|
|
||||||
export const disableDebounce = () => cy.setLocalStorage('noDebounce', 'true');
|
|
||||||
|
|
||||||
const downloadsFolder = Cypress.config('downloadsFolder');
|
const downloadsFolder = Cypress.config('downloadsFolder');
|
||||||
|
|
||||||
export const verifyFileSize = (
|
export const verifyFileSize = (
|
||||||
|
|||||||
@@ -1,28 +1,28 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
"Site Loads": {
|
"Site Loads": {
|
||||||
"Check Home page load": {
|
"Check Home page load": {
|
||||||
"1": "{\"code\":\"graph TD\\n A[Christmas] -->|Get money| B(Go shopping)\\n B --> C{Let me think}\\n C -->|One| D[Laptop]\\n C -->|Two| E[iPhone]\\n C -->|Three| F[fa:fa-car Car]\\n \",\"mermaid\":\"{\\n \\\"theme\\\": \\\"default\\\"\\n}\",\"updateEditor\":true,\"autoSync\":true,\"updateDiagram\":true}"
|
"1": "{\"code\":\"graph TD\\n A[Christmas] -->|Get money| B(Go shopping)\\n B --> C{Let me think}\\n C -->|One| D[Laptop]\\n C -->|Two| E[iPhone]\\n C -->|Three| F[fa:fa-car Car]\\n \",\"mermaid\":\"{\\n \\\"theme\\\": \\\"default\\\"\\n}\",\"autoSync\":true,\"updateDiagram\":true}"
|
||||||
},
|
},
|
||||||
"Check Redirect from old URL": {
|
"Check Redirect from old URL": {
|
||||||
"1": "{\"code\":\"graph TD\\n A[Christmas] -->|Get money| B(Go shopping)\\n B --> C{Let me think}\\n C -->|One| D[Laptop]\\n C -->|Two| E[iPhone]\\n C -->|Three| F[fa:fa-car Car]\",\"mermaid\":\"{\\n \\\"theme\\\": \\\"default\\\"\\n}\",\"updateEditor\":false,\"autoSync\":true,\"updateDiagram\":true}"
|
"1": "{\"code\":\"graph TD\\n A[Christmas] -->|Get money| B(Go shopping)\\n B --> C{Let me think}\\n C -->|One| D[Laptop]\\n C -->|Two| E[iPhone]\\n C -->|Three| F[fa:fa-car Car]\",\"mermaid\":\"{\\n \\\"theme\\\": \\\"default\\\"\\n}\",\"autoSync\":true,\"updateDiagram\":true}"
|
||||||
},
|
},
|
||||||
"should load diagram from gist": {
|
"should load diagram from gist": {
|
||||||
"1": "{\"code\":\"graph TD\\n A[Party] -->|Get money| B(Go shopping!!)\\n \",\"mermaid\":\"{\\n \\\"theme\\\": \\\"forest\\\",\\n \\\"test\\\": \\\"hello world\\\"\\n}\",\"updateEditor\":false,\"autoSync\":true,\"updateDiagram\":true,\"loader\":{\"type\":\"gist\",\"config\":{\"url\":\"https://gist.github.com/sidharthv96/6268a23e673a533dcb198f241fd7012a\"}}}"
|
"1": "{\"code\":\"graph TD\\n A[Party] -->|Get money| B(Go shopping!!)\\n \",\"mermaid\":\"{\\n \\\"theme\\\": \\\"forest\\\",\\n \\\"test\\\": \\\"hello world\\\"\\n}\",\"autoSync\":true,\"updateDiagram\":true,\"loader\":{\"type\":\"gist\",\"config\":{\"url\":\"https://gist.github.com/sidharthv96/6268a23e673a533dcb198f241fd7012a\"}}}"
|
||||||
},
|
},
|
||||||
"should load diagram from gist revision": {
|
"should load diagram from gist revision": {
|
||||||
"1": "{\"code\":\"graph TD\\n A[Party] -->|Get money| B(Go shopping)\\n \",\"mermaid\":\"{\\n \\\"theme\\\": \\\"forest\\\",\\n \\\"test\\\": \\\"hello\\\"\\n}\",\"updateEditor\":false,\"autoSync\":true,\"updateDiagram\":true,\"loader\":{\"type\":\"gist\",\"config\":{\"url\":\"https://gist.github.com/sidharthv96/6268a23e673a533dcb198f241fd7012a/ec9b4ab0e41e4ff6287326cd3cb47affd7851e19\"}}}"
|
"1": "{\"code\":\"graph TD\\n A[Party] -->|Get money| B(Go shopping)\\n \",\"mermaid\":\"{\\n \\\"theme\\\": \\\"forest\\\",\\n \\\"test\\\": \\\"hello\\\"\\n}\",\"autoSync\":true,\"updateDiagram\":true,\"loader\":{\"type\":\"gist\",\"config\":{\"url\":\"https://gist.github.com/sidharthv96/6268a23e673a533dcb198f241fd7012a/ec9b4ab0e41e4ff6287326cd3cb47affd7851e19\"}}}"
|
||||||
},
|
},
|
||||||
"should load diagram from raw files": {
|
"should load diagram from raw files": {
|
||||||
"1": "{\"code\":\"graph TD\\n A[Party] -->|Get money| B(Go shopping!!)\\n \",\"mermaid\":\"{\\n \\\"theme\\\": \\\"forest\\\",\\n \\\"test\\\": \\\"hello world\\\"\\n}\",\"updateEditor\":false,\"autoSync\":true,\"updateDiagram\":true,\"loader\":{\"type\":\"files\",\"config\":{\"codeURL\":\"https://gist.githubusercontent.com/sidharthv96/6268a23e673a533dcb198f241fd7012a/raw/4eb03887e6a41397e80bdcdbf94017c498f8f1e2/code.mmd\",\"configURL\":\"https://gist.githubusercontent.com/sidharthv96/6268a23e673a533dcb198f241fd7012a/raw/4eb03887e6a41397e80bdcdbf94017c498f8f1e2/config.json\"}}}"
|
"1": "{\"code\":\"graph TD\\n A[Party] -->|Get money| B(Go shopping!!)\\n \",\"mermaid\":\"{\\n \\\"theme\\\": \\\"forest\\\",\\n \\\"test\\\": \\\"hello world\\\"\\n}\",\"autoSync\":true,\"updateDiagram\":true,\"loader\":{\"type\":\"files\",\"config\":{\"codeURL\":\"https://gist.githubusercontent.com/sidharthv96/6268a23e673a533dcb198f241fd7012a/raw/4eb03887e6a41397e80bdcdbf94017c498f8f1e2/code.mmd\",\"configURL\":\"https://gist.githubusercontent.com/sidharthv96/6268a23e673a533dcb198f241fd7012a/raw/4eb03887e6a41397e80bdcdbf94017c498f8f1e2/config.json\"}}}"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"__version": "10.6.0",
|
"__version": "10.6.0",
|
||||||
"Auto sync tests": {
|
"Auto sync tests": {
|
||||||
"should dim diagram when code is edited": {
|
"should dim diagram when code is edited": {
|
||||||
"1": "{\"code\":\"graph TD\\n A[Christmas] -->|Get money| B(Go shopping)\\n B --> C{Let me think}\\n C -->|One| D[Laptop]\\n C -->|Two| E[iPhone]\\n C -->|Three| F[fa:fa-car Car]\\n C --> Test\",\"mermaid\":\"{\\n \\\"theme\\\": \\\"default\\\"\\n}\",\"updateEditor\":false,\"autoSync\":false,\"updateDiagram\":false}"
|
"1": "{\"code\":\"graph TD\\n A[Christmas] -->|Get money| B(Go shopping)\\n B --> C{Let me think}\\n C -->|One| D[Laptop]\\n C -->|Two| E[iPhone]\\n C -->|Three| F[fa:fa-car Car]\\n C --> Test\",\"mermaid\":\"{\\n \\\"theme\\\": \\\"default\\\"\\n}\",\"autoSync\":false,\"updateDiagram\":false}"
|
||||||
},
|
},
|
||||||
"should not dim diagram when code is in sync": {
|
"should not dim diagram when code is in sync": {
|
||||||
"1": "{\"code\":\"graph TD\\n A[Christmas] -->|Get money| B(Go shopping)\\n B --> C{Let me think}\\n C -->|One| D[Laptop]\\n C -->|Two| E[iPhone]\\n C -->|Three| F[fa:fa-car Car]\\n C --> Testing\",\"mermaid\":\"{\\n \\\"theme\\\": \\\"default\\\"\\n}\",\"updateEditor\":false,\"autoSync\":true,\"updateDiagram\":false}"
|
"1": "{\"code\":\"graph TD\\n A[Christmas] -->|Get money| B(Go shopping)\\n B --> C{Let me think}\\n C -->|One| D[Laptop]\\n C -->|Two| E[iPhone]\\n C -->|Three| F[fa:fa-car Car]\\n C --> Testing\",\"mermaid\":\"{\\n \\\"theme\\\": \\\"default\\\"\\n}\",\"autoSync\":true,\"updateDiagram\":false}"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"Test themes": {
|
"Test themes": {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite dev",
|
"dev": "vite dev",
|
||||||
|
"dev:test": "MERMAID_DISABLE_DEBOUNCE=true yarn dev",
|
||||||
"build": "vite build",
|
"build": "vite build",
|
||||||
"preview": "vite preview",
|
"preview": "vite preview",
|
||||||
"lint": "prettier --check --cache --plugin-search-dir=. .;eslint --ignore-path .gitignore .",
|
"lint": "prettier --check --cache --plugin-search-dir=. .;eslint --ignore-path .gitignore .",
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
export let isCloseable = true;
|
export let isCloseable = true;
|
||||||
export let isOpen = true;
|
export let isOpen = true;
|
||||||
export let tabs: Tab[] = [];
|
export let tabs: Tab[] = [];
|
||||||
|
export let activeTabID: string = '';
|
||||||
export let title: string;
|
export let title: string;
|
||||||
$: isOpen = isCloseable ? isOpen : true;
|
$: isOpen = isCloseable ? isOpen : true;
|
||||||
$: isTabsShown = isOpen && tabs.length > 0;
|
$: isTabsShown = isOpen && tabs.length > 0;
|
||||||
@@ -15,7 +16,7 @@
|
|||||||
class="bg-primary p-2 {isTabsShown ? 'pb-0' : ''} flex-none cursor-pointer"
|
class="bg-primary p-2 {isTabsShown ? 'pb-0' : ''} flex-none cursor-pointer"
|
||||||
on:click={() => (isOpen = !isOpen)}>
|
on:click={() => (isOpen = !isOpen)}>
|
||||||
<div class="flex justify-between">
|
<div class="flex justify-between">
|
||||||
<Tabs on:select {tabs} bind:isOpen {title} {isCloseable} />
|
<Tabs on:select {tabs} bind:isOpen {title} {isCloseable} {activeTabID} />
|
||||||
<div class="flex gap-x-4 items-center {isTabsShown ? '-mt-2' : ''}">
|
<div class="flex gap-x-4 items-center {isTabsShown ? '-mt-2' : ''}">
|
||||||
<slot name="actions" />
|
<slot name="actions" />
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -3,12 +3,14 @@
|
|||||||
import { createEventDispatcher } from 'svelte';
|
import { createEventDispatcher } from 'svelte';
|
||||||
import { fade } from 'svelte/transition';
|
import { fade } from 'svelte/transition';
|
||||||
export let isCloseable = true;
|
export let isCloseable = true;
|
||||||
export let tabs: Tab[] = [];
|
export let tabs: Tab[];
|
||||||
export let title: string;
|
export let title: string;
|
||||||
export let isOpen = false;
|
export let isOpen = false;
|
||||||
|
export let activeTabID: string;
|
||||||
|
|
||||||
$: activeTabID = tabs[0]?.id;
|
if (!activeTabID && tabs.length > 0) {
|
||||||
|
activeTabID = tabs[0].id;
|
||||||
|
}
|
||||||
const dispatch = createEventDispatcher<TabEvents>();
|
const dispatch = createEventDispatcher<TabEvents>();
|
||||||
const toggleTabs = (tab: Tab) => {
|
const toggleTabs = (tab: Tab) => {
|
||||||
activeTabID = tab.id;
|
activeTabID = tab.id;
|
||||||
|
|||||||
@@ -1,77 +1,95 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import type { EditorEvents } from '$lib/types';
|
import type { EditorMode } from '$lib/types';
|
||||||
import { stateStore } from '$lib/util/state';
|
import { stateStore, updateCode, updateConfig } from '$lib/util/state';
|
||||||
import { themeStore } from '$lib/util/theme';
|
import { themeStore } from '$lib/util/theme';
|
||||||
import { syncDiagram } from '$lib/util/util';
|
import { syncDiagram } from '$lib/util/util';
|
||||||
import type monaco from 'monaco-editor';
|
import type monaco from 'monaco-editor';
|
||||||
import { createEventDispatcher, onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
import initEditor from 'monaco-mermaid';
|
import initEditor from 'monaco-mermaid';
|
||||||
import { logEvent } from '$lib/util/stats';
|
import { logEvent } from '$lib/util/stats';
|
||||||
|
import { debounceEnabled } from '$lib/util/env';
|
||||||
|
|
||||||
let divEl: HTMLDivElement = null;
|
let divEl: HTMLDivElement = null;
|
||||||
let editor: monaco.editor.IStandaloneCodeEditor;
|
let editor: monaco.editor.IStandaloneCodeEditor;
|
||||||
let Monaco;
|
let Monaco: typeof monaco;
|
||||||
|
let editorOptions: monaco.editor.IStandaloneEditorConstructionOptions = {
|
||||||
export let text: string;
|
|
||||||
export let language: string;
|
|
||||||
export let editorOptions: monaco.editor.IStandaloneEditorConstructionOptions = {
|
|
||||||
value: text,
|
|
||||||
language: language,
|
|
||||||
minimap: {
|
minimap: {
|
||||||
enabled: false
|
enabled: false
|
||||||
},
|
},
|
||||||
theme: 'mermaid',
|
theme: 'mermaid',
|
||||||
overviewRulerLanes: 0
|
overviewRulerLanes: 0
|
||||||
};
|
};
|
||||||
let oldText = text;
|
let text = '';
|
||||||
$: editor && Monaco?.editor.setModelLanguage(editor.getModel(), language);
|
|
||||||
|
|
||||||
const handleTextUpdate = (newText: string) => {
|
stateStore.subscribe(({ errorMarkers, editorMode, code, mermaid }) => {
|
||||||
if (newText !== oldText) {
|
if (!editor) return;
|
||||||
if ($stateStore.updateEditor) {
|
|
||||||
editor?.setValue(newText);
|
// Update editor text if it's different
|
||||||
}
|
const newText = editorMode === 'code' ? code : mermaid;
|
||||||
oldText = newText;
|
if (newText !== text) {
|
||||||
|
editor.setValue(newText);
|
||||||
|
text = newText;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
$: handleTextUpdate(text);
|
// Update editor mode if it's different
|
||||||
stateStore.subscribe(({ errorMarkers }) => {
|
const language = editorMode === 'code' ? 'mermaid' : 'json';
|
||||||
editor && Monaco?.editor.setModelMarkers(editor.getModel(), 'test', errorMarkers);
|
if (editor.getModel().getLanguageId() !== language) {
|
||||||
|
Monaco?.editor.setModelLanguage(editor.getModel(), language);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Display errors if present
|
||||||
|
if (errorMarkers.length > 0) {
|
||||||
|
Monaco?.editor.setModelMarkers(editor.getModel(), 'test', errorMarkers);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
themeStore.subscribe(({ isDark }) => {
|
themeStore.subscribe(({ isDark }) => {
|
||||||
editor && Monaco?.editor.setTheme(isDark ? 'mermaid-dark' : 'mermaid');
|
editor && Monaco?.editor.setTheme(isDark ? 'mermaid-dark' : 'mermaid');
|
||||||
});
|
});
|
||||||
|
|
||||||
const dispatch = createEventDispatcher<EditorEvents>();
|
const handleUpdate = (text: string, mode: EditorMode) => {
|
||||||
|
if (mode === 'code') {
|
||||||
|
updateCode(text);
|
||||||
|
} else {
|
||||||
|
updateConfig(text);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Debounce state updates to avoid performance issues
|
||||||
|
let debounce: { [key: string]: number } = {};
|
||||||
|
const updateHandler = (newText: string) => {
|
||||||
|
text = newText;
|
||||||
|
const mode = $stateStore.editorMode;
|
||||||
|
if (debounceEnabled) {
|
||||||
|
clearTimeout(debounce[mode]);
|
||||||
|
debounce[mode] = window.setTimeout(() => {
|
||||||
|
handleUpdate(text, mode);
|
||||||
|
}, 300);
|
||||||
|
} else {
|
||||||
|
handleUpdate(text, mode);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const loadMonaco = async () => {
|
const loadMonaco = async () => {
|
||||||
let i = 0;
|
let i = 0;
|
||||||
while (i++ < 10) {
|
while (i++ < 500) {
|
||||||
try {
|
try {
|
||||||
// @ts-ignore : This is a hack to handle a svelte-kit error when importing monaco.
|
// @ts-ignore : This is a hack to handle a svelte-kit error when importing monaco.
|
||||||
Monaco = monaco;
|
Monaco = window.monaco;
|
||||||
return;
|
return;
|
||||||
} catch {
|
} catch {
|
||||||
await new Promise((r) => setTimeout(r, 500));
|
await new Promise((r) => setTimeout(r, 100));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
alert('Loading Monaco Editor failed. Please try refreshing the page.');
|
alert('Loading Monaco Editor failed. Please try refreshing the page.');
|
||||||
};
|
};
|
||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
try {
|
await loadMonaco(); // Fix https://github.com/mermaid-js/mermaid-live-editor/issues/175
|
||||||
// @ts-ignore : This is a hack to handle a svelte-kit error when importing monaco.
|
|
||||||
Monaco = monaco;
|
|
||||||
} catch {
|
|
||||||
await loadMonaco(); // Fix https://github.com/mermaid-js/mermaid-live-editor/issues/175
|
|
||||||
}
|
|
||||||
initEditor(Monaco);
|
initEditor(Monaco);
|
||||||
editor = Monaco.editor.create(divEl, editorOptions);
|
editor = Monaco.editor.create(divEl, editorOptions);
|
||||||
editor.onDidChangeModelContent(() => {
|
editor.onDidChangeModelContent(() => {
|
||||||
oldText = editor.getValue();
|
updateHandler(editor.getValue());
|
||||||
dispatch('update', {
|
|
||||||
text: oldText
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
editor.addAction({
|
editor.addAction({
|
||||||
id: 'mermaid-render-diagram',
|
id: 'mermaid-render-diagram',
|
||||||
|
|||||||
@@ -90,7 +90,7 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
const restoreHistoryItem = (state: State): void => {
|
const restoreHistoryItem = (state: State): void => {
|
||||||
inputStateStore.set({ ...state, updateEditor: true, updateDiagram: true });
|
inputStateStore.set({ ...state, updateDiagram: true });
|
||||||
};
|
};
|
||||||
|
|
||||||
const relativeTime = (time: number) => {
|
const relativeTime = (time: number) => {
|
||||||
|
|||||||
@@ -95,11 +95,11 @@ describe('history migration', () => {
|
|||||||
it('should inject history IDs as migration', () => {
|
it('should inject history IDs as migration', () => {
|
||||||
window.localStorage.setItem(
|
window.localStorage.setItem(
|
||||||
'manualHistoryStore',
|
'manualHistoryStore',
|
||||||
'[{"state":{"code":"graph TD\\n A[Halloween] -->|Get money| B(Go shopping)","mermaid":"{\\n \\"theme\\": \\"dark\\"\\n}","updateEditor":false,"autoSync":true,"updateDiagram":false},"time":0,"type":"manual","name":"hollow-art"},{"state":{"code":"graph TD\\n A[Christmas] -->|Get money| B(Go shopping)","mermaid":"{\\n \\"theme\\": \\"dark\\"\\n}","updateEditor":true,"autoSync":true,"updateDiagram":true},"time":0,"type":"manual","name":"helpful-ocean"}]'
|
'[{"state":{"code":"graph TD\\n A[Halloween] -->|Get money| B(Go shopping)","mermaid":"{\\n \\"theme\\": \\"dark\\"\\n}","autoSync":true,"updateDiagram":false},"time":0,"type":"manual","name":"hollow-art"},{"state":{"code":"graph TD\\n A[Christmas] -->|Get money| B(Go shopping)","mermaid":"{\\n \\"theme\\": \\"dark\\"\\n}","autoSync":true,"updateDiagram":true},"time":0,"type":"manual","name":"helpful-ocean"}]'
|
||||||
);
|
);
|
||||||
window.localStorage.setItem(
|
window.localStorage.setItem(
|
||||||
'autoHistoryStore',
|
'autoHistoryStore',
|
||||||
'[{"state":{"code":"graph TD\\n A[New Year] -->|Get money| B(Go shopping)","mermaid":"{\\n \\"theme\\": \\"dark\\"\\n}","updateEditor":false,"autoSync":true,"updateDiagram":false},"time":0,"type":"auto","name":"barking-dog"},{"state":{"code":"graph TD\\n A[Christmas] -->|Get money| B(Go shopping)","mermaid":"{\\n \\"theme\\": \\"dark\\"\\n}","updateEditor":true,"autoSync":true,"updateDiagram":true},"time":0,"type":"manual","name":"needy-mosquito"}]'
|
'[{"state":{"code":"graph TD\\n A[New Year] -->|Get money| B(Go shopping)","mermaid":"{\\n \\"theme\\": \\"dark\\"\\n}","autoSync":true,"updateDiagram":false},"time":0,"type":"auto","name":"barking-dog"},{"state":{"code":"graph TD\\n A[Christmas] -->|Get money| B(Go shopping)","mermaid":"{\\n \\"theme\\": \\"dark\\"\\n}","autoSync":true,"updateDiagram":true},"time":0,"type":"manual","name":"needy-mosquito"}]'
|
||||||
);
|
);
|
||||||
let manualHistoryStore: HistoryEntry[] = JSON.parse(
|
let manualHistoryStore: HistoryEntry[] = JSON.parse(
|
||||||
window.localStorage.getItem('manualHistoryStore')
|
window.localStorage.getItem('manualHistoryStore')
|
||||||
|
|||||||
@@ -98,7 +98,6 @@
|
|||||||
const loadSampleDiagram = (diagramType: string): void => {
|
const loadSampleDiagram = (diagramType: string): void => {
|
||||||
updateCode(samples[diagramType], {
|
updateCode(samples[diagramType], {
|
||||||
updateDiagram: true,
|
updateDiagram: true,
|
||||||
updateEditor: true,
|
|
||||||
resetPanZoom: true
|
resetPanZoom: true
|
||||||
});
|
});
|
||||||
void logEvent('loadSampleDiagram', { diagramType });
|
void logEvent('loadSampleDiagram', { diagramType });
|
||||||
|
|||||||
@@ -94,7 +94,7 @@
|
|||||||
outOfSync = true;
|
outOfSync = true;
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('view fail', e);
|
console.error('view fail', e);
|
||||||
error = true;
|
error = true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Vendored
+4
-8
@@ -16,13 +16,6 @@ export interface MarkerData {
|
|||||||
endColumn: number;
|
endColumn: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface EditorUpdateEvent {
|
|
||||||
text: string;
|
|
||||||
}
|
|
||||||
export interface EditorEvents {
|
|
||||||
update: EditorUpdateEvent;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface TabEvents {
|
export interface TabEvents {
|
||||||
select: Tab;
|
select: Tab;
|
||||||
}
|
}
|
||||||
@@ -36,9 +29,9 @@ export interface Tab {
|
|||||||
export interface State {
|
export interface State {
|
||||||
code: string;
|
code: string;
|
||||||
mermaid: string;
|
mermaid: string;
|
||||||
updateEditor: boolean;
|
|
||||||
updateDiagram: boolean;
|
updateDiagram: boolean;
|
||||||
autoSync: boolean;
|
autoSync: boolean;
|
||||||
|
editorMode?: EditorMode;
|
||||||
panZoom?: boolean;
|
panZoom?: boolean;
|
||||||
pan?: { x: number; y: number };
|
pan?: { x: number; y: number };
|
||||||
zoom?: number;
|
zoom?: number;
|
||||||
@@ -46,6 +39,7 @@ export interface State {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface ValidatedState extends State {
|
export interface ValidatedState extends State {
|
||||||
|
editorMode: EditorMode;
|
||||||
error: unknown;
|
error: unknown;
|
||||||
errorMarkers: MarkerData[];
|
errorMarkers: MarkerData[];
|
||||||
serialized: string;
|
serialized: string;
|
||||||
@@ -86,5 +80,7 @@ export interface DocConfig {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type EditorMode = 'code' | 'config';
|
||||||
|
|
||||||
export type Loader = (url: string) => Promise<State>;
|
export type Loader = (url: string) => Promise<State>;
|
||||||
export type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
|
export type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
|
||||||
|
|||||||
@@ -2,3 +2,4 @@ export const rendererUrl: string =
|
|||||||
(import.meta.env.MERMAID_RENDERER_URL as string) ?? 'https://mermaid.ink';
|
(import.meta.env.MERMAID_RENDERER_URL as string) ?? 'https://mermaid.ink';
|
||||||
export const krokiRendererUrl: string =
|
export const krokiRendererUrl: string =
|
||||||
(import.meta.env.MERMAID_KROKI_RENDERER_URL as string) ?? 'https://kroki.io';
|
(import.meta.env.MERMAID_KROKI_RENDERER_URL as string) ?? 'https://kroki.io';
|
||||||
|
export const debounceEnabled: boolean = import.meta.env.MERMAID_DISABLE_DEBOUNCE !== 'true';
|
||||||
|
|||||||
@@ -54,7 +54,6 @@ export const loadDataFromUrl = async (): Promise<void> => {
|
|||||||
updateCodeStore({
|
updateCodeStore({
|
||||||
...state,
|
...state,
|
||||||
autoSync: true,
|
autoSync: true,
|
||||||
updateDiagram: true,
|
updateDiagram: true
|
||||||
updateEditor: true
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -5,11 +5,11 @@ describe('migrations', () => {
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
window.localStorage.setItem(
|
window.localStorage.setItem(
|
||||||
'manualHistoryStore',
|
'manualHistoryStore',
|
||||||
'[{"state":{"code":"graph TD\\n A[Halloween] -->|Get money| B(Go shopping)","mermaid":"{\\n \\"theme\\": \\"dark\\"\\n}","updateEditor":false,"autoSync":true,"updateDiagram":false},"time":0,"type":"manual","name":"hollow-art"},{"state":{"code":"graph TD\\n A[Christmas] -->|Get money| B(Go shopping)","mermaid":"{\\n \\"theme\\": \\"dark\\"\\n}","updateEditor":true,"autoSync":true,"updateDiagram":true},"time":0,"type":"manual","name":"helpful-ocean"}]'
|
'[{"state":{"code":"graph TD\\n A[Halloween] -->|Get money| B(Go shopping)","mermaid":"{\\n \\"theme\\": \\"dark\\"\\n}","autoSync":true,"updateDiagram":false},"time":0,"type":"manual","name":"hollow-art"},{"state":{"code":"graph TD\\n A[Christmas] -->|Get money| B(Go shopping)","mermaid":"{\\n \\"theme\\": \\"dark\\"\\n}","autoSync":true,"updateDiagram":true},"time":0,"type":"manual","name":"helpful-ocean"}]'
|
||||||
);
|
);
|
||||||
window.localStorage.setItem(
|
window.localStorage.setItem(
|
||||||
'autoHistoryStore',
|
'autoHistoryStore',
|
||||||
'[{"state":{"code":"graph TD\\n A[New Year] -->|Get money| B(Go shopping)","mermaid":"{\\n \\"theme\\": \\"dark\\"\\n}","updateEditor":false,"autoSync":true,"updateDiagram":false},"time":0,"type":"auto","name":"barking-dog"},{"state":{"code":"graph TD\\n A[Christmas] -->|Get money| B(Go shopping)","mermaid":"{\\n \\"theme\\": \\"dark\\"\\n}","updateEditor":true,"autoSync":true,"updateDiagram":true},"time":0,"type":"manual","name":"needy-mosquito"}]'
|
'[{"state":{"code":"graph TD\\n A[New Year] -->|Get money| B(Go shopping)","mermaid":"{\\n \\"theme\\": \\"dark\\"\\n}","autoSync":true,"updateDiagram":false},"time":0,"type":"auto","name":"barking-dog"},{"state":{"code":"graph TD\\n A[Christmas] -->|Get money| B(Go shopping)","mermaid":"{\\n \\"theme\\": \\"dark\\"\\n}","autoSync":true,"updateDiagram":true},"time":0,"type":"manual","name":"needy-mosquito"}]'
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -13,19 +13,19 @@ describe('Serde tests', () => {
|
|||||||
|
|
||||||
it('should serialize and deserialize with default serde', () => {
|
it('should serialize and deserialize with default serde', () => {
|
||||||
expect(verifySerde(defaultState)).toMatchInlineSnapshot(
|
expect(verifySerde(defaultState)).toMatchInlineSnapshot(
|
||||||
'"pako:eNpVkM1qw0AMhF9F6JRC_AI-FBo7ySXQQnPz5iC8cnZp9oe1TAm2373rmEKik5j5ZhAasQ2ascRromjgXCsPeT6ayiTbi6P-AkXxPh1ZwAXP9wl2m2OA3oQYrb--rfxugaAaTwvGIMb6n3m1qkf-0_MEdXOiKCFenp3zb5hg39gvk-tfHZM4pw5NR2VHRUsJKkoPBLfoODmyOp8-LopCMexYYZlXzR0NN1Go_JzRIWoS3msrIWGuuvW8RRokfN99i6Wkgf-h2lL-hFvF-Q9-YFyS"'
|
'"pako:eNpVj81qw0AMhF9F6NRC_AI-BGK7zSXQQHLz5iBsObuk-8Naphjb7551fEl1EjPfiNGEjW8Zc7xHChqulXKQ5lCXOppeLPU3yLL9fGQB6x2PMxQfRw-99iEYd__c-GKFoJxOK8Yg2rjHslnlK__jeIaqPlEQH27vzvXPz_BVm7NO5_87OnJKfdcd5R1lDUUoKb4Q3KHlaMm0qfq0KgpFs2WFeVpb7mj4FYXKLQmlQfxldA3mEgfe4RBaEq4MpaftJi5PNtJU8w"'
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should serialize and deserialize with base64 serde', () => {
|
it('should serialize and deserialize with base64 serde', () => {
|
||||||
expect(verifySerde(defaultState, 'base64')).toMatchInlineSnapshot(
|
expect(verifySerde(defaultState, 'base64')).toMatchInlineSnapshot(
|
||||||
'"base64:eyJjb2RlIjoiZ3JhcGggVERcbiAgICBBW0NocmlzdG1hc10gLS0-fEdldCBtb25leXwgQihHbyBzaG9wcGluZylcbiAgICBCIC0tPiBDe0xldCBtZSB0aGlua31cbiAgICBDIC0tPnxPbmV8IERbTGFwdG9wXVxuICAgIEMgLS0-fFR3b3wgRVtpUGhvbmVdXG4gICAgQyAtLT58VGhyZWV8IEZbZmE6ZmEtY2FyIENhcl1cbiAgIiwibWVybWFpZCI6IntcbiAgXCJ0aGVtZVwiOiBcImRlZmF1bHRcIlxufSIsInVwZGF0ZUVkaXRvciI6ZmFsc2UsImF1dG9TeW5jIjp0cnVlLCJ1cGRhdGVEaWFncmFtIjp0cnVlfQ"'
|
'"base64:eyJjb2RlIjoiZ3JhcGggVERcbiAgICBBW0NocmlzdG1hc10gLS0-fEdldCBtb25leXwgQihHbyBzaG9wcGluZylcbiAgICBCIC0tPiBDe0xldCBtZSB0aGlua31cbiAgICBDIC0tPnxPbmV8IERbTGFwdG9wXVxuICAgIEMgLS0-fFR3b3wgRVtpUGhvbmVdXG4gICAgQyAtLT58VGhyZWV8IEZbZmE6ZmEtY2FyIENhcl1cbiAgIiwibWVybWFpZCI6IntcbiAgXCJ0aGVtZVwiOiBcImRlZmF1bHRcIlxufSIsImF1dG9TeW5jIjp0cnVlLCJ1cGRhdGVEaWFncmFtIjp0cnVlfQ"'
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should serialize and deserialize with pako serde', () => {
|
it('should serialize and deserialize with pako serde', () => {
|
||||||
expect(verifySerde(defaultState, 'pako')).toMatchInlineSnapshot(
|
expect(verifySerde(defaultState, 'pako')).toMatchInlineSnapshot(
|
||||||
'"pako:eNpVkM1qw0AMhF9F6JRC_AI-FBo7ySXQQnPz5iC8cnZp9oe1TAm2373rmEKik5j5ZhAasQ2ascRromjgXCsPeT6ayiTbi6P-AkXxPh1ZwAXP9wl2m2OA3oQYrb--rfxugaAaTwvGIMb6n3m1qkf-0_MEdXOiKCFenp3zb5hg39gvk-tfHZM4pw5NR2VHRUsJKkoPBLfoODmyOp8-LopCMexYYZlXzR0NN1Go_JzRIWoS3msrIWGuuvW8RRokfN99i6Wkgf-h2lL-hFvF-Q9-YFyS"'
|
'"pako:eNpVj81qw0AMhF9F6NRC_AI-BGK7zSXQQHLz5iBsObuk-8Naphjb7551fEl1EjPfiNGEjW8Zc7xHChqulXKQ5lCXOppeLPU3yLL9fGQB6x2PMxQfRw-99iEYd__c-GKFoJxOK8Yg2rjHslnlK__jeIaqPlEQH27vzvXPz_BVm7NO5_87OnJKfdcd5R1lDUUoKb4Q3KHlaMm0qfq0KgpFs2WFeVpb7mj4FYXKLQmlQfxldA3mEgfe4RBaEq4MpaftJi5PNtJU8w"'
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+10
-9
@@ -23,7 +23,6 @@ export const defaultState: State = {
|
|||||||
null,
|
null,
|
||||||
2
|
2
|
||||||
),
|
),
|
||||||
updateEditor: false,
|
|
||||||
autoSync: true,
|
autoSync: true,
|
||||||
updateDiagram: true
|
updateDiagram: true
|
||||||
};
|
};
|
||||||
@@ -48,8 +47,10 @@ export const stateStore: Readable<ValidatedState> = derived([inputStateStore], (
|
|||||||
...state,
|
...state,
|
||||||
serialized: '',
|
serialized: '',
|
||||||
errorMarkers: [],
|
errorMarkers: [],
|
||||||
error: undefined
|
error: undefined,
|
||||||
|
editorMode: state.editorMode ?? 'code'
|
||||||
};
|
};
|
||||||
|
|
||||||
// No changes should be done to fields part of `state`.
|
// No changes should be done to fields part of `state`.
|
||||||
try {
|
try {
|
||||||
processed.serialized = serializeState(state);
|
processed.serialized = serializeState(state);
|
||||||
@@ -102,11 +103,12 @@ export const loadState = (data: string): void => {
|
|||||||
state.mermaid = defaultState.mermaid;
|
state.mermaid = defaultState.mermaid;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
updateCodeStore({ ...state, updateEditor: true });
|
updateCodeStore({ ...state });
|
||||||
};
|
};
|
||||||
|
|
||||||
export const updateCodeStore = (newState: Partial<State>): void => {
|
export const updateCodeStore = (newState: Partial<State>): void => {
|
||||||
inputStateStore.update((state) => {
|
inputStateStore.update((state) => {
|
||||||
|
// console.log({ newState, state });
|
||||||
return { ...state, ...newState };
|
return { ...state, ...newState };
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -115,10 +117,9 @@ let prompted = false;
|
|||||||
export const updateCode = (
|
export const updateCode = (
|
||||||
code: string,
|
code: string,
|
||||||
{
|
{
|
||||||
updateEditor,
|
|
||||||
updateDiagram = false,
|
updateDiagram = false,
|
||||||
resetPanZoom = false
|
resetPanZoom = false
|
||||||
}: { updateEditor: boolean; updateDiagram?: boolean; resetPanZoom?: boolean }
|
}: { updateDiagram?: boolean; resetPanZoom?: boolean } = {}
|
||||||
): void => {
|
): void => {
|
||||||
saveStatistics(code);
|
saveStatistics(code);
|
||||||
const lines = (code.match(/\n/g) || '').length + 1;
|
const lines = (code.match(/\n/g) || '').length + 1;
|
||||||
@@ -140,13 +141,13 @@ export const updateCode = (
|
|||||||
state.pan = undefined;
|
state.pan = undefined;
|
||||||
state.zoom = undefined;
|
state.zoom = undefined;
|
||||||
}
|
}
|
||||||
return { ...state, code, updateEditor, updateDiagram };
|
return { ...state, code, updateDiagram };
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const updateConfig = (config: string, updateEditor: boolean): void => {
|
export const updateConfig = (config: string): void => {
|
||||||
inputStateStore.update((state) => {
|
inputStateStore.update((state) => {
|
||||||
return { ...state, mermaid: config, updateEditor };
|
return { ...state, mermaid: config };
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -157,7 +158,7 @@ export const toggleDarkTheme = (dark: boolean): void => {
|
|||||||
config.theme = dark ? 'dark' : 'default';
|
config.theme = dark ? 'dark' : 'default';
|
||||||
}
|
}
|
||||||
|
|
||||||
return { ...state, mermaid: JSON.stringify(config, null, 2), updateEditor: true };
|
return { ...state, mermaid: JSON.stringify(config, null, 2) };
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { initURLSubscription, loadState, updateCodeStore } from './state';
|
|||||||
import { analytics, initAnalytics } from './stats';
|
import { analytics, initAnalytics } from './stats';
|
||||||
import { loadDataFromUrl } from './fileLoaders/loader';
|
import { loadDataFromUrl } from './fileLoaders/loader';
|
||||||
import { initLoading } from './loading';
|
import { initLoading } from './loading';
|
||||||
|
import { applyMigrations } from './migrations';
|
||||||
|
|
||||||
export const loadStateFromURL = (): void => {
|
export const loadStateFromURL = (): void => {
|
||||||
loadState(window.location.hash.slice(1));
|
loadState(window.location.hash.slice(1));
|
||||||
@@ -14,6 +15,7 @@ export const syncDiagram = (): void => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const initHandler = async (): Promise<void> => {
|
export const initHandler = async (): Promise<void> => {
|
||||||
|
applyMigrations();
|
||||||
loadStateFromURL();
|
loadStateFromURL();
|
||||||
await initLoading('Loading Gist...', loadDataFromUrl().catch(console.error));
|
await initLoading('Loading Gist...', loadDataFromUrl().catch(console.error));
|
||||||
syncDiagram();
|
syncDiagram();
|
||||||
@@ -24,5 +26,3 @@ export const initHandler = async (): Promise<void> => {
|
|||||||
|
|
||||||
export const isMac = navigator.platform.toUpperCase().indexOf('MAC') >= 0;
|
export const isMac = navigator.platform.toUpperCase().indexOf('MAC') >= 0;
|
||||||
export const cmdKey = isMac ? 'Cmd' : 'Ctrl';
|
export const cmdKey = isMac ? 'Cmd' : 'Ctrl';
|
||||||
|
|
||||||
export const debounceEnabled = window.localStorage.getItem('noDebounce') !== 'true';
|
|
||||||
|
|||||||
@@ -6,12 +6,10 @@
|
|||||||
import { setTheme, themeStore } from '$lib/util/theme';
|
import { setTheme, themeStore } from '$lib/util/theme';
|
||||||
import { toggleDarkTheme } from '$lib/util/state';
|
import { toggleDarkTheme } from '$lib/util/state';
|
||||||
import { initHandler } from '$lib/util/util';
|
import { initHandler } from '$lib/util/util';
|
||||||
import { applyMigrations } from '$lib/util/migrations';
|
|
||||||
|
|
||||||
// This can be removed once https://github.com/sveltejs/kit/issues/1612 is fixed.
|
// This can be removed once https://github.com/sveltejs/kit/issues/1612 is fixed.
|
||||||
// Then move it into src and vite will bundle it automatically.
|
// Then move it into src and vite will bundle it automatically.
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
applyMigrations();
|
|
||||||
window.addEventListener('hashchange', async (ev) => {
|
window.addEventListener('hashchange', async (ev) => {
|
||||||
await initHandler();
|
await initHandler();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -6,20 +6,12 @@
|
|||||||
import View from '$lib/components/view.svelte';
|
import View from '$lib/components/view.svelte';
|
||||||
import Card from '$lib/components/card/card.svelte';
|
import Card from '$lib/components/card/card.svelte';
|
||||||
import History from '$lib/components/history/history.svelte';
|
import History from '$lib/components/history/history.svelte';
|
||||||
import { updateCode, updateConfig, inputStateStore, stateStore } from '$lib/util/state';
|
import { inputStateStore, stateStore, updateCodeStore } from '$lib/util/state';
|
||||||
import { cmdKey, debounceEnabled, initHandler, syncDiagram } from '$lib/util/util';
|
import { cmdKey, initHandler, syncDiagram } from '$lib/util/util';
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
import type { EditorUpdateEvent, State, Tab, DocConfig } from '$lib/types';
|
import type { Tab, DocConfig, EditorMode, ValidatedState } from '$lib/types';
|
||||||
import { base } from '$app/paths';
|
import { base } from '$app/paths';
|
||||||
|
|
||||||
type Modes = 'code' | 'config';
|
|
||||||
type Languages = 'mermaid' | 'json';
|
|
||||||
|
|
||||||
let selectedMode: Modes = 'code';
|
|
||||||
const languageMap: { [key in Modes]: Languages } = {
|
|
||||||
code: 'mermaid',
|
|
||||||
config: 'json'
|
|
||||||
};
|
|
||||||
const docURLBase = 'https://mermaid-js.github.io/mermaid';
|
const docURLBase = 'https://mermaid-js.github.io/mermaid';
|
||||||
const docMap: DocConfig = {
|
const docMap: DocConfig = {
|
||||||
graph: {
|
graph: {
|
||||||
@@ -60,34 +52,23 @@
|
|||||||
config: '/#/gitgraph?id=gitgraph-specific-configuration-options'
|
config: '/#/gitgraph?id=gitgraph-specific-configuration-options'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let text = '';
|
|
||||||
let docURL = docURLBase;
|
let docURL = docURLBase;
|
||||||
let language: Languages = 'mermaid';
|
let activeTabID = 'code';
|
||||||
const handleModeUpdate = (mode: Modes) => {
|
stateStore.subscribe(({ code, editorMode }: ValidatedState) => {
|
||||||
if (mode === 'code') {
|
activeTabID = editorMode;
|
||||||
text = $stateStore.code;
|
const codeTypeMatch = /([\S]+)[\s\n]/.exec(code);
|
||||||
} else {
|
|
||||||
text = $stateStore.mermaid;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
$: language = languageMap[selectedMode];
|
|
||||||
$: handleModeUpdate(selectedMode);
|
|
||||||
|
|
||||||
stateStore.subscribe((state: State) => {
|
|
||||||
if (state.updateEditor) {
|
|
||||||
text = selectedMode === 'code' ? state.code : state.mermaid;
|
|
||||||
}
|
|
||||||
const codeTypeMatch = /([\S]+)[\s\n]/.exec(state.code);
|
|
||||||
if (codeTypeMatch && codeTypeMatch.length > 1) {
|
if (codeTypeMatch && codeTypeMatch.length > 1) {
|
||||||
const docKey = codeTypeMatch[1];
|
const docKey = codeTypeMatch[1];
|
||||||
const docConfig = docMap[docKey] ?? { code: '' };
|
const docConfig = docMap[docKey] ?? { code: '' };
|
||||||
docURL = docURLBase + (docConfig[selectedMode] ?? docConfig.code ?? '');
|
docURL = docURLBase + (docConfig[editorMode] ?? docConfig.code ?? '');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const tabSelectHandler = (message: CustomEvent<Tab>) => {
|
const tabSelectHandler = (message: CustomEvent<Tab>) => {
|
||||||
selectedMode = message.detail.id === 'code' ? 'code' : 'config';
|
const editorMode: EditorMode = message.detail.id === 'code' ? 'code' : 'config';
|
||||||
$inputStateStore.updateEditor = true;
|
updateCodeStore({ editorMode });
|
||||||
};
|
};
|
||||||
|
|
||||||
const tabs: Tab[] = [
|
const tabs: Tab[] = [
|
||||||
{
|
{
|
||||||
id: 'code',
|
id: 'code',
|
||||||
@@ -101,28 +82,6 @@
|
|||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
const handleUpdate = (text: string) => {
|
|
||||||
if (selectedMode === 'code') {
|
|
||||||
updateCode(text, {
|
|
||||||
updateEditor: false
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
updateConfig(text, false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let debounce: { [key: string]: number } = {};
|
|
||||||
const updateHandler = ({ detail: { text } }: CustomEvent<EditorUpdateEvent>) => {
|
|
||||||
if (debounceEnabled) {
|
|
||||||
clearTimeout(debounce[selectedMode]);
|
|
||||||
debounce[selectedMode] = window.setTimeout(() => {
|
|
||||||
handleUpdate(text);
|
|
||||||
}, 300);
|
|
||||||
} else {
|
|
||||||
handleUpdate(text);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
await initHandler();
|
await initHandler();
|
||||||
const resizer = document.getElementById('resizeHandler');
|
const resizer = document.getElementById('resizeHandler');
|
||||||
@@ -149,7 +108,7 @@
|
|||||||
<Navbar />
|
<Navbar />
|
||||||
<div class="flex-1 flex overflow-hidden">
|
<div class="flex-1 flex overflow-hidden">
|
||||||
<div class="hidden md:flex flex-col" id="editorPane" style="width: 40%">
|
<div class="hidden md:flex flex-col" id="editorPane" style="width: 40%">
|
||||||
<Card on:select={tabSelectHandler} {tabs} isCloseable={false} title="Mermaid">
|
<Card on:select={tabSelectHandler} {tabs} isCloseable={false} {activeTabID} title="Mermaid">
|
||||||
<div slot="actions" class="flex flex-row items-center">
|
<div slot="actions" class="flex flex-row items-center">
|
||||||
<div class="form-control flex-row items-center">
|
<div class="form-control flex-row items-center">
|
||||||
<label class="cursor-pointer label" for="autoSync">
|
<label class="cursor-pointer label" for="autoSync">
|
||||||
@@ -175,7 +134,7 @@
|
|||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Editor on:update={updateHandler} {language} {text} />
|
<Editor />
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
<div class="-mt-2">
|
<div class="-mt-2">
|
||||||
|
|||||||
Reference in New Issue
Block a user