diff --git a/admin/src/components/form/codeeditor/editor.ts b/admin/src/components/form/codeeditor/editor.ts new file mode 100644 index 00000000..346a3aa3 --- /dev/null +++ b/admin/src/components/form/codeeditor/editor.ts @@ -0,0 +1,21 @@ +// Stub for TDD RED (35-03 Task 4): real CodeMirror factory lands in GREEN. +export interface CodeEditorOptions { + parent: HTMLElement + doc: string + language: 'html' | 'css' | 'text' + height: number + attributes: Record + onChange: (doc: string) => void +} + +export interface CodeEditorHandle { + view: unknown + insert(text: string): void + destroy(): void + setDoc(doc: string): void +} + +/** Builds the field's CodeMirror editor on options.parent. */ +export function createCodeEditor(_options: CodeEditorOptions): CodeEditorHandle { + throw new Error('codeeditor not implemented') +} diff --git a/admin/src/components/form/fields/CodeEditorField.vue b/admin/src/components/form/fields/CodeEditorField.vue new file mode 100644 index 00000000..cacfd076 --- /dev/null +++ b/admin/src/components/form/fields/CodeEditorField.vue @@ -0,0 +1,13 @@ + + + diff --git a/admin/tests/browser/codeeditor-smoke.test.ts b/admin/tests/browser/codeeditor-smoke.test.ts new file mode 100644 index 00000000..499c8c82 --- /dev/null +++ b/admin/tests/browser/codeeditor-smoke.test.ts @@ -0,0 +1,135 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { enableAutoUnmount, flushPromises, mount } from '@vue/test-utils' +import type { FormField } from '../../src/api/types' +import { installHotkeys } from '../../src/app/hotkeys' +import { setBundle } from '../../src/app/i18n' +import CodeEditorField from '../../src/components/form/fields/CodeEditorField.vue' +import { langFixture } from '../fixtures/typed' +import { press } from '../helpers' + +// Browser smoke for the codeeditor field (Phase 35 UI-SPEC C1): Tab leaves the +// editor, Mod+S and Mod+Enter reach form save hotkeys, Esc does not Cancel. +// Needs a real engine so contenteditable focus and key routing match production. + +enableAutoUnmount(afterEach) + +const codeeditorBundle = { + ...langFixture.data, + 'backend::lang.codeeditor.load_failed': { + other: 'The code editor could not be loaded. You can still edit the text here.', + }, + 'backend::lang.codeeditor.toolbar': { other: 'Code editor tools' }, + 'backend::lang.list.empty_value': { other: '—' }, +} + +function field(overrides: Partial = {}): FormField { + return { + name: 'content_html', + label: 'HTML', + type: 'codeeditor', + language: 'html', + size: 'large', + ...overrides, + } as FormField +} + +async function editorReady(root: HTMLElement): Promise { + return vi.waitFor(() => { + const content = root.querySelector('.cm-content') + expect(content).not.toBeNull() + return content! + }) +} + +beforeEach(() => { + setBundle(codeeditorBundle, 'en') +}) + +afterEach(() => { + document.body.innerHTML = '' +}) + +describe('codeeditor keyboard in a real browser engine', () => { + it('Tab leaves the editor; Mod+S and Mod+Enter save; Esc does not cancel', async () => { + const uninstall = installHotkeys() + const saveClicks: string[] = [] + const cancelClicks: string[] = [] + + const form = document.createElement('form') + form.setAttribute('role', 'form') + document.body.appendChild(form) + + const next = document.createElement('input') + next.id = 'next-control' + next.type = 'text' + + const save = document.createElement('button') + save.type = 'button' + save.setAttribute('data-hotkey', 'mod+s mod+enter') + save.addEventListener('click', () => saveClicks.push('save')) + + const cancel = document.createElement('button') + cancel.type = 'button' + cancel.setAttribute('data-hotkey', 'escape') + cancel.addEventListener('click', () => cancelClicks.push('cancel')) + + const host = document.createElement('div') + form.append(host, next, save, cancel) + + const wrapper = mount(CodeEditorField, { + props: { + field: field(), + modelValue: 'Hello {{ name }}', + controlId: 'f-html', + }, + attachTo: host, + }) + await flushPromises() + const content = await editorReady(wrapper.element as HTMLElement) + content.focus() + expect(document.activeElement).toBe(content) + + // Tab must leave the editor (no indentWithTab / no trap). + content.dispatchEvent(new KeyboardEvent('keydown', { key: 'Tab', bubbles: true, cancelable: true })) + // Native Tab focus movement: advance to the next control. + next.focus() + expect(document.activeElement).toBe(next) + + content.focus() + expect(press(content, 's', { ctrlKey: true }).defaultPrevented).toBe(true) + expect(saveClicks).toEqual(['save']) + + saveClicks.length = 0 + expect(press(content, 'Enter', { ctrlKey: true }).defaultPrevented).toBe(true) + expect(saveClicks).toEqual(['save']) + + expect(press(content, 'Escape').defaultPrevented).toBe(false) + expect(cancelClicks).toEqual([]) + + uninstall() + wrapper.unmount() + }) + + it('typing emits update:modelValue with the full document', async () => { + const wrapper = mount(CodeEditorField, { + props: { + field: field({ language: 'text' }), + modelValue: 'start', + controlId: 'f-text', + }, + attachTo: document.body, + }) + await flushPromises() + const content = await editorReady(wrapper.element as HTMLElement) + content.focus() + + // Insert at end via beforeinput/input so CodeMirror updates the doc. + document.execCommand('selectAll') + document.execCommand('insertText', false, 'changed') + await flushPromises() + + const emitted = wrapper.emitted('update:modelValue') + expect(emitted).toBeTruthy() + expect(emitted!.at(-1)).toEqual(['changed']) + }) +}) diff --git a/admin/tests/form/mail-smoke.test.ts b/admin/tests/form/mail-smoke.test.ts index 0f1495a0..38420022 100644 --- a/admin/tests/form/mail-smoke.test.ts +++ b/admin/tests/form/mail-smoke.test.ts @@ -10,6 +10,14 @@ import { clearToasts, toasts } from '../../src/state/useToasts' import { API, mockApi, profile, requestsTo, resetState, type Reply, wait } from '../helpers' import { langFixture } from '../fixtures/typed' +const codeeditorBundle = { + 'backend::lang.codeeditor.load_failed': { + other: 'The code editor could not be loaded. You can still edit the text here.', + }, + 'backend::lang.codeeditor.toolbar': { other: 'Code editor tools' }, + 'backend::lang.list.empty_value': { other: '—' }, +} + const mailPreviewBundle = { ...langFixture.data, 'backend::lang.mail.preview.html': { other: 'HTML' }, @@ -209,3 +217,95 @@ describe('MailPreviewField', () => { expect(toasts.value[0]?.text).toContain('dev@example.test') }) }) + +function codeField(overrides: Partial = {}): FormField { + return { + name: 'content_html', + label: 'HTML', + type: 'codeeditor', + language: 'html', + size: 'large', + ...overrides, + } as FormField +} + +describe('CodeEditorField loading and fallback', () => { + beforeEach(() => { + setBundle({ ...mailPreviewBundle, ...codeeditorBundle }, 'en') + }) + + it('shows the bg-skel loading box at the size height while the chunk loads', async () => { + // Hold the dynamic import open so the loading box stays visible. + let release!: () => void + const gate = new Promise((resolve) => { + release = resolve + }) + vi.resetModules() + vi.doMock('../../src/components/form/codeeditor/editor', async () => { + await gate + return await vi.importActual('../../src/components/form/codeeditor/editor') + }) + const Field = (await import('../../src/components/form/fields/CodeEditorField.vue')).default + const wrapper = mount(Field, { + props: { field: codeField(), modelValue: '

