lexical icon indicating copy to clipboard operation
lexical copied to clipboard

Feature: Show non-printing characters

Open levensta opened this issue 6 months ago • 11 comments

Description

Inspired from https://github.com/facebook/lexical/pull/8519

Now that a generalized DOMSlot has been added to lexical and a powerful example of VisibleLineBreakExtension is available in the playground, it makes sense to expand its scope and implement a feature to display non-printing characters. This mode can be enabled in Google Docs and Word via a menu or keyboard shortcut, and certain whitespace characters will become visible. These mainly include:

  • Line break ↵ (Shift + Enter)
  • Paragraph mark (Enter)
  • Space ·
  • Tab →

How it looks in Docs and Word Image Image

In this mode, it is also useful to display ZWSP and WJ, which can be helpful for Asian languages but are also used in typography for other languages. The characters NBSP and SHY are also commonly used in typography, although they are no longer displayed in popular text editors

Impact

This is a particularly useful feature for visually distinguishing line breaks from blank paragraphs. Displaying the other characters is necessary for professional typography, when precise control over word breaks is required.

The most likely solution would be to create an extension in the playground and a button in the interface, since styling the characters requires CSS, which cannot be universal across all user editors

levensta avatar May 29 '26 22:05 levensta

Working on extending the existing VisibleLineBreakExtension to a VisibleNonPrintingExtension (playground extension with a Settings toggle, off by default) — covers ↵ on LineBreakNode, → on TabNode, and on ParagraphNode / HeadingNode / ListItemNode / QuoteNode via a shared data- attribute + CSS ::after. Ran into a layout snag on the trailing line-break case.

When the last child of a block is a LineBreakNode (Shift+Enter at the bottom), $reconcileElementTerminatingLineBreak adds a placeholder <br> so the caret has a line to sit on. The ::after renders one line below that placeholder instead of on the empty line itself, which doesn't match Word.

Tried:

  • > br:last-child { display: none } — removes the line, caret loses its placeholder
  • > br:last-child { visibility: hidden; height: 0 } — same; also bleeds into the empty <p><br></p> path
  • pseudo-element repositioning — ::after can't share the trailing <br>'s line via CSS alone
  • wrapping the __lexicalLineBreak <br> from the override's $updateDOM — fights the reconciler each turn

Any approach you'd suggest here? The other paths (empty block, text, text+Shift+Enter+text, tab, line-break, placeholder overlap) are all fine.

mayrang avatar May 30 '26 06:05 mayrang

What about something like this (obviously with a more specific selector so it's not always enabled):

.PlaygroundEditorTheme__paragraph::after {
  content: "¶";
  color: blue;
  position: absolute;
}
.PlaygroundEditorTheme__paragraph:has(br:last-child)::after {
  bottom: 0;
}
.PlaygroundEditorTheme__paragraph:last-child::after {
  display: none;
}

etrepum avatar May 30 '26 07:05 etrepum

Thanks — that pattern works. Added one tweak: narrowed :has(br:last-child) to :has(> br:last-child) since the LineBreakNode wrapper span's inner <br> is also :last-child of the wrapper and was matching when text followed the line break.

mayrang avatar May 30 '26 11:05 mayrang

Is there anything in this that wasn't addressed by #8594?

etrepum avatar Jun 12 '26 18:06 etrepum

The ¶ ↵ → · set landed in #8594. Took another look — landed up on the same wall I think. The font swap works fine for NBSP (mapped U+00A0 to a raised dot on a local branch, looks like Word's °), but for SHY, ZWSP, and WJ the browser strips the advance to 0 regardless of what's in the cmap. So the swap caps out at NBSP.

The other three would need a wrapper span on each TextNode, which I remember stopping on back then because of the regression risk on IME composition, caret, and search.

mayrang avatar Jun 12 '26 20:06 mayrang

I was thinking about using this in my own app but the extension lives in the playground only. Would it be possible to ship some of these marks as a library-level extension? I saw that some parts for example the space marker and the styles in general might be hard to generalize but thought it was worth asking.

bbertold avatar Jun 21 '26 08:06 bbertold

You’re welcome to copy it into your own project and modify as needed. The playground is for features that are not polished or extensible enough to publish on npm.

etrepum avatar Jun 21 '26 13:06 etrepum

The playground file is self-contained — every import comes from published @lexical/* packages. Copy VisibleNonPrintingExtension.ts into your project and add it to your extension dependencies. The block markers (), skip predicate (no ↵ inside code blocks), and which node classes get each marker are all configured inline — edit to match your setup.

Runtime toggle:

import {$setRenderContextValue} from '@lexical/html';
import {VisibleNonPrintingDisabled} from './VisibleNonPrintingExtension';

$setRenderContextValue(VisibleNonPrintingDisabled, true, editor);  // disable
$setRenderContextValue(VisibleNonPrintingDisabled, false, editor); // enable

mayrang avatar Jun 21 '26 14:06 mayrang

Understood thanks! And thanks @mayrang for the snippet as well!

bbertold avatar Jun 21 '26 15:06 bbertold

The hardest parts to deal with when vendoring files from the playground are that you'll have to track it carefully for updates (if you care about updates) and to figure out what CSS it needs (if any) as the CSS dependencies aren't always colocated with the extension.

etrepum avatar Jun 21 '26 15:06 etrepum

Yes, the updates are actually why I originally asked. I know these sometimes get tweaked when new browser quirks come up, and it would be nice to inherit those fixes. That said, I already found the CSS, so dropping the link here in case anyone has similar goals: https://github.com/facebook/lexical/blob/6352a9a7f99c33d10e8b2698738d3bb6c5b6fe51/packages/lexical-playground/src/themes/PlaygroundEditorTheme.css#L745

bbertold avatar Jun 22 '26 18:06 bbertold