likec4 0.49.0 → 0.51.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/@likec4/diagrams/diagram/Diagram.js +55 -27
- package/dist/@likec4/diagrams/diagram/Edges.js +14 -11
- package/dist/@likec4/diagrams/diagram/Nodes.js +212 -23
- package/dist/@likec4/diagrams/diagram/icons/ZoomIn.js +2 -1
- package/dist/@likec4/diagrams/diagram/shapes/Compound.js +2 -1
- package/dist/@likec4/diagrams/diagram/shapes/Edge.js +1 -1
- package/dist/@likec4/diagrams/diagram/state/atoms.js +35 -60
- package/dist/@likec4/diagrams/diagram/utils.js +14 -0
- package/dist/@likec4/diagrams/hooks/useDiagramApi.js +4 -5
- package/dist/@likec4/diagrams/hooks/useImageLoader.js +1 -1
- package/dist/__app__/likec4.css +8 -5
- package/dist/__app__/src/App.jsx +9 -11
- package/dist/__app__/src/components/CopyToClipboard.jsx +29 -0
- package/dist/__app__/src/components/CopyToClipboard.module.css +16 -0
- package/dist/__app__/src/components/DiagramNotFound.jsx +2 -2
- package/dist/__app__/src/components/index.js +2 -1
- package/dist/__app__/src/components/sidebar/Sidebar.jsx +1 -1
- package/dist/__app__/src/components/sidebar/styles.module.css +4 -1
- package/dist/__app__/src/components/view-page/DisplayModeSelector.jsx +15 -9
- package/dist/__app__/src/components/view-page/Header.jsx +97 -0
- package/dist/__app__/src/components/view-page/Header.module.css +24 -0
- package/dist/__app__/src/components/view-page/ShareDialog.jsx +23 -15
- package/dist/__app__/src/components/view-page/ViewActions.jsx +69 -0
- package/dist/__app__/src/data/atoms.js +4 -22
- package/dist/__app__/src/data/hooks.js +5 -4
- package/dist/__app__/src/data/index-page.js +22 -0
- package/dist/__app__/src/likec4-views.js +1 -1
- package/dist/__app__/src/pages/index-page/index.jsx +99 -0
- package/dist/__app__/src/pages/index-page/index.module.css +20 -0
- package/dist/__app__/src/pages/index.js +1 -1
- package/dist/__app__/src/pages/useTransparentBackground.js +2 -2
- package/dist/__app__/src/pages/view-page/ViewAsReact.jsx +60 -0
- package/dist/__app__/src/pages/view-page/index.js +11 -0
- package/dist/__app__/src/pages/view-page/other-formats/ViewAsD2.jsx +18 -0
- package/dist/__app__/src/pages/view-page/other-formats/ViewAsDot.jsx +33 -0
- package/dist/__app__/src/pages/view-page/other-formats/ViewAsMmd.jsx +18 -0
- package/dist/__app__/src/pages/view-page/other-formats.jsx +43 -0
- package/dist/__app__/src/pages/view-page/view-page.module.css +81 -0
- package/dist/__app__/src/pages/view.page.jsx +12 -65
- package/dist/__app__/src/router.js +90 -20
- package/dist/__app__/src/utils/utils.js +1 -2
- package/dist/__app__/tsconfig.json +1 -0
- package/dist/cli/index.js +286 -212
- package/package.json +19 -20
- package/dist/__app__/postcss.config.cjs +0 -11
- package/dist/__app__/src/components/view-page/ViewActionsToolbar.jsx +0 -66
- package/dist/__app__/src/pages/index.module.css +0 -11
- package/dist/__app__/src/pages/index.page.jsx +0 -57
- package/dist/__app__/src/pages/view-page.module.css +0 -30
- package/dist/__app__/tailwind.config.cjs +0 -17
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
.header {
|
|
2
|
+
background: var(--color-surface);
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
.dimmed {
|
|
6
|
+
color: var(--color-text-dimmed);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
.previewBg {
|
|
10
|
+
overflow: hidden;
|
|
11
|
+
touch-action: none;
|
|
12
|
+
user-select: none;
|
|
13
|
+
pointer-events: none;
|
|
14
|
+
padding: 0;
|
|
15
|
+
margin: 0;
|
|
16
|
+
background-origin: padding-box;
|
|
17
|
+
background-image: radial-gradient(var(--gray-a5) 15%, var(--color-panel-translucent) 15%);
|
|
18
|
+
background-position: 0 0;
|
|
19
|
+
background-size: 12px 12px;
|
|
20
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useIsomorphicLayoutEffect } from '@react-hookz/web/esm';
|
|
2
2
|
// To get the transparent background
|
|
3
3
|
// We need to add a class to the HTML element
|
|
4
4
|
export function useTransparentBackground(enabled = true) {
|
|
5
|
-
|
|
5
|
+
useIsomorphicLayoutEffect(() => {
|
|
6
6
|
const htmlEl = document.body.parentElement;
|
|
7
7
|
if (!htmlEl || enabled !== true)
|
|
8
8
|
return;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Diagram, useDiagramApi } from '@likec4/diagrams';
|
|
2
|
+
import { Box } from '@radix-ui/themes';
|
|
3
|
+
import { useWindowSize } from '@react-hookz/web/esm';
|
|
4
|
+
import { useCallback, useEffect, useRef } from 'react';
|
|
5
|
+
import { $pages } from '../../router';
|
|
6
|
+
import { cn } from '../../utils';
|
|
7
|
+
import styles from './view-page.module.css';
|
|
8
|
+
const Paddings = [70, 20, 20, 40];
|
|
9
|
+
function round(n, d = 1) {
|
|
10
|
+
const m = Math.pow(10, d);
|
|
11
|
+
return Math.round(n * m) / m;
|
|
12
|
+
}
|
|
13
|
+
export function ViewAsReact({ diagram }) {
|
|
14
|
+
const { width, height } = useWindowSize();
|
|
15
|
+
const pageDivRef = useRef(null);
|
|
16
|
+
const [ref, api] = useDiagramApi();
|
|
17
|
+
const handleTransform = useCallback(() => {
|
|
18
|
+
const stage = api.stage;
|
|
19
|
+
const style = pageDivRef.current?.style;
|
|
20
|
+
if (!stage || !style) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const pos = stage.getAbsolutePosition();
|
|
24
|
+
let scale = stage.scaleX();
|
|
25
|
+
while (scale < 0.5) {
|
|
26
|
+
scale = scale / 0.5;
|
|
27
|
+
}
|
|
28
|
+
const size = round(24 * scale);
|
|
29
|
+
const x = round(pos.x);
|
|
30
|
+
const y = round(pos.y);
|
|
31
|
+
style.setProperty('--diagram-bg-size', `${size}px`);
|
|
32
|
+
style.setProperty('--diagram-bg-position-x', `${x}px`);
|
|
33
|
+
style.setProperty('--diagram-bg-position-y', `${y}px`);
|
|
34
|
+
}, [api, pageDivRef]);
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
const stage = api.stage;
|
|
37
|
+
if (!stage) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
stage.on('absoluteTransformChange', handleTransform);
|
|
41
|
+
handleTransform();
|
|
42
|
+
return () => {
|
|
43
|
+
stage.off('absoluteTransformChange', handleTransform);
|
|
44
|
+
};
|
|
45
|
+
}, [api]);
|
|
46
|
+
return (<Box position={'fixed'} inset='0' className={cn(styles.reactDiagram
|
|
47
|
+
// isActive && styles.active
|
|
48
|
+
)} ref={pageDivRef}>
|
|
49
|
+
<Diagram ref={ref} diagram={diagram} padding={Paddings} maxZoom={1.05} width={width} height={height} onNodeClick={node => {
|
|
50
|
+
if (node.navigateTo) {
|
|
51
|
+
$pages.view.open(node.navigateTo);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
api.centerOnNode(node, {
|
|
55
|
+
keepZoom: true
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}} onStageClick={_ => ({})} onEdgeClick={_ => ({})}/>
|
|
59
|
+
</Box>);
|
|
60
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import { lazy } from 'react';
|
|
3
|
+
export * from './ViewAsReact';
|
|
4
|
+
export const ViewAs = lazy(() => import('./other-formats'));
|
|
5
|
+
// export const ViewAs = {
|
|
6
|
+
// Dot: ViewAsDot,
|
|
7
|
+
// D2: ViewAsD2,
|
|
8
|
+
// Mmd: ViewAsMmd,
|
|
9
|
+
// } satisfies Record<Capitalize<Exclude<ViewMode, 'react'>>, React.FunctionComponent<any>>
|
|
10
|
+
// export const ViewAsReact = lazy(() => import('./ViewAsReact'))
|
|
11
|
+
// export const ViewAsReact = lazy(() => import('./ViewAsReact'))
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Box, Code, ScrollArea } from '@radix-ui/themes';
|
|
2
|
+
import { d2Source } from 'virtual:likec4/d2-sources';
|
|
3
|
+
import { CopyToClipboard } from '../../../components';
|
|
4
|
+
export default function ViewAsD2({ viewId }) {
|
|
5
|
+
const src = d2Source(viewId);
|
|
6
|
+
return (<>
|
|
7
|
+
<ScrollArea scrollbars='both'>
|
|
8
|
+
<Box asChild display={'block'} p='2' style={{
|
|
9
|
+
whiteSpace: 'pre'
|
|
10
|
+
}}>
|
|
11
|
+
<Code variant='soft' autoFocus>
|
|
12
|
+
{src}
|
|
13
|
+
</Code>
|
|
14
|
+
</Box>
|
|
15
|
+
</ScrollArea>
|
|
16
|
+
<CopyToClipboard text={src}/>
|
|
17
|
+
</>);
|
|
18
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Box, Code, Grid, ScrollArea } from '@radix-ui/themes';
|
|
2
|
+
import { dotSource, svgSource } from 'virtual:likec4/dot-sources';
|
|
3
|
+
import styles from '../view-page.module.css';
|
|
4
|
+
import { CopyToClipboard } from '../../../components';
|
|
5
|
+
export default function ViewAsDot({ viewId }) {
|
|
6
|
+
const dot = dotSource(viewId);
|
|
7
|
+
return (<Grid
|
|
8
|
+
//@ts-expect-error TODO: fails on columns prop due to `exactOptionalPropertyTypes: true` in tsconfig
|
|
9
|
+
columns='2' gap='2' shrink='1' grow='1'>
|
|
10
|
+
<Box py={'2'} position={'relative'} style={{
|
|
11
|
+
overflow: 'scroll'
|
|
12
|
+
}}>
|
|
13
|
+
<ScrollArea scrollbars='both'>
|
|
14
|
+
<Box asChild display={'block'} p='2' style={{
|
|
15
|
+
whiteSpace: 'pre'
|
|
16
|
+
}}>
|
|
17
|
+
<Code variant='soft' autoFocus>
|
|
18
|
+
{dot}
|
|
19
|
+
</Code>
|
|
20
|
+
</Box>
|
|
21
|
+
</ScrollArea>
|
|
22
|
+
<CopyToClipboard text={dot}/>
|
|
23
|
+
</Box>
|
|
24
|
+
<Box py={'2'} style={{
|
|
25
|
+
overflow: 'scroll',
|
|
26
|
+
overscrollBehavior: 'none'
|
|
27
|
+
}}>
|
|
28
|
+
<Box asChild position={'relative'} className={styles.dotSvg}>
|
|
29
|
+
<div dangerouslySetInnerHTML={{ __html: svgSource(viewId) }}></div>
|
|
30
|
+
</Box>
|
|
31
|
+
</Box>
|
|
32
|
+
</Grid>);
|
|
33
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Box, Code, ScrollArea } from '@radix-ui/themes';
|
|
2
|
+
import { mmdSource } from 'virtual:likec4/mmd-sources';
|
|
3
|
+
import { CopyToClipboard } from '../../../components';
|
|
4
|
+
export default function ViewAsMmd({ viewId }) {
|
|
5
|
+
const src = mmdSource(viewId);
|
|
6
|
+
return (<>
|
|
7
|
+
<ScrollArea scrollbars='both'>
|
|
8
|
+
<Box asChild display={'block'} p='2' style={{
|
|
9
|
+
whiteSpace: 'pre'
|
|
10
|
+
}}>
|
|
11
|
+
<Code variant='soft' autoFocus>
|
|
12
|
+
{src}
|
|
13
|
+
</Code>
|
|
14
|
+
</Box>
|
|
15
|
+
</ScrollArea>
|
|
16
|
+
<CopyToClipboard text={src}/>
|
|
17
|
+
</>);
|
|
18
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Box, Flex, Tabs } from '@radix-ui/themes';
|
|
2
|
+
import { updateSearchParams } from '../../router';
|
|
3
|
+
import ViewAsD2 from './other-formats/ViewAsD2';
|
|
4
|
+
import ViewAsDot from './other-formats/ViewAsDot';
|
|
5
|
+
import ViewAsMmd from './other-formats/ViewAsMmd';
|
|
6
|
+
import styles from './view-page.module.css';
|
|
7
|
+
export default function ViewDiagramInOtherFormats({ viewId, viewMode }) {
|
|
8
|
+
return (<Flex asChild position={'fixed'} inset={'0'} pt={'8'} pl={'8'} pr={'2'} align={'stretch'} direction={'column'}>
|
|
9
|
+
<Tabs.Root value={viewMode} onValueChange={mode => mode !== viewMode && updateSearchParams({ mode: mode })}>
|
|
10
|
+
<Box asChild shrink={'0'} grow={'0'}>
|
|
11
|
+
<Tabs.List>
|
|
12
|
+
<Tabs.Trigger value='dot'>Graphviz</Tabs.Trigger>
|
|
13
|
+
<Tabs.Trigger value='mmd'>Mermaid</Tabs.Trigger>
|
|
14
|
+
<Tabs.Trigger value='d2'>D2</Tabs.Trigger>
|
|
15
|
+
</Tabs.List>
|
|
16
|
+
</Box>
|
|
17
|
+
|
|
18
|
+
<Box p='2' className={styles.otherFormats} position={'relative'}>
|
|
19
|
+
<Tabs.Content value='dot'>
|
|
20
|
+
<ViewAsDot viewId={viewId}/>
|
|
21
|
+
</Tabs.Content>
|
|
22
|
+
|
|
23
|
+
<Tabs.Content value='mmd'>
|
|
24
|
+
<ViewAsMmd viewId={viewId}/>
|
|
25
|
+
</Tabs.Content>
|
|
26
|
+
|
|
27
|
+
<Tabs.Content value='d2'>
|
|
28
|
+
<ViewAsD2 viewId={viewId}/>
|
|
29
|
+
</Tabs.Content>
|
|
30
|
+
</Box>
|
|
31
|
+
</Tabs.Root>
|
|
32
|
+
</Flex>);
|
|
33
|
+
// switch (viewMode) {
|
|
34
|
+
// case 'dot':
|
|
35
|
+
// return <ViewAsDot viewId={viewId} />
|
|
36
|
+
// case 'd2':
|
|
37
|
+
// return <ViewAsD2 viewId={viewId} />
|
|
38
|
+
// case 'mmd':
|
|
39
|
+
// return <ViewAsMmd viewId={viewId} />
|
|
40
|
+
// default:
|
|
41
|
+
// nonexhaustive(viewMode)
|
|
42
|
+
// }
|
|
43
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
.reactDiagram {
|
|
2
|
+
--diagram-bg-size: 24px;
|
|
3
|
+
--diagram-bg-position-x: 0;
|
|
4
|
+
--diagram-bg-position-y: 0;
|
|
5
|
+
|
|
6
|
+
:global(.konvajs-content) {
|
|
7
|
+
|
|
8
|
+
&::before {
|
|
9
|
+
content: '';
|
|
10
|
+
position: absolute;
|
|
11
|
+
padding: 0;
|
|
12
|
+
margin: 0;
|
|
13
|
+
top: 0;
|
|
14
|
+
left: 0;
|
|
15
|
+
width: 100%;
|
|
16
|
+
height: 100%;
|
|
17
|
+
pointer-events: none;
|
|
18
|
+
touch-action: none;
|
|
19
|
+
user-select: none;
|
|
20
|
+
background-origin: border-box;
|
|
21
|
+
background-attachment: fixed;
|
|
22
|
+
background-image: radial-gradient(var(--gray-a3) 12%, transparent 12%);
|
|
23
|
+
background-position: var(--diagram-bg-position-x) var(--diagram-bg-position-y);
|
|
24
|
+
background-size: var(--diagram-bg-size) var(--diagram-bg-size);
|
|
25
|
+
z-index: -1;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/*
|
|
29
|
+
:where(:not(.dimmed)) :global(.konvajs-content) {
|
|
30
|
+
transition:
|
|
31
|
+
filter 200ms ease-out,
|
|
32
|
+
transform 500ms cubic-bezier(0.46, 0.08, 0.27, 0.95);
|
|
33
|
+
filter: none;
|
|
34
|
+
transform: none;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
:where(.dimmed) :global(.konvajs-content) {
|
|
38
|
+
|
|
39
|
+
// &::before {
|
|
40
|
+
// filter: blur(3px);
|
|
41
|
+
// transition-delay: 100ms;
|
|
42
|
+
// transition-timing-function: ease-in;
|
|
43
|
+
// }
|
|
44
|
+
|
|
45
|
+
filter: blur(3px);
|
|
46
|
+
transform: scale(0.95);
|
|
47
|
+
transition-delay: 100ms, 0ms;
|
|
48
|
+
transition-duration: 200ms, 900ms;
|
|
49
|
+
transition-timing-function: ease-in, cubic-bezier(0.46, 0.08, 0.27, 0.95);
|
|
50
|
+
} */
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/* .diagramPreview {
|
|
54
|
+
:global(.konvajs-content) {
|
|
55
|
+
background: var(--color-overlay);
|
|
56
|
+
}
|
|
57
|
+
} */
|
|
58
|
+
|
|
59
|
+
.dotSvg {
|
|
60
|
+
& > svg {
|
|
61
|
+
width: 100%;
|
|
62
|
+
height: auto;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
.otherFormats {
|
|
67
|
+
display: flex;
|
|
68
|
+
align-items: stretch;
|
|
69
|
+
flex: 1 1 auto;
|
|
70
|
+
overflow: scroll;
|
|
71
|
+
|
|
72
|
+
& > :global(.rt-TabsContent) {
|
|
73
|
+
display: flex;
|
|
74
|
+
align-items: stretch;
|
|
75
|
+
flex: 1 1 auto;
|
|
76
|
+
overflow: scroll;
|
|
77
|
+
&[hidden] {
|
|
78
|
+
display: none;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -1,71 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { $pages } from '../router';
|
|
5
|
-
import { DiagramNotFound, ViewActionsToolbar } from '../components';
|
|
1
|
+
import { Suspense } from 'react';
|
|
2
|
+
import { DiagramNotFound } from '../components';
|
|
3
|
+
import { Header } from '../components/view-page/Header';
|
|
6
4
|
import { useLikeC4View } from '../data';
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
const Paddings = [70, 20, 20, 40];
|
|
10
|
-
function round(n, d = 1) {
|
|
11
|
-
const m = Math.pow(10, d);
|
|
12
|
-
return Math.round(n * m) / m;
|
|
13
|
-
}
|
|
14
|
-
export function ViewPage({ viewId, showUI = true }) {
|
|
15
|
-
const { width, height } = useWindowSize();
|
|
5
|
+
import { ViewAs, ViewAsReact } from './view-page';
|
|
6
|
+
export function ViewPage({ viewId, viewMode, showUI = true }) {
|
|
16
7
|
const diagram = useLikeC4View(viewId);
|
|
17
|
-
const pageDivRef = useRef(null);
|
|
18
|
-
const [ref, api] = useDiagramApi();
|
|
19
|
-
const handleTransform = useCallback(() => {
|
|
20
|
-
const stage = api.stage;
|
|
21
|
-
const style = pageDivRef.current?.style;
|
|
22
|
-
if (!stage || !style) {
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
const pos = stage.getAbsolutePosition();
|
|
26
|
-
let scale = stage.scaleX();
|
|
27
|
-
while (scale < 0.5) {
|
|
28
|
-
scale = scale / 0.5;
|
|
29
|
-
}
|
|
30
|
-
const size = round(24 * scale);
|
|
31
|
-
const x = round(pos.x);
|
|
32
|
-
const y = round(pos.y);
|
|
33
|
-
style.setProperty('--diagram-bg-size', `${size}px`);
|
|
34
|
-
style.setProperty('--diagram-bg-position-x', `${x}px`);
|
|
35
|
-
style.setProperty('--diagram-bg-position-y', `${y}px`);
|
|
36
|
-
}, [api, pageDivRef]);
|
|
37
|
-
useEffect(() => {
|
|
38
|
-
const stage = api.stage;
|
|
39
|
-
if (!stage) {
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
stage.on('absoluteTransformChange', handleTransform);
|
|
43
|
-
handleTransform();
|
|
44
|
-
return () => {
|
|
45
|
-
stage.off('absoluteTransformChange', handleTransform);
|
|
46
|
-
};
|
|
47
|
-
}, [api]);
|
|
48
8
|
if (!diagram) {
|
|
49
9
|
return <DiagramNotFound viewId={viewId}/>;
|
|
50
10
|
}
|
|
51
|
-
return (
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
<Flex position={'fixed'} top='0' p='3' style={{
|
|
59
|
-
left: 54
|
|
60
|
-
}} direction={'column'}>
|
|
61
|
-
<Text size={'1'} trim={'start'} color='gray' as='div' className='whitespace-nowrap select-none'>
|
|
62
|
-
id: <span className='select-all'>{diagram.id}</span>
|
|
63
|
-
</Text>
|
|
64
|
-
<Heading size={'5'} className='select-all'>
|
|
65
|
-
{diagram.title || 'Untitled'}
|
|
66
|
-
</Heading>
|
|
67
|
-
</Flex>
|
|
68
|
-
<ViewActionsToolbar diagram={diagram}/>
|
|
69
|
-
</Fragment>)}
|
|
70
|
-
</Box>);
|
|
11
|
+
return (<>
|
|
12
|
+
{viewMode === 'react' && <ViewAsReact diagram={diagram}/>}
|
|
13
|
+
{viewMode !== 'react' && (<Suspense>
|
|
14
|
+
<ViewAs viewMode={viewMode} viewId={viewId}/>
|
|
15
|
+
</Suspense>)}
|
|
16
|
+
<Header diagram={diagram}/>
|
|
17
|
+
</>);
|
|
71
18
|
}
|
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
import { useStore } from '@nanostores/react';
|
|
2
|
-
import {
|
|
2
|
+
import { createRouter, createSearchParams, getPagePath } from '@nanostores/router';
|
|
3
3
|
import { computed } from 'nanostores';
|
|
4
|
+
import { equals, isEmpty, isString, mapValues, omitBy } from 'remeda';
|
|
4
5
|
import { BaseUrl } from './const';
|
|
6
|
+
import { startTransition } from 'react';
|
|
5
7
|
export const $router = createRouter({
|
|
6
8
|
index: BaseUrl,
|
|
7
9
|
view: `${BaseUrl}view/:viewId?`,
|
|
8
10
|
export: `${BaseUrl}export/:viewId`,
|
|
9
11
|
embed: `${BaseUrl}embed/:viewId`
|
|
12
|
+
}, {
|
|
13
|
+
links: true
|
|
14
|
+
});
|
|
15
|
+
const $searchParams = createSearchParams({
|
|
16
|
+
links: true
|
|
10
17
|
});
|
|
11
|
-
const $searchParams = createSearchParams();
|
|
12
18
|
const asTheme = (v) => {
|
|
13
19
|
const vlower = v?.toLowerCase();
|
|
14
20
|
if (vlower === 'light' || vlower === 'dark') {
|
|
@@ -21,17 +27,61 @@ const asPadding = (v) => {
|
|
|
21
27
|
if (parsed && isFinite(parsed) && isNaN(parsed) === false) {
|
|
22
28
|
return Math.round(parsed);
|
|
23
29
|
}
|
|
24
|
-
return
|
|
30
|
+
return undefined;
|
|
31
|
+
};
|
|
32
|
+
const asViewMode = (v) => {
|
|
33
|
+
const mode = v?.toLowerCase() ?? '';
|
|
34
|
+
switch (mode) {
|
|
35
|
+
case 'dot':
|
|
36
|
+
case 'mmd':
|
|
37
|
+
case 'd2':
|
|
38
|
+
case 'react': {
|
|
39
|
+
return mode;
|
|
40
|
+
}
|
|
41
|
+
default: {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
25
45
|
};
|
|
26
|
-
const
|
|
46
|
+
const searchParams = computed($searchParams, v => {
|
|
47
|
+
return {
|
|
48
|
+
theme: asTheme(v.theme),
|
|
49
|
+
padding: asPadding(v.padding),
|
|
50
|
+
mode: asViewMode(v.mode),
|
|
51
|
+
showUI: 'showUI' in v ? v.showUI === 'true' : undefined
|
|
52
|
+
};
|
|
53
|
+
});
|
|
54
|
+
const filter = (v, k) => v == undefined ||
|
|
55
|
+
(k === 'theme' && v === 'dark') ||
|
|
56
|
+
(k === 'mode' && v === 'react') ||
|
|
57
|
+
(k === 'padding' && v === 20);
|
|
58
|
+
function omitDefaults(v) {
|
|
59
|
+
return omitBy(v, filter);
|
|
60
|
+
}
|
|
61
|
+
export function updateSearchParams(update) {
|
|
62
|
+
const current = searchParams.get();
|
|
63
|
+
const next = {
|
|
64
|
+
...current,
|
|
65
|
+
...update
|
|
66
|
+
};
|
|
67
|
+
if (!equals(current, next)) {
|
|
68
|
+
const params = mapValues(omitDefaults(next), v => (isString(v) ? v : String(v)));
|
|
69
|
+
startTransition(() => {
|
|
70
|
+
$searchParams.open(params);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
export const useSearchParams = () => useStore(searchParams);
|
|
75
|
+
const $route = computed([$router, searchParams], (r, v) => {
|
|
27
76
|
if (r?.route === 'view') {
|
|
28
77
|
return {
|
|
29
78
|
route: 'view',
|
|
30
79
|
params: {
|
|
31
|
-
viewId: r.params.viewId ?? 'index',
|
|
32
|
-
theme:
|
|
80
|
+
viewId: (r.params.viewId ?? 'index'),
|
|
81
|
+
theme: v.theme ?? 'dark',
|
|
82
|
+
mode: v.mode ?? 'react'
|
|
33
83
|
},
|
|
34
|
-
showUI:
|
|
84
|
+
showUI: v.showUI ?? true
|
|
35
85
|
};
|
|
36
86
|
}
|
|
37
87
|
if (r?.route === 'export' || r?.route === 'embed') {
|
|
@@ -39,7 +89,7 @@ const $route = computed([$router, $searchParams], (r, v) => {
|
|
|
39
89
|
route: r.route,
|
|
40
90
|
params: {
|
|
41
91
|
viewId: r.params.viewId,
|
|
42
|
-
padding:
|
|
92
|
+
padding: v.padding ?? 20,
|
|
43
93
|
theme: r.route === 'embed' ? asTheme(v.theme) : undefined
|
|
44
94
|
},
|
|
45
95
|
showUI: false
|
|
@@ -50,29 +100,49 @@ const $route = computed([$router, $searchParams], (r, v) => {
|
|
|
50
100
|
params: {
|
|
51
101
|
theme: asTheme(v.theme) ?? 'dark'
|
|
52
102
|
},
|
|
53
|
-
showUI:
|
|
103
|
+
showUI: v.showUI ?? false
|
|
54
104
|
};
|
|
55
105
|
});
|
|
56
106
|
export const useRoute = () => useStore($route);
|
|
57
107
|
export const isCurrentDiagram = (view) => {
|
|
58
108
|
const r = $route.get();
|
|
59
|
-
return (r.route === 'view' || r.route === 'export'
|
|
109
|
+
return ((r.route === 'view' || r.route === 'export' || r.route === 'embed') &&
|
|
110
|
+
r.params.viewId === view.id);
|
|
60
111
|
};
|
|
112
|
+
function currentSearchParams() {
|
|
113
|
+
const params = omitDefaults(searchParams.get());
|
|
114
|
+
if (isEmpty(params)) {
|
|
115
|
+
return '';
|
|
116
|
+
}
|
|
117
|
+
const urlSearchParams = new URLSearchParams();
|
|
118
|
+
for (const [k, v] of Object.entries(params)) {
|
|
119
|
+
urlSearchParams.set(k, isString(v) ? v : String(v));
|
|
120
|
+
}
|
|
121
|
+
const asString = urlSearchParams.toString();
|
|
122
|
+
return asString !== '' ? '?' + asString : '';
|
|
123
|
+
}
|
|
124
|
+
function getRoutePath(name, ...params) {
|
|
125
|
+
// if (params.length > 1) {
|
|
126
|
+
// let p = params[0]!
|
|
127
|
+
// p.
|
|
128
|
+
// }
|
|
129
|
+
return getPagePath($router, name, ...params) + currentSearchParams();
|
|
130
|
+
}
|
|
131
|
+
function openRoute(name, ...params) {
|
|
132
|
+
startTransition(() => {
|
|
133
|
+
$router.open(getRoutePath(name, ...params));
|
|
134
|
+
});
|
|
135
|
+
}
|
|
61
136
|
export const $pages = {
|
|
62
137
|
index: {
|
|
63
|
-
|
|
138
|
+
url: () => getRoutePath('index'),
|
|
139
|
+
open: () => openRoute('index')
|
|
64
140
|
},
|
|
65
141
|
view: {
|
|
66
|
-
|
|
142
|
+
url: (viewId) => getRoutePath('view', { viewId }),
|
|
143
|
+
open: (viewId) => openRoute('view', { viewId })
|
|
67
144
|
},
|
|
68
145
|
embed: {
|
|
69
|
-
path: (viewId) =>
|
|
146
|
+
path: (viewId) => getRoutePath('embed', { viewId })
|
|
70
147
|
}
|
|
71
148
|
};
|
|
72
|
-
// if (import.meta.env.DEV) {
|
|
73
|
-
// logger({
|
|
74
|
-
// $searchParams,
|
|
75
|
-
// $router,
|
|
76
|
-
// $route
|
|
77
|
-
// })
|
|
78
|
-
// }
|