feat(admin): colour picker, mail image field and the image picker with insert

- insertImage escapes markdown/html/css/text snippets per UI-SPEC C4
- MailImagePicker with canUpload-gated upload, radiogroup tiles, insert/choose modes
- ColorPickerField, MailImageField, markdown/codeeditor Insert image wiring

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-11-13 13:34:54 +01:00
parent 48e87b7543
commit 68e1d36fd5
8 changed files with 734 additions and 35 deletions

View File

@@ -1,7 +1,9 @@
<script setup lang="ts">
import { computed, nextTick, onBeforeUnmount, onMounted, ref, shallowRef, watch } from 'vue'
import { Info } from '@lucide/vue'
import { ImagePlus, Info } from '@lucide/vue'
import { t } from '../../../app/i18n'
import MailImagePicker from '../../mail/MailImagePicker.vue'
import { insertImage } from '../../mail/insertImage'
import { controlClass, type FieldControlProps } from '../control'
import type { CodeEditorHandle, CodeEditorLanguage } from '../codeeditor/editor'
@@ -37,6 +39,7 @@ const status = ref<Status>('loading')
const fallbackText = ref('')
const mountEl = ref<HTMLElement | null>(null)
const editor = shallowRef<CodeEditorHandle | null>(null)
const pickerOpen = ref(false)
let module: EditorModule | null = null
let disposed = false
@@ -163,6 +166,10 @@ function insert(text: string): void {
}
}
function onInsert(payload: { url: string; alt: string }): void {
insert(insertImage(language.value, payload.url, payload.alt))
}
defineExpose({ insert })
</script>
@@ -207,9 +214,17 @@ defineExpose({ insert })
class="flex flex-wrap items-center gap-1 border-b border-border bg-subtle p-1"
data-codeeditor-toolbar
>
<!-- Insert image button wired in Task 5 -->
<slot name="toolbar" :insert="insert" />
<button
type="button"
data-codeeditor-insert-image
class="inline-flex h-8 items-center gap-1.5 rounded-pager px-2 text-[13px] text-muted transition-colors duration-150 ease-out hover:bg-hover hover:text-text"
@click="pickerOpen = true"
>
<ImagePlus :size="16" aria-hidden="true" />
{{ t('backend::lang.mail.picker.insert_button') }}
</button>
</div>
<div ref="mountEl" data-codeeditor-mount />
<MailImagePicker v-if="mailImages" v-model:open="pickerOpen" mode="insert" @insert="onInsert" />
</div>
</template>

View File

@@ -1,11 +1,63 @@
<script setup lang="ts">
// Stub for TDD RED (35-03 Task 5).
import type { FieldControlProps } from '../control'
import { computed, ref, watch } from 'vue'
import { t } from '../../../app/i18n'
import { controlClass, type FieldControlProps } from '../control'
defineProps<FieldControlProps>()
defineEmits<{ 'update:modelValue': [value: string] }>()
// type: colorpicker (UI-SPEC C2): native swatch plus #rrggbb text. The server
// validates; the client only syncs a full #rrggbb into the swatch.
const props = defineProps<FieldControlProps>()
const emit = defineEmits<{ 'update:modelValue': [value: string] }>()
const HEX = /^#[0-9a-fA-F]{6}$/
const text = computed(() => (props.modelValue === null || props.modelValue === undefined ? '' : String(props.modelValue)))
const lastValid = ref(HEX.test(text.value) ? text.value.toLowerCase() : '#ffffff')
watch(text, (value) => {
if (HEX.test(value)) {
lastValid.value = value.toLowerCase()
}
})
const swatch = computed(() => (HEX.test(text.value) ? text.value.toLowerCase() : lastValid.value))
function onSwatch(event: Event): void {
const value = (event.target as HTMLInputElement).value
lastValid.value = value.toLowerCase()
emit('update:modelValue', value)
}
function onText(event: Event): void {
const value = (event.target as HTMLInputElement).value
if (HEX.test(value)) {
lastValid.value = value.toLowerCase()
}
emit('update:modelValue', value)
}
</script>
<template>
<div data-colorpicker-stub />
<div class="flex items-center gap-2" data-colorpicker>
<input
type="color"
data-colorpicker-swatch
:value="swatch"
:aria-label="t('backend::lang.colorpicker.pick', { field: field.label ?? field.name })"
class="size-input shrink-0 cursor-pointer rounded-control border border-border-strong bg-surface p-1"
@input="onSwatch"
/>
<input
:id="controlId"
type="text"
data-colorpicker-text
:value="text"
maxlength="7"
spellcheck="false"
:placeholder="t('backend::lang.colorpicker.not_set')"
:aria-describedby="describedBy || undefined"
:aria-invalid="invalid ? 'true' : undefined"
:class="[controlClass(invalid), 'h-input font-mono text-[13px]']"
@input="onText"
/>
</div>
</template>

