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.

1.3 KB
Raw Blame
<template>
  <LazyMarkdownEditor v-if="node" v-model="node" @save="data => save(data)" @auto-save="autoSave" @exit="exit" />
</template>
<script lang="ts" setup>
import type { Node } from '~/stores';

definePageMeta({ breadcrumb: { i18n: 'common.actions.edit' } });

const store = useNodesStore();

const route = useRoute();
const router = useRouter();

const nodeId = route.params.id as string;
const node = ref<Node | undefined>(undefined);
const notifications = useNotifications();

watchEffect(async () => {
  const docFromStore = store.getById(nodeId);
  if (docFromStore?.partial) {
    try {
      node.value = await store.fetch({ id: nodeId });
    } catch (error) {
      console.error('Error fetching node:', error);
    }
  } else node.value = docFromStore;
});

// Actions
const save = (doc: Partial<Node>) => {
  store
    .update(doc as Node)
    .then(() => notifications.add({ title: 'Document successfully updated', type: 'success' }))
    .catch(e => notifications.add({ message: e, title: 'Error', type: 'error' }));
};
const autoSave = (doc: Partial<Node>) => store.update(doc as Node);

const exit = () => {
  const redirect = route.query.redirect as string | undefined;
  if (redirect) router.push(redirect);
  else router.push(`/dashboard/docs/${nodeId}`);
};
</script>