Skip to content

Commit

Permalink
chore: fix saving text to clipboard for input, show button in markdow…
Browse files Browse the repository at this point in the history
…n codeblocks #2025
  • Loading branch information
marek-mihok committed Aug 17, 2023
1 parent 825e26d commit fe85040
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
11 changes: 6 additions & 5 deletions ui/src/copyable_text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ export const ClipboardCopyButton = ({ value, anchorElement, showOnHoverOnly = fa
onClick = async () => {
if (!anchorElement) return
try {
if (document.queryCommandSupported('copy') && anchorElement.tagName === 'input') {
(anchorElement as HTMLInputElement).select()
if (document.queryCommandSupported('copy')) {
anchorElement.select()
document.execCommand('copy')
window.getSelection()?.removeAllRanges()
}
Expand All @@ -91,7 +91,7 @@ export const ClipboardCopyButton = ({ value, anchorElement, showOnHoverOnly = fa
setVisible(false)
})
}
}, [value, anchorElement])
}, [anchorElement])

React.useEffect(() => () => window.clearTimeout(timeoutRef.current), [])

Expand All @@ -111,7 +111,7 @@ export const XCopyableText = ({ model }: { model: CopyableText }) => {
heightStyle = multiline && height === '1' ? fullHeightStyle : undefined,
[inputEl, setInputEl] = React.useState(),
domRef = React.useCallback(node => {
const inputEl = node?.children[0]?.children[1]
const inputEl = node?.children[0]?.children[1]?.children[0]
if (inputEl) setInputEl(inputEl)
}, [])

Expand All @@ -137,7 +137,8 @@ export const XCopyableText = ({ model }: { model: CopyableText }) => {
/>
<ClipboardCopyButton
anchorElement={inputEl}
showOnHoverOnly={!!multiline} value={value}
showOnHoverOnly={!!multiline}
value={value}
/>
</>
)
Expand Down
22 changes: 8 additions & 14 deletions ui/src/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,15 @@ const highlightSyntax = async (str: S, language: S, codeBlockId: S) => {
const highlightedCode = language
? hljs.highlight(str, { language, ignoreIllegals: true }).value
: hljs.highlightAuto(str).value
codeBlock.outerHTML = `<code id="xxxid" class="hljs">${highlightedCode}</code>`
codeBlock.outerHTML = `<code id="codeblock" class="hljs">${highlightedCode}</code>`

// TODO: Use React to render the copy button.
const cb = document.getElementById('xxxid')
const cb = document.getElementById('codeblock')
if (!cb) return

const copyButton = document.createElement("div")
if (copyButton && cb) {
ReactDOM.render(<ClipboardCopyButton value={str} anchorElementRef={cb} showOnHover />, copyButton)
cb.innerHTML += copyButton.innerHTML
}
ReactDOM.render(
ReactDOM.createPortal(<ClipboardCopyButton value={str} anchorElement={cb} showOnHoverOnly />, cb),
document.createElement('div')
)
}

export const
Expand All @@ -114,7 +113,6 @@ export const
}
}),
Markdown = ({ source }: { source: S }) => {
const componentRef = React.useRef<HTMLDivElement>(null)
const onClick = (e: React.MouseEvent<HTMLDivElement>) => {
const hrefAttr = (e.target as HTMLAnchorElement).getAttribute('href')
if (e.target instanceof HTMLAnchorElement && hrefAttr?.startsWith('?')) {
Expand All @@ -126,11 +124,7 @@ export const
return false
}
}
return <>
<div ref={componentRef} onClick={onClick} className={clas(css.markdown, 'wave-markdown')} dangerouslySetInnerHTML={{ __html: markdown.render(source) }} />
{/* TODO: Fix anchoring. */}
<ClipboardCopyButton value={'aaa'} anchorElementRef={componentRef} showOnHover />
</>
return <div onClick={onClick} className={clas(css.markdown, 'wave-markdown')} dangerouslySetInnerHTML={{ __html: markdown.render(source) }} />
}

/**
Expand Down

0 comments on commit fe85040

Please sign in to comment.