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.
- package/README.md +116 -13
- package/dist/RtfViewer.svelte +32 -1
- package/dist/components/FontMenu.svelte +187 -0
- package/dist/components/FontMenu.svelte.d.ts +20 -0
- package/dist/components/ImageOverlay.svelte +153 -17
- package/dist/components/ImageOverlay.svelte.d.ts +23 -2
- package/dist/components/InkEditor.svelte +1398 -266
- package/dist/components/InkEditor.svelte.d.ts +14 -0
- package/dist/components/Toolbar.svelte +70 -2
- package/dist/components/Toolbar.svelte.d.ts +12 -0
- package/dist/history.d.ts +49 -0
- package/dist/history.js +329 -0
- package/dist/list-markers.d.ts +26 -0
- package/dist/list-markers.js +82 -0
- package/dist/lists.d.ts +48 -0
- package/dist/lists.js +576 -0
- package/dist/picture-rows.d.ts +49 -0
- package/dist/picture-rows.js +109 -0
- package/dist/rtf-parser.js +290 -38
- package/dist/rtf-writer.d.ts +10 -0
- package/dist/rtf-writer.js +255 -29
- package/dist/sanitize.js +85 -2
- package/dist/utils.js +41 -3
- package/package.json +1 -1
package/dist/list-markers.js
CHANGED
|
@@ -127,3 +127,85 @@ export function letterReadings(letters) {
|
|
|
127
127
|
}
|
|
128
128
|
return readings;
|
|
129
129
|
}
|
|
130
|
+
// ── The gap after the marker ──
|
|
131
|
+
// How far an item's text starts from its bullet or number is the list's text indent, as Word
|
|
132
|
+
// has it: the marker starts where the list does, and the text that far further in. In the HTML
|
|
133
|
+
// it is the list's own `padding-left`, which is that distance in CSS too: from the list's edge
|
|
134
|
+
// to its items' text. The editor draws the marker at the list's edge (see markItems), and the
|
|
135
|
+
// RTF has it as the hang of each item (\fi-N). A list with no padding of its own has the gap
|
|
136
|
+
// the writer always gave one: a quarter inch, \fi-360.
|
|
137
|
+
/** The least gap there is, in twips: 6 pt. Closer, a number runs into its text. */
|
|
138
|
+
export const LEAST_MARKER_GAP = 120;
|
|
139
|
+
/** The most, in twips: two inches. */
|
|
140
|
+
export const MOST_MARKER_GAP = 2880;
|
|
141
|
+
const TWIPS_PER = { px: 15, pt: 20, in: 1440, cm: 1440 / 2.54, mm: 144 / 2.54 };
|
|
142
|
+
/**
|
|
143
|
+
* The gap a list sets after its markers, in twips, or null where it sets none: no
|
|
144
|
+
* `padding-left` of its own, or one out of the range above (a page's `padding-left: 0`).
|
|
145
|
+
*/
|
|
146
|
+
export function markerGapTwips(list) {
|
|
147
|
+
const value = (list.style?.paddingLeft ?? '').trim().toLowerCase();
|
|
148
|
+
const m = value.match(/^(\d+(?:\.\d+)?|\.\d+)(px|pt|in|cm|mm)$/);
|
|
149
|
+
if (!m)
|
|
150
|
+
return null;
|
|
151
|
+
const twips = Math.round(parseFloat(m[1]) * TWIPS_PER[m[2]]);
|
|
152
|
+
return twips >= LEAST_MARKER_GAP && twips <= MOST_MARKER_GAP ? twips : null;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Where the editor writes, for the screen, the marker an item of a list with a gap shows. A
|
|
156
|
+
* browser draws its own markers against the item's text, and cannot be told how far before
|
|
157
|
+
* it; so for such a list the editor draws them itself, at the list's edge (see the editor's
|
|
158
|
+
* style), from this. It is worked out here, as the RTF writer works it out: a browser's own
|
|
159
|
+
* count does not see an item's `value` in every browser. It is no part of the document, and
|
|
160
|
+
* the editor takes it out of what it gives (getHTML, onsave, the draft).
|
|
161
|
+
*/
|
|
162
|
+
export const MARKER_ATTRIBUTE = 'data-ink-marker';
|
|
163
|
+
/** The bullets of a list in a list, as the browser draws them: disc, circle, square. */
|
|
164
|
+
const BULLETS = { disc: '•', circle: '◦', square: '▪' };
|
|
165
|
+
/**
|
|
166
|
+
* Write the marker of each item of each list with a gap under `root`, and take it off the
|
|
167
|
+
* items of every other list. Only what differs is written, so that a document that has not
|
|
168
|
+
* changed is not changed by this either.
|
|
169
|
+
*/
|
|
170
|
+
export function markItems(root) {
|
|
171
|
+
for (const list of Array.from(root.querySelectorAll('ul, ol'))) {
|
|
172
|
+
const gapped = markerGapTwips(list) !== null;
|
|
173
|
+
const ordered = list.tagName === 'OL';
|
|
174
|
+
let counter = ordered ? listStart(list) : 1;
|
|
175
|
+
let bullet = '';
|
|
176
|
+
if (gapped && !ordered) {
|
|
177
|
+
const own = (list.style?.listStyleType ?? '').trim().toLowerCase();
|
|
178
|
+
let depth = 0;
|
|
179
|
+
for (let up = list.parentElement?.closest('ul, ol'); up && root.contains(up); up = up.parentElement?.closest('ul, ol'))
|
|
180
|
+
depth++;
|
|
181
|
+
bullet = BULLETS[own] ?? (depth === 0 ? BULLETS.disc : depth === 1 ? BULLETS.circle : BULLETS.square);
|
|
182
|
+
}
|
|
183
|
+
for (const item of Array.from(list.children)) {
|
|
184
|
+
if (item.tagName !== 'LI')
|
|
185
|
+
continue;
|
|
186
|
+
let marker = null;
|
|
187
|
+
if (gapped) {
|
|
188
|
+
if (ordered) {
|
|
189
|
+
counter = itemNumber(item, counter);
|
|
190
|
+
marker = `${markerText(counter, numberStyleOf(list, item))}.`;
|
|
191
|
+
counter++;
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
marker = bullet;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (marker === null) {
|
|
198
|
+
if (item.hasAttribute(MARKER_ATTRIBUTE))
|
|
199
|
+
item.removeAttribute(MARKER_ATTRIBUTE);
|
|
200
|
+
}
|
|
201
|
+
else if (item.getAttribute(MARKER_ATTRIBUTE) !== marker) {
|
|
202
|
+
item.setAttribute(MARKER_ATTRIBUTE, marker);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
/** Take the editor's markers out of a copy of a document. */
|
|
208
|
+
export function withoutMarkers(root) {
|
|
209
|
+
for (const item of Array.from(root.querySelectorAll(`[${MARKER_ATTRIBUTE}]`)))
|
|
210
|
+
item.removeAttribute(MARKER_ATTRIBUTE);
|
|
211
|
+
}
|
package/dist/lists.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lists, as the editor makes them.
|
|
3
|
+
*
|
|
4
|
+
* The browsers' own list commands each make something else of the same click. Chromium and
|
|
5
|
+
* Safari leave the list inside the paragraph it was made from, which no HTML can say;
|
|
6
|
+
* Firefox starts a new list beside the one a line is next to, so that every line made an item
|
|
7
|
+
* one at a time is numbered 1; Chromium puts the caret at the start of the line. None of
|
|
8
|
+
* them nests an item under the one before it. So the document is changed here, the same way
|
|
9
|
+
* in every browser, and the editor's own history (history.ts) undoes it.
|
|
10
|
+
*
|
|
11
|
+
* The shape made is the one the RTF reader writes and the writer reads: a <ul> or <ol> of
|
|
12
|
+
* <li>, and a list inside a list as the last thing in the item it is under.
|
|
13
|
+
*
|
|
14
|
+
* <ol><li>Skin, left arm<ul><li>margins clear</li></ul></li><li>Lymph node</li></ol>
|
|
15
|
+
*
|
|
16
|
+
* Every function takes the editor's element and the selection, changes the document where
|
|
17
|
+
* there is something to change, puts the selection back in the words it was in, and returns
|
|
18
|
+
* whether it changed anything.
|
|
19
|
+
*/
|
|
20
|
+
export type ListKind = 'ul' | 'ol';
|
|
21
|
+
/**
|
|
22
|
+
* The toolbar's list button. The lines the selection touches are made items of a list of
|
|
23
|
+
* this kind, and one list with a list of that kind they are next to. Where they are all
|
|
24
|
+
* items of such a list already, they are paragraphs again. Where they are items of the
|
|
25
|
+
* other kind: with a caret, the list the caret is in is made this kind; with words
|
|
26
|
+
* selected, the items selected are.
|
|
27
|
+
*/
|
|
28
|
+
export declare function toggleList(root: HTMLElement, sel: Selection | null, kind: ListKind): boolean;
|
|
29
|
+
/** Which kind of list the selection is in: the innermost list of the line it begins in. */
|
|
30
|
+
export declare function listKindAt(root: HTMLElement, sel: Selection | null): ListKind | null;
|
|
31
|
+
/**
|
|
32
|
+
* Put the items the selection touches one list further in, each under the item before it.
|
|
33
|
+
* Returns false, with nothing changed, where the selection is not all items, or the first of
|
|
34
|
+
* them has no item before it to be under.
|
|
35
|
+
*/
|
|
36
|
+
export declare function nestItems(root: HTMLElement, sel: Selection | null): boolean;
|
|
37
|
+
/** Put the items the selection touches that are in a list within a list one list further out. Returns false where none is. */
|
|
38
|
+
export declare function unnestItems(root: HTMLElement, sel: Selection | null): boolean;
|
|
39
|
+
/**
|
|
40
|
+
* The item the caret is in leaves its list by one step: one list further out, or, from a
|
|
41
|
+
* list that is in no other, a paragraph. (What Enter does in an item with nothing in it, and
|
|
42
|
+
* Backspace at the start of one.)
|
|
43
|
+
*/
|
|
44
|
+
export declare function leaveList(root: HTMLElement, sel: Selection | null): boolean;
|
|
45
|
+
/** Whether the caret is in an item with nothing in it: no words, no picture. (One with a list under it has that.) */
|
|
46
|
+
export declare function inEmptyItem(root: HTMLElement, sel: Selection | null): boolean;
|
|
47
|
+
/** Whether the caret is at the very start of an item: nothing of the item before it. */
|
|
48
|
+
export declare function atItemStart(root: HTMLElement, sel: Selection | null): boolean;
|