The editorImages

Images

A reader pastes an image into a section: a figure of an open cover, or a schema of a proof. This page says where the image lives, how an image that is already stored loads, and how a pasted image is added. The second case reuses the first. It is a proposal. The section “What storage/binding offers” lists what the design needs from the code that connects the editor to its storage.

The image is a record

An image is not text, and it does not live in the section. The storage keeps it as a record of its own. With radif, the record is a row of the root class Image, with these fields:

Field From What it holds
bytes File the file, as the storage names it: the object’s key, the key that opens it, a digest, a size and a media type
title Titled the file’s name
description Described the text a reader who cannot see the image reads
width, height Sized the size in pixels

Image descends from File, which holds the bytes and requires Titled. Image declares no field of its own. It requires Described and Sized. Every field is required. A decorative image says that it is decorative.

A viewer that knows only Sized can size the space before the bytes arrive. An app’s own class, such as a Photo, descends from Image, and the editor draws it as an image. A Video is a sibling of Image: it also requires Timed, which holds its duration in seconds.

The section holds an atom of type image, and its text is the record’s id. The atom says where and how the section shows the image. The record says what the image is. So the section never holds bytes, and any viewer that knows the image record can draw it.

A section names its document through Attached. A pasted image carries no Attached: the atom names it, and no list shows it. A photo that a reader uploads to a project carries Attached, so the project’s list shows it.

The storage may name the bytes by a key that changes, for example when an upload is refused and starts again under a new key. The record takes the new key. The section does not change, because it names the record and not the bytes.

The atom holds a text and a param

The section is a row of the root class Section. It stores its blocks and its atoms as two lists. Each entry of a list has an id and a value. An atom also has a param: an object of texts. On the wire, an entry takes its shortest form: a bare id, [id, value], or [id, value, {param}].

blocks: array<entry<string>>     a list of Prose
atoms:  array<placed<string>>    a list of Text, with a param

["f1", "math\n\\bigcup_{i \\in I} U_i \\in \\tau"]
["f2", "image\nr42", {"width": "50%", "rotate": "90"}]

The atom’s type rides on the first line of its text, as it does today. The rest of the text is the source: the LaTeX of a formula, over as many lines as it needs, or the id of an image record. The param is the placement: how wide the image shows, or how it turns. Most atoms hold an empty param, and an empty param writes nothing. The app owns the keys. Radif checks that the param is an object of texts, and it reads no key. The rule of the atom’s type decodes and encodes it.

A block is Prose, and it merges word by word. An atom’s text is Text, and it merges whole: two readers who change a formula or an image conflict, and no merge makes a formula nobody wrote. A param always merges whole: it is replaced, never mixed. A reader who changes the image and one who resizes it both land, since they change different parts of the entry. Two who resize it conflict, and the local param stands.

Only a list takes a param, and only when its field’s definition says param. atoms says it, and the app reads it through Radif.placed. blocks does not, and the app reads it through Radif.entries. The flag is frozen with the definition, like many.

A section requires Attached, since it always sits in a document. Attached has one field, parents, a list with a param. Each entry names a parent, its value is the position, and its param is the app’s. The sections of a document are one indexed seek back along parents, in position order. A section needs no title: its heading, when it has one, is a block. The document is the root class Document, which requires Titled and takes Attached when it sits in another document. Its extension says what it opens as, such as course. The top document, the course, carries no Attached.

The radif croquis stores the root Document and Section, and defines no class of its own. The founder’s client makes one Document under their Personal node, and one Section under it whose parents names it. The port carries the param: the croquis reads each atom’s text and param, and writes both back.

Four parts share the work:

The layers

An image is drawn from seven layers. Each layer lies over the one below it. Two of them are typed: they hold what the device has and the storage does not have yet.

Layer What it holds Who writes it Who reads it
view the rule’s renderer and editor, over the live atom app declares them the reader
blob the pasted file, typed over the bytes until they are stored storage/binding, from paste the renderer, before the bytes
bytes the stored file, and how far its download is the storage, after the upload storage/binding, which fetches them for the renderer
image row the record: title, description, size, and the name of the bytes storage/binding; the rule’s editor through it the renderer, which draws a placeholder of that size
typed the atom’s text and param on screen, typed over the section row until it is saved @epure/editor @tilia/editor, which gives it to the loader
atom {type: image, text: <record id>, param: <placement>} in the section row @epure/editor, from the atom that paste returns @tilia/editor
block the display block that places the atom @epure/editor, by embed @tilia/editor

The loader reads the three lower layers through the typed text. It fills the live atom from the three upper layers of data: the image row, the bytes and the blob. The renderer never reads a row.

@epure/editor manages a new text. A paste into an empty paragraph keeps the block list, so the atom stays typed and is not saved at once. The loader must read the text on screen, and not the saved row, or that image would not load until the save.

The blob lies over the bytes while the upload runs, like typed text over the row. Unlike typed text, it stays once the storage holds the bytes: it is the same image, and a new source would blink. storage/binding releases it when the image leaves the section.

One rule per type

An image draws the way a formula draws. The host hands the view its rules by name, and an atom names its rule on the first line of its text. A rule holds a codec for the param, a first value, an optional loader, a renderer and an optional editor:

