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
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A <table> as a grid: which cell covers which row and column once rowspan and colspan are
|
|
3
|
+
* resolved. RTF and Markdown both describe a table column by column, so both writers start here.
|
|
4
|
+
*/
|
|
5
|
+
/** One <td> or <th>, with where it starts and how far it reaches. */
|
|
6
|
+
export interface GridCell {
|
|
7
|
+
el: HTMLElement;
|
|
8
|
+
row: number;
|
|
9
|
+
col: number;
|
|
10
|
+
rowspan: number;
|
|
11
|
+
colspan: number;
|
|
12
|
+
}
|
|
13
|
+
export interface TableModel {
|
|
14
|
+
/** The number of columns in the widest row. */
|
|
15
|
+
columns: number;
|
|
16
|
+
/** For each row, the cell that covers each column, or null where no cell does. */
|
|
17
|
+
rows: (GridCell | null)[][];
|
|
18
|
+
cells: GridCell[];
|
|
19
|
+
/** For each column, which column of the markup it is: columns no cell starts in are not counted. */
|
|
20
|
+
source: number[];
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* A table's rows in the order a browser shows them: the head first and the foot last, wherever
|
|
24
|
+
* the source wrote them.
|
|
25
|
+
*/
|
|
26
|
+
export declare function tableRowsInOrder(table: Element): Element[];
|
|
27
|
+
/** A row's own cells. */
|
|
28
|
+
export declare function rowCells(row: Element): HTMLElement[];
|
|
29
|
+
/**
|
|
30
|
+
* Lay a table's cells out on a grid. A cell goes in the first free column of its row and covers
|
|
31
|
+
* colspan columns and rowspan rows from there; rowspan="0" reaches the last row, as in HTML, and
|
|
32
|
+
* no span reaches past the table. Where two cells would overlap, the one placed first keeps the slot.
|
|
33
|
+
*
|
|
34
|
+
* A column in which no cell starts is not a column: a browser gives it no width. That is what
|
|
35
|
+
* colspan="100" on a cell meant to cross "the whole table" leaves behind, and counted as columns
|
|
36
|
+
* they would squeeze the real ones into a sliver of the page.
|
|
37
|
+
*/
|
|
38
|
+
export declare function tableModel(table: Element): TableModel;
|
|
39
|
+
/**
|
|
40
|
+
* The share of the table's width each column takes, adding up to 1. A column is as wide as a
|
|
41
|
+
* cell of its own says, or else its <col> (width: 25%); a cell across several columns gives what
|
|
42
|
+
* it has left to those of them with no width; the rest share what remains equally.
|
|
43
|
+
*
|
|
44
|
+
* Widths given as lengths (120px, 90pt, the bare width="120" of a pasted table) count too: as a
|
|
45
|
+
* share of the table where the table's own width is a length, and otherwise, where no width is a
|
|
46
|
+
* percentage, in proportion to each other. Lengths beside percentages with no table width to
|
|
47
|
+
* measure them against cannot be compared, and are left out.
|
|
48
|
+
*/
|
|
49
|
+
export declare function columnShares(model: TableModel, table?: Element): number[];
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A <table> as a grid: which cell covers which row and column once rowspan and colspan are
|
|
3
|
+
* resolved. RTF and Markdown both describe a table column by column, so both writers start here.
|
|
4
|
+
*/
|
|
5
|
+
/** The most columns one cell may span, as in HTML. */
|
|
6
|
+
const MAX_COLSPAN = 1000;
|
|
7
|
+
/**
|
|
8
|
+
* A table's rows in the order a browser shows them: the head first and the foot last, wherever
|
|
9
|
+
* the source wrote them.
|
|
10
|
+
*/
|
|
11
|
+
export function tableRowsInOrder(table) {
|
|
12
|
+
const pick = (selector) => Array.from(table.querySelectorAll(selector));
|
|
13
|
+
return [
|
|
14
|
+
...pick(':scope > thead > tr'),
|
|
15
|
+
...pick(':scope > tbody > tr, :scope > tr'),
|
|
16
|
+
...pick(':scope > tfoot > tr')
|
|
17
|
+
];
|
|
18
|
+
}
|
|
19
|
+
/** A row's own cells. */
|
|
20
|
+
export function rowCells(row) {
|
|
21
|
+
return Array.from(row.querySelectorAll(':scope > td, :scope > th'));
|
|
22
|
+
}
|
|
23
|
+
function span(el, name) {
|
|
24
|
+
const value = parseInt(el.getAttribute(name) ?? '', 10);
|
|
25
|
+
return Number.isFinite(value) && value >= 0 ? value : 1;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Lay a table's cells out on a grid. A cell goes in the first free column of its row and covers
|
|
29
|
+
* colspan columns and rowspan rows from there; rowspan="0" reaches the last row, as in HTML, and
|
|
30
|
+
* no span reaches past the table. Where two cells would overlap, the one placed first keeps the slot.
|
|
31
|
+
*
|
|
32
|
+
* A column in which no cell starts is not a column: a browser gives it no width. That is what
|
|
33
|
+
* colspan="100" on a cell meant to cross "the whole table" leaves behind, and counted as columns
|
|
34
|
+
* they would squeeze the real ones into a sliver of the page.
|
|
35
|
+
*/
|
|
36
|
+
export function tableModel(table) {
|
|
37
|
+
const trs = tableRowsInOrder(table);
|
|
38
|
+
const cells = [];
|
|
39
|
+
/** Put each cell on a grid, in order; returns the grid and its width. */
|
|
40
|
+
const place = () => {
|
|
41
|
+
const rows = trs.map(() => []);
|
|
42
|
+
let columns = 0;
|
|
43
|
+
for (const cell of cells) {
|
|
44
|
+
for (let dr = 0; dr < cell.rowspan; dr++) {
|
|
45
|
+
for (let dc = 0; dc < cell.colspan; dc++) {
|
|
46
|
+
if (!rows[cell.row + dr][cell.col + dc])
|
|
47
|
+
rows[cell.row + dr][cell.col + dc] = cell;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
columns = Math.max(columns, cell.col + cell.colspan);
|
|
51
|
+
}
|
|
52
|
+
return { rows, columns };
|
|
53
|
+
};
|
|
54
|
+
// First pass: where each cell starts, with the spans the markup gives.
|
|
55
|
+
const taken = trs.map(() => []);
|
|
56
|
+
trs.forEach((tr, r) => {
|
|
57
|
+
let c = 0;
|
|
58
|
+
for (const el of rowCells(tr)) {
|
|
59
|
+
while (taken[r][c])
|
|
60
|
+
c++;
|
|
61
|
+
const colspan = Math.min(MAX_COLSPAN, Math.max(1, span(el, 'colspan')));
|
|
62
|
+
const rowspan = Math.min(trs.length - r, span(el, 'rowspan') || trs.length - r);
|
|
63
|
+
cells.push({ el, row: r, col: c, rowspan, colspan });
|
|
64
|
+
for (let dr = 0; dr < rowspan; dr++) {
|
|
65
|
+
for (let dc = 0; dc < colspan; dc++)
|
|
66
|
+
taken[r + dr][c + dc] = true;
|
|
67
|
+
}
|
|
68
|
+
c += colspan;
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
// Second pass: keep only the columns a cell starts in, and count each span in those.
|
|
72
|
+
const width = cells.reduce((max, cell) => Math.max(max, cell.col + cell.colspan), 0);
|
|
73
|
+
const starts = new Array(width).fill(false);
|
|
74
|
+
for (const cell of cells)
|
|
75
|
+
starts[cell.col] = true;
|
|
76
|
+
const index = [];
|
|
77
|
+
const source = [];
|
|
78
|
+
let kept = 0;
|
|
79
|
+
for (let c = 0; c <= width; c++) {
|
|
80
|
+
index[c] = kept;
|
|
81
|
+
if (starts[c]) {
|
|
82
|
+
source.push(c);
|
|
83
|
+
kept++;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
for (const cell of cells) {
|
|
87
|
+
const end = index[cell.col + cell.colspan];
|
|
88
|
+
cell.col = index[cell.col];
|
|
89
|
+
cell.colspan = Math.max(1, end - cell.col);
|
|
90
|
+
}
|
|
91
|
+
const { rows, columns } = place();
|
|
92
|
+
// Every row as wide as the table, so a short row has explicit empty slots.
|
|
93
|
+
for (const row of rows) {
|
|
94
|
+
for (let c = 0; c < columns; c++)
|
|
95
|
+
row[c] = row[c] ?? null;
|
|
96
|
+
}
|
|
97
|
+
return { columns, rows, cells, source };
|
|
98
|
+
}
|
|
99
|
+
const TWIPS_PER_UNIT = { px: 15, pt: 20, in: 1440, cm: 1440 / 2.54, mm: 144 / 2.54 };
|
|
100
|
+
/** The width an element asks for (style or the width attribute, where a bare number is pixels), or null. */
|
|
101
|
+
function widthOf(el) {
|
|
102
|
+
if (!el)
|
|
103
|
+
return null;
|
|
104
|
+
const value = (el.style?.width || el.getAttribute('width') || '').trim().toLowerCase();
|
|
105
|
+
const m = value.match(/^(\d+(?:\.\d+)?|\.\d+)(%|px|pt|in|cm|mm)?$/);
|
|
106
|
+
if (!m)
|
|
107
|
+
return null;
|
|
108
|
+
const n = parseFloat(m[1]);
|
|
109
|
+
if (!(n > 0))
|
|
110
|
+
return null;
|
|
111
|
+
return m[2] === '%' ? { percent: n } : { twips: n * TWIPS_PER_UNIT[m[2] ?? 'px'] };
|
|
112
|
+
}
|
|
113
|
+
/** The width each column of the markup is given by <col> and <colgroup>, where it is given one. */
|
|
114
|
+
function colWidths(table) {
|
|
115
|
+
const widths = [];
|
|
116
|
+
const add = (el, fallback) => {
|
|
117
|
+
const span = Math.min(MAX_COLSPAN, Math.max(1, parseInt(el.getAttribute('span') ?? '', 10) || 1));
|
|
118
|
+
const width = widthOf(el) ?? fallback;
|
|
119
|
+
for (let k = 0; k < span && widths.length < MAX_COLSPAN; k++)
|
|
120
|
+
widths.push(width);
|
|
121
|
+
};
|
|
122
|
+
for (const child of Array.from(table.children)) {
|
|
123
|
+
const tag = child.tagName.toLowerCase();
|
|
124
|
+
if (tag === 'col')
|
|
125
|
+
add(child, null);
|
|
126
|
+
if (tag !== 'colgroup')
|
|
127
|
+
continue;
|
|
128
|
+
const cols = Array.from(child.children).filter((c) => c.tagName.toLowerCase() === 'col');
|
|
129
|
+
if (cols.length === 0)
|
|
130
|
+
add(child, null);
|
|
131
|
+
for (const col of cols)
|
|
132
|
+
add(col, widthOf(child));
|
|
133
|
+
}
|
|
134
|
+
return widths;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* The share of the table's width each column takes, adding up to 1. A column is as wide as a
|
|
138
|
+
* cell of its own says, or else its <col> (width: 25%); a cell across several columns gives what
|
|
139
|
+
* it has left to those of them with no width; the rest share what remains equally.
|
|
140
|
+
*
|
|
141
|
+
* Widths given as lengths (120px, 90pt, the bare width="120" of a pasted table) count too: as a
|
|
142
|
+
* share of the table where the table's own width is a length, and otherwise, where no width is a
|
|
143
|
+
* percentage, in proportion to each other. Lengths beside percentages with no table width to
|
|
144
|
+
* measure them against cannot be compared, and are left out.
|
|
145
|
+
*/
|
|
146
|
+
export function columnShares(model, table) {
|
|
147
|
+
const cols = table ? colWidths(table) : [];
|
|
148
|
+
const asked = Array.from({ length: model.columns }, (_, c) => cols[model.source[c]] ?? null);
|
|
149
|
+
const own = new Array(model.columns).fill(false);
|
|
150
|
+
for (const cell of model.cells) {
|
|
151
|
+
if (cell.colspan !== 1 || own[cell.col])
|
|
152
|
+
continue;
|
|
153
|
+
const w = widthOf(cell.el);
|
|
154
|
+
if (!w)
|
|
155
|
+
continue;
|
|
156
|
+
asked[cell.col] = w;
|
|
157
|
+
own[cell.col] = true;
|
|
158
|
+
}
|
|
159
|
+
const tableWidth = widthOf(table);
|
|
160
|
+
const tableTwips = tableWidth && 'twips' in tableWidth ? tableWidth.twips : 0;
|
|
161
|
+
const spanning = model.cells.filter((cell) => cell.colspan !== 1).map((cell) => ({ cell, width: widthOf(cell.el) }));
|
|
162
|
+
const anyPercent = asked.some((w) => w && 'percent' in w) || spanning.some((s) => s.width && 'percent' in s.width);
|
|
163
|
+
const lengths = asked.filter((w) => w != null && 'twips' in w);
|
|
164
|
+
if (!tableTwips && !anyPercent && lengths.length > 0) {
|
|
165
|
+
// Lengths only: the columns in proportion, one with no width as wide as the average.
|
|
166
|
+
const average = lengths.reduce((sum, w) => sum + w.twips, 0) / lengths.length;
|
|
167
|
+
const twips = asked.map((w) => (w && 'twips' in w ? w.twips : average));
|
|
168
|
+
const all = twips.reduce((a, b) => a + b, 0) || 1;
|
|
169
|
+
return twips.map((t) => t / all);
|
|
170
|
+
}
|
|
171
|
+
/** A width in percent of the table, or null where it cannot be told. */
|
|
172
|
+
const percent = (w) => {
|
|
173
|
+
if (!w)
|
|
174
|
+
return null;
|
|
175
|
+
if ('percent' in w)
|
|
176
|
+
return w.percent;
|
|
177
|
+
return tableTwips ? (w.twips / tableTwips) * 100 : null;
|
|
178
|
+
};
|
|
179
|
+
const width = asked.map(percent);
|
|
180
|
+
for (const { cell, width: w } of spanning) {
|
|
181
|
+
const pct = percent(w);
|
|
182
|
+
if (pct == null)
|
|
183
|
+
continue;
|
|
184
|
+
const cols = Array.from({ length: cell.colspan }, (_, k) => cell.col + k).filter((c) => c < model.columns);
|
|
185
|
+
const open = cols.filter((c) => width[c] == null);
|
|
186
|
+
const taken = cols.reduce((sum, c) => sum + (width[c] ?? 0), 0);
|
|
187
|
+
if (open.length === 0 || pct <= taken)
|
|
188
|
+
continue;
|
|
189
|
+
for (const c of open)
|
|
190
|
+
width[c] = (pct - taken) / open.length;
|
|
191
|
+
}
|
|
192
|
+
const known = width.filter((w) => w != null);
|
|
193
|
+
const sum = known.reduce((a, b) => a + b, 0);
|
|
194
|
+
const open = width.length - known.length;
|
|
195
|
+
if (open > 0) {
|
|
196
|
+
// What is left of 100%; where the given widths already fill it, the average of those.
|
|
197
|
+
const share = sum < 100 ? (100 - sum) / open : known.length > 0 ? sum / known.length : 100 / open;
|
|
198
|
+
for (let c = 0; c < width.length; c++)
|
|
199
|
+
width[c] = width[c] ?? share;
|
|
200
|
+
}
|
|
201
|
+
const total = width.reduce((a, b) => a + b, 0) || 1;
|
|
202
|
+
return width.map((w) => w / total);
|
|
203
|
+
}
|