svelte-rtf-editor 1.8.3 → 1.10.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 +212 -26
- package/dist/RtfViewer.svelte +69 -7
- package/dist/RtfViewer.svelte.d.ts +2 -1
- package/dist/components/ImageOverlay.svelte +11 -3
- package/dist/components/ImageOverlay.svelte.d.ts +2 -0
- package/dist/components/InkEditor.svelte +705 -35
- package/dist/components/InkEditor.svelte.d.ts +14 -3
- package/dist/components/Modal.svelte +57 -3
- package/dist/components/Toolbar.svelte +44 -1
- package/dist/components/Toolbar.svelte.d.ts +5 -0
- package/dist/components/ToolbarButton.svelte +8 -2
- package/dist/components/ToolbarButton.svelte.d.ts +4 -0
- package/dist/images.d.ts +60 -7
- package/dist/images.js +268 -59
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/rtf-parser.d.ts +16 -3
- package/dist/rtf-parser.js +1682 -534
- package/dist/rtf-writer.d.ts +40 -9
- package/dist/rtf-writer.js +1175 -359
- package/dist/sanitize.d.ts +77 -0
- package/dist/sanitize.js +755 -0
- package/dist/table-model.d.ts +49 -0
- package/dist/table-model.js +203 -0
- package/dist/utils.js +689 -87
- package/package.json +16 -12
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
interface ChangePayload {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
/**
|
|
3
|
+
* `html`, `text` and `estimatedRtfBytes` are worked out when the handler reads them, and
|
|
4
|
+
* once: on a long document each is work, and most handlers read one of them or none.
|
|
5
|
+
* They are the document as it is when first read, so read them in the handler. A payload
|
|
6
|
+
* kept and read after a later edit gives the document as it is by then.
|
|
7
|
+
*/
|
|
8
|
+
readonly html: string;
|
|
9
|
+
readonly text: string;
|
|
4
10
|
wordCount: number;
|
|
5
11
|
charCount: number;
|
|
6
12
|
/**
|
|
7
13
|
* Approximate size of this document as RTF. Pictures dominate it and are
|
|
8
14
|
* counted exactly; see getRtfSize() for the precise figure.
|
|
9
15
|
*/
|
|
10
|
-
estimatedRtfBytes: number;
|
|
16
|
+
readonly estimatedRtfBytes: number;
|
|
11
17
|
}
|
|
12
18
|
interface Props {
|
|
13
19
|
content?: string;
|
|
@@ -18,11 +24,15 @@ interface Props {
|
|
|
18
24
|
showStatusBar?: boolean;
|
|
19
25
|
minHeight?: string;
|
|
20
26
|
readonly?: boolean;
|
|
27
|
+
ariaLabel?: string;
|
|
21
28
|
maxImageEdge?: number;
|
|
22
29
|
maxImageBytes?: number;
|
|
23
30
|
maxImageDisplayWidth?: number;
|
|
31
|
+
/** The largest RTF file Import reads (readRtfFile's maxBytes); 0 for no limit. */
|
|
32
|
+
maxRtfBytes?: number;
|
|
24
33
|
fontFamily?: string;
|
|
25
34
|
fontSize?: number;
|
|
35
|
+
fontSizes?: number[];
|
|
26
36
|
onchange?: (payload: ChangePayload) => void;
|
|
27
37
|
onsave?: (payload: {
|
|
28
38
|
html: string;
|
|
@@ -42,6 +52,7 @@ declare const InkEditor: import("svelte").Component<Props, {
|
|
|
42
52
|
focus: () => void;
|
|
43
53
|
exportFile: (format?: string) => void;
|
|
44
54
|
importRtf: () => void;
|
|
55
|
+
setFontSize: (points: number) => void;
|
|
45
56
|
}, "">;
|
|
46
57
|
type InkEditor = ReturnType<typeof InkEditor>;
|
|
47
58
|
export default InkEditor;
|
|
@@ -11,12 +11,62 @@
|
|
|
11
11
|
|
|
12
12
|
let { open = false, title = '', onclose, children, actions }: Props = $props();
|
|
13
13
|
|
|
14
|
+
let dialogEl: HTMLDivElement | null = $state(null);
|
|
15
|
+
const titleId = `ink-modal-title-${Math.random().toString(36).slice(2, 10)}`;
|
|
16
|
+
|
|
17
|
+
const FOCUSABLE = 'input:not([disabled]):not([type="hidden"]), button:not([disabled]), select:not([disabled]), textarea:not([disabled]), a[href], [tabindex]:not([tabindex="-1"])';
|
|
18
|
+
|
|
19
|
+
function focusable(): HTMLElement[] {
|
|
20
|
+
return dialogEl ? Array.from(dialogEl.querySelectorAll<HTMLElement>(FOCUSABLE)) : [];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// An open dialog takes the keyboard. Left where it was, in the document behind the dialog,
|
|
24
|
+
// what was typed for the dialog (a link's address) replaced the selected text instead.
|
|
25
|
+
// Closed with Escape or Cancel, the keyboard goes back to where it was.
|
|
26
|
+
$effect(() => {
|
|
27
|
+
if (!open || !dialogEl) return;
|
|
28
|
+
const previous = document.activeElement instanceof HTMLElement ? document.activeElement : null;
|
|
29
|
+
const first = dialogEl.querySelector<HTMLElement>('[data-autofocus]') ?? focusable()[0] ?? dialogEl;
|
|
30
|
+
first.focus();
|
|
31
|
+
const dialog = dialogEl;
|
|
32
|
+
return () => {
|
|
33
|
+
// Only if the keyboard is still the dialog's to hand back: whoever closed the dialog
|
|
34
|
+
// may have put it somewhere already (Insert puts it in the document).
|
|
35
|
+
const active = document.activeElement;
|
|
36
|
+
if (previous?.isConnected && (!active || active === document.body || dialog.contains(active))) previous.focus();
|
|
37
|
+
};
|
|
38
|
+
});
|
|
39
|
+
|
|
14
40
|
function handleOverlayClick(e: MouseEvent) {
|
|
15
41
|
if (e.target === e.currentTarget) onclose?.();
|
|
16
42
|
}
|
|
17
43
|
|
|
18
44
|
function handleKeydown(e: KeyboardEvent) {
|
|
19
|
-
if (e.key === 'Escape')
|
|
45
|
+
if (e.key === 'Escape') {
|
|
46
|
+
e.preventDefault();
|
|
47
|
+
onclose?.();
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
// Tab goes round the dialog, not on into the page behind it.
|
|
51
|
+
if (e.key !== 'Tab' || !dialogEl) return;
|
|
52
|
+
const items = focusable();
|
|
53
|
+
if (items.length === 0) {
|
|
54
|
+
e.preventDefault();
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const active = document.activeElement;
|
|
58
|
+
const first = items[0];
|
|
59
|
+
const last = items[items.length - 1];
|
|
60
|
+
if (!dialogEl.contains(active)) {
|
|
61
|
+
e.preventDefault();
|
|
62
|
+
first.focus();
|
|
63
|
+
} else if (e.shiftKey && active === first) {
|
|
64
|
+
e.preventDefault();
|
|
65
|
+
last.focus();
|
|
66
|
+
} else if (!e.shiftKey && active === last) {
|
|
67
|
+
e.preventDefault();
|
|
68
|
+
first.focus();
|
|
69
|
+
}
|
|
20
70
|
}
|
|
21
71
|
</script>
|
|
22
72
|
|
|
@@ -26,8 +76,8 @@
|
|
|
26
76
|
<!-- svelte-ignore a11y_click_events_have_key_events -->
|
|
27
77
|
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
|
28
78
|
<div class="modal-overlay" onclick={handleOverlayClick}>
|
|
29
|
-
<div class="modal">
|
|
30
|
-
<h3>{title}</h3>
|
|
79
|
+
<div class="modal" role="dialog" aria-modal="true" aria-labelledby={titleId} tabindex="-1" bind:this={dialogEl}>
|
|
80
|
+
<h3 id={titleId}>{title}</h3>
|
|
31
81
|
{@render children()}
|
|
32
82
|
{#if actions}
|
|
33
83
|
<div class="modal-actions">
|
|
@@ -71,6 +121,10 @@
|
|
|
71
121
|
to { opacity: 1; transform: translateY(0); }
|
|
72
122
|
}
|
|
73
123
|
|
|
124
|
+
.modal:focus {
|
|
125
|
+
outline: none;
|
|
126
|
+
}
|
|
127
|
+
|
|
74
128
|
.modal h3 {
|
|
75
129
|
font-family: 'Instrument Serif', serif;
|
|
76
130
|
font-size: 22px;
|
|
@@ -9,6 +9,11 @@
|
|
|
9
9
|
onblock?: (tag: string) => void;
|
|
10
10
|
oninsertlink?: () => void;
|
|
11
11
|
oninsertimage?: () => void;
|
|
12
|
+
/** The size of the text at the caret, in points; null where the selection has more than one. */
|
|
13
|
+
fontSize?: number | null;
|
|
14
|
+
/** The sizes offered, in points. */
|
|
15
|
+
fontSizes?: number[];
|
|
16
|
+
onfontsize?: (points: number) => void;
|
|
12
17
|
/** The Copy format brush is in hand: the button shows as pressed. */
|
|
13
18
|
copyingFormat?: boolean;
|
|
14
19
|
oncopyformat?: () => void;
|
|
@@ -21,16 +26,25 @@
|
|
|
21
26
|
onblock,
|
|
22
27
|
oninsertlink,
|
|
23
28
|
oninsertimage,
|
|
29
|
+
fontSize = null,
|
|
30
|
+
fontSizes = [],
|
|
31
|
+
onfontsize,
|
|
24
32
|
copyingFormat = false,
|
|
25
33
|
oncopyformat
|
|
26
34
|
}: Props = $props();
|
|
27
35
|
|
|
36
|
+
// The size at the caret is shown even when it is not one of the sizes offered (13.5 pt in a
|
|
37
|
+
// document from elsewhere), so the list never shows a size the text does not have.
|
|
38
|
+
const sizeChoices = $derived(
|
|
39
|
+
fontSize !== null && !fontSizes.includes(fontSize) ? [...fontSizes, fontSize].sort((a, b) => a - b) : fontSizes
|
|
40
|
+
);
|
|
41
|
+
|
|
28
42
|
function exec(cmd: string, value: string | null = null) {
|
|
29
43
|
onexec?.(cmd, value);
|
|
30
44
|
}
|
|
31
45
|
</script>
|
|
32
46
|
|
|
33
|
-
<div class="toolbar">
|
|
47
|
+
<div class="toolbar" role="toolbar" aria-label="Formatting">
|
|
34
48
|
<!-- Block type -->
|
|
35
49
|
<div class="toolbar-group">
|
|
36
50
|
<select
|
|
@@ -47,6 +61,29 @@
|
|
|
47
61
|
</select>
|
|
48
62
|
</div>
|
|
49
63
|
|
|
64
|
+
{#if fontSizes.length > 0}
|
|
65
|
+
<!-- Font size -->
|
|
66
|
+
<div class="toolbar-group">
|
|
67
|
+
<select
|
|
68
|
+
class="tb-select tb-size"
|
|
69
|
+
title="Font size"
|
|
70
|
+
aria-label="Font size"
|
|
71
|
+
value={fontSize === null ? '' : String(fontSize)}
|
|
72
|
+
onchange={(e) => {
|
|
73
|
+
const points = Number((e.target as HTMLSelectElement).value);
|
|
74
|
+
if (points > 0) onfontsize?.(points);
|
|
75
|
+
}}
|
|
76
|
+
>
|
|
77
|
+
{#if fontSize === null}
|
|
78
|
+
<option value=""></option>
|
|
79
|
+
{/if}
|
|
80
|
+
{#each sizeChoices as size (size)}
|
|
81
|
+
<option value={String(size)}>{size}</option>
|
|
82
|
+
{/each}
|
|
83
|
+
</select>
|
|
84
|
+
</div>
|
|
85
|
+
{/if}
|
|
86
|
+
|
|
50
87
|
<div class="toolbar-divider"></div>
|
|
51
88
|
|
|
52
89
|
<!-- Inline formatting -->
|
|
@@ -215,6 +252,12 @@
|
|
|
215
252
|
background-position: right 6px center;
|
|
216
253
|
}
|
|
217
254
|
|
|
255
|
+
/* Two or three digits: narrower than the list of paragraph kinds. */
|
|
256
|
+
.tb-size {
|
|
257
|
+
min-width: 52px;
|
|
258
|
+
padding-left: 8px;
|
|
259
|
+
}
|
|
260
|
+
|
|
218
261
|
.tb-select:hover {
|
|
219
262
|
background: var(--surface);
|
|
220
263
|
color: var(--text);
|
|
@@ -5,6 +5,11 @@ interface Props {
|
|
|
5
5
|
onblock?: (tag: string) => void;
|
|
6
6
|
oninsertlink?: () => void;
|
|
7
7
|
oninsertimage?: () => void;
|
|
8
|
+
/** The size of the text at the caret, in points; null where the selection has more than one. */
|
|
9
|
+
fontSize?: number | null;
|
|
10
|
+
/** The sizes offered, in points. */
|
|
11
|
+
fontSizes?: number[];
|
|
12
|
+
onfontsize?: (points: number) => void;
|
|
8
13
|
/** The Copy format brush is in hand: the button shows as pressed. */
|
|
9
14
|
copyingFormat?: boolean;
|
|
10
15
|
oncopyformat?: () => void;
|
|
@@ -2,19 +2,25 @@
|
|
|
2
2
|
import type { Snippet } from 'svelte';
|
|
3
3
|
|
|
4
4
|
interface Props {
|
|
5
|
+
/**
|
|
6
|
+
* Whether the button is on, for one that is on or off (Bold). Left out for one that does
|
|
7
|
+
* something once (Undo, Insert image): that one is not announced as pressed or not.
|
|
8
|
+
*/
|
|
5
9
|
active?: boolean;
|
|
6
10
|
title?: string;
|
|
7
11
|
onclick?: () => void;
|
|
8
12
|
children: Snippet;
|
|
9
13
|
}
|
|
10
14
|
|
|
11
|
-
let { active
|
|
15
|
+
let { active, title = '', onclick, children }: Props = $props();
|
|
12
16
|
</script>
|
|
13
17
|
|
|
14
18
|
<button
|
|
15
19
|
class="tb"
|
|
16
|
-
class:active
|
|
20
|
+
class:active={active === true}
|
|
17
21
|
{title}
|
|
22
|
+
aria-label={title || undefined}
|
|
23
|
+
aria-pressed={active}
|
|
18
24
|
onclick={onclick}
|
|
19
25
|
onmousedown={(e) => e.preventDefault()}
|
|
20
26
|
type="button"
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import type { Snippet } from 'svelte';
|
|
2
2
|
interface Props {
|
|
3
|
+
/**
|
|
4
|
+
* Whether the button is on, for one that is on or off (Bold). Left out for one that does
|
|
5
|
+
* something once (Undo, Insert image): that one is not announced as pressed or not.
|
|
6
|
+
*/
|
|
3
7
|
active?: boolean;
|
|
4
8
|
title?: string;
|
|
5
9
|
onclick?: () => void;
|
package/dist/images.d.ts
CHANGED
|
@@ -33,6 +33,23 @@ export declare const DEFAULT_MAX_IMAGE_DISPLAY_WIDTH = 624;
|
|
|
33
33
|
* image is never enlarged beyond its natural size.
|
|
34
34
|
*/
|
|
35
35
|
export declare function initialDisplayWidth(naturalWidth: number, columnWidth: number, maxDisplayWidth: number): number;
|
|
36
|
+
/**
|
|
37
|
+
* A picture that could not be brought under the byte limit. The limit is a ceiling: a picture
|
|
38
|
+
* over it is not handed back for the caller to embed anyway.
|
|
39
|
+
*/
|
|
40
|
+
export declare class ImageTooLargeError extends Error {
|
|
41
|
+
readonly maxBytes: number;
|
|
42
|
+
constructor(maxBytes: number);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* A picture the browser could not open: a format it does not show (TIFF, HEIC in most browsers),
|
|
46
|
+
* or a damaged file. Its size has nothing to do with it, so it is refused in these words whatever
|
|
47
|
+
* the limit. It could not be shown in the editor, and what it holds could not be checked or
|
|
48
|
+
* converted for the RTF.
|
|
49
|
+
*/
|
|
50
|
+
export declare class ImageUnreadableError extends Error {
|
|
51
|
+
constructor();
|
|
52
|
+
}
|
|
36
53
|
export interface ImageLimits {
|
|
37
54
|
/** Longest edge in px; 0 or undefined leaves the dimensions alone. */
|
|
38
55
|
maxEdge?: number;
|
|
@@ -63,23 +80,59 @@ export declare function dataUrlByteLength(dataUrl: string): number;
|
|
|
63
80
|
export declare function isSafeImageUrl(url: string): boolean;
|
|
64
81
|
export declare function readFileAsDataUrl(file: Blob): Promise<string>;
|
|
65
82
|
/**
|
|
66
|
-
* Prepare a data URL for embedding: scale it down to `maxEdge
|
|
67
|
-
*
|
|
68
|
-
* photographs do not balloon; everything else becomes
|
|
69
|
-
*
|
|
70
|
-
*
|
|
83
|
+
* Prepare a data URL for embedding: scale it down to `maxEdge`, re-encode it to
|
|
84
|
+
* PNG when its format cannot be carried by RTF, and bring it under `maxBytes`.
|
|
85
|
+
* JPEG sources stay JPEG so photographs do not balloon; everything else becomes
|
|
86
|
+
* PNG while that fits. Animated images keep their first frame.
|
|
87
|
+
*
|
|
88
|
+
* `maxBytes` is a ceiling. The picture is shrunk until it fits:
|
|
89
|
+
*
|
|
90
|
+
* 1. At full size, then 80%, 64% and 50% of it, a PNG source is tried as PNG first, so
|
|
91
|
+
* screenshots and diagrams stay sharp, and as JPEG only when it has no transparency to lose.
|
|
92
|
+
* 2. If none of those fits, transparency is given up: the picture is laid on white and tried as
|
|
93
|
+
* JPEG from full size again, then smaller and smaller, each step sized from how far over the
|
|
94
|
+
* last one was.
|
|
95
|
+
*
|
|
96
|
+
* A picture that cannot be brought under the limit, because nothing legible would be left of it
|
|
97
|
+
* or because this runtime cannot redraw it, is refused with an ImageTooLargeError; it is never
|
|
98
|
+
* returned over the limit. Where the runtime cannot rasterise (SSR, a test DOM), a picture within
|
|
99
|
+
* the limit is passed through as it is.
|
|
100
|
+
*
|
|
101
|
+
* A browser has a largest canvas (268 million pixels in Chromium, about 16.7 million in Safari on
|
|
102
|
+
* a phone). Only a picture larger than `maxEdge` normally allows can meet it: with `maxEdge` 0, or
|
|
103
|
+
* a very large one. Such a picture is brought down to a size the canvas can hold, and the steps
|
|
104
|
+
* above start from there.
|
|
105
|
+
*
|
|
106
|
+
* A picture the browser cannot open at all is refused with an ImageUnreadableError, whatever its
|
|
107
|
+
* size.
|
|
71
108
|
*/
|
|
72
109
|
export declare function toRtfSafeDataUrl(dataUrl: string, limits?: ImageLimits): Promise<string>;
|
|
73
110
|
/** Read an image File into a data URL the RTF writer can embed. */
|
|
74
111
|
export declare function fileToImageSrc(file: File, limits?: ImageLimits): Promise<string>;
|
|
112
|
+
/** How long a pasted or typed picture address may take to answer before it is given up on. */
|
|
113
|
+
export declare const IMAGE_FETCH_TIMEOUT_MS = 15000;
|
|
114
|
+
/** The most a fetched picture may weigh before it is scaled down: 25 MB. */
|
|
115
|
+
export declare const MAX_FETCHED_IMAGE_BYTES: number;
|
|
75
116
|
/**
|
|
76
117
|
* Resolve a URL to an embeddable data URL. Remote images are fetched so they
|
|
77
|
-
* survive export; when the fetch is blocked (CORS, offline)
|
|
78
|
-
*
|
|
118
|
+
* survive export; when the fetch is blocked (CORS, offline), takes longer than
|
|
119
|
+
* IMAGE_FETCH_TIMEOUT_MS or is larger than MAX_FETCHED_IMAGE_BYTES, the original
|
|
120
|
+
* URL is kept and the image still displays — it just exports as a text
|
|
121
|
+
* placeholder. `embedded` is true only for PNG or JPEG data, the two formats the
|
|
122
|
+
* RTF writer carries: a GIF, WebP or SVG that could not be converted is not.
|
|
123
|
+
*
|
|
124
|
+
* A picture that could not be brought under `maxBytes` is never returned as data. One fetched
|
|
125
|
+
* from an address keeps its address, and so exports as the placeholder. One given as data has
|
|
126
|
+
* no address to fall back to: it comes back with `tooLarge` set, and must not be inserted.
|
|
127
|
+
*
|
|
128
|
+
* Picture data the browser cannot open comes back with `unreadable` set, and must not be inserted
|
|
129
|
+
* either: it would show as a broken picture. `tooLarge` and `unreadable` are never both set.
|
|
79
130
|
*/
|
|
80
131
|
export declare function urlToImageSrc(url: string, limits?: ImageLimits): Promise<{
|
|
81
132
|
src: string;
|
|
82
133
|
embedded: boolean;
|
|
134
|
+
tooLarge?: true;
|
|
135
|
+
unreadable?: true;
|
|
83
136
|
}>;
|
|
84
137
|
/** "sunset-over-lake.png" → "sunset-over-lake" — a usable default alt text. */
|
|
85
138
|
export declare function stripExtension(filename: string): string;
|