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.
@@ -1,4 +1,6 @@
1
1
  <script lang="ts">
2
+ import { MOST_GAP_PT } from '../picture-rows.js';
3
+
2
4
  /**
3
5
  * ImageOverlay — selection frame for an image inside the editor.
4
6
  *
@@ -7,10 +9,22 @@
7
9
  * `bounds`, which must be a positioned ancestor of the editing surface.
8
10
  *
9
11
  * Provides drag-to-resize corner handles plus a small toolbar for
10
- * placement (left / centre / right), width presets, caption editing and
11
- * deletion.
12
+ * placement (left / centre / right), width presets, moving the picture up
13
+ * or down the document, putting it beside the picture above it (or back on
14
+ * a line of its own), caption editing and deletion.
12
15
  */
13
16
 
17
+ /** Where the selected picture can go from where it is. */
18
+ export interface PicturePlace {
19
+ /** There is something above it to move over, and something below. */
20
+ up: boolean;
21
+ down: boolean;
22
+ /** 'join': it can go beside the picture above it. 'leave': it is beside another, and can go on a line of its own. */
23
+ beside: 'join' | 'leave' | null;
24
+ /** For a picture beside another: the space set between the pictures of its row, in points; null where they share the page equally. */
25
+ gap?: number | null;
26
+ }
27
+
14
28
  interface Props {
15
29
  /** Image currently selected in the editor, or null when nothing is selected. */
16
30
  target?: HTMLImageElement | null;
@@ -23,9 +37,32 @@
23
37
  onchange?: () => void;
24
38
  onremove?: () => void;
25
39
  oncaption?: (img: HTMLImageElement) => void;
40
+ /** Where the picture can go; without it the frame has no buttons for moving it. */
41
+ place?: () => PicturePlace;
42
+ /** Move the picture over the block above it (-1) or below it (1). */
43
+ onmove?: (direction: -1 | 1) => void;
44
+ /** Put the picture beside the one above it, or back on a line of its own. */
45
+ onbeside?: () => void;
46
+ /** Set the space between the pictures of the row, in points; null: they share the page equally. */
47
+ ongap?: (points: number | null) => void;
48
+ /** The picture's width changed, and a resize is not over yet. */
49
+ onresizing?: () => void;
26
50
  }
27
51
 
28
- let { target = null, bounds = null, within = null, maxWidth, onchange, onremove, oncaption }: Props = $props();
52
+ let { target = null, bounds = null, within = null, maxWidth, onchange, onremove, oncaption, place, onmove, onbeside, ongap, onresizing }: Props = $props();
53
+
54
+ /** The gap typed: a number of points, or nothing (the row shares the page equally). */
55
+ function gapTyped(e: Event): void {
56
+ const input = e.currentTarget as HTMLInputElement;
57
+ const value = input.value.trim();
58
+ const points = value === '' ? null : Number(value);
59
+ if (points !== null && !(points > 0)) {
60
+ // (Not a gap: what the row has is shown again.)
61
+ input.value = where?.gap == null ? '' : String(where.gap);
62
+ return;
63
+ }
64
+ ongap?.(points);
65
+ }
29
66
 
30
67
  const MIN_WIDTH = 40;
31
68
  const CORNERS = ['nw', 'ne', 'sw', 'se'] as const;
@@ -34,6 +71,27 @@
34
71
  let visible = $state(false);
35
72
  let dragging = $state(false);
36
73
  let sizeLabel = $state('');
74
+ /** Where the picture can go: read again whenever the frame is placed again, as a move changes it. */
75
+ const where = $derived.by(() => (frame && target && place ? place() : null));
76
+ let toolsEl: HTMLElement | null = $state(null);
77
+ /** How far the tools are moved off the middle of the picture, to stay inside `bounds`. */
78
+ let toolsShift = $state(0);
79
+
80
+ // The tools sit over the middle of the picture, and are wider than a small one: over a
81
+ // picture at the edge of the page (the left one of two side by side) they would hang out
82
+ // over that edge, half of them out of reach.
83
+ $effect(() => {
84
+ const width = toolsEl?.offsetWidth ?? 0;
85
+ const room = bounds?.clientWidth ?? 0;
86
+ if (!visible || !width || !room) {
87
+ toolsShift = 0;
88
+ return;
89
+ }
90
+ const middle = frame.left + frame.width / 2;
91
+ const least = 4 + width / 2;
92
+ const most = room - 4 - width / 2;
93
+ toolsShift = most < least ? room / 2 - middle : Math.round(Math.min(Math.max(middle, least), most) - middle);
94
+ });
37
95
 
38
96
  /** Recompute the frame position — call after anything that reflows content. */
39
97
  export function reposition(): void {
@@ -73,6 +131,7 @@
73
131
  target.removeAttribute('height');
74
132
  target.style.width = `${width}px`;
75
133
  target.style.height = 'auto';
134
+ onresizing?.();
76
135
  reposition();
77
136
  }
78
137
 
