@openpen/module-api 1.0.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 (44) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +99 -0
  3. package/package.json +53 -0
  4. package/src/define-module.ts +55 -0
  5. package/src/host/color-utils.ts +18 -0
  6. package/src/host/event-bus.ts +37 -0
  7. package/src/host/host-commands.ts +9 -0
  8. package/src/host/index.ts +53 -0
  9. package/src/host/module-context-registry.ts +34 -0
  10. package/src/host/passthrough.ts +10 -0
  11. package/src/host/popup-anchor.ts +14 -0
  12. package/src/host/slot-entries.ts +9 -0
  13. package/src/host/stroke-store.ts +20 -0
  14. package/src/host/stroke-style.ts +15 -0
  15. package/src/index.ts +106 -0
  16. package/src/inject-keys.ts +143 -0
  17. package/src/locale.ts +52 -0
  18. package/src/slots.ts +135 -0
  19. package/src/types/contributions.ts +298 -0
  20. package/src/types/control-bar-layout.ts +174 -0
  21. package/src/types/module.ts +225 -0
  22. package/src/types/notification.ts +40 -0
  23. package/src/types/tool.ts +82 -0
  24. package/src/uikit/components/.gitkeep +0 -0
  25. package/src/uikit/components/AppBanner.vue +131 -0
  26. package/src/uikit/components/AppButton.vue +135 -0
  27. package/src/uikit/components/AppDialog.vue +393 -0
  28. package/src/uikit/components/AppPopover.vue +268 -0
  29. package/src/uikit/components/AppSegmented.vue +126 -0
  30. package/src/uikit/components/AppSelect.vue +235 -0
  31. package/src/uikit/components/AppSlider.vue +204 -0
  32. package/src/uikit/components/AppTabs.vue +163 -0
  33. package/src/uikit/components/AppToggle.vue +96 -0
  34. package/src/uikit/components/AppTooltip.vue +118 -0
  35. package/src/uikit/components/DialogHost.vue +260 -0
  36. package/src/uikit/composables/dialog-internal-keys.ts +17 -0
  37. package/src/uikit/composables/useDialog.ts +141 -0
  38. package/src/uikit/composables/useDialogPluginComponent.ts +27 -0
  39. package/src/uikit/index.ts +79 -0
  40. package/src/uikit/internal.ts +12 -0
  41. package/src/uikit/primitives.ts +150 -0
  42. package/src/uikit/tokens.css +205 -0
  43. package/src/use-module-context.ts +50 -0
  44. package/src/validation.ts +31 -0
