Skip to content

πŸ“š The open-source, offline-first Notion, Obsidian & Confluence alternative. Advanced Markdown, multi-tenant teams, OIDC/SSO, and local S3 backups. Deploy in one command.

Refactor markdown editor

Smaug6739c926d731f203
diff --git a/frontend/components/MarkdownEditor/Editor.ts b/frontend/components/MarkdownEditor/Editor.ts
index 3c116e34..c39b5ad3 100644
--- a/frontend/components/MarkdownEditor/Editor.ts
+++ b/frontend/components/MarkdownEditor/Editor.ts
@@ -67,6 +67,12 @@ export class Editor extends EventTarget {
case 'preview':
this.showPreview.value = !this.showPreview.value;
break;
+ case 'exit':
+ this.dispatchEvent(new Event('exit'));
+ break;
+ case 'save':
+ this.dispatchEvent(new Event('save'));
+ break;
}
}
public handleKeydown(event: KeyboardEvent) {
@@ -104,13 +110,13 @@ export class Editor extends EventTarget {
// Ctrl + S for saving
if (event.ctrlKey && event.key === 's') {
event.preventDefault();
- this.dispatchEvent(new Event('save'));
+ this.actions('save');
}
if (event.ctrlKey && event.key === 'x') {
// If no text is selected, exit editor
if (this.area.value!.selectionStart === this.area.value!.selectionEnd) {
event.preventDefault();
- this.dispatchEvent(new Event('exit'));
+ this.actions('exit');
}
}
// Ctrl + P for preview
diff --git a/frontend/components/MarkdownEditor/MarkdownEditor.vue b/frontend/components/MarkdownEditor/MarkdownEditor.vue
index 4e1fc64d..47c6d54d 100644
--- a/frontend/components/MarkdownEditor/MarkdownEditor.vue
+++ b/frontend/components/MarkdownEditor/MarkdownEditor.vue
@@ -1,10 +1,10 @@
<template>
<div class="editor-container" @keydown="editor.handleKeydown">
- <Toolbar style="height: fit-content" :document="document" @execute-action="(a: string) => exec(a)" :minimal="options?.toolbar == 'minimal'" />
+ <Toolbar :document="document" :minimal="options?.toolbar == 'minimal'" @execute-action="(a:string) => editor.actions(a)" />
<input placeholder="Title" class="title" v-model="document.name" />
<input placeholder="Description" class="description" v-model="document.description" />
<div class="markdown" ref="container">
- <InlineToolbar ref="toolbar" @execute-action="(a: string) => exec(a)" />
+ <InlineToolbar ref="toolbar" @execute-action="(a:string) => editor.actions(a)" />
<textarea ref="textarea" class="content markdown-input" @scroll="syncScroll" @input="update" v-html="document.content_markdown || ''" placeholder="Write something or use the toolbar to create your document..."></textarea>
<div v-if="editor.showPreview.value" class="markdown-preview document-theme" ref="markdownPreview" v-html="document.content_html || ''"></div>
</div>
@@ -29,37 +29,24 @@ const textarea = ref<HTMLTextAreaElement>();
const editor = new Editor(textarea, toolbar, container, props.options);
-const emit = defineEmits(['save']);
+const emit = defineEmits(['save', 'exit']);
const update = () => (document.value.content_html = compile(textarea.value?.value || ''));
const handleClick = () => editor.handleInlineToolbar();
const markdownPreview = ref<HTMLDivElement>();
-editor.addEventListener('save', () => exec('save'));
-editor.addEventListener('exit', () => exec('exit'));
+editor.addEventListener('save', save);
+editor.addEventListener('exit', () => emit('exit'));
onMounted(() => window.addEventListener('mouseup', handleClick));
onBeforeUnmount(() => window.removeEventListener('mouseup', handleClick));
-function exec(action: string) {
- editor.actions(action);
- switch (action) {
- case 'exit':
- if (props.options?.toolbar == 'minimal') return;
- useRouter().push(document.value.id ? `/dashboard/docs/${document.value.id}` : '/dashboard');
- break;
- case 'save':
- emitSave();
- }
- update();
-}
-
function syncScroll() {
editor.handleInlineToolbar();
if (!textarea.value || !markdownPreview.value) return;
const scrollPercentage = textarea.value.scrollTop / (textarea.value.scrollHeight - textarea.value.clientHeight);
markdownPreview.value.scrollTop = scrollPercentage * (markdownPreview.value.scrollHeight - markdownPreview.value.clientHeight);
}
-function emitSave() {
+function save() {
document.value.content_markdown = textarea.value?.value || '';
document.value.content_html = compile(document.value.content_markdown);
emit('save', document.value);
diff --git a/frontend/pages/dashboard/docs/edit/[id].vue b/frontend/pages/dashboard/docs/edit/[id].vue
index bf349d08..0fd10eca 100644
--- a/frontend/pages/dashboard/docs/edit/[id].vue
+++ b/frontend/pages/dashboard/docs/edit/[id].vue
@@ -1,5 +1,5 @@
<template>
- <MarkdownEditor v-if="document" ref="editor" :doc="document" @save="data => save(data)" />
+ <MarkdownEditor v-if="document" ref="editor" :doc="document" @save="data => save(data)" @exit="exit" />
</template>
<script lang="ts" setup>
import type { Document } from '@/stores';
@@ -32,4 +32,7 @@ function save(doc: Document) {
.then(() => notifications.add({ title: 'Success:', message: 'Document updated', type: 'success', timeout: 3000 }))
.catch(e => notifications.add({ title: 'Error:', message: e, type: 'error', timeout: 3000 }));
}
+function exit() {
+ useRouter().push(`/dashboard/docs/${doc_id}`);
+}
</script>
diff --git a/frontend/pages/dashboard/docs/new.vue b/frontend/pages/dashboard/docs/new.vue
index 87e9686f..2fda7838 100644
--- a/frontend/pages/dashboard/docs/new.vue
+++ b/frontend/pages/dashboard/docs/new.vue
@@ -1,12 +1,11 @@
<template>
- <MarkdownEditor ref="editor" :doc="document" @save="data => save(data)" />
+ <MarkdownEditor :doc="document" @save="data => save(data)" @exit="exit" />
</template>
<script lang="ts" setup>
import MarkdownEditor from '~/components/MarkdownEditor/MarkdownEditor.vue';
import type { Document } from '@/stores';
const store = useDocumentsStore();
-const editor = ref();
const document = ref<Partial<Document>>({
category: useSidebar().workspaceId.value || undefined,
accessibility: 1,
@@ -24,4 +23,7 @@ function save(doc: Document) {
})
.catch(e => notifications.add({ title: 'Error:', message: e, type: 'error', timeout: 3000 }));
}
+function exit() {
+ useRouter().push('/dashboard');
+}
</script>