-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Add markdown code block wrap toggle #35529
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
.markup .code-block-buttons { | ||
position: absolute; | ||
top: 8px; | ||
right: 6px; | ||
display: flex; | ||
gap: 4px; | ||
visibility: hidden; | ||
animation: fadeout 0.2s both; | ||
} | ||
|
||
/* adjustments for comment content having only 14px font size */ | ||
.repository.view.issue .comment-list .comment .markup .code-copy { | ||
right: 5px; | ||
padding: 8px; | ||
} | ||
|
||
.markup .code-block-buttons .btn { | ||
background: var(--color-button) !important; | ||
padding: 9px; | ||
border: 1px solid var(--color-light-border); | ||
} | ||
|
||
.markup .code-block-buttons .btn:hover { | ||
background: var(--color-hover-opaque) !important; | ||
} | ||
|
||
.markup .code-block-buttons .btn[data-active="false"] { | ||
color: var(--color-text-light-3); | ||
} | ||
|
||
.markup .code-block-container:hover .code-block-buttons, | ||
.markup .code-block:hover .code-block-buttons { | ||
visibility: visible; | ||
animation: fadein 0.2s both; | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import {svg, type SvgName} from '../svg.ts'; | ||
import {queryElems, type DOMEvent} from '../utils/dom.ts'; | ||
|
||
export function makeCodeBlockButton(className: string, name: SvgName): HTMLButtonElement { | ||
const button = document.createElement('button'); | ||
button.classList.add(className, 'btn'); | ||
button.innerHTML = svg(name, 14); | ||
return button; | ||
} | ||
|
||
const getMarkupCodeWrap = () => (localStorage.getItem('wrap-markup-code') || 'false') === 'true'; | ||
const setMarkupCodeWrap = (value: boolean) => localStorage.setItem('wrap-markup-code', String(value)); | ||
|
||
function updateWrap(container: Element, wrap: boolean) { | ||
container.classList.remove(wrap ? 'code-overflow-scroll' : 'code-overflow-wrap'); | ||
container.classList.add(wrap ? 'code-overflow-wrap' : 'code-overflow-scroll'); | ||
} | ||
|
||
export function initMarkupCodeBlocks(elMarkup: HTMLElement): void { | ||
// .markup .code-block code | ||
queryElems(elMarkup, '.code-block code', (el) => { | ||
if (!el.textContent) return; | ||
|
||
const copyBtn = makeCodeBlockButton('code-copy', 'octicon-copy'); | ||
copyBtn.setAttribute('data-tooltip-content', window.config.i18n.copy); | ||
// remove final trailing newline introduced during HTML rendering | ||
copyBtn.setAttribute('data-clipboard-text', el.textContent.replace(/\r?\n$/, '')); | ||
|
||
// we only want to use `.code-block-container` if it exists, no matter `.code-block` exists or not. | ||
const container = el.closest('.code-block-container') ?? el.closest('.code-block'); | ||
|
||
const wrapBtn = makeCodeBlockButton('code-wrap', 'material-wrap-text'); | ||
const wrap = getMarkupCodeWrap(); | ||
wrapBtn.setAttribute('data-active', String(wrap)); | ||
updateWrap(container, wrap); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This causes a flicker on page load, but I think it's okay for now. Moving the initial classes to backend will be more work and I don't know how to use the backend user setting storage. |
||
|
||
wrapBtn.setAttribute('data-tooltip-content', window.config.i18n.code_toggle_wrap); | ||
wrapBtn.addEventListener('click', (e) => { | ||
const wrap = !getMarkupCodeWrap(); | ||
updateWrap(container, wrap); | ||
(e.currentTarget as HTMLButtonElement).setAttribute('data-active', String(wrap)); | ||
setMarkupCodeWrap(wrap); | ||
}); | ||
|
||
const btnContainer = document.createElement('div'); | ||
btnContainer.classList.add('code-block-buttons'); | ||
btnContainer.append(wrapBtn); | ||
btnContainer.append(copyBtn); | ||
container.append(btnContainer); | ||
}); | ||
} |
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.