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 CHANGED
@@ -15,7 +15,8 @@ RTF viewer and rich-text editor components for **Svelte 5**.
15
15
  - Block-level controls: headings (H1–H3), paragraph, blockquote, code block
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
- - Optional auto-save to `localStorage`
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
20
  - Word count and character count status bar
20
21
  - Keyboard shortcuts (`Ctrl+S` to save, `Ctrl+B/I/U` for formatting)
21
22
  - **Utility functions** — `rtfToHtml`, `htmlToRtf`, `readRtfFile`, `htmlToMarkdown`, `downloadFile`
@@ -108,8 +109,9 @@ Pass an HTML string as `content`. If the content is already RTF, convert it firs
108
109
  | Prop | Type | Default | Description |
109
110
  |--------------|----------|-----------|------------------------------------------|
110
111
  | `content` | `string` | — | Raw RTF string to parse and display |
111
- | `fontFamily` | `string` | `'Arial'` | Font the document is shown in |
112
+ | `fontFamily` | `string` | `'Segoe UI'` | Font the document is shown in. See [Fonts](#fonts) |
112
113
  | `fontSize` | `number` | `12` | Body text size in points |
114
+ | `maxRtfBytes` | `number` | `52428800` | The largest document it reads (50 MB); `0` reads any size. See [Size limit on import](#size-limit-on-import) |
113
115
 
114
116
  ---
115
117
 
@@ -119,25 +121,28 @@ Pass an HTML string as `content`. If the content is already RTF, convert it firs
119
121
 
120
122
  | Prop | Type | Default | Description |
121
123
  |-----------------|------------|------------------------------|-----------------------------------------------------------|
122
- | `content` | `string` | `'<p></p>'` | Initial HTML content |
124
+ | `content` | `string` | `'<p><br></p>'` | Initial HTML content. Made safe before it is shown: see [HTML from outside](#html-from-outside) |
123
125
  | `placeholder` | `string` | `'Start writing something beautiful...'` | Placeholder text when editor is empty |
124
- | `autosave` | `boolean` | `true` | Save content to `localStorage` on change |
125
- | `storageKey` | `string` | `'ink-editor-content'` | `localStorage` key used for auto-save |
126
+ | `autosave` | `boolean` | `false` | Keep a draft in `localStorage`, and show it on mount in place of `content`. See [Auto-save](#auto-save) |
127
+ | `storageKey` | `string` | `'ink-editor-content'` | `localStorage` key the draft is kept under. Give each document its own |
128
+ | `ariaLabel` | `string` | `'Document'` | The name of the editing area for a screen reader |
126
129
  | `showToolbar` | `boolean` | `true` | Show the formatting toolbar |
127
130
  | `showStatusBar` | `boolean` | `true` | Show the word/character count bar |
128
131
  | `minHeight` | `string` | `'40vh'` | CSS `min-height` of the editing area |
129
132
  | `readonly` | `boolean` | `false` | Disable editing |
130
133
  | `maxImageEdge` | `number` | `1600` | Longest edge (px) an inserted image is scaled down to; `0` keeps full size |
131
- | `maxImageBytes` | `number` | `524288` | Encoded byte ceiling per image; oversized pictures are re-encoded until they fit. `0` disables |
134
+ | `maxImageBytes` | `number` | `524288` | Encoded byte ceiling per image; an oversized picture is re-encoded and shrunk until it fits, and is not inserted if it cannot. `0` disables |
135
+ | `maxRtfBytes` | `number` | `52428800` | The largest RTF file Import reads (50 MB); `0` reads any size |
132
136
  | `maxImageDisplayWidth` | `number` | `624` | Width (px) a newly inserted image is displayed at, unless the column is narrower — 6.5in of page. `0` fills the column |
133
- | `fontFamily` | `string` | `'Arial'` | Document font, on screen and in the RTF. See [Fonts](#fonts) |
137
+ | `fontFamily` | `string` | `'Segoe UI'` | Document font, on screen and in the RTF. See [Fonts](#fonts) |
134
138
  | `fontSize` | `number` | `12` | Body text size in points, on screen and in the RTF |
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) |
135
140
 
136
141
  #### Callback props
137
142
 
138
143
  | Prop | Payload | Description |
139
144
  |------------|----------------------------------------------|---------------------------------------------|
140
- | `onchange` | `{ html, text, wordCount, charCount, estimatedRtfBytes }` | Fired on every content change |
145
+ | `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 |
141
146
  | `onsave` | `{ html }` | Fired on auto-save or `Ctrl+S` |
142
147
  | `onimport` | `{ html }` | Fired after a successful RTF file import |
143
148
 
@@ -146,7 +151,7 @@ Pass an HTML string as `content`. If the content is already RTF, convert it firs
146
151
  ```ts
147
152
  editor.getHTML() // → string — current editor HTML
148
153
  editor.getText() // → string — plain text (no tags)
149
- editor.setHTML(html) // → void — replace content programmatically
154
+ editor.setHTML(html) // → void — replace content programmatically (made safe first)
150
155
  editor.getMarkdown() // → string — Markdown conversion of content
151
156
  editor.getRTF() // → string — RTF conversion of content
152
157
  editor.getRtfSize() // → number — exact byte size of that RTF
@@ -154,49 +159,101 @@ editor.clear() // → void — clear the editor
154
159
  editor.focus() // → void — focus the editor
155
160
  editor.exportFile(format) // → void — download as 'html' | 'md' | 'rtf'
156
161
  editor.importRtf() // → void — open the file picker to import .rtf
162
+ editor.setFontSize(14) // → void — size, in points, of the selected text, or of what is typed next
157
163
  ```
158
164
 
165
+ #### Auto-save
166
+
167
+ `autosave` is off by default. Turned on, the editor writes the document's HTML to `localStorage` under `storageKey` a second after each change, and on mount shows a draft it finds there **in place of `content`**. Two things follow, and both matter where the documents are patient reports:
168
+
169
+ - Give every document its own `storageKey` (`storageKey={`report-${id}`}`). With one key for all of them, the draft of the last document edited is shown in place of the next one opened.
170
+ - The draft is plain text in the browser's storage until `clear()` is called. Call it when the document is saved or abandoned, or leave `autosave` off and keep drafts yourself from `onsave`/`onchange`.
171
+
172
+ #### HTML from outside
173
+
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 brings. `sanitizeHtml` is exported for HTML you show yourself (stored editor output in a preview, say).
175
+
176
+ | Taken out | Why |
177
+ |---|---|
178
+ | `<script>`, `<style>`, `<iframe>`, `<object>`, `<embed>`, `<svg>`, `<video>`, `<audio>`, with what is in them | They run code or load a page of their own |
179
+ | `<button>`, `<textarea>`, `<select>`, `<input>` and `<math>`: the element goes, the words it showed stay as text | They take input, or are not something RTF can carry. A formula of numbers and signs in a row stays as those characters (`30%`); one with a fraction, a power or a root is written `[formula]`, since its parts read in a row would say something else. Only words that were shown stay: none of an element that was hidden (`hidden`, `display:none`, `visibility:hidden`, `mso-hide:all`), none of a hidden part of a button, and a formula that KaTeX wrote twice, once as text and once to be read aloud, is kept once. Elements side by side leave their words a space apart (`1 5`, not `15`) |
180
+ | Every attribute but the ones a document uses (`style`, `href`, `src`, `alt`, `colspan`, `start`, …) | That is how `onclick`, `onerror` and the rest go |
181
+ | A link that is not `http(s):`, `mailto:` or `tel:`; a picture address that is not a web address or picture data | `javascript:`, `data:text/html`, `file:` |
182
+ | Style properties that do not format text, a paragraph, a picture or a table (`position`, `z-index`, …), and any value with `url()` | A pasted block with `position: fixed` covered the page the editor sat in |
183
+ | A size that would put an element outside the page: a margin below zero, a margin, padding or width over 2200 px (a little more than the widest page), a width or height on anything but a picture, a table, its cells or a rule, a `width` or `height` attribute over 2200 or 100%, a padding over 32 px on a piece of text inside a line, a border picture, a size in `vw`/`vh` or worked out with `calc()`, a `display` other than the ones a document uses | A block with a negative margin was drawn over the toolbar, and a link with a padding of thousands of pixels was the size of the page. Sizes counted in ems or percent are worked out from the text size the document's styles give, so they do not grow past the limit by being nested. One margin below zero is kept: on a list, up to the half inch a list is indented by itself, which is how a list read from RTF carries an indent smaller than that |
184
+ | A size that would put words out of sight: a first line that starts further left than its paragraph is moved in (`text-indent: -100%`), and a text size under half a pixel | The words were in the RTF and not on the screen. A hanging indent (`margin-left:36pt; text-indent:-36pt`) stays as written |
185
+ | 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
+ | `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
+
188
+ 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
+
190
+ `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)).
191
+
159
192
  ---
160
193
 
161
194
  ## Utility functions
162
195
 
163
196
  ```ts
164
197
  import {
165
- rtfToHtml, htmlToRtf, estimateRtfBytes, readRtfFile, htmlToMarkdown, downloadFile,
166
- DEFAULT_FONT_FAMILY, DEFAULT_FONT_SIZE, isSafeLinkUrl
198
+ rtfToHtml, htmlToRtf, estimateRtfBytes, readRtfFile, htmlToMarkdown, downloadFile, sanitizeHtml,
199
+ DEFAULT_FONT_FAMILY, DEFAULT_FONT_SIZE, DEFAULT_MAX_RTF_BYTES, isSafeLinkUrl
167
200
  } from 'svelte-rtf-editor';
168
201
  ```
169
202
 
170
203
  | Function | Description |
171
204
  |---------------------------------------|----------------------------------------------------|
172
- | `rtfToHtml(rtf: string, options?: { fontSize? }): string` | Parse an RTF string and return an HTML string. `fontSize` is the size of the screen it will be shown on (default 12) |
173
- | `htmlToRtf(el: HTMLElement, options?: { fontFamily?, fontSize? }): string` | Convert a DOM element's content to RTF, in Arial 12 pt unless told otherwise |
174
- | `estimateRtfBytes(el: HTMLElement): number` | Approximate RTF size of a DOM element without converting it |
175
- | `isSafeLinkUrl(url: string): boolean` | Whether a link address is a web, mail or phone address; `htmlToRtf` writes any other link as its text alone |
176
- | `readRtfFile(file: File): Promise<string>` | Read a `.rtf` File object and return HTML |
177
- | `htmlToMarkdown(el: HTMLElement): string` | Convert a DOM element's content to Markdown |
205
+ | `rtfToHtml(rtf: string, options?: { fontSize?, maxBytes? }): string` | Parse an RTF string and return an HTML string. `fontSize` is the size of the screen it will be shown on (default 12). Throws a `RangeError` for a string longer than `maxBytes` (default 50 MB; see [Size limit on import](#size-limit-on-import)) |
206
+ | `htmlToRtf(el: HTMLElement, options?: { fontFamily?, fontSize? }): string` | Convert a DOM element's content to RTF, in Segoe UI 12 pt unless told otherwise |
207
+ | `estimateRtfBytes(el: HTMLElement, options?: { fontFamily?, fontSize? }): number` | Approximate RTF size of a DOM element without converting it. Give it the options `htmlToRtf` is called with; it is never below the real size |
208
+ | `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
+ | `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
+ | `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 `''` |
178
212
  | `downloadFile(name, content, mime): void` | Trigger a file download in the browser |
179
213
 
180
214
  ---
181
215
 
182
216
  ## Fonts
183
217
 
184
- The editor and the viewer show a document in the same font and size the RTF carries, so what you see on screen is what the receiving system gets. Both default to **Arial 12 pt**.
218
+ The editor and the viewer show a document in the same font and size the RTF carries, so what you see on screen is what the receiving system gets. Both default to **Segoe UI 12 pt** (`DEFAULT_FONT_FAMILY` and `DEFAULT_FONT_SIZE`, which the package exports).
219
+
220
+ To use another font, pass it to each of the three places a document goes through. Nothing else has to change:
185
221
 
186
222
  ```svelte
187
223
  <InkEditor fontFamily="Calibri" fontSize={11} bind:this={editor} />
188
224
  ```
189
225
 
226
+ ```svelte
227
+ <RtfViewer content={rtf} fontFamily="Calibri" fontSize={11} />
228
+ ```
229
+
190
230
  ```ts
191
231
  htmlToRtf(el, { fontFamily: 'Calibri', fontSize: 11 });
192
232
  ```
193
233
 
234
+ An application that shows documents in more than one place does well to keep the two values in one module of its own and pass them from there, so that a change of font is one line.
235
+
236
+ The default was Arial through 1.9.0. A document is written in the font of the editor that saves it, not in the font it was written in before: a report saved in Arial and opened and saved again by an editor left at the default comes out in Segoe UI. To keep Arial, pass `fontFamily="Arial"`.
237
+
238
+ Segoe UI is installed with Windows. On a system without it (macOS, Linux, a phone) the screen shows the system's sans-serif font in its place, and the RTF still names Segoe UI, which is what the receiving system reads.
239
+
194
240
  - `fontFamily` is used for the whole document: body text, headings, lists and tables. Code keeps Courier New.
195
241
  - `fontSize` is the body size in points. Headings scale from it: at 12 pt, `h1` is 24 pt, `h2` 18 pt and `h3` 14 pt.
196
242
  - If you call `htmlToRtf` yourself on the editor's content, pass the same options you gave the editor, or the two produce different RTF.
197
243
  - `fontFamily` names one font. Given a CSS list such as `'Arial, sans-serif'`, only the first family is used.
198
244
  - `fontSize` is rounded to the nearest half-point, which is what RTF measures in, and kept between 1 and 1638 pt.
199
- - If you show RTF in an editor or viewer set to another size, pass the same `fontSize` to `rtfToHtml`. Text at that size then carries no size of its own, text at any other size keeps it, and headings are recognized by the same ratios the writer uses. `InkEditor`'s RTF import and `RtfViewer` do this themselves. At another size than 12 pt, a heading must also be bold, as every heading the writer emits is.
245
+ - If you show RTF in an editor or viewer set to another size, pass the same `fontSize` to `rtfToHtml`. Text at that size then carries no size of its own, text at any other size keeps it, and headings are recognized by the same ratios the writer uses. `InkEditor`'s RTF import and `RtfViewer` do this themselves. A heading must also be bold, at every body size, as every heading the writer emits is.
246
+
247
+ ### Size of selected text
248
+
249
+ The toolbar has a list of sizes, next to the list of paragraph kinds. A size chosen there is given to the selected text; with nothing selected, to what is typed next. The list shows the size of the text at the caret, also when it is not one of the sizes offered, and is empty where the selection starts in one size and ends in another.
250
+
251
+ - The size is written to the RTF as `\fsN` around that text, and read back as that size by `rtfToHtml`.
252
+ - Text set back to the size the text around it has (the body size in a paragraph, the heading's size in a heading) is given that size as `1em`, and follows `fontSize` again like text that was never sized. The writer reads `1em` as no size.
253
+ - The sizes offered are `fontSizes`; pass your own list, or `[]` to leave the size list out. `editor.setFontSize(points)` does the same from your own toolbar.
254
+ - The editor uses the browser's own font-size command, so a change of size is undone and redone with everything else. In the HTML the size is on a `<font>` element (`<font style="font-size: 14pt">`).
255
+ - A whole paragraph that is bold and at the size a heading has is read back from RTF as that heading: it looks the same, and the list of paragraph kinds then names it a heading.
256
+ - `htmlToRtf` also reads `<font size="1">` to `<font size="7">`, which is how older pages and mail carry sizes, as the sizes a browser shows them at (7.5, 10, 12, 13.5, 18, 24 and 36 pt). They were shown at those sizes and written at the body size.
200
257
 
201
258
  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.
202
259
 
@@ -223,6 +280,104 @@ A `<p>` or `<div>` keeps its own indents, spacing and tab stops in the RTF, so a
223
280
  - A heading outside a table cell also writes its right and first-line indents, so a heading read from RTF with a hanging number goes back as it came. A picture is read with a left margin only, where its line starts.
224
281
  - `rtfToHtml` does not yet read spacing, keep-with-next or tab stops back.
225
282
 
283
+ ---
284
+
285
+ ## What is written, and what is read back
286
+
287
+ `htmlToRtf` writes each construct below as the RTF beside it, and `rtfToHtml` reads that RTF back as the same HTML, so a document saved, reloaded and saved again is the same RTF. Where a reload changes how something is written, it changes once and then stays: nothing drifts from save to save.
288
+
289
+ | HTML | RTF | Read back as |
290
+ |---|---|---|
291
+ | `<h1>`, `<h2>`, `<h3>` | a bold paragraph at 2, 1.5 and 7/6 of the body size | the same heading, with nothing inside it but its text |
292
+ | `<h4>`, `<h5>`, `<h6>` (pasted text) | a bold paragraph at 1, 5/6 and 2/3 of the body size | a bold paragraph |
293
+ | `<ul>`, `<ol>` | one paragraph per item: `\li720\fi-360`, then `\bullet\tab` or `1.\tab` | `<ul>`, `<ol>` |
294
+ | a list inside a list item | the same, `\li720` further in for each level | a list inside the item |
295
+ | `<ol start="5">` | `5.\tab`, `6.\tab`, … | `<ol start="5">` |
296
+ | `<a href="https://…">` | `{\field{\*\fldinst HYPERLINK "…"}{\fldrslt …}}`, underlined, in the link color | `<a href="https://…" target="_blank" rel="noopener noreferrer">` |
297
+ | `<code>` | `\f1` (Courier New) | `<code>` |
298
+ | `<pre>` | a paragraph in `\f1` at 5/6 of the body size, lines separated by `\line` | `<pre>` |
299
+ | `<hr>` | a centered line of twenty em dashes | `<hr>` |
300
+ | `<hr style="break-after:page">` | `\page` | the same |
301
+ | `<blockquote>` | paragraphs indented half an inch, in italics | indented italic paragraphs |
302
+ | `<table>` | one `\trowd … \row` per row; see [Tables](#tables) | `<table>` |
303
+ | `<br>` | `\line` | `<br>` |
304
+
305
+ - **Lists.** Only the shape above is read back as a list: a bullet or `N.`, a tab, and a paragraph that hangs by 360 twips and has no right indent or alignment of its own. Word's lists have that shape and are read as lists, including the bullet it writes in a `{\listtext}` group. Anything else stays the paragraph it is, marker and all, because it could not be written back the same: a line typed as `- Invasive adenocarcinoma.`, a list hung by another distance, one marked `a.` or `1)`, a centered one. A list in a table cell is read as lines of text.
306
+ - **List items.** A paragraph inside an item (`<li><p>…</p></li>`, as Word and Google Docs paste a list) is the item's text; a second paragraph in the item is a line under it. A list written straight inside a list (`<ul><ul>`) is one level deeper. An item with no list around it is written as a bulleted line. The list a browser's own list command makes, which the toolbar's list buttons run, is left inside the paragraph it was made from (`<p><ul>…</ul></p>`); it is written as the list alone.
307
+ - **Headings.** A paragraph is read as a heading when all of its text is at a heading's size and all of it is bold, as every heading `htmlToRtf` writes is. Size alone does not make a heading: a report set in 14 pt throughout is 14 pt paragraphs, with the words that were bold still the only bold ones. (Through 1.9.0, at a 12 pt body size, large text was read as a heading whether bold or not, and was saved bold.) A large word in an ordinary paragraph is a large word. A heading set larger than its level keeps that size.
308
+ - **Links.** A link is read back only for an address `isSafeLinkUrl` accepts, the same test the writer applies: an absolute web, mail or phone address. A field with a `javascript:`, `data:` or `file:` address, a relative or network path, or a link to a bookmark is read as its text alone. A web link opens in a tab of its own, so a click in `RtfViewer` does not replace your application. A document with a link carries the link color as the first entry of its color table (`\red0\green0\blue255`); a document without one is written as before.
309
+ - **Code.** Text in a monospace font other than the document's own font is read as code, and a paragraph set wholly in it at the code block size as a code block. In a document whose own font is monospace, nothing is.
310
+ - **Line breaks.** The `<br>` a browser leaves at the end of a paragraph, a list item or a table cell shows as nothing and is not written. A line break that does end a paragraph in the RTF (`\line\par`) is read as `<br><br>`, which a browser shows as that empty last line.
311
+ - **Page breaks.** `\page` is read as a rule that is shown dashed and written back as `\page`, so a page break in a document that passes through the editor is still one.
312
+
313
+ ### Tables
314
+
315
+ RTF has no table element: each row says where its cells end (`\cellx`), so columns only line up if every row is written against the same edges. `htmlToRtf` works the columns out once for the table and writes every row against them.
316
+
317
+ - **`colspan`**: one cell that ends at the right edge of the last column it covers.
318
+ - **`rowspan`**: `\clvmgf` on the cell where it starts and an empty `\clvmrg` cell in the same columns of each row below. `rowspan="0"` reaches the last row, and no span reaches past the table.
319
+ - **Column widths** come from the cells' own `width`, or else from `<col>` and `<colgroup>`. A cell across several columns gives what it has left to those of them with no width; columns with none share what remains. Widths given as lengths (`120px`, `90pt`, the bare `width="120"` of a pasted table) keep the columns in proportion; beside percentages they count only where the table's own width is a length to measure them against. The table is written 6 in wide.
320
+ - **`<caption>`** is written as a centered paragraph before the table, where a browser shows it, and is read back as that paragraph.
321
+ - **A column no cell starts in is not a column.** `colspan="100"` on a cell meant to cross the whole table adds none.
322
+ - **A row short of cells** is filled with empty ones, so its edges are the table's.
323
+ - **`<th>`** gets a bottom border, on that cell only, which is how `rtfToHtml` knows a header cell: a bare `\clbrdrb\brdrs` and no border on any other side. A bottom border with a width or a color (`\brdrw`, `\brdrcf`), which is how Word and TextEdit write a line someone drew under a cell, is not a header: a signature line stays an ordinary cell.
324
+ - **Each cell ends with `\cell` inside its group**, with no `\par` before it, and the last row of a table carries `\lastrow`. Through 1.9.0 a cell ended `\par}\cell`, which Apple's reader (TextEdit, Pages, Mail) showed as an empty second paragraph in every cell, and two tables with nothing between them were read back as one. `rtfToHtml` reads both shapes.
325
+ - **`<thead>` and `<tfoot>`** are written first and last, wherever the source has them.
326
+ - **A cell's `text-align`** is written on its paragraphs (`\qc`, `\qr`, `\qj`), and read back onto the cell.
327
+ - **A table inside a table cell** is written as lines of text in that cell: each of its rows a paragraph, its cells a tab apart. RTF can nest tables, but many readers show a nested table wrongly or not at all; every word is kept, the inner grid is not. `rtfToHtml` reads a nested table Word wrote the same way.
328
+ - **Reading.** `\clvmgf` and `\clvmrg` become `rowspan`; a cell that crosses the column edges of other rows gets `colspan` (as do `\clmgf` and `\clmrg`). Each paragraph in a cell is a line in it. Widths are read to a hundredth of a percent, so a column edge is written back where it was.
329
+
330
+ ### Reading RTF from other programs
331
+
332
+ - `\'xx` is read as Windows-1252, the code page RTF is written in: `\'92`, the apostrophe TextEdit writes, is `’`, not an invisible control character. Other code pages (`\ansicpg1251`, `\mac`) are still read as Windows-1252.
333
+ - Word writes each table row's definition twice, before its cells and again before `\row`. Both are read as one row. A row with no definition repeats the one before it.
334
+ - `\pard` resets the paragraph, not the characters: bold, italic, underline and strikethrough carry on until `\plain` or their own `\b0`.
335
+ - `\sect` ends a paragraph, as `\par` does.
336
+ - A font table is read in both shapes: one group per font, and TextEdit's run of fonts with no groups.
337
+ - **Alignment** (`\qc`, `\qr`, `\qj`) holds until `\pard`, as an indent does: Word and TextEdit state it once for a run of centered paragraphs or right-aligned cells. It ends with the group it was set in, and with the row.
338
+ - **Tracked changes.** Text a tracked change deleted (`\deleted`) is not shown; what replaced it is. Shown, "malignant" struck out and "benign" put in read "malignantbenign".
339
+ - **Hidden text** (`\v`) is not shown, and neither are index and contents entries (`\xe`, `\tc`), nor a footnote that sits in hidden or deleted text.
340
+ - **The Symbol font.** Text set in a font named Symbol is read as the characters that font shows: `m` is μ, `\'b0` is °, `\'b3` is ≥. So is the U+F0xx form Word writes, and a `SYMBOL` field (Insert Symbol in Word). Other symbol fonts (Wingdings) are not mapped.
341
+ - **Footnotes and endnotes** go to the end of the document, each after the number the text refers to it by; the sentence the note sits in is not split.
342
+ - **Text boxes** are read as paragraphs after the paragraph, or the table, they are anchored in. A picture with text wrapped around it is shown where it is anchored, and an embedded object as the picture or text it carries to be shown.
343
+ - **Binary picture data** (`\binN`) is stepped over; a brace among its bytes no longer ends the document. The picture itself is not shown.
344
+ - **U+2028**, which TextEdit writes for Shift-Return, is a line break, and U+2029 a paragraph mark. **A tab character** in the file is a tab, as `\tab` is: TextEdit writes every tab that way. Any other control character is dropped, written as itself or as an escape.
345
+ - **`\lastrow`** ends a table, so a table that follows another with no paragraph between is a table of its own.
346
+ - A byte-order mark or blank lines before `{\rtf1` do not hide the color and font tables.
347
+ - Damaged input does not stop a document being read: a `\'` with no hex digits after it, a `\u` out of range, half of a surrogate pair, a color component outside 0 to 255, groups nested tens of thousands deep, `\intbl` with no `\row`, a table row the file ends in the middle of.
348
+
349
+ ### Writing HTML that was pasted
350
+
351
+ The editor's own buttons make a small set of elements. A paste brings more, and `htmlToRtf` writes what the screen shows of it:
352
+
353
+ - **Formatting set by a style.** `font-weight`, `font-style`, `text-decoration` and `vertical-align` on an element are bold, italic, underline, strikethrough, superscript and subscript, which is how Google Docs writes all of them. A style can also take formatting off: Docs wraps a paste in `<b style="font-weight:normal">`, which is not bold. `color` is written on a heading, a list item and a table cell as it is on a paragraph.
354
+ - **What is not shown is not written:** `<style>`, `<script>` and `<template>`, an element with `hidden`, `display:none` or `visibility:hidden`, and Word's hidden text (`mso-hide:all`).
355
+ - **Blocks of a web page** (`<section>`, `<article>`, `<header>`, `<footer>`, `<main>`, `<aside>`, `<nav>`, `<address>`, `<details>`, `<summary>`, `<dl>`, `<dt>`, `<dd>`, `<center>`, `<fieldset>`, `<form>`) are written as a `<div>` is: each ends its line.
356
+ - **Alignment on a block around others** (`<div style="text-align:center">`, `<center>`) is the alignment of the paragraphs, headings and table cells inside it that set none. A list inside it is written left-aligned, so that it is still read back as a list.
357
+ - **`white-space: pre-wrap`** (also `pre`, `pre-line`, `break-spaces`): a new line in the text is a line break.
358
+ - **A figure with more than its picture** (text typed beside the picture, a second picture) is written as everything it holds, then its caption. The editor's own figure, one picture and a caption, is written as before.
359
+ - **A picture that cannot be embedded** is written as `[Image: description]`, or its address, and never as its data.
360
+ - **Control characters** are not written: a vertical tab or form feed becomes a space, the rest are dropped. 0x0B and 0x1C are among them, the bytes that start and end an HL7 message on the wire.
361
+ - **Loose text after a table** starts a plain paragraph of its own.
362
+
363
+ ### What `rtfToHtml` may return
364
+
365
+ `RtfViewer` puts the HTML `rtfToHtml` returns into the page as it is, so that HTML is limited to a fixed set, whatever the file holds: the elements `p`, `br`, `strong`, `em`, `u`, `s`, `sup`, `sub`, `span`, `code`, `pre`, `h1` to `h3`, `hr`, `ul`, `ol`, `li`, `a`, `table`, `tr`, `td`, `th`, `figure`, `figcaption` and `img`, and the attributes `style` (built from numbers and fixed words only), `href` (a web, mail or phone address, escaped), `target` and `rel` (fixed), `start`, `colspan` and `rowspan` (digits), and on a picture `src` (a PNG or JPEG data URL) and `alt`. Text from the file is always escaped. The test suite checks this against hostile files.
366
+
367
+ ### Size limit on import
368
+
369
+ `rtfToHtml` parses a document in one go on the main thread, and a picture costs two characters per byte, so the only bound on how long a page stops responding is the size of what it is handed. A string longer than `maxBytes` (`DEFAULT_MAX_RTF_BYTES`, 50 MB) is refused with a `RangeError` before any of it is parsed; `readRtfFile` rejects a larger file without reading it, and the editor's import shows that message; `RtfViewer` shows it in place of the document. Pass `maxBytes: 0` to read a document of any size, or a smaller number to refuse sooner; the components take the same number as `maxRtfBytes`. Where reports can carry large pictures, raise it: a 30 MB picture is a document of over 60 MB.
370
+
371
+ Size is not the only way to exhaust a page: a file of nothing but braces is small and still costs memory for each one. A document of more than five million parts (groups, control words, runs of text) is refused with a `RangeError` too, whatever `maxBytes` is. A long Word document has some hundreds of thousands; a picture is one for each line of the file its data takes up.
372
+
373
+ ### Markdown export
374
+
375
+ `htmlToMarkdown` writes a table as a Markdown table (a merged cell where it starts, with the cells it covers left empty, since Markdown has none), a numbered list from its `start`, and `<h4>` to `<h6>` as headings. Characters that Markdown would read as markup are escaped in text (`*`, `_`, `` ` ``, `#`, `[`, `]`, `<`, `~`, `\` and `|`), and so are `-`, `+`, `>` and `1.` at the start of a line: a report line typed as `- Benign.` stays that line. A link with an address `isSafeLinkUrl` refuses is written as its text alone.
376
+
377
+ A line break in a paragraph is a hard break (two spaces before the new line; a `<br>` in a heading or a table cell, which are one line). Text typed before a block is a paragraph of its own, and so is each `<div>`, `<section>`, `<dt>` and `<dd>`. A quotation of several paragraphs, or one holding a code block, carries its `>` on every line. A list written straight inside a list is nested under the item before it. Bold with a space inside the tag (`<b>diagnosis </b>`) is written with the space outside the marks, and `<b style="font-weight:normal">` is not bold. `<sup>`, `<sub>`, `<u>` and `<ins>` are written as those tags, which Markdown allows, and `<del>` as `~~`. A table's `<caption>` is a line before the table; a figure writes everything it holds, then its caption. `&copy;` typed as text stays those characters, and a line of `---` or `===` is escaped so that it is not a rule or a heading. What the screen does not show (`<style>`, `<script>`, `hidden`, `display:none`, `visibility:hidden`, `mso-hide:all`) is not written.
378
+
379
+ Code is the one thing written as it is, so it has to stay inside its code span or its fenced block. A line break in a code span is written as a space, which is how Markdown shows one; a code block starts a line of its own, whatever element is around it (in a heading or a table cell, which are one line, it is a code span on that line); every line of a quotation carries its `>`. Underline is the `<u>` tag around the text itself, and so are `<ins>`, `<sup>` and `<sub>`. No tag is ever alone on a line, or first on one after white space, where it would open a block of HTML: an empty line in a paragraph is a `<br>` at the end of the line before it, and a line that holds white space alone is written as `&nbsp;`. A tab at the start of a line is written as `&#9;`, since a tab there makes the line a code block. Code right after code is one code span, because two spans back to back run their backticks together. A `|` is escaped in all text, not only in a table: two typed lines such as `Stain | Result` and `--- | ---` are a table to a viewer that knows tables. A numbered list starts no lower than 0 and ends no higher than 999999999, the numbers Markdown can write. Bold, a link and underline are not written around a code block; around a quotation, a list or a table they are written on the text inside. Markdown allows HTML, and this export keeps a document's text from being read as any, but a page that shows Markdown from more than one source should still use a viewer that does not pass HTML through, or sanitize what the viewer makes.
380
+
226
381
  ## Theming
227
382
 
228
383
  The components use CSS custom properties with sensible fallbacks, so they work out of the box but are easy to customise. Override these in your own CSS:
@@ -291,7 +446,12 @@ It stays on until **Done** in the bar, `Esc`, or the button again. Each click is
291
446
  ### Pasted text
292
447
 
293
448
  A browser copies a paragraph with its looks written out as inline styles, and repeats its `id`. After a paste, each element that came in loses its `id` and any margin or first-line indent that changes nothing on screen, so a copy pasted inside the editor is the same markup as its source. A line the browser makes as a `<div>` is spaced like a `<p>`; both are written to RTF as a paragraph.
294
- Paragraph alignment (left, center, right, justify) is written to RTF as `\qc`, `\qr` or `\qj`.
449
+
450
+ A picture copied with its text comes in by address (`<img src="https://…">`). The editor fetches it and holds its data in the document, as it does for an address typed in the image dialog, so it reaches the RTF as a picture. When it cannot be fetched (the site forbids it, there is no network, it takes longer than 15 seconds or is larger than 25 MB) the picture still shows and the editor says that it will export as a placeholder. The pictures in one paste are fetched at the same time, so one slow site does not hold up the others. A picture pasted as data in a format RTF cannot carry (GIF, WebP, SVG), or larger than `maxImageBytes`, is converted and scaled the same way.
451
+
452
+ A picture pasted as data that cannot be brought under `maxImageBytes`, or that the browser cannot open, is taken out of the paste, and the editor says so. What was only there for it goes with it: its figure and caption, a link around it, a paragraph or list item that held nothing else. Text that shared its paragraph stays, a table cell stays, and so does anything that was in the document before the paste. Chromium, pasting a figure at the end of a line of text, moves its pictures into that line and leaves the caption in a figure of its own (inside the `<div>` or `<section>` the figure came in, if it had one); that caption goes with the picture too, unless another picture pasted with it is still there. Pasting a block into an empty line, Chromium puts the block in the line's place; if nothing of the paste is left once its pictures are out, the empty line is put back, with the caret in it. Pasting a figure into the middle of a line, it turns the caption into ordinary text of that line, and there the caption stays, since it can no longer be told from the words around it. When one paste has a picture left out and another that will export as a placeholder, the message says both.
453
+
454
+ Paragraph alignment (left, center, right, justify) is written to RTF as `\qc`, `\qr` or `\qj`.
295
455
 
296
456
  Tab stays in the editor so a line can be indented. To leave the editor from the keyboard, press
297
457
  `Esc` first. A readonly editor leaves Tab to the browser.
@@ -314,15 +474,20 @@ Click an image to select it. A frame appears with corner handles you can drag to
314
474
 
315
475
  Images are held inline as base64 data URLs, so a document is self-contained — it auto-saves, exports and re-imports with the pictures intact, and needs no image server.
316
476
 
317
- - **RTF export** writes each picture as a real `\pict` group (`\pngblip` / `\jpegblip`) sized with `\picwgoal` / `\pichgoal`, so it opens with the images in Word, TextEdit and other RTF readers. The description follows as an italic line, tagged with an ignorable `{\*\inkcap}` destination that this library uses to re-attach it to the figure on import; other readers simply skip the tag.
477
+ - **RTF export** writes each picture as a real `\pict` group (`\pngblip` / `\jpegblip`) sized with `\picwgoal` / `\pichgoal`, so it opens with the images in Word and other RTF readers that show pictures. (TextEdit opens the file and shows its text, but does not show the pictures of a plain `.rtf` file.) The description follows as an italic line, tagged with an ignorable `{\*\inkcap}` destination that this library uses to re-attach it to the figure on import; other readers simply skip the tag.
318
478
  - **RTF import** decodes `\pict` PNG and JPEG data (including pictures wrapped in `{\*\shppict}`) back into images at their stored size. Picture formats a browser cannot display — metafiles and device-dependent bitmaps — are skipped.
319
- - **Formats**: PNG and JPEG are embedded as-is. Anything else (GIF, WebP, SVG, …) is re-encoded to PNG when inserted, since RTF carries no other bitmap types. Animated images keep their first frame.
320
- - **Images added by address** are fetched and inlined so they survive export. When the fetch is blocked (CORS or offline) the address is kept and the picture still displays, but it exports as an `[Image: …]` placeholder instead of picture data — the editor says so at insert time rather than letting it go unnoticed until the document is read downstream.
479
+ - **Formats**: PNG and JPEG are embedded as-is. Anything else (GIF, WebP, SVG, …) is re-encoded to PNG when inserted, since RTF carries no other bitmap types. Animated images keep their first frame. A picture the browser cannot open (a TIFF, a HEIC photograph in most browsers, a damaged file) is not inserted, whatever its size, and the editor says that it could not be opened: it would show as a broken picture, and its data could not be converted for the RTF. That holds for a file, for picture data typed in the dialog and for picture data pasted with text. A picture fetched from an address that cannot be opened keeps its address and exports as the placeholder, as above.
480
+ - **Images added by address**, typed in the dialog or pasted with text copied from a web page, are fetched and inlined so they survive export. When the fetch is blocked (CORS or offline) the address is kept and the picture still displays, but it exports as an `[Image: …]` placeholder instead of picture data — the editor says so at insert time rather than letting it go unnoticed until the document is read downstream.
321
481
  - **Size**: RTF stores picture data as hexadecimal — two characters per byte — so an embedded image costs twice its file size in the document. Resizing an image in the editor changes its display size only; two caps applied at insert time are what bound the payload:
322
482
  - `maxImageEdge` (1600 px) scales the picture down so neither edge exceeds it.
323
- - `maxImageBytes` (512 KB, so roughly 1 MB of document per image) is the ceiling that actually matters. A picture over it is re-encoded until it fits: a PNG is tried as PNG first so screenshots and diagrams stay sharp, falls back to JPEG only when it is too heavy *and* has no transparency to lose, then steps down through 80%, 64% and 50% of the target size. Whichever candidate fits first is kept.
483
+ - `maxImageBytes` (512 KB, so roughly 1 MB of document per image) is the ceiling that actually matters, and it is a ceiling: no picture goes into the document over it. A picture over it is re-encoded until it fits:
484
+ 1. At full size, then 80%, 64% and 50% of it, a PNG is tried as PNG first so screenshots and diagrams stay sharp, and as JPEG only when it is too heavy *and* has no transparency to lose.
485
+ 2. If none of those fits, the picture is made smaller still, each step sized from how far over the last one was, down to 16 px on its longest edge. A picture with transparency is laid on white and written as a JPEG from full size again, which keeps far more of it than a PNG a fraction of the size.
486
+ 3. A picture that still does not fit is not inserted, and the editor says so. This takes a limit of a few hundred bytes, or a picture the browser cannot redraw.
324
487
 
325
- Set either to `0` to disable it. Both caps are applied by re-encoding through a `<canvas>`, so they only take effect in a browser — in a runtime without one (SSR, jsdom/happy-dom tests) the original bytes are passed through untouched. Do not rely on them to bound a payload assembled server-side.
488
+ Measured in Chromium with a 6.8 MB, 4000×3000 JPEG dropped on the editor: 312 KB at the defaults, 92 KB at a limit of 100 KB, 14 KB at 20 KB, 4.8 KB at 5 KB, refused at 300 bytes. A 6.9 MB PNG with transparency: a 368 KB JPEG on white at the defaults.
489
+
490
+ Set either to `0` to disable it. With `maxImageEdge` at `0` a very large picture can be more than the browser's canvas holds (268 million pixels in Chromium, about 16.7 million in Safari on a phone). A canvas asked for more gives back an empty picture and no error, so such a picture is first brought down to a size the canvas holds, half the area at a time, and the steps above start from there. Both caps are applied by re-encoding through a `<canvas>`, so pictures are only made smaller in a browser. In a runtime without one (SSR, jsdom/happy-dom tests) a picture within `maxImageBytes` is passed through untouched and one over it is refused, since it cannot be made to fit. The caps apply when a picture is inserted: HTML handed to the editor through `content` or `setHTML` is not checked, so do not rely on them to bound a payload assembled elsewhere.
326
491
  - **Page fit**: a newly inserted picture is displayed at `maxImageDisplayWidth` (624 px) rather than filling the editor. RTF records the display size in twips — 624 px × 15 = 9360 twips = 6.5 in, the text width of a Letter page with 1 in margins — so pictures arrive sized to the page they will be printed or filed on, not to the width of the browser window. Resize handles still go up to the full column width when a picture deserves it.
327
492
 
328
493
  ### Sending RTF over a transport
@@ -353,7 +518,7 @@ The transport cares about the size of the finished RTF, and pictures make that u
353
518
  <button onclick={() => console.log(editor.getRtfSize())}>Check size</button>
354
519
  ```
355
520
 
356
- `getRtfSize()` does the full conversion — call it when a decision depends on it, not on every keystroke. `estimatedRtfBytes` counts picture data exactly (two characters per byte, which is what dominates) and approximates the rest, leaving an error of a few hundred bytes at most. The output is ASCII, so bytes and characters are the same number.
521
+ `getRtfSize()` does the full conversion — call it when a decision depends on it, not on every keystroke. `estimatedRtfBytes` counts picture data exactly (two characters per byte, which is what dominates) and approximates the rest, never below the real size and, for what the editor itself makes, a few hundred bytes above it at most. (A pasted page of many small elements is counted further above, a few bytes for each.) The editor passes its own `fontFamily` and `fontSize`; calling `estimateRtfBytes` yourself, pass the options you give `htmlToRtf`. The output is ASCII, so bytes and characters are the same number.
357
522
 
358
523
  The library reports the size and stops there; whether an oversized document may be sent is the host application's decision.
359
524
 
@@ -376,6 +541,27 @@ npm run changeset
376
541
 
377
542
  Commit both your code and the generated `.changeset/*.md` file together.
378
543
 
544
+ ### Checks
545
+
546
+ Four commands must pass before a change is merged, and CI (`.github/workflows/ci.yml`) runs them on every push and pull request:
547
+
548
+ ```bash
549
+ npm run check # svelte-check: types
550
+ npm run test:coverage # vitest, with a floor under the coverage (vite.config.ts)
551
+ npm run prepack # build the package and lint it with publint
552
+ npm run test:e2e # Playwright, in Chromium (once: npx playwright install chromium)
553
+ ```
554
+
555
+ `npm test` runs the same unit tests without measuring coverage. They run in a test DOM, which has no caret, no editing commands and no clipboard; `e2e/` holds what only a browser shows: where the first words typed go, what a key does after a dialog closes, what a real paste brings in. It drives the page at `src/routes/e2e`.
556
+
557
+ The publish workflow runs the same four before it publishes, so a release cannot go out with a failing test.
558
+
559
+ `src/lib/__tests__/random-documents.test.ts` builds 400 documents at random on each run and checks what must hold for all of them: no text lost, the RTF settles after one reload, the size estimate is never low, the HTML read back is made of known elements. For a longer run, in batches:
560
+
561
+ ```bash
562
+ RTF_RANDOM_DOCUMENTS=4000 RTF_RANDOM_FIRST=4001 npx vitest run random-documents
563
+ ```
564
+
379
565
  ### Publishing a new version
380
566
 
381
567
  When ready to cut a release:
@@ -1,5 +1,5 @@
1
1
  <script lang="ts">
2
- import { rtfToHtml } from "./rtf-parser.js";
2
+ import { rtfToHtml, DEFAULT_MAX_RTF_BYTES } from "./rtf-parser.js";
3
3
  import {
4
4
  cssFontFamily,
5
5
  normalizeFontSize,
@@ -9,22 +9,43 @@
9
9
 
10
10
  // fontFamily and fontSize (points) set how the document is shown, and
11
11
  // default to the font the editor writes, so a document reads the same in
12
- // the viewer as in the editor.
12
+ // the viewer as in the editor. maxRtfBytes is the largest document it will
13
+ // read (rtfToHtml's maxBytes): a picture costs two characters per byte, so
14
+ // raise it where reports can carry large pictures, or pass 0 for no limit.
13
15
  let {
14
16
  content,
15
17
  fontFamily = DEFAULT_FONT_FAMILY,
16
- fontSize = DEFAULT_FONT_SIZE
17
- }: { content: string; fontFamily?: string; fontSize?: number } = $props();
18
+ fontSize = DEFAULT_FONT_SIZE,
19
+ maxRtfBytes = DEFAULT_MAX_RTF_BYTES
20
+ }: { content: string | null | undefined; fontFamily?: string; fontSize?: number; maxRtfBytes?: number } = $props();
18
21
 
19
22
  const screenFontFamily = $derived(cssFontFamily(fontFamily));
20
23
  const screenFontSize = $derived(normalizeFontSize(fontSize));
21
24
 
22
- const htmlContent = $derived(rtfToHtml(content, { fontSize }));
25
+ // rtfToHtml refuses a document over its size limit by throwing. The viewer then says so in
26
+ // place of the document, rather than taking the page down with it.
27
+ const parsed = $derived.by(() => {
28
+ try {
29
+ // A report that has not loaded yet shows as an empty document.
30
+ return { html: rtfToHtml(content ?? '', { fontSize, maxBytes: maxRtfBytes }), error: '' };
31
+ } catch (err) {
32
+ // Only the size limit's message is meant for the reader; anything else is a fault in
33
+ // the reader, not something the person looking at the report can act on.
34
+ const message = err instanceof RangeError && /too large/.test(err.message) ? err.message : '';
35
+ return { html: '', error: message || 'This document could not be read' };
36
+ }
37
+ });
23
38
 
24
39
  let copied = $state(false);
25
40
 
26
41
  async function copyRtf() {
27
- await navigator.clipboard.writeText(content);
42
+ // The clipboard can be refused (no permission, a page that is not served securely):
43
+ // then nothing was copied, and the button does not say it was.
44
+ try {
45
+ await navigator.clipboard.writeText(content ?? "");
46
+ } catch {
47
+ return;
48
+ }
28
49
  copied = true;
29
50
  setTimeout(() => (copied = false), 2000);
30
51
  }
@@ -45,7 +66,11 @@
45
66
  style:font-family={screenFontFamily}
46
67
  style:font-size="{screenFontSize}pt"
47
68
  >
48
- {@html htmlContent}
69
+ {#if parsed.error}
70
+ <p class="rtf-viewer-error" role="alert">{parsed.error}</p>
71
+ {:else}
72
+ {@html parsed.html}
73
+ {/if}
49
74
  </div>
50
75
  </div>
51
76
 
@@ -113,6 +138,43 @@
113
138
  margin-bottom: 0;
114
139
  }
115
140
 
141
+ .rtf-viewer-error {
142
+ color: var(--text-muted, #8a7e72);
143
+ font-style: italic;
144
+ }
145
+
146
+ /* A list item's text starts half an inch in, where the RTF puts it (\li720), and each
147
+ level is half an inch further. */
148
+ .rtf-viewer :global(ul),
149
+ .rtf-viewer :global(ol) {
150
+ margin: 0 0 0.2em;
151
+ padding-left: 36pt;
152
+ }
153
+
154
+ .rtf-viewer :global(li ul),
155
+ .rtf-viewer :global(li ol) {
156
+ margin-bottom: 0;
157
+ }
158
+
159
+ /* Code keeps the font the RTF names for it. A code block wraps, so a long line stays in
160
+ the viewer. */
161
+ .rtf-viewer :global(code),
162
+ .rtf-viewer :global(pre) {
163
+ font-family: 'Courier New', monospace;
164
+ }
165
+
166
+ .rtf-viewer :global(pre) {
167
+ margin: 0 0 0.2em;
168
+ font-size: calc(5em / 6);
169
+ white-space: pre-wrap;
170
+ }
171
+
172
+ /* A page break in the RTF (\page). */
173
+ .rtf-viewer :global(hr[style*='break-after']) {
174
+ border: 0;
175
+ border-top: 1px dashed currentColor;
176
+ }
177
+
116
178
  /* Pictures decoded from \pict groups keep their stored width but never
117
179
  overflow the viewer. */
118
180
  .rtf-viewer :global(img) {
@@ -1,7 +1,8 @@
1
1
  type $$ComponentProps = {
2
- content: string;
2
+ content: string | null | undefined;
3
3
  fontFamily?: string;
4
4
  fontSize?: number;
5
+ maxRtfBytes?: number;
5
6
  };
6
7
  declare const RtfViewer: import("svelte").Component<$$ComponentProps, {}, "">;
7
8
  type RtfViewer = ReturnType<typeof RtfViewer>;
@@ -16,6 +16,8 @@
16
16
  target?: HTMLImageElement | null;
17
17
  /** Positioned element the overlay coordinates are relative to. */
18
18
  bounds?: HTMLElement | null;
19
+ /** The editing area. It scrolls sideways, and the frame is not drawn outside it. */
20
+ within?: HTMLElement | null;
19
21
  /** Widest the image may become — the editor's content box width in px. */
20
22
  maxWidth?: () => number;
21
23
  onchange?: () => void;
@@ -23,7 +25,7 @@
23
25
  oncaption?: (img: HTMLImageElement) => void;
24
26
  }
25
27
 
26
- let { target = null, bounds = null, maxWidth, onchange, onremove, oncaption }: Props = $props();
28
+ let { target = null, bounds = null, within = null, maxWidth, onchange, onremove, oncaption }: Props = $props();
27
29
 
28
30
  const MIN_WIDTH = 40;
29
31
  const CORNERS = ['nw', 'ne', 'sw', 'se'] as const;
@@ -41,8 +43,14 @@
41
43
  }
42
44
  const t = target.getBoundingClientRect();
43
45
  const b = bounds.getBoundingClientRect();
44
- frame = { left: t.left - b.left, top: t.top - b.top, width: t.width, height: t.height };
45
- visible = t.width > 0 && t.height > 0;
46
+ // A picture in a wide table can be scrolled part or all of the way out of the editing
47
+ // area. The frame is drawn over the part of it still in view, and not at all when none
48
+ // is: it used to follow the picture out over the page beside the editor, tools and all.
49
+ const area = within?.getBoundingClientRect();
50
+ const left = area ? Math.max(t.left, area.left) : t.left;
51
+ const right = area ? Math.min(t.right, area.right) : t.right;
52
+ frame = { left: left - b.left, top: t.top - b.top, width: Math.max(0, right - left), height: t.height };
53
+ visible = t.width > 0 && t.height > 0 && right > left;
46
54
  }
47
55
 
48
56
  $effect(() => {
@@ -14,6 +14,8 @@ interface Props {
14
14
  target?: HTMLImageElement | null;
15
15
  /** Positioned element the overlay coordinates are relative to. */
16
16
  bounds?: HTMLElement | null;
17
+ /** The editing area. It scrolls sideways, and the frame is not drawn outside it. */
18
+ within?: HTMLElement | null;
17
19
  /** Widest the image may become — the editor's content box width in px. */
18
20
  maxWidth?: () => number;
19
21
  onchange?: () => void;