x

', controlId: 'f-html' }, + attachTo: document.body, + }) + await flushPromises() + const loading = wrapper.find('[data-codeeditor-loading]') + expect(loading.exists()).toBe(true) + expect(loading.attributes('aria-busy')).toBe('true') + expect((loading.element as HTMLElement).style.height).toBe('320px') + release() + await flushPromises() + wrapper.unmount() + vi.doUnmock('../../src/components/form/codeeditor/editor') + }) + + it('renders a read-only pre without loading CodeMirror', async () => { + const Field = (await import('../../src/components/form/fields/CodeEditorField.vue')).default + const wrapper = mount(Field, { + props: { + field: codeField({ readOnly: true }), + modelValue: 'layout {{ name }}', + controlId: 'f-html', + }, + attachTo: document.body, + }) + await flushPromises() + const pre = wrapper.find('pre') + expect(pre.exists()).toBe(true) + expect(pre.text()).toBe('layout {{ name }}') + expect(wrapper.find('[data-codeeditor-loading]').exists()).toBe(false) + expect(wrapper.find('.cm-editor').exists()).toBe(false) + }) +}) + +// 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') + }) + + 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') + }) + ;(await import('../../src/app/i18n')).setBundle({ ...mailPreviewBundle, ...codeeditorBundle }, 'en') + const Field = (await import('../../src/components/form/fields/CodeEditorField.vue')).default + const wrapper = mount(Field, { + props: { field: codeField(), modelValue: '

Stored

', controlId: 'f-html' }, + attachTo: document.body, + }) + await vi.waitFor(() => { + expect(wrapper.find('[data-codeeditor-load-failed]').exists()).toBe(true) + }) + expect(wrapper.find('[data-codeeditor-load-failed]').text()).toContain('could not be loaded') + const textarea = wrapper.find('textarea') + expect(textarea.exists()).toBe(true) + expect(textarea.element.value).toBe('

Stored

') + expect(textarea.attributes('id')).toBe('f-html') + await textarea.setValue('

Edited

') + expect(wrapper.emitted('update:modelValue')).toEqual([['

Edited

']]) + }) +})