svelte-rtf-editor 1.12.0 → 1.15.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.
@@ -21,6 +21,7 @@ interface Props {
21
21
  autosave?: boolean;
22
22
  storageKey?: string;
23
23
  showToolbar?: boolean;
24
+ stickyToolbar?: boolean;
24
25
  showStatusBar?: boolean;
25
26
  minHeight?: string;
26
27
  readonly?: boolean;
@@ -33,6 +34,8 @@ interface Props {
33
34
  fontFamily?: string;
34
35
  fontSize?: number;
35
36
  fontSizes?: number[];
37
+ /** The fonts offered for text, beside the document's own; none unless given. */
38
+ fonts?: string[];
36
39
  /** `data-` attributes of the application's own, kept on HTML that is set or pasted. */
37
40
  keepAttributes?: string[];
38
41
  /** Class names of the application's own, kept the same way. */
@@ -57,7 +60,20 @@ declare const InkEditor: import("svelte").Component<Props, {
57
60
  exportFile: (format?: string) => void;
58
61
  importRtf: () => void;
59
62
  setFontSize: (points: number) => void;
63
+ setFont: (name: string | null) => void;
64
+ undo: () => boolean;
65
+ redo: () => boolean;
60
66
  clearFormatting: () => void;
67
+ setLineSpacing: (lines: number | null) => void;
68
+ setParagraphSpacing: (space: {
69
+ before?: number | null;
70
+ after?: number | null;
71
+ }) => void;
72
+ setListMarkerGap: (points: number | null) => void;
73
+ insertTable: (rows: number, columns: number, options?: {
74
+ borders?: boolean;
75
+ header?: boolean;
76
+ }) => void;
61
77
  }, "">;
62
78
  type InkEditor = ReturnType<typeof InkEditor>;
63
79
  export default InkEditor;
@@ -54,19 +54,15 @@
54
54
  e.preventDefault();
55
55
  return;
56
56
  }
57
- const active = document.activeElement;
58
- const first = items[0];
59
- const last = items[items.length - 1];
60
- if (!dialogEl.contains(active)) {
61
- e.preventDefault();
62
- first.focus();
63
- } else if (e.shiftKey && active === first) {
64
- e.preventDefault();
65
- last.focus();
66
- } else if (!e.shiftKey && active === last) {
67
- e.preventDefault();
68
- first.focus();
69
- }
57
+ // Every step is made here, not only the turn at the ends. Safari's Tab goes to text
58
+ // fields alone unless the user has asked for more, so from the last field it went past
59
+ // the dialog's buttons and out of the dialog, into the page behind it.
60
+ e.preventDefault();
61
+ const at = items.indexOf(document.activeElement as HTMLElement);
62
+ const next = at === -1 ? items[e.shiftKey ? items.length - 1 : 0] : items[(at + (e.shiftKey ? -1 : 1) + items.length) % items.length];
63
+ next.focus();
64
+ // As Tab into a field does: what it holds is selected, to be typed over.
65
+ if (next instanceof HTMLInputElement && /^(text|url|search|email|tel|password|number)$/.test(next.type)) next.select();
70
66
  }
71
67
  </script>
72
68
 
@@ -9,11 +9,25 @@
9
9
  onblock?: (tag: string) => void;
10
10
  oninsertlink?: () => void;
11
11
  oninsertimage?: () => void;
12
+ oninserttable?: () => void;
12
13
  /** The size of the text at the caret, in points; null where the selection has more than one. */
13
14
  fontSize?: number | null;
14
15
  /** The sizes offered, in points. */
15
16
  fontSizes?: number[];
16
17
  onfontsize?: (points: number) => void;
