Skip to content

Commit

Permalink
Improvements on images/attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
korelstar committed Mar 26, 2022
1 parent 147d79e commit d766937
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
11 changes: 6 additions & 5 deletions src/components/EditorEasyMDE.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
<ActionButton
icon="icon-upload"
:close-after-click="true"
@click="onClickUpload"
@click="onClickUploadImage"
>
{{ t('notes', 'Upload image') }}
</ActionButton>
<ActionButton
icon="icon-picture"
:close-after-click="true"
@click="onClickSelect"
@click="onClickInsertImage"
>
{{ t('notes', 'Insert image') }}
</ActionButton>
Expand Down Expand Up @@ -171,7 +171,7 @@ export default {
}
},
async onClickSelect() {
async onClickInsertImage() {
const apppath = '/' + store.state.app.settings.notesPath + '/'
const currentNotePath = apppath + this.notecategory
Expand All @@ -190,7 +190,8 @@ export default {
}
const label = basename(path)
const relativePath = relative(currentNotePath, path)
doc.replaceRange('![' + label + '](' + relativePath + ')\n', { line: cursor.line })
const encodedPath = relativePath.split('/').map(encodeURIComponent).join('/')
doc.replaceRange('![' + label + '](' + encodedPath + ')\n', { line: cursor.line })
this.mde.codemirror.focus()
},
false,
Expand All @@ -201,7 +202,7 @@ export default {
)
},
async onClickUpload() {
async onClickUploadImage() {
const cm = this.mde.codemirror
const doc = this.mde.codemirror.getDoc()
const cursor = this.mde.codemirror.getCursor()
Expand Down
49 changes: 29 additions & 20 deletions src/components/EditorMarkdownIt.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,38 @@ export default {
// If you are sure other plugins can't add `target` - drop check below
const token = tokens[idx]
const aIndex = token.attrIndex('src')
let download = false
let path = token.attrs[aIndex][1]
if (!path.startsWith('http')) {
path = generateUrl('apps/notes/notes/{id}/attachment?path={path}', { id, path })
if (!path.startsWith('http://')
&& !path.startsWith('https://')
&& !path.startsWith('data:')
) {
path = path.split('?').shift()
const lowecasePath = path.toLowerCase()
path = generateUrl(
'apps/notes/notes/{id}/attachment?path={path}',
{ id, path: decodeURIComponent(path) },
)
token.attrs[aIndex][1] = path
if (!lowecasePath.endsWith('.jpg')
&& !lowecasePath.endsWith('.jpeg')
&& !lowecasePath.endsWith('.bmp')
&& !lowecasePath.endsWith('.webp')
&& !lowecasePath.endsWith('.gif')
&& !lowecasePath.endsWith('.png')
) {
download = true
}
}
token.attrs[aIndex][1] = path
const lowecasePath = path.toLowerCase()
// pass token to default renderer.
if (lowecasePath.endsWith('jpg')
|| lowecasePath.endsWith('jpeg')
|| lowecasePath.endsWith('bmp')
|| lowecasePath.endsWith('webp')
|| lowecasePath.endsWith('gif')
|| lowecasePath.endsWith('png')) {
return defaultRender(tokens, idx, options, env, self)
} else {
if (download) {
const dlimgpath = generateUrl('svg/core/actions/download?color=ffffff')
return '<div class="download-file"><a href="' + path.replace(/"/g, '&quot;') + '"><div class="download-icon"><img class="download-icon-inner" src="' + dlimgpath + '">' + token.content + '</div></a></div>'
} else {
// pass token to default renderer.
return defaultRender(tokens, idx, options, env, self)
}
}
},
Expand Down Expand Up @@ -189,36 +202,32 @@ export default {
& img {
width: 75%;
margin-left: auto;
margin-right: auto;
display: block;
}
.download-file {
width: 75%;
margin-left: auto;
margin-right: auto;
display: block;
text-align: center;
}
.download-icon {
padding: 15px;
margin-left: auto;
margin-right: auto;
width: 75%;
border-radius: 10px;
background-color: var(--color-background-dark);
border: 1px solid transparent; // so that it does not move on hover
}
.download-icon:hover {
border: 1px var(--color-primary-element) solid;
border-color: var(--color-primary-element);
}
.download-icon-inner {
height: 3em;
width: auto;
margin-left: auto;
margin-right: auto;
margin-bottom: 5px;
}
Expand Down

0 comments on commit d766937

Please sign in to comment.