@mdbase-dev/ui 0.1.0-beta.213 → 0.1.0-beta.216
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 +45 -0
- package/css/brand.css +27 -0
- package/css/comments.css +2 -1
- package/css/live-preview.css +33 -0
- package/css/motion.css +27 -15
- package/css/screens.css +98 -36
- package/dist/live-preview/blocks.js +9 -0
- package/dist/live-preview/diagram.d.ts +22 -0
- package/dist/live-preview/diagram.js +159 -0
- package/dist/locales/de/messages.json +1 -1
- package/dist/locales/en/messages.json +1 -1
- package/dist/locales/es/messages.json +1 -1
- package/dist/locales/fr/messages.json +1 -1
- package/dist/locales/it/messages.json +1 -1
- package/dist/locales/ja/messages.json +1 -1
- package/dist/locales/ko/messages.json +1 -1
- package/dist/locales/pt/messages.json +1 -1
- package/dist/locales/ru/messages.json +1 -1
- package/dist/locales/zh/messages.json +1 -1
- package/dist/opening-page.d.ts +40 -0
- package/dist/opening-page.js +104 -0
- package/dist/screens.d.ts +8 -16
- package/dist/screens.js +53 -194
- package/package.json +8 -3
- package/dist/flock.d.ts +0 -63
- package/dist/flock.js +0 -140
package/README.md
CHANGED
|
@@ -40,6 +40,51 @@ applyThemePreference(loadThemePreference());
|
|
|
40
40
|
are omitted; shared metadata never falls back to production origins. The
|
|
41
41
|
switcher's menu ends with a "Build your own" link to the SDK guide.
|
|
42
42
|
|
|
43
|
+
## Opening screen
|
|
44
|
+
|
|
45
|
+
Every app opens on the same screen: its mark scanning above `mdbase <app>`
|
|
46
|
+
and a line saying what it is opening. Paint it from index.html, before any
|
|
47
|
+
script runs, and render `OpeningScreen` from `@mdbase-dev/ui/screens` while
|
|
48
|
+
the app loads after that. Both produce the same DOM, so the hand-over is
|
|
49
|
+
invisible.
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
// vite.config.ts
|
|
53
|
+
import { mdbaseOpeningPage } from "@mdbase-dev/ui/opening-page";
|
|
54
|
+
|
|
55
|
+
plugins: [react(), mdbaseOpeningPage({
|
|
56
|
+
app: "reader",
|
|
57
|
+
title: "Opening your library",
|
|
58
|
+
themeColor: { light: "#fcfcfd", dark: "#1b1d23" },
|
|
59
|
+
})];
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
```html
|
|
63
|
+
<!-- index.html: keep <meta charset>; drop any theme-color meta and theme script -->
|
|
64
|
+
<div id="root"><!--mdbase-opening--></div>
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
```tsx
|
|
68
|
+
<Suspense fallback={<OpeningScreen app="reader" title={t`Opening your library`} />}>
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
- The plugin inlines `tokens.css`, `brand.css` and `screens.css` right after
|
|
72
|
+
`<meta charset>`, ahead of the app's own CSS, and writes the
|
|
73
|
+
`theme-color` meta. It refuses an index.html without the marker or charset,
|
|
74
|
+
or one that already sets theme-color.
|
|
75
|
+
- Its one inline script applies the saved theme before first paint. Until
|
|
76
|
+
the app replaces the page, it also turns a failed bundle or startup error
|
|
77
|
+
into the screen's failure state, with a Reload link. An app with a script
|
|
78
|
+
CSP must allow that script's hash.
|
|
79
|
+
- Without JavaScript the page shows that the app needs it. After eight
|
|
80
|
+
seconds, CSS alone shows "Still opening? Check your connection." with a
|
|
81
|
+
Reload link.
|
|
82
|
+
- The static page is English: it paints before any catalogue loads. The
|
|
83
|
+
React screen takes over in the reader's language.
|
|
84
|
+
- `OpeningScreen`'s `error` shows the failure state, and `onRetry` adds Try
|
|
85
|
+
again. `layout="panel"` renders a `div` without the reload hint, for a
|
|
86
|
+
screen inside the app's own chrome.
|
|
87
|
+
|
|
43
88
|
## Icon-button tooltips
|
|
44
89
|
|
|
45
90
|
Import `Tooltip` from `@mdbase-dev/ui/tooltip` and include `controls.css`.
|
package/css/brand.css
CHANGED
|
@@ -94,3 +94,30 @@
|
|
|
94
94
|
transform: translateX(-3px);
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
|
+
|
|
98
|
+
/*
|
|
99
|
+
* Scan: a brightness band reads the record line by line. It is the one loop every app
|
|
100
|
+
* waits with, so it lives here rather than in motion.css: an opening page inlines this
|
|
101
|
+
* file and paints the scan before any bundle loads.
|
|
102
|
+
*/
|
|
103
|
+
.mdbase-motion-scan :is(.mdbase-mark-fence, .mdbase-mark-row) {
|
|
104
|
+
opacity: 0.35;
|
|
105
|
+
animation: mdbase-scan 1.6s cubic-bezier(0.4, 0, 0.2, 1) infinite both;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
.mdbase-motion-scan .mdbase-mark-row-top { animation-delay: 0.14s; }
|
|
109
|
+
.mdbase-motion-scan .mdbase-mark-row-bottom { animation-delay: 0.28s; }
|
|
110
|
+
.mdbase-motion-scan .mdbase-mark-fence-bottom { animation-delay: 0.42s; }
|
|
111
|
+
|
|
112
|
+
@keyframes mdbase-scan {
|
|
113
|
+
0% { opacity: 0.35; }
|
|
114
|
+
14% { opacity: 1; }
|
|
115
|
+
46%, 100% { opacity: 0.35; }
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
@media (prefers-reduced-motion: reduce) {
|
|
119
|
+
.mdbase-motion-scan :is(.mdbase-mark-fence, .mdbase-mark-row) {
|
|
120
|
+
animation: none;
|
|
121
|
+
opacity: 1;
|
|
122
|
+
}
|
|
123
|
+
}
|
package/css/comments.css
CHANGED
|
@@ -214,7 +214,8 @@ blockquote.mdbase-thread-quote::after { content: "”"; }
|
|
|
214
214
|
border-radius: 50%;
|
|
215
215
|
color: var(--color-text);
|
|
216
216
|
background: color-mix(in oklch, var(--color-warning) 70%, transparent);
|
|
217
|
-
|
|
217
|
+
/* A count digit inside a 12px mark: three quarters of the caption size. */
|
|
218
|
+
font-size: calc(var(--font-size-caption) * 0.75);
|
|
218
219
|
line-height: 12px;
|
|
219
220
|
cursor: pointer;
|
|
220
221
|
}
|
package/css/live-preview.css
CHANGED
|
@@ -370,6 +370,39 @@
|
|
|
370
370
|
.cm-lp-code-header button:hover, .cm-lp-callout-header button:hover { background: var(--color-surface); color: var(--color-text); }
|
|
371
371
|
.cm-lp-code-header button:focus-visible, .cm-lp-callout-header button:focus-visible { outline: 2px solid var(--color-accent); outline-offset: 1px; }
|
|
372
372
|
|
|
373
|
+
.cm-lp-diagram {
|
|
374
|
+
--pele-bg: var(--color-surface);
|
|
375
|
+
--pele-fg: var(--color-text);
|
|
376
|
+
--pele-muted: var(--color-text-muted);
|
|
377
|
+
--pele-line: var(--color-text-soft);
|
|
378
|
+
--pele-surface: var(--color-surface-subtle);
|
|
379
|
+
--pele-surface-alt: var(--color-accent-soft);
|
|
380
|
+
--pele-border: var(--color-border-strong);
|
|
381
|
+
--pele-accent: var(--color-accent);
|
|
382
|
+
--pele-error: var(--color-danger);
|
|
383
|
+
--pele-font-mono: var(--mono);
|
|
384
|
+
--pele-radius: var(--radius-sm);
|
|
385
|
+
--pele-zoom-top: 44px;
|
|
386
|
+
position: relative;
|
|
387
|
+
margin: var(--space-2) 0;
|
|
388
|
+
padding: var(--space-3) var(--space-2);
|
|
389
|
+
}
|
|
390
|
+
.cm-lp-diagram-canvas { position: relative; }
|
|
391
|
+
.cm-lp-diagram-canvas > svg { display: block; margin-inline: auto; }
|
|
392
|
+
.cm-lp-diagram-failed { color: var(--color-text-muted); font-family: var(--sans); font-size: var(--font-size-caption); }
|
|
393
|
+
.cm-lp-diagram-edit {
|
|
394
|
+
position: absolute; top: var(--space-2); right: var(--space-2);
|
|
395
|
+
min-width: 28px; min-height: 28px; padding: 0 var(--space-2);
|
|
396
|
+
border: 1px solid var(--color-border-strong); border-radius: var(--radius-sm);
|
|
397
|
+
background: var(--color-surface); color: var(--color-text-muted);
|
|
398
|
+
font-family: var(--sans); font-size: var(--font-size-caption); cursor: pointer;
|
|
399
|
+
opacity: 0; transition: opacity 120ms;
|
|
400
|
+
}
|
|
401
|
+
.cm-lp-diagram:hover .cm-lp-diagram-edit, .cm-lp-diagram-edit:focus-visible { opacity: 1; }
|
|
402
|
+
.cm-lp-diagram-edit:hover { color: var(--color-text); }
|
|
403
|
+
.cm-lp-diagram-edit:focus-visible { outline: 2px solid var(--color-accent); outline-offset: 1px; }
|
|
404
|
+
@media (hover: none) { .cm-lp-diagram-edit { opacity: 1; } }
|
|
405
|
+
|
|
373
406
|
.cm-lp-callout { background: var(--color-surface-subtle); padding-left: 1em !important; padding-right: 1em !important; }
|
|
374
407
|
.cm-lp-callout-first { border-radius: var(--radius-md) var(--radius-md) 0 0; padding-top: 0.3em !important; font-weight: 600; }
|
|
375
408
|
.cm-lp-callout-last { border-radius: 0 0 var(--radius-md) var(--radius-md); padding-bottom: 0.3em !important; }
|
package/css/motion.css
CHANGED
|
@@ -99,21 +99,7 @@
|
|
|
99
99
|
to { transform: translateX(-28px); }
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
-
/* Scan:
|
|
103
|
-
.mdbase-motion-scan :is(.mdbase-mark-fence, .mdbase-mark-row) {
|
|
104
|
-
opacity: 0.35;
|
|
105
|
-
animation: mdbase-scan 1.6s cubic-bezier(0.4, 0, 0.2, 1) infinite both;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
.mdbase-motion-scan .mdbase-mark-row-top { animation-delay: 0.14s; }
|
|
109
|
-
.mdbase-motion-scan .mdbase-mark-row-bottom { animation-delay: 0.28s; }
|
|
110
|
-
.mdbase-motion-scan .mdbase-mark-fence-bottom { animation-delay: 0.42s; }
|
|
111
|
-
|
|
112
|
-
@keyframes mdbase-scan {
|
|
113
|
-
0% { opacity: 0.35; }
|
|
114
|
-
14% { opacity: 1; }
|
|
115
|
-
46%, 100% { opacity: 0.35; }
|
|
116
|
-
}
|
|
102
|
+
/* Scan lives in brand.css: opening screens paint it before this file loads. */
|
|
117
103
|
|
|
118
104
|
/* Bounce: the fence dashes hop like a typing indicator, the bottom fence mirrored. */
|
|
119
105
|
.mdbase-motion-bounce .mdbase-mark-fence .mdbase-mark-segment {
|
|
@@ -308,6 +294,32 @@
|
|
|
308
294
|
to { opacity: 1; }
|
|
309
295
|
}
|
|
310
296
|
|
|
297
|
+
/* Error: the fields shake once and the highlight turns to danger before recovering. */
|
|
298
|
+
.mdbase-signal-error .mdbase-mark-row {
|
|
299
|
+
animation: mdbase-shake 520ms cubic-bezier(0.36, 0.07, 0.19, 0.97) 1 both;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
.mdbase-signal-error .mdbase-mark-row-bottom {
|
|
303
|
+
animation-delay: 40ms;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
.mdbase-signal-error .mdbase-mark-accent {
|
|
307
|
+
animation: mdbase-danger 1.4s ease 1 both;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
@keyframes mdbase-shake {
|
|
311
|
+
15% { transform: translateX(-4px); }
|
|
312
|
+
35% { transform: translateX(4px); }
|
|
313
|
+
55% { transform: translateX(-2.5px); }
|
|
314
|
+
75% { transform: translateX(1.5px); }
|
|
315
|
+
to { transform: none; }
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
@keyframes mdbase-danger {
|
|
319
|
+
0%, 60% { fill: var(--color-danger); }
|
|
320
|
+
to { fill: var(--mark-accent); }
|
|
321
|
+
}
|
|
322
|
+
|
|
311
323
|
/* ---------- Hover ---------- */
|
|
312
324
|
|
|
313
325
|
/* Wave: one ripple runs through the resting mark in reading order. */
|
package/css/screens.css
CHANGED
|
@@ -17,8 +17,13 @@
|
|
|
17
17
|
.mdbase-page { padding: 2rem 1.25rem; }
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
/*
|
|
20
|
+
/*
|
|
21
|
+
* Opening: the app's mark scans over its name and one quiet line. Apps also paint this
|
|
22
|
+
* markup as static HTML before any script runs (`@mdbase-dev/ui/opening-page`), so these
|
|
23
|
+
* rules must hold with only tokens.css and brand.css beside them.
|
|
24
|
+
*/
|
|
21
25
|
.mdbase-opening {
|
|
26
|
+
box-sizing: border-box;
|
|
22
27
|
width: 100%;
|
|
23
28
|
height: 100%;
|
|
24
29
|
min-height: 100%;
|
|
@@ -26,76 +31,133 @@
|
|
|
26
31
|
place-items: center;
|
|
27
32
|
overflow: hidden;
|
|
28
33
|
background: var(--color-canvas);
|
|
34
|
+
color: var(--color-text-soft);
|
|
35
|
+
font-family: var(--sans);
|
|
36
|
+
font-size: var(--font-size-ui);
|
|
37
|
+
line-height: 1.5;
|
|
38
|
+
text-align: center;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/* A page-level opening fills the window, lifted a little above centre. */
|
|
42
|
+
:is(body, body > *) > main.mdbase-opening {
|
|
43
|
+
min-height: 100dvh;
|
|
44
|
+
padding: 32px 24px calc(32px + 8vh);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
body:has(> main.mdbase-opening, > * > main.mdbase-opening) {
|
|
48
|
+
margin: 0;
|
|
29
49
|
}
|
|
30
50
|
|
|
31
51
|
.mdbase-opening-message {
|
|
32
52
|
display: grid;
|
|
33
53
|
justify-items: center;
|
|
34
|
-
gap: 14px;
|
|
35
|
-
color: var(--color-text-soft);
|
|
36
|
-
text-align: center;
|
|
37
54
|
}
|
|
38
55
|
|
|
39
|
-
.mdbase-opening
|
|
56
|
+
.mdbase-opening-mark {
|
|
57
|
+
display: block;
|
|
58
|
+
width: 44px;
|
|
59
|
+
height: 44px;
|
|
60
|
+
margin-bottom: 20px;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
.mdbase-opening-mark > svg {
|
|
40
64
|
display: block;
|
|
41
65
|
width: 100%;
|
|
42
66
|
height: 100%;
|
|
43
67
|
}
|
|
44
68
|
|
|
45
|
-
.mdbase-opening-
|
|
46
|
-
|
|
47
|
-
|
|
69
|
+
.mdbase-opening-name {
|
|
70
|
+
margin-bottom: 12px;
|
|
71
|
+
color: var(--color-text);
|
|
72
|
+
font-family: var(--mono);
|
|
73
|
+
font-size: var(--font-size-caption);
|
|
74
|
+
font-weight: var(--font-weight-medium);
|
|
75
|
+
line-height: 1;
|
|
76
|
+
letter-spacing: -0.02em;
|
|
77
|
+
white-space: nowrap;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
.mdbase-opening-name strong {
|
|
81
|
+
color: var(--color-text-muted);
|
|
82
|
+
font-weight: inherit;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/* Status, failure and the no-JavaScript notice share one line. */
|
|
86
|
+
.mdbase-opening-text {
|
|
87
|
+
display: grid;
|
|
88
|
+
max-width: 46ch;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
.mdbase-opening-text > * {
|
|
92
|
+
grid-column: 1;
|
|
48
93
|
margin: 0;
|
|
49
94
|
}
|
|
50
95
|
|
|
51
|
-
.mdbase-opening-
|
|
52
|
-
|
|
53
|
-
|
|
96
|
+
.mdbase-opening-text > :is([role="status"], [role="alert"], noscript) {
|
|
97
|
+
grid-row: 1;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
.mdbase-opening-text p {
|
|
101
|
+
margin: 0;
|
|
54
102
|
}
|
|
55
103
|
|
|
56
|
-
.mdbase-opening-
|
|
104
|
+
.mdbase-opening-text small {
|
|
105
|
+
display: block;
|
|
57
106
|
margin-top: 4px;
|
|
58
107
|
color: var(--color-text-muted);
|
|
59
108
|
font-size: var(--font-size-caption);
|
|
60
109
|
}
|
|
61
110
|
|
|
62
|
-
.mdbase-opening[
|
|
111
|
+
.mdbase-opening-text [role="alert"] {
|
|
63
112
|
color: var(--color-danger);
|
|
64
113
|
}
|
|
65
114
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
display: block;
|
|
69
|
-
width: 42px;
|
|
70
|
-
height: 42px;
|
|
71
|
-
margin-bottom: var(--space-2);
|
|
115
|
+
.mdbase-opening-nojs {
|
|
116
|
+
color: var(--color-text);
|
|
72
117
|
}
|
|
73
118
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
119
|
+
/* Help appears only once an open has taken long enough to be worth questioning. */
|
|
120
|
+
.mdbase-opening-help {
|
|
121
|
+
margin: 6px 0 0;
|
|
122
|
+
color: var(--color-text-muted);
|
|
123
|
+
font-size: var(--font-size-caption);
|
|
124
|
+
visibility: hidden;
|
|
125
|
+
opacity: 0;
|
|
126
|
+
animation: mdbase-opening-help 240ms cubic-bezier(0.22, 1, 0.36, 1) 8s forwards;
|
|
78
127
|
}
|
|
79
128
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
visibility: hidden;
|
|
129
|
+
@keyframes mdbase-opening-help {
|
|
130
|
+
to { visibility: visible; opacity: 1; }
|
|
83
131
|
}
|
|
84
132
|
|
|
85
|
-
.mdbase-opening
|
|
86
|
-
|
|
133
|
+
.mdbase-opening a {
|
|
134
|
+
color: var(--color-text);
|
|
135
|
+
text-decoration-thickness: 1px;
|
|
136
|
+
text-underline-offset: 2px;
|
|
87
137
|
}
|
|
88
138
|
|
|
89
|
-
.mdbase-opening
|
|
90
|
-
|
|
139
|
+
.mdbase-opening .mdbase-button {
|
|
140
|
+
margin-top: 14px;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/* Failed, or without JavaScript, nothing is being waited for: the scan and the help stop. */
|
|
144
|
+
.mdbase-opening:is([data-loading-state="failed"], :has(.mdbase-opening-nojs)) :is(.mdbase-opening-text > [role="status"], .mdbase-opening-help) {
|
|
145
|
+
display: none;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
.mdbase-opening:is([data-loading-state="failed"], :has(.mdbase-opening-nojs)) .mdbase-motion-scan :is(.mdbase-mark-fence, .mdbase-mark-row) {
|
|
149
|
+
animation: none;
|
|
150
|
+
opacity: 1;
|
|
91
151
|
}
|
|
92
152
|
|
|
93
|
-
.mdbase-opening-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
153
|
+
.mdbase-opening[data-loading-state="failed"] .mdbase-mark-accent {
|
|
154
|
+
fill: var(--color-danger);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
@media (prefers-reduced-motion: reduce) {
|
|
158
|
+
.mdbase-opening-help {
|
|
159
|
+
animation-duration: 0s;
|
|
160
|
+
}
|
|
99
161
|
}
|
|
100
162
|
|
|
101
163
|
/* Before a collection is open. */
|
|
@@ -3,6 +3,7 @@ import { syntaxTree } from "@codemirror/language";
|
|
|
3
3
|
import { StateEffect, StateField } from "@codemirror/state";
|
|
4
4
|
import { Decoration, EditorView, WidgetType, keymap } from "@codemirror/view";
|
|
5
5
|
import { i18n as _i18n } from "@lingui/core";
|
|
6
|
+
import { DiagramWidget, isDiagramLanguage } from "./diagram.js";
|
|
6
7
|
import { isRevealed, revealField } from "./reveal.js";
|
|
7
8
|
const toggleFold = StateEffect.define({
|
|
8
9
|
map: (value, changes) => ({
|
|
@@ -288,6 +289,14 @@ function build(state) {
|
|
|
288
289
|
const codeFrom = fenced ? Math.min(first.to + 1, state.doc.length) : first.from;
|
|
289
290
|
const codeTo = closed ? Math.max(codeFrom, last.from - 1) : last.to;
|
|
290
291
|
const code = state.sliceDoc(codeFrom, codeTo);
|
|
292
|
+
// Quote and list markers would be part of a nested diagram's source, so only top-level fences render.
|
|
293
|
+
if (closed && isDiagramLanguage(opening[2]) && code.trim() && node.node.parent?.name === "Document" && !isRevealed(state, node.from, node.to)) {
|
|
294
|
+
ranges.push(Decoration.replace({
|
|
295
|
+
widget: new DiagramWidget(code),
|
|
296
|
+
block: true
|
|
297
|
+
}).range(first.from, last.to));
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
291
300
|
for (let n = first.number; n <= last.number; n++) {
|
|
292
301
|
const line = state.doc.line(n);
|
|
293
302
|
const edge = `${n === first.number ? "cm-lp-code-first" : ""} ${n === last.number ? "cm-lp-code-last" : ""}`;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { EditorView } from "@codemirror/view";
|
|
2
|
+
import { WidgetType } from "@codemirror/view";
|
|
3
|
+
import "../i18n.js";
|
|
4
|
+
/**
|
|
5
|
+
* A Mermaid diagram, rendered by Pele in place of its fenced source while the
|
|
6
|
+
* selection is elsewhere. Pele and each diagram type load on first use, so a
|
|
7
|
+
* note without diagrams never fetches them.
|
|
8
|
+
*
|
|
9
|
+
* Diagram text is untrusted: Pele escapes it and filters styles and links, the
|
|
10
|
+
* render options are fixed here (never from the note), remote images are off,
|
|
11
|
+
* and links only open in a new tab.
|
|
12
|
+
*/
|
|
13
|
+
export declare function isDiagramLanguage(language: string): boolean;
|
|
14
|
+
export declare class DiagramWidget extends WidgetType {
|
|
15
|
+
readonly source: string;
|
|
16
|
+
constructor(source: string);
|
|
17
|
+
eq(other: DiagramWidget): boolean;
|
|
18
|
+
get estimatedHeight(): number;
|
|
19
|
+
toDOM(view: EditorView): HTMLDivElement;
|
|
20
|
+
destroy(dom: HTMLElement): void;
|
|
21
|
+
ignoreEvent(): boolean;
|
|
22
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { WidgetType } from "@codemirror/view";
|
|
2
|
+
import { i18n as _i18n } from "@lingui/core";
|
|
3
|
+
import "../i18n.js";
|
|
4
|
+
/**
|
|
5
|
+
* A Mermaid diagram, rendered by Pele in place of its fenced source while the
|
|
6
|
+
* selection is elsewhere. Pele and each diagram type load on first use, so a
|
|
7
|
+
* note without diagrams never fetches them.
|
|
8
|
+
*
|
|
9
|
+
* Diagram text is untrusted: Pele escapes it and filters styles and links, the
|
|
10
|
+
* render options are fixed here (never from the note), remote images are off,
|
|
11
|
+
* and links only open in a new tab.
|
|
12
|
+
*/
|
|
13
|
+
export function isDiagramLanguage(language) {
|
|
14
|
+
return language.toLowerCase() === "mermaid";
|
|
15
|
+
}
|
|
16
|
+
let renderer;
|
|
17
|
+
function loadRenderer() {
|
|
18
|
+
// A failed chunk load is retried by the next diagram rather than cached.
|
|
19
|
+
return renderer ??= import("pele/lazy").catch(error => {
|
|
20
|
+
renderer = undefined;
|
|
21
|
+
throw error;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
/** Last rendered height per source, so a re-created widget reserves its space before rendering. */
|
|
25
|
+
const heights = new Map();
|
|
26
|
+
const HEIGHT_ENTRIES = 256;
|
|
27
|
+
function rememberHeight(source, height) {
|
|
28
|
+
heights.delete(source);
|
|
29
|
+
heights.set(source, height);
|
|
30
|
+
if (heights.size > HEIGHT_ENTRIES) heights.delete(heights.keys().next().value);
|
|
31
|
+
}
|
|
32
|
+
const mounts = new WeakMap();
|
|
33
|
+
function renderOptions(view, box, source) {
|
|
34
|
+
return {
|
|
35
|
+
images: false,
|
|
36
|
+
linkSchemes: ["http", "https", "mailto"],
|
|
37
|
+
linkRel: "noopener noreferrer",
|
|
38
|
+
zoom: {
|
|
39
|
+
labels: {
|
|
40
|
+
zoomIn: _i18n._(
|
|
41
|
+
/** i18n */
|
|
42
|
+
{
|
|
43
|
+
id: "AWOSPo",
|
|
44
|
+
message: "Zoom in"
|
|
45
|
+
}),
|
|
46
|
+
zoomOut: _i18n._(
|
|
47
|
+
/** i18n */
|
|
48
|
+
{
|
|
49
|
+
id: "FjkaiT",
|
|
50
|
+
message: "Zoom out"
|
|
51
|
+
}),
|
|
52
|
+
reset: _i18n._(
|
|
53
|
+
/** i18n */
|
|
54
|
+
{
|
|
55
|
+
id: "jKibyt",
|
|
56
|
+
message: "Reset zoom"
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
onRender: () => view.requestMeasure({
|
|
61
|
+
read: () => box.offsetHeight,
|
|
62
|
+
write: height => {
|
|
63
|
+
if (height > 0) rememberHeight(source, height);
|
|
64
|
+
box.style.minHeight = "";
|
|
65
|
+
}
|
|
66
|
+
})
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
export class DiagramWidget extends WidgetType {
|
|
70
|
+
source;
|
|
71
|
+
constructor(source) {
|
|
72
|
+
super();
|
|
73
|
+
this.source = source;
|
|
74
|
+
}
|
|
75
|
+
eq(other) {
|
|
76
|
+
return this.source === other.source;
|
|
77
|
+
}
|
|
78
|
+
get estimatedHeight() {
|
|
79
|
+
return heights.get(this.source) ?? -1;
|
|
80
|
+
}
|
|
81
|
+
toDOM(view) {
|
|
82
|
+
const box = document.createElement("div");
|
|
83
|
+
box.className = "cm-lp-diagram";
|
|
84
|
+
const height = heights.get(this.source);
|
|
85
|
+
if (height) box.style.minHeight = `${height}px`;
|
|
86
|
+
const canvas = document.createElement("div");
|
|
87
|
+
canvas.className = "cm-lp-diagram-canvas";
|
|
88
|
+
const edit = document.createElement("button");
|
|
89
|
+
edit.type = "button";
|
|
90
|
+
edit.className = "cm-lp-diagram-edit";
|
|
91
|
+
edit.textContent = _i18n._(
|
|
92
|
+
/** i18n */
|
|
93
|
+
{
|
|
94
|
+
id: "Q4zUiV",
|
|
95
|
+
message: "Edit",
|
|
96
|
+
context: "diagram"
|
|
97
|
+
});
|
|
98
|
+
edit.setAttribute("aria-label", _i18n._(
|
|
99
|
+
/** i18n */
|
|
100
|
+
{
|
|
101
|
+
id: "j5JzXy",
|
|
102
|
+
message: "Edit diagram source"
|
|
103
|
+
}));
|
|
104
|
+
edit.addEventListener("click", () => editSource(view, box));
|
|
105
|
+
box.addEventListener("click", openLink);
|
|
106
|
+
box.append(canvas, edit);
|
|
107
|
+
const state = {
|
|
108
|
+
destroyed: false
|
|
109
|
+
};
|
|
110
|
+
mounts.set(box, state);
|
|
111
|
+
loadRenderer().then(({
|
|
112
|
+
mountAsync
|
|
113
|
+
}) => {
|
|
114
|
+
if (state.destroyed) return;
|
|
115
|
+
return mountAsync(canvas, this.source, renderOptions(view, box, this.source)).then(mounted => {
|
|
116
|
+
if (state.destroyed) mounted.destroy();else state.mounted = mounted;
|
|
117
|
+
});
|
|
118
|
+
}).catch(() => {
|
|
119
|
+
if (state.destroyed) return;
|
|
120
|
+
box.style.minHeight = "";
|
|
121
|
+
canvas.className = "cm-lp-diagram-canvas cm-lp-diagram-failed";
|
|
122
|
+
canvas.textContent = _i18n._(
|
|
123
|
+
/** i18n */
|
|
124
|
+
{
|
|
125
|
+
id: "MlIz7Y",
|
|
126
|
+
message: "The diagram could not be shown. Check your connection and reopen the note."
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
return box;
|
|
130
|
+
}
|
|
131
|
+
destroy(dom) {
|
|
132
|
+
const state = mounts.get(dom);
|
|
133
|
+
if (!state) return;
|
|
134
|
+
state.destroyed = true;
|
|
135
|
+
state.mounted?.destroy();
|
|
136
|
+
}
|
|
137
|
+
ignoreEvent() {
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/** Puts the caret on the first line of the diagram's source, which reveals it. */
|
|
142
|
+
function editSource(view, box) {
|
|
143
|
+
const fence = view.state.doc.lineAt(view.posAtDOM(box));
|
|
144
|
+
view.dispatch({
|
|
145
|
+
selection: {
|
|
146
|
+
anchor: Math.min(fence.to + 1, view.state.doc.length)
|
|
147
|
+
},
|
|
148
|
+
scrollIntoView: true
|
|
149
|
+
});
|
|
150
|
+
view.focus();
|
|
151
|
+
}
|
|
152
|
+
/** Links never navigate the app: web and mail links open in a new tab, anything else is inert. */
|
|
153
|
+
function openLink(event) {
|
|
154
|
+
const link = event.target instanceof Element ? event.target.closest("a") : null;
|
|
155
|
+
if (!link) return;
|
|
156
|
+
event.preventDefault();
|
|
157
|
+
const href = link.getAttribute("href") ?? "";
|
|
158
|
+
if (/^(https?|mailto):/i.test(href)) window.open(href, "_blank", "noopener,noreferrer");
|
|
159
|
+
}
|