y-prosemirror
Advanced tools
Comparing version 1.2.6 to 1.2.7
@@ -102,2 +102,6 @@ /** | ||
export function yXmlFragmentToProseMirrorRootNode(yXmlFragment: Y.XmlFragment, schema: Schema): Node; | ||
export function initProseMirrorDoc(yXmlFragment: Y.XmlFragment, schema: Schema): { | ||
doc: Node; | ||
mapping: ProsemirrorMapping; | ||
}; | ||
/** | ||
@@ -104,0 +108,0 @@ * Either a node if type is YXmlElement or an Array of text nodes if YXmlText |
export function isVisible(item: Y.Item, snapshot?: Y.Snapshot): boolean; | ||
export function ySyncPlugin(yXmlFragment: Y.XmlFragment, { colors, colorMapping, permanentUserData, onFirstRender }?: YSyncOpts): any; | ||
export function ySyncPlugin(yXmlFragment: Y.XmlFragment, { colors, colorMapping, permanentUserData, onFirstRender, mapping }?: YSyncOpts): any; | ||
export function getRelativeSelection(pmbinding: any, state: any): { | ||
@@ -15,12 +15,13 @@ anchor: any; | ||
* @param {Y.XmlFragment} yXmlFragment The bind source | ||
* @param {any} prosemirrorView The target binding | ||
* @param {ProsemirrorMapping} mapping | ||
*/ | ||
constructor(yXmlFragment: Y.XmlFragment, prosemirrorView: any); | ||
constructor(yXmlFragment: Y.XmlFragment, mapping?: ProsemirrorMapping); | ||
type: Y.XmlFragment; | ||
/** | ||
* this will be set once the view is created | ||
* @type {any} | ||
*/ | ||
prosemirrorView: any; | ||
mux: import("lib0/mutex").mutex; | ||
isDestroyed: boolean; | ||
/** | ||
* @type {ProsemirrorMapping} | ||
*/ | ||
mapping: ProsemirrorMapping; | ||
@@ -87,2 +88,3 @@ _observeFunction: any; | ||
permanentUserData?: Y.PermanentUserData | null; | ||
mapping?: ProsemirrorMapping; | ||
/** | ||
@@ -89,0 +91,0 @@ * Fired when the content from Yjs is initially rendered to ProseMirror |
@@ -5,2 +5,2 @@ export * from "./plugins/cursor-plugin.js"; | ||
export { ySyncPlugin, isVisible, getRelativeSelection, ProsemirrorBinding, updateYFragment } from "./plugins/sync-plugin.js"; | ||
export { absolutePositionToRelativePosition, relativePositionToAbsolutePosition, setMeta, prosemirrorJSONToYDoc, yDocToProsemirrorJSON, yDocToProsemirror, prosemirrorToYDoc, prosemirrorJSONToYXmlFragment, yXmlFragmentToProsemirrorJSON, yXmlFragmentToProsemirror, prosemirrorToYXmlFragment, yXmlFragmentToProseMirrorRootNode, yXmlFragmentToProseMirrorFragment } from "./lib.js"; | ||
export { absolutePositionToRelativePosition, relativePositionToAbsolutePosition, setMeta, prosemirrorJSONToYDoc, yDocToProsemirrorJSON, yDocToProsemirror, prosemirrorToYDoc, prosemirrorJSONToYXmlFragment, yXmlFragmentToProsemirrorJSON, yXmlFragmentToProsemirror, prosemirrorToYXmlFragment, yXmlFragmentToProseMirrorRootNode, yXmlFragmentToProseMirrorFragment, initProseMirrorDoc } from "./lib.js"; |
{ | ||
"name": "y-prosemirror", | ||
"version": "1.2.6", | ||
"version": "1.2.7", | ||
"description": "Prosemirror bindings for Yjs", | ||
@@ -5,0 +5,0 @@ "main": "./dist/y-prosemirror.cjs", |
@@ -213,3 +213,2 @@ import { updateYFragment, createNodeFromYElement } from './plugins/sync-plugin.js' // eslint-disable-line | ||
* Utility function for converting an Y.Fragment to a ProseMirror node. | ||
* This can be used for supplying the initial content to ProseMirror state. | ||
* | ||
@@ -223,2 +222,25 @@ * @param {Y.XmlFragment} yXmlFragment | ||
/** | ||
* The initial ProseMirror content should be supplied by Yjs. This function transforms a Y.Fragment | ||
* to a ProseMirror Doc node and creates a mapping that is used by the sync plugin. | ||
* | ||
* @param {Y.XmlFragment} yXmlFragment | ||
* @param {Schema} schema | ||
*/ | ||
export const initProseMirrorDoc = (yXmlFragment, schema) => { | ||
/** | ||
* @type {ProsemirrorMapping} | ||
*/ | ||
const mapping = new Map() | ||
const fragmentContent = yXmlFragment.toArray().map((t) => | ||
createNodeFromYElement( | ||
/** @type {Y.XmlElement} */ (t), | ||
schema, | ||
mapping | ||
) | ||
).filter((n) => n !== null) | ||
const doc = schema.topNodeType.create(null, Fragment.fromArray(fragmentContent)) | ||
return { doc, mapping } | ||
} | ||
/** | ||
* Utility method to convert a Prosemirror Doc Node into a Y.Doc. | ||
@@ -225,0 +247,0 @@ * |
@@ -51,2 +51,3 @@ /** | ||
* @property {Y.PermanentUserData|null} [YSyncOpts.permanentUserData] | ||
* @property {ProsemirrorMapping} [YSyncOpts.mapping] | ||
* @property {function} [YSyncOpts.onFirstRender] Fired when the content from Yjs is initially rendered to ProseMirror | ||
@@ -91,5 +92,7 @@ */ | ||
permanentUserData = null, | ||
onFirstRender = () => {} | ||
onFirstRender = () => {}, | ||
mapping | ||
} = {}) => { | ||
let initialContentChanged = false | ||
const binding = new ProsemirrorBinding(yXmlFragment, mapping) | ||
const plugin = new Plugin({ | ||
@@ -111,3 +114,3 @@ props: { | ||
doc: yXmlFragment.doc, | ||
binding: null, | ||
binding, | ||
snapshot: null, | ||
@@ -136,3 +139,3 @@ prevSnapshot: null, | ||
pluginState.isUndoRedoOperation = change !== undefined && !!change.isChangeOrigin && !!change.isUndoRedoOperation | ||
if (pluginState.binding !== null) { | ||
if (binding.prosemirrorView !== null) { | ||
if ( | ||
@@ -144,9 +147,7 @@ change !== undefined && | ||
eventloop.timeout(0, () => { | ||
if ( | ||
pluginState.binding == null || pluginState.binding.isDestroyed | ||
) { | ||
if (binding.isDestroyed) { | ||
return | ||
} | ||
if (change.restore == null) { | ||
pluginState.binding._renderSnapshot( | ||
binding._renderSnapshot( | ||
change.snapshot, | ||
@@ -157,3 +158,3 @@ change.prevSnapshot, | ||
} else { | ||
pluginState.binding._renderSnapshot( | ||
binding._renderSnapshot( | ||
change.snapshot, | ||
@@ -167,5 +168,5 @@ change.snapshot, | ||
delete pluginState.prevSnapshot | ||
pluginState.binding.mux(() => { | ||
pluginState.binding._prosemirrorChanged( | ||
pluginState.binding.prosemirrorView.state.doc | ||
binding.mux(() => { | ||
binding._prosemirrorChanged( | ||
binding.prosemirrorView.state.doc | ||
) | ||
@@ -181,4 +182,7 @@ }) | ||
view: (view) => { | ||
const binding = new ProsemirrorBinding(yXmlFragment, view) | ||
binding._forceRerender() | ||
binding.prosemirrorView = view | ||
if (mapping == null) { | ||
// force rerender to update the bindings mapping | ||
binding._forceRerender() | ||
} | ||
onFirstRender() | ||
@@ -278,13 +282,14 @@ return { | ||
* @param {Y.XmlFragment} yXmlFragment The bind source | ||
* @param {any} prosemirrorView The target binding | ||
* @param {ProsemirrorMapping} mapping | ||
*/ | ||
constructor (yXmlFragment, prosemirrorView) { | ||
constructor (yXmlFragment, mapping = new Map()) { | ||
this.type = yXmlFragment | ||
this.prosemirrorView = prosemirrorView | ||
/** | ||
* this will be set once the view is created | ||
* @type {any} | ||
*/ | ||
this.prosemirrorView = null | ||
this.mux = createMutex() | ||
this.isDestroyed = false | ||
/** | ||
* @type {ProsemirrorMapping} | ||
*/ | ||
this.mapping = new Map() | ||
this.mapping = mapping | ||
this._observeFunction = this._typeChanged.bind(this) | ||
@@ -304,3 +309,3 @@ /** | ||
this, | ||
prosemirrorView.state | ||
this.prosemirrorView.state | ||
) | ||
@@ -382,3 +387,3 @@ } | ||
unrenderSnapshot () { | ||
this.mapping = new Map() | ||
this.mapping.clear() | ||
this.mux(() => { | ||
@@ -404,3 +409,3 @@ const fragmentContent = this.type.toArray().map((t) => | ||
_forceRerender () { | ||
this.mapping = new Map() | ||
this.mapping.clear() | ||
this.mux(() => { | ||
@@ -459,3 +464,3 @@ // If this is a forced rerender, this might neither happen as a pm change nor within a Yjs | ||
// clear mapping because we are going to rerender | ||
this.mapping = new Map() | ||
this.mapping.clear() | ||
this.mux(() => { | ||
@@ -462,0 +467,0 @@ historyDoc.transact((transaction) => { |
@@ -9,3 +9,4 @@ export * from './plugins/cursor-plugin.js' | ||
prosemirrorJSONToYXmlFragment, yXmlFragmentToProsemirrorJSON, yXmlFragmentToProsemirror, | ||
prosemirrorToYXmlFragment, yXmlFragmentToProseMirrorRootNode, yXmlFragmentToProseMirrorFragment | ||
prosemirrorToYXmlFragment, yXmlFragmentToProseMirrorRootNode, yXmlFragmentToProseMirrorFragment, | ||
initProseMirrorDoc | ||
} from './lib.js' |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
264451
4109