Nicer code copy, bumped to v28

This commit is contained in:
Magnus Åhall 2026-06-16 09:43:31 +02:00
parent 7c46127938
commit c36b4ace13
2 changed files with 11 additions and 9 deletions

View file

@ -23,7 +23,7 @@ import (
"text/template"
)
const VERSION = "v27"
const VERSION = "v28"
const CONTEXT_USER = 1
const SYNC_PAGINATION = 200

View file

@ -94,7 +94,7 @@ export class MarkedPosition {
constructor() {// {{{
window.marked_setpos = (event) => this.setpos(event)
window.marked_changecheckbox = (event) => this.changecheckbox(event)
window.marked_copy_to_clipboard = (event) => this.copy_to_clipboard(event)
window.marked_copy_to_clipboard = (event, tagname) => this.copy_to_clipboard(event, tagname)
this.render()
}// }}}
setpos(event) {// {{{
@ -120,7 +120,7 @@ export class MarkedPosition {
}
})
}// }}}
async copy_to_clipboard(event) {// {{{
async copy_to_clipboard(event, tagname) {// {{{
if (!event.shiftKey)
return
@ -134,8 +134,11 @@ export class MarkedPosition {
const text = event.target.innerText
await navigator.clipboard.writeText(text)
event.target.classList.add('copy')
setTimeout(()=>event.target.classList.remove('copy'), 250)
const tagClasslist = event.target.closest(tagname).classList
tagClasslist.add('copy')
setTimeout(()=>tagClasslist.remove('copy'), 250)
} catch (err) {
console.error('Failed to copy: ', err)
alert('Failed to copy: ', err)
@ -143,7 +146,6 @@ export class MarkedPosition {
}// }}}
render() {// {{{
const markedObject = this
this.marked = new Marked()
this.marked.use(markedTokenPosition())
this.marked.use({
@ -188,12 +190,12 @@ export class MarkedPosition {
const code = token.text.replace(other.endingNewline, '') + '\n'
if (!langString) {
return `<pre ondblclick="marked_setpos(event)" onmousedown="marked_copy_to_clipboard(event)" data-offset-start="${token.position.start.offset}" data-offset-end="${token.position.end.offset}"><code>`
return `<pre ondblclick="marked_setpos(event)" onmousedown="marked_copy_to_clipboard(event, 'pre')" data-offset-start="${token.position.start.offset}" data-offset-end="${token.position.end.offset}"><code>`
+ (token.escaped ? code : escapeHtmlEntities(code, true))
+ '</code></pre>\n'
}
return `<pre ondblclick="marked_setpos(event)" onmousedown="marked_copy_to_clipboard(event)" data-offset-start="${token.position.start.offset}" data-offset-end="${token.position.end.offset}"><code class="language-`
return `<pre ondblclick="marked_setpos(event)" onmousedown="marked_copy_to_clipboard(event, 'pre')" data-offset-start="${token.position.start.offset}" data-offset-end="${token.position.end.offset}"><code class="language-`
+ escapeHtmlEntities(langString)
+ '">'
+ (token.escaped ? code : escapeHtmlEntities(code, true))
@ -283,7 +285,7 @@ export class MarkedPosition {
},
codespan(token) {
return `<code ondblclick="marked_setpos(event)" onmousedown="marked_copy_to_clipboard(event)" data-offset-start="${token.position.start.offset}" data-offset-end="${token.position.end.offset}">${escapeHtmlEntities(token.text, true)}</code>`
return `<code ondblclick="marked_setpos(event)" onmousedown="marked_copy_to_clipboard(event, 'code')" data-offset-start="${token.position.start.offset}" data-offset-end="${token.position.end.offset}">${escapeHtmlEntities(token.text, true)}</code>`
},
br(token) {