Skip to content

Commit f228d16

Browse files
committed
feat(editor): Use text editor
Signed-off-by: Julius Härtl <[email protected]>
1 parent 3e403aa commit f228d16

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

lib/Controller/PageController.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
use OCA\Notes\Service\NotesService;
88

9+
use OCA\Text\Event\LoadEditor;
910
use OCP\AppFramework\Controller;
1011
use OCP\AppFramework\Http\TemplateResponse;
1112
use OCP\AppFramework\Http\ContentSecurityPolicy;
1213
use OCP\AppFramework\Http\RedirectResponse;
14+
use OCP\EventDispatcher\IEventDispatcher;
1315
use OCP\IRequest;
1416
use OCP\IURLGenerator;
1517
use OCP\IUserSession;
@@ -18,18 +20,21 @@ class PageController extends Controller {
1820
private NotesService $notesService;
1921
private IUserSession $userSession;
2022
private IURLGenerator $urlGenerator;
23+
private IEventDispatcher $eventDispatcher;
2124

2225
public function __construct(
2326
string $AppName,
2427
IRequest $request,
2528
NotesService $notesService,
2629
IUserSession $userSession,
27-
IURLGenerator $urlGenerator
30+
IURLGenerator $urlGenerator,
31+
IEventDispatcher $eventDispatcher
2832
) {
2933
parent::__construct($AppName, $request);
3034
$this->notesService = $notesService;
3135
$this->userSession = $userSession;
3236
$this->urlGenerator = $urlGenerator;
37+
$this->eventDispatcher = $eventDispatcher;
3338
}
3439

3540

@@ -45,6 +50,10 @@ public function index() : TemplateResponse {
4550
[ ]
4651
);
4752

53+
if (class_exists(LoadEditor::class)) {
54+
$this->eventDispatcher->dispatchTyped(new LoadEditor());
55+
}
56+
4857
$csp = new ContentSecurityPolicy();
4958
$csp->addAllowedImageDomain('*');
5059
$response->setContentSecurityPolicy($csp);

src/components/Note.vue

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<template>
22
<NcAppContent :class="{ loading: loading || isManualSave, 'icon-error': !loading && (!note || note.error), 'sidebar-open': sidebarOpen }">
3-
<div v-if="!loading && note && !note.error && !note.deleting"
3+
<div class="text-editor" v-if="useTextEditor" ref="editor" v-show="!loading"></div>
4+
<div v-else-if="!loading && note && !note.error && !note.deleting"
45
id="note-container"
56
class="note-container"
67
:class="{ fullscreen: fullscreen }"
@@ -27,7 +28,7 @@
2728
</div>
2829
</div>
2930
</NcModal>
30-
<div class="note-editor">
31+
<div class="note-editor" v-else>
3132
<div v-show="!note.content" class="placeholder">
3233
{{ preview ? t('notes', 'Empty note') : t('notes', 'Write …') }}
3334
</div>
@@ -154,6 +155,7 @@ export default {
154155
155156
data() {
156157
return {
158+
useTextEditor: true,
157159
loading: false,
158160
fullscreen: false,
159161
preview: false,
@@ -163,6 +165,7 @@ export default {
163165
refreshTimer: null,
164166
etag: null,
165167
showConflict: false,
168+
editor: null,
166169
}
167170
},
168171
@@ -217,8 +220,10 @@ export default {
217220
this.onUpdateTitle(null)
218221
},
219222
223+
224+
220225
methods: {
221-
fetchData() {
226+
async fetchData() {
222227
this.etag = null
223228
this.stopRefreshTimer()
224229
@@ -235,11 +240,38 @@ export default {
235240
showError(t('notes', 'Error from Nextcloud server: {msg}', { msg: note.errorType }))
236241
}
237242
this.startRefreshTimer()
243+
this.loadTextEditor()
238244
})
239245
.catch(() => {
240246
// note not found
241247
})
242248
.then(() => {
249+
this.loading = this.useTextEditor || false
250+
})
251+
},
252+
253+
async loadTextEditor() {
254+
if (!this.useTextEditor) {
255+
return
256+
}
257+
if (this.useTextEditor && !this.$refs?.editor) {
258+
await this.$nextTick
259+
}
260+
this?.editor?.destroy()
261+
this.loading = true
262+
this.editor = (await window.OCA.Text.createEditor({
263+
el: this.$refs.editor,
264+
fileId: parseInt(this.noteId),
265+
readOnly: false,
266+
onUpdate: ({ markdown }) => {
267+
if (this.note) {
268+
this.onEdit(markdown)
269+
}
270+
//this.content = markdown
271+
//this.editorFile.setContent(markdown)
272+
},
273+
}))
274+
.onLoaded(() => {
243275
this.loading = false
244276
})
245277
},
@@ -299,6 +331,7 @@ export default {
299331
},
300332
301333
onVisibilityChange() {
334+
return;
302335
if (document.visibilityState === 'visible') {
303336
this.stopRefreshTimer()
304337
this.refreshNote()
@@ -336,6 +369,21 @@ export default {
336369
},
337370
338371
onEdit(newContent) {
372+
if (this.useTextEditor) {
373+
const note = {
374+
...this.note,
375+
content: newContent,
376+
unsaved: true,
377+
}
378+
store.commit('updateNote', note)
379+
if (this.isNewNote) {
380+
queueCommand(note.id, 'autotitle')
381+
}
382+
this.$forceUpdate()
383+
384+
return
385+
}
386+
339387
if (this.note.content !== newContent) {
340388
this.stopRefreshTimer()
341389
const note = {
@@ -376,6 +424,9 @@ export default {
376424
},
377425
378426
onKeyPress(event) {
427+
if (this.useTextEditor) {
428+
return
429+
}
379430
if (event.ctrlKey || event.metaKey) {
380431
switch (event.key.toLowerCase()) {
381432
case 's':
@@ -391,6 +442,10 @@ export default {
391442
},
392443
393444
onManualSave() {
445+
if (!this.useTextEditor) {
446+
return
447+
}
448+
394449
const note = {
395450
...this.note,
396451
}
@@ -413,6 +468,10 @@ export default {
413468
}
414469
</script>
415470
<style scoped>
471+
.text-editor {
472+
height: 100%;
473+
}
474+
416475
.note-container {
417476
min-height: 100%;
418477
width: 100%;

0 commit comments

Comments
 (0)