Skip to content

Commit

Permalink
add function to generate the initial doc for prosemirror
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed May 8, 2024
1 parent 7b0a65c commit 58a73c1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
3 changes: 2 additions & 1 deletion demo/prosemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import * as Y from 'yjs'
import { WebrtcProvider } from 'y-webrtc'
import { ySyncPlugin, yCursorPlugin, yUndoPlugin, undo, redo } from '../src/y-prosemirror.js'
import { ySyncPlugin, yCursorPlugin, yUndoPlugin, undo, redo, yXmlFragmentToProseMirrorRootNode } from '../src/y-prosemirror.js'
import { EditorState } from 'prosemirror-state'
import { EditorView } from 'prosemirror-view'
import { schema } from './schema.js'
Expand All @@ -20,6 +20,7 @@ window.addEventListener('load', () => {
editorContainer.insertBefore(editor, null)
const prosemirrorView = new EditorView(editor, {
state: EditorState.create({
doc: yXmlFragmentToProseMirrorRootNode(type, schema),
schema,
plugins: [
ySyncPlugin(type),
Expand Down
41 changes: 39 additions & 2 deletions src/lib.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { updateYFragment } from './plugins/sync-plugin.js' // eslint-disable-line
import { updateYFragment, createNodeFromYElement } from './plugins/sync-plugin.js' // eslint-disable-line
import { ySyncPluginKey } from './plugins/keys.js'
import * as Y from 'yjs'
import { EditorView } from 'prosemirror-view' // eslint-disable-line
import { Node, Schema } from 'prosemirror-model' // eslint-disable-line
import { Node, Schema, Fragment } from 'prosemirror-model' // eslint-disable-line
import * as error from 'lib0/error'
import * as map from 'lib0/map'
import * as eventloop from 'lib0/eventloop'
Expand Down Expand Up @@ -191,6 +191,33 @@ export const relativePositionToAbsolutePosition = (y, documentType, relPos, mapp
return pos - 1 // we don't count the most outer tag, because it is a fragment
}

/**
* Utility function for converting an Y.Fragment to a ProseMirror fragment.
*
* @param {Y.XmlFragment} yXmlFragment
* @param {Schema} schema
*/
export const yXmlFragmentToProseMirrorFragment = (yXmlFragment, schema) => {
const fragmentContent = yXmlFragment.toArray().map((t) =>
createNodeFromYElement(
/** @type {Y.XmlElement} */ (t),
schema,
new Map()
)
).filter((n) => n !== null)
return Fragment.fromArray(fragmentContent)
}

/**
* Utility function for converting an Y.Fragment to a ProseMirror node.
* This can be used for supplying the initial content to ProseMirror state.
*
* @param {Y.XmlFragment} yXmlFragment
* @param {Schema} schema
*/
export const yXmlFragmentToProseMirrorRootNode = (yXmlFragment, schema) =>
schema.topNodeType.create(null, yXmlFragmentToProseMirrorFragment(yXmlFragment, schema))

/**
* Utility method to convert a Prosemirror Doc Node into a Y.Doc.
*
Expand Down Expand Up @@ -271,6 +298,8 @@ export function prosemirrorJSONToYXmlFragment (schema, state, xmlFragment) {
}

/**
* @deprecated Use `yXmlFragmentToProseMirrorNode` instead
*
* Utility method to convert a Y.Doc to a Prosemirror Doc node.
*
* @param {Schema} schema
Expand All @@ -283,6 +312,9 @@ export function yDocToProsemirror (schema, ydoc) {
}

/**
*
* @deprecated Use `yXmlFragmentToProseMirrorNode` instead
*
* Utility method to convert a Y.XmlFragment to a Prosemirror Doc node.
*
* @param {Schema} schema
Expand All @@ -295,6 +327,9 @@ export function yXmlFragmentToProsemirror (schema, xmlFragment) {
}

/**
*
* @deprecated Use `yXmlFragmentToProseMirrorNode` instead
*
* Utility method to convert a Y.Doc to Prosemirror compatible JSON.
*
* @param {Y.Doc} ydoc
Expand All @@ -309,6 +344,8 @@ export function yDocToProsemirrorJSON (
}

/**
* @deprecated Use `yXmlFragmentToProseMirrorNode` instead
*
* Utility method to convert a Y.Doc to Prosemirror compatible JSON.
*
* @param {Y.XmlFragment} xmlFragment The fragment, which must be part of a Y.Doc.
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/sync-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ const createNodeIfNotExists = (
* @param {function('removed' | 'added', Y.ID):any} [computeYChange]
* @return {PModel.Node | null} Returns node if node could be created. Otherwise it deletes the yjs type and returns null
*/
const createNodeFromYElement = (
export const createNodeFromYElement = (
el,
schema,
mapping,
Expand Down
6 changes: 4 additions & 2 deletions src/y-prosemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ export { ySyncPlugin, isVisible, getRelativeSelection, ProsemirrorBinding, updat
export * from './plugins/undo-plugin.js'
export * from './plugins/keys.js'
export {
absolutePositionToRelativePosition, relativePositionToAbsolutePosition, setMeta, prosemirrorJSONToYDoc, yDocToProsemirrorJSON, yDocToProsemirror, prosemirrorToYDoc,
prosemirrorJSONToYXmlFragment, yXmlFragmentToProsemirrorJSON, yXmlFragmentToProsemirror, prosemirrorToYXmlFragment
absolutePositionToRelativePosition, relativePositionToAbsolutePosition, setMeta,
prosemirrorJSONToYDoc, yDocToProsemirrorJSON, yDocToProsemirror, prosemirrorToYDoc,
prosemirrorJSONToYXmlFragment, yXmlFragmentToProsemirrorJSON, yXmlFragmentToProsemirror,
prosemirrorToYXmlFragment, yXmlFragmentToProseMirrorRootNode, yXmlFragmentToProseMirrorFragment
} from './lib.js'

2 comments on commit 58a73c1

@noy4
Copy link

@noy4 noy4 commented on 58a73c1 Jun 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe typo? yXmlFragmentToProseMirrorNodeyXmlFragmentToProseMirrorRootNode

@dmonad
Copy link
Member Author

@dmonad dmonad commented on 58a73c1 Jun 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @noy4 Fixed

Please sign in to comment.