@diegopluna/narvi 0.1.0 → 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/README.md +102 -41
- package/dist/base.css +2 -2
- package/dist/color.d.ts +1 -1
- package/dist/color.js +1 -1
- package/dist/components/activity/ActivityContent.svelte +3 -15
- package/dist/components/activity/ActivityStep.svelte +12 -12
- package/dist/components/activity/ActivityThought.svelte +4 -4
- package/dist/components/activity/ActivityTrigger.svelte +3 -3
- package/dist/components/app-shell/AppShellBar.svelte +89 -0
- package/dist/components/app-shell/AppShellBar.svelte.d.ts +20 -0
- package/dist/components/app-shell/AppShellHeader.svelte +17 -16
- package/dist/components/app-shell/AppShellHeader.svelte.d.ts +2 -2
- package/dist/components/app-shell/AppShellMain.svelte +4 -20
- package/dist/components/app-shell/AppShellPanel.svelte +21 -19
- package/dist/components/app-shell/AppShellPanes.svelte +58 -0
- package/dist/components/app-shell/AppShellPanes.svelte.d.ts +11 -0
- package/dist/components/app-shell/AppShellRail.svelte +85 -0
- package/dist/components/app-shell/AppShellRail.svelte.d.ts +11 -0
- package/dist/components/app-shell/AppShellRoot.svelte +50 -24
- package/dist/components/app-shell/AppShellRoot.svelte.d.ts +8 -6
- package/dist/components/app-shell/AppShellSheet.svelte +95 -0
- package/dist/components/app-shell/AppShellSheet.svelte.d.ts +17 -0
- package/dist/components/app-shell/AppShellSidebar.svelte +37 -61
- package/dist/components/app-shell/AppShellSidebar.svelte.d.ts +1 -5
- package/dist/components/app-shell/AppShellToggle.svelte +62 -23
- package/dist/components/app-shell/AppShellToggle.svelte.d.ts +2 -0
- package/dist/components/app-shell/context.d.ts +6 -0
- package/dist/components/app-shell/index.d.ts +9 -1
- package/dist/components/app-shell/index.js +5 -1
- package/dist/components/approval/Approval.svelte +91 -19
- package/dist/components/approval/Approval.svelte.d.ts +7 -0
- package/dist/components/avatar/AvatarFallback.svelte +1 -2
- package/dist/components/avatar/AvatarRoot.svelte +2 -2
- package/dist/components/badge/Badge.svelte +5 -5
- package/dist/components/button/Button.svelte +219 -40
- package/dist/components/button/Button.svelte.d.ts +38 -2
- package/dist/components/card/Card.svelte +15 -26
- package/dist/components/change-stats/ChangeStats.svelte +1 -2
- package/dist/components/changes-summary/ChangesSummary.svelte +14 -15
- package/dist/components/checkbox/Checkbox.svelte +18 -15
- package/dist/components/checkbox/Checkbox.svelte.d.ts +5 -0
- package/dist/components/code/Code.svelte +1 -2
- package/dist/components/collapsible/CollapsibleTrigger.svelte +2 -2
- package/dist/components/command/CommandItem.svelte +1 -1
- package/dist/components/command/styles.d.ts +3 -2
- package/dist/components/command/styles.js +13 -10
- package/dist/components/composer/ComposerAttachButton.svelte +1 -1
- package/dist/components/composer/ComposerAttachment.svelte +9 -12
- package/dist/components/composer/ComposerModelPicker.svelte +18 -20
- package/dist/components/composer/ComposerRoot.svelte +8 -7
- package/dist/components/composer/ComposerSubmit.svelte +8 -15
- package/dist/components/composer/ComposerTextarea.svelte +1 -1
- package/dist/components/composer/ComposerToggle.svelte +9 -11
- package/dist/components/composer/ComposerTray.svelte +78 -0
- package/dist/components/composer/ComposerTray.svelte.d.ts +15 -0
- package/dist/components/composer/index.d.ts +3 -1
- package/dist/components/composer/index.js +3 -2
- package/dist/components/conversation/ConversationContent.svelte +3 -2
- package/dist/components/conversation/ConversationEmpty.svelte +1 -1
- package/dist/components/conversation/ConversationScrollButton.svelte +3 -3
- package/dist/components/dialog/ConfirmDialog.svelte +1 -1
- package/dist/components/dialog/styles.d.ts +2 -3
- package/dist/components/dialog/styles.js +5 -6
- package/dist/components/divider/Divider.svelte +3 -3
- package/dist/components/empty-state/EmptyState.svelte +4 -5
- package/dist/components/field/Field.svelte +4 -4
- package/dist/components/frame/Frame.svelte +6 -7
- package/dist/components/frame/Frame.svelte.d.ts +1 -1
- package/dist/components/heading/Heading.svelte +2 -2
- package/dist/components/kbd/Kbd.svelte +12 -7
- package/dist/components/link/Link.svelte +7 -3
- package/dist/components/link/Link.svelte.d.ts +4 -1
- package/dist/components/markdown/Markdown.svelte +714 -0
- package/dist/components/markdown/Markdown.svelte.d.ts +80 -0
- package/dist/components/markdown/MarkdownBlock.svelte +231 -0
- package/dist/components/markdown/MarkdownBlock.svelte.d.ts +11 -0
- package/dist/components/markdown/MarkdownBlocks.svelte +115 -0
- package/dist/components/markdown/MarkdownBlocks.svelte.d.ts +15 -0
- package/dist/components/markdown/MarkdownCode.svelte +583 -0
- package/dist/components/markdown/MarkdownCode.svelte.d.ts +23 -0
- package/dist/components/markdown/MarkdownDiagram.svelte +310 -0
- package/dist/components/markdown/MarkdownDiagram.svelte.d.ts +6 -0
- package/dist/components/markdown/MarkdownFile.svelte +134 -0
- package/dist/components/markdown/MarkdownFile.svelte.d.ts +19 -0
- package/dist/components/markdown/MarkdownFootnotes.svelte +61 -0
- package/dist/components/markdown/MarkdownFootnotes.svelte.d.ts +8 -0
- package/dist/components/markdown/MarkdownFresh.svelte +69 -0
- package/dist/components/markdown/MarkdownFresh.svelte.d.ts +6 -0
- package/dist/components/markdown/MarkdownHtml.svelte +72 -0
- package/dist/components/markdown/MarkdownHtml.svelte.d.ts +7 -0
- package/dist/components/markdown/MarkdownImage.svelte +150 -0
- package/dist/components/markdown/MarkdownImage.svelte.d.ts +8 -0
- package/dist/components/markdown/MarkdownInlines.svelte +225 -0
- package/dist/components/markdown/MarkdownInlines.svelte.d.ts +7 -0
- package/dist/components/markdown/MarkdownLink.svelte +120 -0
- package/dist/components/markdown/MarkdownLink.svelte.d.ts +25 -0
- package/dist/components/markdown/MarkdownMath.svelte +138 -0
- package/dist/components/markdown/MarkdownMath.svelte.d.ts +7 -0
- package/dist/components/markdown/MarkdownTable.svelte +265 -0
- package/dist/components/markdown/MarkdownTable.svelte.d.ts +17 -0
- package/dist/components/markdown/caret.d.ts +14 -0
- package/dist/components/markdown/caret.js +67 -0
- package/dist/components/markdown/context.d.ts +93 -0
- package/dist/components/markdown/context.js +128 -0
- package/dist/components/markdown/copy.d.ts +13 -0
- package/dist/components/markdown/copy.js +74 -0
- package/dist/components/markdown/hidden.d.ts +12 -0
- package/dist/components/markdown/hidden.js +31 -0
- package/dist/components/markdown/kept.d.ts +32 -0
- package/dist/components/markdown/kept.js +71 -0
- package/dist/components/markdown/labels.d.ts +46 -0
- package/dist/components/markdown/labels.js +35 -0
- package/dist/components/markdown/save.d.ts +11 -0
- package/dist/components/markdown/save.js +58 -0
- package/dist/components/markdown/scrollable.d.ts +24 -0
- package/dist/components/markdown/scrollable.js +152 -0
- package/dist/components/markdown/static.d.ts +37 -0
- package/dist/components/markdown/static.js +233 -0
- package/dist/components/markdown/styles.d.ts +328 -0
- package/dist/components/markdown/styles.js +302 -0
- package/dist/components/menu/MenuRadioItem.svelte +3 -3
- package/dist/components/menu/index.d.ts +23 -0
- package/dist/components/menu/index.js +3 -1
- package/dist/components/message/MessageAction.svelte +22 -2
- package/dist/components/message/MessageAction.svelte.d.ts +9 -0
- package/dist/components/message/MessageContent.svelte +2 -3
- package/dist/components/message/MessageCopy.svelte +62 -3
- package/dist/components/message/MessageCopy.svelte.d.ts +17 -1
- package/dist/components/message/MessageHeader.svelte +3 -3
- package/dist/components/message/MessagePending.svelte +1 -1
- package/dist/components/message/MessageTime.svelte +1 -1
- package/dist/components/notice/Notice.svelte +14 -10
- package/dist/components/plan/Plan.svelte +22 -17
- package/dist/components/progress/Progress.svelte +31 -33
- package/dist/components/questionnaire/Questionnaire.svelte +19 -22
- package/dist/components/queued-message/QueuedMessage.svelte +14 -7
- package/dist/components/rail/RailItem.svelte +37 -34
- package/dist/components/rail/RailItem.svelte.d.ts +3 -1
- package/dist/components/rail/RailRoot.svelte +18 -4
- package/dist/components/rail/RailRoot.svelte.d.ts +1 -1
- package/dist/components/rail/RailSeparator.svelte +17 -0
- package/dist/components/rail/RailSeparator.svelte.d.ts +18 -0
- package/dist/components/rail/index.d.ts +2 -1
- package/dist/components/rail/index.js +2 -1
- package/dist/components/reasoning/ReasoningContent.svelte +4 -8
- package/dist/components/reasoning/ReasoningTrigger.svelte +2 -2
- package/dist/components/resize-handle/ResizeHandle.svelte +2 -3
- package/dist/components/segmented/SegmentedControl.svelte +7 -8
- package/dist/components/select/Select.svelte +1 -1
- package/dist/components/separator/Separator.svelte +1 -1
- package/dist/components/settings-row/SettingsRow.svelte +5 -5
- package/dist/components/shimmer/Shimmer.svelte +34 -21
- package/dist/components/sidebar/SidebarContent.svelte +1 -1
- package/dist/components/sidebar/SidebarFolder.svelte +142 -0
- package/dist/components/sidebar/SidebarFolder.svelte.d.ts +19 -0
- package/dist/components/sidebar/SidebarFooter.svelte +1 -1
- package/dist/components/sidebar/SidebarGroup.svelte +36 -32
- package/dist/components/sidebar/SidebarGroup.svelte.d.ts +3 -3
- package/dist/components/sidebar/SidebarHeader.svelte +12 -6
- package/dist/components/sidebar/SidebarHeader.svelte.d.ts +4 -1
- package/dist/components/sidebar/SidebarItem.svelte +26 -32
- package/dist/components/sidebar/SidebarRoot.svelte +62 -0
- package/dist/components/sidebar/SidebarRoot.svelte.d.ts +14 -0
- package/dist/components/sidebar/index.d.ts +5 -1
- package/dist/components/sidebar/index.js +3 -1
- package/dist/components/skeleton/Skeleton.svelte +26 -15
- package/dist/components/slider/Slider.svelte +11 -10
- package/dist/components/spinner/Spinner.svelte +4 -11
- package/dist/components/status-indicator/StatusIndicator.svelte +1 -1
- package/dist/components/suggestions/SuggestionsItem.svelte +5 -5
- package/dist/components/switch/Switch.svelte +9 -14
- package/dist/components/tab-strip/TabStrip.svelte +340 -0
- package/dist/components/tab-strip/TabStrip.svelte.d.ts +31 -0
- package/dist/components/tabs/Tabs.svelte +13 -9
- package/dist/components/tabs/Tabs.svelte.d.ts +4 -0
- package/dist/components/text/Text.svelte +31 -23
- package/dist/components/text/Text.svelte.d.ts +8 -4
- package/dist/components/toast/Toaster.svelte +5 -5
- package/dist/components/tool/ToolContent.svelte +3 -8
- package/dist/components/tool/ToolSection.svelte +4 -5
- package/dist/components/tool/ToolTrigger.svelte +10 -9
- package/dist/components/tooltip/TooltipContent.svelte +3 -3
- package/dist/components/work-fold/WorkFold.svelte +5 -9
- package/dist/index.d.ts +16 -8
- package/dist/index.js +17 -10
- package/dist/internal/Code.svelte +3 -4
- package/dist/internal/Glyph.svelte +16 -0
- package/dist/internal/Glyph.svelte.d.ts +6 -0
- package/dist/internal/Tile.svelte +17 -37
- package/dist/internal/control.d.ts +4 -4
- package/dist/internal/control.js +20 -17
- package/dist/internal/layer.d.ts +3 -5
- package/dist/internal/layer.js +10 -12
- package/dist/internal/markers.stylex.d.ts +8 -0
- package/dist/internal/markers.stylex.js +4 -0
- package/dist/internal/plugin-order.d.ts +8 -0
- package/dist/internal/plugin-order.js +16 -0
- package/dist/internal/roving.js +5 -2
- package/dist/internal/shared.d.ts +8 -1
- package/dist/internal/shared.js +21 -5
- package/dist/internal/sx.d.ts +1 -1
- package/dist/internal/toggle.d.ts +1 -2
- package/dist/internal/toggle.js +8 -12
- package/dist/internal/visible.d.ts +32 -0
- package/dist/internal/visible.js +44 -0
- package/dist/markdown/ast.d.ts +209 -0
- package/dist/markdown/ast.js +10 -0
- package/dist/markdown/blocks.d.ts +177 -0
- package/dist/markdown/blocks.js +1489 -0
- package/dist/markdown/code-info.d.ts +10 -0
- package/dist/markdown/code-info.js +16 -0
- package/dist/markdown/common.d.ts +25 -0
- package/dist/markdown/common.js +107 -0
- package/dist/markdown/diagrams.d.ts +2 -0
- package/dist/markdown/diagrams.js +18 -0
- package/dist/markdown/file-paths.d.ts +29 -0
- package/dist/markdown/file-paths.js +80 -0
- package/dist/markdown/files.d.ts +16 -0
- package/dist/markdown/files.js +133 -0
- package/dist/markdown/highlight.d.ts +30 -0
- package/dist/markdown/highlight.js +127 -0
- package/dist/markdown/html.d.ts +23 -0
- package/dist/markdown/html.js +289 -0
- package/dist/markdown/images.d.ts +15 -0
- package/dist/markdown/images.js +45 -0
- package/dist/markdown/index.d.ts +12 -0
- package/dist/markdown/index.js +5 -0
- package/dist/markdown/inlines.d.ts +48 -0
- package/dist/markdown/inlines.js +1714 -0
- package/dist/markdown/math.d.ts +2 -0
- package/dist/markdown/math.js +22 -0
- package/dist/markdown/parse.d.ts +81 -0
- package/dist/markdown/parse.js +204 -0
- package/dist/markdown/plugin.d.ts +178 -0
- package/dist/markdown/plugin.js +160 -0
- package/dist/markdown/sanitize.d.ts +78 -0
- package/dist/markdown/sanitize.js +522 -0
- package/dist/markdown/share.d.ts +12 -0
- package/dist/markdown/share.js +59 -0
- package/dist/markdown/table.d.ts +13 -0
- package/dist/markdown/table.js +60 -0
- package/dist/preset.d.ts +40 -16
- package/dist/preset.js +169 -51
- package/dist/tokens.stylex.d.ts +55 -37
- package/dist/tokens.stylex.js +107 -62
- package/dist/typography.d.ts +48 -0
- package/dist/typography.js +55 -0
- package/dist/vite.d.ts +7 -1
- package/dist/vite.js +27 -2
- package/package.json +53 -6
- package/skills/building-ui/SKILL.md +242 -0
- package/skills/building-ui/references/components.md +207 -0
- package/skills/markdown/SKILL.md +452 -0
- package/skills/markdown/references/plugins.md +103 -0
- package/skills/setup/SKILL.md +191 -0
- package/skills/styling/SKILL.md +269 -0
- package/skills/theming/SKILL.md +184 -0
- package/dist/internal/loop.d.ts +0 -35
- package/dist/internal/loop.js +0 -72
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
A Mermaid diagram, drawn by Pele as SVG in Narvi's colours. Pele's core loads the first time
|
|
3
|
+
a diagram is shown, and each kind of diagram's parser and layout the first time one of that
|
|
4
|
+
kind is. A diagram that cannot be drawn shows why, over its source. What Pele draws is
|
|
5
|
+
sanitized: a diagram can come from text anyone wrote.
|
|
6
|
+
-->
|
|
7
|
+
<script lang="ts" module>
|
|
8
|
+
import * as stylex from '@stylexjs/stylex';
|
|
9
|
+
import type { DiagramType, Mounted } from 'pele/lazy';
|
|
10
|
+
import { attrs } from '../../internal/sx.js';
|
|
11
|
+
import type { CodeBlock } from '../../markdown/ast.js';
|
|
12
|
+
import type { NodeProps } from '../../markdown/plugin.js';
|
|
13
|
+
import { colors, font, radius } from '../../tokens.stylex.js';
|
|
14
|
+
import Button from '../button/Button.svelte';
|
|
15
|
+
import * as Dialog from '../dialog/index.js';
|
|
16
|
+
import { getMarkdownContext } from './context.js';
|
|
17
|
+
import MarkdownCode from './MarkdownCode.svelte';
|
|
18
|
+
import { scrollable } from './scrollable.js';
|
|
19
|
+
import { prose } from './styles.js';
|
|
20
|
+
|
|
21
|
+
export type MarkdownDiagramProps = NodeProps<CodeBlock>;
|
|
22
|
+
|
|
23
|
+
type Pele = typeof import('pele/lazy');
|
|
24
|
+
|
|
25
|
+
let core: Promise<Pele> | null = null;
|
|
26
|
+
// A cache shared by every diagram, never read by the page: a plain Map is right here
|
|
27
|
+
// eslint-disable-next-line svelte/prefer-svelte-reactivity
|
|
28
|
+
const kinds = new Map<DiagramType, Promise<unknown>>();
|
|
29
|
+
|
|
30
|
+
/** Pele, ready to draw `source`: with what draws its kind of diagram loaded. */
|
|
31
|
+
async function peleFor(source: string): Promise<Pele> {
|
|
32
|
+
core ??= import('pele/lazy').catch((cause: unknown) => {
|
|
33
|
+
// Asked again the next time, after it failed to load
|
|
34
|
+
core = null;
|
|
35
|
+
throw cause;
|
|
36
|
+
});
|
|
37
|
+
const pele = await core;
|
|
38
|
+
let kind: DiagramType | null = null;
|
|
39
|
+
try {
|
|
40
|
+
kind = pele.detectType(source);
|
|
41
|
+
} catch {
|
|
42
|
+
// Drawing it says what is wrong with it
|
|
43
|
+
}
|
|
44
|
+
if (kind !== null) {
|
|
45
|
+
const type = kind;
|
|
46
|
+
let loading = kinds.get(type);
|
|
47
|
+
if (!loading) {
|
|
48
|
+
loading = pele.load(type).catch((cause: unknown) => {
|
|
49
|
+
kinds.delete(type);
|
|
50
|
+
throw cause;
|
|
51
|
+
});
|
|
52
|
+
kinds.set(type, loading);
|
|
53
|
+
}
|
|
54
|
+
await loading;
|
|
55
|
+
}
|
|
56
|
+
return pele;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
let purify: Promise<(svg: string) => Node> | null = null;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* An SVG with nothing that could navigate, run or fetch. Pele escapes its text and is asked
|
|
63
|
+
* for no links or images, so this is a second guard.
|
|
64
|
+
*/
|
|
65
|
+
function sanitizer(): Promise<(svg: string) => Node> {
|
|
66
|
+
purify ??= import('dompurify').then(
|
|
67
|
+
({ default: DOMPurify }) => {
|
|
68
|
+
const purifier = DOMPurify(window);
|
|
69
|
+
return (svg: string) =>
|
|
70
|
+
purifier.sanitize(svg, {
|
|
71
|
+
FORBID_ATTR: ['href', 'xlink:href', 'src', 'srcset'],
|
|
72
|
+
FORBID_TAGS: ['a', 'image', 'foreignObject', 'style'],
|
|
73
|
+
USE_PROFILES: { svg: true, svgFilters: true },
|
|
74
|
+
RETURN_DOM_FRAGMENT: true
|
|
75
|
+
});
|
|
76
|
+
},
|
|
77
|
+
(cause: unknown) => {
|
|
78
|
+
// Asked again the next time, after it failed to load
|
|
79
|
+
purify = null;
|
|
80
|
+
throw cause;
|
|
81
|
+
}
|
|
82
|
+
);
|
|
83
|
+
return purify;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const styles = stylex.create({
|
|
87
|
+
frame: {
|
|
88
|
+
minWidth: 0,
|
|
89
|
+
borderRadius: radius.control,
|
|
90
|
+
// An opaque surface: Pele paints it behind the labels on lines, so a line stops at its label
|
|
91
|
+
backgroundColor: colors.bgRaised,
|
|
92
|
+
boxShadow: `inset 0 0 0 1px ${colors.border}`,
|
|
93
|
+
outlineStyle: 'solid',
|
|
94
|
+
outlineWidth: '1px',
|
|
95
|
+
outlineColor: 'transparent',
|
|
96
|
+
outlineOffset: '-1px',
|
|
97
|
+
// Pele's colours, from Narvi's tokens, so a diagram follows the theme without being drawn again
|
|
98
|
+
'--pele-bg': colors.bgRaised,
|
|
99
|
+
'--pele-fg': colors.text,
|
|
100
|
+
'--pele-muted': colors.textMuted,
|
|
101
|
+
'--pele-line': colors.textMuted,
|
|
102
|
+
'--pele-surface': colors.fillSunken,
|
|
103
|
+
'--pele-surface-alt': colors.fillMuted,
|
|
104
|
+
'--pele-border': colors.borderStrong,
|
|
105
|
+
'--pele-accent': colors.accent,
|
|
106
|
+
'--pele-critical': colors.danger,
|
|
107
|
+
'--pele-font': 'inherit',
|
|
108
|
+
'--pele-font-mono': font.mono,
|
|
109
|
+
'--pele-radius': radius.item,
|
|
110
|
+
'--pele-series-1': colors.accent,
|
|
111
|
+
'--pele-series-2': colors.codeString,
|
|
112
|
+
'--pele-series-3': colors.codeNumber,
|
|
113
|
+
'--pele-series-4': colors.codeLiteral,
|
|
114
|
+
'--pele-series-5': colors.codeKey,
|
|
115
|
+
'--pele-series-6': colors.success,
|
|
116
|
+
'--pele-series-7': colors.warning,
|
|
117
|
+
'--pele-series-8': colors.danger
|
|
118
|
+
},
|
|
119
|
+
header: {
|
|
120
|
+
display: 'flex',
|
|
121
|
+
alignItems: 'center',
|
|
122
|
+
justifyContent: 'space-between',
|
|
123
|
+
gap: '0.5rem',
|
|
124
|
+
minHeight: '2rem',
|
|
125
|
+
paddingInlineStart: '0.75rem',
|
|
126
|
+
paddingInlineEnd: '0.25rem',
|
|
127
|
+
color: colors.textSubtle,
|
|
128
|
+
fontSize: font.sizeCaption,
|
|
129
|
+
lineHeight: 1,
|
|
130
|
+
userSelect: 'none',
|
|
131
|
+
WebkitUserSelect: 'none'
|
|
132
|
+
},
|
|
133
|
+
canvas: {
|
|
134
|
+
display: 'flex',
|
|
135
|
+
// `safe`: a diagram wider than the canvas starts at its edge, where it can be scrolled to
|
|
136
|
+
justifyContent: 'safe center',
|
|
137
|
+
paddingBlockEnd: '0.75rem',
|
|
138
|
+
paddingInline: '0.75rem',
|
|
139
|
+
overflowX: 'auto',
|
|
140
|
+
scrollbarWidth: 'thin'
|
|
141
|
+
},
|
|
142
|
+
hidden: { display: 'none' },
|
|
143
|
+
actions: { display: 'inline-flex', gap: '0.125rem' },
|
|
144
|
+
// As large as the window lets it be
|
|
145
|
+
viewer: { maxWidth: 'min(92vw, 80rem)', height: 'min(86vh, 56rem)' },
|
|
146
|
+
stage: {
|
|
147
|
+
flexGrow: 1,
|
|
148
|
+
minHeight: 0,
|
|
149
|
+
marginInline: '1rem',
|
|
150
|
+
marginBlockEnd: '1rem',
|
|
151
|
+
overflow: 'hidden',
|
|
152
|
+
boxShadow: 'none',
|
|
153
|
+
backgroundColor: 'transparent',
|
|
154
|
+
'--pele-bg': colors.bgRaised
|
|
155
|
+
},
|
|
156
|
+
failure: {
|
|
157
|
+
margin: 0,
|
|
158
|
+
paddingInline: '0.75rem',
|
|
159
|
+
paddingBlockEnd: '0.5rem',
|
|
160
|
+
color: colors.danger,
|
|
161
|
+
fontSize: '0.8125em'
|
|
162
|
+
},
|
|
163
|
+
code: { paddingInline: '0.5rem', paddingBlockEnd: '0.5rem' }
|
|
164
|
+
});
|
|
165
|
+
</script>
|
|
166
|
+
|
|
167
|
+
<script lang="ts">
|
|
168
|
+
let { node }: MarkdownDiagramProps = $props();
|
|
169
|
+
|
|
170
|
+
const md = getMarkdownContext();
|
|
171
|
+
|
|
172
|
+
const source = $derived(node.value.trim());
|
|
173
|
+
|
|
174
|
+
/** Why it could not be drawn; `reload` when what draws diagrams did not load. */
|
|
175
|
+
let failure = $state<{ message: string; reload: boolean } | null>(null);
|
|
176
|
+
/** Bumped to load again after a failure to. */
|
|
177
|
+
let attempt = $state(0);
|
|
178
|
+
let showCode = $state(false);
|
|
179
|
+
/** Whether the diagram has been drawn once. Until then its code holds its place. */
|
|
180
|
+
let drawn = $state(false);
|
|
181
|
+
let canvas = $state<HTMLElement | null>(null);
|
|
182
|
+
|
|
183
|
+
let expanded = $state(false);
|
|
184
|
+
let stage = $state<HTMLElement | null>(null);
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Pele shrinks a diagram to the width it has. In a narrow column that makes its labels too
|
|
188
|
+
* small to read, so past three quarters of its own size the diagram scrolls instead.
|
|
189
|
+
*/
|
|
190
|
+
function keepLegible(element: HTMLElement) {
|
|
191
|
+
const svg = element.querySelector<SVGSVGElement>(':scope > svg');
|
|
192
|
+
const natural = Number(svg?.getAttribute('width'));
|
|
193
|
+
if (svg && natural > 0) svg.style.minWidth = `${Math.round(natural * 0.75)}px`;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Draws the diagram in an element, and again when its source changes. `zoom` lets it pan and zoom. */
|
|
197
|
+
function draw(element: HTMLElement | null, zoom: boolean) {
|
|
198
|
+
const text = source;
|
|
199
|
+
void attempt;
|
|
200
|
+
if (!element) return;
|
|
201
|
+
let gone = false;
|
|
202
|
+
let mounted: Mounted | null = null;
|
|
203
|
+
void Promise.all([peleFor(text), sanitizer()]).then(
|
|
204
|
+
([pele, sanitize]) => {
|
|
205
|
+
if (gone) return;
|
|
206
|
+
mounted = pele.mount(element, text, {
|
|
207
|
+
links: false,
|
|
208
|
+
images: false,
|
|
209
|
+
zoom: zoom
|
|
210
|
+
? {
|
|
211
|
+
always: true,
|
|
212
|
+
labels: {
|
|
213
|
+
zoomIn: md.labels.zoomIn,
|
|
214
|
+
zoomOut: md.labels.zoomOut,
|
|
215
|
+
reset: md.labels.resetZoom
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
: false,
|
|
219
|
+
// The author's direction: a wide diagram scrolls rather than turning on its side
|
|
220
|
+
autoDirection: false,
|
|
221
|
+
sanitize,
|
|
222
|
+
onRender: () => {
|
|
223
|
+
failure = null;
|
|
224
|
+
drawn = true;
|
|
225
|
+
if (!zoom) keepLegible(element);
|
|
226
|
+
},
|
|
227
|
+
onError: (cause) =>
|
|
228
|
+
(failure = {
|
|
229
|
+
message: cause instanceof Error ? cause.message : md.labels.diagramInvalid,
|
|
230
|
+
reload: false
|
|
231
|
+
})
|
|
232
|
+
});
|
|
233
|
+
},
|
|
234
|
+
() => {
|
|
235
|
+
if (!gone) failure = { message: md.labels.diagramNotLoaded, reload: true };
|
|
236
|
+
}
|
|
237
|
+
);
|
|
238
|
+
return () => {
|
|
239
|
+
gone = true;
|
|
240
|
+
mounted?.destroy();
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
$effect(() => draw(canvas, false));
|
|
245
|
+
// The full-size view draws its own copy, which pans and zooms
|
|
246
|
+
$effect(() => draw(stage, true));
|
|
247
|
+
</script>
|
|
248
|
+
|
|
249
|
+
<div data-markdown-diagram {...attrs([prose.block, styles.frame])}>
|
|
250
|
+
<div data-markdown-chrome {...attrs([styles.header])}>
|
|
251
|
+
<span>{md.labels.diagram}</span>
|
|
252
|
+
{#if failure?.reload}
|
|
253
|
+
<Button variant="ghost" size="sm" onclick={() => (attempt += 1)}>{md.labels.tryAgain}</Button>
|
|
254
|
+
{:else if !failure}
|
|
255
|
+
<span {...attrs([styles.actions])}>
|
|
256
|
+
<Button variant="ghost" size="sm" onclick={() => (showCode = !showCode)}>
|
|
257
|
+
{showCode ? md.labels.showDiagram : md.labels.showCode}
|
|
258
|
+
</Button>
|
|
259
|
+
<Button variant="ghost" size="sm" onclick={() => (expanded = true)}
|
|
260
|
+
>{md.labels.expand}</Button
|
|
261
|
+
>
|
|
262
|
+
</span>
|
|
263
|
+
{/if}
|
|
264
|
+
</div>
|
|
265
|
+
{#if failure}
|
|
266
|
+
<!-- Said, not shouted: a thread can open with several of these -->
|
|
267
|
+
<p role="status" {...attrs([styles.failure])}>
|
|
268
|
+
{md.labels.diagramFailed(failure.message)}
|
|
269
|
+
</p>
|
|
270
|
+
{/if}
|
|
271
|
+
<!-- Kept in place while the code shows, so going back draws nothing again -->
|
|
272
|
+
<div
|
|
273
|
+
bind:this={canvas}
|
|
274
|
+
data-markdown-diagram-canvas
|
|
275
|
+
use:scrollable
|
|
276
|
+
{...attrs([styles.canvas, (showCode || failure !== null || !drawn) && styles.hidden])}
|
|
277
|
+
></div>
|
|
278
|
+
<!-- Until the diagram is first drawn its code holds the place, so the reply does not
|
|
279
|
+
collapse and grow again while what draws it loads -->
|
|
280
|
+
{#if showCode || failure || !drawn}
|
|
281
|
+
<div {...attrs([styles.code])}><MarkdownCode {node} /></div>
|
|
282
|
+
{/if}
|
|
283
|
+
</div>
|
|
284
|
+
|
|
285
|
+
<Dialog.Root bind:open={expanded}>
|
|
286
|
+
<Dialog.Content sx={styles.viewer} aria-describedby={undefined}>
|
|
287
|
+
<Dialog.Header>
|
|
288
|
+
<Dialog.Title>{md.labels.diagram}</Dialog.Title>
|
|
289
|
+
</Dialog.Header>
|
|
290
|
+
<!-- Drag to pan; scroll or pinch to zoom -->
|
|
291
|
+
<div
|
|
292
|
+
bind:this={stage}
|
|
293
|
+
data-markdown-diagram-stage
|
|
294
|
+
{...attrs([styles.frame, styles.stage])}
|
|
295
|
+
></div>
|
|
296
|
+
</Dialog.Content>
|
|
297
|
+
</Dialog.Root>
|
|
298
|
+
|
|
299
|
+
<!--
|
|
300
|
+
Pele sizes its SVG to the diagram, with inline styles it also uses to zoom. In the full-size
|
|
301
|
+
view the SVG fills the stage instead, centred by its view box, so the diagram is as large as
|
|
302
|
+
the window lets it be. StyleX cannot reach an element Pele makes, hence the style block.
|
|
303
|
+
-->
|
|
304
|
+
<style>
|
|
305
|
+
:global([data-markdown-diagram-stage] > svg) {
|
|
306
|
+
width: 100% !important;
|
|
307
|
+
height: 100% !important;
|
|
308
|
+
max-width: none !important;
|
|
309
|
+
}
|
|
310
|
+
</style>
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { CodeBlock } from '../../markdown/ast.js';
|
|
2
|
+
import type { NodeProps } from '../../markdown/plugin.js';
|
|
3
|
+
export type MarkdownDiagramProps = NodeProps<CodeBlock>;
|
|
4
|
+
declare const MarkdownDiagram: import("svelte").Component<MarkdownDiagramProps, {}, "">;
|
|
5
|
+
type MarkdownDiagram = ReturnType<typeof MarkdownDiagram>;
|
|
6
|
+
export default MarkdownDiagram;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
A file named in a reply, as a chip: its name, with the whole path as its title. With a way
|
|
3
|
+
to open files it is a button; without one it only shows.
|
|
4
|
+
-->
|
|
5
|
+
<script lang="ts" module>
|
|
6
|
+
import * as stylex from '@stylexjs/stylex';
|
|
7
|
+
import type { Snippet } from 'svelte';
|
|
8
|
+
import Glyph, { glyphHtml } from '../../internal/Glyph.svelte';
|
|
9
|
+
import { attrs, classes } from '../../internal/sx.js';
|
|
10
|
+
import type { StaticTools } from '../../markdown/plugin.js';
|
|
11
|
+
import type { InlineCustom } from '../../markdown/ast.js';
|
|
12
|
+
import { formatPosition, type FileReference } from '../../markdown/file-paths.js';
|
|
13
|
+
import { colors, motion, radius } from '../../tokens.stylex.js';
|
|
14
|
+
import { media } from '../../media.stylex.js';
|
|
15
|
+
|
|
16
|
+
export type MarkdownFileProps = {
|
|
17
|
+
node: InlineCustom;
|
|
18
|
+
children?: Snippet;
|
|
19
|
+
open?: (file: FileReference, event: MouseEvent) => void;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const styles = stylex.create({
|
|
23
|
+
chip: {
|
|
24
|
+
display: 'inline-flex',
|
|
25
|
+
alignItems: 'center',
|
|
26
|
+
gap: '0.25em',
|
|
27
|
+
maxWidth: '100%',
|
|
28
|
+
margin: 0,
|
|
29
|
+
paddingBlock: '0.0625em',
|
|
30
|
+
paddingInline: '0.3125em',
|
|
31
|
+
borderWidth: 0,
|
|
32
|
+
borderRadius: radius.chip,
|
|
33
|
+
backgroundColor: colors.fillSunken,
|
|
34
|
+
color: colors.text,
|
|
35
|
+
fontSize: '0.875em',
|
|
36
|
+
lineHeight: 'inherit',
|
|
37
|
+
verticalAlign: 'baseline',
|
|
38
|
+
textAlign: 'start',
|
|
39
|
+
textDecorationLine: 'none',
|
|
40
|
+
// The edge is a shadow, which forced-colours mode removes
|
|
41
|
+
outlineStyle: 'solid',
|
|
42
|
+
outlineWidth: '1px',
|
|
43
|
+
outlineColor: 'transparent',
|
|
44
|
+
outlineOffset: '-1px'
|
|
45
|
+
},
|
|
46
|
+
button: {
|
|
47
|
+
cursor: 'pointer',
|
|
48
|
+
appearance: 'none',
|
|
49
|
+
backgroundColor: {
|
|
50
|
+
default: colors.fillSunken,
|
|
51
|
+
':hover': { default: null, [media.hover]: colors.fillSelected }
|
|
52
|
+
},
|
|
53
|
+
outlineWidth: { default: '1px', ':focus-visible': '2px' },
|
|
54
|
+
outlineColor: { default: 'transparent', ':focus-visible': colors.focusRing },
|
|
55
|
+
outlineOffset: { default: '-1px', ':focus-visible': '1px' },
|
|
56
|
+
transitionProperty: 'background-color',
|
|
57
|
+
transitionDuration: { default: '0s', [media.motion]: motion.fast }
|
|
58
|
+
},
|
|
59
|
+
// A chip made from a link keeps the link's words, in the text's own face
|
|
60
|
+
words: { fontFamily: 'inherit', fontSize: '0.9375em' },
|
|
61
|
+
icon: { flexShrink: 0, color: colors.textSubtle },
|
|
62
|
+
label: { minWidth: 0, overflowWrap: 'anywhere' }
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
const chipClass = {
|
|
66
|
+
shown: classes(styles.chip),
|
|
67
|
+
shownWords: classes(styles.chip, styles.words),
|
|
68
|
+
button: classes(styles.chip, styles.button),
|
|
69
|
+
buttonWords: classes(styles.chip, styles.button, styles.words)
|
|
70
|
+
};
|
|
71
|
+
const iconClass = classes(styles.icon);
|
|
72
|
+
const labelClass = classes(styles.label);
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* A chip as markup, as this component draws it: for `static`. As a button it carries the
|
|
76
|
+
* file it names, for `chipFile` to read back when it is pressed.
|
|
77
|
+
*/
|
|
78
|
+
export function fileHtml(node: InlineCustom, opens: boolean, tools: StaticTools): string {
|
|
79
|
+
const file = node.data as FileReference & { source: string; link?: boolean };
|
|
80
|
+
const attributes =
|
|
81
|
+
` data-markdown-file="true" data-markdown-copy="${tools.escape(file.source)}"` +
|
|
82
|
+
` title="${tools.escape(file.path + formatPosition(file))}"`;
|
|
83
|
+
const inside = `${glyphHtml('file', 12, 1.5, iconClass)}<span class="${labelClass}">${tools.children()}</span>`;
|
|
84
|
+
if (!opens)
|
|
85
|
+
return `<span${attributes} class="${file.link ? chipClass.shownWords : chipClass.shown}">${inside}</span>`;
|
|
86
|
+
const { path, line, column, endLine } = file;
|
|
87
|
+
const kept = tools.escape(JSON.stringify({ path, line, column, endLine }));
|
|
88
|
+
return `<button type="button"${attributes}${tools.part} data-file="${kept}" class="${file.link ? chipClass.buttonWords : chipClass.button}">${inside}</button>`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The file a chip written by `fileHtml` names. */
|
|
92
|
+
export function chipFile(chip: HTMLElement): FileReference | null {
|
|
93
|
+
try {
|
|
94
|
+
const file = JSON.parse(chip.dataset.file ?? '') as FileReference;
|
|
95
|
+
return typeof file.path === 'string' ? file : null;
|
|
96
|
+
} catch {
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
</script>
|
|
101
|
+
|
|
102
|
+
<script lang="ts">
|
|
103
|
+
let { node, children, open }: MarkdownFileProps = $props();
|
|
104
|
+
|
|
105
|
+
const file = $derived(node.data as FileReference & { source: string; link?: boolean });
|
|
106
|
+
const title = $derived(file.path + formatPosition(file));
|
|
107
|
+
</script>
|
|
108
|
+
|
|
109
|
+
{#snippet content()}
|
|
110
|
+
<Glyph name="file" size={12} {...attrs([styles.icon])} />
|
|
111
|
+
<span {...attrs([styles.label])}>{@render children?.()}</span>
|
|
112
|
+
{/snippet}
|
|
113
|
+
|
|
114
|
+
{#if open}
|
|
115
|
+
<button
|
|
116
|
+
type="button"
|
|
117
|
+
data-markdown-file
|
|
118
|
+
data-markdown-copy={file.source}
|
|
119
|
+
{title}
|
|
120
|
+
onclick={(event) => open(file, event)}
|
|
121
|
+
{...attrs([styles.chip, styles.button, file.link && styles.words])}
|
|
122
|
+
>
|
|
123
|
+
{@render content()}
|
|
124
|
+
</button>
|
|
125
|
+
{:else}
|
|
126
|
+
<span
|
|
127
|
+
data-markdown-file
|
|
128
|
+
data-markdown-copy={file.source}
|
|
129
|
+
{title}
|
|
130
|
+
{...attrs([styles.chip, file.link && styles.words])}
|
|
131
|
+
>
|
|
132
|
+
{@render content()}
|
|
133
|
+
</span>
|
|
134
|
+
{/if}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
import type { StaticTools } from '../../markdown/plugin.js';
|
|
3
|
+
import type { InlineCustom } from '../../markdown/ast.js';
|
|
4
|
+
import { type FileReference } from '../../markdown/file-paths.js';
|
|
5
|
+
export type MarkdownFileProps = {
|
|
6
|
+
node: InlineCustom;
|
|
7
|
+
children?: Snippet;
|
|
8
|
+
open?: (file: FileReference, event: MouseEvent) => void;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* A chip as markup, as this component draws it: for `static`. As a button it carries the
|
|
12
|
+
* file it names, for `chipFile` to read back when it is pressed.
|
|
13
|
+
*/
|
|
14
|
+
export declare function fileHtml(node: InlineCustom, opens: boolean, tools: StaticTools): string;
|
|
15
|
+
/** The file a chip written by `fileHtml` names. */
|
|
16
|
+
export declare function chipFile(chip: HTMLElement): FileReference | null;
|
|
17
|
+
declare const MarkdownFile: import("svelte").Component<MarkdownFileProps, {}, "">;
|
|
18
|
+
type MarkdownFile = ReturnType<typeof MarkdownFile>;
|
|
19
|
+
export default MarkdownFile;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
<!-- The footnotes a document refers to, in the order they are first referred to. -->
|
|
2
|
+
<script lang="ts" module>
|
|
3
|
+
import { attrs } from '../../internal/sx.js';
|
|
4
|
+
import type { FootnoteDefinition } from '../../markdown/ast.js';
|
|
5
|
+
import Link from '../link/Link.svelte';
|
|
6
|
+
import { getMarkdownContext } from './context.js';
|
|
7
|
+
import MarkdownBlock from './MarkdownBlock.svelte';
|
|
8
|
+
import { prose } from './styles.js';
|
|
9
|
+
|
|
10
|
+
export type MarkdownFootnotesProps = {
|
|
11
|
+
footnotes: FootnoteDefinition[];
|
|
12
|
+
order: ReadonlyMap<string, number>;
|
|
13
|
+
};
|
|
14
|
+
</script>
|
|
15
|
+
|
|
16
|
+
<script lang="ts">
|
|
17
|
+
let { footnotes, order }: MarkdownFootnotesProps = $props();
|
|
18
|
+
|
|
19
|
+
const md = getMarkdownContext();
|
|
20
|
+
|
|
21
|
+
const used = $derived(
|
|
22
|
+
footnotes
|
|
23
|
+
.filter((f) => order.has(f.label.toLowerCase()))
|
|
24
|
+
.sort((a, b) => order.get(a.label.toLowerCase())! - order.get(b.label.toLowerCase())!)
|
|
25
|
+
);
|
|
26
|
+
</script>
|
|
27
|
+
|
|
28
|
+
{#if used.length > 0}
|
|
29
|
+
<section {...attrs([prose.footnotes])} aria-labelledby="{md.id}-footnotes">
|
|
30
|
+
<svelte:element
|
|
31
|
+
this={`h${md.headingLevel(2)}`}
|
|
32
|
+
id="{md.id}-footnotes"
|
|
33
|
+
data-markdown-chrome
|
|
34
|
+
{...attrs([prose.srOnly])}
|
|
35
|
+
>
|
|
36
|
+
{md.labels.footnotes}
|
|
37
|
+
</svelte:element>
|
|
38
|
+
<ol {...attrs([prose.footnoteList])}>
|
|
39
|
+
{#each used as footnote (footnote.id)}
|
|
40
|
+
{@const label = footnote.label.toLowerCase()}
|
|
41
|
+
<li id="{md.id}-fn-{label}" tabindex="-1" {...attrs([prose.item, prose.footnote])}>
|
|
42
|
+
<!-- A footnote of one paragraph runs on into its back link -->
|
|
43
|
+
{#each footnote.children as child (child.id)}
|
|
44
|
+
<MarkdownBlock
|
|
45
|
+
node={child}
|
|
46
|
+
tight={footnote.children.length === 1 && child.type === 'paragraph'}
|
|
47
|
+
/>
|
|
48
|
+
{/each}
|
|
49
|
+
<Link
|
|
50
|
+
href="#{md.id}-fnref-{label}"
|
|
51
|
+
onclick={(event: MouseEvent) => {
|
|
52
|
+
if (md.reveal(`${md.id}-fnref-${label}`)) event.preventDefault();
|
|
53
|
+
}}
|
|
54
|
+
aria-label={md.labels.backToReference(order.get(label) ?? 0)}
|
|
55
|
+
sx={prose.backref}>↩</Link
|
|
56
|
+
>
|
|
57
|
+
</li>
|
|
58
|
+
{/each}
|
|
59
|
+
</ol>
|
|
60
|
+
</section>
|
|
61
|
+
{/if}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { FootnoteDefinition } from '../../markdown/ast.js';
|
|
2
|
+
export type MarkdownFootnotesProps = {
|
|
3
|
+
footnotes: FootnoteDefinition[];
|
|
4
|
+
order: ReadonlyMap<string, number>;
|
|
5
|
+
};
|
|
6
|
+
declare const MarkdownFootnotes: import("svelte").Component<MarkdownFootnotesProps, {}, "">;
|
|
7
|
+
type MarkdownFootnotes = ReturnType<typeof MarkdownFootnotes>;
|
|
8
|
+
export default MarkdownFootnotes;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
Text that fades in as it arrives. What was there stays as it is; what is new since the
|
|
3
|
+
last look is set in a span of its own, which fades in once and is then folded back into
|
|
4
|
+
the text before it. So a sentence appears a few words at a time, and a moment later is
|
|
5
|
+
one run of text again, with nothing left behind.
|
|
6
|
+
|
|
7
|
+
Text that changes rather than grows (a mark that was held back and is now shown) does not
|
|
8
|
+
fade: it replaces what the reader was already looking at.
|
|
9
|
+
-->
|
|
10
|
+
<script lang="ts" module>
|
|
11
|
+
import * as stylex from '@stylexjs/stylex';
|
|
12
|
+
import { classes } from '../../internal/sx.js';
|
|
13
|
+
import { media } from '../../media.stylex.js';
|
|
14
|
+
import { motion } from '../../tokens.stylex.js';
|
|
15
|
+
|
|
16
|
+
const appear = stylex.keyframes({ from: { opacity: 0 }, to: { opacity: 1 } });
|
|
17
|
+
|
|
18
|
+
const styles = stylex.create({
|
|
19
|
+
fresh: {
|
|
20
|
+
animationName: { default: null, [media.motion]: appear },
|
|
21
|
+
animationDuration: motion.slow,
|
|
22
|
+
animationTimingFunction: motion.easeOut,
|
|
23
|
+
animationFillMode: 'backwards'
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
const freshClass = classes(styles.fresh);
|
|
27
|
+
|
|
28
|
+
/** How long a piece stays a span of its own: the fade, and a little over. */
|
|
29
|
+
const LIFE = 260;
|
|
30
|
+
</script>
|
|
31
|
+
|
|
32
|
+
<script lang="ts">
|
|
33
|
+
let { value }: { value: string } = $props();
|
|
34
|
+
|
|
35
|
+
// Notes to itself, not state: what was shown last, how much of it has settled, and the
|
|
36
|
+
// pieces still fading in, oldest first
|
|
37
|
+
let previous = '';
|
|
38
|
+
let settled = 0;
|
|
39
|
+
let pieces: Array<{ from: number; to: number; until: number }> = [];
|
|
40
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
41
|
+
/** Bumped when a piece's time is up, so the text is looked at again and the piece folded in. */
|
|
42
|
+
let due = $state(0);
|
|
43
|
+
|
|
44
|
+
const parts = $derived.by(() => {
|
|
45
|
+
void due;
|
|
46
|
+
const now = performance.now();
|
|
47
|
+
if (value.length < previous.length || value.slice(0, previous.length) !== previous) {
|
|
48
|
+
pieces = [];
|
|
49
|
+
settled = value.length;
|
|
50
|
+
} else if (value.length > previous.length) {
|
|
51
|
+
pieces.push({ from: previous.length, to: value.length, until: now + LIFE });
|
|
52
|
+
}
|
|
53
|
+
previous = value;
|
|
54
|
+
while (pieces.length > 0 && pieces[0]!.until <= now) settled = pieces.shift()!.to;
|
|
55
|
+
if (pieces.length > 0) {
|
|
56
|
+
clearTimeout(timer);
|
|
57
|
+
timer = setTimeout(() => due++, pieces[pieces.length - 1]!.until - now + 20);
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
settled: value.slice(0, settled),
|
|
61
|
+
fresh: pieces.map((piece) => ({ key: piece.from, text: value.slice(piece.from, piece.to) }))
|
|
62
|
+
};
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
$effect(() => () => clearTimeout(timer));
|
|
66
|
+
</script>
|
|
67
|
+
|
|
68
|
+
{parts.settled}{#each parts.fresh as piece (piece.key)}<span class={freshClass}>{piece.text}</span
|
|
69
|
+
>{/each}
|