mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-08 08:16:30 +02:00
Allow setting a defaultBlockType as object for HTML serializer (#595)
* Allow setting a defaultBlockType as object for HTML serializer * Add test for html deserializer default block as object * Code enhancement/compacting
This commit is contained in:
committed by
Ian Storm Taylor
parent
0430efcdc8
commit
add174ef37
@@ -70,6 +70,7 @@ class Html {
|
|||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @property {Array} rules
|
* @property {Array} rules
|
||||||
* @property {String} defaultBlockType
|
* @property {String} defaultBlockType
|
||||||
|
* @property {String|Object} defaultBlockType
|
||||||
*/
|
*/
|
||||||
|
|
||||||
constructor(options = {}) {
|
constructor(options = {}) {
|
||||||
@@ -108,11 +109,10 @@ class Html {
|
|||||||
return memo
|
return memo
|
||||||
}
|
}
|
||||||
|
|
||||||
const block = {
|
const commonProps = {kind: 'block', nodes: [node]}
|
||||||
kind: 'block',
|
const block = this.defaultBlockType.typeof === 'string'
|
||||||
type: this.defaultBlockType,
|
? {type: this.defaultBlockType, ...commonProps}
|
||||||
nodes: [node]
|
: {...commonProps, ...this.defaultBlockType}
|
||||||
}
|
|
||||||
|
|
||||||
memo.push(block)
|
memo.push(block)
|
||||||
return memo
|
return memo
|
||||||
|
@@ -0,0 +1,24 @@
|
|||||||
|
|
||||||
|
export default {
|
||||||
|
rules: [
|
||||||
|
{
|
||||||
|
deserialize(el, next) {
|
||||||
|
switch (el.tagName) {
|
||||||
|
case 'p': {
|
||||||
|
return {
|
||||||
|
kind: 'block',
|
||||||
|
type: 'paragraph',
|
||||||
|
nodes: next(el.children)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
defaultBlockType: {
|
||||||
|
type: 'contentBlock',
|
||||||
|
data: {
|
||||||
|
style: 'default'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,2 @@
|
|||||||
|
<p>one</p>
|
||||||
|
<div>two</div>
|
@@ -0,0 +1,25 @@
|
|||||||
|
data: {}
|
||||||
|
nodes:
|
||||||
|
- type: paragraph
|
||||||
|
isVoid: false
|
||||||
|
data: {}
|
||||||
|
nodes:
|
||||||
|
- characters:
|
||||||
|
- text: o
|
||||||
|
marks: []
|
||||||
|
- text: n
|
||||||
|
marks: []
|
||||||
|
- text: e
|
||||||
|
marks: []
|
||||||
|
- type: contentBlock
|
||||||
|
isVoid: false
|
||||||
|
data: {style: default}
|
||||||
|
nodes:
|
||||||
|
- characters:
|
||||||
|
- text: t
|
||||||
|
marks: []
|
||||||
|
- text: w
|
||||||
|
marks: []
|
||||||
|
- text: o
|
||||||
|
marks: []
|
||||||
|
|
Reference in New Issue
Block a user