Skip to content

Commit

Permalink
initialContent option
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHung committed May 20, 2021
1 parent ccfd444 commit c96ec7c
Show file tree
Hide file tree
Showing 10 changed files with 27,683 additions and 48 deletions.
27,629 changes: 27,629 additions & 0 deletions demo/dist/prosemirror.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions demo/dist/prosemirror.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion dist/src/plugins/sync-plugin.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export function isVisible(item: any, snapshot?: any): boolean;
export function ySyncPlugin(yXmlFragment: any, { colors, colorMapping, permanentUserData }?: YSyncOpts): any;
export function ySyncPlugin(yXmlFragment: any, { colors, colorMapping, permanentUserData, initialContent }?: YSyncOpts): any;
export function getRelativeSelection(pmbinding: any, state: any): {
anchor: any;
head: any;
Expand Down Expand Up @@ -69,6 +69,7 @@ export type YSyncOpts = {
colors?: Array<ColorDef>;
colorMapping?: Map<string, ColorDef>;
permanentUserData?: any | null;
initialContent?: JSON | null;
};
export type NormalizedPNodeContent = (PMNode<any> | PMNode<any>[])[];
import { Node as PMNode } from "prosemirror-model";
23 changes: 12 additions & 11 deletions dist/test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -9574,6 +9574,7 @@ const isVisible$1 = (item, snapshot) => snapshot === undefined ? !item.deleted :
* @property {Array<ColorDef>} [YSyncOpts.colors]
* @property {Map<string,ColorDef>} [YSyncOpts.colorMapping]
* @property {Y.PermanentUserData|null} [YSyncOpts.permanentUserData]
* @property {JSON|null} [YSyncOpts.initialContent]
*/

/**
Expand Down Expand Up @@ -9602,7 +9603,7 @@ const getUserColor = (colorMapping, colors, user) => {
* @param {YSyncOpts} opts
* @return {any} Returns a prosemirror plugin that binds to this type
*/
const ySyncPlugin = (yXmlFragment, { colors = defaultColors, colorMapping = new Map(), permanentUserData = null } = {}) => {
const ySyncPlugin = (yXmlFragment, { colors = defaultColors, colorMapping = new Map(), permanentUserData = null, initialContent = null } = {}) => {
let changedInitialContent = false;
const plugin = new prosemirrorState.Plugin({
props: {
Expand Down Expand Up @@ -9657,16 +9658,16 @@ const ySyncPlugin = (yXmlFragment, { colors = defaultColors, colorMapping = new
}
},
view: view => {

const binding = new ProsemirrorBinding(yXmlFragment, view);
binding._forceRerender();
view.dispatch(view.state.tr.setMeta(ySyncPluginKey, { binding }));

return {
update: () => {
const pluginState = plugin.getState(view.state);
if (pluginState.snapshot == null && pluginState.prevSnapshot == null) {
if (changedInitialContent || view.state.doc.content.findDiffStart(view.state.doc.type.createAndFill().content) != null) {
changedInitialContent = changedInitialContent || initialContent
? view.state.doc.content.findDiffStart(view.state.schema.nodeFromJSON(initialContent)) !== null
: view.state.doc.content.findDiffStart(view.state.doc.type.createAndFill().content) !== null;
if (changedInitialContent) {
changedInitialContent = true;
binding._prosemirrorChanged(view.state.doc);
}
Expand Down Expand Up @@ -9804,12 +9805,12 @@ class ProsemirrorBinding {
_forceRerender () {
this.mapping = new Map();
this.mux(() => {
let content = this.type.toArray().map(t => createNodeFromYElement(/** @type {Y.XmlElement} */ (t), this.prosemirrorView.state.schema, this.mapping)).filter(n => n !== null);
if (content.length) {
// @ts-ignore
const tr = this.prosemirrorView.state.tr.replaceWith(0, this.prosemirrorView.state.doc.content.size, new prosemirrorModel.Fragment(content));
this.prosemirrorView.dispatch(tr);
}
// const fragmentContent = this.type.toArray().map(t => createNodeFromYElement(/** @type {Y.XmlElement} */ (t), this.prosemirrorView.state.schema, this.mapping)).filter(n => n !== null)
// @ts-ignore
// const tr = this.prosemirrorView.state.tr.replace(0, this.prosemirrorView.state.doc.content.size, new PModel.Slice(new PModel.Fragment(fragmentContent), 0, 0)) const tr = this.prosemirrorView.state.tr
const tr = this.prosemirrorView.state.tr;
tr.setMeta(ySyncPluginKey, { binding: this });
this.prosemirrorView.dispatch(tr);
});
}

Expand Down
2 changes: 1 addition & 1 deletion dist/test.cjs.map

Large diffs are not rendered by default.

23 changes: 12 additions & 11 deletions dist/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23911,6 +23911,7 @@
* @property {Array<ColorDef>} [YSyncOpts.colors]
* @property {Map<string,ColorDef>} [YSyncOpts.colorMapping]
* @property {Y.PermanentUserData|null} [YSyncOpts.permanentUserData]
* @property {JSON|null} [YSyncOpts.initialContent]
*/

/**
Expand Down Expand Up @@ -23939,7 +23940,7 @@
* @param {YSyncOpts} opts
* @return {any} Returns a prosemirror plugin that binds to this type
*/
const ySyncPlugin = (yXmlFragment, { colors = defaultColors, colorMapping = new Map(), permanentUserData = null } = {}) => {
const ySyncPlugin = (yXmlFragment, { colors = defaultColors, colorMapping = new Map(), permanentUserData = null, initialContent = null } = {}) => {
let changedInitialContent = false;
const plugin = new Plugin({
props: {
Expand Down Expand Up @@ -23994,16 +23995,16 @@
}
},
view: view => {

const binding = new ProsemirrorBinding(yXmlFragment, view);
binding._forceRerender();
view.dispatch(view.state.tr.setMeta(ySyncPluginKey, { binding }));

return {
update: () => {
const pluginState = plugin.getState(view.state);
if (pluginState.snapshot == null && pluginState.prevSnapshot == null) {
if (changedInitialContent || view.state.doc.content.findDiffStart(view.state.doc.type.createAndFill().content) != null) {
changedInitialContent = changedInitialContent || initialContent
? view.state.doc.content.findDiffStart(view.state.schema.nodeFromJSON(initialContent)) !== null
: view.state.doc.content.findDiffStart(view.state.doc.type.createAndFill().content) !== null;
if (changedInitialContent) {
changedInitialContent = true;
binding._prosemirrorChanged(view.state.doc);
}
Expand Down Expand Up @@ -24141,12 +24142,12 @@
_forceRerender () {
this.mapping = new Map();
this.mux(() => {
let content = this.type.toArray().map(t => createNodeFromYElement(/** @type {Y.XmlElement} */ (t), this.prosemirrorView.state.schema, this.mapping)).filter(n => n !== null);
if (content.length) {
// @ts-ignore
const tr = this.prosemirrorView.state.tr.replaceWith(0, this.prosemirrorView.state.doc.content.size, new Fragment(content));
this.prosemirrorView.dispatch(tr);
}
// const fragmentContent = this.type.toArray().map(t => createNodeFromYElement(/** @type {Y.XmlElement} */ (t), this.prosemirrorView.state.schema, this.mapping)).filter(n => n !== null)
// @ts-ignore
// const tr = this.prosemirrorView.state.tr.replace(0, this.prosemirrorView.state.doc.content.size, new PModel.Slice(new PModel.Fragment(fragmentContent), 0, 0)) const tr = this.prosemirrorView.state.tr
const tr = this.prosemirrorView.state.tr;
tr.setMeta(ySyncPluginKey, { binding: this });
this.prosemirrorView.dispatch(tr);
});
}

Expand Down
2 changes: 1 addition & 1 deletion dist/test.js.map

Large diffs are not rendered by default.

23 changes: 12 additions & 11 deletions dist/y-prosemirror.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const isVisible = (item, snapshot) => snapshot === undefined ? !item.deleted : (
* @property {Array<ColorDef>} [YSyncOpts.colors]
* @property {Map<string,ColorDef>} [YSyncOpts.colorMapping]
* @property {Y.PermanentUserData|null} [YSyncOpts.permanentUserData]
* @property {JSON|null} [YSyncOpts.initialContent]
*/

/**
Expand Down Expand Up @@ -92,7 +93,7 @@ const getUserColor = (colorMapping, colors, user) => {
* @param {YSyncOpts} opts
* @return {any} Returns a prosemirror plugin that binds to this type
*/
const ySyncPlugin = (yXmlFragment, { colors = defaultColors, colorMapping = new Map(), permanentUserData = null } = {}) => {
const ySyncPlugin = (yXmlFragment, { colors = defaultColors, colorMapping = new Map(), permanentUserData = null, initialContent = null } = {}) => {
let changedInitialContent = false;
const plugin = new prosemirrorState.Plugin({
props: {
Expand Down Expand Up @@ -147,16 +148,16 @@ const ySyncPlugin = (yXmlFragment, { colors = defaultColors, colorMapping = new
}
},
view: view => {

const binding = new ProsemirrorBinding(yXmlFragment, view);
binding._forceRerender();
view.dispatch(view.state.tr.setMeta(ySyncPluginKey, { binding }));

return {
update: () => {
const pluginState = plugin.getState(view.state);
if (pluginState.snapshot == null && pluginState.prevSnapshot == null) {
if (changedInitialContent || view.state.doc.content.findDiffStart(view.state.doc.type.createAndFill().content) != null) {
changedInitialContent = changedInitialContent || initialContent
? view.state.doc.content.findDiffStart(view.state.schema.nodeFromJSON(initialContent)) !== null
: view.state.doc.content.findDiffStart(view.state.doc.type.createAndFill().content) !== null;
if (changedInitialContent) {
changedInitialContent = true;
binding._prosemirrorChanged(view.state.doc);
}
Expand Down Expand Up @@ -294,12 +295,12 @@ class ProsemirrorBinding {
_forceRerender () {
this.mapping = new Map();
this.mux(() => {
let content = this.type.toArray().map(t => createNodeFromYElement(/** @type {Y.XmlElement} */ (t), this.prosemirrorView.state.schema, this.mapping)).filter(n => n !== null);
if (content.length) {
// @ts-ignore
const tr = this.prosemirrorView.state.tr.replaceWith(0, this.prosemirrorView.state.doc.content.size, new prosemirrorModel.Fragment(content));
this.prosemirrorView.dispatch(tr);
}
// const fragmentContent = this.type.toArray().map(t => createNodeFromYElement(/** @type {Y.XmlElement} */ (t), this.prosemirrorView.state.schema, this.mapping)).filter(n => n !== null)
// @ts-ignore
// const tr = this.prosemirrorView.state.tr.replace(0, this.prosemirrorView.state.doc.content.size, new PModel.Slice(new PModel.Fragment(fragmentContent), 0, 0)) const tr = this.prosemirrorView.state.tr
const tr = this.prosemirrorView.state.tr;
tr.setMeta(ySyncPluginKey, { binding: this });
this.prosemirrorView.dispatch(tr);
});
}

Expand Down
2 changes: 1 addition & 1 deletion dist/y-prosemirror.cjs.map

Large diffs are not rendered by default.

23 changes: 12 additions & 11 deletions src/plugins/sync-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const isVisible = (item, snapshot) => snapshot === undefined ? !item.dele
* @property {Array<ColorDef>} [YSyncOpts.colors]
* @property {Map<string,ColorDef>} [YSyncOpts.colorMapping]
* @property {Y.PermanentUserData|null} [YSyncOpts.permanentUserData]
* @property {JSON|null} [YSyncOpts.initialContent]
*/

/**
Expand Down Expand Up @@ -65,7 +66,7 @@ const getUserColor = (colorMapping, colors, user) => {
* @param {YSyncOpts} opts
* @return {any} Returns a prosemirror plugin that binds to this type
*/
export const ySyncPlugin = (yXmlFragment, { colors = defaultColors, colorMapping = new Map(), permanentUserData = null } = {}) => {
export const ySyncPlugin = (yXmlFragment, { colors = defaultColors, colorMapping = new Map(), permanentUserData = null, initialContent = null } = {}) => {
let changedInitialContent = false
const plugin = new Plugin({
props: {
Expand Down Expand Up @@ -120,16 +121,16 @@ export const ySyncPlugin = (yXmlFragment, { colors = defaultColors, colorMapping
}
},
view: view => {

const binding = new ProsemirrorBinding(yXmlFragment, view);
binding._forceRerender();
view.dispatch(view.state.tr.setMeta(ySyncPluginKey, { binding }));

return {
update: () => {
const pluginState = plugin.getState(view.state)
if (pluginState.snapshot == null && pluginState.prevSnapshot == null) {
if (changedInitialContent || view.state.doc.content.findDiffStart(view.state.doc.type.createAndFill().content) != null) {
changedInitialContent = changedInitialContent || initialContent
? view.state.doc.content.findDiffStart(view.state.schema.nodeFromJSON(initialContent)) !== null
: view.state.doc.content.findDiffStart(view.state.doc.type.createAndFill().content) !== null
if (changedInitialContent) {
changedInitialContent = true
binding._prosemirrorChanged(view.state.doc)
}
Expand Down Expand Up @@ -267,12 +268,12 @@ export class ProsemirrorBinding {
_forceRerender () {
this.mapping = new Map()
this.mux(() => {
let content = this.type.toArray().map(t => createNodeFromYElement(/** @type {Y.XmlElement} */ (t), this.prosemirrorView.state.schema, this.mapping)).filter(n => n !== null);
if (content.length) {
// @ts-ignore
const tr = this.prosemirrorView.state.tr.replaceWith(0, this.prosemirrorView.state.doc.content.size, new PMFragment(content));
this.prosemirrorView.dispatch(tr);
}
// const fragmentContent = this.type.toArray().map(t => createNodeFromYElement(/** @type {Y.XmlElement} */ (t), this.prosemirrorView.state.schema, this.mapping)).filter(n => n !== null)
// @ts-ignore
// const tr = this.prosemirrorView.state.tr.replace(0, this.prosemirrorView.state.doc.content.size, new PModel.Slice(new PModel.Fragment(fragmentContent), 0, 0)) const tr = this.prosemirrorView.state.tr
const tr = this.prosemirrorView.state.tr
tr.setMeta(ySyncPluginKey, { binding: this })
this.prosemirrorView.dispatch(tr)
})
}

Expand Down

0 comments on commit c96ec7c

Please sign in to comment.