Skip to content

Commit 7df2fa9

Browse files
authored
Add simple attachment button
1 parent 083d127 commit 7df2fa9

File tree

4 files changed

+89
-25
lines changed

4 files changed

+89
-25
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OCA\Notes\Service;
6+
7+
use Exception;
8+
9+
class ImageNotWritableException extends Exception {
10+
}

lib/Service/NotesService.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ public function getAttachment(string $userId, int $noteid, string $path) : File
229229
* @param $noteid
230230
* @param $fileDataArray
231231
* @throws NotPermittedException
232+
* @throws ImageNotWritableException
232233
* https://github.com/nextcloud/deck/blob/master/lib/Service/AttachmentService.php
233234
*/
234235
public function createImage(string $userId, int $noteid, $fileDataArray) {
@@ -244,14 +245,17 @@ public function createImage(string $userId, int $noteid, $fileDataArray) {
244245
}
245246
$filename = $filename . '.' . explode('.', $fileDataArray['name'])[1];
246247

248+
if ($fileDataArray['tmp_name'] === "") {
249+
throw new ImageNotWritableException();
250+
}
251+
247252
// read uploaded file from disk
248253
$fp = fopen($fileDataArray['tmp_name'], 'r');
249254
$content = fread($fp, $fileDataArray['size']);
250255
fclose($fp);
251256

252257
$result = [];
253258
$result['filename'] = $filename;
254-
255259
$this->noteUtil->getRoot()->newFile($parent->getPath() . '/' . $filename, $content);
256260
return $result;
257261
}

src/components/EditorEasyMDE.vue

Lines changed: 73 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
<template>
2-
<div class="markdown-editor" @click="onClickEditor">
3-
<textarea />
2+
<div>
3+
<div class="upload-button">
4+
<Actions
5+
container=".upload-button"
6+
default-icon="icon-picture"
7+
menu-align="right"
8+
>
9+
<ActionButton
10+
icon="icon-upload"
11+
:close-after-click="true"
12+
@click="onClickUpload"
13+
>
14+
{{ t('notes', 'Upload image') }}
15+
</ActionButton>
16+
<ActionButton
17+
icon="icon-picture"
18+
:close-after-click="true"
19+
@click="onClickSelect"
20+
>
21+
{{ t('notes', 'Insert image') }}
22+
</ActionButton>
23+
</Actions>
24+
</div>
25+
<div class="markdown-editor" @click="onClickEditor">
26+
<textarea />
27+
</div>
428
</div>
529
</template>
630
<script>
@@ -9,10 +33,22 @@ import EasyMDE from 'easymde'
933
import axios from '@nextcloud/axios'
1034
import { generateUrl } from '@nextcloud/router'
1135
import store from '../store'
36+
import { showError } from '@nextcloud/dialogs'
37+
import '@nextcloud/dialogs/styles/toast.scss'
38+
import {
39+
Actions,
40+
ActionButton,
41+
} from '@nextcloud/vue'
42+
import { basename, relative } from 'path'
1243
1344
export default {
1445
name: 'EditorEasyMDE',
1546
47+
components: {
48+
Actions,
49+
ActionButton,
50+
},
51+
1652
props: {
1753
value: {
1854
type: String,
@@ -26,6 +62,10 @@ export default {
2662
type: String,
2763
required: true,
2864
},
65+
notecategory: {
66+
type: String,
67+
required: true,
68+
},
2969
},
3070
3171
data() {
@@ -66,7 +106,7 @@ export default {
66106
methods: {
67107
initialize() {
68108
const config = Object.assign({
69-
element: this.$el.firstElementChild,
109+
element: this.$el.lastElementChild.firstElementChild,
70110
initialValue: this.value,
71111
renderingConfig: {},
72112
}, this.config)
@@ -132,9 +172,8 @@ export default {
132172
},
133173
134174
async onClickSelect() {
135-
const apppath = '/' + store.state.app.settings.notesPath
136-
const categories = store.getters.getCategories()
137-
const currentNotePath = apppath + '/' + categories
175+
const apppath = '/' + store.state.app.settings.notesPath + '/'
176+
const currentNotePath = apppath + this.notecategory
138177
139178
const doc = this.mde.codemirror.getDoc()
140179
const cursor = this.mde.codemirror.getCursor()
@@ -149,14 +188,10 @@ export default {
149188
)
150189
return
151190
}
152-
const noteLevel = ((currentNotePath + '/').split('/').length) - 1
153-
const imageLevel = (path.split('/').length - 1)
154-
const upwardsLevel = noteLevel - imageLevel
155-
for (let i = 0; i < upwardsLevel; i++) {
156-
path = '../' + path
157-
}
158-
path = path.replace(apppath + '/', '')
159-
doc.replaceRange('![' + path + '](' + path + ')', { line: cursor.line })
191+
const label = basename(path)
192+
const relativePath = relative(currentNotePath, path)
193+
doc.replaceRange('![' + label + '](' + relativePath + ')\n', { line: cursor.line })
194+
this.mde.codemirror.focus()
160195
},
161196
false,
162197
['image/jpeg', 'image/png'],
@@ -167,6 +202,7 @@ export default {
167202
},
168203
169204
async onClickUpload() {
205+
const cm = this.mde.codemirror
170206
const doc = this.mde.codemirror.getDoc()
171207
const cursor = this.mde.codemirror.getCursor()
172208
const id = this.noteid
@@ -176,16 +212,21 @@ export default {
176212
temporaryInput.onchange = async function() {
177213
const data = new FormData()
178214
data.append('file', temporaryInput.files[0])
179-
const response = await axios({
180-
method: 'POST',
181-
url: generateUrl('apps/notes') + '/notes/' + id + '/attachment',
182-
data,
183-
})
184-
const name = response.data[0].filename
185-
const position = {
186-
line: cursor.line,
187-
}
188-
doc.replaceRange('![' + name + '](' + name + ')', position)
215+
const originalFilename = temporaryInput.files[0].name
216+
217+
axios.post(generateUrl('apps/notes') + '/notes/' + id + '/attachment', data)
218+
.then((response) => {
219+
const name = response.data.filename
220+
const position = {
221+
line: cursor.line,
222+
}
223+
doc.replaceRange('![' + originalFilename + '](' + name + ')\n', position)
224+
cm.focus()
225+
})
226+
.catch((error) => {
227+
console.error(error)
228+
showError(t('notes', 'The file was not uploaded. Check your serverlogs.'),)
229+
})
189230
}
190231
temporaryInput.click()
191232
},
@@ -320,4 +361,12 @@ export default {
320361
opacity: 0.5;
321362
text-decoration: line-through;
322363
}
364+
365+
.upload-button {
366+
position: fixed;
367+
right: 64px;
368+
z-index: 10;
369+
height: 40px;
370+
margin-right: 5px;
371+
}
323372
</style>

src/components/Note.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<TheEditor v-else
3535
:value="note.content"
3636
:noteid="noteId"
37+
:notecategory="note.category"
3738
:readonly="note.readonly"
3839
@input="onEdit"
3940
/>

0 commit comments

Comments
 (0)