paseo-plugin-mermaid 0.2.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/LICENSE +21 -0
- package/README.md +182 -0
- package/client/diagram.tsx +395 -0
- package/client/item.tsx +25 -0
- package/client/markdown.tsx +107 -0
- package/client/toolbar.tsx +145 -0
- package/client/viewer.tsx +255 -0
- package/index.client.tsx +56 -0
- package/package.json +41 -0
- package/paseo-plugin.json +4 -0
- package/shared/flowchart.ts +205 -0
- package/shared/layout.ts +440 -0
- package/shared/markdown.ts +135 -0
- package/shared/segment.ts +97 -0
- package/shared/sequence.ts +122 -0
- package/shared/transform.ts +32 -0
- package/shared/zoom.ts +114 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { PluginTheme } from "@getpaseo/plugin";
|
|
2
|
+
import React, { useMemo } from "react";
|
|
3
|
+
import { StyleSheet, Text, View } from "react-native";
|
|
4
|
+
import { parseMarkdown, type Block, type Inline } from "../shared/markdown";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Renders the prose around a diagram.
|
|
8
|
+
*
|
|
9
|
+
* Taking over a timeline entry means Paseo's own Markdown no longer runs on it,
|
|
10
|
+
* so this covers the formatting agents actually use next to a diagram. It is
|
|
11
|
+
* deliberately small; anything richer belongs to Paseo, not to a plugin.
|
|
12
|
+
*/
|
|
13
|
+
export function Markdown({ text, theme, compact }: { text: string; theme: PluginTheme; compact: boolean }) {
|
|
14
|
+
const blocks = useMemo(() => parseMarkdown(text), [text]);
|
|
15
|
+
return (
|
|
16
|
+
<View style={{ gap: compact ? 6 : 8 }}>
|
|
17
|
+
{blocks.map((block, index) => (
|
|
18
|
+
<BlockView key={index} block={block} theme={theme} />
|
|
19
|
+
))}
|
|
20
|
+
</View>
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function BlockView({ block, theme }: { block: Block; theme: PluginTheme }) {
|
|
25
|
+
const body = { color: theme.colors.foreground, fontSize: 14, lineHeight: 20 };
|
|
26
|
+
|
|
27
|
+
if (block.kind === "rule") {
|
|
28
|
+
return <View style={{ height: StyleSheet.hairlineWidth, backgroundColor: theme.colors.foregroundMuted }} />;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (block.kind === "code") {
|
|
32
|
+
return (
|
|
33
|
+
<View
|
|
34
|
+
style={{
|
|
35
|
+
borderRadius: 8,
|
|
36
|
+
borderWidth: StyleSheet.hairlineWidth,
|
|
37
|
+
borderColor: theme.colors.foregroundMuted,
|
|
38
|
+
padding: 10,
|
|
39
|
+
}}
|
|
40
|
+
>
|
|
41
|
+
<Text selectable style={{ color: theme.colors.foreground, fontFamily: "Menlo", fontSize: 12 }}>
|
|
42
|
+
{block.text}
|
|
43
|
+
</Text>
|
|
44
|
+
</View>
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (block.kind === "heading") {
|
|
49
|
+
return (
|
|
50
|
+
<Text selectable style={{ ...body, fontSize: 20 - block.level, fontWeight: "700" }}>
|
|
51
|
+
<Spans content={block.content} theme={theme} />
|
|
52
|
+
</Text>
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (block.kind === "quote") {
|
|
57
|
+
return (
|
|
58
|
+
<View style={{ borderLeftWidth: 2, borderLeftColor: theme.colors.accent, paddingLeft: 10 }}>
|
|
59
|
+
<Text selectable style={{ ...body, color: theme.colors.foregroundMuted }}>
|
|
60
|
+
<Spans content={block.content} theme={theme} />
|
|
61
|
+
</Text>
|
|
62
|
+
</View>
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (block.kind === "bullet" || block.kind === "ordered") {
|
|
67
|
+
return (
|
|
68
|
+
<View style={{ gap: 3 }}>
|
|
69
|
+
{block.items.map((item, index) => (
|
|
70
|
+
<View key={index} style={{ flexDirection: "row", gap: 8 }}>
|
|
71
|
+
<Text style={{ ...body, color: theme.colors.foregroundMuted }}>
|
|
72
|
+
{block.kind === "bullet" ? "·" : `${block.start + index}.`}
|
|
73
|
+
</Text>
|
|
74
|
+
<Text selectable style={{ ...body, flex: 1 }}>
|
|
75
|
+
<Spans content={item} theme={theme} />
|
|
76
|
+
</Text>
|
|
77
|
+
</View>
|
|
78
|
+
))}
|
|
79
|
+
</View>
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return (
|
|
84
|
+
<Text selectable style={body}>
|
|
85
|
+
<Spans content={block.content} theme={theme} />
|
|
86
|
+
</Text>
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function Spans({ content, theme }: { content: Inline[]; theme: PluginTheme }) {
|
|
91
|
+
return (
|
|
92
|
+
<>
|
|
93
|
+
{content.map((span, index) => {
|
|
94
|
+
if (span.kind === "bold") return <Text key={index} style={{ fontWeight: "700" }}>{span.text}</Text>;
|
|
95
|
+
if (span.kind === "italic") return <Text key={index} style={{ fontStyle: "italic" }}>{span.text}</Text>;
|
|
96
|
+
if (span.kind === "code") {
|
|
97
|
+
return (
|
|
98
|
+
<Text key={index} style={{ fontFamily: "Menlo", fontSize: 12.5, color: theme.colors.accent }}>
|
|
99
|
+
{span.text}
|
|
100
|
+
</Text>
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
return <Text key={index}>{span.text}</Text>;
|
|
104
|
+
})}
|
|
105
|
+
</>
|
|
106
|
+
);
|
|
107
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import type { PluginTheme } from "@getpaseo/plugin";
|
|
2
|
+
import { Icon } from "@getpaseo/plugin/client/react-native";
|
|
3
|
+
import React from "react";
|
|
4
|
+
import { Pressable, Text, View } from "react-native";
|
|
5
|
+
import { toolbarState, zoomLabel } from "../shared/zoom";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The controls above a drawing: the zoom steps, the percent, fit to view, pop
|
|
9
|
+
* out, and the source toggle. Paseo's own diagram box offers the same set on
|
|
10
|
+
* hover; this row is always visible so it is reachable on touch. In the compact
|
|
11
|
+
* layout the labels drop and the icons and the percent stay, so the row still
|
|
12
|
+
* fits a phone.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export interface ToolbarProps {
|
|
16
|
+
theme: PluginTheme;
|
|
17
|
+
compact: boolean;
|
|
18
|
+
zoom: number;
|
|
19
|
+
/** The zoom "Fit to view" would set; the button goes quiet once it is there. */
|
|
20
|
+
fitZoom: number;
|
|
21
|
+
showCode: boolean;
|
|
22
|
+
onZoomIn(): void;
|
|
23
|
+
onZoomOut(): void;
|
|
24
|
+
onFit(): void;
|
|
25
|
+
onToggleCode(): void;
|
|
26
|
+
/** Absent inside the pop-out, where there is nowhere further to go. */
|
|
27
|
+
onPopOut?(): void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function Toolbar({
|
|
31
|
+
theme,
|
|
32
|
+
compact,
|
|
33
|
+
zoom,
|
|
34
|
+
fitZoom,
|
|
35
|
+
showCode,
|
|
36
|
+
onZoomIn,
|
|
37
|
+
onZoomOut,
|
|
38
|
+
onFit,
|
|
39
|
+
onToggleCode,
|
|
40
|
+
onPopOut,
|
|
41
|
+
}: ToolbarProps) {
|
|
42
|
+
const enabled = toolbarState({ zoom, fitZoom, showCode });
|
|
43
|
+
const button = { theme, compact };
|
|
44
|
+
|
|
45
|
+
return (
|
|
46
|
+
<View
|
|
47
|
+
style={{
|
|
48
|
+
flexDirection: "row",
|
|
49
|
+
alignItems: "center",
|
|
50
|
+
justifyContent: "space-between",
|
|
51
|
+
flexWrap: "wrap",
|
|
52
|
+
gap: 8,
|
|
53
|
+
}}
|
|
54
|
+
>
|
|
55
|
+
<View style={{ flexDirection: "row", alignItems: "center", flexWrap: "wrap", gap: 8 }}>
|
|
56
|
+
<ToolButton {...button} icon="ZoomOut" label="Zoom out" onPress={onZoomOut} disabled={!enabled.zoomOut} />
|
|
57
|
+
<Text
|
|
58
|
+
style={{
|
|
59
|
+
minWidth: 40,
|
|
60
|
+
textAlign: "center",
|
|
61
|
+
fontSize: 12,
|
|
62
|
+
// Digits of equal width, so 80% → 100% → 125% does not nudge the buttons.
|
|
63
|
+
fontVariant: ["tabular-nums"],
|
|
64
|
+
color: theme.colors.foregroundMuted,
|
|
65
|
+
opacity: showCode ? 0.4 : 1,
|
|
66
|
+
}}
|
|
67
|
+
>
|
|
68
|
+
{zoomLabel(zoom)}
|
|
69
|
+
</Text>
|
|
70
|
+
<ToolButton {...button} icon="ZoomIn" label="Zoom in" onPress={onZoomIn} disabled={!enabled.zoomIn} />
|
|
71
|
+
<ToolButton {...button} icon="Scan" label="Fit to view" onPress={onFit} disabled={!enabled.fit} />
|
|
72
|
+
{onPopOut ? (
|
|
73
|
+
<ToolButton {...button} icon="Maximize2" label="Pop out" onPress={onPopOut} disabled={!enabled.popOut} />
|
|
74
|
+
) : null}
|
|
75
|
+
</View>
|
|
76
|
+
<ToolButton
|
|
77
|
+
{...button}
|
|
78
|
+
icon={showCode ? "Workflow" : "Code"}
|
|
79
|
+
label={showCode ? "Show diagram" : "Show code"}
|
|
80
|
+
onPress={onToggleCode}
|
|
81
|
+
/>
|
|
82
|
+
</View>
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** `pressed` is what React Native reports; `hovered` arrives on web only. */
|
|
87
|
+
interface PressState {
|
|
88
|
+
pressed: boolean;
|
|
89
|
+
hovered?: boolean;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function ToolButton({
|
|
93
|
+
theme,
|
|
94
|
+
compact,
|
|
95
|
+
icon,
|
|
96
|
+
label,
|
|
97
|
+
onPress,
|
|
98
|
+
disabled = false,
|
|
99
|
+
}: {
|
|
100
|
+
theme: PluginTheme;
|
|
101
|
+
compact: boolean;
|
|
102
|
+
icon: string;
|
|
103
|
+
label: string;
|
|
104
|
+
onPress(): void;
|
|
105
|
+
disabled?: boolean;
|
|
106
|
+
}) {
|
|
107
|
+
// The visible button is shorter than a comfortable target; the slop brings
|
|
108
|
+
// the hit area to 40px on desktop and 44px on touch without touching the
|
|
109
|
+
// neighbour, since the row gap is wider than two horizontal slops.
|
|
110
|
+
const height = compact ? 32 : 28;
|
|
111
|
+
return (
|
|
112
|
+
<Pressable
|
|
113
|
+
accessibilityRole="button"
|
|
114
|
+
accessibilityLabel={label}
|
|
115
|
+
accessibilityState={{ disabled }}
|
|
116
|
+
disabled={disabled}
|
|
117
|
+
hitSlop={{ top: 6, bottom: 6, left: 3, right: 3 }}
|
|
118
|
+
onPress={onPress}
|
|
119
|
+
style={({ pressed, hovered }: PressState) => ({
|
|
120
|
+
flexDirection: "row",
|
|
121
|
+
alignItems: "center",
|
|
122
|
+
gap: 5,
|
|
123
|
+
height,
|
|
124
|
+
paddingHorizontal: compact ? 9 : 8,
|
|
125
|
+
borderRadius: 6,
|
|
126
|
+
borderWidth: 1,
|
|
127
|
+
borderColor: theme.colors.border,
|
|
128
|
+
backgroundColor: (hovered || pressed) && !disabled ? theme.colors.surface2 : theme.colors.surface1,
|
|
129
|
+
opacity: disabled ? 0.4 : 1,
|
|
130
|
+
transform: [{ scale: pressed && !disabled ? 0.96 : 1 }],
|
|
131
|
+
})}
|
|
132
|
+
>
|
|
133
|
+
{({ hovered }: PressState) => (
|
|
134
|
+
<>
|
|
135
|
+
<Icon
|
|
136
|
+
name={icon}
|
|
137
|
+
size={14}
|
|
138
|
+
color={hovered && !disabled ? theme.colors.foreground : theme.colors.foregroundMuted}
|
|
139
|
+
/>
|
|
140
|
+
{compact ? null : <Text style={{ fontSize: 12, color: theme.colors.foreground }}>{label}</Text>}
|
|
141
|
+
</>
|
|
142
|
+
)}
|
|
143
|
+
</Pressable>
|
|
144
|
+
);
|
|
145
|
+
}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import type { PluginTheme } from "@getpaseo/plugin";
|
|
2
|
+
import { Icon, Modal, ScrollView as SheetScrollView } from "@getpaseo/plugin/client/react-native";
|
|
3
|
+
import React, { useMemo, useState } from "react";
|
|
4
|
+
import { LayoutChangeEvent, ScrollView, StyleSheet, Text, View } from "react-native";
|
|
5
|
+
import { fitScale, overflowsAfterFit } from "../shared/layout";
|
|
6
|
+
import { fitBothScale, fitRowZoom, zoomIn, zoomOut, type Size } from "../shared/zoom";
|
|
7
|
+
import { Drawing, naturalSize, parseDiagram, Unsupported, type DrawableDiagram } from "./diagram";
|
|
8
|
+
import { Toolbar } from "./toolbar";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Puts a drawing on screen at the size the reader wants.
|
|
12
|
+
*
|
|
13
|
+
* The drawing itself is laid out once at its natural size; everything here is
|
|
14
|
+
* about where it goes and how big. In the row it is fitted to the width it was
|
|
15
|
+
* given and the reader zooms from there. Popped out, it is fitted to the window
|
|
16
|
+
* instead. Both keep their own zoom, because 100% means something different in
|
|
17
|
+
* each, and both can swap the drawing for its source.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** Narrower than this is a measurement artefact, not a row anyone can read in. */
|
|
21
|
+
const MIN_MEASURABLE = 120;
|
|
22
|
+
/** Kept clear around the drawing in the pop-out, so it never touches the edges. */
|
|
23
|
+
const POP_OUT_PADDING = 16;
|
|
24
|
+
|
|
25
|
+
export function DiagramViewer({ source, theme, compact }: { source: string; theme: PluginTheme; compact: boolean }) {
|
|
26
|
+
const parsed = useMemo(() => parseDiagram(source), [source]);
|
|
27
|
+
const [popped, setPopped] = useState(false);
|
|
28
|
+
|
|
29
|
+
if (parsed.kind === "unsupported") {
|
|
30
|
+
// Showing the source beats drawing a guess at what it meant.
|
|
31
|
+
return <Unsupported source={source} theme={theme} />;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
<View style={{ gap: 6 }}>
|
|
36
|
+
<RowViewer parsed={parsed} source={source} theme={theme} compact={compact} onPopOut={() => setPopped(true)} />
|
|
37
|
+
{parsed.kind === "flow" && parsed.skipped.length > 0 ? (
|
|
38
|
+
<Text style={{ color: theme.colors.foregroundMuted, fontSize: 11 }}>
|
|
39
|
+
{parsed.skipped.length} line{parsed.skipped.length === 1 ? "" : "s"} not drawn: subgraphs and styling are
|
|
40
|
+
not supported yet.
|
|
41
|
+
</Text>
|
|
42
|
+
) : null}
|
|
43
|
+
{popped ? (
|
|
44
|
+
<PopOut parsed={parsed} source={source} theme={theme} compact={compact} onClose={() => setPopped(false)} />
|
|
45
|
+
) : null}
|
|
46
|
+
</View>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The drawing in the transcript row.
|
|
52
|
+
*
|
|
53
|
+
* The row is measured, the drawing is fitted to it (grown a little when small,
|
|
54
|
+
* shrunk no further than the labels stay readable), and the zoom multiplies
|
|
55
|
+
* that. Zooming in makes the row taller and, past the row's width, scrolls it
|
|
56
|
+
* sideways; the transcript itself scrolls vertically, so nothing is clipped.
|
|
57
|
+
*/
|
|
58
|
+
function RowViewer({
|
|
59
|
+
parsed,
|
|
60
|
+
source,
|
|
61
|
+
theme,
|
|
62
|
+
compact,
|
|
63
|
+
onPopOut,
|
|
64
|
+
}: {
|
|
65
|
+
parsed: DrawableDiagram;
|
|
66
|
+
source: string;
|
|
67
|
+
theme: PluginTheme;
|
|
68
|
+
compact: boolean;
|
|
69
|
+
onPopOut(): void;
|
|
70
|
+
}) {
|
|
71
|
+
const natural = naturalSize(parsed);
|
|
72
|
+
const [available, setAvailable] = useState(0);
|
|
73
|
+
const [zoom, setZoom] = useState(1);
|
|
74
|
+
const [showCode, setShowCode] = useState(false);
|
|
75
|
+
|
|
76
|
+
const onLayout = (event: LayoutChangeEvent) => {
|
|
77
|
+
const measured = Math.round(event.nativeEvent.layout.width);
|
|
78
|
+
// A first pass can report a width the row does not really have, while the
|
|
79
|
+
// parent is still deriving its own size. Treating that as real is what made
|
|
80
|
+
// a freshly arrived diagram render narrow until something forced a reload.
|
|
81
|
+
if (measured < MIN_MEASURABLE) return;
|
|
82
|
+
if (measured !== available) setAvailable(measured);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const rowScale = fitScale(available, natural.width);
|
|
86
|
+
const fitZoom = fitRowZoom(rowScale, natural.width, available);
|
|
87
|
+
const scale = rowScale * zoom;
|
|
88
|
+
const box = { width: Math.floor(natural.width * scale), height: Math.floor(natural.height * scale) };
|
|
89
|
+
const scrolls = overflowsAfterFit(available, natural.width, zoom);
|
|
90
|
+
|
|
91
|
+
const scaled = <Scaled parsed={parsed} natural={natural} scale={scale} box={box} theme={theme} />;
|
|
92
|
+
|
|
93
|
+
return (
|
|
94
|
+
<View style={{ gap: 8 }}>
|
|
95
|
+
<Toolbar
|
|
96
|
+
theme={theme}
|
|
97
|
+
compact={compact}
|
|
98
|
+
zoom={zoom}
|
|
99
|
+
fitZoom={fitZoom}
|
|
100
|
+
showCode={showCode}
|
|
101
|
+
onZoomIn={() => setZoom(zoomIn)}
|
|
102
|
+
onZoomOut={() => setZoom(zoomOut)}
|
|
103
|
+
onFit={() => setZoom(fitZoom)}
|
|
104
|
+
onToggleCode={() => setShowCode((shown) => !shown)}
|
|
105
|
+
onPopOut={onPopOut}
|
|
106
|
+
/>
|
|
107
|
+
<View style={{ alignSelf: "stretch" }}>
|
|
108
|
+
{/*
|
|
109
|
+
The measurement happens on a probe, not on the container that holds the
|
|
110
|
+
drawing. Measuring the container would close a loop: its height comes
|
|
111
|
+
from the scale, the scale comes from the measurement, and a view whose
|
|
112
|
+
size depends on its own measurement can settle on a wrong value and then
|
|
113
|
+
never be laid out again. The probe is always the full width of the row
|
|
114
|
+
and never changes, so every change in the row reaches it.
|
|
115
|
+
*/}
|
|
116
|
+
<View onLayout={onLayout} style={{ alignSelf: "stretch", height: 0 }} />
|
|
117
|
+
{showCode ? (
|
|
118
|
+
<Source source={source} theme={theme} />
|
|
119
|
+
) : (
|
|
120
|
+
<View style={{ alignSelf: "stretch", height: box.height }}>
|
|
121
|
+
{scrolls ? (
|
|
122
|
+
<ScrollView horizontal showsHorizontalScrollIndicator={!compact} style={{ height: box.height }}>
|
|
123
|
+
{scaled}
|
|
124
|
+
</ScrollView>
|
|
125
|
+
) : (
|
|
126
|
+
scaled
|
|
127
|
+
)}
|
|
128
|
+
</View>
|
|
129
|
+
)}
|
|
130
|
+
</View>
|
|
131
|
+
</View>
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* The drawing in a host modal.
|
|
137
|
+
*
|
|
138
|
+
* Here 100% means fitted to the window on both axes, which grows a small
|
|
139
|
+
* drawing as far as the window allows: seeing it large is what popping out is
|
|
140
|
+
* for. Past the window the drawing scrolls on both axes rather than being
|
|
141
|
+
* panned, since a scroll view is something every platform here already has.
|
|
142
|
+
*/
|
|
143
|
+
function PopOut({
|
|
144
|
+
parsed,
|
|
145
|
+
source,
|
|
146
|
+
theme,
|
|
147
|
+
compact,
|
|
148
|
+
onClose,
|
|
149
|
+
}: {
|
|
150
|
+
parsed: DrawableDiagram;
|
|
151
|
+
source: string;
|
|
152
|
+
theme: PluginTheme;
|
|
153
|
+
compact: boolean;
|
|
154
|
+
onClose(): void;
|
|
155
|
+
}) {
|
|
156
|
+
const natural = naturalSize(parsed);
|
|
157
|
+
const [viewport, setViewport] = useState<Size>({ width: 0, height: 0 });
|
|
158
|
+
const [zoom, setZoom] = useState(1);
|
|
159
|
+
const [showCode, setShowCode] = useState(false);
|
|
160
|
+
|
|
161
|
+
const onLayout = (event: LayoutChangeEvent) => {
|
|
162
|
+
const { width, height } = event.nativeEvent.layout;
|
|
163
|
+
const next = { width: Math.round(width), height: Math.round(height) };
|
|
164
|
+
if (next.width < MIN_MEASURABLE || next.height < MIN_MEASURABLE) return;
|
|
165
|
+
if (next.width !== viewport.width || next.height !== viewport.height) setViewport(next);
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
const scale = fitBothScale(natural, viewport, POP_OUT_PADDING) * zoom;
|
|
169
|
+
const box = { width: Math.floor(natural.width * scale), height: Math.floor(natural.height * scale) };
|
|
170
|
+
|
|
171
|
+
return (
|
|
172
|
+
<Modal
|
|
173
|
+
title="Diagram"
|
|
174
|
+
icon={<Icon name="Workflow" size={16} color={theme.colors.foregroundMuted} />}
|
|
175
|
+
open
|
|
176
|
+
onOpenChange={(open) => {
|
|
177
|
+
if (!open) onClose();
|
|
178
|
+
}}
|
|
179
|
+
>
|
|
180
|
+
<Modal.Content scrollable={false} style={{ flex: 1 }} contentContainerStyle={{ flex: 1, padding: 0, gap: 0 }}>
|
|
181
|
+
<View style={{ flex: 1, minHeight: 0, gap: 8, padding: compact ? 12 : 16 }}>
|
|
182
|
+
<Toolbar
|
|
183
|
+
theme={theme}
|
|
184
|
+
compact={compact}
|
|
185
|
+
zoom={zoom}
|
|
186
|
+
fitZoom={1}
|
|
187
|
+
showCode={showCode}
|
|
188
|
+
onZoomIn={() => setZoom(zoomIn)}
|
|
189
|
+
onZoomOut={() => setZoom(zoomOut)}
|
|
190
|
+
onFit={() => setZoom(1)}
|
|
191
|
+
onToggleCode={() => setShowCode((shown) => !shown)}
|
|
192
|
+
/>
|
|
193
|
+
<View style={{ flex: 1, minHeight: 0 }} onLayout={onLayout}>
|
|
194
|
+
{showCode ? (
|
|
195
|
+
<SheetScrollView style={{ flex: 1 }}>
|
|
196
|
+
<Source source={source} theme={theme} />
|
|
197
|
+
</SheetScrollView>
|
|
198
|
+
) : (
|
|
199
|
+
<SheetScrollView style={{ flex: 1 }} contentContainerStyle={{ flexGrow: 1, justifyContent: "center" }}>
|
|
200
|
+
<ScrollView
|
|
201
|
+
horizontal
|
|
202
|
+
showsHorizontalScrollIndicator={!compact}
|
|
203
|
+
style={{ alignSelf: "stretch" }}
|
|
204
|
+
contentContainerStyle={{ flexGrow: 1, justifyContent: "center", padding: POP_OUT_PADDING }}
|
|
205
|
+
>
|
|
206
|
+
<Scaled parsed={parsed} natural={natural} scale={scale} box={box} theme={theme} />
|
|
207
|
+
</ScrollView>
|
|
208
|
+
</SheetScrollView>
|
|
209
|
+
)}
|
|
210
|
+
</View>
|
|
211
|
+
</View>
|
|
212
|
+
</Modal.Content>
|
|
213
|
+
</Modal>
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** The drawing at natural size inside a box of its scaled size, so layout sees the scaled one. */
|
|
218
|
+
function Scaled({
|
|
219
|
+
parsed,
|
|
220
|
+
natural,
|
|
221
|
+
scale,
|
|
222
|
+
box,
|
|
223
|
+
theme,
|
|
224
|
+
}: {
|
|
225
|
+
parsed: DrawableDiagram;
|
|
226
|
+
natural: Size;
|
|
227
|
+
scale: number;
|
|
228
|
+
box: Size;
|
|
229
|
+
theme: PluginTheme;
|
|
230
|
+
}) {
|
|
231
|
+
return (
|
|
232
|
+
<View style={box}>
|
|
233
|
+
<View style={{ width: natural.width, height: natural.height, transform: [{ scale }], transformOrigin: "top left" }}>
|
|
234
|
+
<Drawing parsed={parsed} theme={theme} />
|
|
235
|
+
</View>
|
|
236
|
+
</View>
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function Source({ source, theme }: { source: string; theme: PluginTheme }) {
|
|
241
|
+
return (
|
|
242
|
+
<View
|
|
243
|
+
style={{
|
|
244
|
+
borderRadius: 8,
|
|
245
|
+
borderWidth: StyleSheet.hairlineWidth,
|
|
246
|
+
borderColor: theme.colors.border,
|
|
247
|
+
padding: 10,
|
|
248
|
+
}}
|
|
249
|
+
>
|
|
250
|
+
<Text selectable style={{ color: theme.colors.foreground, fontFamily: "Menlo", fontSize: 12, lineHeight: 18 }}>
|
|
251
|
+
{source}
|
|
252
|
+
</Text>
|
|
253
|
+
</View>
|
|
254
|
+
);
|
|
255
|
+
}
|
package/index.client.tsx
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { PluginClientContext } from "@getpaseo/plugin/client";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { DiagramItem, TextItem } from "./client/item";
|
|
4
|
+
import { DIAGRAM_KIND, split, TEXT_KIND } from "./shared/transform";
|
|
5
|
+
|
|
6
|
+
const textSchema = z.object({ text: z.string() });
|
|
7
|
+
const diagramSchema = z.object({ source: z.string() });
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Renders Mermaid diagrams inside the transcript.
|
|
11
|
+
*
|
|
12
|
+
* A transformer replaces the whole timeline entry, so a message carrying a
|
|
13
|
+
* diagram is split into its parts and every part is emitted as a plugin item:
|
|
14
|
+
* the prose keeps its place instead of disappearing with the code fence.
|
|
15
|
+
*
|
|
16
|
+
* Paseo runs transformers while building the render model, on every streaming
|
|
17
|
+
* update as well as on the finished message, and memoizes by source item. The
|
|
18
|
+
* transform is therefore pure and synchronous, and it claims a mermaid fence
|
|
19
|
+
* the moment the fence opens so the drawing never swaps renderers mid-stream.
|
|
20
|
+
*
|
|
21
|
+
* The item stores the Mermaid source rather than a parsed graph. Parsing happens
|
|
22
|
+
* at render time, which keeps the stored data small and lets a later fix to the
|
|
23
|
+
* parser improve diagrams that were sent months ago.
|
|
24
|
+
*
|
|
25
|
+
* Everything here runs in the app. The plugin has no server entry and starts no
|
|
26
|
+
* daemon process.
|
|
27
|
+
*/
|
|
28
|
+
export default function contribute(client: PluginClientContext) {
|
|
29
|
+
// Registered one by one rather than in a loop: the contribution is generic in
|
|
30
|
+
// the item type, and a loop widens it to a union that no longer infers `item`.
|
|
31
|
+
client.addTimelineTransformer({
|
|
32
|
+
id: "mermaid-assistant",
|
|
33
|
+
query: { itemType: "assistant_message" },
|
|
34
|
+
transform: ({ item }) => split(item.text),
|
|
35
|
+
});
|
|
36
|
+
client.addTimelineTransformer({
|
|
37
|
+
id: "mermaid-user",
|
|
38
|
+
query: { itemType: "user_message" },
|
|
39
|
+
transform: ({ item }) => split(item.text),
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
client.addTimelineRenderer({
|
|
43
|
+
kind: DIAGRAM_KIND,
|
|
44
|
+
version: 1,
|
|
45
|
+
schema: diagramSchema,
|
|
46
|
+
Component: DiagramItem,
|
|
47
|
+
});
|
|
48
|
+
client.addTimelineRenderer({
|
|
49
|
+
kind: TEXT_KIND,
|
|
50
|
+
version: 1,
|
|
51
|
+
schema: textSchema,
|
|
52
|
+
Component: TextItem,
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
return () => {};
|
|
56
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "paseo-plugin-mermaid",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Render Mermaid diagrams inside the Paseo chat timeline",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "dutchakdev",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/dutchakdev/paseo-plugin-mermaid.git"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"paseo",
|
|
13
|
+
"paseo-plugin",
|
|
14
|
+
"mermaid",
|
|
15
|
+
"diagrams"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"typecheck": "tsc --noEmit",
|
|
19
|
+
"test": "vitest run",
|
|
20
|
+
"preview": "tsx scripts/preview.ts"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@getpaseo/client": "0.8.0",
|
|
24
|
+
"@getpaseo/plugin": "0.8.0",
|
|
25
|
+
"@getpaseo/protocol": "0.8.0",
|
|
26
|
+
"@tanstack/react-query": "^5.90.11",
|
|
27
|
+
"@types/react": "~19.2.0",
|
|
28
|
+
"react": "19.1.0",
|
|
29
|
+
"react-native": "0.81.5",
|
|
30
|
+
"tsx": "^4.23.13",
|
|
31
|
+
"typescript": "^5.9.3",
|
|
32
|
+
"vitest": "^4.1.11",
|
|
33
|
+
"zod": "^4.4.3"
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"paseo-plugin.json",
|
|
37
|
+
"index.client.tsx",
|
|
38
|
+
"client/",
|
|
39
|
+
"shared/"
|
|
40
|
+
]
|
|
41
|
+
}
|