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:
@@ -298,17 +298,38 @@ describe('insertImage', () => {
|
||||
})
|
||||
})
|
||||
|
||||
const pickerBundle = {
|
||||
...mailPreviewBundle,
|
||||
...codeeditorBundle,
|
||||
'backend::lang.colorpicker.pick': { other: 'Pick a colour for “:field”' },
|
||||
'backend::lang.colorpicker.not_set': { other: 'Not set' },
|
||||
'backend::lang.mail.picker.insert_button': { other: 'Insert image' },
|
||||
'backend::lang.mail.picker.insert_title': { other: 'Insert an image from the library' },
|
||||
'backend::lang.mail.picker.choose_title': { other: 'Choose an image' },
|
||||
'backend::lang.mail.picker.search': { other: 'Search images' },
|
||||
'backend::lang.mail.picker.insert': { other: 'Insert image' },
|
||||
'backend::lang.mail.picker.use': { other: 'Use this image' },
|
||||
'backend::lang.mail.picker.upload': { other: 'Upload image' },
|
||||
'backend::lang.mail.picker.uploading': { other: 'Uploading…' },
|
||||
'backend::lang.mail.picker.empty_title': { other: 'No images yet' },
|
||||
'backend::lang.mail.picker.empty_upload': { other: 'Upload an image to use it in mail.' },
|
||||
'backend::lang.mail.picker.empty_pick_only': { other: 'Images are added in Settings.' },
|
||||
'backend::lang.mail.picker.no_results': { other: 'No images match “:query”.' },
|
||||
'backend::lang.mail.picker.load_failed': { other: 'The image library could not be loaded.' },
|
||||
'backend::lang.mail.picker.alt': { other: 'Alternative text' },
|
||||
'backend::lang.mail.picker.alt_comment': { other: 'Describes the image.' },
|
||||
'backend::lang.mailimage.empty': { other: 'No image chosen' },
|
||||
'backend::lang.mailimage.choose': { other: 'Choose image' },
|
||||
'backend::lang.mailimage.change': { other: 'Change image' },
|
||||
'backend::lang.mailimage.remove': { other: 'Remove image' },
|
||||
'backend::lang.mailimage.missing': { other: 'This image is no longer in the library. Choose another one.' },
|
||||
'backend::lang.form.cancel': { other: 'Cancel' },
|
||||
}
|
||||
|
||||
describe('ColorPickerField', () => {
|
||||
it('syncs a typed #rrggbb into the swatch and keeps the last valid on partial', async () => {
|
||||
const Field = (await import('../../src/components/form/fields/ColorPickerField.vue')).default
|
||||
setBundle(
|
||||
{
|
||||
...mailPreviewBundle,
|
||||
'backend::lang.colorpicker.pick': { other: 'Pick a colour for “:field”' },
|
||||
'backend::lang.colorpicker.not_set': { other: 'Not set' },
|
||||
},
|
||||
'en',
|
||||
)
|
||||
setBundle(pickerBundle, 'en')
|
||||
const wrapper = mount(Field, {
|
||||
props: {
|
||||
field: { name: 'body_bg', label: 'Body background', type: 'colorpicker' } as FormField,
|
||||
@@ -328,17 +349,105 @@ describe('ColorPickerField', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('MailImagePicker and MarkdownField mailImages', () => {
|
||||
beforeEach(() => {
|
||||
setBundle(pickerBundle, 'en')
|
||||
})
|
||||
|
||||
it('shows the upload row only when meta.canUpload is true', async () => {
|
||||
const Picker = (await import('../../src/components/mail/MailImagePicker.vue')).default
|
||||
mockApi({
|
||||
[`GET ${API}/mail/images`]: {
|
||||
body: { data: [], meta: { canUpload: false, total: 0, page: 1, perPage: 40 } },
|
||||
},
|
||||
})
|
||||
const wrapper = mount(Picker, {
|
||||
props: { open: true, mode: 'insert' },
|
||||
attachTo: document.body,
|
||||
})
|
||||
await flushPromises()
|
||||
expect(document.querySelector('[data-mail-image-upload]')).toBeNull()
|
||||
|
||||
mockApi({
|
||||
[`GET ${API}/mail/images`]: {
|
||||
body: { data: [], meta: { canUpload: true, total: 0, page: 1, perPage: 40 } },
|
||||
},
|
||||
})
|
||||
await wrapper.setProps({ open: false })
|
||||
await flushPromises()
|
||||
await wrapper.setProps({ open: true })
|
||||
await flushPromises()
|
||||
expect(document.querySelector('[data-mail-image-upload]')).not.toBeNull()
|
||||
})
|
||||
|
||||
it('hides Preview and inserts markdown at the caret when mailImages is set', async () => {
|
||||
const Field = (await import('../../src/components/form/fields/MarkdownField.vue')).default
|
||||
mockApi({
|
||||
[`GET ${API}/mail/images`]: {
|
||||
body: {
|
||||
data: [{ id: 9, fileName: 'logo.png', title: 'Logo', url: 'https://cdn.example.com/logo.png' }],
|
||||
meta: { canUpload: false, total: 1, page: 1, perPage: 40 },
|
||||
},
|
||||
},
|
||||
})
|
||||
const wrapper = mount(Field, {
|
||||
props: {
|
||||
field: { name: 'body', label: 'Body', type: 'markdown', mailImages: true } as FormField,
|
||||
modelValue: 'Hello ',
|
||||
controlId: 'f-body',
|
||||
},
|
||||
attachTo: document.body,
|
||||
})
|
||||
expect(wrapper.find('[data-markdown-preview]').exists()).toBe(false)
|
||||
expect(wrapper.find('[data-markdown-insert-image]').exists()).toBe(true)
|
||||
await wrapper.find('[data-markdown-insert-image]').trigger('click')
|
||||
await flushPromises()
|
||||
const tile = document.querySelector<HTMLElement>('[data-mail-image-id="9"]')
|
||||
expect(tile).not.toBeNull()
|
||||
tile!.click()
|
||||
await flushPromises()
|
||||
document.querySelector<HTMLElement>('[data-mail-image-confirm]')!.click()
|
||||
await flushPromises()
|
||||
expect(wrapper.emitted('update:modelValue')?.at(-1)).toEqual(['Hello '])
|
||||
})
|
||||
})
|
||||
|
||||
describe('MailImageField', () => {
|
||||
it('shows the missing line when the stored id is gone', async () => {
|
||||
;(await import('../../src/app/i18n')).setBundle(pickerBundle, 'en')
|
||||
const Field = (await import('../../src/components/form/fields/MailImageField.vue')).default
|
||||
mockApi({
|
||||
[`GET ${API}/mail/images/3`]: { status: 404, body: { error: { code: 'not_found', message: 'gone', details: {} } } },
|
||||
})
|
||||
const wrapper = mount(Field, {
|
||||
props: {
|
||||
field: { name: 'logo', label: 'Logo', type: 'mailimage' } as FormField,
|
||||
modelValue: 3,
|
||||
controlId: 'f-logo',
|
||||
},
|
||||
attachTo: document.body,
|
||||
})
|
||||
await flushPromises()
|
||||
expect(wrapper.find('[data-mailimage-missing]').exists()).toBe(true)
|
||||
expect(wrapper.find('[data-mailimage-missing]').text()).toContain('no longer in the library')
|
||||
})
|
||||
})
|
||||
|
||||
// Last in the file: it replaces the editor chunk for the rest of the run.
|
||||
describe('CodeEditorField chunk failure', () => {
|
||||
afterEach(() => {
|
||||
vi.doUnmock('../../src/components/form/codeeditor/editor')
|
||||
vi.resetModules()
|
||||
})
|
||||
|
||||
it('shows the load_failed note and a monospace textarea that still emits', async () => {
|
||||
vi.resetModules()
|
||||
vi.doMock('../../src/components/form/codeeditor/editor', () => {
|
||||
throw new Error('chunk failed')
|
||||
})
|
||||
vi.unmock('../../src/components/form/codeeditor/editor')
|
||||
vi.doMock('../../src/components/form/codeeditor/editor', () => ({
|
||||
createCodeEditor: () => {
|
||||
throw new Error('chunk failed')
|
||||
},
|
||||
}))
|
||||
;(await import('../../src/app/i18n')).setBundle({ ...mailPreviewBundle, ...codeeditorBundle }, 'en')
|
||||
const Field = (await import('../../src/components/form/fields/CodeEditorField.vue')).default
|
||||
const wrapper = mount(Field, {
|
||||
|
||||
Reference in New Issue
Block a user