@@ -0,0 +1,135 @@
1
+ <script setup lang="ts">
2
+ /**
3
+ * AppButton — control-bar button wrapper (Layer 1).
4
+ *
5
+ * Provides the standard 36×36 rounded button appearance that matches the
6
+ * host's control-bar buttons, so plugins do not need to replicate the
7
+ * visual design manually.
8
+ *
9
+ * Disabled state uses aria-disabled + a CSS class rather than the HTML
10
+ * `disabled` attribute, because `disabled` removes pointer events and would
11
+ * swallow the tooltip hover that this component is expected to keep alive.
12
+ */
13
+
14
+ // ── Public API ────────────────────────────────────────────────────────────────
15
+
16
+ const props = withDefaults(defineProps<{
17
+ variant?: 'default' | 'danger'
18
+ active?: boolean
19
+ disabled?: boolean
20
+ tooltip?: string
21
+ ariaLabel?: string
22
+ }>(), {
23
+ variant: 'default',
24
+ active: false,
25
+ disabled: false,
26
+ tooltip: undefined,
27
+ ariaLabel: undefined,
28
+ })
29
+
30
+ const emit = defineEmits<{
31
+ click: []
32
+ }>()
33
+
34
+ function handleClick() {
35
+ if (!props.disabled) {
36
+ emit('click')
37
+ }
38
+ }
39
+ </script>
40
+
41
+ <template>
42
+ <button
43
+ class="app-btn"
44
+ :class="{
45
+ active,
46
+ danger: variant === 'danger',
47
+ 'app-btn-disabled': disabled,
48
+ }"
49
+ :data-tip="tooltip"
50
+ :aria-label="ariaLabel"
51
+ :aria-disabled="disabled || undefined"
52
+ @click="handleClick"
53
+ >
54
+ <slot />
55
+ </button>
56
+ </template>
57
+
58
+ <style scoped>
59
+ .app-btn {
60
+ display: flex;
61
+ align-items: center;
62
+ justify-content: center;
63
+ width: 36px;
64
+ height: 36px;
65
+ border: none;
66
+ border-radius: var(--openpen-radius-md);
67
+ background: transparent;
68
+ color: var(--openpen-color-text-dim);
69
+ cursor: pointer;
70
+ transition: background var(--openpen-duration-fast), color var(--openpen-duration-fast), transform var(--openpen-duration-fast);
71
+ position: relative;
72
+ flex-shrink: 0;
73
+ padding: 0;
74
+ }
75
+
76
+ .app-btn:hover {
77
+ background: var(--openpen-color-control-hover);
78
+ color: var(--openpen-color-text-primary);
79
+ }
80
+
81
+ .app-btn:active {
82
+ transform: scale(0.88);
83
+ }
84
+
85
+ .app-btn.active {
86
+ background: var(--openpen-color-accent-bg);
87
+ color: var(--openpen-color-accent);
88
+ box-shadow: 0 0 0 1px var(--openpen-color-accent-glow);
89
+ }
90
+
91
+ .app-btn.danger {
92
+ color: var(--openpen-color-danger);
93
+ }
94
+
95
+ .app-btn.danger:hover {
96
+ background: var(--openpen-color-danger-hover-bg);
97
+ color: var(--openpen-color-danger-hover);
98
+ }
99
+
100
+ /* Disabled: keeps pointer events alive so the tooltip ::after hover still fires. */
101
+ .app-btn.app-btn-disabled {
102
+ cursor: not-allowed;
103
+ }
104
+
105
+ /* Dim only the button content — tooltip pseudo-element stays at full opacity. */
106
+ .app-btn.app-btn-disabled > * {
107
+ opacity: 0.30;
108
+ }
109
+
110
+ /* Tooltip */
111
+ .app-btn[data-tip]::after {
112
+ content: attr(data-tip);
113
+ position: absolute;
114
+ bottom: calc(100% + 8px);
115
+ left: 50%;
116
+ transform: translateX(-50%);
117
+ background: var(--openpen-color-tooltip-bg);
118
+ border: 1px solid var(--openpen-color-tooltip-border);
119
+ color: var(--openpen-color-tooltip-text);
120
+ font-size: 11.5px;
121
+ font-weight: 500;
122
+ white-space: nowrap;
123
+ padding: 5px 10px;
124
+ border-radius: var(--openpen-radius-sm);
125
+ pointer-events: none;
126
+ opacity: 0;
127
+ transition: opacity var(--openpen-duration-fast);
128
+ box-shadow: var(--openpen-shadow-sm);
129
+ z-index: 41;
130
+ }
131
+
132
+ .app-btn[data-tip]:hover::after {
133
+ opacity: 1;
134
+ }
135
+ </style>
@@ -0,0 +1,393 @@
1
+ <script setup lang="ts">
2
+ /**
3
+ * AppDialog — High-level wrapper (Layer 1).
4
+ *
5
+ * Plugin authors use this component without needing to know about inject
6
+ * keys, Reka UI, modal manager mutual exclusion, or the animating guard.
7
+ *
8
+ * Public API is stable: switching the underlying headless library in the
9
+ * future does not break the plugin author's code.
10
+ *
11
+ * Implementation notes:
12
+ * - `open` prop is the single source of truth (controlled mode). The
13
+ * component emits `update:open` for two-way binding. Internally it also
14
+ * registers with the modal manager for mutual exclusion (same pattern as
15
+ * AppPopover): when `open` turns true we call manager.open(); when
16
+ * another panel causes the manager to close us we emit update:open(false).
17
+ * - `persistent`: when true, ESC and backdrop clicks are suppressed via
18
+ * @interact-outside.prevent and @escape-key-down.prevent on DialogContent.
19
+ * - `danger`: adds class `openpen-modal-danger` on the content element as a
20
+ * styling hook for destructive-action dialogs (no default style yet).
21
+ * - animating guard: auto-close during ControlBar scaleY animation.
22
+ * - usePassthroughGuard: registered on the dialog content element so the
23
+ * transparent Electron window remains clickable when dialog is open.
24
+ * - Styles are unscoped because DialogContent is teleported outside the
25
+ * component's DOM subtree (same reason as AppPopover).
26
+ *
27
+ * CSS class names (.openpen-modal-*) are intentionally kept as the carried-over
28
+ * internal styling tokens — renaming carries no DX benefit and risks accidental
29
+ * style breakage in any code path still referencing them.
30
+ */
31
+ import { computed, inject, onBeforeUnmount, ref, useTemplateRef, watch } from 'vue'
32
+ import {
33
+ DialogRoot,
34
+ DialogTrigger,
35
+ DialogPortal,
36
+ DialogContent,
37
+ DialogOverlay,
38
+ DialogClose,
39
+ DialogTitle,
40
+ DialogDescription,
41
+ } from '../primitives'
42
+ // Inject keys MUST come from the package barrel — see AppPopover.vue
43
+ // header comment for why relative imports break Symbol identity in prod.
44
+ import {
45
+ MODAL_MANAGER_KEY,
46
+ CONTROL_BAR_ANIMATING_KEY,
47
+ HOST_DIALOG_OPEN_COUNT_KEY,
48
+ } from '@openpen/module-api'
49
+ import { usePassthroughGuard } from '../../host/passthrough'
50
+
51
+ // ── OpenPen-owned types (MUST NOT reference reka-ui types) ───────────────────
52
+
53
+ export interface AppDialogProps {
54
+ /** Globally unique modal id; used for modal-stack mutual exclusion. */
55
+ modalId: string
56
+ /** Dialog title displayed in the dialog header. */
57
+ title: string
58
+ /** Controlled open state. The parent must handle update:open to toggle. */
59
+ open: boolean
60
+ /**
61
+ * When true, ESC key and backdrop clicks do not close the dialog.
62
+ * Use for confirmations that require an explicit button press.
63
+ */
64
+ persistent?: boolean
65
+ /**
66
+ * When true, adds the `openpen-modal-danger` CSS class on the content
67
+ * element — a hook for styling destructive-action dialogs.
68
+ */
69
+ danger?: boolean
70
+ }
71
+
72
+ export interface AppDialogSlots {
73
+ /**
74
+ * Trigger slot; scope exposes active state + toggle / open / close actions.
75
+ * MUST NOT write @click="toggle" on the trigger — DialogTrigger's as-child
76
+ * handles activation automatically; toggle/open/close are escape hatches for
77
+ * programmatic control only.
78
+ */
79
+ trigger: (scope: {
80
+ active: boolean
81
+ toggle: () => void
82
+ open: () => void
83
+ close: () => void
84
+ }) => unknown
85
+ /** Dialog body content slot. */
86
+ default: () => unknown
87
+ /** Optional footer slot (action buttons etc.). */
88
+ footer?: () => unknown
89
+ }
90
+
91
+ const props = withDefaults(defineProps<AppDialogProps>(), {
92
+ persistent: false,
93
+ danger: false,
94
+ })
95
+
96
+ const emit = defineEmits<{
97
+ 'update:open': [value: boolean]
98
+ }>()
99
+
100
+ defineSlots<AppDialogSlots>()
101
+
102
+ // ── 1. Inject host context (plugin author never sees these) ───────────────────
103
+
104
+ const modalManager = inject(MODAL_MANAGER_KEY)
105
+ const animating = inject(CONTROL_BAR_ANIMATING_KEY)
106
+ const dialogOpenCount = inject(HOST_DIALOG_OPEN_COUNT_KEY, null)
107
+
108
+ // Track whether THIS instance currently holds a count, so we never double
109
+ // increment / decrement (e.g. on rapid open / close cycles or unmount-while-open).
110
+ let counted = false
111
+
112
+ function acquireCount() {
113
+ if (counted || !dialogOpenCount) return
114
+ dialogOpenCount.value += 1
115
+ counted = true
116
+ }
117
+
118
+ function releaseCount() {
119
+ if (!counted || !dialogOpenCount) return
120
+ dialogOpenCount.value -= 1
121
+ counted = false
122
+ }
123
+
124
+ onBeforeUnmount(releaseCount)
125
+
126
+ // ── 2. Modal manager mutual exclusion + controlled open prop ─────────────────
127
+ //
128
+ // The `open` prop is the canonical state. We watch it and push changes into
129
+ // the modal manager. When the manager closes us (e.g. another panel opens),
130
+ // we detect that via the computed isManagerOpen and emit update:open(false).
131
+
132
+ function openDialog() {
133
+ modalManager?.open(props.modalId)
134
+ emit('update:open', true)
135
+ }
136
+
137
+ function closeDialog() {
138
+ modalManager?.close(props.modalId)
139
+ emit('update:open', false)
140
+ }
141
+
142
+ function toggleDialog() {
143
+ if (props.open) {
144
+ closeDialog()
145
+ } else {
146
+ openDialog()
147
+ }
148
+ }
149
+
150
+ // When the prop turns true: register with the manager + bump host dialog count.
151
+ // When it turns false: unregister + release count. Keeps everything in sync.
152
+ watch(
153
+ () => props.open,
154
+ (newOpen) => {
155
+ if (newOpen) {
156
+ modalManager?.open(props.modalId)
157
+ acquireCount()
158
+ } else {
159
+ modalManager?.close(props.modalId)
160
+ releaseCount()
161
+ }
162
+ },
163
+ { immediate: true }
164
+ )
165
+
166
+ // When the manager closes us (another panel opened), emit update:open(false)
167
+ // so the parent's v-model updates.
168
+ const isManagerOpen = computed(() => modalManager?.isOpen(props.modalId) ?? props.open)
169
+
170
+ watch(isManagerOpen, (managerOpen) => {
171
+ if (!managerOpen && props.open) {
172
+ emit('update:open', false)
173
+ }
174
+ })
175
+
176
+ // Effective open state passed to Reka UI's DialogRoot.
177
+ const isOpen = computed(() => props.open && isManagerOpen.value)
178
+
179
+ // Sync Reka UI's internal close events (ESC, overlay click) back to caller
180
+ // unless `persistent` suppresses them.
181
+ function onUpdateOpen(v: boolean) {
182
+ if (!v && props.persistent) {
183
+ // Persistent mode: ignore Reka UI close attempts; stay open.
184
+ return
185
+ }
186
+ if (v) {
187
+ openDialog()
188
+ } else {
189
+ closeDialog()
190
+ }
191
+ }
192
+
193
+ // ── 3. ControlBar animation guard — auto-close during transitions ─────────────
194
+
195
+ watch(
196
+ () => animating?.value,
197
+ (isAnimating) => {
198
+ if (isAnimating && props.open) {
199
+ closeDialog()
200
+ }
201
+ }
202
+ )
203
+
204
+ // ── 4. Passthrough guard integration ─────────────────────────────────────────
205
+ //
206
+ // DialogContent is a Vue component; normalize to HTMLElement via watcher.
207
+
208
+ const contentInstance = useTemplateRef<HTMLElement | { $el?: HTMLElement } | null>('content')
209
+ const contentEl = ref<HTMLElement | null>(null)
210
+
211
+ watch(contentInstance, (instance) => {
212
+ if (!instance) {
213
+ contentEl.value = null
214
+ } else if (instance instanceof HTMLElement) {
215
+ contentEl.value = instance
216
+ } else if (typeof instance === 'object' && '$el' in instance && instance.$el instanceof HTMLElement) {
217
+ contentEl.value = instance.$el
218
+ } else {
219
+ contentEl.value = null
220
+ }
221
+ })
222
+
223
+ usePassthroughGuard(contentEl)
224
+ </script>
225
+
226
+ <template>
227
+ <DialogRoot :open="isOpen" @update:open="onUpdateOpen">
228
+ <DialogTrigger as-child>
229
+ <slot
230
+ name="trigger"
231
+ :active="isOpen"
232
+ :toggle="toggleDialog"
233
+ :open="openDialog"
234
+ :close="closeDialog"
235
+ />
236
+ </DialogTrigger>
237
+ <DialogPortal>
238
+ <DialogOverlay class="openpen-modal-overlay" />
239
+ <DialogContent
240
+ ref="content"
241
+ class="openpen-modal-content openpen-interactive"
242
+ :class="{ 'openpen-modal-danger': danger }"
243
+ @interact-outside="(e: Event) => persistent && e.preventDefault()"
244
+ @escape-key-down="(e: KeyboardEvent) => persistent && e.preventDefault()"
245
+ >
246
+ <DialogTitle class="openpen-modal-title">{{ title }}</DialogTitle>
247
+ <DialogDescription class="openpen-modal-description sr-only" />
248
+ <div class="openpen-modal-body">
249
+ <slot />
250
+ </div>
251
+ <div v-if="$slots.footer" class="openpen-modal-footer">
252
+ <slot name="footer" />
253
+ </div>
254
+ <DialogClose class="openpen-modal-close" aria-label="Close">
255
+ <svg width="14" height="14" viewBox="0 0 14 14" fill="none" aria-hidden="true">
256
+ <path d="M1 1L13 13M13 1L1 13" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
257
+ </svg>
258
+ </DialogClose>
259
+ </DialogContent>
260
+ </DialogPortal>
261
+ </DialogRoot>
262
+ </template>
263
+
264
+ <!-- Styles MUST be unscoped — Vue scoped CSS does not follow Teleport targets,
265
+ so .openpen-modal-* classes would not receive the scoped data-v-* attribute
266
+ when rendered inside the portal. -->
267
+ <style>
268
+ /* ── Overlay (transparent click absorber, NOT a dim backdrop) ─────────────────
269
+ OpenPen's overlay window is transparent + frameless. A traditional dim/blur
270
+ backdrop renders as a small dark patch only over the visible chrome — visually
271
+ jarring. The overlay is therefore kept fully transparent here; its only job
272
+ is to absorb outside clicks (so Reka can dispatch interact-outside → close
273
+ when not persistent) and to protect the canvas from stray clicks while the
274
+ dialog is open. Host containers (e.g. ControlBar) render their own "locked"
275
+ visual state via HOST_DIALOG_OPEN_COUNT_KEY. */
276
+ .openpen-modal-overlay {
277
+ position: fixed;
278
+ inset: 0;
279
+ background: transparent;
280
+ z-index: 200;
281
+ pointer-events: auto;
282
+ }
283
+
284
+ /* ── Dialog content panel ────────────────────────────────────────────────── */
285
+ .openpen-modal-content {
286
+ position: fixed;
287
+ top: 50%;
288
+ left: 50%;
289
+ transform: translate(-50%, -50%);
290
+ z-index: 201;
291
+ min-width: 320px;
292
+ max-width: min(480px, calc(100vw - 48px));
293
+ max-height: calc(100vh - 96px);
294
+ overflow-y: auto;
295
+
296
+ background: var(--openpen-color-surface-popup);
297
+ border: 1px solid var(--openpen-color-border-hi);
298
+ border-radius: var(--openpen-radius-lg);
299
+ box-shadow: var(--openpen-shadow);
300
+ backdrop-filter: var(--openpen-blur);
301
+ -webkit-backdrop-filter: var(--openpen-blur);
302
+
303
+ color: var(--openpen-color-text-primary);
304
+ font-size: 13px;
305
+ padding: var(--openpen-space-lg);
306
+ pointer-events: auto;
307
+ }
308
+
309
+ /* ── Title ───────────────────────────────────────────────────────────────── */
310
+ .openpen-modal-title {
311
+ font-size: 15px;
312
+ font-weight: 600;
313
+ color: var(--openpen-color-text-primary);
314
+ margin: 0 0 var(--openpen-space-md) 0;
315
+ padding-right: 28px; /* leave room for close button */
316
+ line-height: 1.4;
317
+ }
318
+
319
+ /* ── Visually hidden description (a11y) ──────────────────────────────────── */
320
+ .openpen-modal-description.sr-only {
321
+ position: absolute;
322
+ width: 1px;
323
+ height: 1px;
324
+ padding: 0;
325
+ margin: -1px;
326
+ overflow: hidden;
327
+ clip: rect(0, 0, 0, 0);
328
+ white-space: nowrap;
329
+ border-width: 0;
330
+ }
331
+
332
+ /* ── Body ────────────────────────────────────────────────────────────────── */
333
+ .openpen-modal-body {
334
+ color: var(--openpen-color-text-primary);
335
+ }
336
+
337
+ /* ── Footer ──────────────────────────────────────────────────────────────── */
338
+ .openpen-modal-footer {
339
+ display: flex;
340
+ justify-content: flex-end;
341
+ gap: var(--openpen-space-sm);
342
+ margin-top: var(--openpen-space-lg);
343
+ padding-top: var(--openpen-space-md);
344
+ border-top: 1px solid var(--openpen-color-border);
345
+ }
346
+
347
+ /* ── Close button ────────────────────────────────────────────────────────── */
348
+ .openpen-modal-close {
349
+ position: absolute;
350
+ top: var(--openpen-space-md);
351
+ right: var(--openpen-space-md);
352
+ width: 24px;
353
+ height: 24px;
354
+ display: flex;
355
+ align-items: center;
356
+ justify-content: center;
357
+ border: none;
358
+ background: transparent;
359
+ color: var(--openpen-color-text-dim);
360
+ border-radius: var(--openpen-radius-sm);
361
+ cursor: pointer;
362
+ transition: background var(--openpen-duration-fast), color var(--openpen-duration-fast);
363
+ }
364
+
365
+ .openpen-modal-close:hover {
366
+ background: var(--openpen-color-control-hover);
367
+ color: var(--openpen-color-text-primary);
368
+ }
369
+
370
+ /* ── Open / close animations ─────────────────────────────────────────────────
371
+ Reka UI's Presence keeps the element in the DOM until the CSS animation
372
+ ends, so [data-state="closed"] animations DO play on close.
373
+ Pattern matches AppPopover keyframes (200ms bounce enter, 150ms ease leave). */
374
+
375
+ /* Overlay is always transparent — no fade-in/out animation needed.
376
+ Only the dialog content panel animates. */
377
+
378
+ .openpen-modal-content[data-state='open'] {
379
+ animation: openpen-modal-in 200ms cubic-bezier(0.34, 1.56, 0.64, 1);
380
+ }
381
+ .openpen-modal-content[data-state='closed'] {
382
+ animation: openpen-modal-out 150ms ease forwards;
383
+ }
384
+
385
+ @keyframes openpen-modal-in {
386
+ from { opacity: 0; transform: translate(-50%, -50%) scale(0.92); }
387
+ to { opacity: 1; transform: translate(-50%, -50%) scale(1); }
388
+ }
389
+ @keyframes openpen-modal-out {
390
+ from { opacity: 1; transform: translate(-50%, -50%) scale(1); }
391
+ to { opacity: 0; transform: translate(-50%, -50%) scale(0.92); }
392
+ }
393
+ </style>