## Résumé
Nouveau composant `MalioInputRichText` : éditeur WYSIWYG basé sur **TipTap v3** + **StarterKit** + **tiptap-markdown**, aligné sur le thème Malio (couleurs `m-*`, icônes `mdi:*`, états error / success / hint).
## Détails
- **Toolbar** : gras, italique, barré, H2, H3, liste à puces, liste numérotée, citation, code inline, bloc de code, lien (prompt URL), undo / redo
- **Sortie** : `markdown` (par défaut) ou `html` via la prop `outputFormat`
- **Modes** : `editable`, `disabled`, `readonly` ; mode lecture seule (`editable=false`) rend le contenu en `prose` sans toolbar
- **Accessibilité** : label `for/id`, `aria-invalid`, `aria-describedby`, `aria-pressed` sur les boutons toolbar
- **Style** : floating focus border `m-primary`, error `m-danger`, success `m-success`, toolbar `bg-m-bg`
## Dépendances ajoutées (purement additives, aucun bump existant)
- `@tiptap/vue-3` ^3.22.5
- `@tiptap/starter-kit` ^3.22.5
- `@tiptap/extension-placeholder` ^3.22.5
- `@tiptap/pm` ^3.22.5
- `tiptap-markdown` ^0.9.0
> Note : `@tiptap/extension-link` n'est pas installé séparément car StarterKit v3 l'inclut nativement (configuré via `StarterKit.configure({ link: { ... } })`).
## Test plan
- [x] `npm run test` — 315/315 (12 nouveaux tests sur InputRichText)
- [x] `npm run lint` — 0 erreur sur les fichiers ajoutés
- [x] `npm run story:build` — Histoire build OK (story `Input/RichText` listée)
- [x] `npm run dev` — playground `/composant/input/inputRichText` (vérification visuelle des 8 variantes : simple, hint, erreur, succès, readonly, disabled, lecture seule, sortie HTML)
- [x] `npm run story:dev` — story `Input/RichText` avec docs
## Fichiers
- `app/components/malio/input/InputRichText.vue` — composant
- `app/components/malio/input/InputRichText.test.ts` — tests
- `.playground/pages/composant/input/inputRichText.vue` — playground
- `app/story/input/inputRichText.story.vue` — story Histoire
- `histoire.config.ts` — alias ESM + `optimizeDeps` pour `tiptap-markdown` (sinon Histoire choisit la build UMD)
- `CHANGELOG.md`, `COMPONENTS.md` — documentation
Reviewed-on: #37
Co-authored-by: matthieu <matthieu@yuno.malio.fr>
Co-committed-by: matthieu <matthieu@yuno.malio.fr>
134 lines
4.8 KiB
TypeScript
134 lines
4.8 KiB
TypeScript
import {afterEach, describe, expect, it} from 'vitest'
|
|
import {flushPromises, mount} from '@vue/test-utils'
|
|
import type {DefineComponent} from 'vue'
|
|
import InputRichText from './InputRichText.vue'
|
|
|
|
type InputRichTextProps = {
|
|
id?: string
|
|
label?: string
|
|
modelValue?: string | null
|
|
placeholder?: string
|
|
minHeight?: string
|
|
editable?: boolean
|
|
disabled?: boolean
|
|
readonly?: boolean
|
|
hint?: string
|
|
error?: string
|
|
success?: string
|
|
outputFormat?: 'markdown' | 'html'
|
|
groupClass?: string
|
|
labelClass?: string
|
|
editorClass?: string
|
|
}
|
|
|
|
const InputRichTextForTest = InputRichText as DefineComponent<InputRichTextProps>
|
|
|
|
const mountComponent = async (props: InputRichTextProps = {}) => {
|
|
const wrapper = mount(InputRichTextForTest, {
|
|
props,
|
|
attachTo: document.body,
|
|
})
|
|
await flushPromises()
|
|
return wrapper
|
|
}
|
|
|
|
afterEach(() => {
|
|
document.body.replaceChildren()
|
|
})
|
|
|
|
describe('MalioInputRichText', () => {
|
|
it('renders the label and reuses a provided id', async () => {
|
|
const wrapper = await mountComponent({id: 'custom-rt-id', label: 'Description'})
|
|
|
|
const label = wrapper.get('label')
|
|
expect(label.text()).toBe('Description')
|
|
expect(label.attributes('for')).toBe('custom-rt-id')
|
|
expect(wrapper.get('#custom-rt-id').exists()).toBe(true)
|
|
})
|
|
|
|
it('generates an id when missing', async () => {
|
|
const wrapper = await mountComponent({label: 'Description'})
|
|
|
|
const labelFor = wrapper.get('label').attributes('for')
|
|
expect(labelFor?.startsWith('malio-input-rich-text-')).toBe(true)
|
|
})
|
|
|
|
it('renders the toolbar buttons in editable mode', async () => {
|
|
const wrapper = await mountComponent({modelValue: ''})
|
|
|
|
const buttons = wrapper.findAll('button[type="button"]')
|
|
expect(buttons.length).toBeGreaterThanOrEqual(13)
|
|
expect(wrapper.find('button[title="Gras"]').exists()).toBe(true)
|
|
expect(wrapper.find('button[title="Italique"]').exists()).toBe(true)
|
|
expect(wrapper.find('button[title="Lien"]').exists()).toBe(true)
|
|
expect(wrapper.find('button[title="Annuler"]').exists()).toBe(true)
|
|
expect(wrapper.find('button[title="Rétablir"]').exists()).toBe(true)
|
|
})
|
|
|
|
it('does not render the toolbar in readonly display mode (editable=false)', async () => {
|
|
const wrapper = await mountComponent({editable: false, modelValue: '**hi**'})
|
|
|
|
expect(wrapper.find('button[title="Gras"]').exists()).toBe(false)
|
|
})
|
|
|
|
it('disables toolbar buttons when disabled', async () => {
|
|
const wrapper = await mountComponent({disabled: true, modelValue: ''})
|
|
|
|
const boldBtn = wrapper.get('button[title="Gras"]')
|
|
expect(boldBtn.attributes('disabled')).toBeDefined()
|
|
})
|
|
|
|
it('disables toolbar buttons when readonly', async () => {
|
|
const wrapper = await mountComponent({readonly: true, modelValue: ''})
|
|
|
|
const boldBtn = wrapper.get('button[title="Gras"]')
|
|
expect(boldBtn.attributes('disabled')).toBeDefined()
|
|
})
|
|
|
|
it('shows hint message in muted color', async () => {
|
|
const wrapper = await mountComponent({hint: 'Helpful hint'})
|
|
|
|
expect(wrapper.get('p.text-m-muted').text()).toBe('Helpful hint')
|
|
})
|
|
|
|
it('shows error state on wrapper, label and message', async () => {
|
|
const wrapper = await mountComponent({label: 'Description', error: 'Editor error'})
|
|
|
|
expect(wrapper.get('label').classes()).toContain('text-m-danger')
|
|
expect(wrapper.get('p.text-m-danger').text()).toBe('Editor error')
|
|
expect(wrapper.get('.rich-text-wrapper').classes()).toContain('border-m-danger')
|
|
})
|
|
|
|
it('shows success state on wrapper, label and message', async () => {
|
|
const wrapper = await mountComponent({label: 'Description', success: 'Editor success'})
|
|
|
|
expect(wrapper.get('label').classes()).toContain('text-m-success')
|
|
expect(wrapper.get('p.text-m-success').text()).toBe('Editor success')
|
|
expect(wrapper.get('.rich-text-wrapper').classes()).toContain('border-m-success')
|
|
})
|
|
|
|
it('prioritizes error over success', async () => {
|
|
const wrapper = await mountComponent({error: 'Editor error', success: 'Editor success'})
|
|
|
|
expect(wrapper.get('.rich-text-wrapper').classes()).toContain('border-m-danger')
|
|
expect(wrapper.find('p.text-m-success').exists()).toBe(false)
|
|
expect(wrapper.get('p.text-m-danger').text()).toBe('Editor error')
|
|
})
|
|
|
|
it('sets aria-invalid and aria-describedby on the editor content when error', async () => {
|
|
const wrapper = await mountComponent({id: 'rt-aria', error: 'Boom'})
|
|
|
|
const editorContent = wrapper.find('[aria-invalid="true"]')
|
|
expect(editorContent.exists()).toBe(true)
|
|
expect(editorContent.attributes('aria-describedby')).toBe('rt-aria-describedby')
|
|
})
|
|
|
|
it('renders initial markdown content visually', async () => {
|
|
const wrapper = await mountComponent({modelValue: '## Mon titre\n\nUn paragraphe.'})
|
|
|
|
const html = wrapper.html()
|
|
expect(html).toContain('Mon titre')
|
|
expect(html).toContain('Un paragraphe.')
|
|
})
|
|
})
|