Skip to content

Commit

Permalink
fix(Message): check for code block nodes on hover
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
  • Loading branch information
Antreesy committed Feb 7, 2025
1 parent 374c043 commit 4504958
Showing 1 changed file with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ export default {
return {
isEditing: false,
showReloadButton: false,
codeBlocks: null,
currentCodeBlock: null,
copyButtonOffset: 0,
isVisible: false,
Expand Down Expand Up @@ -334,12 +333,6 @@ export default {
if (this.isEditable) {
EventBus.on('editing-message-processing', this.setIsEditing)
}

if (!this.containsCodeBlocks) {
return
}

this.codeBlocks = Array.from(this.$refs.messageMain?.querySelectorAll('pre'))
},

beforeDestroy() {
Expand All @@ -348,15 +341,24 @@ export default {

methods: {
t,
handleMarkdownMouseOver(event) {

getCodeBlocks() {
if (!this.containsCodeBlocks) {
return
return null
}

const index = this.codeBlocks.findIndex(item => item.contains(event.target))
return Array.from(this.$refs.messageMain?.querySelectorAll('pre'))
},

handleMarkdownMouseOver(event) {
const codeBlocks = this.getCodeBlocks()
if (!codeBlocks) {
return
}
const index = codeBlocks.findIndex(item => item.contains(event.target))
if (index !== -1) {
this.currentCodeBlock = index
const el = this.codeBlocks[index]
const el = codeBlocks[index]
this.copyButtonOffset = `${el.offsetTop}px`
}
},
Expand All @@ -367,8 +369,8 @@ export default {
},

async copyCodeBlock() {
const code = this.codeBlocks[this.currentCodeBlock].textContent
try {
const code = this.getCodeBlocks()[this.currentCodeBlock].textContent
await navigator.clipboard.writeText(code)
showSuccess(t('spreed', 'Code block copied to clipboard'))
} catch (error) {
Expand Down

0 comments on commit 4504958

Please sign in to comment.