svelte-rtf-editor 1.12.0 → 1.15.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.
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Pictures side by side with a set gap between them.
3
+ *
4
+ * Pictures side by side are a table of one row, a picture in each cell. Without a gap the row
5
+ * is as wide as the page and its cells share it equally; how far apart the pictures are then
6
+ * depends on how wide the page is, and each is aligned within its share.
7
+ *
8
+ * With a gap, the row is laid out exactly: each picture's cell is as wide as the picture,
9
+ * and between two pictures is an empty cell as wide as the gap. The table has no width of
10
+ * its own, so it is as wide as what it holds, on the screen and in the RTF, where each cell
11
+ * ends at an edge in twips (\cellx) and nothing is stretched to the page.
12
+ *
13
+ * <table style="border-collapse:collapse;margin:4px 0"><tbody><tr>
14
+ * <td style="padding:0;width:150pt"><figure><img …></figure></td>
15
+ * <td style="padding:0;width:18pt"></td>
16
+ * <td style="padding:0;width:150pt"><figure><img …></figure></td>
17
+ * </tr></tbody></table>
18
+ *
19
+ * The RTF reader knows the shape again, a row of picture cells with an empty cell between
20
+ * each two, and gives the same widths back.
21
+ */
22
+ /** The style of the table of a row of pictures with a set gap: no width of its own. */
23
+ export declare const GAPPED_ROW_STYLE = "border-collapse:collapse;margin:4px 0";
24
+ /** The widest gap that can be set, in points: two inches. */
25
+ export declare const MOST_GAP_PT = 144;
26
+ /** The width an element asks for as a length (not a share), in twips; null where it asks for none. */
27
+ export declare function lengthTwips(el: Element): number | null;
28
+ /** A width in twips as the points a cell is given: exact to the twip. */
29
+ export declare const pointsOf: (twips: number) => string;
30
+ /**
31
+ * A cell nothing is drawn on or around: no border, no background. A table that has them is a
32
+ * table of its own, whatever it holds, and is written and read as one.
33
+ */
34
+ export declare function isPlainCell(cell: Element): boolean;
35
+ /**
36
+ * The gap between two pictures: a plain cell with a width of its own and nothing in it. (A
37
+ * line break or blanks are nothing: the caret put in the cell and taken out again leaves a
38
+ * <br> there in Chromium, and that is still the gap.)
39
+ */
40
+ export declare function isGapCell(cell: Element): boolean;
41
+ /** A cell that holds one picture and nothing else: alone, or in its figure with its caption. */
42
+ export declare function holdsOnePicture(cell: Element): boolean;
43
+ /**
44
+ * Each column's width in twips, where the table is a row of pictures with set gaps: one row,
45
+ * a picture's cell first and last and between each two of them a gap cell, every cell plain
46
+ * and with a length of its own, and the table with no width. Null for any other table, which is written
47
+ * across the page as it always was.
48
+ */
49
+ export declare function gappedRowWidths(table: Element): number[] | null;
@@ -0,0 +1,109 @@
1
+ /**
2
+ * Pictures side by side with a set gap between them.
3
+ *
4
+ * Pictures side by side are a table of one row, a picture in each cell. Without a gap the row
5
+ * is as wide as the page and its cells share it equally; how far apart the pictures are then
6
+ * depends on how wide the page is, and each is aligned within its share.
7
+ *
8
+ * With a gap, the row is laid out exactly: each picture's cell is as wide as the picture,
9
+ * and between two pictures is an empty cell as wide as the gap. The table has no width of
10
+ * its own, so it is as wide as what it holds, on the screen and in the RTF, where each cell
11
+ * ends at an edge in twips (\cellx) and nothing is stretched to the page.
12
+ *
13
+ * <table style="border-collapse:collapse;margin:4px 0"><tbody><tr>
14
+ * <td style="padding:0;width:150pt"><figure><img …></figure></td>
15
+ * <td style="padding:0;width:18pt"></td>
16
+ * <td style="padding:0;width:150pt"><figure><img …></figure></td>
17
+ * </tr></tbody></table>
18
+ *
19
+ * The RTF reader knows the shape again, a row of picture cells with an empty cell between
20
+ * each two, and gives the same widths back.
21
+ */
22
+ /** The style of the table of a row of pictures with a set gap: no width of its own. */
23
+ export const GAPPED_ROW_STYLE = 'border-collapse:collapse;margin:4px 0';
24
+ /** The widest gap that can be set, in points: two inches. */
25
+ export const MOST_GAP_PT = 144;
26
+ const TWIPS_PER_UNIT = { px: 15, pt: 20, in: 1440, cm: 1440 / 2.54, mm: 144 / 2.54 };
27
+ /** The width an element asks for as a length (not a share), in twips; null where it asks for none. */
28
+ export function lengthTwips(el) {
29
+ const value = (el.style?.width || el.getAttribute('width') || '').trim().toLowerCase();
30
+ const m = value.match(/^(\d+(?:\.\d+)?|\.\d+)(px|pt|in|cm|mm)?$/);
31
+ if (!m)
32
+ return null;
33
+ const n = parseFloat(m[1]);
34
+ return n > 0 ? n * TWIPS_PER_UNIT[m[2] ?? 'px'] : null;
35
+ }
36
+ /** A width in twips as the points a cell is given: exact to the twip. */
37
+ export const pointsOf = (twips) => `${Math.round(twips) / 20}pt`;
38
+ const isCell = (el) => /^(td|th)$/i.test(el.tagName);
39
+ /**
40
+ * A cell nothing is drawn on or around: no border, no background. A table that has them is a
41
+ * table of its own, whatever it holds, and is written and read as one.
42
+ */
43
+ export function isPlainCell(cell) {
44
+ if (cell.hasAttribute('bgcolor') || cell.hasAttribute('background'))
45
+ return false;
46
+ const style = cell.style;
47
+ if (!style)
48
+ return true;
49
+ for (const name of Array.from(style))
50
+ if (/^(border|background)/.test(name) && !/^(none|0|0px|transparent|initial)$/.test(style.getPropertyValue(name).trim()))
51
+ return false;
52
+ return true;
53
+ }
54
+ /**
55
+ * The gap between two pictures: a plain cell with a width of its own and nothing in it. (A
56
+ * line break or blanks are nothing: the caret put in the cell and taken out again leaves a
57
+ * <br> there in Chromium, and that is still the gap.)
58
+ */
59
+ export function isGapCell(cell) {
60
+ if (!isCell(cell) || !isPlainCell(cell) || lengthTwips(cell) === null)
61
+ return false;
62
+ return Array.from(cell.childNodes).every((node) => (node.nodeType === 1 ? node.tagName === 'BR' : node.nodeType === 3 ? (node.textContent ?? '').trim() === '' : node.nodeType === 8));
63
+ }
64
+ /** A cell that holds one picture and nothing else: alone, or in its figure with its caption. */
65
+ export function holdsOnePicture(cell) {
66
+ const held = Array.from(cell.children).filter((child) => child.tagName !== 'BR');
67
+ if (held.length !== 1)
68
+ return false;
69
+ const unit = held[0];
70
+ const tag = unit.tagName.toUpperCase();
71
+ const pictures = tag === 'IMG' ? 1 : tag === 'FIGURE' ? unit.querySelectorAll('img').length : 0;
72
+ if (pictures !== 1)
73
+ return false;
74
+ // (Words typed beside the picture make the cell an ordinary one.)
75
+ return (cell.textContent ?? '').trim() === (unit.textContent ?? '').trim();
76
+ }
77
+ /**
78
+ * Each column's width in twips, where the table is a row of pictures with set gaps: one row,
79
+ * a picture's cell first and last and between each two of them a gap cell, every cell plain
80
+ * and with a length of its own, and the table with no width. Null for any other table, which is written
81
+ * across the page as it always was.
82
+ */
83
+ export function gappedRowWidths(table) {
84
+ if (lengthOrShare(table))
85
+ return null;
86
+ const rows = Array.from(table.querySelectorAll('tr')).filter((row) => row.closest('table') === table);
87
+ if (rows.length !== 1)
88
+ return null;
89
+ const cells = Array.from(rows[0].children).filter(isCell);
90
+ if (cells.length < 3 || cells.length % 2 === 0)
91
+ return null;
92
+ const widths = [];
93
+ for (let i = 0; i < cells.length; i++) {
94
+ const cell = cells[i];
95
+ if ((cell.getAttribute('colspan') ?? '1') !== '1' || (cell.getAttribute('rowspan') ?? '1') !== '1')
96
+ return null;
97
+ if (i % 2 === 0 ? !holdsOnePicture(cell) || !isPlainCell(cell) : !isGapCell(cell))
98
+ return null;
99
+ const twips = lengthTwips(cell);
100
+ if (twips === null)
101
+ return null;
102
+ widths.push(twips);
103
+ }
104
+ return widths;
105
+ }
106
+ /** Whether an element says how wide it is, in any unit. */
107
+ function lengthOrShare(el) {
108
+ return !!(el.style?.width || el.getAttribute('width'));
109
+ }
@@ -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>;