@atmo-dev/kit 0.20.0 → 0.22.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.
Files changed (104) hide show
  1. package/README.md +1 -1
  2. package/dist/components/RichText.svelte +17 -9
  3. package/dist/components/RichTextEditor.svelte +67 -11
  4. package/dist/components/RichTextEditor.svelte.d.ts +0 -2
  5. package/dist/components/StreamplacePicker.svelte +2 -2
  6. package/dist/components/StreamplacePicker.svelte.d.ts +1 -1
  7. package/dist/components/composer/EmojiPanel.svelte +55 -0
  8. package/dist/components/composer/EmojiPanel.svelte.d.ts +6 -0
  9. package/dist/components/composer/EmojiPicker.svelte +2 -38
  10. package/dist/components/data/DataLabel.svelte +120 -0
  11. package/dist/components/data/DataLabel.svelte.d.ts +10 -0
  12. package/dist/components/data/issues.d.ts +5 -0
  13. package/dist/components/data/issues.js +52 -0
  14. package/dist/components/data/types.d.ts +28 -0
  15. package/dist/components/data/types.js +1 -0
  16. package/dist/components/data/visibility.d.ts +7 -0
  17. package/dist/components/data/visibility.js +15 -0
  18. package/dist/components/legal/links.js +1 -0
  19. package/dist/components/legal/types.d.ts +1 -1
  20. package/dist/components/notifications/NotificationList.svelte +137 -0
  21. package/dist/components/notifications/NotificationList.svelte.d.ts +19 -0
  22. package/dist/components/notifications/NotificationPushToggle.svelte +76 -0
  23. package/dist/components/notifications/NotificationPushToggle.svelte.d.ts +19 -0
  24. package/dist/components/notifications/NotificationSettings.svelte +125 -0
  25. package/dist/components/notifications/NotificationSettings.svelte.d.ts +16 -0
  26. package/dist/components/notifications/types.d.ts +37 -0
  27. package/dist/components/notifications/types.js +1 -0
  28. package/dist/editor/ImageDialog.svelte +87 -0
  29. package/dist/editor/ImageDialog.svelte.d.ts +16 -0
  30. package/dist/editor/image-node.d.ts +6 -2
  31. package/dist/editor/image-node.js +36 -37
  32. package/dist/events/EventCard.svelte +6 -1
  33. package/dist/events/VodPlayer.svelte +1 -1
  34. package/dist/events/atmo-rsvp.d.ts +6 -0
  35. package/dist/events/atmo-rsvp.js +34 -0
  36. package/dist/events/atproto-helpers.d.ts +1 -0
  37. package/dist/events/atproto-helpers.js +3 -0
  38. package/dist/events/event-link-embed.d.ts +7 -3
  39. package/dist/events/event-link-embed.js +11 -5
  40. package/dist/events/event-view/StreamPlacePlayer.svelte +14 -108
  41. package/dist/events/linked-records.d.ts +1 -1
  42. package/dist/events/linked-records.js +2 -2
  43. package/dist/events-card.d.ts +9 -0
  44. package/dist/events-card.js +7 -0
  45. package/dist/events-data.d.ts +1 -0
  46. package/dist/events-data.js +1 -0
  47. package/dist/gestures.d.ts +5 -1
  48. package/dist/gestures.js +6 -3
  49. package/dist/grain/xrpc.js +11 -4
  50. package/dist/index.d.ts +13 -1
  51. package/dist/index.js +7 -1
  52. package/dist/legal.d.ts +3 -0
  53. package/dist/legal.js +2 -0
  54. package/dist/richtext/serialize.js +13 -8
  55. package/dist/streamplace/StreamCard.svelte +184 -0
  56. package/dist/streamplace/StreamCard.svelte.d.ts +24 -0
  57. package/dist/streamplace/StreamLinkEmbed.svelte +59 -0
  58. package/dist/streamplace/StreamLinkEmbed.svelte.d.ts +5 -0
  59. package/dist/streamplace/StreamPlayer.svelte +539 -0
  60. package/dist/streamplace/StreamPlayer.svelte.d.ts +42 -0
  61. package/dist/streamplace/VideoCard.svelte +159 -0
  62. package/dist/streamplace/VideoCard.svelte.d.ts +17 -0
  63. package/dist/streamplace/api.d.ts +74 -0
  64. package/dist/streamplace/api.js +182 -0
  65. package/dist/streamplace/broadcast.d.ts +3 -0
  66. package/dist/streamplace/broadcast.js +110 -0
  67. package/dist/streamplace/channel.svelte.d.ts +74 -0
  68. package/dist/streamplace/channel.svelte.js +515 -0
  69. package/dist/streamplace/chat/Composer.svelte +232 -0
  70. package/dist/streamplace/chat/Composer.svelte.d.ts +15 -0
  71. package/dist/streamplace/chat/Embeds.svelte +135 -0
  72. package/dist/streamplace/chat/Embeds.svelte.d.ts +13 -0
  73. package/dist/streamplace/chat/Message.svelte +303 -0
  74. package/dist/streamplace/chat/Message.svelte.d.ts +25 -0
  75. package/dist/streamplace/chat/StreamChat.svelte +463 -0
  76. package/dist/streamplace/chat/StreamChat.svelte.d.ts +33 -0
  77. package/dist/streamplace/chat/StreamChatPopups.svelte +151 -0
  78. package/dist/streamplace/chat/StreamChatPopups.svelte.d.ts +15 -0
  79. package/dist/streamplace/chat/UserCard.svelte +76 -0
  80. package/dist/streamplace/chat/UserCard.svelte.d.ts +12 -0
  81. package/dist/streamplace/chat/color.d.ts +7 -0
  82. package/dist/streamplace/chat/color.js +33 -0
  83. package/dist/streamplace/chat/links.d.ts +10 -0
  84. package/dist/streamplace/chat/links.js +29 -0
  85. package/dist/streamplace/facets.d.ts +15 -0
  86. package/dist/streamplace/facets.js +112 -0
  87. package/dist/streamplace/link-embed.d.ts +13 -0
  88. package/dist/streamplace/link-embed.js +16 -0
  89. package/dist/streamplace/links.d.ts +6 -0
  90. package/dist/streamplace/links.js +1 -0
  91. package/dist/streamplace/reactions.d.ts +21 -0
  92. package/dist/streamplace/reactions.js +46 -0
  93. package/dist/streamplace/types.d.ts +200 -0
  94. package/dist/streamplace/types.js +1 -0
  95. package/dist/streamplace-data.d.ts +11 -0
  96. package/dist/streamplace-data.js +10 -0
  97. package/dist/streamplace.d.ts +14 -6
  98. package/dist/streamplace.js +11 -1
  99. package/dist/{events/event-view → video}/Player.svelte +69 -21
  100. package/dist/video/Player.svelte.d.ts +38 -0
  101. package/dist/video/engines.d.ts +59 -0
  102. package/dist/video/engines.js +647 -0
  103. package/package.json +11 -1
  104. package/dist/events/event-view/Player.svelte.d.ts +0 -24
