refactor: improve code readability and selection behavior in markdown-code-run.ts

This commit is contained in:
purocean
2024-07-12 17:37:53 +08:00
parent f3e7735cde
commit 85058b21a6
+13 -1
View File
@@ -171,6 +171,18 @@ const RunCode = defineComponent({
runner.value = getAllRunners().find((runner) => runner.match(props.language!, props.firstLine!))
}
const selectRunResult = (e: MouseEvent) => {
e.preventDefault()
e.stopPropagation()
const target = e.currentTarget as HTMLElement
const selection = target.ownerDocument.defaultView!.getSelection()
const range = target.ownerDocument.createRange()
range.selectNodeContents(e.currentTarget as HTMLElement)
selection?.removeAllRanges()
selection?.addRange(range)
}
watchEffect(refreshRunner)
registerHook('CODE_RUNNER_CHANGE', refreshRunner)
@@ -197,7 +209,7 @@ const RunCode = defineComponent({
onClick: runInXterm
}),
]),
h('div', { class: 'p-mcr-run-code-result skip-export', ref: resultRef }, h('output', { key: runResult, innerHTML: runResult })),
h('div', { class: 'p-mcr-run-code-result skip-export', ref: resultRef }, h('output', { onDblclick: selectRunResult, key: runResult, innerHTML: runResult })),
h('div', { class: 'p-mcr-clear-btn-wrapper skip-print' }, h(
h(
'div',