rule<'a, 'p>:
  param:   dict<string> <=> 'p                                 decodes and encodes the placement
  first:   atom => 'a                                          the live atom before the loader sets
  loader:  (atom, 'a, set) => unit                             sets the live atom, with the previous one
  render:  ('a, ~param, ~display) => React.element             draws the atom, inline or as a display
  editor:  ('a, ~param, ~onChange, ~onClose) => React.element  opens under the atom on a click

The loader reads the atom’s text, and never its param. The renderer and the editor read the decoded param.

'a is the live atom, the app’s model of the atom. Each rule has its own, and only the rule knows what it is. The view hands the live atom to the renderer, and to the editor when a click opens it. To @tilia/editor, these are plain calls. It never waits, and it never reads a loading state.

editor is optional: a rule without one enters the editor’s box. Its two callbacks are:

onChange: {text?, param?} => unit    a new text, or a new param that replaces the old one
onClose:  unit => unit               closes the editor and gives the focus back

onChange goes through @epure/editor, like a typed letter. The change is typed until the save, it can be undone, and it merges. A new file is not a text: the editor hands it to paste first, and passes the record id it returns.

math and image are two rules of the same shape:

Rule 'a first loader renderer editor
math the atom the atom none KaTeX, inline or in display mode none: it enters the box
image the image model Loading(text) image from storage/binding a placeholder, then the image, sized by the param the title and the description, the width and the rotation

The editor holds stable objects

The core is a value: every act returns a new document, and its atoms are new records. @tilia/editor holds the section in Live: the core’s row and document, one tilia object for each atom id, and one live atom for each.

After every act, save and landing, Live.sync writes the new document into the same objects:

So a change reaches only what reads the part that changed. A resize writes a key of the param: the renderer draws again, and no loader runs. A new text runs the loader again, since it read the text, and the loader receives the previous live atom. An image loader keeps the old image on screen until the new one is ready, so a replaced image does not blink, and a renderer can animate the change with CSS. The loader also reads the record and its bytes from the binding, so tilia runs it again when they change.

math needs no loader: its live atom is the atom object, so a new formula changes its text in place.

A loader runs when its live atom is first read, which is when it is first drawn. The first value rarely shows. When the storage holds the record and the bytes on the device, the loader calls set at once, in the same tick.

Loading an image

A section arrives with an atom {type: image, text: <record id>}.

  1. @tilia/editoropens the section and builds a source for the atom
  2. storage/bindingruns as the source's setup, with the record id on screen
  3. @tilia/editorhands the live image to the image renderer
  4. storage/bindingfetches the record and the bytes, and sets each state it reaches
  5. appdraws each state
An image already stored, drawn when its section opens

A draw reads the source, so it runs again each time the loader sets. The editor does not see the change. The image model is Image.t in @tilia/editor:

image:
  Loading(record)                           the row is on its way
  Uploading({record, meta?, src, progress?})  this device holds the file and stores it
  Downloading({meta, progress?})            the bytes are on their way down
  Missing(record)                           no such row, or the reader cannot reach it
  Ready({meta, src})                        the bytes are stored and on the device
meta: {record, object, width, height, title, description}
progress: {done, total, state}
upload: Waiting | Offline | Sending | Saving | Retrying(reason) | Stopped(reason)
download: Offline | Receiving | Checking

An image that only came down shows no overlay once it is ready.

@tilia/editor ships no styles. A host draws an image’s display atom as a block, .atom--display { display: block; }. An atom otherwise shrinks to its content, and a placeholder wider than the column would overflow it.

A row that does not answer may still be on its way: a client cannot tell a row it has not pulled from a row that does not exist. So the radif loader sets Missing only once the client is in step and no pull has brought anything for 500 milliseconds. Offline, the image stays a placeholder.

A click on an image opens the rule’s editor. It writes the title and the description to the record, and the new values come back through the source.

Pasting an image

A reader pastes an image file. The pasted image then loads the way a stored one does. The only difference is that the bytes are already on the device.

The blob lives in the storage. It never lives in the editor or in the atom. In the demo, it sits in a map in memory, under the record’s id. With radif, it sits in the client’s blob store, config.blobs, under the object’s key. client.bytes(~row, blob) keeps the blob and answers the value that the record’s bytes field holds. The client uploads the blob before the push that names it, and the push carries the value only.

  1. @tilia/editorreads the file from the clipboard and hands it to storage/binding
  2. storage/bindingmakes an image record, keeps the blob under its id, and returns an atom
  3. @epure/editorputs the atom in a display block, and the section is saved
  4. @tilia/editorbuilds a source for the atom, as for a stored image
  5. storage/bindingsets Loading with the blob at once, then Ready once its bytes open
  6. appdraws the blob from the first draw
One pasted image, from the clipboard to the screen

The upload

The editor does not see the upload: the loader does. The pasted blob is the image’s src from the first draw, so the image shows before any byte has left the device. It stays the src once the bytes are stored, so the image never blinks. The binding releases the pasted blob when the image leaves the section.

The loader follows the bytes with client.transfers. Radif says each state on its way, Upload(_) or Download(_), and the loader turns it into Uploading or Downloading with its progress. The upload’s last state, Done, sets Ready. A file kept before a reload still uploads after it, and client.blob answers it from the device, so the image shows its file and its upload again.

Radif caches the files a client read, and the files it uploaded once their push landed. A cached image answers at once, with no download.

A refused save starts the upload again. With radif, the client mints a new object key, rewrites the record, and uploads again under that key. An offline device uploads once it is back online. Nothing here is an error. An image is local first, like the text around it.

The description is required

A pasted image has a title, the file’s name, but no description. So it cannot be a complete record until the reader writes one. With radif, the pasted image is a draft: a row of Image that carries the Draft part. A draft is stored and synced, and it does not have to meet the record’s requirements yet. The row becomes a record when the reader writes the description.

The demo

The demo has no storage and no radif. It declares its own image rule, and its storage/binding keeps the records and the blobs in memory. On a paste, the binding reads the size from the decoded image and moves the upload from 0 to 100 and on to Ready on a timer, so a reader can watch the image travel. It stores nothing.

What storage/binding offers

Radif already gives the binding what it stands on:

Not yet