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

Enhance Y.XMLFragment to/from JSON serialization #53

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

SamDuvall
Copy link

If the y-prosemirror utility functions were called from a Y.XMLElement perspective, instead of a Y.Doc with a xmlFragment key perspective, developers could re-use the utility functions to perform Y.XMLElement updates on the server. This would give developers another tool to build documents on a server before they are served to clients.

export const appendNodes = (
  yXmlFragment: Y.XmlFragment,
  jsonNodes: XmlNode[],
  schema: Schema
) => {
  // Create the initial state
  const json = yXmlFragmentToProsemirrorJSON(yXmlFragment)
  const doc = Node.fromJSON(schema, json)
  const state = EditorState.create({ doc, schema })
  
  // Create the new state
  const nodes = jsonNodes.map(node => Node.fromJSON(schema, node))
  const endPosition = state.doc.content.size
  const tr = state.tr.insert(endPosition, nodes)
  const newState = state.apply(tr)

  // Update the yDoc
  updateYXmlFragmentFromProsemirror(type, newState.doc)
}

@SamDuvall
Copy link
Author

I've had a slight change of heart from my initial comment. I really only needed updateYFragment export from the sync-plugin.

However, I do think the signature for yDocToProsemirrorJSON isn't quite right, because I have a use case where my Y.XmlFragment is not hanging off of the Y.Doc, which I think is a valid use case. I exported another function yXmlFragmentToProsemirrorJSON which converts a Y.XmlFragment to JSON and then had yDocToProsemirrorJSON use yXmlFragmentToProsemirrorJSON to preserve backward-compatibility and not add any duplication.

@SamDuvall SamDuvall changed the title Added method to update a Y.XMLFragment from a Prosemirror Doc Node Enhance Y.XMLFragment to/from JSON serialization Aug 17, 2021
Copy link
Member

@dmonad dmonad left a comment

Choose a reason for hiding this comment

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

Looks good. Please just clarify this one thing.

@@ -1,5 +1,5 @@
export * from './plugins/cursor-plugin.js'
export { ySyncPlugin, isVisible, getRelativeSelection, ProsemirrorBinding } from './plugins/sync-plugin.js'
export { ySyncPlugin, isVisible, getRelativeSelection, updateYFragment, ProsemirrorBinding } from './plugins/sync-plugin.js'
Copy link
Member

Choose a reason for hiding this comment

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

Do you need updateYFragment? I don't think this should be exported.

Copy link
Author

Choose a reason for hiding this comment

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

No, what I really want is prosemirrorJSONToYFragment instead of prosemirrorJSONToYDoc. Should I make a separate PR or amend this one?

Copy link
Contributor

Choose a reason for hiding this comment

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

Just wondering, would prosemirrorJSONToYFragment also allow for the conversion of json to Y.XMLElement?

I'm looking through y-prosemirror looking for blockers preventing people from using Y.XMLElement as the type (in the case of a custom top document), and noticing prosemirrorJSONToYDoc hardcodes the Y.XMLFragment with ydoc.get(xmlFragment, Y.XmlFragment).

Right now, one can't make a prosemirrorJSONToYXmlElement because updateYFragment isn't exported as you pointed out.

Copy link
Contributor

@tommoor tommoor Feb 28, 2022

Choose a reason for hiding this comment

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

updateYFragment exported with a slightly cleaner interface would be incredibly useful. Perhaps something with the signature of: applyProsemirrorToYFragment(ydoc, prosemirrorDoc)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants