svelte-rtf-editor 1.11.0 → 1.13.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 +130 -14
- package/dist/RtfViewer.svelte +9 -4
- package/dist/RtfViewer.svelte.d.ts +1 -0
- package/dist/components/InkEditor.svelte +1326 -124
- package/dist/components/InkEditor.svelte.d.ts +5 -0
- package/dist/components/Modal.svelte +9 -13
- package/dist/components/Toolbar.svelte +75 -20
- package/dist/components/Toolbar.svelte.d.ts +11 -0
- package/dist/list-markers.d.ts +38 -0
- package/dist/list-markers.js +129 -0
- package/dist/paste-pictures.d.ts +22 -0
- package/dist/paste-pictures.js +57 -0
- package/dist/rtf-parser.d.ts +23 -0
- package/dist/rtf-parser.js +186 -17
- package/dist/rtf-writer.d.ts +55 -3
- package/dist/rtf-writer.js +293 -53
- package/dist/sanitize.d.ts +55 -4
- package/dist/sanitize.js +228 -56
- package/dist/utils.js +22 -1
- package/package.json +1 -1
|
@@ -21,6 +21,7 @@ interface Props {
|
|
|
21
21
|
autosave?: boolean;
|
|
22
22
|
storageKey?: string;
|
|
23
23
|
showToolbar?: boolean;
|
|
24
|
+
stickyToolbar?: boolean;
|
|
24
25
|
showStatusBar?: boolean;
|
|
25
26
|
minHeight?: string;
|
|
26
27
|
readonly?: boolean;
|
|
@@ -33,6 +34,8 @@ interface Props {
|
|
|
33
34
|
fontFamily?: string;
|
|
34
35
|
fontSize?: number;
|
|
35
36
|
fontSizes?: number[];
|
|
37
|
+
/** The fonts offered for text, beside the document's own; none unless given. */
|
|
38
|
+
fonts?: string[];
|
|
36
39
|
/** `data-` attributes of the application's own, kept on HTML that is set or pasted. */
|
|
37
40
|
keepAttributes?: string[];
|
|
38
41
|
/** Class names of the application's own, kept the same way. */
|
|
@@ -57,6 +60,8 @@ declare const InkEditor: import("svelte").Component<Props, {
|
|
|
57
60
|
exportFile: (format?: string) => void;
|
|
58
61
|
importRtf: () => void;
|
|
59
62
|
setFontSize: (points: number) => void;
|
|
63
|
+
setFont: (name: string | null) => void;
|
|
64
|
+
clearFormatting: () => void;
|
|
60
65
|
}, "">;
|
|
61
66
|
type InkEditor = ReturnType<typeof InkEditor>;
|
|
62
67
|
export default InkEditor;
|
|
@@ -54,19 +54,15 @@
|
|
|
54
54
|
e.preventDefault();
|
|
55
55
|
return;
|
|
56
56
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
} else if (!e.shiftKey && active === last) {
|
|
67
|
-
e.preventDefault();
|
|
68
|
-
first.focus();
|
|
69
|
-
}
|
|
57
|
+
// Every step is made here, not only the turn at the ends. Safari's Tab goes to text
|
|
58
|
+
// fields alone unless the user has asked for more, so from the last field it went past
|
|
59
|
+
// the dialog's buttons and out of the dialog, into the page behind it.
|
|
60
|
+
e.preventDefault();
|
|
61
|
+
const at = items.indexOf(document.activeElement as HTMLElement);
|
|
62
|
+
const next = at === -1 ? items[e.shiftKey ? items.length - 1 : 0] : items[(at + (e.shiftKey ? -1 : 1) + items.length) % items.length];
|
|
63
|
+
next.focus();
|
|
64
|
+
// As Tab into a field does: what it holds is selected, to be typed over.
|
|
65
|
+
if (next instanceof HTMLInputElement && /^(text|url|search|email|tel|password|number)$/.test(next.type)) next.select();
|
|
70
66
|
}
|
|
71
67
|
</script>
|
|
72
68
|
|
|
@@ -14,6 +14,13 @@
|
|
|
14
14
|
/** The sizes offered, in points. */
|
|
15
15
|
fontSizes?: number[];
|
|
16
16
|
onfontsize?: (points: number) => void;
|
|
17
|
+
/** The font of the text at the caret, by its key: '' for the document font, null where the selection has more than one. */
|
|
18
|
+
font?: string | null;
|
|
19
|
+
/** The fonts offered beside the document's own. None, and there is no font list. */
|
|
20
|
+
fonts?: { name: string; css: string; key: string }[];
|
|
21
|
+
/** The document font's name: the first of the list, and what '' is shown as. */
|
|
22
|
+
documentFont?: string;
|
|
23
|
+
onfont?: (name: string) => void;
|
|
17
24
|
/** The Copy format brush is in hand: the button shows as pressed. */
|
|
18
25
|
copyingFormat?: boolean;
|
|
19
26
|
oncopyformat?: () => void;
|
|
@@ -29,6 +36,10 @@
|
|
|
29
36
|
fontSize = null,
|
|
30
37
|
fontSizes = [],
|
|
31
38
|
onfontsize,
|
|
39
|
+
font = '',
|
|
40
|
+
fonts = [],
|
|
41
|
+
documentFont = '',
|
|
42
|
+
onfont,
|
|
32
43
|
copyingFormat = false,
|
|
33
44
|
oncopyformat
|
|
34
45
|
}: Props = $props();
|
|
@@ -49,6 +60,8 @@
|
|
|
49
60
|
<div class="toolbar-group">
|
|
50
61
|
<select
|
|
51
62
|
class="tb-select"
|
|
63
|
+
title="Paragraph style"
|
|
64
|
+
aria-label="Paragraph style"
|
|
52
65
|
value={blockType}
|
|
53
66
|
onchange={(e) => onblock?.((e.target as HTMLSelectElement).value)}
|
|
54
67
|
>
|
|
@@ -61,6 +74,30 @@
|
|
|
61
74
|
</select>
|
|
62
75
|
</div>
|
|
63
76
|
|
|
77
|
+
{#if fonts.length > 0}
|
|
78
|
+
<!-- Font -->
|
|
79
|
+
<div class="toolbar-group">
|
|
80
|
+
<select
|
|
81
|
+
class="tb-select tb-font"
|
|
82
|
+
title="Font"
|
|
83
|
+
aria-label="Font"
|
|
84
|
+
value={font === null ? ' ' : font}
|
|
85
|
+
onchange={(e) => {
|
|
86
|
+
const key = (e.target as HTMLSelectElement).value;
|
|
87
|
+
if (key !== ' ') onfont?.(key);
|
|
88
|
+
}}
|
|
89
|
+
>
|
|
90
|
+
{#if font === null}
|
|
91
|
+
<option value=" "></option>
|
|
92
|
+
{/if}
|
|
93
|
+
<option value="">{documentFont}</option>
|
|
94
|
+
{#each fonts as choice (choice.key)}
|
|
95
|
+
<option value={choice.key} style:font-family={choice.css}>{choice.name}</option>
|
|
96
|
+
{/each}
|
|
97
|
+
</select>
|
|
98
|
+
</div>
|
|
99
|
+
{/if}
|
|
100
|
+
|
|
64
101
|
{#if fontSizes.length > 0}
|
|
65
102
|
<!-- Font size -->
|
|
66
103
|
<div class="toolbar-group">
|
|
@@ -104,9 +141,12 @@
|
|
|
104
141
|
|
|
105
142
|
<div class="toolbar-divider"></div>
|
|
106
143
|
|
|
107
|
-
<!--
|
|
144
|
+
<!-- Color, and all of the above taken off again -->
|
|
108
145
|
<div class="toolbar-group">
|
|
109
146
|
<ColorPicker value="#2C2520" title="Text color" onchange={(c) => exec('foreColor', c)} />
|
|
147
|
+
<ToolbarButton title="Clear formatting" onclick={() => exec('removeFormat')}>
|
|
148
|
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M4 7V4h16v3"/><path d="M9 20h6"/><path d="M12 4v16"/><line x1="18" y1="4" x2="4" y2="20" stroke="var(--accent)" stroke-width="2"/></svg>
|
|
149
|
+
</ToolbarButton>
|
|
110
150
|
</div>
|
|
111
151
|
|
|
112
152
|
<div class="toolbar-divider"></div>
|
|
@@ -197,20 +237,17 @@
|
|
|
197
237
|
<ToolbarButton title="Redo" onclick={() => exec('redo')}>
|
|
198
238
|
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M21 7v6h-6"/><path d="M21 13c0 0-2.03-7-9-7a9 9 0 0 0-9 9 9 9 0 0 0 9 9c3.5 0 6-1.5 7.5-4"/></svg>
|
|
199
239
|
</ToolbarButton>
|
|
200
|
-
<ToolbarButton title="Clear formatting" onclick={() => exec('removeFormat')}>
|
|
201
|
-
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M4 7V4h16v3"/><path d="M9 20h6"/><path d="M12 4v16"/><line x1="18" y1="4" x2="4" y2="20" stroke="var(--accent)" stroke-width="2"/></svg>
|
|
202
|
-
</ToolbarButton>
|
|
203
240
|
</div>
|
|
204
241
|
</div>
|
|
205
242
|
|
|
206
243
|
<style>
|
|
244
|
+
/* (Where it stands, and that it stays in sight of a long document, is the editor's: see
|
|
245
|
+
.ink-head there. Its ground is not see-through: the document passes under it.) */
|
|
207
246
|
.toolbar {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
border:
|
|
211
|
-
border-radius: var(--radius);
|
|
247
|
+
background: var(--bg-toolbar, #fff);
|
|
248
|
+
border: 1px solid var(--border, #e5e2dc);
|
|
249
|
+
border-radius: var(--radius, 8px);
|
|
212
250
|
padding: 6px 8px;
|
|
213
|
-
margin: 24px 0 16px;
|
|
214
251
|
display: flex;
|
|
215
252
|
align-items: center;
|
|
216
253
|
gap: 2px;
|
|
@@ -232,34 +269,52 @@
|
|
|
232
269
|
flex-shrink: 0;
|
|
233
270
|
}
|
|
234
271
|
|
|
272
|
+
/* The lists are boxes one can see: a list that shows nothing (a selection in two fonts, or
|
|
273
|
+
in two sizes) is still there to be chosen from. */
|
|
235
274
|
.tb-select {
|
|
236
|
-
height:
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
border-radius:
|
|
275
|
+
height: 32px;
|
|
276
|
+
margin: 0 3px;
|
|
277
|
+
padding: 0 26px 0 11px;
|
|
278
|
+
border: 1px solid var(--border, #d9d4cc);
|
|
279
|
+
border-radius: 8px;
|
|
280
|
+
background-color: var(--bg-select, transparent);
|
|
241
281
|
font-family: 'DM Sans', sans-serif;
|
|
242
282
|
font-size: 13px;
|
|
243
283
|
font-weight: 500;
|
|
244
284
|
color: var(--text-muted);
|
|
245
285
|
cursor: pointer;
|
|
246
|
-
transition:
|
|
286
|
+
transition:
|
|
287
|
+
border-color var(--transition, 150ms ease),
|
|
288
|
+
background-color var(--transition, 150ms ease),
|
|
289
|
+
box-shadow var(--transition, 150ms ease);
|
|
247
290
|
appearance: none;
|
|
248
291
|
-webkit-appearance: none;
|
|
249
|
-
padding-right: 22px;
|
|
250
292
|
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%238A7E72' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
|
|
251
293
|
background-repeat: no-repeat;
|
|
252
|
-
background-position: right
|
|
294
|
+
background-position: right 8px center;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/* Wide enough for a font's name, and no narrower when it shows none. */
|
|
298
|
+
.tb-font {
|
|
299
|
+
min-width: 9em;
|
|
300
|
+
max-width: 12em;
|
|
253
301
|
}
|
|
254
302
|
|
|
255
303
|
/* Two or three digits: narrower than the list of paragraph kinds. */
|
|
256
304
|
.tb-size {
|
|
257
|
-
min-width:
|
|
258
|
-
padding-left: 8px;
|
|
305
|
+
min-width: 62px;
|
|
259
306
|
}
|
|
260
307
|
|
|
308
|
+
/* (The color alone: `background` would take the arrow off with it.) */
|
|
261
309
|
.tb-select:hover {
|
|
262
|
-
background: var(--surface);
|
|
310
|
+
background-color: var(--surface, #f6f4f0);
|
|
311
|
+
border-color: var(--border-active, #b8ab98);
|
|
263
312
|
color: var(--text);
|
|
264
313
|
}
|
|
314
|
+
|
|
315
|
+
.tb-select:focus-visible {
|
|
316
|
+
outline: none;
|
|
317
|
+
border-color: var(--accent, #6e56cf);
|
|
318
|
+
box-shadow: 0 0 0 3px var(--accent-soft, #ede9fe);
|
|
319
|
+
}
|
|
265
320
|
</style>
|
|
@@ -10,6 +10,17 @@ interface Props {
|
|
|
10
10
|
/** The sizes offered, in points. */
|
|
11
11
|
fontSizes?: number[];
|
|
12
12
|
onfontsize?: (points: number) => void;
|
|
13
|
+
/** The font of the text at the caret, by its key: '' for the document font, null where the selection has more than one. */
|
|
14
|
+
font?: string | null;
|
|
15
|
+
/** The fonts offered beside the document's own. None, and there is no font list. */
|
|
16
|
+
fonts?: {
|
|
17
|
+
name: string;
|
|
18
|
+
css: string;
|
|
19
|
+
key: string;
|
|
20
|
+
}[];
|
|
21
|
+
/** The document font's name: the first of the list, and what '' is shown as. */
|
|
22
|
+
documentFont?: string;
|
|
23
|
+
onfont?: (name: string) => void;
|
|
13
24
|
/** The Copy format brush is in hand: the button shows as pressed. */
|
|
14
25
|
copyingFormat?: boolean;
|
|
15
26
|
oncopyformat?: () => void;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The marker a numbered list item shows: "3.", "C.", "iii.".
|
|
3
|
+
*
|
|
4
|
+
* A browser numbers the items of an <ol> by the list's `type` or `list-style-type`, the list's
|
|
5
|
+
* `start` and an item's own `value`. The RTF and the Markdown carry a marker as text, so what is
|
|
6
|
+
* written is worked out here, once, the way a browser shows it: a list lettered A, B, C that was
|
|
7
|
+
* written 1, 2, 3 left "see part B" pointing at an item labelled "2.".
|
|
8
|
+
*/
|
|
9
|
+
/** The ways a numbered list counts that are written as they show. Any other is written in digits. */
|
|
10
|
+
export type NumberStyle = 'decimal' | 'upper-alpha' | 'lower-alpha' | 'upper-roman' | 'lower-roman';
|
|
11
|
+
/** The `type` attribute that shows a style: 'A' for upper-alpha. */
|
|
12
|
+
export declare function listTypeAttribute(style: NumberStyle): string;
|
|
13
|
+
/**
|
|
14
|
+
* How an item of a numbered list counts: its own `list-style-type` or `type` (Google Docs puts
|
|
15
|
+
* the style on each item), then the list's. A style that is none of NumberStyle counts in digits.
|
|
16
|
+
*/
|
|
17
|
+
export declare function numberStyleOf(list: Element, item?: Element | null): NumberStyle;
|
|
18
|
+
/** The number a list starts at: its `start`, or 1. */
|
|
19
|
+
export declare function listStart(list: Element): number;
|
|
20
|
+
/** The number of an item: its own `value` where it has one, else the count so far. */
|
|
21
|
+
export declare function itemNumber(item: Element, counter: number): number;
|
|
22
|
+
/**
|
|
23
|
+
* A number as a list of that style shows it, without the period: 3, "C", "iii". A number the
|
|
24
|
+
* style cannot write (letters below 1, Roman numerals below 1 or above 3999) is shown in digits,
|
|
25
|
+
* as a browser shows it.
|
|
26
|
+
*/
|
|
27
|
+
export declare function markerText(n: number, style: NumberStyle): string;
|
|
28
|
+
/** A marker written in letters, read as a number: by which style, and which number. */
|
|
29
|
+
export interface LetterReading {
|
|
30
|
+
style: NumberStyle;
|
|
31
|
+
number: number;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* What a marker written in letters can mean: "C" is the third letter and the Roman 100; "IV" is
|
|
35
|
+
* a Roman 4; "ab" is the 28th item of a lettered list. Empty for letters that are no marker: of
|
|
36
|
+
* mixed case, more than two letters that are no Roman numeral.
|
|
37
|
+
*/
|
|
38
|
+
export declare function letterReadings(letters: string): LetterReading[];
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The marker a numbered list item shows: "3.", "C.", "iii.".
|
|
3
|
+
*
|
|
4
|
+
* A browser numbers the items of an <ol> by the list's `type` or `list-style-type`, the list's
|
|
5
|
+
* `start` and an item's own `value`. The RTF and the Markdown carry a marker as text, so what is
|
|
6
|
+
* written is worked out here, once, the way a browser shows it: a list lettered A, B, C that was
|
|
7
|
+
* written 1, 2, 3 left "see part B" pointing at an item labelled "2.".
|
|
8
|
+
*/
|
|
9
|
+
/** The `type` attribute of an <ol> for each style, and back. */
|
|
10
|
+
const TYPE_OF_STYLE = {
|
|
11
|
+
decimal: '1',
|
|
12
|
+
'upper-alpha': 'A',
|
|
13
|
+
'lower-alpha': 'a',
|
|
14
|
+
'upper-roman': 'I',
|
|
15
|
+
'lower-roman': 'i'
|
|
16
|
+
};
|
|
17
|
+
const STYLE_OF_TYPE = new Map(Object.entries(TYPE_OF_STYLE).map(([style, type]) => [type, style]));
|
|
18
|
+
/** What CSS calls each style; `upper-latin` is another name for `upper-alpha`. */
|
|
19
|
+
const STYLE_OF_CSS = new Map([
|
|
20
|
+
['decimal', 'decimal'],
|
|
21
|
+
['upper-alpha', 'upper-alpha'],
|
|
22
|
+
['upper-latin', 'upper-alpha'],
|
|
23
|
+
['lower-alpha', 'lower-alpha'],
|
|
24
|
+
['lower-latin', 'lower-alpha'],
|
|
25
|
+
['upper-roman', 'upper-roman'],
|
|
26
|
+
['lower-roman', 'lower-roman']
|
|
27
|
+
]);
|
|
28
|
+
/** The `type` attribute that shows a style: 'A' for upper-alpha. */
|
|
29
|
+
export function listTypeAttribute(style) {
|
|
30
|
+
return TYPE_OF_STYLE[style];
|
|
31
|
+
}
|
|
32
|
+
/** The style an element asks for by itself, or null: its `list-style-type`, then its `type`. */
|
|
33
|
+
function ownStyle(el) {
|
|
34
|
+
if (!el)
|
|
35
|
+
return null;
|
|
36
|
+
const css = (el.style?.listStyleType ?? '').trim().toLowerCase();
|
|
37
|
+
if (css)
|
|
38
|
+
return STYLE_OF_CSS.get(css) ?? 'decimal';
|
|
39
|
+
// (The attribute tells "A" from "a": it is not lowercased.)
|
|
40
|
+
return STYLE_OF_TYPE.get((el.getAttribute('type') ?? '').trim()) ?? null;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* How an item of a numbered list counts: its own `list-style-type` or `type` (Google Docs puts
|
|
44
|
+
* the style on each item), then the list's. A style that is none of NumberStyle counts in digits.
|
|
45
|
+
*/
|
|
46
|
+
export function numberStyleOf(list, item) {
|
|
47
|
+
return ownStyle(item) ?? ownStyle(list) ?? 'decimal';
|
|
48
|
+
}
|
|
49
|
+
/** The number a list starts at: its `start`, or 1. */
|
|
50
|
+
export function listStart(list) {
|
|
51
|
+
const start = parseInt(list.getAttribute('start') ?? '', 10);
|
|
52
|
+
return Number.isFinite(start) ? start : 1;
|
|
53
|
+
}
|
|
54
|
+
/** The number of an item: its own `value` where it has one, else the count so far. */
|
|
55
|
+
export function itemNumber(item, counter) {
|
|
56
|
+
if (!item.hasAttribute('value'))
|
|
57
|
+
return counter;
|
|
58
|
+
const value = parseInt(item.getAttribute('value') ?? '', 10);
|
|
59
|
+
return Number.isFinite(value) ? value : counter;
|
|
60
|
+
}
|
|
61
|
+
const ROMAN = [
|
|
62
|
+
[1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'], [100, 'C'], [90, 'XC'],
|
|
63
|
+
[50, 'L'], [40, 'XL'], [10, 'X'], [9, 'IX'], [5, 'V'], [4, 'IV'], [1, 'I']
|
|
64
|
+
];
|
|
65
|
+
function roman(n) {
|
|
66
|
+
let out = '';
|
|
67
|
+
for (const [value, letters] of ROMAN) {
|
|
68
|
+
while (n >= value) {
|
|
69
|
+
out += letters;
|
|
70
|
+
n -= value;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
/** A, B, … Z, AA, AB, …: the letters count as a browser's do. */
|
|
76
|
+
function alpha(n) {
|
|
77
|
+
let out = '';
|
|
78
|
+
for (; n > 0; n = Math.floor((n - 1) / 26))
|
|
79
|
+
out = String.fromCharCode(65 + ((n - 1) % 26)) + out;
|
|
80
|
+
return out;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* A number as a list of that style shows it, without the period: 3, "C", "iii". A number the
|
|
84
|
+
* style cannot write (letters below 1, Roman numerals below 1 or above 3999) is shown in digits,
|
|
85
|
+
* as a browser shows it.
|
|
86
|
+
*/
|
|
87
|
+
export function markerText(n, style) {
|
|
88
|
+
if (style === 'decimal' || !Number.isInteger(n) || n < 1)
|
|
89
|
+
return String(n);
|
|
90
|
+
if (style === 'upper-alpha')
|
|
91
|
+
return alpha(n);
|
|
92
|
+
if (style === 'lower-alpha')
|
|
93
|
+
return alpha(n).toLowerCase();
|
|
94
|
+
if (n > 3999)
|
|
95
|
+
return String(n);
|
|
96
|
+
return style === 'upper-roman' ? roman(n) : roman(n).toLowerCase();
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* What a marker written in letters can mean: "C" is the third letter and the Roman 100; "IV" is
|
|
100
|
+
* a Roman 4; "ab" is the 28th item of a lettered list. Empty for letters that are no marker: of
|
|
101
|
+
* mixed case, more than two letters that are no Roman numeral.
|
|
102
|
+
*/
|
|
103
|
+
export function letterReadings(letters) {
|
|
104
|
+
const upper = letters.toUpperCase();
|
|
105
|
+
const isUpper = letters === upper;
|
|
106
|
+
if (!/^[A-Z]+$/.test(upper) || (!isUpper && letters !== letters.toLowerCase()))
|
|
107
|
+
return [];
|
|
108
|
+
const readings = [];
|
|
109
|
+
if (upper.length <= 2) {
|
|
110
|
+
let number = 0;
|
|
111
|
+
for (const letter of upper)
|
|
112
|
+
number = number * 26 + (letter.charCodeAt(0) - 64);
|
|
113
|
+
readings.push({ style: isUpper ? 'upper-alpha' : 'lower-alpha', number });
|
|
114
|
+
}
|
|
115
|
+
if (/^[MDCLXVI]+$/.test(upper)) {
|
|
116
|
+
let number = 0;
|
|
117
|
+
let rest = upper;
|
|
118
|
+
for (const [value, numeral] of ROMAN) {
|
|
119
|
+
while (rest.startsWith(numeral)) {
|
|
120
|
+
number += value;
|
|
121
|
+
rest = rest.slice(numeral.length);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
// (Only a numeral as it is written: "IIII" and "VX" are letters.)
|
|
125
|
+
if (number >= 1 && number <= 3999 && roman(number) === upper)
|
|
126
|
+
readings.push({ style: isUpper ? 'upper-roman' : 'lower-roman', number });
|
|
127
|
+
}
|
|
128
|
+
return readings;
|
|
129
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** What is known of a paste's pictures before the browser has pasted it. */
|
|
2
|
+
export interface PastePictures {
|
|
3
|
+
/** For each image of the HTML, in order: whether it is a file on the writer's disk. */
|
|
4
|
+
local: boolean[];
|
|
5
|
+
/** The picture of each of those that are, in order: its data, or null for one no browser shows. */
|
|
6
|
+
pictures: (string | null)[];
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Read from a paste's HTML and RTF the pictures the HTML only names: null where it names none,
|
|
10
|
+
* or the RTF does not have them, which is nearly every paste.
|
|
11
|
+
*
|
|
12
|
+
* Nothing is guessed: unless the RTF has as many pictures as the HTML has images on the
|
|
13
|
+
* writer's disk, it is not known which is which, and none is given. A wrong picture in a
|
|
14
|
+
* report is worse than a missing one.
|
|
15
|
+
*/
|
|
16
|
+
export declare function pastePictures(html: string, rtf: string): PastePictures | null;
|
|
17
|
+
/**
|
|
18
|
+
* Give the images a paste brought in the pictures read for them. `images` are those that came
|
|
19
|
+
* in, in order: where they are not as many as the HTML had, the browser has pasted something
|
|
20
|
+
* else than was read, and nothing is done. Returns how many were given a picture.
|
|
21
|
+
*/
|
|
22
|
+
export declare function givePastedPictures(images: Element[], known: PastePictures): number;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pictures of a paste from Word.
|
|
3
|
+
*
|
|
4
|
+
* Word puts what is copied on the clipboard several times over. Its HTML names each picture by
|
|
5
|
+
* the address of a file on the writer's own disk (file:///…/msohtmlclip/clip_image001.png),
|
|
6
|
+
* which a page may not open: the picture came in broken, and left in the RTF as "[Image: …]",
|
|
7
|
+
* its description and no picture. Word's RTF copy of the same selection holds the pictures
|
|
8
|
+
* themselves. So where a paste has both, the pictures are read from the RTF and given to the
|
|
9
|
+
* images of the HTML, in order.
|
|
10
|
+
*/
|
|
11
|
+
import { DEFAULT_MAX_RTF_BYTES, rtfPictures } from './rtf-parser.js';
|
|
12
|
+
/** An address that is a file on the disk of the computer the copy was made on. */
|
|
13
|
+
const LOCAL_FILE = /^\s*file:/i;
|
|
14
|
+
/**
|
|
15
|
+
* Read from a paste's HTML and RTF the pictures the HTML only names: null where it names none,
|
|
16
|
+
* or the RTF does not have them, which is nearly every paste.
|
|
17
|
+
*
|
|
18
|
+
* Nothing is guessed: unless the RTF has as many pictures as the HTML has images on the
|
|
19
|
+
* writer's disk, it is not known which is which, and none is given. A wrong picture in a
|
|
20
|
+
* report is worse than a missing one.
|
|
21
|
+
*/
|
|
22
|
+
export function pastePictures(html, rtf) {
|
|
23
|
+
if (!rtf || !/<img\b/i.test(html) || !/file:/i.test(html) || !rtf.includes('\\pict') || typeof DOMParser === 'undefined')
|
|
24
|
+
return null;
|
|
25
|
+
if (rtf.length > DEFAULT_MAX_RTF_BYTES)
|
|
26
|
+
return null;
|
|
27
|
+
const images = Array.from(new DOMParser().parseFromString(html, 'text/html').body.querySelectorAll('img'));
|
|
28
|
+
const local = images.map((img) => LOCAL_FILE.test(img.getAttribute('src') ?? ''));
|
|
29
|
+
const count = local.filter(Boolean).length;
|
|
30
|
+
if (count === 0)
|
|
31
|
+
return null;
|
|
32
|
+
const pictures = rtfPictures(rtf);
|
|
33
|
+
if (pictures.length !== count || pictures.every((picture) => picture === null))
|
|
34
|
+
return null;
|
|
35
|
+
return { local, pictures };
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Give the images a paste brought in the pictures read for them. `images` are those that came
|
|
39
|
+
* in, in order: where they are not as many as the HTML had, the browser has pasted something
|
|
40
|
+
* else than was read, and nothing is done. Returns how many were given a picture.
|
|
41
|
+
*/
|
|
42
|
+
export function givePastedPictures(images, known) {
|
|
43
|
+
if (images.length !== known.local.length)
|
|
44
|
+
return 0;
|
|
45
|
+
let given = 0;
|
|
46
|
+
let next = 0;
|
|
47
|
+
images.forEach((img, i) => {
|
|
48
|
+
if (!known.local[i])
|
|
49
|
+
return;
|
|
50
|
+
const picture = known.pictures[next++];
|
|
51
|
+
if (picture === null)
|
|
52
|
+
return;
|
|
53
|
+
img.setAttribute('src', picture);
|
|
54
|
+
given++;
|
|
55
|
+
});
|
|
56
|
+
return given;
|
|
57
|
+
}
|
package/dist/rtf-parser.d.ts
CHANGED
|
@@ -17,6 +17,16 @@
|
|
|
17
17
|
* of what is handed in is the only bound on how long the page stops responding.
|
|
18
18
|
*/
|
|
19
19
|
export declare const DEFAULT_MAX_RTF_BYTES: number;
|
|
20
|
+
/**
|
|
21
|
+
* The pictures of an RTF document, in the order they are in it: each as the data address a
|
|
22
|
+
* browser shows (PNG or JPEG), or null for one it cannot show (a metafile).
|
|
23
|
+
*
|
|
24
|
+
* Word copies a picture to the clipboard twice: in its HTML as the address of a file on the
|
|
25
|
+
* writer's own disk, which no page may open, and in its RTF as the picture itself. So the
|
|
26
|
+
* pictures of a paste are read from here (see pastePictures). The second copy Word writes of
|
|
27
|
+
* each for older readers (\nonshppict, and a shape's \shprslt) is not counted.
|
|
28
|
+
*/
|
|
29
|
+
export declare function rtfPictures(rtf: string): (string | null)[];
|
|
20
30
|
/** Options for rtfToHtml. */
|
|
21
31
|
export interface RtfReadOptions {
|
|
22
32
|
/**
|
|
@@ -33,6 +43,19 @@ export interface RtfReadOptions {
|
|
|
33
43
|
* (50 MB). 0 reads a document of any size.
|
|
34
44
|
*/
|
|
35
45
|
maxBytes?: number;
|
|
46
|
+
/**
|
|
47
|
+
* The fonts text may be shown in, beside the document's own: the editor's or viewer's
|
|
48
|
+
* `fonts`. Text the RTF sets in one of them carries that font; text in any other font is
|
|
49
|
+
* shown in the document font, as all text is where this is not given. Each is a font name,
|
|
50
|
+
* or a CSS list whose first family is the name. Default: none.
|
|
51
|
+
*/
|
|
52
|
+
fonts?: readonly string[];
|
|
53
|
+
/**
|
|
54
|
+
* The document font of the screen the HTML will be shown on: the editor's or viewer's
|
|
55
|
+
* `fontFamily`. Text the RTF sets in it carries no font of its own. Only read where `fonts`
|
|
56
|
+
* is given. Default: DEFAULT_FONT_FAMILY.
|
|
57
|
+
*/
|
|
58
|
+
fontFamily?: string;
|
|
36
59
|
}
|
|
37
60
|
export declare function rtfToHtml(rtfString: string, options?: RtfReadOptions): string;
|
|
38
61
|
export declare function readRtfFile(file: File, options?: RtfReadOptions): Promise<string>;
|