@@ -113,20 +172,37 @@
113
172
  handle.addEventListener('pointercancel', onUp);
114
173
  }
115
174
 
175
+ /**
176
+ * What says where the picture is aligned: its figure, or the block it is in. A picture
177
+ * alone in a table cell (one of two side by side) is aligned by the cell, which is where
178
+ * the RTF has it and where it is read back.
179
+ */
180
+ function alignBlock(): HTMLElement | null {
181
+ if (!target) return null;
182
+ const figure = target.closest('figure');
183
+ const cell = (figure ?? target).parentElement;
184
+ if (cell && /^(TD|TH)$/.test(cell.tagName) && cell.children.length === 1) return cell;
185
+ return figure ?? target.parentElement;
186
+ }
187
+
116
188
  function align(value: 'left' | 'center' | 'right'): void {
117
- if (!target) return;
118
- const block = target.closest('figure') ?? target.parentElement;
119
- if (!block) return;
120
- if (value === 'left') block.removeAttribute('style');
121
- else (block as HTMLElement).style.textAlign = value;
189
+ const block = alignBlock();
190
+ if (!block || !target) return;
191
+ const figure = target.closest('figure');
192
+ if (figure && figure !== block) {
193
+ figure.style.textAlign = '';
194
+ if (!figure.getAttribute('style')) figure.removeAttribute('style');
195
+ }
196
+ // (A header cell says so when it is left: a browser centers one that does not. Only the
197
+ // alignment is changed: a picture's indent and the space around it stay.)
198
+ block.style.textAlign = value === 'left' && block.tagName !== 'TH' ? '' : value;
199
+ if (!block.getAttribute('style')) block.removeAttribute('style');
122
200
  reposition();
123
201
  onchange?.();
124
202
  }
125
203
 
126
- function currentAlign(): string {
127
- const block = target?.closest('figure') ?? target?.parentElement;
128
- return (block as HTMLElement | null)?.style?.textAlign || 'left';
129
- }
204
+ /** How the picture is aligned: read again whenever the frame is placed again, as a button here changes it. */
205
+ const aligned = $derived.by(() => (frame && target ? alignBlock()?.style?.textAlign || 'left' : 'left'));
130
206
 
