svelte-rtf-editor 1.10.0 → 1.12.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 +38 -5
- package/dist/components/InkEditor.svelte +797 -85
- package/dist/components/InkEditor.svelte.d.ts +5 -0
- package/dist/components/Toolbar.svelte +4 -4
- package/dist/index.d.ts +1 -0
- package/dist/rtf-writer.js +36 -21
- package/dist/sanitize.d.ts +43 -6
- package/dist/sanitize.js +192 -59
- package/package.json +1 -1
|
@@ -33,6 +33,10 @@ interface Props {
|
|
|
33
33
|
fontFamily?: string;
|
|
34
34
|
fontSize?: number;
|
|
35
35
|
fontSizes?: number[];
|
|
36
|
+
/** `data-` attributes of the application's own, kept on HTML that is set or pasted. */
|
|
37
|
+
keepAttributes?: string[];
|
|
38
|
+
/** Class names of the application's own, kept the same way. */
|
|
39
|
+
keepClasses?: string[];
|
|
36
40
|
onchange?: (payload: ChangePayload) => void;
|
|
37
41
|
onsave?: (payload: {
|
|
38
42
|
html: string;
|
|
@@ -53,6 +57,7 @@ declare const InkEditor: import("svelte").Component<Props, {
|
|
|
53
57
|
exportFile: (format?: string) => void;
|
|
54
58
|
importRtf: () => void;
|
|
55
59
|
setFontSize: (points: number) => void;
|
|
60
|
+
clearFormatting: () => void;
|
|
56
61
|
}, "">;
|
|
57
62
|
type InkEditor = ReturnType<typeof InkEditor>;
|
|
58
63
|
export default InkEditor;
|
|
@@ -104,9 +104,12 @@
|
|
|
104
104
|
|
|
105
105
|
<div class="toolbar-divider"></div>
|
|
106
106
|
|
|
107
|
-
<!--
|
|
107
|
+
<!-- Color, and all of the above taken off again -->
|
|
108
108
|
<div class="toolbar-group">
|
|
109
109
|
<ColorPicker value="#2C2520" title="Text color" onchange={(c) => exec('foreColor', c)} />
|
|
110
|
+
<ToolbarButton title="Clear formatting" onclick={() => exec('removeFormat')}>
|
|
111
|
+
<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>
|
|
112
|
+
</ToolbarButton>
|
|
110
113
|
</div>
|
|
111
114
|
|
|
112
115
|
<div class="toolbar-divider"></div>
|
|
@@ -197,9 +200,6 @@
|
|
|
197
200
|
<ToolbarButton title="Redo" onclick={() => exec('redo')}>
|
|
198
201
|
<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
202
|
</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
203
|
</div>
|
|
204
204
|
</div>
|
|
205
205
|
|
package/dist/index.d.ts
CHANGED
package/dist/rtf-writer.js
CHANGED
|
@@ -88,20 +88,26 @@ function parseColor(colorStr) {
|
|
|
88
88
|
};
|
|
89
89
|
}
|
|
90
90
|
// Basic named colors
|
|
91
|
-
|
|
92
|
-
black: { r: 0, g: 0, b: 0 },
|
|
93
|
-
white: { r: 255, g: 255, b: 255 },
|
|
94
|
-
red: { r: 255, g: 0, b: 0 },
|
|
95
|
-
green: { r: 0, g: 128, b: 0 },
|
|
96
|
-
blue: { r: 0, g: 0, b: 255 },
|
|
97
|
-
yellow: { r: 255, g: 255, b: 0 },
|
|
98
|
-
orange: { r: 255, g: 165, b: 0 },
|
|
99
|
-
purple: { r: 128, g: 0, b: 128 },
|
|
100
|
-
gray: { r: 128, g: 128, b: 128 },
|
|
101
|
-
grey: { r: 128, g: 128, b: 128 }
|
|
102
|
-
};
|
|
103
|
-
return named[s] || null;
|
|
91
|
+
return NAMED_COLORS.get(s) ?? null;
|
|
104
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* The colors known by name. A Map, like every table here that is asked for a name out of the
|
|
95
|
+
* document (a color, a tag): an object also answers to the names every object has, so
|
|
96
|
+
* <font color="constructor"> was a color, and went into the color table as
|
|
97
|
+
* "\redundefined\greenundefined\blueundefined".
|
|
98
|
+
*/
|
|
99
|
+
const NAMED_COLORS = new Map(Object.entries({
|
|
100
|
+
black: { r: 0, g: 0, b: 0 },
|
|
101
|
+
white: { r: 255, g: 255, b: 255 },
|
|
102
|
+
red: { r: 255, g: 0, b: 0 },
|
|
103
|
+
green: { r: 0, g: 128, b: 0 },
|
|
104
|
+
blue: { r: 0, g: 0, b: 255 },
|
|
105
|
+
yellow: { r: 255, g: 255, b: 0 },
|
|
106
|
+
orange: { r: 255, g: 165, b: 0 },
|
|
107
|
+
purple: { r: 128, g: 0, b: 128 },
|
|
108
|
+
gray: { r: 128, g: 128, b: 128 },
|
|
109
|
+
grey: { r: 128, g: 128, b: 128 }
|
|
110
|
+
}));
|
|
105
111
|
function colorKey(c) {
|
|
106
112
|
return `${c.r},${c.g},${c.b}`;
|
|
107
113
|
}
|
|
@@ -451,7 +457,7 @@ const PICTURE_OVERHEAD_BYTES = 80;
|
|
|
451
457
|
* and closing runs measured from walkChildren. Anything not listed costs
|
|
452
458
|
* nothing of its own beyond the text it contains.
|
|
453
459
|
*/
|
|
454
|
-
const BLOCK_OVERHEAD_BYTES = {
|
|
460
|
+
const BLOCK_OVERHEAD_BYTES = new Map(Object.entries({
|
|
455
461
|
p: 14,
|
|
456
462
|
div: 14,
|
|
457
463
|
h1: 29,
|
|
@@ -490,7 +496,16 @@ const BLOCK_OVERHEAD_BYTES = {
|
|
|
490
496
|
hr: 154, // "\pard\qc " + 20 x "\emdash" + "\par "
|
|
491
497
|
ul: 0,
|
|
492
498
|
ol: 0
|
|
493
|
-
};
|
|
499
|
+
}));
|
|
500
|
+
/**
|
|
501
|
+
* What an element of this tag costs, or nothing for one not listed. (Looked up in a Map: asked
|
|
502
|
+
* of an object, an element named <constructor> cost a function, and the estimate, added to
|
|
503
|
+
* that, was a piece of text. An application comparing it with a size limit then let every
|
|
504
|
+
* document through.)
|
|
505
|
+
*/
|
|
506
|
+
function overheadBytes(tag) {
|
|
507
|
+
return BLOCK_OVERHEAD_BYTES.get(tag) ?? 0;
|
|
508
|
+
}
|
|
494
509
|
/**
|
|
495
510
|
* Blocks that are written as a <div> is: a paragraph when they hold text, a container when they
|
|
496
511
|
* hold blocks. Pasted pages are made of them, and each one not known as a block had its lines
|
|
@@ -599,7 +614,7 @@ export function estimateRtfBytes(editorEl, options = {}) {
|
|
|
599
614
|
// A <div> that only holds blocks writes nothing of its own: its blocks are counted.
|
|
600
615
|
const container = tag === 'div' && Array.from(el.children).some(isBlock) && !startsWithInline(el) && !endsWithInline(el);
|
|
601
616
|
if (!container)
|
|
602
|
-
total +=
|
|
617
|
+
total += overheadBytes(tag);
|
|
603
618
|
// A heading and a code block each write their size and the size around them.
|
|
604
619
|
if (HEADING_TAGS.has(tag) || tag === 'pre')
|
|
605
620
|
total += 2 * sizeDigits;
|
|
@@ -667,14 +682,14 @@ export function estimateRtfBytes(editorEl, options = {}) {
|
|
|
667
682
|
// rowspan="0" reaches the last row; no span reaches past the table.
|
|
668
683
|
const rowCount = el.closest('table')?.querySelectorAll('tr').length ?? 1;
|
|
669
684
|
const spanned = Math.min(rowCount, rows > 0 ? rows : rowCount);
|
|
670
|
-
total += 7 + (spanned - 1) * ((
|
|
685
|
+
total += 7 + (spanned - 1) * (overheadBytes(tag) + 7);
|
|
671
686
|
}
|
|
672
687
|
}
|
|
673
688
|
// A row with fewer cells than the table has columns is filled with empty ones.
|
|
674
689
|
if (tag === 'table' && !el.parentElement?.closest('td, th')) {
|
|
675
690
|
const model = tableModel(el);
|
|
676
691
|
for (const row of model.rows)
|
|
677
|
-
total += row.filter((slot) => slot === null).length *
|
|
692
|
+
total += row.filter((slot) => slot === null).length * overheadBytes('td');
|
|
678
693
|
}
|
|
679
694
|
// Anything in a table that is in no cell is written before it, each run a paragraph
|
|
680
695
|
// ("\pard\intbl\qc\liN " and "\par ", at most 27).
|
|
@@ -1185,7 +1200,7 @@ function walkNodeAt(node, ctx, restate, endLooseText) {
|
|
|
1185
1200
|
}
|
|
1186
1201
|
// ── Character formatting ──
|
|
1187
1202
|
/** What a tag by itself does to its text. */
|
|
1188
|
-
const TAG_FORMAT = {
|
|
1203
|
+
const TAG_FORMAT = new Map(Object.entries({
|
|
1189
1204
|
b: { bold: true },
|
|
1190
1205
|
strong: { bold: true },
|
|
1191
1206
|
i: { italic: true },
|
|
@@ -1200,7 +1215,7 @@ const TAG_FORMAT = {
|
|
|
1200
1215
|
del: { strike: true },
|
|
1201
1216
|
sup: { script: 'super' },
|
|
1202
1217
|
sub: { script: 'sub' }
|
|
1203
|
-
};
|
|
1218
|
+
}));
|
|
1204
1219
|
/** Whether a font-weight is bold; null when it does not say. */
|
|
1205
1220
|
function cssBold(weight) {
|
|
1206
1221
|
if (!weight)
|
|
@@ -1225,7 +1240,7 @@ function cssBold(weight) {
|
|
|
1225
1240
|
*/
|
|
1226
1241
|
function charFormat(el, rawTag, ctx, block = false) {
|
|
1227
1242
|
const style = el.style;
|
|
1228
|
-
const tagFormat = TAG_FORMAT
|
|
1243
|
+
const tagFormat = TAG_FORMAT.get(rawTag) ?? {};
|
|
1229
1244
|
const weight = cssBold(style?.fontWeight);
|
|
1230
1245
|
const bold = weight ?? (tagFormat.bold || ctx.bold);
|
|
1231
1246
|
const fontStyle = style?.fontStyle ?? '';
|
package/dist/sanitize.d.ts
CHANGED
|
@@ -24,27 +24,54 @@
|
|
|
24
24
|
* sight (a margin below zero, a padding of thousands of pixels, a first line that starts left
|
|
25
25
|
* of its paragraph, text of no size).
|
|
26
26
|
*/
|
|
27
|
+
/** What an application asked to have kept on its own documents. */
|
|
28
|
+
export interface KeepOptions {
|
|
29
|
+
/** `data-` attributes that stay on any element. */
|
|
30
|
+
keepAttributes?: readonly string[];
|
|
31
|
+
/** Class names that stay in an element's class list. */
|
|
32
|
+
keepClasses?: readonly string[];
|
|
33
|
+
}
|
|
34
|
+
/** The same, as the sets the sanitizer reads. */
|
|
35
|
+
export interface Kept {
|
|
36
|
+
attributes: ReadonlySet<string>;
|
|
37
|
+
classes: ReadonlySet<string>;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* What an application asked to have kept (the editor's `keepAttributes` and `keepClasses`), as
|
|
41
|
+
* the sets the sanitizer reads. Only `data-` attributes can be asked for: they mark an element
|
|
42
|
+
* and do nothing else, where `onclick`, `src` or `style` asked for here would undo what this
|
|
43
|
+
* file is for. A class is kept by its name alone; what it looks like is the page's stylesheet,
|
|
44
|
+
* which is why none is kept unless the application names it. A name that is neither is left out.
|
|
45
|
+
*/
|
|
46
|
+
export declare function keptMarks(options: KeepOptions | undefined): Kept;
|
|
27
47
|
/**
|
|
28
48
|
* Make the elements under `root` safe, in place. With `only`, just the elements it accepts (the
|
|
29
49
|
* ones a paste brought in) are looked at; the rest of the document is left as it is. With
|
|
30
|
-
* `idTaken`, an id it says the page already uses is taken off.
|
|
50
|
+
* `idTaken`, an id it says the page already uses is taken off. With `keep` (see keptMarks),
|
|
51
|
+
* the application's own `data-` attributes and classes stay. `wordsLeft` is told of each element
|
|
52
|
+
* taken out with its words left in its place (a button, a text box), and of the text that holds
|
|
53
|
+
* them: an editor whose Redo puts the element back needs to know those words are there already.
|
|
54
|
+
* `leftOut` is told of each element that could not be checked, and so was taken out with what
|
|
55
|
+
* was in it: whoever shows the rest can say that a part is missing.
|
|
31
56
|
* Returns whether anything was taken out or changed.
|
|
32
57
|
*/
|
|
33
|
-
export declare function sanitizeTree(root: Element, only?: (el: Element) => boolean, idTaken?: (id: string) => boolean): boolean;
|
|
58
|
+
export declare function sanitizeTree(root: Element, only?: (el: Element) => boolean, idTaken?: (id: string) => boolean, keep?: Kept, wordsLeft?: (el: Element, words: Text) => void, leftOut?: (el: Element) => void): boolean;
|
|
34
59
|
/**
|
|
35
60
|
* HTML made safe to show and edit: see the top of this file for what is kept. It is parsed in a
|
|
36
61
|
* document of its own, where nothing in it runs or loads, before any of it reaches the page.
|
|
37
62
|
* Where there is no DOM to parse it with (on a server), nothing is returned: HTML that could
|
|
38
|
-
* not be checked is not passed on.
|
|
63
|
+
* not be checked is not passed on. `keepAttributes` and `keepClasses` name marks of the
|
|
64
|
+
* application's own to leave on (see keptMarks).
|
|
39
65
|
*/
|
|
40
|
-
export declare function sanitizeHtml(html: string): string;
|
|
66
|
+
export declare function sanitizeHtml(html: string, options?: KeepOptions): string;
|
|
41
67
|
/**
|
|
42
68
|
* The same, as nodes for `into` to hold. The editor puts these in the page as they are: what was
|
|
43
69
|
* checked is what is shown. Written out as text and read in again, a second reading has to
|
|
44
70
|
* agree with the first for the check to mean anything, and that is where a sanitizer is got
|
|
45
71
|
* round; it is also a third pass over a document that may hold megabytes of pictures.
|
|
72
|
+
* `leftOut` is told of each element that could not be checked (see sanitizeTree).
|
|
46
73
|
*/
|
|
47
|
-
export declare function sanitizedNodes(html: string, into: Document, idTaken?: (id: string) => boolean): Node[];
|
|
74
|
+
export declare function sanitizedNodes(html: string, into: Document, idTaken?: (id: string) => boolean, keep?: Kept, leftOut?: (el: Element) => void): Node[];
|
|
48
75
|
/** A run of a paste's text. */
|
|
49
76
|
interface PastePiece {
|
|
50
77
|
text: string;
|
|
@@ -60,6 +87,12 @@ export interface PasteNotes {
|
|
|
60
87
|
/** What each of its text boxes held. */
|
|
61
88
|
textareas: string[];
|
|
62
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* Whether a paste's HTML holds words. Word and Excel copy their text as HTML and put a picture
|
|
92
|
+
* of the same text beside it; a browser copies a picture as the picture and an `<img>` with no
|
|
93
|
+
* words. So a paste that has both is words where its HTML has any, and a picture where not.
|
|
94
|
+
*/
|
|
95
|
+
export declare function pasteHasWords(html: string): boolean;
|
|
63
96
|
/**
|
|
64
97
|
* Read from a paste's HTML what the browser's own paste may lose: null where it holds neither
|
|
65
98
|
* Word's hidden text nor a text box, which is nearly every paste.
|
|
@@ -72,6 +105,10 @@ export declare function pasteNotes(html: string): PasteNotes | null;
|
|
|
72
105
|
* characters were the hidden ones. An element left with nothing in it goes too, where
|
|
73
106
|
* `mayRemove` allows (a paragraph that was hidden whole would stay as an empty line in the
|
|
74
107
|
* RTF). Returns whether anything was taken out.
|
|
108
|
+
*
|
|
109
|
+
* With null for `to`, the paste is one that Redo has put back, after its hidden text was taken
|
|
110
|
+
* out once. A browser then brings back some of that text and not the rest, so each hidden
|
|
111
|
+
* piece may be there or not; all the other text of the paste has to be, from `from` on.
|
|
75
112
|
*/
|
|
76
|
-
export declare function removeWordHidden(root: Element, from: number, to: number, notes: PasteNotes, mayRemove?: (el: Element) => boolean): boolean;
|
|
113
|
+
export declare function removeWordHidden(root: Element, from: number, to: number | null, notes: PasteNotes, mayRemove?: (el: Element) => boolean): boolean;
|
|
77
114
|
export {};
|