Skip to content
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

feat: add collaboration ability #535

Closed
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
19 changes: 13 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"downshift": "^7.6.0",
"js-yaml": "4.1.0",
"lexical": "^0.16.1",
"lodash": "^4.17.21",
"mdast-util-directive": "^3.0.0",
"mdast-util-from-markdown": "^2.0.0",
"mdast-util-frontmatter": "^2.0.1",
Expand Down Expand Up @@ -106,6 +107,7 @@
"@testing-library/react": "^14.1.2",
"@types/express": "^4.17.17",
"@types/js-yaml": "4.0.5",
"@types/lodash": "^4.17.6",
"@types/mdast": "^4.0.3",
"@types/multer": "^1.4.7",
"@types/node": "^20.2.5",
Expand Down
11 changes: 11 additions & 0 deletions src/plugins/codemirror/CodeMirrorEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const CodeMirrorEditor = ({ language, nodeKey, code, focusEmitter }: Code
const { setCode } = useCodeBlockEditorContext()
const editorViewRef = React.useRef<EditorView | null>(null)
const elRef = React.useRef<HTMLDivElement | null>(null)
const extensionsRef = React.useRef<Extension | null>(null)

const setCodeRef = React.useRef(setCode)
setCodeRef.current = setCode
Expand Down Expand Up @@ -72,6 +73,7 @@ export const CodeMirrorEditor = ({ language, nodeKey, code, focusEmitter }: Code
parent: elRef.current!,
state: EditorState.create({ doc: code, extensions })
})
extensionsRef.current = extensions;
})()
return () => {
editorViewRef.current?.destroy()
Expand All @@ -80,6 +82,15 @@ export const CodeMirrorEditor = ({ language, nodeKey, code, focusEmitter }: Code
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [readOnly, language])

React.useEffect(() => {
if (editorViewRef.current && extensionsRef.current) {
const oldState = editorViewRef.current.state;
editorViewRef.current.setState(
EditorState.create({ doc: code, extensions: extensionsRef.current, selection: oldState.selection })
)
}
}, [code])

return (
<div
className={styles.codeMirrorWrapper}
Expand Down
28 changes: 19 additions & 9 deletions src/plugins/table/TableEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
createEditor
} from 'lexical'
import * as Mdast from 'mdast'
import React, { ElementType } from 'react'
import React, { ElementType, useEffect } from 'react'
import { exportLexicalTreeToMdast } from '../../exportMarkdownFromLexical'
import { importMdastTreeToLexical } from '../../importMarkdownToLexical'
import { lexicalTheme } from '../../styles/lexicalTheme'
Expand Down Expand Up @@ -45,6 +45,7 @@ import {
usedLexicalNodes$
} from '../core'
import { useCellValues } from '@mdxeditor/gurx'
import { isEqual } from "lodash"

/**
* Returns the element type for the cell based on the rowIndex
Expand Down Expand Up @@ -311,7 +312,7 @@ export interface CellProps {
align?: Mdast.AlignType
activeCell: [number, number] | null
setActiveCell: (cell: [number, number] | null) => void
focus: boolean
focus: boolean,
}

const Cell: React.FC<Omit<CellProps, 'focus'>> = ({ align, ...props }) => {
Expand Down Expand Up @@ -344,7 +345,7 @@ const CellEditor: React.FC<CellProps> = ({ focus, setActiveCell, parentEditor, l
directiveDescriptors,
codeBlockEditorDescriptors,
jsxIsAvailable,
rootEditor
rootEditor,
] = useCellValues(
importVisitors$,
exportVisitors$,
Expand All @@ -353,16 +354,26 @@ const CellEditor: React.FC<CellProps> = ({ focus, setActiveCell, parentEditor, l
directiveDescriptors$,
codeBlockEditorDescriptors$,
jsxIsAvailable$,
rootEditor$
rootEditor$,
)


const lastContents = React.useRef<Mdast.PhrasingContent[] | null>(null)
const [editor] = React.useState(() => {
const editor = createEditor({
nodes: usedLexicalNodes,
theme: lexicalTheme
})

return editor
})

useEffect(() => {
if( isEqual(contents, lastContents.current) )
return;
lastContents.current = [...contents];

editor.update(() => {
$getRoot().clear();
importMdastTreeToLexical({
root: $getRoot(),
mdastRoot: { type: 'root', children: [{ type: 'paragraph', children: contents }] },
Expand All @@ -372,9 +383,8 @@ const CellEditor: React.FC<CellProps> = ({ focus, setActiveCell, parentEditor, l
codeBlockEditorDescriptors
})
})

return editor
})
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [editor, contents, importVisitors])

const saveAndFocus = React.useCallback(
(nextCell: [number, number] | null) => {
Expand Down Expand Up @@ -462,7 +472,7 @@ const CellEditor: React.FC<CellProps> = ({ focus, setActiveCell, parentEditor, l
}, [focus, editor])

return (
<LexicalNestedComposer initialEditor={editor}>
<LexicalNestedComposer initialEditor={editor} skipCollabChecks>
<RichTextPlugin contentEditable={<ContentEditable />} placeholder={<div></div>} ErrorBoundary={LexicalErrorBoundary} />
<HistoryPlugin />
</LexicalNestedComposer>
Expand Down
6 changes: 4 additions & 2 deletions src/plugins/table/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Signal, map } from '@mdxeditor/gurx'
import * as Mdast from 'mdast'
import { gfmTableFromMarkdown, gfmTableToMarkdown } from 'mdast-util-gfm-table'
import { gfmTable } from 'micromark-extension-gfm-table'
import type { Doc } from 'yjs';
import { Provider } from '@lexical/yjs';
import {
addExportVisitor$,
addImportVisitor$,
Expand Down Expand Up @@ -80,7 +82,7 @@ export const insertTable$ = Signal<{
* @group Table
*/
export const tablePlugin = realmPlugin({
init(realm) {
init(realm, params) {
realm.pubIn({
// import
[addMdastExtension$]: gfmTableFromMarkdown(),
Expand All @@ -89,7 +91,7 @@ export const tablePlugin = realmPlugin({
// export
[addLexicalNode$]: TableNode,
[addExportVisitor$]: LexicalTableVisitor,
[addToMarkdownExtension$]: gfmTableToMarkdown({ tableCellPadding: true, tablePipeAlign: true })
[addToMarkdownExtension$]: gfmTableToMarkdown({ tableCellPadding: true, tablePipeAlign: true }),
})
}
})