Rich text / foldkit-richtext-dom
Embed the editor as a form control
foldkit-richtext-dom/input integrates the editor into a foldkit-form field backed by a RichText.Document or a custom serialized format:
import { Schema } from 'effect'
import { Entity } from 'foldkit-entity'
import { Form } from 'foldkit-form'
import * as RichText from 'foldkit-richtext'
import { richTextInput } from 'foldkit-richtext-dom/input'
const Post = Entity.define('Post', Schema.Struct({ id: Schema.String, body: RichText.Document }))
const PostInput = Entity.input(Post, Schema.Struct({ body: Post.fields.body.schema }))
const PostForm = Form.make('PostForm', PostInput, {
inputs: { body: richTextInput('post-body', { placeholder: 'Write the post…' }) },
})Because a form field owns its draft state without a parent OutMessage, EditorInput stores its Document inside its own Model and reports changes to the form like any other input. New records start with one empty paragraph, and blank documents report as empty so required validation catches them. One hostId identifies one editor, so repeated form rows cannot each host a richTextInput yet.
To store Markdown or another text format in the Entity while editing rich text, provide a codec with value and fill:
import { parse, print } from 'foldkit-richtext-markdown'
const Note = Entity.define('Note', Schema.Struct({ id: Schema.String, body: Schema.String }))
const NoteInput = Entity.input(Note, Schema.Struct({ body: Note.fields.body.schema }))
const NoteForm = Form.make('NoteForm', NoteInput, {
inputs: {
body: richTextInput('note-body', {}, {
codec: {
value: document => print(document).markdown,
fill: text => {
let next = 0
return parse(text, { mint: () => `m${next++}` }).document
},
},
}),
},
})fill runs when populating stored data; avoid throwing on malformed input, and return EditorInput.init({ hostId }).model.document when filling empty text ('') so the editor always starts with one paragraph. Calling settled on a restored draft preserves caret, stored marks, and undo history while updating the hostId.