131
207
  function scaleTo(percent: number): void {
132
208
  const max = maxWidth?.() ?? 0;
@@ -144,11 +220,11 @@
144
220
  class:dragging
145
221
  style="left:{frame.left}px;top:{frame.top}px;width:{frame.width}px;height:{frame.height}px"
146
222
  >
147
- <div class="ink-img-tools">
223
+ <div class="ink-img-tools" bind:this={toolsEl} style={toolsShift ? `transform:translateX(calc(-50% + ${toolsShift}px))` : undefined}>
148
224
  <button
149
225
  type="button"
150
226
  title="Align left"
151
- class:active={currentAlign() === 'left'}
227
+ class:active={aligned === 'left'}
152
228
  onclick={() => align('left')}
153
229
  >
154
230
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><line x1="3" y1="6" x2="21" y2="6"/><line x1="3" y1="12" x2="15" y2="12"/><line x1="3" y1="18" x2="18" y2="18"/></svg>
@@ -156,7 +232,7 @@
156
232
  <button
157
233
  type="button"
158
234
  title="Align center"
159
- class:active={currentAlign() === 'center'}
235
+ class:active={aligned === 'center'}
160
236
  onclick={() => align('center')}
161
237
  >
162
238
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><line x1="3" y1="6" x2="21" y2="6"/><line x1="6" y1="12" x2="18" y2="12"/><line x1="4" y1="18" x2="20" y2="18"/></svg>
@@ -164,7 +240,7 @@
164
240
  <button
165
241
  type="button"
166
242
  title="Align right"
167
- class:active={currentAlign() === 'right'}
243
+ class:active={aligned === 'right'}
168
244
  onclick={() => align('right')}
169
245
  >
170
246
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><line x1="3" y1="6" x2="21" y2="6"/><line x1="9" y1="12" x2="21" y2="12"/><line x1="6" y1="18" x2="21" y2="18"/></svg>
@@ -178,6 +254,40 @@
178
254
 
179
255
  <span class="ink-img-sep"></span>
180
256
 
257
+ {#if where}
258
+ <button type="button" title="Move up" disabled={!where.up} onclick={() => onmove?.(-1)}>
259
+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="12" y1="19" x2="12" y2="5"/><polyline points="6 11 12 5 18 11"/></svg>
260
+ </button>
261
+ <button type="button" title="Move down" disabled={!where.down} onclick={() => onmove?.(1)}>
262
+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="12" y1="5" x2="12" y2="19"/><polyline points="6 13 12 19 18 13"/></svg>
263
+ </button>
264
+ {#if where.beside === 'leave'}
265
+ <button type="button" class="active" title="Place on its own line" onclick={() => onbeside?.()}>
266
+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="6" width="8" height="12" rx="1"/><rect x="13" y="6" width="8" height="12" rx="1"/></svg>
267
+ </button>
268
+ <label class="ink-img-gap" title="Space between the pictures, in points (72 pt = 1 inch). Empty: the pictures share the width of the page.">
269
+ <span>Gap</span>
270
+ <input
271
+ type="number"
272
+ min="1"
273
+ max={MOST_GAP_PT}
274
+ step="1"
275
+ placeholder="auto"
276
+ aria-label="Gap between the pictures, in points"
277
+ value={where.gap ?? ''}
278
+ onchange={gapTyped}
279
+ />
280
+ <span>pt</span>
281
+ </label>
282
+ {:else}
283
+ <button type="button" title="Place beside the picture above" disabled={where.beside !== 'join'} onclick={() => onbeside?.()}>
284
+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="6" width="8" height="12" rx="1"/><rect x="13" y="6" width="8" height="12" rx="1"/></svg>
285
+ </button>
286
+ {/if}
287
+
288
+ <span class="ink-img-sep"></span>
289
+ {/if}
290
+
181
291
  <button type="button" title="Edit description" onclick={() => target && oncaption?.(target)}>
182
292
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M4 7V5h16v2"/><line x1="9" y1="19" x2="15" y2="19"/><line x1="12" y1="5" x2="12" y2="19"/></svg>
183
293
  </button>
@@ -267,11 +377,16 @@
267
377
  transition: all 150ms cubic-bezier(0.4, 0, 0.2, 1);
268
378
  }
269
379
 
270
- .ink-img-tools button:hover {
380
+ .ink-img-tools button:hover:not(:disabled) {
271
381
  background: var(--surface, #f2f0ec);
272
382
  color: var(--text, #2c2520);
273
383
  }
274
384
 
385
+ .ink-img-tools button:disabled {
386
+ opacity: 0.35;
387
+ cursor: default;
388
+ }
389
+
275
390
  .ink-img-tools button.active {
276
391
  background: var(--accent-soft, #fdf0e9);
277
392
  color: var(--accent, #d4622b);
@@ -287,6 +402,27 @@
287
402
  height: 14px;
288
403
  }
289
404
 
405
+ .ink-img-gap {
406
+ display: inline-flex;
407
+ align-items: center;
408
+ gap: 3px;
409
+ margin: 0 2px;
410
+ font-family: 'DM Sans', sans-serif;
411
+ font-size: 11px;
412
+ font-weight: 500;
413
+ color: var(--text-muted, #8a7e72);
414
+ }
415
+
416
+ .ink-img-gap input {
417
+ width: 4.2em;
418
+ padding: 1px 3px;
419
+ font: inherit;
420
+ color: inherit;
421
+ background: transparent;
422
+ border: 1px solid var(--border, #e5e2dc);
423
+ border-radius: 3px;
424
+ }
425
+
290
426
  .ink-img-sep {
291
427
  width: 1px;
292
428
  height: 16px;
@@ -6,9 +6,20 @@
6
6
  * `bounds`, which must be a positioned ancestor of the editing surface.
7
7
  *
8
8
  * Provides drag-to-resize corner handles plus a small toolbar for
9
- * placement (left / centre / right), width presets, caption editing and
10
- * deletion.
9
+ * placement (left / centre / right), width presets, moving the picture up
10
+ * or down the document, putting it beside the picture above it (or back on
11
+ * a line of its own), caption editing and deletion.
11
12
  */
13
+ /** Where the selected picture can go from where it is. */
14
+ export interface PicturePlace {
15
+ /** There is something above it to move over, and something below. */
16
+ up: boolean;
17
+ down: boolean;
18
+ /** 'join': it can go beside the picture above it. 'leave': it is beside another, and can go on a line of its own. */
19
+ beside: 'join' | 'leave' | null;
20
+ /** For a picture beside another: the space set between the pictures of its row, in points; null where they share the page equally. */
21
+ gap?: number | null;
22
+ }
12
23
  interface Props {
13
24
  /** Image currently selected in the editor, or null when nothing is selected. */
14
25
  target?: HTMLImageElement | null;
@@ -21,6 +32,16 @@ interface Props {
21
32
  onchange?: () => void;
22
33
  onremove?: () => void;
23
34
  oncaption?: (img: HTMLImageElement) => void;
35
+ /** Where the picture can go; without it the frame has no buttons for moving it. */
36
+ place?: () => PicturePlace;
37
+ /** Move the picture over the block above it (-1) or below it (1). */
38
+ onmove?: (direction: -1 | 1) => void;
39
+ /** Put the picture beside the one above it, or back on a line of its own. */
40
+ onbeside?: () => void;
41
+ /** Set the space between the pictures of the row, in points; null: they share the page equally. */
42
+ ongap?: (points: number | null) => void;
43
+ /** The picture's width changed, and a resize is not over yet. */
44
+ onresizing?: () => void;
24
45
  }
25
46
  declare const ImageOverlay: import("svelte").Component<Props, {
26
47
  reposition: () => void;