svelte-rtf-editor 1.9.0 → 1.11.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 +135 -28
- package/dist/RtfViewer.svelte +7 -1
- package/dist/components/ImageOverlay.svelte +11 -3
- package/dist/components/ImageOverlay.svelte.d.ts +2 -0
- package/dist/components/InkEditor.svelte +683 -54
- package/dist/components/InkEditor.svelte.d.ts +16 -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 +51 -5
- package/dist/images.js +242 -53
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/rtf-parser.js +666 -159
- package/dist/rtf-writer.d.ts +14 -6
- package/dist/rtf-writer.js +638 -183
- package/dist/sanitize.d.ts +99 -0
- package/dist/sanitize.js +789 -0
- package/dist/table-model.d.ts +10 -3
- package/dist/table-model.js +81 -17
- package/dist/utils.js +628 -169
- package/package.json +16 -12
package/dist/table-model.d.ts
CHANGED
|
@@ -16,6 +16,8 @@ export interface TableModel {
|
|
|
16
16
|
/** For each row, the cell that covers each column, or null where no cell does. */
|
|
17
17
|
rows: (GridCell | null)[][];
|
|
18
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[];
|
|
19
21
|
}
|
|
20
22
|
/**
|
|
21
23
|
* A table's rows in the order a browser shows them: the head first and the foot last, wherever
|
|
@@ -36,7 +38,12 @@ export declare function rowCells(row: Element): HTMLElement[];
|
|
|
36
38
|
export declare function tableModel(table: Element): TableModel;
|
|
37
39
|
/**
|
|
38
40
|
* The share of the table's width each column takes, adding up to 1. A column is as wide as a
|
|
39
|
-
* cell of its own says (width: 25%); a cell across several columns gives what
|
|
40
|
-
* those of them with no width; the rest share what remains equally.
|
|
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.
|
|
41
48
|
*/
|
|
42
|
-
export declare function columnShares(model: TableModel): number[];
|
|
49
|
+
export declare function columnShares(model: TableModel, table?: Element): number[];
|
package/dist/table-model.js
CHANGED
|
@@ -74,11 +74,14 @@ export function tableModel(table) {
|
|
|
74
74
|
for (const cell of cells)
|
|
75
75
|
starts[cell.col] = true;
|
|
76
76
|
const index = [];
|
|
77
|
+
const source = [];
|
|
77
78
|
let kept = 0;
|
|
78
79
|
for (let c = 0; c <= width; c++) {
|
|
79
80
|
index[c] = kept;
|
|
80
|
-
if (starts[c])
|
|
81
|
+
if (starts[c]) {
|
|
82
|
+
source.push(c);
|
|
81
83
|
kept++;
|
|
84
|
+
}
|
|
82
85
|
}
|
|
83
86
|
for (const cell of cells) {
|
|
84
87
|
const end = index[cell.col + cell.colspan];
|
|
@@ -91,30 +94,91 @@ export function tableModel(table) {
|
|
|
91
94
|
for (let c = 0; c < columns; c++)
|
|
92
95
|
row[c] = row[c] ?? null;
|
|
93
96
|
}
|
|
94
|
-
return { columns, rows, cells };
|
|
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'] };
|
|
95
112
|
}
|
|
96
|
-
/** The
|
|
97
|
-
function
|
|
98
|
-
const
|
|
99
|
-
const
|
|
100
|
-
|
|
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;
|
|
101
135
|
}
|
|
102
136
|
/**
|
|
103
137
|
* The share of the table's width each column takes, adding up to 1. A column is as wide as a
|
|
104
|
-
* cell of its own says (width: 25%); a cell across several columns gives what
|
|
105
|
-
* those of them with no width; the rest share what remains equally.
|
|
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.
|
|
106
145
|
*/
|
|
107
|
-
export function columnShares(model) {
|
|
108
|
-
const
|
|
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);
|
|
109
150
|
for (const cell of model.cells) {
|
|
110
|
-
if (cell.colspan !== 1 ||
|
|
151
|
+
if (cell.colspan !== 1 || own[cell.col])
|
|
111
152
|
continue;
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
for (const cell of model.cells) {
|
|
115
|
-
if (cell.colspan === 1)
|
|
153
|
+
const w = widthOf(cell.el);
|
|
154
|
+
if (!w)
|
|
116
155
|
continue;
|
|
117
|
-
|
|
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);
|
|
118
182
|
if (pct == null)
|
|
119
183
|
continue;
|
|
120
184
|
const cols = Array.from({ length: cell.colspan }, (_, k) => cell.col + k).filter((c) => c < model.columns);
|