diff --git a/components/container/edit-container.tsx b/components/container/edit-container.tsx index 8e59a4af8..2c151a763 100644 --- a/components/container/edit-container.tsx +++ b/components/container/edit-container.tsx @@ -24,6 +24,7 @@ export const EditContainer = () => { abortFindNote, findOrCreateNote, initNote, + resetLocalDocState, note, } = NoteState.useContainer() const { query } = useRouter() @@ -40,13 +41,15 @@ export const EditContainer = () => { findOrCreateNote(id, { id, title: id, - content: '\n', pid: settings.daily_root_id, }) + // you can create a note via `/new` } else if (id === 'new') { const url = `/${genNewId()}?new` + (pid ? `&pid=${pid}` : '') router.replace(url, undefined, { shallow: true }) + resetLocalDocState() + // fetch note by id } else if (id && !isNew) { try { const result = await fetchNote(id) @@ -55,11 +58,14 @@ export const EditContainer = () => { return } } catch (msg) { - if (msg.name !== 'AbortError') { - toast(msg.message, 'error') - router.push('/', undefined, { shallow: true }) + if (msg instanceof Error) { + if (msg.name !== 'AbortError') { + toast(msg.message, 'error') + router.push('/', undefined, { shallow: true }) + } } } + // default } else { if (await noteCache.getItem(id)) { router.push(`/${id}`, undefined, { shallow: true }) @@ -68,11 +74,11 @@ export const EditContainer = () => { initNote({ id, - content: '\n', }) } if (!isNew && id !== 'new') { + // todo: store in localStorage mutateSettings({ last_visit: `/${id}`, }) @@ -84,6 +90,7 @@ export const EditContainer = () => { settings.daily_root_id, genNewId, pid, + resetLocalDocState, fetchNote, toast, initNote, @@ -94,7 +101,8 @@ export const EditContainer = () => { useEffect(() => { abortFindNote() loadNoteById(id) - }, [loadNoteById, abortFindNote, id]) + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [id]) useEffect(() => { updateTitle(note?.title) diff --git a/components/editor/editor.tsx b/components/editor/editor.tsx index 5fa5733ec..165bc5708 100644 --- a/components/editor/editor.tsx +++ b/components/editor/editor.tsx @@ -2,7 +2,6 @@ import { FC, useEffect, useState } from 'react' import { use100vh } from 'react-div-100vh' import MarkdownEditor, { Props } from 'rich-markdown-editor' import { useEditorTheme } from './theme' -import useMounted from 'libs/web/hooks/use-mounted' import Tooltip from './tooltip' import extensions from './extensions' import EditorState from 'libs/web/state/editor' @@ -21,13 +20,11 @@ const Editor: FC = ({ readOnly, isPreview }) => { onClickLink, onUploadImage, onHoverLink, - onEditorChange, backlinks, editorEl, note, } = EditorState.useContainer() const height = use100vh() - const mounted = useMounted() const editorTheme = useEditorTheme() const [hasMinHeight, setHasMinHeight] = useState(true) const toast = useToast() @@ -44,9 +41,8 @@ const Editor: FC = ({ readOnly, isPreview }) => { onUploadImage(file, note?.id)} diff --git a/components/editor/extensions/index.ts b/components/editor/extensions/index.ts index e2c8a934d..8b10957e9 100644 --- a/components/editor/extensions/index.ts +++ b/components/editor/extensions/index.ts @@ -1,6 +1,7 @@ import { Extension } from 'rich-markdown-editor' import Bracket from './bracket' +import YSync from './y-sync' -const extensions: Extension[] = [new Bracket()] +const extensions: Extension[] = [new Bracket(), new YSync()] export default extensions diff --git a/components/editor/extensions/y-sync.ts b/components/editor/extensions/y-sync.ts new file mode 100644 index 000000000..2ec4b05c2 --- /dev/null +++ b/components/editor/extensions/y-sync.ts @@ -0,0 +1,26 @@ +import { Extension } from 'rich-markdown-editor' +import { ySyncPlugin } from 'y-prosemirror' +import * as Y from 'yjs' + +export const YJS_DOC_KEY = 'prosemirror' +export default class YSync extends Extension { + get name() { + return 'y-sync' + } + + yDoc?: Y.Doc + + constructor(options?: Record) { + super(options) + } + + get plugins() { + if (this.yDoc) { + this.yDoc.destroy() + } + this.yDoc = new Y.Doc() + const type = this.yDoc.get(YJS_DOC_KEY, Y.XmlFragment) + + return [ySyncPlugin(type)] + } +} diff --git a/components/note-nav.tsx b/components/note-nav.tsx index 7ea41753a..e7b1b0aea 100644 --- a/components/note-nav.tsx +++ b/components/note-nav.tsx @@ -58,6 +58,8 @@ const NoteNav = () => { [note, menu] ) + const getTitle = (title?: string) => title ?? t('Untitled') + return (