allow parseOptions to be passed to setContent and Editor constructor
This commit is contained in:
@@ -27,6 +27,7 @@ export default class Editor {
|
||||
},
|
||||
useBuiltInExtensions: true,
|
||||
dropCursor: {},
|
||||
parseOptions: {},
|
||||
onInit: () => {},
|
||||
onUpdate: () => {},
|
||||
onFocus: () => {},
|
||||
@@ -185,7 +186,7 @@ export default class Editor {
|
||||
})
|
||||
}
|
||||
|
||||
createDocument(content) {
|
||||
createDocument(content, parseOptions = this.options.parseOptions) {
|
||||
if (content === null) {
|
||||
return this.schema.nodeFromJSON(this.options.emptyDocument)
|
||||
}
|
||||
@@ -203,7 +204,7 @@ export default class Editor {
|
||||
const element = document.createElement('div')
|
||||
element.innerHTML = content.trim()
|
||||
|
||||
return DOMParser.fromSchema(this.schema).parse(element)
|
||||
return DOMParser.fromSchema(this.schema).parse(element, parseOptions)
|
||||
}
|
||||
|
||||
return false
|
||||
@@ -321,10 +322,10 @@ export default class Editor {
|
||||
return this.state.doc.toJSON()
|
||||
}
|
||||
|
||||
setContent(content = {}, emitUpdate = false) {
|
||||
setContent(content = {}, emitUpdate = false, parseOptions) {
|
||||
this.state = EditorState.create({
|
||||
schema: this.state.schema,
|
||||
doc: this.createDocument(content),
|
||||
doc: this.createDocument(content, parseOptions),
|
||||
plugins: this.state.plugins,
|
||||
})
|
||||
|
||||
|
||||
@@ -278,3 +278,52 @@ test('update callback', done => {
|
||||
|
||||
editor.setContent('<p>Bar</p>', true)
|
||||
})
|
||||
|
||||
test('parse options in set content', done => {
|
||||
const editor = new Editor({
|
||||
content: '<p>Foo</p>',
|
||||
onUpdate: ({ getHTML, getJSON }) => {
|
||||
expect(getHTML()).toEqual('<p> Foo </p>')
|
||||
expect(getJSON()).toEqual({
|
||||
type: 'doc',
|
||||
content: [
|
||||
{
|
||||
type: 'paragraph',
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: ' Foo ',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
})
|
||||
done()
|
||||
},
|
||||
})
|
||||
|
||||
editor.setContent('<p> Foo </p>', true, { preserveWhitespace: true })
|
||||
})
|
||||
|
||||
test('parse options in constructor', () => {
|
||||
const editor = new Editor({
|
||||
content: '<p> Foo </p>',
|
||||
parseOptions: { preserveWhitespace: true },
|
||||
})
|
||||
|
||||
expect(editor.getHTML()).toEqual('<p> Foo </p>')
|
||||
expect(editor.getJSON()).toEqual({
|
||||
type: 'doc',
|
||||
content: [
|
||||
{
|
||||
type: 'paragraph',
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: ' Foo ',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user