Skip to content

Integrate Nextcloud Text as editor for notes #652

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@

use OCA\Notes\Service\NotesService;

use OCA\Viewer\Event\LoadViewer;

use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUserSession;
Expand All @@ -19,6 +22,8 @@ class PageController extends Controller {
private $notesService;
/** @var IUserSession */
private $userSession;
/** @var IEventDispatcher */
private $eventDispatcher;
/** @IURLGenerator */
private $urlGenerator;

Expand All @@ -27,11 +32,13 @@ public function __construct(
IRequest $request,
NotesService $notesService,
IUserSession $userSession,
IEventDispatcher $eventDispatcher,
IURLGenerator $urlGenerator
) {
parent::__construct($AppName, $request);
$this->notesService = $notesService;
$this->userSession = $userSession;
$this->eventDispatcher = $eventDispatcher;
$this->urlGenerator = $urlGenerator;
}

Expand All @@ -41,6 +48,7 @@ public function __construct(
* @NoCSRFRequired
*/
public function index() : TemplateResponse {
$this->eventDispatcher->dispatch(LoadViewer::class, new LoadViewer());
$devMode = !is_file(dirname(__FILE__).'/../../js/notes-main.js');
$response = new TemplateResponse(
$this->appName,
Expand Down
147 changes: 31 additions & 116 deletions src/components/Note.vue
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
<template>
<AppContent :class="{ loading: loading || isManualSave, 'icon-error': !loading && (!note || note.error), 'sidebar-open': sidebarOpen }">
<AppContent :class="{ loading: loading, 'icon-error': !loading && (!note || note.error), 'sidebar-open': sidebarOpen }">
<div v-if="!loading && note && !note.error && !note.deleting"
id="note-container"
class="note-container"
:class="{ fullscreen: fullscreen }"
>
<div class="note-editor">
<div v-show="!note.content" class="placeholder">
{{ preview ? t('notes', 'Empty note') : t('notes', 'Write …') }}
</div>
<ThePreview v-if="preview" :value="note.content" />
<TheEditor v-else :value="note.content" @input="onEdit" />
<component :is="viewer.component"
ref="texteditor"
:fileid="fileId"
:basename="title"
:active="true"
:has-preview="true"
mime="text/markdown"
class="text-editor"
@ready="onEditorReady"
/>
</div>
<span class="action-buttons">
<Actions :open.sync="actionsOpen" menu-align="right">
Expand All @@ -20,13 +25,6 @@
>
{{ t('notes', 'Details') }}
</ActionButton>
<ActionButton
v-tooltip.left="t('notes', 'CTRL + /')"
:icon="preview ? 'icon-rename' : 'icon-toggle'"
@click="onTogglePreview"
>
{{ preview ? t('notes', 'Edit') : t('notes', 'Preview') }}
</ActionButton>
<ActionButton
icon="icon-fullscreen"
:class="{ active: fullscreen }"
Expand All @@ -35,11 +33,6 @@
{{ fullscreen ? t('notes', 'Exit full screen') : t('notes', 'Full screen') }}
</ActionButton>
</Actions>
<button v-show="note.saveError"
v-tooltip.right="t('notes', 'Save failed. Click to retry.')"
class="action-error icon-error-color"
@click="onManualSave"
/>
</span>
</div>
</AppContent>
Expand All @@ -53,13 +46,10 @@ import {
Tooltip,
isMobile,
} from '@nextcloud/vue'
import { showError } from '@nextcloud/dialogs'
import { emit } from '@nextcloud/event-bus'

import { config } from '../config'
import { fetchNote, refreshNote, saveNote, saveNoteManually, autotitleNote, routeIsNewNote } from '../NotesService'
import TheEditor from './EditorEasyMDE'
import ThePreview from './EditorMarkdownIt'
import { autotitleNote, routeIsNewNote } from '../NotesService'
import store from '../store'

export default {
Expand All @@ -69,8 +59,6 @@ export default {
Actions,
ActionButton,
AppContent,
TheEditor,
ThePreview,
},

directives: {
Expand All @@ -90,11 +78,8 @@ export default {
return {
loading: false,
fullscreen: false,
preview: false,
actionsOpen: false,
autosaveTimer: null,
autotitleTimer: null,
refreshTimer: null,
etag: null,
}
},
Expand All @@ -109,33 +94,37 @@ export default {
isNewNote() {
return routeIsNewNote(this.$route)
},
isManualSave() {
return store.state.app.isManualSave
},
sidebarOpen() {
return store.state.app.sidebarOpen
},
fileId() {
return parseInt(this.noteId)
},
viewer() {
return OCA.Viewer.availableHandlers.filter(h => h.id === 'text')[0]
},
},

watch: {
$route(to, from) {
if (to.name !== from.name || to.params.noteId !== from.params.noteId) {
this.fetchData()
// this.loading = true
this.initNote()
this.$refs.texteditor.$children[0].reconnect()
}
},
title: 'onUpdateTitle',
},

created() {
this.fetchData()
this.initNote()
document.addEventListener('webkitfullscreenchange', this.onDetectFullscreen)
document.addEventListener('mozfullscreenchange', this.onDetectFullscreen)
document.addEventListener('fullscreenchange', this.onDetectFullscreen)
document.addEventListener('keydown', this.onKeyPress)
},

destroyed() {
this.stopRefreshTimer()
document.removeEventListener('webkitfullscreenchange', this.onDetectFullscreen)
document.removeEventListener('mozfullscreenchange', this.onDetectFullscreen)
document.removeEventListener('fullscreenchange', this.onDetectFullscreen)
Expand All @@ -145,31 +134,14 @@ export default {
},

methods: {
fetchData() {
initNote() {
store.commit('setSidebarOpen', false)
this.etag = null
this.stopRefreshTimer()

if (this.isMobile) {
emit('toggle-navigation', { open: false })
}

this.onUpdateTitle(this.title)
this.loading = true
this.preview = false
fetchNote(parseInt(this.noteId))
.then((note) => {
if (note.errorMessage) {
showError(note.errorMessage)
}
this.startRefreshTimer()
})
.catch(() => {
// note not found
})
.then(() => {
this.loading = false
})
},

onUpdateTitle(title) {
Expand All @@ -181,11 +153,6 @@ export default {
}
},

onTogglePreview() {
this.preview = !this.preview
this.actionsOpen = false
},

onDetectFullscreen() {
this.fullscreen = document.fullScreen || document.mozFullScreen || document.webkitIsFullScreen
},
Expand Down Expand Up @@ -226,38 +193,13 @@ export default {
this.actionsOpen = false
},

stopRefreshTimer() {
if (this.refreshTimer !== null) {
clearTimeout(this.refreshTimer)
this.refreshTimer = null
}
},

startRefreshTimer() {
this.stopRefreshTimer()
this.refreshTimer = setTimeout(() => {
this.refreshTimer = null
this.refreshNote()
}, config.interval.note.refresh * 1000)
},

refreshNote() {
if (this.note.unsaved) {
this.startRefreshTimer()
return
}
refreshNote(parseInt(this.noteId), this.etag).then(etag => {
if (etag) {
this.etag = etag
this.$forceUpdate()
}
this.startRefreshTimer()
})
onEditorReady() {
console.debug('onEditorReady')
this.loading = false
},

onEdit(newContent) {
if (this.note.content !== newContent) {
this.stopRefreshTimer()
const note = {
...this.note,
content: newContent,
Expand All @@ -266,18 +208,6 @@ export default {
store.commit('updateNote', note)
this.$forceUpdate()

// queue auto saving note content
if (this.autosaveTimer === null) {
this.autosaveTimer = setTimeout(() => {
this.autosaveTimer = null
saveNote(note.id)
}, config.interval.note.autosave * 1000)
}

// (re-) start auto refresh timer
// TODO should be after save is finished
this.startRefreshTimer()

// stop old autotitle timer
if (this.autotitleTimer !== null) {
clearTimeout(this.autotitleTimer)
Expand All @@ -296,26 +226,6 @@ export default {
},

onKeyPress(event) {
if (event.ctrlKey || event.metaKey) {
switch (event.key.toLowerCase()) {
case 's':
event.preventDefault()
this.onManualSave()
break
case '/':
event.preventDefault()
this.onTogglePreview()
break
}
}
},

onManualSave() {
const note = {
...this.note,
}
store.commit('updateNote', note)
saveNoteManually(this.note.id)
},
},
}
Expand All @@ -334,6 +244,11 @@ export default {
padding-bottom: 0;
}

.text-editor {
position: absolute !important;
top: 0 !important;
}

/* center editor on large screens */
@media (min-width: 1600px) {
.note-editor {
Expand Down