View File

@@ -0,0 +1,94 @@
<script setup lang="ts">
import { computed, ref, watch } from 'vue'
import { Image } from '@lucide/vue'
import { api } from '../../../api/client'
import type { AdminMailImage } from '../../../api/types'
import { t } from '../../../app/i18n'
import MailImagePicker from '../../mail/MailImagePicker.vue'
import Button from '../../ui/Button.vue'
import type { FieldControlProps } from '../control'
// type: mailimage (UI-SPEC C11): library image id or null for branding logo.
const props = defineProps<FieldControlProps>()
const emit = defineEmits<{ 'update:modelValue': [value: number | null] }>()
const pickerOpen = ref(false)
const image = ref<AdminMailImage | null>(null)
const missing = ref(false)
const loading = ref(false)
const id = computed(() => {
const value = props.modelValue
if (value === null || value === undefined || value === '') {
return null
}
const n = Number(value)
return Number.isInteger(n) && n > 0 ? n : null
})
async function load(): Promise<void> {
const current = id.value
if (current === null) {
image.value = null
missing.value = false
return
}
loading.value = true
missing.value = false
try {
const { data, response } = await api.GET('/mail/images/{id}', { params: { path: { id: current } } })
if (data) {
image.value = data.data
missing.value = false
} else {
image.value = null
missing.value = response.status === 404
}
} catch {
image.value = null
missing.value = true
} finally {
loading.value = false
}
}
watch(id, () => {
void load()
}, { immediate: true })
function onChoose(chosenId: number): void {
emit('update:modelValue', chosenId)
}
function onRemove(): void {
emit('update:modelValue', null)
}
</script>
<template>
<div class="flex items-center gap-4" data-mailimage>
<div class="flex size-[96px] shrink-0 items-center justify-center overflow-hidden rounded-inner border border-border bg-subtle">
<img
v-if="image"
:src="image.thumbUrl || image.url"
:alt="image.title || image.fileName"
class="size-full object-contain"
/>
<Image v-else :size="24" class="text-muted" aria-hidden="true" />
</div>
<div class="flex flex-col gap-2">
<p v-if="image" class="text-[14px]">{{ image.fileName }}</p>
<p v-else class="text-[13px] text-muted">{{ t('backend::lang.mailimage.empty') }}</p>
<div class="flex gap-2">
<Button variant="outline" size="sm" data-mailimage-choose @click="pickerOpen = true">
{{ image ? t('backend::lang.mailimage.change') : t('backend::lang.mailimage.choose') }}
</Button>
<Button v-if="image || id !== null" variant="ghost" size="sm" data-mailimage-remove @click="onRemove">
{{ t('backend::lang.mailimage.remove') }}
</Button>
</div>
<p v-if="missing" data-mailimage-missing class="text-[13px] text-danger">{{ t('backend::lang.mailimage.missing') }}</p>
</div>
<MailImagePicker v-model:open="pickerOpen" mode="choose" @choose="onChoose" />
</div>
</template>

View File