@@ -0,0 +1,137 @@
1
+ <script lang="ts" module>
2
+ import type { HTMLAttributes } from 'svelte/elements';
3
+ import type { NotificationItem } from './types.js';
4
+ export type NotificationListProps = Omit<HTMLAttributes<HTMLElement>, 'children'> & {
5
+ items: readonly NotificationItem[];
6
+ /** Called when an item is activated, before navigation. Items without a link become buttons. */
7
+ onopen?: (item: NotificationItem) => void;
8
+ /** Shown when there are no items. */
9
+ empty?: string;
10
+ /** Text for the unread marker, for assistive technology. */
11
+ unreadLabel?: string;
12
+ locale?: string;
13
+ /** Time zone for calendar dates; defaults to the runtime's. */
14
+ timeZone?: string;
15
+ /** Reference time for relative dates; defaults to the current time. */
16
+ now?: number;
17
+ };
18
+
19
+ const steps: Array<[Intl.RelativeTimeFormatUnit, number]> = [
20
+ ['minute', 60_000],
21
+ ['hour', 3_600_000],
22
+ ['day', 86_400_000]
23
+ ];
24
+ </script>
25
+
26
+ <script lang="ts">
27
+ import { cn } from '../../utils.js';
28
+ import { focus, hoverRecess } from '../../styles.js';
29
+ import { safeLinkHref, safeHttpUrl } from '../../urls.js';
30
+ import Avatar from '../Avatar.svelte';
31
+
32
+ let {
33
+ items,
34
+ onopen,
35
+ empty = 'No notifications yet.',
36
+ unreadLabel = 'Unread',
37
+ locale,
38
+ timeZone,
39
+ now = Date.now(),
40
+ class: className,
41
+ ...rest
42
+ }: NotificationListProps = $props();
43
+
44
+ const relative = $derived(new Intl.RelativeTimeFormat(locale, { numeric: 'auto' }));
45
+ const absolute = $derived(new Intl.DateTimeFormat(locale, { dateStyle: 'medium', timeZone }));
46
+ const row = 'flex items-start gap-3 px-2 py-3';
47
+ const interactive = cn(
48
+ 'rounded-ui no-underline transition-colors duration-(--atmo-duration) motion-reduce:transition-none',
49
+ hoverRecess,
50
+ focus
51
+ );
52
+
53
+ function link(item: NotificationItem) {
54
+ const href = safeLinkHref(item.uri);
55
+ return href && (href.startsWith('/') || href.startsWith('#') || safeHttpUrl(href))
56
+ ? href
57
+ : undefined;
58
+ }
59
+
60
+ function iso(time: number) {
61
+ const date = new Date(time);
62
+ return Number.isNaN(date.getTime()) ? undefined : date.toISOString();
63
+ }
64
+
65
+ function when(time: number) {
66
+ if (!Number.isFinite(time)) return '';
67
+ const elapsed = Math.max(0, (Number.isFinite(now) ? now : Date.now()) - time);
68
+ if (elapsed < 60_000) return relative.format(0, 'second');
69
+ if (elapsed >= 7 * 86_400_000) return absolute.format(time);
70
+ const [unit, size] = steps.findLast(([, size]) => elapsed >= size)!;
71
+ return relative.format(-Math.floor(elapsed / size), unit);
72
+ }
73
+ </script>
74
+
75
+ {#snippet content(item: NotificationItem)}
76
+ {@const actor = item.actors?.[0]}
77
+ {@const unread = item.readAt == null}
78
+ {@const datetime = iso(item.sendAt)}
79
+ {#if actor}
80
+ <Avatar
81
+ name={actor.displayName || actor.handle || actor.did}
82
+ src={actor.avatar}
83
+ size={36}
84
+ aria-hidden="true"
85
+ />
86
+ {/if}
87
+ <span class="flex min-w-0 flex-1 flex-col gap-0.5 wrap-anywhere">
88
+ <span class={cn('text-sm text-base-950 dark:text-base-50', unread && 'font-semibold')}>
89
+ {item.title}
90
+ </span>
91
+ {#if item.body}
92
+ <span class="text-sm text-base-600 dark:text-base-400">{item.body}</span>
93
+ {/if}
94
+ {#if datetime}
95
+ <time class="text-xs text-base-600 dark:text-base-400" {datetime}>
96
+ {when(item.sendAt)}
97
+ </time>
98
+ {/if}
99
+ </span>
100
+ {#if unread}
101
+ <span
102
+ class="mt-1.5 size-2 shrink-0 rounded-full bg-accent-700 dark:bg-accent-300"
103
+ aria-hidden="true"
104
+ ></span>
105
+ <span class="sr-only">{unreadLabel}</span>
106
+ {/if}
107
+ {/snippet}
108
+
109
+ {#if items.length === 0}
110
+ <p {...rest} class={cn('text-sm text-base-600 dark:text-base-400', className)}>{empty}</p>
111
+ {:else}
112
+ <ul
113
+ {...rest}
114
+ class={cn('m-0 list-none divide-y divide-base-200 p-0 dark:divide-base-800', className)}
115
+ >
116
+ {#each items as item (item.id)}
117
+ {@const href = link(item)}
118
+ <li>
119
+ {#if href}
120
+ <a {href} onclick={() => onopen?.(item)} class={cn(row, interactive)}>
121
+ {@render content(item)}
122
+ </a>
123
+ {:else if onopen}
124
+ <button
125
+ type="button"
126
+ onclick={() => onopen(item)}
127
+ class={cn(row, interactive, 'w-full cursor-pointer text-left')}
128
+ >
129
+ {@render content(item)}
130
+ </button>
131
+ {:else}
132
+ <div class={row}>{@render content(item)}</div>
133
+ {/if}
134
+ </li>
135
+ {/each}
136
+ </ul>
137
+ {/if}
@@ -0,0 +1,19 @@
1
+ import type { HTMLAttributes } from 'svelte/elements';
2
+ import type { NotificationItem } from './types.js';
3
+ export type NotificationListProps = Omit<HTMLAttributes<HTMLElement>, 'children'> & {
4
+ items: readonly NotificationItem[];
5
+ /** Called when an item is activated, before navigation. Items without a link become buttons. */
6
+ onopen?: (item: NotificationItem) => void;
7
+ /** Shown when there are no items. */
8
+ empty?: string;
9
+ /** Text for the unread marker, for assistive technology. */
10
+ unreadLabel?: string;
11
+ locale?: string;
12
+ /** Time zone for calendar dates; defaults to the runtime's. */
13
+ timeZone?: string;
14
+ /** Reference time for relative dates; defaults to the current time. */
15
+ now?: number;
16
+ };
17
+ declare const NotificationList: import("svelte").Component<NotificationListProps, {}, "">;
18
+ type NotificationList = ReturnType<typeof NotificationList>;
19
+ export default NotificationList;
@@ -0,0 +1,76 @@
1
+ <script lang="ts" module>
2
+ import type { HTMLAttributes } from 'svelte/elements';
3
+ import type { NotificationPushState } from './types.js';
4
+ export type NotificationPushToggleProps = Omit<
5
+ HTMLAttributes<HTMLElement>,
6
+ 'children' | 'onclick'
7
+ > & {
8
+ state: NotificationPushState;
9
+ /** True while a change is in progress. */
10
+ loading?: boolean;
11
+ disabled?: boolean;
12
+ onenable: () => void;
13
+ ondisable: () => void;
14
+ labels?: Partial<{ enable: string; disable: string; unsupported: string; denied: string }>;
15
+ };
16
+ </script>
17
+
18
+ <script lang="ts">
19
+ import { tick, untrack } from 'svelte';
20
+ import { cn } from '../../utils.js';
21
+ import { focus } from '../../styles.js';
22
+ import Button from '../Button.svelte';
23
+
24
+ let {
25
+ state: push,
26
+ loading = false,
27
+ disabled = false,
28
+ onenable,
29
+ ondisable,
30
+ labels,
31
+ class: className,
32
+ ...rest
33
+ }: NotificationPushToggleProps = $props();
34
+
35
+ let button = $state<HTMLButtonElement | HTMLAnchorElement | null>(null);
36
+ let blocked = $state<HTMLParagraphElement | null>(null);
37
+ // When the browser blocks the request, the button is replaced by the explanation.
38
+ // Move focus there so it is neither lost nor left unannounced.
39
+ $effect.pre(() => {
40
+ if (push !== 'denied') return;
41
+ const current = untrack(() => button);
42
+ if (current && document.activeElement === current) void tick().then(() => blocked?.focus());
43
+ });
44
+ </script>
45
+
46
+ {#if push === 'unsupported'}
47
+ <p {...rest} class={cn('text-sm text-base-600 dark:text-base-400', className)}>
48
+ {labels?.unsupported ??
49
+ 'This browser does not support push notifications. On iPhone and iPad, add this site to your Home Screen first.'}
50
+ </p>
51
+ {:else if push === 'denied'}
52
+ <p
53
+ role="status"
54
+ tabindex="-1"
55
+ {...rest}
56
+ bind:this={blocked}
57
+ class={cn('rounded text-sm text-base-600 dark:text-base-400', focus, className)}
58
+ >
59
+ {labels?.denied ??
60
+ 'Notifications are blocked for this site. Allow them in your browser settings to turn on push.'}
61
+ </p>
62
+ {:else}
63
+ <Button
64
+ {...rest}
65
+ bind:ref={button}
66
+ variant={push === 'on' ? 'secondary' : 'primary'}
67
+ {loading}
68
+ disabled={disabled || loading}
69
+ onclick={push === 'on' ? ondisable : onenable}
70
+ class={className}
71
+ >
72
+ {push === 'on'
73
+ ? (labels?.disable ?? 'Turn off push on this device')
74
+ : (labels?.enable ?? 'Turn on push on this device')}
75
+ </Button>
76
+ {/if}
@@ -0,0 +1,19 @@
1
+ import type { HTMLAttributes } from 'svelte/elements';
2
+ import type { NotificationPushState } from './types.js';
3
+ export type NotificationPushToggleProps = Omit<HTMLAttributes<HTMLElement>, 'children' | 'onclick'> & {
4
+ state: NotificationPushState;
5
+ /** True while a change is in progress. */
6
+ loading?: boolean;
7
+ disabled?: boolean;
8
+ onenable: () => void;
9
+ ondisable: () => void;
10
+ labels?: Partial<{
11
+ enable: string;
12
+ disable: string;
13
+ unsupported: string;
14
+ denied: string;
15
+ }>;
16
+ };
17
+ declare const NotificationPushToggle: import("svelte").Component<NotificationPushToggleProps, {}, "">;
18
+ type NotificationPushToggle = ReturnType<typeof NotificationPushToggle>;
19
+ export default NotificationPushToggle;
@@ -0,0 +1,125 @@
1
+ <script lang="ts" module>
2
+ import type { HTMLTableAttributes } from 'svelte/elements';
3
+ import type {
4
+ NotificationChannel,
5
+ NotificationPreference,
6
+ NotificationPreferenceValue
7
+ } from './types.js';
8
+ export type NotificationSettingsProps = Omit<HTMLTableAttributes, 'children' | 'onchange'> & {
9
+ preferences: readonly NotificationPreference[];
10
+ /** Extra delivery channels to offer, e.g. `{ id: 'push', label: 'Push' }`. */
11
+ channels?: readonly NotificationChannel[];
12
+ /** Column heading for the in-app list. */
13
+ inAppLabel?: string;
14
+ /** Action for a category with custom settings. */
15
+ resetLabel?: string;
16
+ disabled?: boolean;
17
+ onchange: (category: string, value: NotificationPreferenceValue) => void;
18
+ };
19
+ </script>
20
+
21
+ <script lang="ts">
22
+ import { cn } from '../../utils.js';
23
+ import Checkbox from '../Checkbox.svelte';
24
+ import Button from '../Button.svelte';
25
+
26
+ let {
27
+ preferences,
28
+ channels = [],
29
+ inAppLabel = 'In app',
30
+ resetLabel = 'Use default',
31
+ disabled = false,
32
+ onchange,
33
+ class: className,
34
+ ...rest
35
+ }: NotificationSettingsProps = $props();
36
+
37
+ function toggleChannel(preference: NotificationPreference, channel: string, on: boolean) {
38
+ const rest = preference.channels.filter((id) => id !== channel);
39
+ onchange(preference.category, on ? [...rest, channel] : rest);
40
+ }
41
+ </script>
42
+
43
+ {#snippet choice(
44
+ label: string,
45
+ checked: boolean,
46
+ unavailable: boolean,
47
+ change: (next: boolean) => void
48
+ )}
49
+ <label
50
+ class={cn(
51
+ 'inline-flex size-10 items-center justify-center',
52
+ unavailable ? 'cursor-not-allowed' : 'cursor-pointer'
53
+ )}
54
+ >
55
+ <Checkbox
56
+ aria-label={label}
57
+ bind:checked={() => checked, () => {}}
58
+ disabled={unavailable}
59
+ onchange={(event) => {
60
+ // A click changes the native input before dispatching change. Restore the
61
+ // committed value now; only a new preference should update the display.
62
+ const next = event.currentTarget.checked;
63
+ event.currentTarget.checked = checked;
64
+ change(next);
65
+ }}
66
+ />
67
+ </label>
68
+ {/snippet}
69
+
70
+ {#if preferences.length > 0}
71
+ <table {...rest} class={cn('w-full border-collapse text-sm', className)}>
72
+ <thead>
73
+ <tr class="text-xs text-base-600 dark:text-base-400">
74
+ <td></td>
75
+ <th scope="col" class="min-w-16 px-2 pb-2 text-center font-normal">{inAppLabel}</th>
76
+ {#each channels as channel (channel.id)}
77
+ <th scope="col" class="min-w-16 px-2 pb-2 text-center font-normal">{channel.label}</th>
78
+ {/each}
79
+ </tr>
80
+ </thead>
81
+ <tbody>
82
+ {#each preferences as preference (preference.category)}
83
+ <tr>
84
+ <th scope="row" class="py-2 pr-3 text-left font-normal text-base-950 dark:text-base-50">
85
+ {preference.title}
86
+ {#if preference.description}
87
+ <span class="block text-xs text-base-600 dark:text-base-400">
88
+ {preference.description}
89
+ </span>
90
+ {/if}
91
+ {#if preference.isDefault === false}
92
+ <div class="mt-0.5">
93
+ <Button
94
+ variant="link"
95
+ class="text-xs"
96
+ {disabled}
97
+ aria-label={`${preference.title}: ${resetLabel}`}
98
+ onclick={() => onchange(preference.category, null)}>{resetLabel}</Button
99
+ >
100
+ </div>
101
+ {/if}
102
+ </th>
103
+ <td class="px-1 text-center">
104
+ {@render choice(
105
+ `${preference.title}: ${inAppLabel}`,
106
+ preference.enabled,
107
+ disabled,
108
+ (next) => onchange(preference.category, next ? null : 'off')
109
+ )}
110
+ </td>
111
+ {#each channels as channel (channel.id)}
112
+ <td class="px-1 text-center">
113
+ {@render choice(
114
+ `${preference.title}: ${channel.label}`,
115
+ preference.enabled && preference.channels.includes(channel.id),
116
+ disabled || !preference.enabled,
117
+ (next) => toggleChannel(preference, channel.id, next)
118
+ )}
119
+ </td>
120
+ {/each}
121
+ </tr>
122
+ {/each}
123
+ </tbody>
124
+ </table>
125
+ {/if}
@@ -0,0 +1,16 @@
1
+ import type { HTMLTableAttributes } from 'svelte/elements';
2
+ import type { NotificationChannel, NotificationPreference, NotificationPreferenceValue } from './types.js';
3
+ export type NotificationSettingsProps = Omit<HTMLTableAttributes, 'children' | 'onchange'> & {
4
+ preferences: readonly NotificationPreference[];
5
+ /** Extra delivery channels to offer, e.g. `{ id: 'push', label: 'Push' }`. */
6
+ channels?: readonly NotificationChannel[];
7
+ /** Column heading for the in-app list. */
8
+ inAppLabel?: string;
9
+ /** Action for a category with custom settings. */
10
+ resetLabel?: string;
11
+ disabled?: boolean;
12
+ onchange: (category: string, value: NotificationPreferenceValue) => void;
13
+ };
14
+ declare const NotificationSettings: import("svelte").Component<NotificationSettingsProps, {}, "">;
15
+ type NotificationSettings = ReturnType<typeof NotificationSettings>;
16
+ export default NotificationSettings;
@@ -0,0 +1,37 @@
1
+ /** Someone a notification is about, e.g. the person who replied. */
2
+ export type NotificationActor = {
3
+ did: string;
4
+ handle?: string;
5
+ displayName?: string;
6
+ avatar?: string;
7
+ };
8
+ export type NotificationItem = {
9
+ id: string;
10
+ title: string;
11
+ body?: string | null;
12
+ /** Where the notification leads. Items without one are not links. */
13
+ uri?: string | null;
14
+ actors?: NotificationActor[];
15
+ /** When it was sent, in milliseconds since the epoch. */
16
+ sendAt: number;
17
+ /** When it was read; empty while unread. */
18
+ readAt?: number | null;
19
+ };
20
+ export type NotificationPreference = {
21
+ category: string;
22
+ title: string;
23
+ description?: string | null;
24
+ /** False when the person turned the category off entirely. */
25
+ enabled: boolean;
26
+ /** Channels, such as `push`, used in addition to the in-app list. */
27
+ channels: string[];
28
+ /** Whether this category uses app defaults. Omit to hide the reset action. */
29
+ isDefault?: boolean;
30
+ };
31
+ export type NotificationChannel = {
32
+ id: string;
33
+ label: string;
34
+ };
35
+ /** Channels to use (empty for in-app only), `'off'` for nothing, or `null` for defaults. */
36
+ export type NotificationPreferenceValue = string[] | 'off' | null;
37
+ export type NotificationPushState = 'unsupported' | 'denied' | 'off' | 'on';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,87 @@
1
+ <script lang="ts">
2
+ import Button from '../components/Button.svelte';
3
+ import Input from '../components/Input.svelte';
4
+ import Label from '../components/Label.svelte';
5
+ import Modal from '../components/Modal.svelte';
6
+ import Textarea from '../components/Textarea.svelte';
7
+ import { recess } from '../styles.js';
8
+
9
+ // The description and caption of an image in an article being written.
10
+ let {
11
+ open = $bindable(false),
12
+ src,
13
+ alt,
14
+ caption,
15
+ onsave,
16
+ onremove,
17
+ onCloseAutoFocus
18
+ }: {
19
+ open?: boolean;
20
+ /** The image, shown so that it can be described while looking at it. */
21
+ src?: string;
22
+ alt: string;
23
+ caption: string;
24
+ onsave: (values: { alt: string; caption: string }) => void;
25
+ onremove: () => void;
26
+ onCloseAutoFocus?: (event: Event) => void;
27
+ } = $props();
28
+
29
+ const id = $props.id();
30
+ const clean = (value: string) => value.replace(/\s*[\r\n]+\s*/g, ' ').trim();
31
+ let altText = $state('');
32
+ let captionText = $state('');
33
+ let wasOpen = false;
34
+ $effect(() => {
35
+ if (open && !wasOpen) {
36
+ altText = alt;
37
+ captionText = caption;
38
+ }
39
+ wasOpen = open;
40
+ });
41
+ </script>
42
+
43
+ <Modal bind:open title="Image" class="max-w-md" {onCloseAutoFocus}>
44
+ <form
45
+ class="grid gap-4"
46
+ onsubmit={(event) => {
47
+ event.preventDefault();
48
+ onsave({ alt: clean(altText), caption: clean(captionText) });
49
+ open = false;
50
+ }}
51
+ >
52
+ {#if src}
53
+ <img
54
+ {src}
55
+ alt=""
56
+ referrerpolicy="no-referrer"
57
+ class="max-h-56 w-full rounded-ui object-contain {recess}"
58
+ />
59
+ {/if}
60
+ <div class="grid gap-1.5">
61
+ <Label for="{id}-alt">Alt text</Label>
62
+ <Textarea
63
+ id="{id}-alt"
64
+ bind:value={altText}
65
+ rows={3}
66
+ class="min-h-20"
67
+ placeholder="Describe this for people who can't see it"
68
+ data-autofocus
69
+ />
70
+ </div>
71
+ <div class="grid gap-1.5">
72
+ <Label for="{id}-caption">Caption</Label>
73
+ <Input id="{id}-caption" bind:value={captionText} placeholder="Shown under the image" />
74
+ </div>
75
+ <div class="flex items-center justify-between gap-3">
76
+ <Button
77
+ variant="ghost"
78
+ class="-ml-3"
79
+ onclick={() => {
80
+ onremove();
81
+ open = false;
82
+ }}>Remove</Button
83
+ >
84
+ <Button type="submit">Save</Button>
85
+ </div>
86
+ </form>
87
+ </Modal>
@@ -0,0 +1,16 @@
1
+ type $$ComponentProps = {
2
+ open?: boolean;
3
+ /** The image, shown so that it can be described while looking at it. */
4
+ src?: string;
5
+ alt: string;
6
+ caption: string;
7
+ onsave: (values: {
8
+ alt: string;
9
+ caption: string;
10
+ }) => void;
11
+ onremove: () => void;
12
+ onCloseAutoFocus?: (event: Event) => void;
13
+ };
14
+ declare const ImageDialog: import("svelte").Component<$$ComponentProps, {}, "open">;
15
+ type ImageDialog = ReturnType<typeof ImageDialog>;
16
+ export default ImageDialog;
@@ -2,7 +2,11 @@ import { Node } from '@tiptap/core';
2
2
  export type ImageNodeOptions = {
3
3
  /** The address to load a destination from; undefined when it cannot be shown. */
4
4
  src: (url: string) => string | undefined;
5
- altLabel: string;
5
+ /** Opens the image at this position for its description and caption. */
6
+ edit: (position: number) => void;
6
7
  };
7
- /** An image on its own line, written to Markdown as `![alt](url)`. */
8
+ /**
9
+ * An image on its own line, written to Markdown as `![alt](url "caption")`. Clicking it, or
10
+ * Enter while it is selected, opens its description and caption.
11
+ */
8
12
  export declare const ImageNode: Node<ImageNodeOptions, any>;
@@ -1,61 +1,62 @@
1
1
  import { Node } from '@tiptap/core';
2
+ import { NodeSelection } from '@tiptap/pm/state';
2
3
  import { IMAGE } from './document.js';
3
- /** An image on its own line, written to Markdown as `![alt](url)`. */
4
+ /**
5
+ * An image on its own line, written to Markdown as `![alt](url "caption")`. Clicking it, or
6
+ * Enter while it is selected, opens its description and caption.
7
+ */
4
8
  export const ImageNode = Node.create({
5
9
  name: IMAGE,
6
10
  group: 'block',
7
11
  atom: true,
8
12
  selectable: true,
9
13
  draggable: true,
10
- addOptions: () => ({ src: () => undefined, altLabel: 'Alt text' }),
14
+ addOptions: () => ({ src: () => undefined, edit: () => { } }),
11
15
  addAttributes: () => ({ url: { default: '' }, alt: { default: '' }, title: { default: '' } }),
12
- parseHTML: () => [{ tag: 'div[data-image]' }],
13
- renderHTML: ({ node }) => ['div', { 'data-image': '' }, node.attrs.alt || ''],
16
+ parseHTML: () => [{ tag: 'figure[data-image]' }],
17
+ renderHTML: ({ node }) => ['figure', { 'data-image': '' }, node.attrs.alt || ''],
14
18
  renderText: ({ node }) => node.attrs.alt || '',
19
+ addKeyboardShortcuts() {
20
+ return {
21
+ Enter: ({ editor }) => {
22
+ const { selection } = editor.state;
23
+ if (!(selection instanceof NodeSelection) || selection.node.type.name !== IMAGE)
24
+ return false;
25
+ if (editor.isEditable)
26
+ this.options.edit(selection.from);
27
+ return true;
28
+ }
29
+ };
30
+ },
15
31
  addNodeView() {
16
32
  return ({ node, editor, getPos }) => {
17
33
  let current = node;
18
- const dom = document.createElement('div');
34
+ const dom = document.createElement('figure');
19
35
  dom.dataset.image = '';
20
36
  dom.contentEditable = 'false';
21
37
  const img = document.createElement('img');
22
38
  img.draggable = false;
23
- const input = document.createElement('input');
24
- input.type = 'text';
25
- input.placeholder = this.options.altLabel;
26
- input.setAttribute('aria-label', this.options.altLabel);
27
- dom.append(img, input);
39
+ img.referrerPolicy = 'no-referrer';
40
+ const caption = document.createElement('figcaption');
41
+ dom.append(img, caption);
28
42
  const show = () => {
29
43
  const src = this.options.src(current.attrs.url) ?? '';
30
- if (img.getAttribute('src') !== src)
31
- img.setAttribute('src', src);
32
- img.alt = current.attrs.alt || '';
33
- if (current.attrs.title)
34
- img.title = current.attrs.title;
44
+ if (src) {
45
+ if (img.getAttribute('src') !== src)
46
+ img.setAttribute('src', src);
47
+ }
35
48
  else
36
- img.removeAttribute('title');
37
- if (document.activeElement !== input)
38
- input.value = current.attrs.alt || '';
39
- input.readOnly = !editor.isEditable;
49
+ img.removeAttribute('src');
50
+ img.alt = current.attrs.alt || '';
51
+ const text = String(current.attrs.title ?? '').trim();
52
+ caption.textContent = text;
53
+ caption.hidden = !text;
40
54
  };
41
55
  show();
42
- input.addEventListener('input', () => {
43
- const pos = getPos();
44
- if (typeof pos !== 'number' || !editor.isEditable)
45
- return;
46
- editor.view.dispatch(editor.state.tr.setNodeMarkup(pos, undefined, { ...current.attrs, alt: input.value }));
47
- });
48
- input.addEventListener('keydown', (event) => {
49
- if (event.key !== 'Enter' && event.key !== 'Escape')
50
- return;
51
- event.preventDefault();
56
+ dom.addEventListener('click', () => {
52
57
  const pos = getPos();
53
- if (typeof pos === 'number')
54
- editor
55
- .chain()
56
- .focus()
57
- .setTextSelection(pos + current.nodeSize)
58
- .run();
58
+ if (typeof pos === 'number' && editor.isEditable)
59
+ this.options.edit(pos);
59
60
  });
60
61
  return {
61
62
  dom,
@@ -66,8 +67,6 @@ export const ImageNode = Node.create({
66
67
  show();
67
68
  return true;
68
69
  },
69
- // Typing in the description stays there.
70
- stopEvent: (event) => event.target === input,
71
70
  ignoreMutation: () => true
72
71
  };
73
72
  };