Merge pull request #1636 from mermaid-js/release-promotion
Release live editor
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
import { inputStateStore, stateStore, updateCodeStore } from '$lib/util/state';
|
import { inputStateStore, stateStore, updateCodeStore } from '$lib/util/state';
|
||||||
import { logEvent, saveStatistics } from '$lib/util/stats';
|
import { logEvent, saveStatistics } from '$lib/util/stats';
|
||||||
import { cmdKey } from '$lib/util/util';
|
import { cmdKey } from '$lib/util/util';
|
||||||
|
import uniqueID from 'lodash-es/uniqueId';
|
||||||
import type { MermaidConfig } from 'mermaid';
|
import type { MermaidConfig } from 'mermaid';
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
import panzoom from 'svg-pan-zoom';
|
import panzoom from 'svg-pan-zoom';
|
||||||
@@ -17,7 +18,6 @@
|
|||||||
let view: HTMLDivElement | undefined = $state();
|
let view: HTMLDivElement | undefined = $state();
|
||||||
let error = $state(false);
|
let error = $state(false);
|
||||||
let outOfSync = $state(false);
|
let outOfSync = $state(false);
|
||||||
let hide = $state(false);
|
|
||||||
let manualUpdate = true;
|
let manualUpdate = true;
|
||||||
let panZoomEnabled = $stateStore.panZoom;
|
let panZoomEnabled = $stateStore.panZoom;
|
||||||
let pzoom: typeof panzoom | undefined;
|
let pzoom: typeof panzoom | undefined;
|
||||||
@@ -31,18 +31,13 @@
|
|||||||
logEvent('panZoom');
|
logEvent('panZoom');
|
||||||
};
|
};
|
||||||
|
|
||||||
const handlePanZoom = (state: State) => {
|
const handlePanZoom = (state: State, graphDiv: SVGSVGElement) => {
|
||||||
if (!state.panZoom) {
|
if (!state.panZoom) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
hide = true;
|
|
||||||
pzoom?.destroy();
|
pzoom?.destroy();
|
||||||
pzoom = undefined;
|
pzoom = undefined;
|
||||||
void Promise.resolve().then(() => {
|
void Promise.resolve().then(() => {
|
||||||
const graphDiv = document.querySelector<HTMLElement>('#graph-div');
|
|
||||||
if (!graphDiv) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
pzoom = panzoom(graphDiv, {
|
pzoom = panzoom(graphDiv, {
|
||||||
onPan: handlePanZoomChange,
|
onPan: handlePanZoomChange,
|
||||||
onZoom: handlePanZoomChange,
|
onZoom: handlePanZoomChange,
|
||||||
@@ -55,7 +50,6 @@
|
|||||||
pzoom.zoom(zoom);
|
pzoom.zoom(zoom);
|
||||||
pzoom.pan(pan);
|
pzoom.pan(pan);
|
||||||
}
|
}
|
||||||
hide = false;
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -95,20 +89,16 @@
|
|||||||
rough = state.rough;
|
rough = state.rough;
|
||||||
const scroll = view?.parentElement?.scrollTop;
|
const scroll = view?.parentElement?.scrollTop;
|
||||||
delete container.dataset.processed;
|
delete container.dataset.processed;
|
||||||
|
const viewID = uniqueID('graph-');
|
||||||
const {
|
const {
|
||||||
svg,
|
svg,
|
||||||
bindFunctions,
|
bindFunctions,
|
||||||
diagramType: detectedDiagramType
|
diagramType: detectedDiagramType
|
||||||
} = await renderDiagram(
|
} = await renderDiagram(JSON.parse(state.mermaid) as MermaidConfig, code, viewID);
|
||||||
Object.assign({}, JSON.parse(state.mermaid)) as MermaidConfig,
|
|
||||||
code,
|
|
||||||
'graph-div'
|
|
||||||
);
|
|
||||||
diagramType = detectedDiagramType;
|
diagramType = detectedDiagramType;
|
||||||
if (svg.length > 0) {
|
if (svg.length > 0) {
|
||||||
handlePanZoom(state);
|
|
||||||
container.innerHTML = svg;
|
container.innerHTML = svg;
|
||||||
const graphDiv = document.querySelector<SVGSVGElement>('#graph-div');
|
let graphDiv = document.querySelector<SVGSVGElement>(`#${viewID}`);
|
||||||
if (!graphDiv) {
|
if (!graphDiv) {
|
||||||
throw new Error('graph-div not found');
|
throw new Error('graph-div not found');
|
||||||
}
|
}
|
||||||
@@ -117,7 +107,7 @@
|
|||||||
svg2roughjs.svg = graphDiv;
|
svg2roughjs.svg = graphDiv;
|
||||||
await svg2roughjs.sketch();
|
await svg2roughjs.sketch();
|
||||||
graphDiv.remove();
|
graphDiv.remove();
|
||||||
const sketch = document.querySelector<HTMLElement>('#container > svg');
|
const sketch = document.querySelector<SVGSVGElement>('#container > svg');
|
||||||
if (!sketch) {
|
if (!sketch) {
|
||||||
throw new Error('sketch not found');
|
throw new Error('sketch not found');
|
||||||
}
|
}
|
||||||
@@ -127,6 +117,7 @@
|
|||||||
sketch.setAttribute('width', '100%');
|
sketch.setAttribute('width', '100%');
|
||||||
sketch.setAttribute('viewBox', `0 0 ${width} ${height}`);
|
sketch.setAttribute('viewBox', `0 0 ${width} ${height}`);
|
||||||
sketch.style.maxWidth = '100%';
|
sketch.style.maxWidth = '100%';
|
||||||
|
graphDiv = sketch;
|
||||||
} else {
|
} else {
|
||||||
graphDiv.setAttribute('height', '100%');
|
graphDiv.setAttribute('height', '100%');
|
||||||
graphDiv.style.maxWidth = '100%';
|
graphDiv.style.maxWidth = '100%';
|
||||||
@@ -134,6 +125,7 @@
|
|||||||
bindFunctions(graphDiv);
|
bindFunctions(graphDiv);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
handlePanZoom(state, graphDiv);
|
||||||
}
|
}
|
||||||
if (view?.parentElement && scroll) {
|
if (view?.parentElement && scroll) {
|
||||||
view.parentElement.scrollTop = scroll;
|
view.parentElement.scrollTop = scroll;
|
||||||
@@ -183,24 +175,15 @@
|
|||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
<div id="view" bind:this={view} class="h-full p-2" class:error class:outOfSync>
|
<div id="view" bind:this={view} class="h-full p-2" class:error class:outOfSync>
|
||||||
<div id="container" bind:this={container} class="h-full overflow-auto" class:hide></div>
|
<div id="container" bind:this={container} class="h-full overflow-auto"></div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
#view {
|
#view {
|
||||||
flex: 1;
|
flex: 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#container {
|
|
||||||
transition: visibility 0.3s;
|
|
||||||
}
|
|
||||||
|
|
||||||
.error,
|
.error,
|
||||||
.outOfSync {
|
.outOfSync {
|
||||||
opacity: 0.5;
|
opacity: 0.5;
|
||||||
}
|
}
|
||||||
|
|
||||||
.hide {
|
|
||||||
visibility: hidden;
|
|
||||||
}
|
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -192,7 +192,7 @@
|
|||||||
<div class="flex flex-row items-center gap-2">
|
<div class="flex flex-row items-center gap-2">
|
||||||
<label
|
<label
|
||||||
class="label flex cursor-pointer gap-1 py-0"
|
class="label flex cursor-pointer gap-1 py-0"
|
||||||
title="Rough mode is in beta. Features like clickable nodes, Pan & Zoom, will be disabled."
|
title="Rough mode is in beta. Features like clickable nodes and ZenUML diagram will not work."
|
||||||
for="rough">
|
for="rough">
|
||||||
<span>Rough</span>
|
<span>Rough</span>
|
||||||
<input
|
<input
|
||||||
@@ -201,16 +201,12 @@
|
|||||||
id="rough"
|
id="rough"
|
||||||
bind:checked={$inputStateStore.rough} />
|
bind:checked={$inputStateStore.rough} />
|
||||||
</label>
|
</label>
|
||||||
<label
|
<label class="label flex cursor-pointer gap-1 py-0" for="panZoom">
|
||||||
class="label flex cursor-pointer gap-1 py-0"
|
|
||||||
title={$stateStore.rough ? 'Pan & Zoom is disabled in rough mode.' : ''}
|
|
||||||
for="panZoom">
|
|
||||||
<span>Pan & Zoom</span>
|
<span>Pan & Zoom</span>
|
||||||
<input
|
<input
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
class="toggle {$stateStore.panZoom ? 'btn-secondary' : 'toggle-primary'}"
|
class="toggle {$stateStore.panZoom ? 'btn-secondary' : 'toggle-primary'}"
|
||||||
id="panZoom"
|
id="panZoom"
|
||||||
disabled={$stateStore.rough}
|
|
||||||
bind:checked={$inputStateStore.panZoom} />
|
bind:checked={$inputStateStore.panZoom} />
|
||||||
</label>
|
</label>
|
||||||
<a
|
<a
|
||||||
|
|||||||
Reference in New Issue
Block a user