Skip to content

Commit

Permalink
forced, custom layouts with y-prosemirror
Browse files Browse the repository at this point in the history
The following commit allows y-prosemirror to work with documents with forced, custom layouts. An example of this is a document with a required title, with the schema defined as `content: "title block+"`.

Previously, it was assumed that an empty ProseMirror document is a single paragraph node, with content size of 2. As a result, when ProseMirror initializes an empty document with a custom layout (with content size > 2), the sync-plugin erroneously triggers a call to `binding._prosemirrorChanged` even though the initial content has not changed.

To calculate the size of an empty ProseMirror document with *any* custom layout, we use `view.state.doc` to obtain the node describing the schema (layout) and call `createAndFill().content.size` (ProseMirror documentation [here](https://prosemirror.net/docs/ref/#model.NodeType.createAndFill)). We then use this constant instead of 2 in the comparison for `changedInitialContent`.
  • Loading branch information
BrianHung authored Mar 14, 2020
1 parent dcdd7d1 commit 2de9e41
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/plugins/sync-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ export const ySyncPlugin = (yXmlFragment, { colors = defaultColors, colorMapping
update: () => {
const pluginState = plugin.getState(view.state)
if (pluginState.snapshot == null && pluginState.prevSnapshot == null) {
if (changedInitialContent || view.state.doc.content.size > 2) {
const emptySize = view.state.doc.createAndFill().content.size;
if (changedInitialContent || view.state.doc.content.size > emptySize) {
changedInitialContent = true
binding._prosemirrorChanged(view.state.doc)
}
Expand Down

0 comments on commit 2de9e41

Please sign in to comment.