/** * Editing a field ON the page, in the Studio's preview: a click on a headline * puts the caret in the headline. * * Plain DOM, nothing of React's or the Studio's, and in `shared` for the * reason `elementPicker.ts` is: the canvas bridge of each binding * (`@valbuild/next`, `@valbuild/tanstack`) runs it inside the page, the only * place the page's elements can be reached. What crosses the frame is the * value typed (`inlineEdit` in `valCanvasProtocol.ts`), and the Studio writes * it as the field in its own column would. * * ## The page's element is never edited: a copy of it is * * The element on the page is the framework's. React holds its text nodes and * writes to them when the content moves -- and every edit made here comes * back as exactly that, a moment later, once the Studio has made a patch of * it. Typing into React's own node therefore means the caret jumping to the * start on every echo, the letters typed since coming back out, and, once a * paragraph has been split or merged, React reconciling against a tree that * is no longer the one it built. * * So the element is hidden and a COPY of it stands in its place, with the * same classes and therefore the same look. The copy is nobody's but ours: * the person types in it, and the framework goes on updating the hidden * original. When editing ends the copy stays until the original shows what * was typed (or a short wait runs out), and then the original comes back. * * ## What can be edited this way * * - A **string**, when the element shows that string and nothing else: no * child elements, and its text is the value. `

{title}

` is; * `

Hi {name}!

`, an `alt`, an `href` or a value the page transformed * is not. * - A **richtext**, when the element's children are the document's nodes, * tag for tag (what `ValRichText` renders without a `transform` that * changes the structure). Text can be typed and deleted anywhere, * paragraphs and list items split (Enter) and joined (Backspace, Delete), * and the marks, links and images that are there are kept. Nothing NEW can * be formatted here: a mark's class comes from the page's own theme, which * only the page's renderer knows. * * Anything else is answered with `null`, and the Studio opens the field in * its own column instead, as it always has. */ /** A node of a richtext document, as its source holds it. */ export type InlineRichTextNode = string | InlineRichTextElement; export type InlineRichTextElement = { readonly tag: string; readonly children?: readonly InlineRichTextNode[]; readonly [key: string]: unknown; }; /** What a field holds once it has been edited here. */ export type InlineEditValue = string | InlineRichTextNode[]; /** * A field the Studio offers for editing in place: its path, what kind it is * and what it holds now, which is how the page tells that an element shows * it. */ export type InlineEditCandidate = { path: string; kind: "string"; value: string; /** Enter is a new line rather than the end of the edit. */ multiline?: boolean; } | { path: string; kind: "richtext"; value: InlineRichTextNode[]; }; /** * A place in a richtext document: which paragraph or heading, counted in the * order they are read, and how far into it. Text counts by the character * and an image or a line break as one -- which is how the Studio's editor * counts too, so a place means the same on both sides of the frame. */ export type InlinePoint = { block: number; offset: number; }; /** What is selected in a richtext being edited: from `anchor` to `head`. */ export type InlineSelection = { anchor: InlinePoint; head: InlinePoint; }; /** Where the copy being typed in is, in the page's viewport. */ export type InlineRect = { top: number; left: number; width: number; height: number; }; /** An edit in progress. */ export type InlineEditing = { /** The field being edited. */ path: string; /** Whether it is still being typed in: false once it has been stopped. */ isActive(): boolean; /** Whether `node` is in the copy being typed in. */ contains(node: Node | null): boolean; /** * End it. The copy stays, no longer editable, until the page's own element * shows what was typed, and `onEnd` is called at once. */ stop(): void; /** * End it and give the page its element back NOW, whatever it shows: for * when the element is wanted again, by the next edit or by the page going * away. */ release(): void; /** * Put a whole new document in the copy, with this selected: what one of * the Studio's controls made of the document (a word made bold, a * paragraph made a heading). A richtext only. * * Drawn at once with what this can tell of the page's look -- a new node * is classed as one of its kind already in the document is -- and again, * exactly, once the page's own element has rendered it. */ apply(value: InlineRichTextNode[], selection: InlineSelection | null): void; /** * The marks the next text typed at the caret has, whatever the text * around it has: one of the Studio's controls pressed with nothing * selected (Bold, and then the next word is bold). `styles` as a span's * `styles` holds them -- `[]` for none, which is how Bold is turned off * inside a bold word -- and `null` to forget them. Forgotten too when the * caret moves, as the Studio's own editor forgets them. A richtext only. * * `at` is where the caret was when they were asked for: when it is * somewhere else by now, they are not taken at all. */ mark(styles: readonly string[] | null, at: InlineSelection | null): void; /** * Put the caret back in the copy, where it was: the focus went to one of * the Studio's controls -- a box to type a link in -- and is done there. */ focus(): void; }; /** On the copy being typed in. */ export declare const INLINE_EDITOR_ATTRIBUTE = "data-val-inline-editor"; /** On the page's own element, hidden while its copy is typed in. */ export declare const INLINE_ORIGINAL_ATTRIBUTE = "data-val-inline-original"; /** * How long the copy waits for the page's element to show what was typed * before giving way to it regardless. Content the SERVER rendered only moves * after a round trip, which can take longer than this: then the old text * shows for a moment before the page re-renders. */ export declare const INLINE_EDIT_SETTLE_MS = 2000; /** * Typing with no longer a pause than this between two keystrokes is one * step of undo. A pause, the caret moving, or switching between typing and * deleting starts the next. */ export declare const INLINE_UNDO_PAUSE_MS = 1000; /** Whether a value is a richtext document: what `editInline` may carry. */ export declare function isInlineRichText(value: unknown): value is InlineRichTextNode[]; /** Whether a value is a candidate: what `editInline` may carry. */ export declare function isInlineEditCandidate(value: unknown): value is InlineEditCandidate; /** Whether a value is a selection: what `applyInlineEdit` may carry. */ export declare function isInlineSelection(value: unknown): value is InlineSelection; /** Whether a value is what an edit reports: a string, or a document. */ export declare function isInlineEditValue(value: unknown): value is InlineEditValue; /** * Whether `element` shows `candidate` and nothing else, so that typing in * (a copy of) it is typing the field. */ export declare function canEditInline(element: Element, candidate: InlineEditCandidate): boolean; /** * Start editing `candidate` where `element` shows it, or `null` when the * element does not show it in a way that can be typed in * ({@link canEditInline}). */ export declare function startInlineEditing(element: Element, candidate: InlineEditCandidate, options: { /** * Where the click that asked for this was, in the viewport: the caret * goes there. Absent, it goes to the end. */ at?: { x: number; y: number; } | null; /** * The outline's colour: the Studio's selection colour. Set as a style * property, which takes a colour or nothing, so it needs no checking * here. */ color: string; /** Something was typed: the field's new value, whole. */ onChange: (value: InlineEditValue) => void; /** Editing ended, however it did. Called once. */ onEnd: () => void; /** * The selection moved, or the copy did: what the Studio's controls act * on, and where to draw them and the field's validation errors. For a * string the selection is always `null`: there are no controls for * one, but its errors are drawn by it all the same. */ onSelection?: (selection: InlineSelection | null, rect: InlineRect, /** The caret itself, where the browser can say: to keep off it. */ caret: InlineRect | null) => void; /** {@link INLINE_EDIT_SETTLE_MS}, for a test that will not wait. */ settleMs?: number; }): InlineEditing | null;