Skip to content
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

[RFC] Use css counter for line numbers #4743

Closed
wants to merge 2 commits into from
Closed

Conversation

fantactuka
Copy link
Contributor

@fantactuka fantactuka commented Jul 5, 2023

Alternative approach for line numbers. Current implementation adds data-gutter attribute that includes line numbers that are rendered via code_block:before content. It has issues with line-wraps as data-gutter does not calculate amount of visible lines taken by one line of code. On top of it, it uses caching logic to throttle gutter recalculation and its value might become invalid.

This approach uses css counters. Spans or line breaks that are preceded by a line break are line starters, so we could use those to count lines and style their :before to include counter content. It requires zero javascript and its only downside is it can't render line numbers for empty lines because br:before is not accepting much of styles:

Screenshot 2023-07-04 at 8 27 20 PM

@vercel
Copy link

vercel bot commented Jul 5, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lexical ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 5, 2023 9:31pm
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 5, 2023 9:31pm

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 5, 2023
@github-actions
Copy link

github-actions bot commented Jul 5, 2023

size-limit report 📦

Path Size Loading time (3g) Running time (snapdragon) Total time
packages/lexical/dist/Lexical.js 27.95 KB (0%) 560 ms (0%) 245 ms (+66.83% 🔺) 804 ms
packages/lexical-rich-text/dist/LexicalRichText.js 39.01 KB (0%) 781 ms (0%) 277 ms (-24.92% 🔽) 1.1 s
packages/lexical-plain-text/dist/LexicalPlainText.js 38.98 KB (0%) 780 ms (0%) 268 ms (-23.85% 🔽) 1.1 s

@fantactuka fantactuka marked this pull request as ready for review July 6, 2023 13:38
@fantactuka fantactuka changed the title Use css counter for line numbers [RFC] Use css counter for line numbers Jul 6, 2023
@ibastawisi
Copy link
Contributor

incredible! btw you can display content before the <br> tag; this worked for me 😊

.PlaygroundTheme__code>br+br {
  display: block;
  content: counter(line-number);
  height: 20px;
}

.PlaygroundTheme__code>br+br:before {
  color: #777;
  content: counter(line-number);
  user-select: none;
  position: absolute;
  left: 5px;
  width: 50px;
}

@fantactuka fantactuka closed this Nov 28, 2023
@HarshithMjh
Copy link

Hi @fantactuka , I too needed this fix. Since you closed this, Are you planning on any alternate fix for code block line numbers issue?

@thorn0
Copy link
Contributor

thorn0 commented Dec 26, 2023

@ibastawisi That doesn't work in Firefox and Safari, unfortunately. Also in Chrome the caret starts behaving funny around empty lines. If you move it with the left/right arrow keys, it needs two key presses instead of one.

@2wheeh
Copy link
Contributor

2wheeh commented Jan 4, 2024

For me, I just needed this to style exported html without data-gutter. It helped me a lot thanks !

@ibastawisi
Copy link
Contributor

For me, I just needed this to style exported html without data-gutter. It helped me a lot thanks !

It's quite buggy as @thorn0 said, though I'm still using it on my project. But I plan to set data-gutter to the exported markup in my custom code node.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants