@diegopluna/narvi 0.5.9 → 0.5.10
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/components/markdown/Markdown.svelte +68 -7
- package/dist/components/markdown/Markdown.svelte.d.ts +9 -0
- package/dist/components/markdown/MarkdownBlocks.svelte +4 -3
- package/dist/components/markdown/MarkdownCode.svelte +108 -9
- package/dist/components/markdown/MarkdownCode.svelte.d.ts +5 -1
- package/dist/components/markdown/MarkdownHtmlPreview.svelte +186 -0
- package/dist/components/markdown/MarkdownHtmlPreview.svelte.d.ts +11 -0
- package/dist/components/markdown/MarkdownPreview.svelte +17 -0
- package/dist/components/markdown/MarkdownPreview.svelte.d.ts +6 -0
- package/dist/components/markdown/MarkdownTable.svelte +61 -4
- package/dist/components/markdown/context.d.ts +21 -0
- package/dist/components/markdown/labels.d.ts +12 -0
- package/dist/components/markdown/labels.js +5 -0
- package/dist/components/markdown/preview.d.ts +9 -0
- package/dist/components/markdown/preview.js +14 -0
- package/dist/components/markdown/static.d.ts +2 -0
- package/dist/components/markdown/static.js +5 -1
- package/dist/components/markdown/table-image.d.ts +15 -0
- package/dist/components/markdown/table-image.js +342 -0
- package/dist/components/message/MessageAction.svelte +17 -2
- package/dist/components/message/MessageAction.svelte.d.ts +7 -0
- package/dist/markdown/html.d.ts +3 -1
- package/dist/markdown/html.js +6 -0
- package/dist/markdown/index.d.ts +2 -2
- package/dist/markdown/index.js +2 -2
- package/dist/markdown/table.d.ts +20 -1
- package/dist/markdown/table.js +111 -0
- package/package.json +1 -1
- package/skills/building-ui/SKILL.md +1 -1
- package/skills/markdown/SKILL.md +24 -5
- package/skills/setup/SKILL.md +1 -1
- package/skills/styling/SKILL.md +1 -1
- package/skills/theming/SKILL.md +1 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
<!--
|
|
2
2
|
A table, in a frame that scrolls sideways when it is wide. A button at its corner, shown
|
|
3
|
-
on hover or focus, copies it as Markdown or
|
|
3
|
+
on hover or focus, copies it as Markdown, aligned text, HTML, CSV or a PNG, or saves it as
|
|
4
|
+
a CSV file.
|
|
4
5
|
-->
|
|
5
6
|
<script lang="ts" module>
|
|
6
7
|
import * as stylex from '@stylexjs/stylex';
|
|
@@ -11,7 +12,7 @@
|
|
|
11
12
|
import { escapeHtml } from '../../markdown/html.js';
|
|
12
13
|
import type { Table } from '../../markdown/ast.js';
|
|
13
14
|
import { groupInlineHtml } from '../../markdown/sanitize.js';
|
|
14
|
-
import { tableToCsv, tableToMarkdown } from '../../markdown/table.js';
|
|
15
|
+
import { tableToCsv, tableToHtml, tableToMarkdown, tableToText } from '../../markdown/table.js';
|
|
15
16
|
import { colors, motion, radius } from '../../tokens.stylex.js';
|
|
16
17
|
import { media } from '../../media.stylex.js';
|
|
17
18
|
import Button, { buttonClass, buttonHtml } from '../button/Button.svelte';
|
|
@@ -64,6 +65,21 @@
|
|
|
64
65
|
}
|
|
65
66
|
});
|
|
66
67
|
|
|
68
|
+
/**
|
|
69
|
+
* Whether this browser can put a picture on the clipboard: a `ClipboardItem` that takes a
|
|
70
|
+
* PNG. Asked when the menu is made, which is only ever in a browser.
|
|
71
|
+
*/
|
|
72
|
+
function canCopyImage(): boolean {
|
|
73
|
+
if (typeof ClipboardItem === 'undefined' || typeof navigator.clipboard?.write !== 'function')
|
|
74
|
+
return false;
|
|
75
|
+
const supports = (ClipboardItem as { supports?: (type: string) => boolean }).supports;
|
|
76
|
+
try {
|
|
77
|
+
return typeof supports === 'function' ? supports('image/png') : true;
|
|
78
|
+
} catch {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
67
83
|
/** The button of a table whose menu has not been asked for: worked out once. */
|
|
68
84
|
const idleButtonClass = buttonClass({
|
|
69
85
|
variant: 'ghost',
|
|
@@ -158,9 +174,13 @@
|
|
|
158
174
|
let copied = $state(false);
|
|
159
175
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
160
176
|
|
|
161
|
-
|
|
177
|
+
let root = $state<HTMLDivElement | null>(null);
|
|
178
|
+
const imageCopies = $derived(armed && canCopyImage());
|
|
179
|
+
|
|
180
|
+
/** Puts something on the clipboard, then says so, or says it could not. */
|
|
181
|
+
async function put(write: () => Promise<void>) {
|
|
162
182
|
try {
|
|
163
|
-
await
|
|
183
|
+
await write();
|
|
164
184
|
} catch {
|
|
165
185
|
md.announce(md.labels.copyFailed);
|
|
166
186
|
return;
|
|
@@ -171,6 +191,35 @@
|
|
|
171
191
|
timer = setTimeout(() => (copied = false), 2000);
|
|
172
192
|
}
|
|
173
193
|
|
|
194
|
+
const copy = (text: string) => put(() => navigator.clipboard.writeText(text));
|
|
195
|
+
|
|
196
|
+
/** As HTML for a rich-text editor, and the same markup as text for one that takes no formatting. */
|
|
197
|
+
function copyHtml() {
|
|
198
|
+
const html = tableToHtml(node);
|
|
199
|
+
if (typeof ClipboardItem === 'undefined' || typeof navigator.clipboard.write !== 'function')
|
|
200
|
+
return copy(html);
|
|
201
|
+
return put(() =>
|
|
202
|
+
navigator.clipboard.write([
|
|
203
|
+
new ClipboardItem({
|
|
204
|
+
'text/html': new Blob([html], { type: 'text/html' }),
|
|
205
|
+
'text/plain': new Blob([html], { type: 'text/plain' })
|
|
206
|
+
})
|
|
207
|
+
])
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* As a picture. The clipboard is written to now, inside the press, with the picture still
|
|
213
|
+
* to come: Safari takes a write only while the press is being handled, and drawing it, and
|
|
214
|
+
* loading what draws it, come after.
|
|
215
|
+
*/
|
|
216
|
+
function copyImage() {
|
|
217
|
+
const table = root?.querySelector('table');
|
|
218
|
+
if (!table) return;
|
|
219
|
+
const png = import('./table-image.js').then(({ tableImage }) => tableImage(table));
|
|
220
|
+
return put(() => navigator.clipboard.write([new ClipboardItem({ 'image/png': png })]));
|
|
221
|
+
}
|
|
222
|
+
|
|
174
223
|
function download() {
|
|
175
224
|
// The byte order mark tells a spreadsheet the file is UTF-8
|
|
176
225
|
saveFile('table.csv', '\uFEFF' + tableToCsv(node), 'text/csv');
|
|
@@ -180,6 +229,7 @@
|
|
|
180
229
|
</script>
|
|
181
230
|
|
|
182
231
|
<div
|
|
232
|
+
bind:this={root}
|
|
183
233
|
data-markdown-table
|
|
184
234
|
{...attrs([prose.block, styles.wrap, markdownTableMarker, enter && prose.enter])}
|
|
185
235
|
>
|
|
@@ -269,7 +319,14 @@
|
|
|
269
319
|
<Menu.Item onSelect={() => copy(tableToMarkdown(node))}
|
|
270
320
|
>{md.labels.copyAsMarkdown}</Menu.Item
|
|
271
321
|
>
|
|
322
|
+
<Menu.Item onSelect={() => copy(tableToText(node))}>{md.labels.copyAsText}</Menu.Item>
|
|
323
|
+
<Menu.Item onSelect={copyHtml}>{md.labels.copyAsHtml}</Menu.Item>
|
|
272
324
|
<Menu.Item onSelect={() => copy(tableToCsv(node))}>{md.labels.copyAsCsv}</Menu.Item>
|
|
325
|
+
<!-- Left out where the clipboard takes no pictures -->
|
|
326
|
+
{#if imageCopies}
|
|
327
|
+
<Menu.Item onSelect={copyImage}>{md.labels.copyAsPng}</Menu.Item>
|
|
328
|
+
{/if}
|
|
329
|
+
<Menu.Separator />
|
|
273
330
|
<Menu.Item onSelect={download}>{md.labels.saveAsCsv}</Menu.Item>
|
|
274
331
|
</Menu.Content>
|
|
275
332
|
</Menu.Root>
|
|
@@ -3,6 +3,7 @@ import type { Block, Document, FootnoteReference, Heading, ListItem } from '../.
|
|
|
3
3
|
import type { MarkdownLabels } from './labels.js';
|
|
4
4
|
import { type Highlighter, type ResolvedLinkPolicy, type MarkdownPlugin, type PluginComponent, type StaticPart } from '../../markdown/plugin.js';
|
|
5
5
|
import type { UrlPolicy } from '../../markdown/sanitize.js';
|
|
6
|
+
import type { PreviewKind } from './preview.js';
|
|
6
7
|
export type MarkdownContext = {
|
|
7
8
|
/** Components that replace Narvi's, by node type. */
|
|
8
9
|
component: (type: string) => PluginComponent | undefined;
|
|
@@ -65,6 +66,15 @@ export type MarkdownContext = {
|
|
|
65
66
|
lineNumbers: boolean;
|
|
66
67
|
download: boolean;
|
|
67
68
|
};
|
|
69
|
+
/** What a finished code block in a language can be shown rendered as, or null. */
|
|
70
|
+
preview: (lang: string | null) => PreviewKind | null;
|
|
71
|
+
/** Whether a code block is shown rendered now. */
|
|
72
|
+
previewed: (id: number) => boolean;
|
|
73
|
+
/**
|
|
74
|
+
* The document's options, for a Markdown block shown rendered: the same plugins and the
|
|
75
|
+
* same way of reading, with nothing that streams or that would show a block rendered again.
|
|
76
|
+
*/
|
|
77
|
+
nested: () => NestedOptions;
|
|
68
78
|
/**
|
|
69
79
|
* How much formula markup the document holds, which its formulas share a limit on, and
|
|
70
80
|
* what a formula in a line of text calls once it is on the page.
|
|
@@ -84,6 +94,17 @@ export type MarkdownContext = {
|
|
|
84
94
|
/** Scrolls to a heading by its anchor; false when the document has none by that name. */
|
|
85
95
|
goto: (name: string) => boolean;
|
|
86
96
|
};
|
|
97
|
+
export type NestedOptions = {
|
|
98
|
+
gfm: boolean;
|
|
99
|
+
breaks: boolean;
|
|
100
|
+
singleTilde: boolean;
|
|
101
|
+
frontmatter: boolean;
|
|
102
|
+
plugins: ReadonlyArray<MarkdownPlugin>;
|
|
103
|
+
lineNumbers: boolean;
|
|
104
|
+
download: boolean;
|
|
105
|
+
labels: Partial<MarkdownLabels> | undefined;
|
|
106
|
+
headingOffset: number;
|
|
107
|
+
};
|
|
87
108
|
export declare function setMarkdownContext(context: MarkdownContext): void;
|
|
88
109
|
export declare function getMarkdownContext(): MarkdownContext;
|
|
89
110
|
/** Merges plugins: later ones win, one component or policy function at a time. */
|
|
@@ -11,8 +11,20 @@ export type MarkdownLabels = {
|
|
|
11
11
|
/** Names the table's menu button. */
|
|
12
12
|
tableActions: string;
|
|
13
13
|
copyAsMarkdown: string;
|
|
14
|
+
/** Copies the table as text whose columns line up in a monospace font. */
|
|
15
|
+
copyAsText: string;
|
|
16
|
+
copyAsHtml: string;
|
|
14
17
|
copyAsCsv: string;
|
|
18
|
+
/** Copies the table as a picture, as it is drawn. */
|
|
19
|
+
copyAsPng: string;
|
|
15
20
|
saveAsCsv: string;
|
|
21
|
+
/**
|
|
22
|
+
* Names the toggle that shows a Markdown or HTML code block rendered. It keeps this name
|
|
23
|
+
* either way; whether it is pressed says which is shown.
|
|
24
|
+
*/
|
|
25
|
+
showRendered: string;
|
|
26
|
+
/** Names a rendered HTML code block's frame. */
|
|
27
|
+
htmlPreview: string;
|
|
16
28
|
diagram: string;
|
|
17
29
|
showCode: string;
|
|
18
30
|
showDiagram: string;
|
|
@@ -5,8 +5,13 @@ export const defaultLabels = {
|
|
|
5
5
|
copyFailed: "Couldn't copy",
|
|
6
6
|
tableActions: 'Copy or save this table',
|
|
7
7
|
copyAsMarkdown: 'Copy as Markdown',
|
|
8
|
+
copyAsText: 'Copy as text',
|
|
9
|
+
copyAsHtml: 'Copy as HTML',
|
|
8
10
|
copyAsCsv: 'Copy as CSV',
|
|
11
|
+
copyAsPng: 'Copy as PNG',
|
|
9
12
|
saveAsCsv: 'Save as CSV',
|
|
13
|
+
showRendered: 'Show rendered',
|
|
14
|
+
htmlPreview: 'Rendered HTML',
|
|
10
15
|
diagram: 'Diagram',
|
|
11
16
|
showCode: 'Show code',
|
|
12
17
|
showDiagram: 'Show diagram',
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which code blocks can be shown rendered: Markdown and HTML, when the document's `previews`
|
|
3
|
+
* option asks for them.
|
|
4
|
+
*/
|
|
5
|
+
export type PreviewKind = 'markdown' | 'html';
|
|
6
|
+
/** What `<Markdown previews>` takes: every kind, or the ones listed. */
|
|
7
|
+
export type PreviewsOption = boolean | ReadonlyArray<PreviewKind> | undefined;
|
|
8
|
+
/** What a fence's language renders as, when the option allows it; null for any other block. */
|
|
9
|
+
export declare function previewKind(lang: string | null, previews: PreviewsOption): PreviewKind | null;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which code blocks can be shown rendered: Markdown and HTML, when the document's `previews`
|
|
3
|
+
* option asks for them.
|
|
4
|
+
*/
|
|
5
|
+
const KINDS = { markdown: 'markdown', md: 'markdown', html: 'html' };
|
|
6
|
+
/** What a fence's language renders as, when the option allows it; null for any other block. */
|
|
7
|
+
export function previewKind(lang, previews) {
|
|
8
|
+
if (!previews || lang === null)
|
|
9
|
+
return null;
|
|
10
|
+
const kind = KINDS[lang.toLowerCase()];
|
|
11
|
+
if (kind === undefined)
|
|
12
|
+
return null;
|
|
13
|
+
return previews === true || previews.includes(kind) ? kind : null;
|
|
14
|
+
}
|
|
@@ -23,6 +23,8 @@ export type StaticContext = {
|
|
|
23
23
|
lineNumbers: boolean;
|
|
24
24
|
download: boolean;
|
|
25
25
|
};
|
|
26
|
+
/** Whether a code block in a language can be shown rendered. */
|
|
27
|
+
preview: (lang: string | null) => boolean;
|
|
26
28
|
/** Whether task items can be ticked. */
|
|
27
29
|
tickable: boolean;
|
|
28
30
|
/** Whether a block has been asked for as parts. */
|
|
@@ -198,11 +198,15 @@ function block(node, tight, depth, ctx) {
|
|
|
198
198
|
// A diagram, or any language a plugin draws, is a part of its own
|
|
199
199
|
if (ctx.language(node.lang))
|
|
200
200
|
throw NOT_PLAIN;
|
|
201
|
+
// One that has been shown rendered is drawn by its component, which holds the toggle
|
|
202
|
+
if (ctx.woken(node.id))
|
|
203
|
+
throw NOT_PLAIN;
|
|
201
204
|
return codeHtml(node, {
|
|
202
205
|
labels: ctx.labels,
|
|
203
206
|
highlight: ctx.highlight,
|
|
204
207
|
lineNumbers: ctx.code.lineNumbers,
|
|
205
|
-
download: ctx.code.download
|
|
208
|
+
download: ctx.code.download,
|
|
209
|
+
preview: ctx.preview(node.lang)
|
|
206
210
|
});
|
|
207
211
|
case 'table':
|
|
208
212
|
// One whose menu has been asked for is drawn by its component, which holds the menu
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A table drawn as a PNG, as it is on the screen: for "Copy as PNG". Loaded the first time a
|
|
3
|
+
* table is copied that way.
|
|
4
|
+
*
|
|
5
|
+
* It is drawn from the layout the browser has already worked out, not from a copy of the
|
|
6
|
+
* table in an SVG `<foreignObject>`: each cell's place and fill, and each word's place, font
|
|
7
|
+
* and colour, are read from the page (client rects and computed styles) and painted onto a
|
|
8
|
+
* canvas. Nothing is cloned and nothing is fetched, so the picture is what the reader sees,
|
|
9
|
+
* in the theme they see it in, including the columns scrolled out of view.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* A table as a PNG, as it is drawn on the page now. A picture from another site, which would
|
|
13
|
+
* keep the canvas from being read, is left out: the table is drawn again without pictures.
|
|
14
|
+
*/
|
|
15
|
+
export declare function tableImage(table: HTMLTableElement): Promise<Blob>;
|
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A table drawn as a PNG, as it is on the screen: for "Copy as PNG". Loaded the first time a
|
|
3
|
+
* table is copied that way.
|
|
4
|
+
*
|
|
5
|
+
* It is drawn from the layout the browser has already worked out, not from a copy of the
|
|
6
|
+
* table in an SVG `<foreignObject>`: each cell's place and fill, and each word's place, font
|
|
7
|
+
* and colour, are read from the page (client rects and computed styles) and painted onto a
|
|
8
|
+
* canvas. Nothing is cloned and nothing is fetched, so the picture is what the reader sees,
|
|
9
|
+
* in the theme they see it in, including the columns scrolled out of view.
|
|
10
|
+
*/
|
|
11
|
+
/** As sharp as the screen, and no more than three times the table's size. */
|
|
12
|
+
const MAX_SCALE = 3;
|
|
13
|
+
/** Room around the table, in the colour behind it. */
|
|
14
|
+
const MARGIN = 16;
|
|
15
|
+
/** Whether a computed colour paints nothing. */
|
|
16
|
+
function clear(color) {
|
|
17
|
+
return (color === '' ||
|
|
18
|
+
color === 'transparent' ||
|
|
19
|
+
/^rgba\(.*,\s*0\)$/.test(color) ||
|
|
20
|
+
/\/\s*0%?\s*\)$/.test(color));
|
|
21
|
+
}
|
|
22
|
+
/** The colour of an inset box shadow, as Narvi draws a frame's edge, or null. */
|
|
23
|
+
function edgeColor(shadow) {
|
|
24
|
+
if (!shadow || shadow === 'none' || !shadow.includes('inset'))
|
|
25
|
+
return null;
|
|
26
|
+
const color = /^(?:[a-z-]+\([^)]*(?:\([^)]*\)[^)]*)*\)|#[0-9a-f]+|[a-z]+)/i.exec(shadow.trim());
|
|
27
|
+
return color && !clear(color[0]) ? color[0] : null;
|
|
28
|
+
}
|
|
29
|
+
function roundRect(ctx, rect, radius) {
|
|
30
|
+
ctx.beginPath();
|
|
31
|
+
if (radius > 0 && typeof ctx.roundRect === 'function')
|
|
32
|
+
ctx.roundRect(rect.left, rect.top, rect.width, rect.height, radius);
|
|
33
|
+
else
|
|
34
|
+
ctx.rect(rect.left, rect.top, rect.width, rect.height);
|
|
35
|
+
}
|
|
36
|
+
function fontOf(style) {
|
|
37
|
+
return `${style.fontStyle} ${style.fontWeight} ${style.fontSize} ${style.fontFamily}`;
|
|
38
|
+
}
|
|
39
|
+
/** An inline SVG as a picture: its own markup, with `currentColor` made the colour it is drawn in. */
|
|
40
|
+
function svgImage(svg) {
|
|
41
|
+
const copy = svg.cloneNode(true);
|
|
42
|
+
const rect = svg.getBoundingClientRect();
|
|
43
|
+
copy.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
|
|
44
|
+
copy.setAttribute('width', String(rect.width));
|
|
45
|
+
copy.setAttribute('height', String(rect.height));
|
|
46
|
+
copy.style.color = getComputedStyle(svg).color;
|
|
47
|
+
const image = new Image();
|
|
48
|
+
const url = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(copy.outerHTML)}`;
|
|
49
|
+
return new Promise((resolve) => {
|
|
50
|
+
image.onload = () => resolve(image);
|
|
51
|
+
image.onerror = () => resolve(null);
|
|
52
|
+
image.src = url;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
class Painter {
|
|
56
|
+
ctx;
|
|
57
|
+
images;
|
|
58
|
+
/** Pictures to draw once they have loaded: an inline SVG is read as an image first. */
|
|
59
|
+
pictures = [];
|
|
60
|
+
segmenter = typeof Intl !== 'undefined' && 'Segmenter' in Intl
|
|
61
|
+
? new Intl.Segmenter(undefined, { granularity: 'grapheme' })
|
|
62
|
+
: null;
|
|
63
|
+
constructor(ctx, images) {
|
|
64
|
+
this.ctx = ctx;
|
|
65
|
+
this.images = images;
|
|
66
|
+
}
|
|
67
|
+
async done() {
|
|
68
|
+
await Promise.all(this.pictures);
|
|
69
|
+
}
|
|
70
|
+
/** An element's fill, under each box it is drawn in: a line of inline code that wraps has two. */
|
|
71
|
+
fill(element, style) {
|
|
72
|
+
if (clear(style.backgroundColor))
|
|
73
|
+
return;
|
|
74
|
+
const radius = parseFloat(style.borderTopLeftRadius) || 0;
|
|
75
|
+
this.ctx.fillStyle = style.backgroundColor;
|
|
76
|
+
for (const rect of element.getClientRects()) {
|
|
77
|
+
roundRect(this.ctx, rect, Math.min(radius, rect.height / 2));
|
|
78
|
+
this.ctx.fill();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/** A cell's own lines, as `border-*` draws them. */
|
|
82
|
+
borders(rect, style) {
|
|
83
|
+
const side = (width, kind, color, draw) => {
|
|
84
|
+
const w = parseFloat(width) || 0;
|
|
85
|
+
if (w <= 0 || kind === 'none' || kind === 'hidden' || clear(color))
|
|
86
|
+
return;
|
|
87
|
+
const r = draw(w);
|
|
88
|
+
this.ctx.fillStyle = color;
|
|
89
|
+
this.ctx.fillRect(r.left, r.top, r.width, r.height);
|
|
90
|
+
};
|
|
91
|
+
const { left, top, width, height } = rect;
|
|
92
|
+
side(style.borderTopWidth, style.borderTopStyle, style.borderTopColor, (w) => ({
|
|
93
|
+
left,
|
|
94
|
+
top: top - w / 2,
|
|
95
|
+
width,
|
|
96
|
+
height: w
|
|
97
|
+
}));
|
|
98
|
+
side(style.borderBottomWidth, style.borderBottomStyle, style.borderBottomColor, (w) => ({
|
|
99
|
+
left,
|
|
100
|
+
top: top + height - w / 2,
|
|
101
|
+
width,
|
|
102
|
+
height: w
|
|
103
|
+
}));
|
|
104
|
+
side(style.borderLeftWidth, style.borderLeftStyle, style.borderLeftColor, (w) => ({
|
|
105
|
+
left: left - w / 2,
|
|
106
|
+
top,
|
|
107
|
+
width: w,
|
|
108
|
+
height
|
|
109
|
+
}));
|
|
110
|
+
side(style.borderRightWidth, style.borderRightStyle, style.borderRightColor, (w) => ({
|
|
111
|
+
left: left + width - w / 2,
|
|
112
|
+
top,
|
|
113
|
+
width: w,
|
|
114
|
+
height
|
|
115
|
+
}));
|
|
116
|
+
}
|
|
117
|
+
/** What is in an element: its fills, its pictures and its words, in the order they are drawn. */
|
|
118
|
+
contents(element, lines) {
|
|
119
|
+
for (const child of element.childNodes) {
|
|
120
|
+
if (child.nodeType === Node.TEXT_NODE) {
|
|
121
|
+
this.text(child, lines);
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (!(child instanceof Element))
|
|
125
|
+
continue;
|
|
126
|
+
const style = getComputedStyle(child);
|
|
127
|
+
if (style.display === 'none' || style.visibility === 'hidden')
|
|
128
|
+
continue;
|
|
129
|
+
const decoration = style.textDecorationLine;
|
|
130
|
+
const color = clear(style.textDecorationColor) ? style.color : style.textDecorationColor;
|
|
131
|
+
const inner = {
|
|
132
|
+
under: decoration.includes('underline') ? color : lines.under,
|
|
133
|
+
through: decoration.includes('line-through') ? color : lines.through
|
|
134
|
+
};
|
|
135
|
+
this.fill(child, style);
|
|
136
|
+
if (child instanceof SVGSVGElement) {
|
|
137
|
+
if (this.images)
|
|
138
|
+
this.svg(child);
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (child instanceof HTMLImageElement) {
|
|
142
|
+
if (this.images && child.complete && child.naturalWidth > 0) {
|
|
143
|
+
const r = child.getBoundingClientRect();
|
|
144
|
+
this.ctx.drawImage(child, r.left, r.top, r.width, r.height);
|
|
145
|
+
}
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
this.contents(child, inner);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
svg(svg) {
|
|
152
|
+
const rect = svg.getBoundingClientRect();
|
|
153
|
+
if (rect.width === 0 || rect.height === 0)
|
|
154
|
+
return;
|
|
155
|
+
// Drawn in the place it has now, once it has loaded; nothing is drawn over it later
|
|
156
|
+
const { ctx } = this;
|
|
157
|
+
const transform = ctx.getTransform();
|
|
158
|
+
this.pictures.push(svgImage(svg).then((image) => {
|
|
159
|
+
if (!image)
|
|
160
|
+
return;
|
|
161
|
+
ctx.save();
|
|
162
|
+
ctx.setTransform(transform);
|
|
163
|
+
ctx.drawImage(image, rect.left, rect.top, rect.width, rect.height);
|
|
164
|
+
ctx.restore();
|
|
165
|
+
}));
|
|
166
|
+
}
|
|
167
|
+
/** The pieces of text that are drawn on their own: each word, or each character of a word that wraps. */
|
|
168
|
+
pieces(text) {
|
|
169
|
+
const out = [];
|
|
170
|
+
for (const match of text.matchAll(/\S+/g)) {
|
|
171
|
+
out.push({ start: match.index, end: match.index + match[0].length });
|
|
172
|
+
}
|
|
173
|
+
return out;
|
|
174
|
+
}
|
|
175
|
+
characters(text, start, end) {
|
|
176
|
+
const word = text.slice(start, end);
|
|
177
|
+
const out = [];
|
|
178
|
+
let at = start;
|
|
179
|
+
const parts = this.segmenter
|
|
180
|
+
? Array.from(this.segmenter.segment(word), (part) => part.segment)
|
|
181
|
+
: Array.from(word);
|
|
182
|
+
for (const part of parts) {
|
|
183
|
+
out.push({ start: at, end: at + part.length });
|
|
184
|
+
at += part.length;
|
|
185
|
+
}
|
|
186
|
+
return out;
|
|
187
|
+
}
|
|
188
|
+
text(node, lines) {
|
|
189
|
+
const value = node.data;
|
|
190
|
+
if (!/\S/.test(value) || !node.parentElement)
|
|
191
|
+
return;
|
|
192
|
+
const style = getComputedStyle(node.parentElement);
|
|
193
|
+
if (style.visibility === 'hidden')
|
|
194
|
+
return;
|
|
195
|
+
const { ctx } = this;
|
|
196
|
+
ctx.font = fontOf(style);
|
|
197
|
+
ctx.fillStyle = style.color;
|
|
198
|
+
ctx.textBaseline = 'alphabetic';
|
|
199
|
+
ctx.textAlign = 'left';
|
|
200
|
+
if ('letterSpacing' in ctx)
|
|
201
|
+
ctx.letterSpacing = style.letterSpacing === 'normal' ? '0px' : style.letterSpacing;
|
|
202
|
+
const size = parseFloat(style.fontSize) || 16;
|
|
203
|
+
const range = document.createRange();
|
|
204
|
+
const draw = (start, end) => {
|
|
205
|
+
range.setStart(node, start);
|
|
206
|
+
range.setEnd(node, end);
|
|
207
|
+
const rects = [...range.getClientRects()].filter((r) => r.width > 0);
|
|
208
|
+
if (rects.length !== 1)
|
|
209
|
+
return false;
|
|
210
|
+
const rect = rects[0];
|
|
211
|
+
const piece = value.slice(start, end);
|
|
212
|
+
const metrics = ctx.measureText(piece);
|
|
213
|
+
const ascent = metrics.fontBoundingBoxAscent ?? size * 0.8;
|
|
214
|
+
const descent = metrics.fontBoundingBoxDescent ?? size * 0.2;
|
|
215
|
+
// The text's box is its font's ascent and descent; the baseline is the ascent down from
|
|
216
|
+
// the top of what is left once they are centred in it
|
|
217
|
+
const baseline = rect.top + (rect.height - ascent - descent) / 2 + ascent;
|
|
218
|
+
ctx.fillText(piece, rect.left, baseline);
|
|
219
|
+
const thickness = Math.max(1, size / 16);
|
|
220
|
+
if (lines.under) {
|
|
221
|
+
ctx.fillStyle = lines.under;
|
|
222
|
+
ctx.fillRect(rect.left, baseline + Math.max(1, descent * 0.35), rect.width, thickness);
|
|
223
|
+
ctx.fillStyle = style.color;
|
|
224
|
+
}
|
|
225
|
+
if (lines.through) {
|
|
226
|
+
ctx.fillStyle = lines.through;
|
|
227
|
+
ctx.fillRect(rect.left, baseline - size * 0.3, rect.width, thickness);
|
|
228
|
+
ctx.fillStyle = style.color;
|
|
229
|
+
}
|
|
230
|
+
return true;
|
|
231
|
+
};
|
|
232
|
+
for (const { start, end } of this.pieces(value)) {
|
|
233
|
+
// A word that the line broke inside, such as a run of CJK with no spaces, is drawn a
|
|
234
|
+
// character at a time
|
|
235
|
+
if (!draw(start, end))
|
|
236
|
+
for (const character of this.characters(value, start, end))
|
|
237
|
+
draw(character.start, character.end);
|
|
238
|
+
}
|
|
239
|
+
range.detach();
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
/** The colours behind an element, from the page down: drawn in that order, they are what shows. */
|
|
243
|
+
function backgrounds(element) {
|
|
244
|
+
const out = [];
|
|
245
|
+
for (let at = element; at; at = at.parentElement) {
|
|
246
|
+
const color = getComputedStyle(at).backgroundColor;
|
|
247
|
+
if (!clear(color))
|
|
248
|
+
out.unshift(color);
|
|
249
|
+
// An opaque colour hides everything under it
|
|
250
|
+
if (/^rgb\(/.test(color))
|
|
251
|
+
break;
|
|
252
|
+
}
|
|
253
|
+
return out;
|
|
254
|
+
}
|
|
255
|
+
function draw(table, images) {
|
|
256
|
+
const frame = table.parentElement ?? table;
|
|
257
|
+
const box = table.getBoundingClientRect();
|
|
258
|
+
const scale = Math.min(MAX_SCALE, Math.max(1, window.devicePixelRatio || 1));
|
|
259
|
+
const canvas = document.createElement('canvas');
|
|
260
|
+
canvas.width = Math.ceil((box.width + MARGIN * 2) * scale);
|
|
261
|
+
canvas.height = Math.ceil((box.height + MARGIN * 2) * scale);
|
|
262
|
+
const ctx = canvas.getContext('2d');
|
|
263
|
+
if (!ctx)
|
|
264
|
+
throw new Error('No canvas to draw on');
|
|
265
|
+
ctx.scale(scale, scale);
|
|
266
|
+
// What is behind the table, so a dark table is not pasted onto white
|
|
267
|
+
const behind = backgrounds(frame);
|
|
268
|
+
// A page that paints nothing shows the browser's own white
|
|
269
|
+
if (behind.length === 0)
|
|
270
|
+
behind.push('#fff');
|
|
271
|
+
for (const color of behind) {
|
|
272
|
+
ctx.fillStyle = color;
|
|
273
|
+
ctx.fillRect(0, 0, box.width + MARGIN * 2, box.height + MARGIN * 2);
|
|
274
|
+
}
|
|
275
|
+
// From here on, a place on the page is the same place on the canvas
|
|
276
|
+
ctx.translate(MARGIN - box.left, MARGIN - box.top);
|
|
277
|
+
const frameStyle = getComputedStyle(frame);
|
|
278
|
+
const radius = parseFloat(frameStyle.borderTopLeftRadius) || 0;
|
|
279
|
+
const shape = { left: box.left, top: box.top, width: box.width, height: box.height };
|
|
280
|
+
ctx.save();
|
|
281
|
+
roundRect(ctx, shape, radius);
|
|
282
|
+
ctx.clip();
|
|
283
|
+
const painter = new Painter(ctx, images);
|
|
284
|
+
painter.fill(table, getComputedStyle(table));
|
|
285
|
+
for (const row of table.rows) {
|
|
286
|
+
painter.fill(row, getComputedStyle(row));
|
|
287
|
+
for (const cell of row.cells) {
|
|
288
|
+
const style = getComputedStyle(cell);
|
|
289
|
+
const rect = cell.getBoundingClientRect();
|
|
290
|
+
if (!clear(style.backgroundColor)) {
|
|
291
|
+
ctx.fillStyle = style.backgroundColor;
|
|
292
|
+
ctx.fillRect(rect.left, rect.top, rect.width, rect.height);
|
|
293
|
+
}
|
|
294
|
+
painter.borders(rect, style);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
for (const row of table.rows)
|
|
298
|
+
for (const cell of row.cells)
|
|
299
|
+
painter.contents(cell, { under: null, through: null });
|
|
300
|
+
return painter.done().then(() => {
|
|
301
|
+
ctx.restore();
|
|
302
|
+
// The frame's edge, which Narvi draws as an inset shadow, over the cells it rounds off
|
|
303
|
+
const edge = edgeColor(frameStyle.boxShadow);
|
|
304
|
+
if (edge) {
|
|
305
|
+
ctx.strokeStyle = edge;
|
|
306
|
+
ctx.lineWidth = 1;
|
|
307
|
+
roundRect(ctx, {
|
|
308
|
+
left: box.left + 0.5,
|
|
309
|
+
top: box.top + 0.5,
|
|
310
|
+
width: box.width - 1,
|
|
311
|
+
height: box.height - 1
|
|
312
|
+
}, Math.max(0, radius - 0.5));
|
|
313
|
+
ctx.stroke();
|
|
314
|
+
}
|
|
315
|
+
return canvas;
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
function toBlob(canvas) {
|
|
319
|
+
return new Promise((resolve, reject) => {
|
|
320
|
+
try {
|
|
321
|
+
canvas.toBlob((blob) => (blob ? resolve(blob) : reject(new Error('The table could not be drawn'))), 'image/png');
|
|
322
|
+
}
|
|
323
|
+
catch (error) {
|
|
324
|
+
reject(error);
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* A table as a PNG, as it is drawn on the page now. A picture from another site, which would
|
|
330
|
+
* keep the canvas from being read, is left out: the table is drawn again without pictures.
|
|
331
|
+
*/
|
|
332
|
+
export async function tableImage(table) {
|
|
333
|
+
await document.fonts?.ready;
|
|
334
|
+
try {
|
|
335
|
+
return await toBlob(await draw(table, true));
|
|
336
|
+
}
|
|
337
|
+
catch (error) {
|
|
338
|
+
if (!(error instanceof DOMException && error.name === 'SecurityError'))
|
|
339
|
+
throw error;
|
|
340
|
+
return toBlob(await draw(table, false));
|
|
341
|
+
}
|
|
342
|
+
}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
import type { Sx } from '../../internal/sx.js';
|
|
5
5
|
import { colors } from '../../tokens.stylex.js';
|
|
6
6
|
import { media } from '../../media.stylex.js';
|
|
7
|
-
import Button, { buttonHtml, type ButtonProps } from '../button/Button.svelte';
|
|
7
|
+
import Button, { buttonClass, buttonHtml, type ButtonProps } from '../button/Button.svelte';
|
|
8
8
|
|
|
9
9
|
export type MessageActionProps = Omit<
|
|
10
10
|
ButtonProps,
|
|
@@ -34,11 +34,26 @@
|
|
|
34
34
|
}
|
|
35
35
|
});
|
|
36
36
|
|
|
37
|
+
/** An action's class, as the component draws it: for a button made by hand. */
|
|
38
|
+
export function actionClass(options: {
|
|
39
|
+
pressed?: boolean | undefined;
|
|
40
|
+
sx?: Sx | undefined;
|
|
41
|
+
}): string {
|
|
42
|
+
return buttonClass({
|
|
43
|
+
variant: 'ghost',
|
|
44
|
+
size: 'sm',
|
|
45
|
+
iconOnly: true,
|
|
46
|
+
sx: [styles.action, options.pressed && styles.pressed, options.sx]
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
37
50
|
/** An action as markup, as the component draws it. `label` and `attributes` are escaped already. */
|
|
38
51
|
export function actionHtml(options: {
|
|
39
52
|
label: string;
|
|
40
53
|
attributes?: string | undefined;
|
|
41
54
|
sx?: Sx | undefined;
|
|
55
|
+
/** Marks a toggle as on; give `aria-pressed` in `attributes` as well. */
|
|
56
|
+
pressed?: boolean | undefined;
|
|
42
57
|
inner: string;
|
|
43
58
|
/** As `buttonHtml` takes it: the button and its icon, with nothing between. */
|
|
44
59
|
bare?: boolean | undefined;
|
|
@@ -48,7 +63,7 @@
|
|
|
48
63
|
variant: 'ghost',
|
|
49
64
|
size: 'sm',
|
|
50
65
|
iconOnly: true,
|
|
51
|
-
sx: [styles.action, options.sx],
|
|
66
|
+
sx: [styles.action, options.pressed && styles.pressed, options.sx],
|
|
52
67
|
attributes: ` aria-label="${options.label}" data-slot="message-action"${options.attributes ?? ''}`,
|
|
53
68
|
inner: options.inner
|
|
54
69
|
});
|
|
@@ -11,11 +11,18 @@ export type MessageActionProps = Omit<ButtonProps, 'children' | 'variant' | 'siz
|
|
|
11
11
|
/** A 16px icon. */
|
|
12
12
|
children?: Snippet | undefined;
|
|
13
13
|
};
|
|
14
|
+
/** An action's class, as the component draws it: for a button made by hand. */
|
|
15
|
+
export declare function actionClass(options: {
|
|
16
|
+
pressed?: boolean | undefined;
|
|
17
|
+
sx?: Sx | undefined;
|
|
18
|
+
}): string;
|
|
14
19
|
/** An action as markup, as the component draws it. `label` and `attributes` are escaped already. */
|
|
15
20
|
export declare function actionHtml(options: {
|
|
16
21
|
label: string;
|
|
17
22
|
attributes?: string | undefined;
|
|
18
23
|
sx?: Sx | undefined;
|
|
24
|
+
/** Marks a toggle as on; give `aria-pressed` in `attributes` as well. */
|
|
25
|
+
pressed?: boolean | undefined;
|
|
19
26
|
inner: string;
|
|
20
27
|
/** As `buttonHtml` takes it: the button and its icon, with nothing between. */
|
|
21
28
|
bare?: boolean | undefined;
|