Skip to content

Commit

Permalink
feat: add default schema to ProsemirrorTransformer and default extens…
Browse files Browse the repository at this point in the history
…ions to TiptapTransformer, making both parameters in toYdoc optional
  • Loading branch information
kriskbx committed Apr 7, 2021
1 parent 0b0b76d commit e330ffd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
19 changes: 16 additions & 3 deletions packages/transformer/src/Prosemirror.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ import { Transformer } from './types'

class Prosemirror implements Transformer {

defaultSchema: Schema = new Schema({
nodes: {
text: {},
doc: { content: 'text*' },
},
})

schema(schema: Schema): Prosemirror {
this.defaultSchema = schema

return this
}

fromYdoc(document: Doc, fieldName?: string | Array<string>): any {
const data = {}

Expand All @@ -26,17 +39,17 @@ class Prosemirror implements Transformer {
return data
}

toYdoc(document: any, schema: Schema, fieldName: string | Array<string> = 'prosemirror'): Doc {
toYdoc(document: any, schema?: Schema, fieldName: string | Array<string> = 'prosemirror'): Doc {
// allow a single field name
if (typeof fieldName === 'string') {
return prosemirrorJSONToYDoc(schema, document, fieldName)
return prosemirrorJSONToYDoc(schema || this.defaultSchema, document, fieldName)
}

const ydoc = new Doc()

fieldName.forEach(field => {
const update = encodeStateAsUpdate(
prosemirrorJSONToYDoc(schema, document, field),
prosemirrorJSONToYDoc(schema || this.defaultSchema, document, field),
)

applyUpdate(ydoc, update)
Expand Down
19 changes: 16 additions & 3 deletions packages/transformer/src/Tiptap.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
import { Doc } from 'yjs'
import { Extensions } from '@tiptap/core/dist/packages/core/src/types'
import { getSchema } from '@tiptap/core'
import { defaultExtensions } from '@tiptap/starter-kit'
import { ProsemirrorTransformer } from './Prosemirror'
import { Transformer } from './types'

class Tiptap implements Transformer {
export class Tiptap implements Transformer {

defaultExtensions: Extensions = defaultExtensions()

extensions(extensions: Extensions): Tiptap {
this.defaultExtensions = extensions

return this
}

fromYdoc(document: Doc, fieldName?: string | Array<string>): any {
return ProsemirrorTransformer.fromYdoc(document, fieldName)
}

toYdoc(document: any, extensions: Extensions, fieldName: string | Array<string> = 'default'): Doc {
return ProsemirrorTransformer.toYdoc(document, getSchema(extensions), fieldName)
toYdoc(document: any, extensions?: Extensions, fieldName: string | Array<string> = 'default'): Doc {
return ProsemirrorTransformer.toYdoc(
document,
getSchema(extensions || this.defaultExtensions),
fieldName,
)
}

}
Expand Down

0 comments on commit e330ffd

Please sign in to comment.