Rasura

Editing text

Replacing characters in a paragraph, and what happens to the lines around them.

Ranges

An edit names a paragraph and a character range within it. Offsets are into the reconstructed text, so they line up with what you showed the user.

JavaScript
const session = doc.edit()
await session.replaceText(p.id, { start: 12, end: 19 }, 'replacement')

Send the smallest range that differs

Replacing a whole paragraph works, and re-breaks every line in it. Trimming the common prefix and suffix first usually keeps the edit inside one text-showing operator, which is what makes the difference between the exact rung and a reflow. This is the caller's job, not the library's.

Reflow and overflow

Longer text has to go somewhere. The policy decides where, and it is set on the session rather than per call.

  • refuse: the edit fails if the text no longer fits its box.
  • allow: the paragraph grows downward and may overlap what follows.
  • grow: the box is enlarged, and the shape of the change is reported.
  • shrink: the type size is reduced until it fits, and by how much is reported.

Line breaking defaults to greedy, matching what most producers did. The optimal setting uses Knuth-Plass, which produces better paragraphs and different breaks from the original, so it changes more of the page than the edit strictly needed.

Sessions and undo

Operations stage until you commit. Everything in a session shares one undo stack, across every kind of operation: an image move and a text edit staged together come off in reverse order and leave the file byte-identical.

JavaScript
const session = doc.edit()
await session.replaceText(p.id, { start: 0, end: 5 }, 'first')
await session.deletePage(4)
const { staged, canUndo } = await session.status() // 2, true
await session.undo() // the page comes back
const { bytes } = await session.commit() // only the text edit lands