-
Notifications
You must be signed in to change notification settings - Fork 0
Typeset workspace prose with Justif #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
maceip
wants to merge
1
commit into
main
Choose a base branch
from
exp/typesetting-justif
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # Typesetting | ||
|
|
||
| ## What changed | ||
|
|
||
| Prose in the workspace is now set with [Justif](https://github.com/lyallcooper/justif) | ||
| (MIT): Knuth–Plass line breaking over the whole paragraph, TeX hyphenation | ||
| patterns, punctuation hung into the margin, and glyph protrusion at the edges. | ||
| The browser's one-line-at-a-time justification produces rivers and loose | ||
| lines; Justif evaluates the paragraph as a whole, the way a book is set. | ||
|
|
||
| Where it applies: | ||
|
|
||
| - Every Markdown paragraph, list item and blockquote in the agent transcript and | ||
| the discussion (the `Prose` component), once the text has settled. | ||
| - Not while a turn is still streaming: those paragraphs stay ragged with | ||
| `text-wrap: pretty` so the text does not reflow under the reader, and are | ||
| typeset the moment the turn completes. | ||
| - Not on teasers and labels. Two-line card descriptions were hyphenating | ||
| ("exist-ing parser API"), which is fussier than it is beautiful, so short | ||
| copy keeps the browser's `pretty` breaker and headings get `balance`. | ||
|
|
||
| Files: `ui/src/typeset.tsx` (the `typeset()` helper, `useTypeset` hook and a | ||
| `Typeset` block for plain text), `ui/src/components.tsx` (`Prose` gains a | ||
| `settled` prop), `ui/src/Transcript.tsx`, `ui/src/style.css`. | ||
|
|
||
| ## How it coexists with React | ||
|
|
||
| Justif rewrites the inside of each paragraph (one span per line with tuned | ||
| word spacing and tracking), and restores it on `destroy()`. React must never | ||
| update nodes Justif has rearranged, so `Prose` keys its element by content: | ||
| new text means a fresh element, the old controller is destroyed on unmount, | ||
| and the new one runs in a layout effect. Resize and font loading are handled | ||
| by Justif's own observers. | ||
|
|
||
| One CSS detail mattered: `overflow-wrap: anywhere` (which the prose container | ||
| uses so long URLs cannot blow out the layout) lets the browser take emergency | ||
| breaks inside words, which undid Justif's plan and produced a stray short | ||
| line after inline `code`. Justified blocks now set `overflow-wrap: normal`; | ||
| long unbreakable strings are handled by Justif declining that paragraph and | ||
| leaving it native. | ||
|
|
||
| ## Where else it could go | ||
|
|
||
| - The agent's tool output and comments already flow through `Prose`. The | ||
| activity feed still renders raw comment text with `white-space: pre-wrap`; | ||
| moving it to `Prose` would justify it too. | ||
| - Justif supports per-line `wdth` adjustments on variable fonts. AXP Runde is | ||
| static, so that lever is unused; Recursive (see `typography.md`) would enable it. | ||
| - German, French and twenty other hyphenation dictionaries are available if | ||
| the workspace ever carries a `lang` other than English. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import { createElement, useLayoutEffect, useRef } from "react"; | ||
| import { justify } from "justif"; | ||
| import { hyphenateEnUS } from "justif/hyphenate/en-us"; | ||
|
|
||
| /* Paragraph-level typesetting. | ||
| * | ||
| * Justif runs Knuth–Plass line breaking over each paragraph it is given, | ||
| * hyphenates with TeX patterns, hangs punctuation, and protrudes glyphs at | ||
| * the margin. It only touches elements whose computed text-align is | ||
| * `justify`, so the CSS decides which surfaces get the treatment and this | ||
| * module decides when: never while text is still streaming in, and always | ||
| * on a fresh element (callers key the element by its content) so React and | ||
| * Justif never edit the same nodes. */ | ||
|
|
||
| const BLOCKS = "p, li, dd, blockquote, figcaption"; | ||
|
|
||
| export function typeset(root: HTMLElement): () => void { | ||
| const targets = root.matches(BLOCKS) | ||
| ? [root] | ||
| : [...root.querySelectorAll<HTMLElement>(BLOCKS)]; | ||
| if (!targets.length) return () => {}; | ||
| const controller = justify(targets, { | ||
| hyphenate: hyphenateEnUS, | ||
| // Bringhurst's "at least a third": short last lines are widened rather | ||
| // than left as a single hanging word. | ||
| lastLineMinWidth: 0.33, | ||
| cleanClipboard: true, | ||
| }); | ||
| return () => controller.destroy(); | ||
| } | ||
|
|
||
| /** Typeset the referenced element once it is settled. Re-run when `key` changes. */ | ||
| export function useTypeset<T extends HTMLElement>( | ||
| settled: boolean, | ||
| key: string, | ||
| ) { | ||
| const ref = useRef<T>(null); | ||
| useLayoutEffect(() => { | ||
| if (!settled || !ref.current) return; | ||
| return typeset(ref.current); | ||
| }, [settled, key]); | ||
| return ref; | ||
| } | ||
|
|
||
| /** A justified block of plain text. The element remounts when its text changes. */ | ||
| export function Typeset({ | ||
| as = "p", | ||
| className, | ||
| children, | ||
| }: { | ||
| as?: "p" | "div"; | ||
| className?: string; | ||
| children: string; | ||
| }) { | ||
| return createElement(Block, { key: children, as, className, text: children }); | ||
| } | ||
|
|
||
| function Block({ | ||
| as, | ||
| className, | ||
| text, | ||
| }: { | ||
| as: "p" | "div"; | ||
| className?: string; | ||
| text: string; | ||
| }) { | ||
| const ref = useTypeset<HTMLElement>(true, text); | ||
| return createElement( | ||
| as, | ||
| { ref, className: className ? `typeset ${className}` : "typeset" }, | ||
| text, | ||
| ); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When settled Markdown contains an inline code token longer than the available line, this higher-specificity rule overrides the existing
code { overflow-wrap: anywhere }fallback while also disabling hyphenation. Such a token cannot wrap, and the transcript's.contribution-contentcontainer usesoverflow: hidden, so its tail is clipped on narrow panes; retain an emergency wrapping path for unbreakable code that Justif cannot set.Useful? React with 👍 / 👎.