@@ -1,6 +1,10 @@
<script setup lang="ts">
import { computed, onBeforeUnmount, ref, watch } from 'vue'
import { computed, nextTick, onBeforeUnmount, ref, watch } from 'vue'
import { ImagePlus } from '@lucide/vue'
import { api } from '../../../api/client'
import { t } from '../../../app/i18n'
import MailImagePicker from '../../mail/MailImagePicker.vue'
import { insertImage } from '../../mail/insertImage'
import { controlAttributes, controlClass, type FieldControlProps } from '../control'
// Source editor for type: markdown (and, through MLMarkdownField, for the
@@ -10,6 +14,7 @@ import { controlAttributes, controlClass, type FieldControlProps } from '../cont
// 422 and is shown as a text notice. The pane's raw-HTML binding reads
// sanitizedHtml, which is assigned only from a 2xx answer's data.html; the
// markdown source is never bound as HTML.
// Phase 35: mailImages replaces the Preview toggle with Insert image (C3).
const props = defineProps<FieldControlProps>()
const emit = defineEmits<{ 'update:modelValue': [value: string] }>()
@@ -20,10 +25,13 @@ const rowsBySize: Record<string, number> = { tiny: 2, small: 4, large: 8, huge:
const rows = computed(() => rowsBySize[props.field.size ?? ''] ?? 8)
const text = computed(() => (props.modelValue === null || props.modelValue === undefined ? '' : String(props.modelValue)))
const attrs = computed(() => controlAttributes(props.field))
const mailImages = computed(() => !!props.field.mailImages)
const preview = ref(false)
const sanitizedHtml = ref('')
const previewError = ref('')
const loading = ref(false)
const pickerOpen = ref(false)
const area = ref<HTMLTextAreaElement | null>(null)
let sequence = 0
let previewTimer: ReturnType<typeof setTimeout> | null = null
@@ -84,12 +92,29 @@ function togglePreview() {
if (preview.value) {
void renderPreview()
} else {
// Drop any answer still in flight for the closed pane.
sequence++
loading.value = false
}
}
function onInsert(payload: { url: string; alt: string }): void {
const snippet = insertImage('markdown', payload.url, payload.alt)
const el = area.value
const current = text.value
const start = el?.selectionStart ?? current.length
const end = el?.selectionEnd ?? start
const next = current.slice(0, start) + snippet + current.slice(end)
emit('update:modelValue', next)
void nextTick(() => {
if (!el) {
return
}
el.focus()
const caret = start + snippet.length
el.setSelectionRange(caret, caret)
})
}
watch(text, () => {
if (!preview.value) {
return
@@ -109,16 +134,29 @@ onBeforeUnmount(() => {
<template>
<div class="flex flex-col gap-1.5">
<button
type="button"
data-markdown-preview
class="self-start rounded-pager px-2 py-1 text-[13px] text-muted hover:bg-hover hover:text-text"
:aria-pressed="preview ? 'true' : 'false'"
@click="togglePreview"
>
Preview
</button>
<div v-if="preview" class="flex flex-col gap-1.5">
<div class="flex items-center gap-2">
<button
v-if="mailImages"
type="button"
data-markdown-insert-image
class="inline-flex h-8 items-center gap-1.5 rounded-pager px-2 text-[13px] text-muted transition-colors duration-150 ease-out hover:bg-hover hover:text-text"
@click="pickerOpen = true"
>
<ImagePlus :size="16" aria-hidden="true" />
{{ t('backend::lang.mail.picker.insert_button') }}
</button>
<button
v-else
type="button"
data-markdown-preview
class="self-start rounded-pager px-2 py-1 text-[13px] text-muted hover:bg-hover hover:text-text"
:aria-pressed="preview ? 'true' : 'false'"
@click="togglePreview"
>
Preview
</button>
</div>
<div v-if="preview && !mailImages" class="flex flex-col gap-1.5">
<p v-if="previewError" data-markdown-preview-error role="status" class="text-[13px] text-danger">{{ previewError }}</p>
<div
data-markdown-preview-pane
@@ -131,6 +169,7 @@ onBeforeUnmount(() => {
<textarea
v-else
:id="controlId"
ref="area"
v-bind="attrs"
:name="field.name"
:rows="rows"
@@ -143,5 +182,6 @@ onBeforeUnmount(() => {
class="min-h-input resize-y py-2.5"
@input="emit('update:modelValue', ($event.target as HTMLTextAreaElement).value)"
/>
<MailImagePicker v-if="mailImages" v-model:open="pickerOpen" mode="insert" @insert="onInsert" />
</div>
</template>

View File

@@ -58,6 +58,8 @@ import TextareaField from './fields/TextareaField.vue'
import MarkdownField from './fields/MarkdownField.vue'
import MailPreviewField from './fields/MailPreviewField.vue'
import CodeEditorField from './fields/CodeEditorField.vue'
import ColorPickerField from './fields/ColorPickerField.vue'
import MailImageField from './fields/MailImageField.vue'
import MoneyField from './fields/MoneyField.vue'
import TaglistField from './fields/TaglistField.vue'
import MLTextField from './fields/MLTextField.vue'
@@ -105,6 +107,8 @@ const renderers = new Map<string, Component>([
['taglist', TaglistField],
['mailpreview', MailPreviewField],
['codeeditor', CodeEditorField],
['colorpicker', ColorPickerField],
['mailimage', MailImageField],
])
/** Types whose control shows the label itself (toggle cards, relation manager). */
@@ -134,6 +138,7 @@ const groupLabelledTypes = new Set<string>([
'mlmarkdown',
'repeater',
'mailpreview',
'mailimage',
])
export function rendererFor(type: string): Component {