svelte-rtf-editor 1.10.0 → 1.12.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 +38 -5
- package/dist/components/InkEditor.svelte +797 -85
- package/dist/components/InkEditor.svelte.d.ts +5 -0
- package/dist/components/Toolbar.svelte +4 -4
- package/dist/index.d.ts +1 -0
- package/dist/rtf-writer.js +36 -21
- package/dist/sanitize.d.ts +43 -6
- package/dist/sanitize.js +192 -59
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ RTF viewer and rich-text editor components for **Svelte 5**.
|
|
|
16
16
|
- Images: upload, paste or drag-and-drop, with a description below each picture, drag-to-resize and left/centre/right placement — embedded in the RTF, not linked
|
|
17
17
|
- Import an RTF file from disk; export as RTF, HTML, or Markdown
|
|
18
18
|
- Optional auto-save to `localStorage` (off unless asked for)
|
|
19
|
-
- HTML from outside (the `content` prop, `setHTML`, a restored draft, a paste) is made safe before it is shown. See [HTML from outside](#html-from-outside)
|
|
19
|
+
- HTML from outside (the `content` prop, `setHTML`, a restored draft, a paste, a drop) is made safe before it is shown. See [HTML from outside](#html-from-outside)
|
|
20
20
|
- Word count and character count status bar
|
|
21
21
|
- Keyboard shortcuts (`Ctrl+S` to save, `Ctrl+B/I/U` for formatting)
|
|
22
22
|
- **Utility functions** — `rtfToHtml`, `htmlToRtf`, `readRtfFile`, `htmlToMarkdown`, `downloadFile`
|
|
@@ -137,13 +137,15 @@ Pass an HTML string as `content`. If the content is already RTF, convert it firs
|
|
|
137
137
|
| `fontFamily` | `string` | `'Segoe UI'` | Document font, on screen and in the RTF. See [Fonts](#fonts) |
|
|
138
138
|
| `fontSize` | `number` | `12` | Body text size in points, on screen and in the RTF |
|
|
139
139
|
| `fontSizes` | `number[]` | `[8, 9, 10, 11, 12, 14, 16, 18, 20, 24, 28, 36]` | The sizes, in points, the toolbar's size list offers for selected text. The body size is always among them; `[]` takes the list off the toolbar. See [Size of selected text](#size-of-selected-text) |
|
|
140
|
+
| `keepAttributes` | `string[]` | `[]` | `data-` attributes of your own that stay on HTML you set or the user pastes. See [HTML from outside](#html-from-outside) |
|
|
141
|
+
| `keepClasses` | `string[]` | `[]` | Class names of your own that stay the same way |
|
|
140
142
|
|
|
141
143
|
#### Callback props
|
|
142
144
|
|
|
143
145
|
| Prop | Payload | Description |
|
|
144
146
|
|------------|----------------------------------------------|---------------------------------------------|
|
|
145
147
|
| `onchange` | `{ html, text, wordCount, charCount, estimatedRtfBytes }` | Fired on every content change. `html`, `text` and `estimatedRtfBytes` are worked out when the handler reads them, so a handler that only wants the counts costs nothing on a long document. They are the document as it is when first read: read them in the handler. A payload kept and read after a later edit gives the document as it is by then |
|
|
146
|
-
| `onsave` | `{ html }` | Fired on auto-save
|
|
148
|
+
| `onsave` | `{ html }` | Fired on `Ctrl+S`, and after each auto-save where `autosave` is on |
|
|
147
149
|
| `onimport` | `{ html }` | Fired after a successful RTF file import |
|
|
148
150
|
|
|
149
151
|
#### Methods (via `bind:this`)
|
|
@@ -151,7 +153,7 @@ Pass an HTML string as `content`. If the content is already RTF, convert it firs
|
|
|
151
153
|
```ts
|
|
152
154
|
editor.getHTML() // → string — current editor HTML
|
|
153
155
|
editor.getText() // → string — plain text (no tags)
|
|
154
|
-
editor.setHTML(html) // → void — replace content programmatically (made safe first)
|
|
156
|
+
editor.setHTML(html) // → void — replace content programmatically (made safe first; throws, leaving the editor empty, if it cannot be)
|
|
155
157
|
editor.getMarkdown() // → string — Markdown conversion of content
|
|
156
158
|
editor.getRTF() // → string — RTF conversion of content
|
|
157
159
|
editor.getRtfSize() // → number — exact byte size of that RTF
|
|
@@ -160,6 +162,7 @@ editor.focus() // → void — focus the editor
|
|
|
160
162
|
editor.exportFile(format) // → void — download as 'html' | 'md' | 'rtf'
|
|
161
163
|
editor.importRtf() // → void — open the file picker to import .rtf
|
|
162
164
|
editor.setFontSize(14) // → void — size, in points, of the selected text, or of what is typed next
|
|
165
|
+
editor.clearFormatting() // → void — take bold, italic, underline, strike, color, size and font off the selected text
|
|
163
166
|
```
|
|
164
167
|
|
|
165
168
|
#### Auto-save
|
|
@@ -171,7 +174,11 @@ editor.setFontSize(14) // → void — size, in points, of the selected te
|
|
|
171
174
|
|
|
172
175
|
#### HTML from outside
|
|
173
176
|
|
|
174
|
-
Whatever HTML reaches the editor is made safe before it is shown: the `content` prop, `setHTML()`, a draft restored from `localStorage`, and what a paste
|
|
177
|
+
Whatever HTML reaches the editor is made safe before it is shown: the `content` prop, `setHTML()`, a draft restored from `localStorage`, what a paste brings, what is dragged in from another page or from elsewhere in the document, and what Undo and Redo put back (Redo after a paste gives the browser's own copy of the paste, not the one that was made safe). `sanitizeHtml` is exported for HTML you show yourself (stored editor output in a preview, say).
|
|
178
|
+
|
|
179
|
+
A paste and a drop are made safe just after the browser has put them in the document, not before: a frame or a picture in them may be asked for from its address in that moment, though it does not stay.
|
|
180
|
+
|
|
181
|
+
An element that cannot be checked is taken out, and a document that cannot be checked at all is not shown: the editor is left empty and says so, `setHTML()` throws, and the document that was there before does not stay on the screen in its place.
|
|
175
182
|
|
|
176
183
|
| Taken out | Why |
|
|
177
184
|
|---|---|
|
|
@@ -185,6 +192,14 @@ Whatever HTML reaches the editor is made safe before it is shown: the `content`
|
|
|
185
192
|
| Style text the browser did not read as a property (`width: expression(…)`, a property named twice) | It does nothing here, but it would still be in the HTML for whatever shows it next. Where there is any, the attribute is written again from the properties that were read and kept. An attribute that is all read and kept stays exactly as it was written, `border: 1px solid black` included |
|
|
186
193
|
| `class`; an `id` that names something on `document` or `window`; the `id` of a link; in the editor, an `id` the page already uses for an element outside an editor | They would take on the page's own styles, or answer to a name or an `id` the page's scripts look up. A document's own ids are kept, also when it is shown again while its first showing is still in the editor. They are still names on `window`, as every `id` in a page is: a page whose scripts read settings from `window` by a name they do not define should not show documents from strangers |
|
|
187
194
|
|
|
195
|
+
**Marks of your own.** An application that marks elements in its documents and reads the marks back (`<p data-section="2">`), or styles one by a class (`<pre class="summary">`), names them in `keepAttributes` and `keepClasses`, and they stay on HTML it sets and on what the user pastes:
|
|
196
|
+
|
|
197
|
+
```svelte
|
|
198
|
+
<InkEditor content={html} keepAttributes={['data-section', 'data-row']} keepClasses={['summary']} />
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Only attribute names that start with `data-` are kept, and after it only letters, digits, `-`, `_` and `.`; anything else in the list (`onclick`, `style`, `src`) is ignored, so the list cannot let in what the table above takes out. Any class name can be named, `md:flex` and `w-1/2` included. A kept class is kept by name alone: how it looks is your stylesheet's doing, and a paste that happens to use the same name gets it too. A kept attribute comes in with a paste the same way, with whatever value the pasted page gave it, so read a mark back as you would read anything a user typed; and a marked paragraph copied and pasted is two paragraphs with the same mark. Neither changes the RTF. `sanitizeHtml(html, { keepAttributes, keepClasses })` does the same for HTML you show yourself. Without the settings both are taken off, as since 1.10.0: an application that upgraded from an earlier version and relies on one has to name it.
|
|
202
|
+
|
|
188
203
|
The editing area also clips what it holds: nothing in a document is drawn outside it, whatever its styles, and a table wider than the area scrolls inside it. The frame around a selected picture stays inside it too. Text that Word marked hidden (`mso-hide: all`) is hidden on screen as it is left out of the RTF, and in a paste it is not pasted. A closed `<details>` is shown open, since what is in it is written to the RTF either way.
|
|
189
204
|
|
|
190
205
|
`getHTML()` returns what is in the editor, which has been through this. HTML from `rtfToHtml` is safe by construction (see [What `rtfToHtml` may return](#what-rtftohtml-may-return)).
|
|
@@ -208,7 +223,7 @@ import {
|
|
|
208
223
|
| `isSafeLinkUrl(url: string): boolean` | Whether a link address is a web, mail or phone address; `htmlToRtf` and `htmlToMarkdown` write any other link as its text alone, and `rtfToHtml` reads any other as text |
|
|
209
224
|
| `readRtfFile(file: File, options?: { fontSize?, maxBytes? }): Promise<string>` | Read a `.rtf` File object and return HTML. Rejects, without reading it, for a file larger than `maxBytes` |
|
|
210
225
|
| `htmlToMarkdown(el: HTMLElement): string` | Convert a DOM element's content to Markdown. See [Markdown export](#markdown-export) |
|
|
211
|
-
| `sanitizeHtml(html: string): string` | HTML made safe to show and edit. See [HTML from outside](#html-from-outside). Needs a DOM (`DOMParser`); without one it returns `''` |
|
|
226
|
+
| `sanitizeHtml(html: string, options?: { keepAttributes?: string[]; keepClasses?: string[] }): string` | HTML made safe to show and edit. See [HTML from outside](#html-from-outside). Needs a DOM (`DOMParser`); without one it returns `''` |
|
|
212
227
|
| `downloadFile(name, content, mime): void` | Trigger a file download in the browser |
|
|
213
228
|
|
|
214
229
|
---
|
|
@@ -257,6 +272,24 @@ The toolbar has a list of sizes, next to the list of paragraph kinds. A size cho
|
|
|
257
272
|
|
|
258
273
|
The font has to be installed on the machine that displays the document. The RTF names it, and a reader without it substitutes one of its own.
|
|
259
274
|
|
|
275
|
+
### Clear formatting
|
|
276
|
+
|
|
277
|
+
The toolbar's Clear formatting button, next to the text color, takes the formatting off the selected text. `editor.clearFormatting()` does the same from your own toolbar. With nothing selected it changes nothing in the document.
|
|
278
|
+
|
|
279
|
+
| Taken off | Stays |
|
|
280
|
+
| --- | --- |
|
|
281
|
+
| Bold, italic, underline, strikethrough | Headings, lists, block quotes, code blocks, tables |
|
|
282
|
+
| Text color and highlight | Links |
|
|
283
|
+
| Size and font | Alignment, indents, hanging indents, tabs |
|
|
284
|
+
| Superscript, subscript, inline code | Pictures, their size and captions |
|
|
285
|
+
| The same written as a style on a paragraph, a table cell or a pasted `<span>` | A table cell's shading |
|
|
286
|
+
| | Elements carrying your `keepAttributes` or `keepClasses` |
|
|
287
|
+
|
|
288
|
+
- Only the selected words change: clearing two words of a bold sentence leaves the rest bold.
|
|
289
|
+
- The result is the same HTML in Chromium, Firefox and Safari. The browsers' own command is not: one takes a paragraph's alignment with its color, another leaves the color on the paragraph, a third takes out the element your mark is on.
|
|
290
|
+
- Undo puts the formatting back and Redo takes it off again, from the keyboard and from the toolbar, also after more has been typed and undone. In Safari one Undo may take back the step before it as well.
|
|
291
|
+
- A mark stays whole in all three browsers, whatever part of it is selected. In Firefox the words inside it keep the bold or italic they have there: its command cannot clear them without cutting the mark in two.
|
|
292
|
+
|
|
260
293
|
---
|
|
261
294
|
|
|
262
295
|
## Paragraph formatting
|