Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple attachment button #823

Merged
merged 25 commits into from
Mar 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
7e3ffd0
Add simple attachment button
newhinton Feb 23, 2022
90b4238
fix lint
newhinton Feb 24, 2022
070fe81
overhaul buttons
newhinton Feb 27, 2022
7490913
use original filename after upload
newhinton Feb 27, 2022
112bc0a
Merge branch 'master' into feature/74/imageupload
newhinton Feb 27, 2022
8b293c2
add basic error checking for filesize
newhinton Feb 27, 2022
2525aaa
fix lint
newhinton Feb 27, 2022
688fbd5
fix lint
newhinton Mar 2, 2022
f737234
Add simple attachment button
newhinton Feb 23, 2022
fc173ee
fix lint
newhinton Feb 24, 2022
da4a3a6
overhaul buttons
newhinton Feb 27, 2022
ab6592d
use original filename after upload
newhinton Feb 27, 2022
4a2385f
add basic error checking for filesize
newhinton Feb 27, 2022
c08c3f2
fix lint
newhinton Feb 27, 2022
73ed711
fix lint
newhinton Mar 2, 2022
54ed311
use Actions and ActionButton components
korelstar Mar 13, 2022
096e042
Merge remote-tracking branch 'origin/feature/74/imageupload' into fea…
newhinton Mar 14, 2022
6facc00
Add 5px margin to new menu
newhinton Mar 14, 2022
9fc4724
let the noteservice fail properly when php-memory-limit is too low
newhinton Mar 14, 2022
4f29113
properly calculate image path offset for existing images
newhinton Mar 14, 2022
eeae9c3
refocus and add newline after image text was inserted
newhinton Mar 14, 2022
df2a1b3
fix lint
newhinton Mar 14, 2022
beb637f
fix reference to "this"
korelstar Mar 14, 2022
a9d08ce
simplify path operations by using 'path' module
korelstar Mar 14, 2022
12e973e
fix dissappearance of image button
korelstar Mar 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/Service/ImageNotWritableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace OCA\Notes\Service;

use Exception;

class ImageNotWritableException extends Exception {
}
6 changes: 5 additions & 1 deletion lib/Service/NotesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ public function getAttachment(string $userId, int $noteid, string $path) : File
* @param $noteid
* @param $fileDataArray
* @throws NotPermittedException
* @throws ImageNotWritableException
* https://github.com/nextcloud/deck/blob/master/lib/Service/AttachmentService.php
*/
public function createImage(string $userId, int $noteid, $fileDataArray) {
Expand All @@ -244,14 +245,17 @@ public function createImage(string $userId, int $noteid, $fileDataArray) {
}
$filename = $filename . '.' . explode('.', $fileDataArray['name'])[1];

if ($fileDataArray['tmp_name'] === "") {
throw new ImageNotWritableException();
}

// read uploaded file from disk
$fp = fopen($fileDataArray['tmp_name'], 'r');
$content = fread($fp, $fileDataArray['size']);
fclose($fp);

$result = [];
$result['filename'] = $filename;

$this->noteUtil->getRoot()->newFile($parent->getPath() . '/' . $filename, $content);
return $result;
}
Expand Down
97 changes: 73 additions & 24 deletions src/components/EditorEasyMDE.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
<template>
<div class="markdown-editor" @click="onClickEditor">
<textarea />
<div>
<div class="upload-button">
<Actions
container=".upload-button"
default-icon="icon-picture"
menu-align="right"
>
<ActionButton
icon="icon-upload"
:close-after-click="true"
@click="onClickUpload"
>
{{ t('notes', 'Upload image') }}
</ActionButton>
<ActionButton
icon="icon-picture"
:close-after-click="true"
@click="onClickSelect"
>
{{ t('notes', 'Insert image') }}
</ActionButton>
</Actions>
</div>
<div class="markdown-editor" @click="onClickEditor">
<textarea />
</div>
</div>
</template>
<script>
Expand All @@ -9,10 +33,22 @@ import EasyMDE from 'easymde'
import axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'
import store from '../store'
import { showError } from '@nextcloud/dialogs'
import '@nextcloud/dialogs/styles/toast.scss'
import {
Actions,
ActionButton,
} from '@nextcloud/vue'
import { basename, relative } from 'path'

export default {
name: 'EditorEasyMDE',

components: {
Actions,
ActionButton,
},

props: {
value: {
type: String,
Expand All @@ -26,6 +62,10 @@ export default {
type: String,
required: true,
},
notecategory: {
type: String,
required: true,
},
},

data() {
Expand Down Expand Up @@ -66,7 +106,7 @@ export default {
methods: {
initialize() {
const config = Object.assign({
element: this.$el.firstElementChild,
element: this.$el.lastElementChild.firstElementChild,
initialValue: this.value,
renderingConfig: {},
}, this.config)
Expand Down Expand Up @@ -132,9 +172,8 @@ export default {
},

async onClickSelect() {
const apppath = '/' + store.state.app.settings.notesPath
const categories = store.getters.getCategories()
const currentNotePath = apppath + '/' + categories
const apppath = '/' + store.state.app.settings.notesPath + '/'
const currentNotePath = apppath + this.notecategory

const doc = this.mde.codemirror.getDoc()
const cursor = this.mde.codemirror.getCursor()
Expand All @@ -149,14 +188,10 @@ export default {
)
return
}
const noteLevel = ((currentNotePath + '/').split('/').length) - 1
const imageLevel = (path.split('/').length - 1)
const upwardsLevel = noteLevel - imageLevel
for (let i = 0; i < upwardsLevel; i++) {
path = '../' + path
}
path = path.replace(apppath + '/', '')
doc.replaceRange('![' + path + '](' + path + ')', { line: cursor.line })
const label = basename(path)
const relativePath = relative(currentNotePath, path)
doc.replaceRange('![' + label + '](' + relativePath + ')\n', { line: cursor.line })
this.mde.codemirror.focus()
},
false,
['image/jpeg', 'image/png'],
Expand All @@ -167,6 +202,7 @@ export default {
},

async onClickUpload() {
const cm = this.mde.codemirror
const doc = this.mde.codemirror.getDoc()
const cursor = this.mde.codemirror.getCursor()
const id = this.noteid
Expand All @@ -176,16 +212,21 @@ export default {
temporaryInput.onchange = async function() {
const data = new FormData()
data.append('file', temporaryInput.files[0])
const response = await axios({
method: 'POST',
url: generateUrl('apps/notes') + '/notes/' + id + '/attachment',
data,
})
const name = response.data[0].filename
const position = {
line: cursor.line,
}
doc.replaceRange('![' + name + '](' + name + ')', position)
const originalFilename = temporaryInput.files[0].name

axios.post(generateUrl('apps/notes') + '/notes/' + id + '/attachment', data)
.then((response) => {
const name = response.data.filename
const position = {
line: cursor.line,
}
doc.replaceRange('![' + originalFilename + '](' + name + ')\n', position)
cm.focus()
})
.catch((error) => {
console.error(error)
showError(t('notes', 'The file was not uploaded. Check your serverlogs.'),)
})
}
temporaryInput.click()
},
Expand Down Expand Up @@ -320,4 +361,12 @@ export default {
opacity: 0.5;
text-decoration: line-through;
}

.upload-button {
position: fixed;
right: 64px;
z-index: 10;
height: 40px;
margin-right: 5px;
}
</style>
1 change: 1 addition & 0 deletions src/components/Note.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<TheEditor v-else
:value="note.content"
:noteid="noteId"
:notecategory="note.category"
:readonly="note.readonly"
@input="onEdit"
/>
Expand Down