18
+ /** The font of the text at the caret, by its key: '' for the document font, null where the selection has more than one. */
19
+ font?: string | null;
20
+ /** The fonts offered beside the document's own. None, and there is no font list. */
21
+ fonts?: { name: string; css: string; key: string }[];
22
+ /** The document font's name: the first of the list, and what '' is shown as. */
23
+ documentFont?: string;
24
+ onfont?: (name: string) => void;
25
+ /**
26
+ * The spacing of the paragraph at the caret: lines apart (null: none set), and points before
27
+ * and after; and in a list, the gap after its markers in points (null: the list sets none).
28
+ * Out of a list `gap` is left out, and the list does not offer it.
29
+ */
30
+ spacing?: { line: number | null; before: number; after: number; gap?: number | null };
17
31
  /** The Copy format brush is in hand: the button shows as pressed. */
18
32
  copyingFormat?: boolean;
19
33
  oncopyformat?: () => void;
@@ -26,9 +40,15 @@
26
40
  onblock,
27
41
  oninsertlink,
28
42
  oninsertimage,
43
+ oninserttable,
29
44
  fontSize = null,
30
45
  fontSizes = [],
31
46
  onfontsize,
47
+ font = '',
48
+ fonts = [],
49
+ documentFont = '',
50
+ onfont,
51
+ spacing = { line: null, before: 0, after: 0 },
32
52
  copyingFormat = false,
33
53
  oncopyformat
34
54
  }: Props = $props();
@@ -42,6 +62,24 @@
42
62
  function exec(cmd: string, value: string | null = null) {
43
63
  onexec?.(cmd, value);
44
64
  }
65
+
66
+ // What the spacing list offers. The paragraph's own setting is offered too where it is
67
+ // another (8 pt after, in a document from elsewhere), so the list shows what the paragraph has.
68
+ const offered = (choices: number[], current: number | null) =>
69
+ current !== null && !choices.includes(current) ? [...choices, current].sort((a, b) => a - b) : choices;
70
+ const lineChoices = $derived(offered([1, 1.15, 1.5, 2], spacing.line));
71
+ const beforeChoices = $derived(offered([0, 6, 12], spacing.before));
72
+ const afterChoices = $derived(offered([0, 6, 12], spacing.after));
73
+ const gapChoices = $derived(offered([9, 18, 27, 36], spacing.gap ?? null));
74
+ const mark = (on: boolean) => (on ? '\u2713 ' : '\u2003');
75
+
76
+ /** One choice of the spacing list is a command; the list goes back to showing its name. */
77
+ function chooseSpacing(e: Event) {
78
+ const list = e.target as HTMLSelectElement;
79
+ const [cmd, value] = list.value.split(':');
80
+ list.value = '';
81
+ if (cmd) exec(cmd, value === 'none' ? null : value);
82
+ }
45
83
  </script>
46
84
 
47
85
  <div class="toolbar" role="toolbar" aria-label="Formatting">
@@ -49,6 +87,8 @@
49
87
  <div class="toolbar-group">
50
88
  <select
51
89
  class="tb-select"
90
+ title="Paragraph style"
91
+ aria-label="Paragraph style"
52
92
  value={blockType}
53
93
  onchange={(e) => onblock?.((e.target as HTMLSelectElement).value)}
54
94
  >
@@ -61,6 +101,30 @@
61
101
  </select>
62
102
  </div>
63
103
 
