Skip to content

Commit

Permalink
fix: open non-image attachment
Browse files Browse the repository at this point in the history
Signed-off-by: Luka Trovic <[email protected]>
  • Loading branch information
luka-nextcloud committed Mar 10, 2023
1 parent 50b68d8 commit 79ac9d4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/components/Editor/MediaHandler.vue
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export default {
? this.$editor.chain().focus(position)
: this.$editor.chain()
chain.setImage({ src, alt }).run()
chain.setImage({ src, alt, fileId, mimeType }).run()
const selection = this.$editor.view.state.selection
if (!selection.empty) {
Expand Down
37 changes: 37 additions & 0 deletions src/nodes/Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import { Plugin } from '@tiptap/pm/state'
import ImageView from './ImageView.vue'
import { VueNodeViewRenderer } from '@tiptap/vue-2'
import { defaultMarkdownSerializer } from '@tiptap/pm/markdown'
import { nodeInputRule } from '@tiptap/core'

const inputRegex = /(?:^|\s)(!\[(.+|:?)]\((\S+)(?:(?:\s+)["'](\S+)["'])?\))$/

const Image = TiptapImage.extend({

Expand Down Expand Up @@ -100,6 +103,40 @@ const Image = TiptapImage.extend({
defaultMarkdownSerializer.nodes.image(state, node, parent, index)
state.closeBlock(node)
},

addAttributes() {
return {
src: {
default: null,
},
alt: {
default: null,
},
title: {
default: null,
},
fileId: {
default: null,
},
mimeType: {
default: null,
},
}
},

addInputRules() {
return [
nodeInputRule({
find: inputRegex,
type: this.type,
getAttributes: match => {
const [,, alt, src, title, fileId, mimeType] = match

return { src, alt, title, fileId, mimeType }
},
}),
]
},
})

export default Image
34 changes: 10 additions & 24 deletions src/nodes/ImageView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -141,23 +141,6 @@ import { NodeViewWrapper } from '@tiptap/vue-2'
import { logger } from '../helpers/logger.js'
import { Image as ImageIcon, Delete as DeleteIcon } from '../components/icons.js'
const getQueryVariable = (src, variable) => {
const query = src.split('?')[1]
if (typeof query === 'undefined') {
return
}
const vars = query.split(/[&#]/)
if (typeof vars === 'undefined') {
return
}
for (let i = 0; i < vars.length; i++) {
const pair = vars[i].split('=')
if (decodeURIComponent(pair[0]) === variable) {
return decodeURIComponent(pair[1])
}
}
}
class LoadImageError extends Error {
constructor(reason, imageUrl) {
Expand Down Expand Up @@ -224,16 +207,13 @@ export default {
return this.loaded && this.imageLoaded
},
imageFileId() {
return getQueryVariable(this.src, 'fileId')
},
isSupportedImage() {
return typeof this.mime === 'undefined'
|| IMAGE_MIMES.indexOf(this.mime) !== -1
return typeof this.mimeType === 'undefined'
|| IMAGE_MIMES.indexOf(this.mimeType) !== -1
},
internalLinkOrImage() {
if (this.imageFileId) {
return generateUrl('/f/' + this.imageFileId)
if (this.fileId) {
return generateUrl('/apps/files/f/' + this.fileId)
}
return this.src
},
Expand All @@ -257,6 +237,12 @@ export default {
})
},
},
fileId() {
return this.node.attrs.fileId || null
},
mimeType() {
return this.node.attrs.mimeType || null
},
t() {
return (a, s) => window.t(a, s)
},
Expand Down

0 comments on commit 79ac9d4

Please sign in to comment.