svelte-rtf-editor 1.13.0 → 1.16.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,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
+ }