104
+ {#if fonts.length > 0}
105
+ <!-- Font -->
106
+ <div class="toolbar-group">
107
+ <select
108
+ class="tb-select tb-font"
109
+ title="Font"
110
+ aria-label="Font"
111
+ value={font === null ? ' ' : font}
112
+ onchange={(e) => {
113
+ const key = (e.target as HTMLSelectElement).value;
114
+ if (key !== ' ') onfont?.(key);
115
+ }}
116
+ >
117
+ {#if font === null}
118
+ <option value=" "></option>
119
+ {/if}
120
+ <option value="">{documentFont}</option>
121
+ {#each fonts as choice (choice.key)}
122
+ <option value={choice.key} style:font-family={choice.css}>{choice.name}</option>
123
+ {/each}
124
+ </select>
125
+ </div>
126
+ {/if}
127
+
64
128
  {#if fontSizes.length > 0}
65
129
  <!-- Font size -->
66
130
  <div class="toolbar-group">
@@ -116,10 +180,10 @@
116
180
 
117
181
  <!-- Lists -->
118
182
  <div class="toolbar-group">
119
- <ToolbarButton title="Bullet list" onclick={() => exec('insertUnorderedList')}>
183
+ <ToolbarButton active={formatState.bulletList} title="Bullet list" onclick={() => exec('insertUnorderedList')}>
120
184
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><line x1="9" y1="6" x2="20" y2="6"/><line x1="9" y1="12" x2="20" y2="12"/><line x1="9" y1="18" x2="20" y2="18"/><circle cx="5" cy="6" r="1.5" fill="currentColor"/><circle cx="5" cy="12" r="1.5" fill="currentColor"/><circle cx="5" cy="18" r="1.5" fill="currentColor"/></svg>
121
185
  </ToolbarButton>
122
- <ToolbarButton title="Numbered list" onclick={() => exec('insertOrderedList')}>
186
+ <ToolbarButton active={formatState.numberedList} title="Numbered list" onclick={() => exec('insertOrderedList')}>
123
187
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><line x1="10" y1="6" x2="20" y2="6"/><line x1="10" y1="12" x2="20" y2="12"/><line x1="10" y1="18" x2="20" y2="18"/><text x="3" y="8" font-size="8" fill="currentColor" stroke="none" font-family="sans-serif">1</text><text x="3" y="14" font-size="8" fill="currentColor" stroke="none" font-family="sans-serif">2</text><text x="3" y="20" font-size="8" fill="currentColor" stroke="none" font-family="sans-serif">3</text></svg>
124
188
  </ToolbarButton>
125
189
  </div>
@@ -168,6 +232,39 @@
168
232
 
169
233
  <div class="toolbar-divider"></div>
170
234
 
235
+ <!-- Spacing -->
236
+ <div class="toolbar-group">
237
+ <select class="tb-select tb-spacing" title="Line and paragraph spacing" aria-label="Line and paragraph spacing" value="" onchange={chooseSpacing}>
238
+ <option value="" hidden>Spacing</option>
239
+ <optgroup label="Line spacing">
240
+ {#each lineChoices as lines (lines)}
241
+ <option value="lineSpacing:{lines}">{mark(spacing.line === lines)}{lines === 1 ? '1.0' : lines === 2 ? '2.0' : lines}</option>
242
+ {/each}
243
+ <option value="lineSpacing:none">{mark(spacing.line === null)}Default</option>
244
+ </optgroup>
245
+ <optgroup label="Space before paragraph">
246
+ {#each beforeChoices as points (points)}
247
+ <option value="spaceBefore:{points}">{mark(spacing.before === points)}{points} pt</option>
248
+ {/each}
249
+ </optgroup>
250
+ <optgroup label="Space after paragraph">
251
+ {#each afterChoices as points (points)}
252
+ <option value="spaceAfter:{points}">{mark(spacing.after === points)}{points} pt</option>
253
+ {/each}
254
+ </optgroup>
255
+ {#if spacing.gap !== undefined}
256
+ <optgroup label="Bullet or number gap">
257
+ {#each gapChoices as points (points)}
258
+ <option value="markerGap:{points}">{mark(spacing.gap === points)}{points} pt</option>
259
+ {/each}
260
+ <option value="markerGap:none">{mark(spacing.gap === null)}Default</option>
261
+ </optgroup>
262
+ {/if}
263
+ </select>
264
+ </div>
265
+
266
+ <div class="toolbar-divider"></div>
267
+
171
268
  <!-- Copy format -->
172
269
  <div class="toolbar-group">
173
270
  <ToolbarButton active={copyingFormat} title="Copy format" onclick={oncopyformat}>
@@ -185,6 +282,9 @@
185
282
  <ToolbarButton title="Insert image" onclick={oninsertimage}>
186
283
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><rect x="3" y="3" width="18" height="18" rx="2"/><circle cx="8.5" cy="8.5" r="1.5"/><path d="M21 15l-5-5L5 21"/></svg>
187
284
  </ToolbarButton>
285
+ <ToolbarButton title="Insert table" onclick={oninserttable}>
286
+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="4" width="18" height="16" rx="2"/><line x1="3" y1="10" x2="21" y2="10"/><line x1="3" y1="15" x2="21" y2="15"/><line x1="9" y1="4" x2="9" y2="20"/><line x1="15" y1="4" x2="15" y2="20"/></svg>
287
+ </ToolbarButton>
188
288
  <ToolbarButton title="Horizontal rule" onclick={() => exec('insertHorizontalRule')}>
189
289
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><line x1="3" y1="12" x2="21" y2="12"/></svg>
190
290
  </ToolbarButton>
@@ -204,13 +304,13 @@
204
304
  </div>
205
305
 
206
306
  <style>
307
+ /* (Where it stands, and that it stays in sight of a long document, is the editor's: see
308
+ .ink-head there. Its ground is not see-through: the document passes under it.) */
207
309
  .toolbar {
208
- position: static;
209
- background: var(--bg-toolbar);
210
- border: 1px solid var(--border);
211
- border-radius: var(--radius);
310
+ background: var(--bg-toolbar, #fff);
311
+ border: 1px solid var(--border, #e5e2dc);
312
+ border-radius: var(--radius, 8px);
212
313
  padding: 6px 8px;
213
- margin: 24px 0 16px;
214
314
  display: flex;
215
315
  align-items: center;
216
316
  gap: 2px;
@@ -232,34 +332,57 @@
232
332
  flex-shrink: 0;
233
333
  }
234
334
 
335
+ /* The lists are boxes one can see: a list that shows nothing (a selection in two fonts, or
336
+ in two sizes) is still there to be chosen from. */
235
337
  .tb-select {
236
- height: 34px;
237
- padding: 0 10px;
238
- border: none;
239
- background: transparent;
240
- border-radius: var(--radius-sm);
338
+ height: 32px;
339
+ margin: 0 3px;
340
+ padding: 0 26px 0 11px;
341
+ border: 1px solid var(--border, #d9d4cc);
342
+ border-radius: 8px;
343
+ background-color: var(--bg-select, transparent);
241
344
  font-family: 'DM Sans', sans-serif;
242
345
  font-size: 13px;
243
346
  font-weight: 500;
244
347
  color: var(--text-muted);
245
348
  cursor: pointer;
246
- transition: all var(--transition);
349
+ transition:
350
+ border-color var(--transition, 150ms ease),
351
+ background-color var(--transition, 150ms ease),
352
+ box-shadow var(--transition, 150ms ease);
247
353
  appearance: none;
248
354
  -webkit-appearance: none;
249
- padding-right: 22px;
250
355
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%238A7E72' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
251
356
  background-repeat: no-repeat;
252
- background-position: right 6px center;
357
+ background-position: right 8px center;
358
+ }
359
+
360
+ /* Wide enough for a font's name, and no narrower when it shows none. */
361
+ .tb-font {
362
+ min-width: 9em;
363
+ max-width: 12em;
253
364
  }
254
365
 
255
366
  /* Two or three digits: narrower than the list of paragraph kinds. */
256
367
  .tb-size {
257
- min-width: 52px;
258
- padding-left: 8px;
368
+ min-width: 62px;
259
369
  }
260
370
 
371
+ /* It shows its name, not the longest of its choices. */
372
+ .tb-spacing {
373
+ width: 6.6em;
374
+ }
375
+
376
+ /* (The color alone: `background` would take the arrow off with it.) */
261
377
  .tb-select:hover {
262
- background: var(--surface);
378
+ background-color: var(--surface, #f6f4f0);
379
+ border-color: var(--border-active, #b8ab98);
263
380
  color: var(--text);
264
381
  }
382
+
383
+ .tb-select:focus-visible {
384
+ outline: none;
385
+ border-color: var(--accent, #6e56cf);
386
+ box-shadow: 0 0 0 3px var(--accent-soft, #ede9fe);
387
+ }
265
388
  </style>
@@ -5,11 +5,34 @@ interface Props {
5
5
  onblock?: (tag: string) => void;
6
6
  oninsertlink?: () => void;
7
7
  oninsertimage?: () => void;
8
+ oninserttable?: () => void;
8
9
  /** The size of the text at the caret, in points; null where the selection has more than one. */
9
10
  fontSize?: number | null;
10
11
  /** The sizes offered, in points. */
11
12
  fontSizes?: number[];
12
13
  onfontsize?: (points: number) => void;
14
+ /** The font of the text at the caret, by its key: '' for the document font, null where the selection has more than one. */
15
+ font?: string | null;
16
+ /** The fonts offered beside the document's own. None, and there is no font list. */
17
+ fonts?: {
18
+ name: string;
19
+ css: string;
20
+ key: string;
21
+ }[];
22
+ /** The document font's name: the first of the list, and what '' is shown as. */
23
+ documentFont?: string;
24
+ onfont?: (name: string) => void;
25
+ /**
26
+ * The spacing of the paragraph at the caret: lines apart (null: none set), and points before
27
+ * and after; and in a list, the gap after its markers in points (null: the list sets none).
28
+ * Out of a list `gap` is left out, and the list does not offer it.
29
+ */
30
+ spacing?: {
31
+ line: number | null;
32
+ before: number;
33
+ after: number;
34
+ gap?: number | null;
35
+ };
13
36
  /** The Copy format brush is in hand: the button shows as pressed. */
14
37
  copyingFormat?: boolean;
15
38
  oncopyformat?: () => void;
@@ -0,0 +1,49 @@
1
+ /**
2
+ * The editor's own Undo and Redo.
3
+ *
4
+ * A browser's history knows what its own commands and the keyboard changed. It knows nothing
5
+ * of what the editor changes in the document itself: a picture or a table put in, a picture
6
+ * moved, an indent. Its Undo skipped those and took back the typing before them, and what
7
+ * the editor then changed behind it could leave it unable to undo at all. So the editor
8
+ * keeps the history: the document as it was before each step, and Undo puts that back.
9
+ *
10
+ * A step is whatever changed between two calls of `step()`, however it was changed: by a
11
+ * key, by a browser command, or by the editor. The editor calls `step()` before each thing it
12
+ * does, and `typing()` before each key's worth of input, which keeps a run of typing as one
13
+ * step. Nothing has to be said after a change: a MutationObserver knows there was one.
14
+ *
15
+ * What is kept is the document's HTML, with each embedded picture kept once, and the
16
+ * selection. Putting a step back replaces only the blocks that differ, so a picture in a
17
+ * part of the document that did not change is not read again.
18
+ */
19
+ export interface HistoryOptions {
20
+ /** How many steps are kept. */
21
+ limit?: number;
22
+ /** Typing after a pause this long, in ms, is a new step. */
23
+ pause?: number;
24
+ }
25
+ export interface DocumentHistory {
26
+ /** What changed since the last step is a step, from here. Call before changing the document. */
27
+ step(): void;
28
+ /**
29
+ * A key's worth of input is about to change the document. A run of the same kind ('insert'
30
+ * or 'delete'), at the caret the last one left, with no pause, is one step; `null` is input
31
+ * that is a step of its own (a new paragraph, a paste, a cut).
32
+ */
33
+ typing(kind: 'insert' | 'delete' | null): void;
34
+ /** The input has changed the document. */
35
+ typed(): void;
36
+ /** Put the document back as it was before the last step. `then` runs once it is back, and what it changes is part of what was put back. */
37
+ undo(then?: () => void): boolean;
38
+ redo(then?: () => void): boolean;
39
+ canUndo(): boolean;
40
+ canRedo(): boolean;
41
+ /** A change that is no step of its own: with nothing else changed since the last step, it is part of that one. */
42
+ quietly(change: () => void): void;
43
+ /** Forget every step: the document is another one. */
44
+ reset(): void;
45
+ /** Text is being composed (an input method): it is one run of typing, and nothing is undone until it is done. */
46
+ composing(on: boolean): void;
47
+ destroy(): void;
48
+ }
49
+ export declare function createHistory(root: HTMLElement, options?: HistoryOptions): DocumentHistory;