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 14, 2023
1 parent 32fb25b commit 3b5fa1d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 41 deletions.
4 changes: 2 additions & 2 deletions src/components/Editor/MediaHandler.vue
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export default {
const src = dirname + '/'
+ encodeURIComponent(name).replace(/[!'()*]/g, (c) => {
return '%' + c.charCodeAt(0).toString(16).toUpperCase()
})
}) + '?fileId=' + fileId + '&mimeType=' + encodeURIComponent(mimeType)
// simply get rid of brackets to make sure link text is valid
// as it does not need to be unique and matching the real file name
const alt = name.replaceAll(/[[\]]/g, '')
Expand All @@ -194,7 +194,7 @@ export default {
? this.$editor.chain().focus(position)
: this.$editor.chain()
chain.setImage({ src, alt, fileId, mimeType }).run()
chain.setImage({ src, alt }).run()
const selection = this.$editor.view.state.selection
if (!selection.empty) {
Expand Down
37 changes: 0 additions & 37 deletions src/nodes/Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ 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 @@ -103,40 +100,6 @@ 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
21 changes: 19 additions & 2 deletions src/nodes/ImageView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,23 @@ 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 @@ -238,10 +255,10 @@ export default {
},
},
fileId() {
return this.node.attrs.fileId || null
return getQueryVariable(this.src, 'fileId')
},
mimeType() {
return this.node.attrs.mimeType || null
return getQueryVariable(this.src, 'mimeType')
},
t() {
return (a, s) => window.t(a, s)
Expand Down

0 comments on commit 3b5fa1d

Please sign in to comment.