feat(ui): added word wrap to code viewer#2028
feat(ui): added word wrap to code viewer#2028stephansama wants to merge 7 commits intonpmx-dev:mainfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
2 Skipped Deployments
|
Lunaria Status Overview🌕 This pull request will trigger status changes. Learn moreBy default, every PR changing files present in the Lunaria configuration's You can change this by adding one of the keywords present in the Tracked Files
Warnings reference
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughAdds a word-wrap feature to the code viewer: a new optional Suggested reviewers
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment Tip Migrating from UI to YAML configuration.Use the |
There was a problem hiding this comment.
Actionable comments posted: 3
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 29d80634-bca0-4cc4-8959-376aa22e6480
📒 Files selected for processing (4)
app/components/Code/Viewer.vueapp/pages/package-code/[[org]]/[packageName]/v/[version]/[...filePath].vuei18n/locales/en.jsoni18n/schema.json
| // Synchronize line number heights with code line heights (needed for word wrap) | ||
| function syncLineHeights() { | ||
| if (!props.wordWrap || !codeRef.value || !lineNumbersRef.value) { | ||
| // Reset heights if word wrap is disabled | ||
| if (lineNumbersRef.value) { | ||
| const nums = lineNumbersRef.value.querySelectorAll<HTMLElement>('.line-number') | ||
| nums.forEach(num => (num.style.height = '')) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| const lines = codeRef.value.querySelectorAll<HTMLElement>('code > .line') | ||
| const nums = lineNumbersRef.value.querySelectorAll<HTMLElement>('.line-number') | ||
|
|
||
| lines.forEach((line, index) => { | ||
| const num = nums[index] | ||
| if (num) { | ||
| // Use getBoundingClientRect for more precision if needed, but offsetHeight is usually enough | ||
| const height = line.offsetHeight | ||
| num.style.height = `${height}px` | ||
| } | ||
| }) | ||
| } |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Verify that markdown files default to preview mode, that CodeViewer is mounted with v-show,
# and that syncLineHeights currently only reruns on html/wordWrap/resize.
rg -n -C3 "const markdownViewMode|<CodeViewer|v-show=\"!fileContent.markdownHtml \\|\\| markdownViewMode === 'code'\"" "app/pages/package-code/[[org]]/[packageName]/v/[version]/[...filePath].vue"
rg -n -C3 "function syncLineHeights|offsetHeight|watch\\(|props.wordWrap|useEventListener\\(window, 'resize'" "app/components/Code/Viewer.vue"Repository: npmx-dev/npmx.dev
Length of output: 2129
Re-sync wrapped line heights after the viewer becomes visible.
The parent component mounts CodeViewer with v-show="!fileContent.markdownHtml || markdownViewMode === 'code'" and defaults to preview mode. For markdown files, this means the component is initially hidden (display: none), yet the immediate watcher on [props.selectedLines, props.html] fires during mount and calls syncLineHeights() while the viewer is not rendered. This causes offsetHeight to return 0, setting all line-number heights to 0px. The heights are never corrected when the user switches to code view with word wrap enabled, leaving the line-number column collapsed.
Add visibility detection to skip measurement while hidden, or use a ResizeObserver on the rendered code block to continuously maintain synchronisation.
🧰 Tools
🪛 Biome (2.4.6)
[error] 43-43: This callback passed to forEach() iterable method should not return a value.
(lint/suspicious/useIterableCallbackReturn)
app/pages/package-code/[[org]]/[packageName]/v/[version]/[...filePath].vue
Outdated
Show resolved
Hide resolved
|
Hi and thank you for this change! In my opinion, and for accessibility reasons (for example for people who don't use a mouse) I believe that it's always better to wrap code regardless just using CSS. For that reason I don't think we need a toggle and the wrapping can just be implemented using CSS. |
|
Perhaps we should even have a max line length in the code viewer like in many IDEs. |
i was trying to get it to work with css only however i was having a little issue. like it would mostly work but not all the way. i can take another crack at it again when i get out of work. 🫡 |
|
I created this PR in response to the same issue, avoiding a toggle Hope I'm not stepping on toes, it's my first OSS PR ever 😅 |
I think having a toggle/setting + shortcut would be better because sometimes you do not want the code to be wrapped |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
app/pages/package-code/[[org]]/[packageName]/v/[version]/[...filePath].vue (1)
431-432: Simplify icon toggle usingv-elseand remove redundant!!.
!!wordWrapis unnecessary sincewordWrapis already a boolean ref.- Using two
v-ifdirectives instead ofv-if/v-elsemeans Vue evaluates both conditions on every render.♻️ Proposed fix
-<span v-if="!!wordWrap" class="i-lucide:wrap-text w-3 h-3" /> -<span v-if="!wordWrap" class="i-lucide:text w-3 h-3" /> +<span v-if="wordWrap" class="i-lucide:wrap-text w-3 h-3" /> +<span v-else class="i-lucide:text w-3 h-3" />
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: ab3db15d-0017-40f9-a208-64e41af7e966
📒 Files selected for processing (1)
app/pages/package-code/[[org]]/[packageName]/v/[version]/[...filePath].vue
i think the toggle is better for user experience as i prefer word wrap however regular people that i meet hate word wrap. giving the choice to the people i think is the correct move |
🔗 Linked issue
resolves #2027
🧭 Context
scrolling horizontally is annoying when trying to just read code in my opinion.
📚 Description
I just added a small button in the toolbar to toggle word wrap and a class to enable and disable word wrap
I did use gemini to help with this PR however i implemented a similar feature on my personal blog on code blocks. just used the ai to quickly get up to speed