@sanity/block-tools
Advanced tools
Comparing version 0.122.0-weakrefs.bc3b5c11 to 0.122.3
@@ -11,2 +11,6 @@ 'use strict'; | ||
var _randomKey = require('../util/randomKey'); | ||
var _randomKey2 = _interopRequireDefault(_randomKey); | ||
var _resolveJsType = require('../util/resolveJsType'); | ||
@@ -43,2 +47,7 @@ | ||
function sanitySpanToRawSlateBlockNode(span, sanityBlock) { | ||
if (!span._key) { | ||
span._key = (0, _randomKey2.default)(12); | ||
} | ||
if (span._type !== 'span') { | ||
@@ -77,3 +86,2 @@ return { | ||
} | ||
var range = { | ||
@@ -86,3 +94,3 @@ kind: 'range', | ||
if (!annotations) { | ||
return { kind: 'text', ranges: [range] }; | ||
return { kind: 'text', key: span._key, ranges: [range] }; | ||
} | ||
@@ -93,8 +101,10 @@ | ||
isVoid: false, | ||
key: span._key, | ||
type: 'span', | ||
data: { annotations: annotations }, | ||
nodes: [{ kind: 'text', ranges: [range] }] | ||
nodes: [{ kind: 'text', key: span._key + '0', ranges: [range] }] | ||
}; | ||
} | ||
// Block type object | ||
function sanityBlockToRawNode(sanityBlock, type) { | ||
@@ -109,4 +119,9 @@ // eslint-disable-next-line no-unused-vars | ||
if (!sanityBlock._key) { | ||
sanityBlock._key = (0, _randomKey2.default)(12); | ||
} | ||
return _extends({ | ||
kind: 'block', | ||
key: sanityBlock._key, | ||
isVoid: false, | ||
@@ -121,5 +136,10 @@ type: 'contentBlock' | ||
// Embedded object | ||
function sanityBlockItemToRaw(blockItem, type) { | ||
if (!blockItem._key) { | ||
blockItem._key = (0, _randomKey2.default)(12); | ||
} | ||
return { | ||
kind: 'block', | ||
key: blockItem._key, | ||
type: type ? type.name : '__unknown', // __unknown is needed to map to component in slate schema, see prepareSlateForBlockEditor.js | ||
@@ -126,0 +146,0 @@ isVoid: true, |
@@ -19,9 +19,14 @@ 'use strict'; | ||
var _randomKey = require('../util/randomKey'); | ||
var _randomKey2 = _interopRequireDefault(_randomKey); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function toSanitySpan(blockNode, sanityBlock) { | ||
if (blockNode.kind === 'text') { | ||
return blockNode.ranges.map(function (range) { | ||
function toSanitySpan(node, sanityBlock, spanIndex) { | ||
if (node.kind === 'text') { | ||
return node.ranges.map(function (range) { | ||
return { | ||
_type: 'span', | ||
_key: '' + sanityBlock._key + spanIndex(), | ||
text: range.text, | ||
@@ -34,12 +39,12 @@ marks: range.marks.map(function (mark) { | ||
} | ||
if (blockNode.kind === 'inline') { | ||
var nodes = blockNode.nodes, | ||
data = blockNode.data; | ||
if (node.kind === 'inline') { | ||
var nodes = node.nodes, | ||
data = node.data; | ||
return (0, _flatten3.default)(nodes.map(function (node) { | ||
if (node.kind !== 'text') { | ||
throw new Error('Unexpected non-text child node for inline text: ' + node.kind); | ||
return (0, _flatten3.default)(nodes.map(function (nodesNode) { | ||
if (nodesNode.kind !== 'text') { | ||
throw new Error('Unexpected non-text child node for inline text: ' + nodesNode.kind); | ||
} | ||
if (blockNode.type !== 'span') { | ||
return blockNode.data.value; | ||
if (node.type !== 'span') { | ||
return node.data.value; | ||
} | ||
@@ -56,5 +61,6 @@ var annotations = data.annotations; | ||
} | ||
return node.ranges.map(function (range) { | ||
return nodesNode.ranges.map(function (range) { | ||
return { | ||
_type: 'span', | ||
_key: '' + sanityBlock._key + spanIndex(), | ||
text: range.text, | ||
@@ -68,3 +74,3 @@ marks: range.marks.map(function (mark) { | ||
} | ||
throw new Error('Unsupported kind ' + blockNode.kind); | ||
throw new Error('Unsupported kind ' + node.kind); | ||
} | ||
@@ -76,6 +82,11 @@ | ||
_type: 'block', | ||
_key: block.key || block.data._key || (0, _randomKey2.default)(12), | ||
markDefs: block.data.markDefs || [] | ||
}); | ||
var index = 0; | ||
var spanIndex = function spanIndex() { | ||
return index++; | ||
}; | ||
sanityBlock.children = (0, _flatten3.default)(block.nodes.map(function (node) { | ||
return toSanitySpan(node, sanityBlock); | ||
return toSanitySpan(node, sanityBlock, spanIndex); | ||
})); | ||
@@ -82,0 +93,0 @@ return sanityBlock; |
{ | ||
"name": "@sanity/block-tools", | ||
"version": "0.122.0-weakrefs.bc3b5c11", | ||
"version": "0.122.3", | ||
"description": "Can format HTML, Slate JSON or Sanity block array into any other format.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
51509
1233