mirror of https://github.com/vuejs/vitepress
Merge e1a7fcb2d7
into d870ba48ea
commit
e7dcbe302f
@ -0,0 +1,51 @@
|
|||||||
|
import { inBrowser } from 'vitepress'
|
||||||
|
|
||||||
|
export function useCodeModal() {
|
||||||
|
if (inBrowser) {
|
||||||
|
window.addEventListener('click', (e) => {
|
||||||
|
const el = e.target as HTMLElement
|
||||||
|
|
||||||
|
if (el.matches('div[class*="language-"] > button.modal')) {
|
||||||
|
//remove focus from button
|
||||||
|
el.blur()
|
||||||
|
|
||||||
|
const parent = el.parentElement
|
||||||
|
const sibling = el.nextElementSibling
|
||||||
|
if (!parent || !sibling) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sibling.classList.add('open')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
el.matches('div[class*="language-"] div.modal-container button.close')
|
||||||
|
) {
|
||||||
|
const parent = el.parentElement?.parentElement
|
||||||
|
if (!parent) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
parent.classList.remove('open')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (el.matches('div[class*="language-"] > div.modal-container')) {
|
||||||
|
el.classList.remove('open')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
window.addEventListener('keydown', (ev) => {
|
||||||
|
if (ev.key == 'Escape') {
|
||||||
|
let modal = window.document.querySelector(
|
||||||
|
'div[class*="language-"] > div.modal-container.open'
|
||||||
|
)
|
||||||
|
|
||||||
|
if (!modal) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
modal.classList.remove('open')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
// markdown-it plugin for generating line numbers.
|
||||||
|
// It depends on preWrapper plugin.
|
||||||
|
|
||||||
|
import type MarkdownIt from 'markdown-it'
|
||||||
|
import type { MarkdownOptions } from '../markdown'
|
||||||
|
|
||||||
|
export const codeModalPlugin = (
|
||||||
|
md: MarkdownIt,
|
||||||
|
enable = false,
|
||||||
|
options: MarkdownOptions = {}
|
||||||
|
) => {
|
||||||
|
const fence = md.renderer.rules.fence!
|
||||||
|
md.renderer.rules.fence = (...args) => {
|
||||||
|
const rawCode = fence(...args)
|
||||||
|
|
||||||
|
const [tokens, idx] = args
|
||||||
|
const info = tokens[idx].info
|
||||||
|
|
||||||
|
if (
|
||||||
|
(!enable && !/:modal($| |=)/.test(info)) ||
|
||||||
|
(enable && /:no-modal($| )/.test(info))
|
||||||
|
) {
|
||||||
|
return rawCode
|
||||||
|
}
|
||||||
|
|
||||||
|
let end = rawCode.lastIndexOf('</div>')
|
||||||
|
|
||||||
|
let innerCode =
|
||||||
|
rawCode.substring(0, end) +
|
||||||
|
`<button title="${options.codeModalButtonTitle}" class="close"></button>` +
|
||||||
|
'</div>'
|
||||||
|
|
||||||
|
const modal =
|
||||||
|
`<button title="${options.codeModalButtonTitle}" class="modal"></button>` +
|
||||||
|
'<div class="modal-container">' +
|
||||||
|
innerCode +
|
||||||
|
'</div>'
|
||||||
|
|
||||||
|
return rawCode.substring(0, end) + modal + '</div>'
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue