feat(admin): wire summer-media-pick MediaPickerModal event in PluginField and rebuild boardwalk dist

This commit is contained in:
Jakub Zych
2026-11-27 13:27:27 +01:00
parent 27a600921f
commit 6e4471438e
7 changed files with 115 additions and 16 deletions

View File

@@ -2,11 +2,21 @@
import { computed, inject, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import { api } from '../../../api/client'
import { errorMessage } from '../../../api/files'
import type { AdminMediaItem } from '../../../api/types'
import { fieldTypeScripts } from '../../../app/fieldTypes'
import { currentLocale, t } from '../../../app/i18n'
import MediaPickerModal from '../../media/MediaPickerModal.vue'
import ExtensionFailure from '../../ui/ExtensionFailure.vue'
import type { FieldControlProps } from '../control'
import { FIELD_CHANGE_EVENT, FIELD_UPLOAD_EVENT, FIELD_UPLOAD_RESULT_EVENT, FORM_LOCALE, WIDGET_TIMEOUT } from '../formContext'
import {
FIELD_CHANGE_EVENT,
FIELD_MEDIA_PICK_EVENT,
FIELD_MEDIA_PICK_RESULT_EVENT,
FIELD_UPLOAD_EVENT,
FIELD_UPLOAD_RESULT_EVENT,
FORM_LOCALE,
WIDGET_TIMEOUT,
} from '../formContext'
// A field whose type a plugin registers (pact.HasFormFieldTypes, D-13). The
// type's custom element is created imperatively inside a node Vue never
@@ -40,6 +50,12 @@ const mountPoint = ref<HTMLElement | null>(null)
let element: ValueElement | null = null
let unmounted = false
const mediaPickerOpen = ref(false)
const mediaPickerMode = ref<'all' | 'image' | 'file'>('image')
const mediaPickerFolder = ref<string | undefined>(undefined)
const mediaPickerShowAlt = ref(true)
let activeMediaPickId: string | null = null
const uiLocale = computed(() => formLocale?.value || currentLocale.value)
const optionsJson = computed(() => JSON.stringify(props.field.typeOptions ?? {}))
const disabled = computed(() => props.field.readOnly === true)
@@ -154,6 +170,66 @@ function uploadListener(event: Event): void {
void onUpload(event)
}
function onMediaPick(event: Event): void {
const detail: unknown = (event as CustomEvent<unknown>).detail
if (!element || detail === null || typeof detail !== 'object') {
return
}
const { id, mode, defaultFolder, showAlt } = detail as {
id?: unknown
mode?: unknown
defaultFolder?: unknown
showAlt?: unknown
}
if (typeof id !== 'string' || id === '') {
return
}
activeMediaPickId = id
mediaPickerMode.value = mode === 'file' || mode === 'all' ? mode : 'image'
mediaPickerFolder.value = typeof defaultFolder === 'string' && defaultFolder.trim() !== '' ? defaultFolder.trim() : undefined
mediaPickerShowAlt.value = showAlt !== false
mediaPickerOpen.value = true
}
function onMediaPickSelect(payload: { items: AdminMediaItem[]; alt: string }): void {
const id = activeMediaPickId
activeMediaPickId = null
const chosen = payload.items[0]
if (!id || !chosen || unmounted || !element) {
return
}
element.dispatchEvent(
new CustomEvent(FIELD_MEDIA_PICK_RESULT_EVENT, {
detail: {
id,
item: {
url: chosen.url,
alt: payload.alt || chosen.alt || '',
title: chosen.title || '',
caption: chosen.caption || '',
width: chosen.width ?? null,
height: chosen.height ?? null,
},
},
}),
)
}
function onMediaPickOpenChange(open: boolean): void {
mediaPickerOpen.value = open
if (!open && activeMediaPickId) {
const id = activeMediaPickId
activeMediaPickId = null
if (!unmounted && element) {
element.dispatchEvent(
new CustomEvent(FIELD_MEDIA_PICK_RESULT_EVENT, {
detail: { id, cancelled: true },
}),
)
}
}
}
function onChange(event: Event): void {
const detail: unknown = (event as CustomEvent<unknown>).detail
if (detail !== null && typeof detail === 'object' && Object.hasOwn(detail, 'value')) {
@@ -176,6 +252,7 @@ onMounted(async () => {
syncValue(created, props.modelValue)
created.addEventListener(FIELD_CHANGE_EVENT, onChange)
created.addEventListener(FIELD_UPLOAD_EVENT, uploadListener)
created.addEventListener(FIELD_MEDIA_PICK_EVENT, onMediaPick)
mountPoint.value.append(created)
element = created
status.value = 'ready'
@@ -210,6 +287,7 @@ onBeforeUnmount(() => {
unmounted = true
element?.removeEventListener(FIELD_CHANGE_EVENT, onChange)
element?.removeEventListener(FIELD_UPLOAD_EVENT, uploadListener)
element?.removeEventListener(FIELD_MEDIA_PICK_EVENT, onMediaPick)
element = null
})
</script>
@@ -234,5 +312,13 @@ onBeforeUnmount(() => {
>
<span v-if="status === 'loading'" data-plugin-field-skeleton class="h-[42px] w-full rounded-control bg-skel" aria-hidden="true" />
<div ref="mountPoint" class="contents" />
<MediaPickerModal
:open="mediaPickerOpen"
:mode="mediaPickerMode"
:default-folder="mediaPickerFolder"
:show-alt="mediaPickerShowAlt"
@update:open="onMediaPickOpenChange"
@select="onMediaPickSelect"
/>
</div>
</template>

View File

@@ -141,6 +141,19 @@ export const FIELD_UPLOAD_EVENT = 'summer-upload'
*/
export const FIELD_UPLOAD_RESULT_EVENT = 'summer-upload-result'
/**
* Event a plugin field type's element dispatches (bubbling) to open the host
* admin Media Picker modal, with detail {id, mode?, defaultFolder?, showAlt?}.
*/
export const FIELD_MEDIA_PICK_EVENT = 'summer-media-pick'
/**
* Event the SPA dispatches on the element when the Media Picker modal closes,
* with detail {id, item: {url, alt, title, caption, width, height}} on confirm
* or {id, cancelled: true} on dismiss.
*/
export const FIELD_MEDIA_PICK_RESULT_EVENT = 'summer-media-pick-result'
/** How long a widget waits for its custom element to be defined. */
export const WIDGET_TIMEOUT = 5000

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -1,2 +1,2 @@
const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["./editor-uDgt9VKj.js","./index-Vcq4gwWv.js"])))=>i.map(i=>d[i]);
import{_ as r}from"./index-BFpmmD32.js";function t(){return r(()=>import("./editor-uDgt9VKj.js"),__vite__mapDeps([0,1]),import.meta.url)}export{t as importCodeEditor};
import{_ as r}from"./index-Dbeb3pG-.js";function t(){return r(()=>import("./editor-uDgt9VKj.js"),__vite__mapDeps([0,1]),import.meta.url)}export{t as importCodeEditor};

View File

@@ -8,7 +8,7 @@
<meta name="summer-admin-build" content="__SUMMER_ADMIN_BUILD__" />
<link rel="icon" href="./favicon.ico" sizes="48x48" />
<title>SummerCMS</title>
<script type="module" crossorigin src="./assets/index-BFpmmD32.js"></script>
<script type="module" crossorigin src="./assets/index-Dbeb3pG-.js"></script>
<link rel="stylesheet" crossorigin href="./assets/index-Dc12VzLj.css">
</head>
<body>