slate-hyperscript
Advanced tools
Comparing version 0.10.8 to 0.11.0
@@ -7,2 +7,64 @@ # Changelog | ||
### `0.11.0` — October 9, 2018 | ||
###### BREAKING | ||
**Updated to the latest version of `slate`.** The `slate-hyperscript` codebase has been updated to be compatible with the latest version of `slate`, `0.42.0`. This is a backward incompatible upgrade, and so the peer dependency range has been bumped. | ||
**`slate-hyperscript` no longer normalizes values.** This behavior was very problematic because it meant that you could not determine exactly what output you'd receive from any given hyperscript creation. The logic for creating child nodes was inconsistent, relying on the built-in normalization to help keep it "normal". While this is sometimes helpful, it makes writing tests for invalid states very tricky, if not impossible. | ||
Now, `slate-hyperscript` does not do any normalization, meaning that you can create any document structure with it. For example, you can create a block node inside an inline node, even though a Slate editor wouldn't allow it. Or, if you don't create leaf text nodes, they won't exist in the output. | ||
For example these are no longer equivalent: | ||
```jsx | ||
<document> | ||
<paragraph> | ||
<link>word</link> | ||
</paragraph> | ||
</document> | ||
``` | ||
```jsx | ||
<document> | ||
<paragraph> | ||
<text /> | ||
<link>word</link> | ||
<text /> | ||
</paragraph> | ||
</document> | ||
``` | ||
Similarly, these are no longer equivalent either: | ||
```jsx | ||
<document> | ||
<paragraph /> | ||
</document> | ||
``` | ||
```jsx | ||
<document> | ||
<paragraph> | ||
<text /> | ||
</paragraph> | ||
</document> | ||
``` | ||
This allows you to much more easily test invalid states and transition states. However, it means that you need to be more explicit in the "normal" states than previously. | ||
**The `<text>` and `<mark>` creators now return useful objects.** This is a related change that makes the library more useful. Previously you could expect to receive a `value` from the `<value>` creator, but the others were less consistent. For example, the `<text>` creator would actually return an array, instead of the `Text` node that you expect. | ||
```js | ||
// Previously you had to do... | ||
const text = <text>word</text>[0] | ||
// But now it's more obvious... | ||
const text = <text>word</text> | ||
``` | ||
Similarly, the `mark` creator used to return a `Text` node. Now it returns a list of `Leaf` objects, which can be passed directly as children to the `<text>` creator. | ||
--- | ||
### `0.10.0` — August 22, 2018 | ||
@@ -9,0 +71,0 @@ |
@@ -98,473 +98,583 @@ (function (global, factory) { | ||
/** | ||
* Point classes that can be created at different points in the document and | ||
* then searched for afterwards, for creating ranges. | ||
* Auto-incrementing ID to keep track of paired decorations. | ||
* | ||
* @type {Class} | ||
* @type {Number} | ||
*/ | ||
var CursorPoint = function CursorPoint() { | ||
classCallCheck(this, CursorPoint); | ||
var uid = 0; | ||
this.offset = null; | ||
}; | ||
/** | ||
* Create an anchor point. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {AnchorPoint} | ||
*/ | ||
var AnchorPoint = function AnchorPoint() { | ||
var attrs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
classCallCheck(this, AnchorPoint); | ||
var _attrs$key = attrs.key, | ||
key = _attrs$key === undefined ? null : _attrs$key, | ||
_attrs$offset = attrs.offset, | ||
offset = _attrs$offset === undefined ? null : _attrs$offset, | ||
_attrs$path = attrs.path, | ||
path = _attrs$path === undefined ? null : _attrs$path; | ||
function createAnchor(tagName, attributes, children) { | ||
return new AnchorPoint(attributes); | ||
} | ||
this.key = key; | ||
this.offset = offset; | ||
this.path = path; | ||
}; | ||
/** | ||
* Create a block. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {Block} | ||
*/ | ||
var FocusPoint = function FocusPoint() { | ||
var attrs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
classCallCheck(this, FocusPoint); | ||
var _attrs$key2 = attrs.key, | ||
key = _attrs$key2 === undefined ? null : _attrs$key2, | ||
_attrs$offset2 = attrs.offset, | ||
offset = _attrs$offset2 === undefined ? null : _attrs$offset2, | ||
_attrs$path2 = attrs.path, | ||
path = _attrs$path2 === undefined ? null : _attrs$path2; | ||
function createBlock(tagName, attributes, children) { | ||
var attrs = _extends({}, attributes, { object: 'block' }); | ||
var block = createNode('node', attrs, children); | ||
return block; | ||
} | ||
this.key = key; | ||
this.offset = offset; | ||
this.path = path; | ||
}; | ||
/** | ||
* Create a cursor point. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {CursorPoint} | ||
*/ | ||
var DecorationPoint = function DecorationPoint(attrs) { | ||
var _this = this; | ||
function createCursor(tagName, attributes, children) { | ||
return new CursorPoint(attributes); | ||
} | ||
classCallCheck(this, DecorationPoint); | ||
/** | ||
* Create a decoration point, or wrap a list of leaves and set the decoration | ||
* point tracker on them. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {DecorationPoint|List<Leaf>} | ||
*/ | ||
this.combine = function (focus) { | ||
if (!(focus instanceof DecorationPoint)) { | ||
throw new Error('misaligned decorations'); | ||
} | ||
function createDecoration(tagName, attributes, children) { | ||
var key = attributes.key, | ||
data = attributes.data; | ||
return slate.Decoration.create({ | ||
anchor: { | ||
key: _this.key, | ||
offset: _this.offset | ||
}, | ||
focus: { | ||
key: focus.key, | ||
offset: focus.offset | ||
}, | ||
mark: { | ||
type: _this.type, | ||
data: _this.data | ||
} | ||
}); | ||
}; | ||
var type = tagName; | ||
var _attrs$key3 = attrs.key, | ||
key = _attrs$key3 === undefined ? null : _attrs$key3, | ||
_attrs$data = attrs.data, | ||
data = _attrs$data === undefined ? {} : _attrs$data, | ||
type = attrs.type; | ||
if (key) { | ||
return new DecorationPoint({ id: key, type: type, data: data }); | ||
} | ||
this.id = key; | ||
this.offset = 0; | ||
this.type = type; | ||
this.data = data; | ||
}; | ||
var leaves = createLeaves('leaves', {}, children); | ||
var first = leaves.first(); | ||
var last = leaves.last(); | ||
var id = '__decoration_' + uid++ + '__'; | ||
var start = new DecorationPoint({ id: id, type: type, data: data }); | ||
var end = new DecorationPoint({ id: id, type: type, data: data }); | ||
setPoint(first, start, 0); | ||
setPoint(last, end, last.text.length); | ||
return leaves; | ||
} | ||
/** | ||
* The default Slate hyperscript creator functions. | ||
* Create a document. | ||
* | ||
* @type {Object} | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {Document} | ||
*/ | ||
var CREATORS = { | ||
anchor: function anchor(tagName, attributes, children) { | ||
return new AnchorPoint(attributes); | ||
}, | ||
block: function block(tagName, attributes, children) { | ||
return slate.Block.create(_extends({}, attributes, { | ||
nodes: createChildren(children) | ||
})); | ||
}, | ||
cursor: function cursor(tagName, attributes, children) { | ||
return new CursorPoint(); | ||
}, | ||
decoration: function decoration(tagName, attributes, children) { | ||
var key = attributes.key, | ||
data = attributes.data; | ||
function createDocument(tagName, attributes, children) { | ||
var attrs = _extends({}, attributes, { object: 'document' }); | ||
var document = createNode('node', attrs, children); | ||
return document; | ||
} | ||
var type = tagName; | ||
/** | ||
* Create a focus point. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {FocusPoint} | ||
*/ | ||
if (key) { | ||
return new DecorationPoint({ key: key, type: type, data: data }); | ||
} | ||
function createFocus(tagName, attributes, children) { | ||
return new FocusPoint(attributes); | ||
} | ||
var nodes = createChildren(children); | ||
var node = nodes[0]; | ||
/** | ||
* Create an inline. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {Inline} | ||
*/ | ||
var _node$__decorations = node.__decorations, | ||
__decorations = _node$__decorations === undefined ? [] : _node$__decorations; | ||
function createInline(tagName, attributes, children) { | ||
var attrs = _extends({}, attributes, { object: 'inline' }); | ||
var inline = createNode('node', attrs, children); | ||
return inline; | ||
} | ||
var __decoration = { | ||
anchorOffset: 0, | ||
focusOffset: nodes.reduce(function (len, n) { | ||
return len + n.text.length; | ||
}, 0), | ||
type: type, | ||
data: data | ||
}; | ||
/** | ||
* Create a list of leaves. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {List<Leaf>} | ||
*/ | ||
__decorations.push(__decoration); | ||
node.__decorations = __decorations; | ||
return nodes; | ||
}, | ||
document: function document(tagName, attributes, children) { | ||
return slate.Document.create(_extends({}, attributes, { | ||
nodes: createChildren(children) | ||
})); | ||
}, | ||
focus: function focus(tagName, attributes, children) { | ||
return new FocusPoint(attributes); | ||
}, | ||
inline: function inline(tagName, attributes, children) { | ||
return slate.Inline.create(_extends({}, attributes, { | ||
nodes: createChildren(children) | ||
})); | ||
}, | ||
mark: function mark(tagName, attributes, children) { | ||
var marks = slate.Mark.createSet([attributes]); | ||
var nodes = createChildren(children, { marks: marks }); | ||
return nodes; | ||
}, | ||
selection: function selection(tagName, attributes, children) { | ||
var anchor = children.find(function (c) { | ||
return c instanceof AnchorPoint; | ||
}); | ||
var focus = children.find(function (c) { | ||
return c instanceof FocusPoint; | ||
}); | ||
var marks = attributes.marks, | ||
focused = attributes.focused; | ||
function createLeaves(tagName, attributes, children) { | ||
var _attributes$marks = attributes.marks, | ||
marks = _attributes$marks === undefined ? slate.Mark.createSet() : _attributes$marks; | ||
var selection = slate.Selection.create({ | ||
marks: marks, | ||
isFocused: focused, | ||
anchor: anchor && { | ||
key: anchor.key, | ||
offset: anchor.offset, | ||
path: anchor.path | ||
}, | ||
focus: focus && { | ||
key: focus.key, | ||
offset: focus.offset, | ||
path: focus.path | ||
var length = 0; | ||
var leaves = slate.Leaf.createList([]); | ||
var leaf = void 0; | ||
children.forEach(function (child) { | ||
if (slate.Leaf.isLeafList(child)) { | ||
if (leaf) { | ||
leaves = leaves.push(leaf); | ||
leaf = null; | ||
} | ||
}); | ||
return selection; | ||
}, | ||
text: function text(tagName, attributes, children) { | ||
var nodes = createChildren(children, { key: attributes.key }); | ||
return nodes; | ||
}, | ||
value: function value(tagName, attributes, children) { | ||
var data = attributes.data, | ||
_attributes$normalize = attributes.normalize, | ||
normalize = _attributes$normalize === undefined ? true : _attributes$normalize; | ||
child.forEach(function (l) { | ||
l = preservePoint(l, function (obj) { | ||
return obj.addMarks(marks); | ||
}); | ||
leaves = leaves.push(l); | ||
}); | ||
} else { | ||
if (!leaf) { | ||
leaf = slate.Leaf.create({ marks: marks, text: '' }); | ||
length = 0; | ||
} | ||
var document = children.find(slate.Document.isDocument); | ||
var selection = children.find(slate.Selection.isSelection) || slate.Selection.create(); | ||
var anchor = void 0; | ||
var focus = void 0; | ||
var decorations = []; | ||
var partials = {}; | ||
if (typeof child === 'string') { | ||
var offset = leaf.text.length; | ||
leaf = preservePoint(leaf, function (obj) { | ||
return obj.insertText(offset, child); | ||
}); | ||
length += child.length; | ||
} | ||
// Search the document's texts to see if any of them have the anchor or | ||
// focus information saved, or decorations applied. | ||
if (document) { | ||
document.getTexts().forEach(function (text) { | ||
if (text.__anchor != null) { | ||
anchor = slate.Point.create({ key: text.key, offset: text.__anchor.offset }); | ||
} | ||
if (isPoint(child)) { | ||
setPoint(leaf, child, length); | ||
} | ||
} | ||
}); | ||
if (text.__focus != null) { | ||
focus = slate.Point.create({ key: text.key, offset: text.__focus.offset }); | ||
} | ||
if (!leaves.size && !leaf) { | ||
leaf = slate.Leaf.create({ marks: marks, text: '' }); | ||
} | ||
if (text.__decorations != null) { | ||
text.__decorations.forEach(function (dec) { | ||
var id = dec.id; | ||
if (leaf) { | ||
leaves = leaves.push(leaf); | ||
} | ||
var range = void 0; | ||
return leaves; | ||
} | ||
if (!id) { | ||
range = slate.Decoration.create({ | ||
anchor: { | ||
key: text.key, | ||
offset: dec.anchorOffset | ||
}, | ||
focus: { | ||
key: text.key, | ||
offset: dec.focusOffset | ||
}, | ||
mark: { | ||
type: dec.type, | ||
data: dec.data | ||
} | ||
}); | ||
} else if (partials[id]) { | ||
var partial = partials[id]; | ||
delete partials[id]; | ||
/** | ||
* Create a list of leaves from a mark. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {List<Leaf>} | ||
*/ | ||
range = slate.Decoration.create({ | ||
anchor: { | ||
key: partial.key, | ||
offset: partial.offset | ||
}, | ||
focus: { | ||
key: text.key, | ||
offset: dec.offset | ||
}, | ||
mark: { | ||
type: dec.type, | ||
data: dec.data | ||
} | ||
}); | ||
} else { | ||
dec.key = text.key; | ||
partials[id] = dec; | ||
} | ||
function createMark(tagName, attributes, children) { | ||
var marks = slate.Mark.createSet([attributes]); | ||
var leaves = createLeaves('leaves', { marks: marks }, children); | ||
return leaves; | ||
} | ||
if (range) { | ||
decorations.push(range); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
/** | ||
* Create a node. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {Node} | ||
*/ | ||
if (Object.keys(partials).length > 0) { | ||
throw new Error('Slate hyperscript must have both a start and an end defined for each decoration using the `key=` prop.'); | ||
} | ||
function createNode(tagName, attributes, children) { | ||
var object = attributes.object; | ||
if (anchor && !focus) { | ||
throw new Error('Slate hyperscript ranges must have both `<anchor />` and `<focus />` defined if one is defined, but you only defined `<anchor />`. For collapsed selections, use `<cursor />` instead.'); | ||
} | ||
if (!anchor && focus) { | ||
throw new Error('Slate hyperscript ranges must have both `<anchor />` and `<focus />` defined if one is defined, but you only defined `<focus />`. For collapsed selections, use `<cursor />` instead.'); | ||
} | ||
if (object === 'text') { | ||
return createText('text', {}, children); | ||
} | ||
var value = slate.Value.fromJSON(_extends({ data: data, document: document, selection: selection }, attributes), { normalize: normalize }); | ||
var nodes = []; | ||
var others = []; | ||
if (anchor || focus) { | ||
selection = selection.setPoints([anchor, focus]); | ||
selection = selection.setIsFocused(true); | ||
selection = selection.normalize(value.document); | ||
value = value.set('selection', selection); | ||
} | ||
children.forEach(function (child) { | ||
if (slate.Node.isNode(child)) { | ||
if (others.length) { | ||
var text = createText('text', {}, others); | ||
nodes.push(text); | ||
} | ||
if (decorations.length > 0) { | ||
decorations = decorations.map(function (d) { | ||
return d.normalize(value.document); | ||
}); | ||
decorations = slate.Decoration.createList(decorations); | ||
value = value.set('decorations', decorations); | ||
nodes.push(child); | ||
others = []; | ||
} else { | ||
others.push(child); | ||
} | ||
}); | ||
return value; | ||
if (others.length) { | ||
var text = createText('text', {}, others); | ||
nodes.push(text); | ||
} | ||
}; | ||
var node = slate.Node.create(_extends({}, attributes, { nodes: nodes })); | ||
return node; | ||
} | ||
/** | ||
* Create a Slate hyperscript function with `options`. | ||
* Create a selection. | ||
* | ||
* @param {Object} options | ||
* @return {Function} | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {Selection} | ||
*/ | ||
function createHyperscript() { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
function createSelection(tagName, attributes, children) { | ||
var anchor = children.find(function (c) { | ||
return c instanceof AnchorPoint; | ||
}); | ||
var focus = children.find(function (c) { | ||
return c instanceof FocusPoint; | ||
}); | ||
var marks = attributes.marks, | ||
focused = attributes.focused; | ||
var creators = resolveCreators(options); | ||
function create(tagName, attributes) { | ||
for (var _len = arguments.length, children = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { | ||
children[_key - 2] = arguments[_key]; | ||
var selection = slate.Selection.create({ | ||
marks: marks, | ||
isFocused: focused, | ||
anchor: anchor && { | ||
key: anchor.key, | ||
offset: anchor.offset, | ||
path: anchor.path | ||
}, | ||
focus: focus && { | ||
key: focus.key, | ||
offset: focus.offset, | ||
path: focus.path | ||
} | ||
}); | ||
var creator = creators[tagName]; | ||
return selection; | ||
} | ||
if (!creator) { | ||
throw new Error('No hyperscript creator found for tag: "' + tagName + '"'); | ||
} | ||
/** | ||
* Create a text node. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {Text} | ||
*/ | ||
if (attributes == null) { | ||
attributes = {}; | ||
} | ||
function createText(tagName, attributes, children) { | ||
var key = attributes.key; | ||
if (!isPlainObject(attributes)) { | ||
children = [attributes].concat(children); | ||
attributes = {}; | ||
} | ||
var leaves = createLeaves('leaves', {}, children); | ||
var text = slate.Text.create({ key: key, leaves: leaves }); | ||
var length = 0; | ||
children = children.filter(function (child) { | ||
return Boolean(child); | ||
}).reduce(function (memo, child) { | ||
return memo.concat(child); | ||
}, []); | ||
leaves.forEach(function (leaf) { | ||
incrementPoint(leaf, length); | ||
preservePoint(leaf, function () { | ||
return text; | ||
}); | ||
length += leaf.text.length; | ||
}); | ||
var element = creator(tagName, attributes, children); | ||
return element; | ||
} | ||
return create; | ||
return text; | ||
} | ||
/** | ||
* Create an array of `children`, storing selection anchor and focus. | ||
* Create a value. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @param {Object} options | ||
* @return {Array} | ||
* @return {Value} | ||
*/ | ||
function createChildren(children) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
function createValue(tagName, attributes, children) { | ||
var data = attributes.data; | ||
var array = []; | ||
var length = 0; | ||
var document = children.find(slate.Document.isDocument); | ||
var selection = children.find(slate.Selection.isSelection); | ||
var anchor = void 0; | ||
var focus = void 0; | ||
var decorations = []; | ||
var partials = {}; | ||
// When creating the new node, try to preserve a key if one exists. | ||
var firstNodeOrText = children.find(function (c) { | ||
return typeof c !== 'string'; | ||
}); | ||
var firstText = slate.Text.isText(firstNodeOrText) ? firstNodeOrText : null; | ||
var key = options.key ? options.key : firstText ? firstText.key : undefined; | ||
var node = slate.Text.create({ key: key, leaves: [{ text: '', marks: options.marks }] }); | ||
// Search the document's texts to see if any of them have the anchor or | ||
// focus information saved, or decorations applied. | ||
if (document) { | ||
document.getTexts().forEach(function (text) { | ||
if (text.__anchor != null) { | ||
anchor = slate.Point.create({ key: text.key, offset: text.__anchor.offset }); | ||
} | ||
// Create a helper to update the current node while preserving any stored | ||
// anchor or focus information. | ||
function setNode(next) { | ||
var _node = node, | ||
__anchor = _node.__anchor, | ||
__focus = _node.__focus, | ||
__decorations = _node.__decorations; | ||
if (text.__focus != null) { | ||
focus = slate.Point.create({ key: text.key, offset: text.__focus.offset }); | ||
} | ||
if (__anchor != null) next.__anchor = __anchor; | ||
if (__focus != null) next.__focus = __focus; | ||
if (__decorations != null) next.__decorations = __decorations; | ||
node = next; | ||
} | ||
if (text.__decorations != null) { | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
children.forEach(function (child, index) { | ||
var isLast = index === children.length - 1; | ||
try { | ||
for (var _iterator = text.__decorations[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var dec = _step.value; | ||
var id = dec.id; | ||
// If the child is a non-text node, push the current node and the new child | ||
// onto the array, then creating a new node for future selection tracking. | ||
if (slate.Node.isNode(child) && !slate.Text.isText(child)) { | ||
if (node.text.length || node.__anchor != null || node.__focus != null || node.getMarksAtIndex(0).size) { | ||
array.push(node); | ||
var partial = partials[id]; | ||
delete partials[id]; | ||
if (!partial) { | ||
dec.key = text.key; | ||
partials[id] = dec; | ||
continue; | ||
} | ||
var decoration = slate.Decoration.create({ | ||
anchor: { | ||
key: partial.key, | ||
offset: partial.offset | ||
}, | ||
focus: { | ||
key: text.key, | ||
offset: dec.offset | ||
}, | ||
mark: { | ||
type: dec.type, | ||
data: dec.data | ||
} | ||
}); | ||
decorations.push(decoration); | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
array.push(child); | ||
if (Object.keys(partials).length > 0) { | ||
throw new Error('Slate hyperscript must have both a start and an end defined for each decoration using the `key=` prop.'); | ||
} | ||
node = isLast ? null : slate.Text.create({ leaves: [{ text: '', marks: options.marks }] }); | ||
if (anchor && !focus) { | ||
throw new Error('Slate hyperscript ranges must have both `<anchor />` and `<focus />` defined if one is defined, but you only defined `<anchor />`. For collapsed selections, use `<cursor />` instead.'); | ||
} | ||
length = 0; | ||
} | ||
if (!anchor && focus) { | ||
throw new Error('Slate hyperscript ranges must have both `<anchor />` and `<focus />` defined if one is defined, but you only defined `<focus />`. For collapsed selections, use `<cursor />` instead.'); | ||
} | ||
// If the child is a string insert it into the node. | ||
if (typeof child == 'string') { | ||
setNode(node.insertText(node.text.length, child, options.marks)); | ||
length += child.length; | ||
if (anchor || focus) { | ||
if (!selection) { | ||
selection = slate.Selection.create({ anchor: anchor, focus: focus, isFocused: true }); | ||
} else { | ||
selection = selection.setPoints([anchor, focus]); | ||
} | ||
} else if (!selection) { | ||
selection = slate.Selection.create(); | ||
} | ||
// If the node is a `Text` add its text and marks to the existing node. If | ||
// the existing node is empty, and the `key` option wasn't set, preserve the | ||
// child's key when updating the node. | ||
if (slate.Text.isText(child)) { | ||
var __anchor = child.__anchor, | ||
__focus = child.__focus, | ||
__decorations = child.__decorations; | ||
selection = selection.normalize(document); | ||
var i = node.text.length; | ||
if (decorations.length > 0) { | ||
decorations = decorations.map(function (d) { | ||
return d.normalize(document); | ||
}); | ||
} | ||
if (!options.key && node.text.length == 0) { | ||
setNode(node.set('key', child.key)); | ||
} | ||
var value = slate.Value.fromJSON(_extends({ | ||
data: data, | ||
decorations: decorations, | ||
document: document, | ||
selection: selection | ||
}, attributes)); | ||
child.getLeaves().forEach(function (leaf) { | ||
var marks = leaf.marks; | ||
return value; | ||
} | ||
if (options.marks) marks = marks.union(options.marks); | ||
setNode(node.insertText(i, leaf.text, marks)); | ||
i += leaf.text.length; | ||
}); | ||
/** | ||
* Point classes that can be created at different points in the document and | ||
* then searched for afterwards, for creating ranges. | ||
* | ||
* @type {Class} | ||
*/ | ||
if (__anchor != null) { | ||
node.__anchor = new AnchorPoint(); | ||
node.__anchor.offset = __anchor.offset + length; | ||
} | ||
var CursorPoint = function CursorPoint() { | ||
classCallCheck(this, CursorPoint); | ||
if (__focus != null) { | ||
node.__focus = new FocusPoint(); | ||
node.__focus.offset = __focus.offset + length; | ||
} | ||
this.offset = null; | ||
}; | ||
if (__decorations != null) { | ||
__decorations.forEach(function (d) { | ||
if (d instanceof DecorationPoint) { | ||
d.offset += length; | ||
} else { | ||
d.anchorOffset += length; | ||
d.focusOffset += length; | ||
} | ||
}); | ||
var AnchorPoint = function AnchorPoint() { | ||
var attrs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
classCallCheck(this, AnchorPoint); | ||
var _attrs$key = attrs.key, | ||
key = _attrs$key === undefined ? null : _attrs$key, | ||
_attrs$offset = attrs.offset, | ||
offset = _attrs$offset === undefined ? null : _attrs$offset, | ||
_attrs$path = attrs.path, | ||
path = _attrs$path === undefined ? null : _attrs$path; | ||
node.__decorations = node.__decorations || []; | ||
node.__decorations = node.__decorations.concat(__decorations); | ||
} | ||
this.key = key; | ||
this.offset = offset; | ||
this.path = path; | ||
}; | ||
length += child.text.length; | ||
} | ||
var FocusPoint = function FocusPoint() { | ||
var attrs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
classCallCheck(this, FocusPoint); | ||
var _attrs$key2 = attrs.key, | ||
key = _attrs$key2 === undefined ? null : _attrs$key2, | ||
_attrs$offset2 = attrs.offset, | ||
offset = _attrs$offset2 === undefined ? null : _attrs$offset2, | ||
_attrs$path2 = attrs.path, | ||
path = _attrs$path2 === undefined ? null : _attrs$path2; | ||
if (child instanceof AnchorPoint || child instanceof CursorPoint) { | ||
child.offset = length; | ||
node.__anchor = child; | ||
} | ||
this.key = key; | ||
this.offset = offset; | ||
this.path = path; | ||
}; | ||
if (child instanceof FocusPoint || child instanceof CursorPoint) { | ||
child.offset = length; | ||
node.__focus = child; | ||
} | ||
var DecorationPoint = function DecorationPoint(attrs) { | ||
classCallCheck(this, DecorationPoint); | ||
var _attrs$id = attrs.id, | ||
id = _attrs$id === undefined ? null : _attrs$id, | ||
_attrs$data = attrs.data, | ||
data = _attrs$data === undefined ? {} : _attrs$data, | ||
type = attrs.type; | ||
if (child instanceof DecorationPoint) { | ||
child.offset = length; | ||
node.__decorations = node.__decorations || []; | ||
node.__decorations = node.__decorations.concat(child); | ||
} | ||
}); | ||
this.id = id; | ||
this.offset = null; | ||
this.type = type; | ||
this.data = data; | ||
}; | ||
// Make sure the most recent node is added. | ||
if (node != null) { | ||
array.push(node); | ||
/** | ||
* Increment any existing `point` on object by `n`. | ||
* | ||
* @param {Any} object | ||
* @param {Number} n | ||
*/ | ||
function incrementPoint(object, n) { | ||
var __anchor = object.__anchor, | ||
__focus = object.__focus, | ||
__decorations = object.__decorations; | ||
if (__anchor != null) { | ||
__anchor.offset += n; | ||
} | ||
return array; | ||
if (__focus != null && __focus !== __anchor) { | ||
__focus.offset += n; | ||
} | ||
if (__decorations != null) { | ||
__decorations.forEach(function (d) { | ||
return d.offset += n; | ||
}); | ||
} | ||
} | ||
/** | ||
* Resolve a set of hyperscript creators an `options` object. | ||
* Check whether an `object` is a point. | ||
* | ||
* @param {Any} object | ||
* @return {Boolean} | ||
*/ | ||
function isPoint(object) { | ||
return object instanceof AnchorPoint || object instanceof CursorPoint || object instanceof DecorationPoint || object instanceof FocusPoint; | ||
} | ||
/** | ||
* Preserve any point information on an object. | ||
* | ||
* @param {Any} object | ||
* @param {Function} updator | ||
* @return {Any} | ||
*/ | ||
function preservePoint(object, updator) { | ||
var __anchor = object.__anchor, | ||
__focus = object.__focus, | ||
__decorations = object.__decorations; | ||
var next = updator(object); | ||
if (__anchor != null) next.__anchor = __anchor; | ||
if (__focus != null) next.__focus = __focus; | ||
if (__decorations != null) next.__decorations = __decorations; | ||
return next; | ||
} | ||
/** | ||
* Set a `point` on an `object`. | ||
* | ||
* @param {Any} object | ||
* @param {*Point} point | ||
* @param {Number} offset | ||
*/ | ||
function setPoint(object, point, offset) { | ||
if (point instanceof AnchorPoint || point instanceof CursorPoint) { | ||
point.offset = offset; | ||
object.__anchor = point; | ||
} | ||
if (point instanceof FocusPoint || point instanceof CursorPoint) { | ||
point.offset = offset; | ||
object.__focus = point; | ||
} | ||
if (point instanceof DecorationPoint) { | ||
point.offset = offset; | ||
object.__decorations = object.__decorations || []; | ||
object.__decorations = object.__decorations.concat(point); | ||
} | ||
} | ||
/** | ||
* Create a Slate hyperscript function with `options`. | ||
* | ||
* @param {Object} options | ||
* @return {Object} | ||
* @return {Function} | ||
*/ | ||
function resolveCreators(options) { | ||
function createHyperscript() { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var _options$blocks = options.blocks, | ||
@@ -577,78 +687,78 @@ blocks = _options$blocks === undefined ? {} : _options$blocks, | ||
_options$decorations = options.decorations, | ||
decorations = _options$decorations === undefined ? {} : _options$decorations, | ||
schema = options.schema; | ||
decorations = _options$decorations === undefined ? {} : _options$decorations; | ||
var creators = _extends({}, CREATORS, options.creators || {}); | ||
var creators = _extends({ | ||
anchor: createAnchor, | ||
block: createBlock, | ||
cursor: createCursor, | ||
decoration: createDecoration, | ||
document: createDocument, | ||
focus: createFocus, | ||
inline: createInline, | ||
mark: createMark, | ||
node: createNode, | ||
selection: createSelection, | ||
text: createText, | ||
value: createValue | ||
}, options.creators || {}); | ||
Object.keys(blocks).map(function (key) { | ||
creators[key] = normalizeNode(blocks[key], 'block'); | ||
}); | ||
for (var key in blocks) { | ||
creators[key] = normalizeCreator(blocks[key], createBlock); | ||
} | ||
Object.keys(inlines).map(function (key) { | ||
creators[key] = normalizeNode(inlines[key], 'inline'); | ||
}); | ||
for (var _key in inlines) { | ||
creators[_key] = normalizeCreator(inlines[_key], createInline); | ||
} | ||
Object.keys(marks).map(function (key) { | ||
creators[key] = normalizeMark(marks[key]); | ||
}); | ||
for (var _key2 in marks) { | ||
creators[_key2] = normalizeCreator(marks[_key2], createMark); | ||
} | ||
Object.keys(decorations).map(function (key) { | ||
creators[key] = normalizeNode(decorations[key], 'decoration'); | ||
}); | ||
for (var _key3 in decorations) { | ||
creators[_key3] = normalizeCreator(decorations[_key3], createDecoration); | ||
} | ||
creators.value = function (tagName) { | ||
var attributes = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var children = arguments[2]; | ||
function create(tagName, attributes) { | ||
for (var _len = arguments.length, children = Array(_len > 2 ? _len - 2 : 0), _key4 = 2; _key4 < _len; _key4++) { | ||
children[_key4 - 2] = arguments[_key4]; | ||
} | ||
var attrs = _extends({ schema: schema }, attributes); | ||
return CREATORS.value(tagName, attrs, children); | ||
}; | ||
var creator = creators[tagName]; | ||
return creators; | ||
} | ||
if (!creator) { | ||
throw new Error('No hyperscript creator found for tag: "' + tagName + '"'); | ||
} | ||
/** | ||
* Normalize a node creator of `value` and `object`. | ||
* | ||
* @param {Function|Object|String} value | ||
* @param {String} object | ||
* @return {Function} | ||
*/ | ||
if (attributes == null) { | ||
attributes = {}; | ||
} | ||
function normalizeNode(value, object) { | ||
if (typeof value == 'function') { | ||
return value; | ||
} | ||
if (!isPlainObject(attributes)) { | ||
children = [attributes].concat(children); | ||
attributes = {}; | ||
} | ||
if (typeof value == 'string') { | ||
value = { type: value }; | ||
} | ||
children = children.filter(function (child) { | ||
return Boolean(child); | ||
}).reduce(function (memo, child) { | ||
return memo.concat(child); | ||
}, []); | ||
if (isPlainObject(value)) { | ||
return function (tagName, attributes, children) { | ||
var key = attributes.key, | ||
rest = objectWithoutProperties(attributes, ['key']); | ||
var attrs = _extends({}, value, { | ||
object: object, | ||
key: key, | ||
data: _extends({}, value.data || {}, rest) | ||
}); | ||
return CREATORS[object](tagName, attrs, children); | ||
}; | ||
var ret = creator(tagName, attributes, children); | ||
return ret; | ||
} | ||
throw new Error('Slate hyperscript ' + object + ' creators can be either functions, objects or strings, but you passed: ' + value); | ||
return create; | ||
} | ||
/** | ||
* Normalize a mark creator of `value`. | ||
* Normalize a `creator` of `value`. | ||
* | ||
* @param {Function|Object|String} value | ||
* @param {Function} creator | ||
* @return {Function} | ||
*/ | ||
function normalizeMark(value) { | ||
function normalizeCreator(value, creator) { | ||
if (typeof value == 'function') { | ||
@@ -664,11 +774,15 @@ return value; | ||
return function (tagName, attributes, children) { | ||
var key = attributes.key, | ||
rest = objectWithoutProperties(attributes, ['key']); | ||
var attrs = _extends({}, value, { | ||
data: _extends({}, value.data || {}, attributes) | ||
key: key, | ||
data: _extends({}, value.data || {}, rest) | ||
}); | ||
return CREATORS.mark(tagName, attrs, children); | ||
return creator(tagName, attrs, children); | ||
}; | ||
} | ||
throw new Error('Slate hyperscript mark creators can be either functions, objects or strings, but you passed: ' + value); | ||
throw new Error('Slate hyperscript creators can be either functions, objects or strings, but you passed: ' + value); | ||
} | ||
@@ -675,0 +789,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("slate")):"function"==typeof define&&define.amd?define(["exports","slate"],t):t(e.SlateHyperscript={},e.Slate)}(this,function(e,t){"use strict";var n=function(e){return null!=e&&"object"==typeof e&&!1===Array.isArray(e)};function o(e){return!0===n(e)&&"[object Object]"===Object.prototype.toString.call(e)}var r=function(e){var t,n;return!1!==o(e)&&("function"==typeof(t=e.constructor)&&(!1!==o(n=t.prototype)&&!1!==n.hasOwnProperty("isPrototypeOf")))},a=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},i=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n)Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o])}return e},c=function(e,t){var n={};for(var o in e)t.indexOf(o)>=0||Object.prototype.hasOwnProperty.call(e,o)&&(n[o]=e[o]);return n},s=function e(){a(this,e),this.offset=null},f=function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};a(this,e);var n=t.key,o=void 0===n?null:n,r=t.offset,i=void 0===r?null:r,c=t.path,s=void 0===c?null:c;this.key=o,this.offset=i,this.path=s},u=function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};a(this,e);var n=t.key,o=void 0===n?null:n,r=t.offset,i=void 0===r?null:r,c=t.path,s=void 0===c?null:c;this.key=o,this.offset=i,this.path=s},l=function e(n){var o=this;a(this,e),this.combine=function(n){if(!(n instanceof e))throw new Error("misaligned decorations");return t.Decoration.create({anchor:{key:o.key,offset:o.offset},focus:{key:n.key,offset:n.offset},mark:{type:o.type,data:o.data}})};var r=n.key,i=void 0===r?null:r,c=n.data,s=void 0===c?{}:c,f=n.type;this.id=i,this.offset=0,this.type=f,this.data=s},d={anchor:function(e,t,n){return new f(t)},block:function(e,n,o){return t.Block.create(i({},n,{nodes:y(o)}))},cursor:function(e,t,n){return new s},decoration:function(e,t,n){var o=t.key,r=t.data,a=e;if(o)return new l({key:o,type:a,data:r});var i=y(n),c=i[0],s=c.__decorations,f=void 0===s?[]:s,u={anchorOffset:0,focusOffset:i.reduce(function(e,t){return e+t.text.length},0),type:a,data:r};return f.push(u),c.__decorations=f,i},document:function(e,n,o){return t.Document.create(i({},n,{nodes:y(o)}))},focus:function(e,t,n){return new u(t)},inline:function(e,n,o){return t.Inline.create(i({},n,{nodes:y(o)}))},mark:function(e,n,o){return y(o,{marks:t.Mark.createSet([n])})},selection:function(e,n,o){var r=o.find(function(e){return e instanceof f}),a=o.find(function(e){return e instanceof u}),i=n.marks,c=n.focused,s=t.Selection.create({marks:i,isFocused:c,anchor:r&&{key:r.key,offset:r.offset,path:r.path},focus:a&&{key:a.key,offset:a.offset,path:a.path}});return s},text:function(e,t,n){return y(n,{key:t.key})},value:function(e,n,o){var r=n.data,a=n.normalize,c=void 0===a||a,s=o.find(t.Document.isDocument),f=o.find(t.Selection.isSelection)||t.Selection.create(),u=void 0,l=void 0,d=[],h={};if(s&&s.getTexts().forEach(function(e){null!=e.__anchor&&(u=t.Point.create({key:e.key,offset:e.__anchor.offset})),null!=e.__focus&&(l=t.Point.create({key:e.key,offset:e.__focus.offset})),null!=e.__decorations&&e.__decorations.forEach(function(n){var o=n.id,r=void 0;if(o)if(h[o]){var a=h[o];delete h[o],r=t.Decoration.create({anchor:{key:a.key,offset:a.offset},focus:{key:e.key,offset:n.offset},mark:{type:n.type,data:n.data}})}else n.key=e.key,h[o]=n;else r=t.Decoration.create({anchor:{key:e.key,offset:n.anchorOffset},focus:{key:e.key,offset:n.focusOffset},mark:{type:n.type,data:n.data}});r&&d.push(r)})}),Object.keys(h).length>0)throw new Error("Slate hyperscript must have both a start and an end defined for each decoration using the `key=` prop.");if(u&&!l)throw new Error("Slate hyperscript ranges must have both `<anchor />` and `<focus />` defined if one is defined, but you only defined `<anchor />`. For collapsed selections, use `<cursor />` instead.");if(!u&&l)throw new Error("Slate hyperscript ranges must have both `<anchor />` and `<focus />` defined if one is defined, but you only defined `<focus />`. For collapsed selections, use `<cursor />` instead.");var y=t.Value.fromJSON(i({data:r,document:s,selection:f},n),{normalize:c});return(u||l)&&(f=(f=(f=f.setPoints([u,l])).setIsFocused(!0)).normalize(y.document),y=y.set("selection",f)),d.length>0&&(d=d.map(function(e){return e.normalize(y.document)}),d=t.Decoration.createList(d),y=y.set("decorations",d)),y}};function h(){var e=function(e){var t=e.blocks,n=void 0===t?{}:t,o=e.inlines,a=void 0===o?{}:o,c=e.marks,s=void 0===c?{}:c,f=e.decorations,u=void 0===f?{}:f,l=e.schema,h=i({},d,e.creators||{});return Object.keys(n).map(function(e){h[e]=p(n[e],"block")}),Object.keys(a).map(function(e){h[e]=p(a[e],"inline")}),Object.keys(s).map(function(e){h[e]=function(e){if("function"==typeof e)return e;if("string"==typeof e&&(e={type:e}),r(e))return function(t,n,o){var r=i({},e,{data:i({},e.data||{},n)});return d.mark(t,r,o)};throw new Error("Slate hyperscript mark creators can be either functions, objects or strings, but you passed: "+e)}(s[e])}),Object.keys(u).map(function(e){h[e]=p(u[e],"decoration")}),h.value=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=arguments[2],o=i({schema:l},t);return d.value(e,o,n)},h}(arguments.length>0&&void 0!==arguments[0]?arguments[0]:{});return function(t,n){for(var o=arguments.length,a=Array(o>2?o-2:0),i=2;i<o;i++)a[i-2]=arguments[i];var c=e[t];if(!c)throw new Error('No hyperscript creator found for tag: "'+t+'"');return null==n&&(n={}),r(n)||(a=[n].concat(a),n={}),c(t,n,a=a.filter(function(e){return Boolean(e)}).reduce(function(e,t){return e.concat(t)},[]))}}function y(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=[],r=0,a=e.find(function(e){return"string"!=typeof e}),i=t.Text.isText(a)?a:null,c=n.key?n.key:i?i.key:void 0,d=t.Text.create({key:c,leaves:[{text:"",marks:n.marks}]});function h(e){var t=d,n=t.__anchor,o=t.__focus,r=t.__decorations;null!=n&&(e.__anchor=n),null!=o&&(e.__focus=o),null!=r&&(e.__decorations=r),d=e}return e.forEach(function(a,i){var c=i===e.length-1;if(t.Node.isNode(a)&&!t.Text.isText(a)&&((d.text.length||null!=d.__anchor||null!=d.__focus||d.getMarksAtIndex(0).size)&&o.push(d),o.push(a),d=c?null:t.Text.create({leaves:[{text:"",marks:n.marks}]}),r=0),"string"==typeof a&&(h(d.insertText(d.text.length,a,n.marks)),r+=a.length),t.Text.isText(a)){var y=a.__anchor,p=a.__focus,k=a.__decorations,_=d.text.length;n.key||0!=d.text.length||h(d.set("key",a.key)),a.getLeaves().forEach(function(e){var t=e.marks;n.marks&&(t=t.union(n.marks)),h(d.insertText(_,e.text,t)),_+=e.text.length}),null!=y&&(d.__anchor=new f,d.__anchor.offset=y.offset+r),null!=p&&(d.__focus=new u,d.__focus.offset=p.offset+r),null!=k&&(k.forEach(function(e){e instanceof l?e.offset+=r:(e.anchorOffset+=r,e.focusOffset+=r)}),d.__decorations=d.__decorations||[],d.__decorations=d.__decorations.concat(k)),r+=a.text.length}(a instanceof f||a instanceof s)&&(a.offset=r,d.__anchor=a),(a instanceof u||a instanceof s)&&(a.offset=r,d.__focus=a),a instanceof l&&(a.offset=r,d.__decorations=d.__decorations||[],d.__decorations=d.__decorations.concat(a))}),null!=d&&o.push(d),o}function p(e,t){if("function"==typeof e)return e;if("string"==typeof e&&(e={type:e}),r(e))return function(n,o,r){var a=o.key,s=c(o,["key"]),f=i({},e,{object:t,key:a,data:i({},e.data||{},s)});return d[t](n,f,r)};throw new Error("Slate hyperscript "+t+" creators can be either functions, objects or strings, but you passed: "+e)}var k=h();e.default=k,e.createHyperscript=h,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("slate")):"function"==typeof define&&define.amd?define(["exports","slate"],t):t(e.SlateHyperscript={},e.Slate)}(this,function(e,t){"use strict";var n=function(e){return null!=e&&"object"==typeof e&&!1===Array.isArray(e)};function o(e){return!0===n(e)&&"[object Object]"===Object.prototype.toString.call(e)}var r=function(e){var t,n;return!1!==o(e)&&("function"==typeof(t=e.constructor)&&(!1!==o(n=t.prototype)&&!1!==n.hasOwnProperty("isPrototypeOf")))},i=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},a=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n)Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o])}return e},f=function(e,t){var n={};for(var o in e)t.indexOf(o)>=0||Object.prototype.hasOwnProperty.call(e,o)&&(n[o]=e[o]);return n},c=0;function s(e,t,n){return new x(t)}function u(e,t,n){return k("node",a({},t,{object:"block"}),n)}function l(e,t,n){return new w(t)}function d(e,t,n){var o=t.key,r=t.data,i=e;if(o)return new j({id:o,type:i,data:r});var a=v("leaves",{},n),f=a.first(),s=a.last(),u="__decoration_"+c+++"__",l=new j({id:u,type:i,data:r}),d=new j({id:u,type:i,data:r});return E(f,l,0),E(s,d,s.text.length),a}function h(e,t,n){return k("node",a({},t,{object:"document"}),n)}function y(e,t,n){return new S(t)}function p(e,t,n){return k("node",a({},t,{object:"inline"}),n)}function v(e,n,o){var r=n.marks,i=void 0===r?t.Mark.createSet():r,a=0,f=t.Leaf.createList([]),c=void 0;return o.forEach(function(e){if(t.Leaf.isLeafList(e))c&&(f=f.push(c),c=null),e.forEach(function(e){e=O(e,function(e){return e.addMarks(i)}),f=f.push(e)});else{if(c||(c=t.Leaf.create({marks:i,text:""}),a=0),"string"==typeof e){var n=c.text.length;c=O(c,function(t){return t.insertText(n,e)}),a+=e.length}((o=e)instanceof x||o instanceof w||o instanceof j||o instanceof S)&&E(c,e,a)}var o}),f.size||c||(c=t.Leaf.create({marks:i,text:""})),c&&(f=f.push(c)),f}function _(e,n,o){return v(0,{marks:t.Mark.createSet([n])},o)}function k(e,n,o){if("text"===n.object)return m("text",{},o);var r=[],i=[];if(o.forEach(function(e){if(t.Node.isNode(e)){if(i.length){var n=m("text",{},i);r.push(n)}r.push(e),i=[]}else i.push(e)}),i.length){var f=m("text",{},i);r.push(f)}return t.Node.create(a({},n,{nodes:r}))}function b(e,n,o){var r=o.find(function(e){return e instanceof x}),i=o.find(function(e){return e instanceof S}),a=n.marks,f=n.focused;return t.Selection.create({marks:a,isFocused:f,anchor:r&&{key:r.key,offset:r.offset,path:r.path},focus:i&&{key:i.key,offset:i.offset,path:i.path}})}function m(e,n,o){var r=n.key,i=v(0,{},o),a=t.Text.create({key:r,leaves:i}),f=0;return i.forEach(function(e){!function(e,t){var n=e.__anchor,o=e.__focus,r=e.__decorations;null!=n&&(n.offset+=t);null!=o&&o!==n&&(o.offset+=t);null!=r&&r.forEach(function(e){return e.offset+=t})}(e,f),O(e,function(){return a}),f+=e.text.length}),a}function g(e,n,o){var r=n.data,i=o.find(t.Document.isDocument),f=o.find(t.Selection.isSelection),c=void 0,s=void 0,u=[],l={};if(i&&i.getTexts().forEach(function(e){if(null!=e.__anchor&&(c=t.Point.create({key:e.key,offset:e.__anchor.offset})),null!=e.__focus&&(s=t.Point.create({key:e.key,offset:e.__focus.offset})),null!=e.__decorations){var n=!0,o=!1,r=void 0;try{for(var i,a=e.__decorations[Symbol.iterator]();!(n=(i=a.next()).done);n=!0){var f=i.value,d=f.id,h=l[d];if(delete l[d],h){var y=t.Decoration.create({anchor:{key:h.key,offset:h.offset},focus:{key:e.key,offset:f.offset},mark:{type:f.type,data:f.data}});u.push(y)}else f.key=e.key,l[d]=f}}catch(e){o=!0,r=e}finally{try{!n&&a.return&&a.return()}finally{if(o)throw r}}}}),Object.keys(l).length>0)throw new Error("Slate hyperscript must have both a start and an end defined for each decoration using the `key=` prop.");if(c&&!s)throw new Error("Slate hyperscript ranges must have both `<anchor />` and `<focus />` defined if one is defined, but you only defined `<anchor />`. For collapsed selections, use `<cursor />` instead.");if(!c&&s)throw new Error("Slate hyperscript ranges must have both `<anchor />` and `<focus />` defined if one is defined, but you only defined `<focus />`. For collapsed selections, use `<cursor />` instead.");return c||s?f=f?f.setPoints([c,s]):t.Selection.create({anchor:c,focus:s,isFocused:!0}):f||(f=t.Selection.create()),f=f.normalize(i),u.length>0&&(u=u.map(function(e){return e.normalize(i)})),t.Value.fromJSON(a({data:r,decorations:u,document:i,selection:f},n))}var w=function e(){i(this,e),this.offset=null},x=function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};i(this,e);var n=t.key,o=void 0===n?null:n,r=t.offset,a=void 0===r?null:r,f=t.path,c=void 0===f?null:f;this.key=o,this.offset=a,this.path=c},S=function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};i(this,e);var n=t.key,o=void 0===n?null:n,r=t.offset,a=void 0===r?null:r,f=t.path,c=void 0===f?null:f;this.key=o,this.offset=a,this.path=c},j=function e(t){i(this,e);var n=t.id,o=void 0===n?null:n,r=t.data,a=void 0===r?{}:r,f=t.type;this.id=o,this.offset=null,this.type=f,this.data=a};function O(e,t){var n=e.__anchor,o=e.__focus,r=e.__decorations,i=t(e);return null!=n&&(i.__anchor=n),null!=o&&(i.__focus=o),null!=r&&(i.__decorations=r),i}function E(e,t,n){(t instanceof x||t instanceof w)&&(t.offset=n,e.__anchor=t),(t instanceof S||t instanceof w)&&(t.offset=n,e.__focus=t),t instanceof j&&(t.offset=n,e.__decorations=e.__decorations||[],e.__decorations=e.__decorations.concat(t))}function P(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.blocks,n=void 0===t?{}:t,o=e.inlines,i=void 0===o?{}:o,f=e.marks,c=void 0===f?{}:f,v=e.decorations,w=void 0===v?{}:v,x=a({anchor:s,block:u,cursor:l,decoration:d,document:h,focus:y,inline:p,mark:_,node:k,selection:b,text:m,value:g},e.creators||{});for(var S in n)x[S]=L(n[S],u);for(var j in i)x[j]=L(i[j],p);for(var O in c)x[O]=L(c[O],_);for(var E in w)x[E]=L(w[E],d);return function(e,t){for(var n=arguments.length,o=Array(n>2?n-2:0),i=2;i<n;i++)o[i-2]=arguments[i];var a=x[e];if(!a)throw new Error('No hyperscript creator found for tag: "'+e+'"');return null==t&&(t={}),r(t)||(o=[t].concat(o),t={}),a(e,t,o=o.filter(function(e){return Boolean(e)}).reduce(function(e,t){return e.concat(t)},[]))}}function L(e,t){if("function"==typeof e)return e;if("string"==typeof e&&(e={type:e}),r(e))return function(n,o,r){var i=o.key,c=f(o,["key"]),s=a({},e,{key:i,data:a({},e.data||{},c)});return t(n,s,r)};throw new Error("Slate hyperscript creators can be either functions, objects or strings, but you passed: "+e)}var N=P();e.default=N,e.createHyperscript=P,Object.defineProperty(e,"__esModule",{value:!0})}); |
@@ -0,3 +1,3 @@ | ||
import { Decoration, Document, Leaf, Mark, Node, Point, Selection, Text, Value } from 'slate'; | ||
import isPlainObject from 'is-plain-object'; | ||
import { Block, Decoration, Document, Inline, Mark, Node, Point, Selection, Text, Value } from 'slate'; | ||
@@ -57,473 +57,583 @@ var classCallCheck = function (instance, Constructor) { | ||
/** | ||
* Point classes that can be created at different points in the document and | ||
* then searched for afterwards, for creating ranges. | ||
* Auto-incrementing ID to keep track of paired decorations. | ||
* | ||
* @type {Class} | ||
* @type {Number} | ||
*/ | ||
var CursorPoint = function CursorPoint() { | ||
classCallCheck(this, CursorPoint); | ||
var uid = 0; | ||
this.offset = null; | ||
}; | ||
/** | ||
* Create an anchor point. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {AnchorPoint} | ||
*/ | ||
var AnchorPoint = function AnchorPoint() { | ||
var attrs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
classCallCheck(this, AnchorPoint); | ||
var _attrs$key = attrs.key, | ||
key = _attrs$key === undefined ? null : _attrs$key, | ||
_attrs$offset = attrs.offset, | ||
offset = _attrs$offset === undefined ? null : _attrs$offset, | ||
_attrs$path = attrs.path, | ||
path = _attrs$path === undefined ? null : _attrs$path; | ||
function createAnchor(tagName, attributes, children) { | ||
return new AnchorPoint(attributes); | ||
} | ||
this.key = key; | ||
this.offset = offset; | ||
this.path = path; | ||
}; | ||
/** | ||
* Create a block. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {Block} | ||
*/ | ||
var FocusPoint = function FocusPoint() { | ||
var attrs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
classCallCheck(this, FocusPoint); | ||
var _attrs$key2 = attrs.key, | ||
key = _attrs$key2 === undefined ? null : _attrs$key2, | ||
_attrs$offset2 = attrs.offset, | ||
offset = _attrs$offset2 === undefined ? null : _attrs$offset2, | ||
_attrs$path2 = attrs.path, | ||
path = _attrs$path2 === undefined ? null : _attrs$path2; | ||
function createBlock(tagName, attributes, children) { | ||
var attrs = _extends({}, attributes, { object: 'block' }); | ||
var block = createNode('node', attrs, children); | ||
return block; | ||
} | ||
this.key = key; | ||
this.offset = offset; | ||
this.path = path; | ||
}; | ||
/** | ||
* Create a cursor point. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {CursorPoint} | ||
*/ | ||
var DecorationPoint = function DecorationPoint(attrs) { | ||
var _this = this; | ||
function createCursor(tagName, attributes, children) { | ||
return new CursorPoint(attributes); | ||
} | ||
classCallCheck(this, DecorationPoint); | ||
/** | ||
* Create a decoration point, or wrap a list of leaves and set the decoration | ||
* point tracker on them. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {DecorationPoint|List<Leaf>} | ||
*/ | ||
this.combine = function (focus) { | ||
if (!(focus instanceof DecorationPoint)) { | ||
throw new Error('misaligned decorations'); | ||
} | ||
function createDecoration(tagName, attributes, children) { | ||
var key = attributes.key, | ||
data = attributes.data; | ||
return Decoration.create({ | ||
anchor: { | ||
key: _this.key, | ||
offset: _this.offset | ||
}, | ||
focus: { | ||
key: focus.key, | ||
offset: focus.offset | ||
}, | ||
mark: { | ||
type: _this.type, | ||
data: _this.data | ||
} | ||
}); | ||
}; | ||
var type = tagName; | ||
var _attrs$key3 = attrs.key, | ||
key = _attrs$key3 === undefined ? null : _attrs$key3, | ||
_attrs$data = attrs.data, | ||
data = _attrs$data === undefined ? {} : _attrs$data, | ||
type = attrs.type; | ||
if (key) { | ||
return new DecorationPoint({ id: key, type: type, data: data }); | ||
} | ||
this.id = key; | ||
this.offset = 0; | ||
this.type = type; | ||
this.data = data; | ||
}; | ||
var leaves = createLeaves('leaves', {}, children); | ||
var first = leaves.first(); | ||
var last = leaves.last(); | ||
var id = '__decoration_' + uid++ + '__'; | ||
var start = new DecorationPoint({ id: id, type: type, data: data }); | ||
var end = new DecorationPoint({ id: id, type: type, data: data }); | ||
setPoint(first, start, 0); | ||
setPoint(last, end, last.text.length); | ||
return leaves; | ||
} | ||
/** | ||
* The default Slate hyperscript creator functions. | ||
* Create a document. | ||
* | ||
* @type {Object} | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {Document} | ||
*/ | ||
var CREATORS = { | ||
anchor: function anchor(tagName, attributes, children) { | ||
return new AnchorPoint(attributes); | ||
}, | ||
block: function block(tagName, attributes, children) { | ||
return Block.create(_extends({}, attributes, { | ||
nodes: createChildren(children) | ||
})); | ||
}, | ||
cursor: function cursor(tagName, attributes, children) { | ||
return new CursorPoint(); | ||
}, | ||
decoration: function decoration(tagName, attributes, children) { | ||
var key = attributes.key, | ||
data = attributes.data; | ||
function createDocument(tagName, attributes, children) { | ||
var attrs = _extends({}, attributes, { object: 'document' }); | ||
var document = createNode('node', attrs, children); | ||
return document; | ||
} | ||
var type = tagName; | ||
/** | ||
* Create a focus point. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {FocusPoint} | ||
*/ | ||
if (key) { | ||
return new DecorationPoint({ key: key, type: type, data: data }); | ||
} | ||
function createFocus(tagName, attributes, children) { | ||
return new FocusPoint(attributes); | ||
} | ||
var nodes = createChildren(children); | ||
var node = nodes[0]; | ||
/** | ||
* Create an inline. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {Inline} | ||
*/ | ||
var _node$__decorations = node.__decorations, | ||
__decorations = _node$__decorations === undefined ? [] : _node$__decorations; | ||
function createInline(tagName, attributes, children) { | ||
var attrs = _extends({}, attributes, { object: 'inline' }); | ||
var inline = createNode('node', attrs, children); | ||
return inline; | ||
} | ||
var __decoration = { | ||
anchorOffset: 0, | ||
focusOffset: nodes.reduce(function (len, n) { | ||
return len + n.text.length; | ||
}, 0), | ||
type: type, | ||
data: data | ||
}; | ||
/** | ||
* Create a list of leaves. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {List<Leaf>} | ||
*/ | ||
__decorations.push(__decoration); | ||
node.__decorations = __decorations; | ||
return nodes; | ||
}, | ||
document: function document(tagName, attributes, children) { | ||
return Document.create(_extends({}, attributes, { | ||
nodes: createChildren(children) | ||
})); | ||
}, | ||
focus: function focus(tagName, attributes, children) { | ||
return new FocusPoint(attributes); | ||
}, | ||
inline: function inline(tagName, attributes, children) { | ||
return Inline.create(_extends({}, attributes, { | ||
nodes: createChildren(children) | ||
})); | ||
}, | ||
mark: function mark(tagName, attributes, children) { | ||
var marks = Mark.createSet([attributes]); | ||
var nodes = createChildren(children, { marks: marks }); | ||
return nodes; | ||
}, | ||
selection: function selection(tagName, attributes, children) { | ||
var anchor = children.find(function (c) { | ||
return c instanceof AnchorPoint; | ||
}); | ||
var focus = children.find(function (c) { | ||
return c instanceof FocusPoint; | ||
}); | ||
var marks = attributes.marks, | ||
focused = attributes.focused; | ||
function createLeaves(tagName, attributes, children) { | ||
var _attributes$marks = attributes.marks, | ||
marks = _attributes$marks === undefined ? Mark.createSet() : _attributes$marks; | ||
var selection = Selection.create({ | ||
marks: marks, | ||
isFocused: focused, | ||
anchor: anchor && { | ||
key: anchor.key, | ||
offset: anchor.offset, | ||
path: anchor.path | ||
}, | ||
focus: focus && { | ||
key: focus.key, | ||
offset: focus.offset, | ||
path: focus.path | ||
var length = 0; | ||
var leaves = Leaf.createList([]); | ||
var leaf = void 0; | ||
children.forEach(function (child) { | ||
if (Leaf.isLeafList(child)) { | ||
if (leaf) { | ||
leaves = leaves.push(leaf); | ||
leaf = null; | ||
} | ||
}); | ||
return selection; | ||
}, | ||
text: function text(tagName, attributes, children) { | ||
var nodes = createChildren(children, { key: attributes.key }); | ||
return nodes; | ||
}, | ||
value: function value(tagName, attributes, children) { | ||
var data = attributes.data, | ||
_attributes$normalize = attributes.normalize, | ||
normalize = _attributes$normalize === undefined ? true : _attributes$normalize; | ||
child.forEach(function (l) { | ||
l = preservePoint(l, function (obj) { | ||
return obj.addMarks(marks); | ||
}); | ||
leaves = leaves.push(l); | ||
}); | ||
} else { | ||
if (!leaf) { | ||
leaf = Leaf.create({ marks: marks, text: '' }); | ||
length = 0; | ||
} | ||
var document = children.find(Document.isDocument); | ||
var selection = children.find(Selection.isSelection) || Selection.create(); | ||
var anchor = void 0; | ||
var focus = void 0; | ||
var decorations = []; | ||
var partials = {}; | ||
if (typeof child === 'string') { | ||
var offset = leaf.text.length; | ||
leaf = preservePoint(leaf, function (obj) { | ||
return obj.insertText(offset, child); | ||
}); | ||
length += child.length; | ||
} | ||
// Search the document's texts to see if any of them have the anchor or | ||
// focus information saved, or decorations applied. | ||
if (document) { | ||
document.getTexts().forEach(function (text) { | ||
if (text.__anchor != null) { | ||
anchor = Point.create({ key: text.key, offset: text.__anchor.offset }); | ||
} | ||
if (isPoint(child)) { | ||
setPoint(leaf, child, length); | ||
} | ||
} | ||
}); | ||
if (text.__focus != null) { | ||
focus = Point.create({ key: text.key, offset: text.__focus.offset }); | ||
} | ||
if (!leaves.size && !leaf) { | ||
leaf = Leaf.create({ marks: marks, text: '' }); | ||
} | ||
if (text.__decorations != null) { | ||
text.__decorations.forEach(function (dec) { | ||
var id = dec.id; | ||
if (leaf) { | ||
leaves = leaves.push(leaf); | ||
} | ||
var range = void 0; | ||
return leaves; | ||
} | ||
if (!id) { | ||
range = Decoration.create({ | ||
anchor: { | ||
key: text.key, | ||
offset: dec.anchorOffset | ||
}, | ||
focus: { | ||
key: text.key, | ||
offset: dec.focusOffset | ||
}, | ||
mark: { | ||
type: dec.type, | ||
data: dec.data | ||
} | ||
}); | ||
} else if (partials[id]) { | ||
var partial = partials[id]; | ||
delete partials[id]; | ||
/** | ||
* Create a list of leaves from a mark. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {List<Leaf>} | ||
*/ | ||
range = Decoration.create({ | ||
anchor: { | ||
key: partial.key, | ||
offset: partial.offset | ||
}, | ||
focus: { | ||
key: text.key, | ||
offset: dec.offset | ||
}, | ||
mark: { | ||
type: dec.type, | ||
data: dec.data | ||
} | ||
}); | ||
} else { | ||
dec.key = text.key; | ||
partials[id] = dec; | ||
} | ||
function createMark(tagName, attributes, children) { | ||
var marks = Mark.createSet([attributes]); | ||
var leaves = createLeaves('leaves', { marks: marks }, children); | ||
return leaves; | ||
} | ||
if (range) { | ||
decorations.push(range); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
/** | ||
* Create a node. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {Node} | ||
*/ | ||
if (Object.keys(partials).length > 0) { | ||
throw new Error('Slate hyperscript must have both a start and an end defined for each decoration using the `key=` prop.'); | ||
} | ||
function createNode(tagName, attributes, children) { | ||
var object = attributes.object; | ||
if (anchor && !focus) { | ||
throw new Error('Slate hyperscript ranges must have both `<anchor />` and `<focus />` defined if one is defined, but you only defined `<anchor />`. For collapsed selections, use `<cursor />` instead.'); | ||
} | ||
if (!anchor && focus) { | ||
throw new Error('Slate hyperscript ranges must have both `<anchor />` and `<focus />` defined if one is defined, but you only defined `<focus />`. For collapsed selections, use `<cursor />` instead.'); | ||
} | ||
if (object === 'text') { | ||
return createText('text', {}, children); | ||
} | ||
var value = Value.fromJSON(_extends({ data: data, document: document, selection: selection }, attributes), { normalize: normalize }); | ||
var nodes = []; | ||
var others = []; | ||
if (anchor || focus) { | ||
selection = selection.setPoints([anchor, focus]); | ||
selection = selection.setIsFocused(true); | ||
selection = selection.normalize(value.document); | ||
value = value.set('selection', selection); | ||
} | ||
children.forEach(function (child) { | ||
if (Node.isNode(child)) { | ||
if (others.length) { | ||
var text = createText('text', {}, others); | ||
nodes.push(text); | ||
} | ||
if (decorations.length > 0) { | ||
decorations = decorations.map(function (d) { | ||
return d.normalize(value.document); | ||
}); | ||
decorations = Decoration.createList(decorations); | ||
value = value.set('decorations', decorations); | ||
nodes.push(child); | ||
others = []; | ||
} else { | ||
others.push(child); | ||
} | ||
}); | ||
return value; | ||
if (others.length) { | ||
var text = createText('text', {}, others); | ||
nodes.push(text); | ||
} | ||
}; | ||
var node = Node.create(_extends({}, attributes, { nodes: nodes })); | ||
return node; | ||
} | ||
/** | ||
* Create a Slate hyperscript function with `options`. | ||
* Create a selection. | ||
* | ||
* @param {Object} options | ||
* @return {Function} | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {Selection} | ||
*/ | ||
function createHyperscript() { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
function createSelection(tagName, attributes, children) { | ||
var anchor = children.find(function (c) { | ||
return c instanceof AnchorPoint; | ||
}); | ||
var focus = children.find(function (c) { | ||
return c instanceof FocusPoint; | ||
}); | ||
var marks = attributes.marks, | ||
focused = attributes.focused; | ||
var creators = resolveCreators(options); | ||
function create(tagName, attributes) { | ||
for (var _len = arguments.length, children = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { | ||
children[_key - 2] = arguments[_key]; | ||
var selection = Selection.create({ | ||
marks: marks, | ||
isFocused: focused, | ||
anchor: anchor && { | ||
key: anchor.key, | ||
offset: anchor.offset, | ||
path: anchor.path | ||
}, | ||
focus: focus && { | ||
key: focus.key, | ||
offset: focus.offset, | ||
path: focus.path | ||
} | ||
}); | ||
var creator = creators[tagName]; | ||
return selection; | ||
} | ||
if (!creator) { | ||
throw new Error('No hyperscript creator found for tag: "' + tagName + '"'); | ||
} | ||
/** | ||
* Create a text node. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {Text} | ||
*/ | ||
if (attributes == null) { | ||
attributes = {}; | ||
} | ||
function createText(tagName, attributes, children) { | ||
var key = attributes.key; | ||
if (!isPlainObject(attributes)) { | ||
children = [attributes].concat(children); | ||
attributes = {}; | ||
} | ||
var leaves = createLeaves('leaves', {}, children); | ||
var text = Text.create({ key: key, leaves: leaves }); | ||
var length = 0; | ||
children = children.filter(function (child) { | ||
return Boolean(child); | ||
}).reduce(function (memo, child) { | ||
return memo.concat(child); | ||
}, []); | ||
leaves.forEach(function (leaf) { | ||
incrementPoint(leaf, length); | ||
preservePoint(leaf, function () { | ||
return text; | ||
}); | ||
length += leaf.text.length; | ||
}); | ||
var element = creator(tagName, attributes, children); | ||
return element; | ||
} | ||
return create; | ||
return text; | ||
} | ||
/** | ||
* Create an array of `children`, storing selection anchor and focus. | ||
* Create a value. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @param {Object} options | ||
* @return {Array} | ||
* @return {Value} | ||
*/ | ||
function createChildren(children) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
function createValue(tagName, attributes, children) { | ||
var data = attributes.data; | ||
var array = []; | ||
var length = 0; | ||
var document = children.find(Document.isDocument); | ||
var selection = children.find(Selection.isSelection); | ||
var anchor = void 0; | ||
var focus = void 0; | ||
var decorations = []; | ||
var partials = {}; | ||
// When creating the new node, try to preserve a key if one exists. | ||
var firstNodeOrText = children.find(function (c) { | ||
return typeof c !== 'string'; | ||
}); | ||
var firstText = Text.isText(firstNodeOrText) ? firstNodeOrText : null; | ||
var key = options.key ? options.key : firstText ? firstText.key : undefined; | ||
var node = Text.create({ key: key, leaves: [{ text: '', marks: options.marks }] }); | ||
// Search the document's texts to see if any of them have the anchor or | ||
// focus information saved, or decorations applied. | ||
if (document) { | ||
document.getTexts().forEach(function (text) { | ||
if (text.__anchor != null) { | ||
anchor = Point.create({ key: text.key, offset: text.__anchor.offset }); | ||
} | ||
// Create a helper to update the current node while preserving any stored | ||
// anchor or focus information. | ||
function setNode(next) { | ||
var _node = node, | ||
__anchor = _node.__anchor, | ||
__focus = _node.__focus, | ||
__decorations = _node.__decorations; | ||
if (text.__focus != null) { | ||
focus = Point.create({ key: text.key, offset: text.__focus.offset }); | ||
} | ||
if (__anchor != null) next.__anchor = __anchor; | ||
if (__focus != null) next.__focus = __focus; | ||
if (__decorations != null) next.__decorations = __decorations; | ||
node = next; | ||
} | ||
if (text.__decorations != null) { | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
children.forEach(function (child, index) { | ||
var isLast = index === children.length - 1; | ||
try { | ||
for (var _iterator = text.__decorations[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var dec = _step.value; | ||
var id = dec.id; | ||
// If the child is a non-text node, push the current node and the new child | ||
// onto the array, then creating a new node for future selection tracking. | ||
if (Node.isNode(child) && !Text.isText(child)) { | ||
if (node.text.length || node.__anchor != null || node.__focus != null || node.getMarksAtIndex(0).size) { | ||
array.push(node); | ||
var partial = partials[id]; | ||
delete partials[id]; | ||
if (!partial) { | ||
dec.key = text.key; | ||
partials[id] = dec; | ||
continue; | ||
} | ||
var decoration = Decoration.create({ | ||
anchor: { | ||
key: partial.key, | ||
offset: partial.offset | ||
}, | ||
focus: { | ||
key: text.key, | ||
offset: dec.offset | ||
}, | ||
mark: { | ||
type: dec.type, | ||
data: dec.data | ||
} | ||
}); | ||
decorations.push(decoration); | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
array.push(child); | ||
if (Object.keys(partials).length > 0) { | ||
throw new Error('Slate hyperscript must have both a start and an end defined for each decoration using the `key=` prop.'); | ||
} | ||
node = isLast ? null : Text.create({ leaves: [{ text: '', marks: options.marks }] }); | ||
if (anchor && !focus) { | ||
throw new Error('Slate hyperscript ranges must have both `<anchor />` and `<focus />` defined if one is defined, but you only defined `<anchor />`. For collapsed selections, use `<cursor />` instead.'); | ||
} | ||
length = 0; | ||
} | ||
if (!anchor && focus) { | ||
throw new Error('Slate hyperscript ranges must have both `<anchor />` and `<focus />` defined if one is defined, but you only defined `<focus />`. For collapsed selections, use `<cursor />` instead.'); | ||
} | ||
// If the child is a string insert it into the node. | ||
if (typeof child == 'string') { | ||
setNode(node.insertText(node.text.length, child, options.marks)); | ||
length += child.length; | ||
if (anchor || focus) { | ||
if (!selection) { | ||
selection = Selection.create({ anchor: anchor, focus: focus, isFocused: true }); | ||
} else { | ||
selection = selection.setPoints([anchor, focus]); | ||
} | ||
} else if (!selection) { | ||
selection = Selection.create(); | ||
} | ||
// If the node is a `Text` add its text and marks to the existing node. If | ||
// the existing node is empty, and the `key` option wasn't set, preserve the | ||
// child's key when updating the node. | ||
if (Text.isText(child)) { | ||
var __anchor = child.__anchor, | ||
__focus = child.__focus, | ||
__decorations = child.__decorations; | ||
selection = selection.normalize(document); | ||
var i = node.text.length; | ||
if (decorations.length > 0) { | ||
decorations = decorations.map(function (d) { | ||
return d.normalize(document); | ||
}); | ||
} | ||
if (!options.key && node.text.length == 0) { | ||
setNode(node.set('key', child.key)); | ||
} | ||
var value = Value.fromJSON(_extends({ | ||
data: data, | ||
decorations: decorations, | ||
document: document, | ||
selection: selection | ||
}, attributes)); | ||
child.getLeaves().forEach(function (leaf) { | ||
var marks = leaf.marks; | ||
return value; | ||
} | ||
if (options.marks) marks = marks.union(options.marks); | ||
setNode(node.insertText(i, leaf.text, marks)); | ||
i += leaf.text.length; | ||
}); | ||
/** | ||
* Point classes that can be created at different points in the document and | ||
* then searched for afterwards, for creating ranges. | ||
* | ||
* @type {Class} | ||
*/ | ||
if (__anchor != null) { | ||
node.__anchor = new AnchorPoint(); | ||
node.__anchor.offset = __anchor.offset + length; | ||
} | ||
var CursorPoint = function CursorPoint() { | ||
classCallCheck(this, CursorPoint); | ||
if (__focus != null) { | ||
node.__focus = new FocusPoint(); | ||
node.__focus.offset = __focus.offset + length; | ||
} | ||
this.offset = null; | ||
}; | ||
if (__decorations != null) { | ||
__decorations.forEach(function (d) { | ||
if (d instanceof DecorationPoint) { | ||
d.offset += length; | ||
} else { | ||
d.anchorOffset += length; | ||
d.focusOffset += length; | ||
} | ||
}); | ||
var AnchorPoint = function AnchorPoint() { | ||
var attrs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
classCallCheck(this, AnchorPoint); | ||
var _attrs$key = attrs.key, | ||
key = _attrs$key === undefined ? null : _attrs$key, | ||
_attrs$offset = attrs.offset, | ||
offset = _attrs$offset === undefined ? null : _attrs$offset, | ||
_attrs$path = attrs.path, | ||
path = _attrs$path === undefined ? null : _attrs$path; | ||
node.__decorations = node.__decorations || []; | ||
node.__decorations = node.__decorations.concat(__decorations); | ||
} | ||
this.key = key; | ||
this.offset = offset; | ||
this.path = path; | ||
}; | ||
length += child.text.length; | ||
} | ||
var FocusPoint = function FocusPoint() { | ||
var attrs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
classCallCheck(this, FocusPoint); | ||
var _attrs$key2 = attrs.key, | ||
key = _attrs$key2 === undefined ? null : _attrs$key2, | ||
_attrs$offset2 = attrs.offset, | ||
offset = _attrs$offset2 === undefined ? null : _attrs$offset2, | ||
_attrs$path2 = attrs.path, | ||
path = _attrs$path2 === undefined ? null : _attrs$path2; | ||
if (child instanceof AnchorPoint || child instanceof CursorPoint) { | ||
child.offset = length; | ||
node.__anchor = child; | ||
} | ||
this.key = key; | ||
this.offset = offset; | ||
this.path = path; | ||
}; | ||
if (child instanceof FocusPoint || child instanceof CursorPoint) { | ||
child.offset = length; | ||
node.__focus = child; | ||
} | ||
var DecorationPoint = function DecorationPoint(attrs) { | ||
classCallCheck(this, DecorationPoint); | ||
var _attrs$id = attrs.id, | ||
id = _attrs$id === undefined ? null : _attrs$id, | ||
_attrs$data = attrs.data, | ||
data = _attrs$data === undefined ? {} : _attrs$data, | ||
type = attrs.type; | ||
if (child instanceof DecorationPoint) { | ||
child.offset = length; | ||
node.__decorations = node.__decorations || []; | ||
node.__decorations = node.__decorations.concat(child); | ||
} | ||
}); | ||
this.id = id; | ||
this.offset = null; | ||
this.type = type; | ||
this.data = data; | ||
}; | ||
// Make sure the most recent node is added. | ||
if (node != null) { | ||
array.push(node); | ||
/** | ||
* Increment any existing `point` on object by `n`. | ||
* | ||
* @param {Any} object | ||
* @param {Number} n | ||
*/ | ||
function incrementPoint(object, n) { | ||
var __anchor = object.__anchor, | ||
__focus = object.__focus, | ||
__decorations = object.__decorations; | ||
if (__anchor != null) { | ||
__anchor.offset += n; | ||
} | ||
return array; | ||
if (__focus != null && __focus !== __anchor) { | ||
__focus.offset += n; | ||
} | ||
if (__decorations != null) { | ||
__decorations.forEach(function (d) { | ||
return d.offset += n; | ||
}); | ||
} | ||
} | ||
/** | ||
* Resolve a set of hyperscript creators an `options` object. | ||
* Check whether an `object` is a point. | ||
* | ||
* @param {Any} object | ||
* @return {Boolean} | ||
*/ | ||
function isPoint(object) { | ||
return object instanceof AnchorPoint || object instanceof CursorPoint || object instanceof DecorationPoint || object instanceof FocusPoint; | ||
} | ||
/** | ||
* Preserve any point information on an object. | ||
* | ||
* @param {Any} object | ||
* @param {Function} updator | ||
* @return {Any} | ||
*/ | ||
function preservePoint(object, updator) { | ||
var __anchor = object.__anchor, | ||
__focus = object.__focus, | ||
__decorations = object.__decorations; | ||
var next = updator(object); | ||
if (__anchor != null) next.__anchor = __anchor; | ||
if (__focus != null) next.__focus = __focus; | ||
if (__decorations != null) next.__decorations = __decorations; | ||
return next; | ||
} | ||
/** | ||
* Set a `point` on an `object`. | ||
* | ||
* @param {Any} object | ||
* @param {*Point} point | ||
* @param {Number} offset | ||
*/ | ||
function setPoint(object, point, offset) { | ||
if (point instanceof AnchorPoint || point instanceof CursorPoint) { | ||
point.offset = offset; | ||
object.__anchor = point; | ||
} | ||
if (point instanceof FocusPoint || point instanceof CursorPoint) { | ||
point.offset = offset; | ||
object.__focus = point; | ||
} | ||
if (point instanceof DecorationPoint) { | ||
point.offset = offset; | ||
object.__decorations = object.__decorations || []; | ||
object.__decorations = object.__decorations.concat(point); | ||
} | ||
} | ||
/** | ||
* Create a Slate hyperscript function with `options`. | ||
* | ||
* @param {Object} options | ||
* @return {Object} | ||
* @return {Function} | ||
*/ | ||
function resolveCreators(options) { | ||
function createHyperscript() { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var _options$blocks = options.blocks, | ||
@@ -536,78 +646,78 @@ blocks = _options$blocks === undefined ? {} : _options$blocks, | ||
_options$decorations = options.decorations, | ||
decorations = _options$decorations === undefined ? {} : _options$decorations, | ||
schema = options.schema; | ||
decorations = _options$decorations === undefined ? {} : _options$decorations; | ||
var creators = _extends({}, CREATORS, options.creators || {}); | ||
var creators = _extends({ | ||
anchor: createAnchor, | ||
block: createBlock, | ||
cursor: createCursor, | ||
decoration: createDecoration, | ||
document: createDocument, | ||
focus: createFocus, | ||
inline: createInline, | ||
mark: createMark, | ||
node: createNode, | ||
selection: createSelection, | ||
text: createText, | ||
value: createValue | ||
}, options.creators || {}); | ||
Object.keys(blocks).map(function (key) { | ||
creators[key] = normalizeNode(blocks[key], 'block'); | ||
}); | ||
for (var key in blocks) { | ||
creators[key] = normalizeCreator(blocks[key], createBlock); | ||
} | ||
Object.keys(inlines).map(function (key) { | ||
creators[key] = normalizeNode(inlines[key], 'inline'); | ||
}); | ||
for (var _key in inlines) { | ||
creators[_key] = normalizeCreator(inlines[_key], createInline); | ||
} | ||
Object.keys(marks).map(function (key) { | ||
creators[key] = normalizeMark(marks[key]); | ||
}); | ||
for (var _key2 in marks) { | ||
creators[_key2] = normalizeCreator(marks[_key2], createMark); | ||
} | ||
Object.keys(decorations).map(function (key) { | ||
creators[key] = normalizeNode(decorations[key], 'decoration'); | ||
}); | ||
for (var _key3 in decorations) { | ||
creators[_key3] = normalizeCreator(decorations[_key3], createDecoration); | ||
} | ||
creators.value = function (tagName) { | ||
var attributes = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var children = arguments[2]; | ||
function create(tagName, attributes) { | ||
for (var _len = arguments.length, children = Array(_len > 2 ? _len - 2 : 0), _key4 = 2; _key4 < _len; _key4++) { | ||
children[_key4 - 2] = arguments[_key4]; | ||
} | ||
var attrs = _extends({ schema: schema }, attributes); | ||
return CREATORS.value(tagName, attrs, children); | ||
}; | ||
var creator = creators[tagName]; | ||
return creators; | ||
} | ||
if (!creator) { | ||
throw new Error('No hyperscript creator found for tag: "' + tagName + '"'); | ||
} | ||
/** | ||
* Normalize a node creator of `value` and `object`. | ||
* | ||
* @param {Function|Object|String} value | ||
* @param {String} object | ||
* @return {Function} | ||
*/ | ||
if (attributes == null) { | ||
attributes = {}; | ||
} | ||
function normalizeNode(value, object) { | ||
if (typeof value == 'function') { | ||
return value; | ||
} | ||
if (!isPlainObject(attributes)) { | ||
children = [attributes].concat(children); | ||
attributes = {}; | ||
} | ||
if (typeof value == 'string') { | ||
value = { type: value }; | ||
} | ||
children = children.filter(function (child) { | ||
return Boolean(child); | ||
}).reduce(function (memo, child) { | ||
return memo.concat(child); | ||
}, []); | ||
if (isPlainObject(value)) { | ||
return function (tagName, attributes, children) { | ||
var key = attributes.key, | ||
rest = objectWithoutProperties(attributes, ['key']); | ||
var attrs = _extends({}, value, { | ||
object: object, | ||
key: key, | ||
data: _extends({}, value.data || {}, rest) | ||
}); | ||
return CREATORS[object](tagName, attrs, children); | ||
}; | ||
var ret = creator(tagName, attributes, children); | ||
return ret; | ||
} | ||
throw new Error('Slate hyperscript ' + object + ' creators can be either functions, objects or strings, but you passed: ' + value); | ||
return create; | ||
} | ||
/** | ||
* Normalize a mark creator of `value`. | ||
* Normalize a `creator` of `value`. | ||
* | ||
* @param {Function|Object|String} value | ||
* @param {Function} creator | ||
* @return {Function} | ||
*/ | ||
function normalizeMark(value) { | ||
function normalizeCreator(value, creator) { | ||
if (typeof value == 'function') { | ||
@@ -623,11 +733,15 @@ return value; | ||
return function (tagName, attributes, children) { | ||
var key = attributes.key, | ||
rest = objectWithoutProperties(attributes, ['key']); | ||
var attrs = _extends({}, value, { | ||
data: _extends({}, value.data || {}, attributes) | ||
key: key, | ||
data: _extends({}, value.data || {}, rest) | ||
}); | ||
return CREATORS.mark(tagName, attrs, children); | ||
return creator(tagName, attrs, children); | ||
}; | ||
} | ||
throw new Error('Slate hyperscript mark creators can be either functions, objects or strings, but you passed: ' + value); | ||
throw new Error('Slate hyperscript creators can be either functions, objects or strings, but you passed: ' + value); | ||
} | ||
@@ -634,0 +748,0 @@ |
@@ -7,4 +7,4 @@ 'use strict'; | ||
var slate = require('slate'); | ||
var isPlainObject = _interopDefault(require('is-plain-object')); | ||
var slate = require('slate'); | ||
@@ -64,473 +64,583 @@ var classCallCheck = function (instance, Constructor) { | ||
/** | ||
* Point classes that can be created at different points in the document and | ||
* then searched for afterwards, for creating ranges. | ||
* Auto-incrementing ID to keep track of paired decorations. | ||
* | ||
* @type {Class} | ||
* @type {Number} | ||
*/ | ||
var CursorPoint = function CursorPoint() { | ||
classCallCheck(this, CursorPoint); | ||
var uid = 0; | ||
this.offset = null; | ||
}; | ||
/** | ||
* Create an anchor point. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {AnchorPoint} | ||
*/ | ||
var AnchorPoint = function AnchorPoint() { | ||
var attrs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
classCallCheck(this, AnchorPoint); | ||
var _attrs$key = attrs.key, | ||
key = _attrs$key === undefined ? null : _attrs$key, | ||
_attrs$offset = attrs.offset, | ||
offset = _attrs$offset === undefined ? null : _attrs$offset, | ||
_attrs$path = attrs.path, | ||
path = _attrs$path === undefined ? null : _attrs$path; | ||
function createAnchor(tagName, attributes, children) { | ||
return new AnchorPoint(attributes); | ||
} | ||
this.key = key; | ||
this.offset = offset; | ||
this.path = path; | ||
}; | ||
/** | ||
* Create a block. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {Block} | ||
*/ | ||
var FocusPoint = function FocusPoint() { | ||
var attrs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
classCallCheck(this, FocusPoint); | ||
var _attrs$key2 = attrs.key, | ||
key = _attrs$key2 === undefined ? null : _attrs$key2, | ||
_attrs$offset2 = attrs.offset, | ||
offset = _attrs$offset2 === undefined ? null : _attrs$offset2, | ||
_attrs$path2 = attrs.path, | ||
path = _attrs$path2 === undefined ? null : _attrs$path2; | ||
function createBlock(tagName, attributes, children) { | ||
var attrs = _extends({}, attributes, { object: 'block' }); | ||
var block = createNode('node', attrs, children); | ||
return block; | ||
} | ||
this.key = key; | ||
this.offset = offset; | ||
this.path = path; | ||
}; | ||
/** | ||
* Create a cursor point. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {CursorPoint} | ||
*/ | ||
var DecorationPoint = function DecorationPoint(attrs) { | ||
var _this = this; | ||
function createCursor(tagName, attributes, children) { | ||
return new CursorPoint(attributes); | ||
} | ||
classCallCheck(this, DecorationPoint); | ||
/** | ||
* Create a decoration point, or wrap a list of leaves and set the decoration | ||
* point tracker on them. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {DecorationPoint|List<Leaf>} | ||
*/ | ||
this.combine = function (focus) { | ||
if (!(focus instanceof DecorationPoint)) { | ||
throw new Error('misaligned decorations'); | ||
} | ||
function createDecoration(tagName, attributes, children) { | ||
var key = attributes.key, | ||
data = attributes.data; | ||
return slate.Decoration.create({ | ||
anchor: { | ||
key: _this.key, | ||
offset: _this.offset | ||
}, | ||
focus: { | ||
key: focus.key, | ||
offset: focus.offset | ||
}, | ||
mark: { | ||
type: _this.type, | ||
data: _this.data | ||
} | ||
}); | ||
}; | ||
var type = tagName; | ||
var _attrs$key3 = attrs.key, | ||
key = _attrs$key3 === undefined ? null : _attrs$key3, | ||
_attrs$data = attrs.data, | ||
data = _attrs$data === undefined ? {} : _attrs$data, | ||
type = attrs.type; | ||
if (key) { | ||
return new DecorationPoint({ id: key, type: type, data: data }); | ||
} | ||
this.id = key; | ||
this.offset = 0; | ||
this.type = type; | ||
this.data = data; | ||
}; | ||
var leaves = createLeaves('leaves', {}, children); | ||
var first = leaves.first(); | ||
var last = leaves.last(); | ||
var id = '__decoration_' + uid++ + '__'; | ||
var start = new DecorationPoint({ id: id, type: type, data: data }); | ||
var end = new DecorationPoint({ id: id, type: type, data: data }); | ||
setPoint(first, start, 0); | ||
setPoint(last, end, last.text.length); | ||
return leaves; | ||
} | ||
/** | ||
* The default Slate hyperscript creator functions. | ||
* Create a document. | ||
* | ||
* @type {Object} | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {Document} | ||
*/ | ||
var CREATORS = { | ||
anchor: function anchor(tagName, attributes, children) { | ||
return new AnchorPoint(attributes); | ||
}, | ||
block: function block(tagName, attributes, children) { | ||
return slate.Block.create(_extends({}, attributes, { | ||
nodes: createChildren(children) | ||
})); | ||
}, | ||
cursor: function cursor(tagName, attributes, children) { | ||
return new CursorPoint(); | ||
}, | ||
decoration: function decoration(tagName, attributes, children) { | ||
var key = attributes.key, | ||
data = attributes.data; | ||
function createDocument(tagName, attributes, children) { | ||
var attrs = _extends({}, attributes, { object: 'document' }); | ||
var document = createNode('node', attrs, children); | ||
return document; | ||
} | ||
var type = tagName; | ||
/** | ||
* Create a focus point. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {FocusPoint} | ||
*/ | ||
if (key) { | ||
return new DecorationPoint({ key: key, type: type, data: data }); | ||
} | ||
function createFocus(tagName, attributes, children) { | ||
return new FocusPoint(attributes); | ||
} | ||
var nodes = createChildren(children); | ||
var node = nodes[0]; | ||
/** | ||
* Create an inline. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {Inline} | ||
*/ | ||
var _node$__decorations = node.__decorations, | ||
__decorations = _node$__decorations === undefined ? [] : _node$__decorations; | ||
function createInline(tagName, attributes, children) { | ||
var attrs = _extends({}, attributes, { object: 'inline' }); | ||
var inline = createNode('node', attrs, children); | ||
return inline; | ||
} | ||
var __decoration = { | ||
anchorOffset: 0, | ||
focusOffset: nodes.reduce(function (len, n) { | ||
return len + n.text.length; | ||
}, 0), | ||
type: type, | ||
data: data | ||
}; | ||
/** | ||
* Create a list of leaves. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {List<Leaf>} | ||
*/ | ||
__decorations.push(__decoration); | ||
node.__decorations = __decorations; | ||
return nodes; | ||
}, | ||
document: function document(tagName, attributes, children) { | ||
return slate.Document.create(_extends({}, attributes, { | ||
nodes: createChildren(children) | ||
})); | ||
}, | ||
focus: function focus(tagName, attributes, children) { | ||
return new FocusPoint(attributes); | ||
}, | ||
inline: function inline(tagName, attributes, children) { | ||
return slate.Inline.create(_extends({}, attributes, { | ||
nodes: createChildren(children) | ||
})); | ||
}, | ||
mark: function mark(tagName, attributes, children) { | ||
var marks = slate.Mark.createSet([attributes]); | ||
var nodes = createChildren(children, { marks: marks }); | ||
return nodes; | ||
}, | ||
selection: function selection(tagName, attributes, children) { | ||
var anchor = children.find(function (c) { | ||
return c instanceof AnchorPoint; | ||
}); | ||
var focus = children.find(function (c) { | ||
return c instanceof FocusPoint; | ||
}); | ||
var marks = attributes.marks, | ||
focused = attributes.focused; | ||
function createLeaves(tagName, attributes, children) { | ||
var _attributes$marks = attributes.marks, | ||
marks = _attributes$marks === undefined ? slate.Mark.createSet() : _attributes$marks; | ||
var selection = slate.Selection.create({ | ||
marks: marks, | ||
isFocused: focused, | ||
anchor: anchor && { | ||
key: anchor.key, | ||
offset: anchor.offset, | ||
path: anchor.path | ||
}, | ||
focus: focus && { | ||
key: focus.key, | ||
offset: focus.offset, | ||
path: focus.path | ||
var length = 0; | ||
var leaves = slate.Leaf.createList([]); | ||
var leaf = void 0; | ||
children.forEach(function (child) { | ||
if (slate.Leaf.isLeafList(child)) { | ||
if (leaf) { | ||
leaves = leaves.push(leaf); | ||
leaf = null; | ||
} | ||
}); | ||
return selection; | ||
}, | ||
text: function text(tagName, attributes, children) { | ||
var nodes = createChildren(children, { key: attributes.key }); | ||
return nodes; | ||
}, | ||
value: function value(tagName, attributes, children) { | ||
var data = attributes.data, | ||
_attributes$normalize = attributes.normalize, | ||
normalize = _attributes$normalize === undefined ? true : _attributes$normalize; | ||
child.forEach(function (l) { | ||
l = preservePoint(l, function (obj) { | ||
return obj.addMarks(marks); | ||
}); | ||
leaves = leaves.push(l); | ||
}); | ||
} else { | ||
if (!leaf) { | ||
leaf = slate.Leaf.create({ marks: marks, text: '' }); | ||
length = 0; | ||
} | ||
var document = children.find(slate.Document.isDocument); | ||
var selection = children.find(slate.Selection.isSelection) || slate.Selection.create(); | ||
var anchor = void 0; | ||
var focus = void 0; | ||
var decorations = []; | ||
var partials = {}; | ||
if (typeof child === 'string') { | ||
var offset = leaf.text.length; | ||
leaf = preservePoint(leaf, function (obj) { | ||
return obj.insertText(offset, child); | ||
}); | ||
length += child.length; | ||
} | ||
// Search the document's texts to see if any of them have the anchor or | ||
// focus information saved, or decorations applied. | ||
if (document) { | ||
document.getTexts().forEach(function (text) { | ||
if (text.__anchor != null) { | ||
anchor = slate.Point.create({ key: text.key, offset: text.__anchor.offset }); | ||
} | ||
if (isPoint(child)) { | ||
setPoint(leaf, child, length); | ||
} | ||
} | ||
}); | ||
if (text.__focus != null) { | ||
focus = slate.Point.create({ key: text.key, offset: text.__focus.offset }); | ||
} | ||
if (!leaves.size && !leaf) { | ||
leaf = slate.Leaf.create({ marks: marks, text: '' }); | ||
} | ||
if (text.__decorations != null) { | ||
text.__decorations.forEach(function (dec) { | ||
var id = dec.id; | ||
if (leaf) { | ||
leaves = leaves.push(leaf); | ||
} | ||
var range = void 0; | ||
return leaves; | ||
} | ||
if (!id) { | ||
range = slate.Decoration.create({ | ||
anchor: { | ||
key: text.key, | ||
offset: dec.anchorOffset | ||
}, | ||
focus: { | ||
key: text.key, | ||
offset: dec.focusOffset | ||
}, | ||
mark: { | ||
type: dec.type, | ||
data: dec.data | ||
} | ||
}); | ||
} else if (partials[id]) { | ||
var partial = partials[id]; | ||
delete partials[id]; | ||
/** | ||
* Create a list of leaves from a mark. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {List<Leaf>} | ||
*/ | ||
range = slate.Decoration.create({ | ||
anchor: { | ||
key: partial.key, | ||
offset: partial.offset | ||
}, | ||
focus: { | ||
key: text.key, | ||
offset: dec.offset | ||
}, | ||
mark: { | ||
type: dec.type, | ||
data: dec.data | ||
} | ||
}); | ||
} else { | ||
dec.key = text.key; | ||
partials[id] = dec; | ||
} | ||
function createMark(tagName, attributes, children) { | ||
var marks = slate.Mark.createSet([attributes]); | ||
var leaves = createLeaves('leaves', { marks: marks }, children); | ||
return leaves; | ||
} | ||
if (range) { | ||
decorations.push(range); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
/** | ||
* Create a node. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {Node} | ||
*/ | ||
if (Object.keys(partials).length > 0) { | ||
throw new Error('Slate hyperscript must have both a start and an end defined for each decoration using the `key=` prop.'); | ||
} | ||
function createNode(tagName, attributes, children) { | ||
var object = attributes.object; | ||
if (anchor && !focus) { | ||
throw new Error('Slate hyperscript ranges must have both `<anchor />` and `<focus />` defined if one is defined, but you only defined `<anchor />`. For collapsed selections, use `<cursor />` instead.'); | ||
} | ||
if (!anchor && focus) { | ||
throw new Error('Slate hyperscript ranges must have both `<anchor />` and `<focus />` defined if one is defined, but you only defined `<focus />`. For collapsed selections, use `<cursor />` instead.'); | ||
} | ||
if (object === 'text') { | ||
return createText('text', {}, children); | ||
} | ||
var value = slate.Value.fromJSON(_extends({ data: data, document: document, selection: selection }, attributes), { normalize: normalize }); | ||
var nodes = []; | ||
var others = []; | ||
if (anchor || focus) { | ||
selection = selection.setPoints([anchor, focus]); | ||
selection = selection.setIsFocused(true); | ||
selection = selection.normalize(value.document); | ||
value = value.set('selection', selection); | ||
} | ||
children.forEach(function (child) { | ||
if (slate.Node.isNode(child)) { | ||
if (others.length) { | ||
var text = createText('text', {}, others); | ||
nodes.push(text); | ||
} | ||
if (decorations.length > 0) { | ||
decorations = decorations.map(function (d) { | ||
return d.normalize(value.document); | ||
}); | ||
decorations = slate.Decoration.createList(decorations); | ||
value = value.set('decorations', decorations); | ||
nodes.push(child); | ||
others = []; | ||
} else { | ||
others.push(child); | ||
} | ||
}); | ||
return value; | ||
if (others.length) { | ||
var text = createText('text', {}, others); | ||
nodes.push(text); | ||
} | ||
}; | ||
var node = slate.Node.create(_extends({}, attributes, { nodes: nodes })); | ||
return node; | ||
} | ||
/** | ||
* Create a Slate hyperscript function with `options`. | ||
* Create a selection. | ||
* | ||
* @param {Object} options | ||
* @return {Function} | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {Selection} | ||
*/ | ||
function createHyperscript() { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
function createSelection(tagName, attributes, children) { | ||
var anchor = children.find(function (c) { | ||
return c instanceof AnchorPoint; | ||
}); | ||
var focus = children.find(function (c) { | ||
return c instanceof FocusPoint; | ||
}); | ||
var marks = attributes.marks, | ||
focused = attributes.focused; | ||
var creators = resolveCreators(options); | ||
function create(tagName, attributes) { | ||
for (var _len = arguments.length, children = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { | ||
children[_key - 2] = arguments[_key]; | ||
var selection = slate.Selection.create({ | ||
marks: marks, | ||
isFocused: focused, | ||
anchor: anchor && { | ||
key: anchor.key, | ||
offset: anchor.offset, | ||
path: anchor.path | ||
}, | ||
focus: focus && { | ||
key: focus.key, | ||
offset: focus.offset, | ||
path: focus.path | ||
} | ||
}); | ||
var creator = creators[tagName]; | ||
return selection; | ||
} | ||
if (!creator) { | ||
throw new Error('No hyperscript creator found for tag: "' + tagName + '"'); | ||
} | ||
/** | ||
* Create a text node. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @return {Text} | ||
*/ | ||
if (attributes == null) { | ||
attributes = {}; | ||
} | ||
function createText(tagName, attributes, children) { | ||
var key = attributes.key; | ||
if (!isPlainObject(attributes)) { | ||
children = [attributes].concat(children); | ||
attributes = {}; | ||
} | ||
var leaves = createLeaves('leaves', {}, children); | ||
var text = slate.Text.create({ key: key, leaves: leaves }); | ||
var length = 0; | ||
children = children.filter(function (child) { | ||
return Boolean(child); | ||
}).reduce(function (memo, child) { | ||
return memo.concat(child); | ||
}, []); | ||
leaves.forEach(function (leaf) { | ||
incrementPoint(leaf, length); | ||
preservePoint(leaf, function () { | ||
return text; | ||
}); | ||
length += leaf.text.length; | ||
}); | ||
var element = creator(tagName, attributes, children); | ||
return element; | ||
} | ||
return create; | ||
return text; | ||
} | ||
/** | ||
* Create an array of `children`, storing selection anchor and focus. | ||
* Create a value. | ||
* | ||
* @param {String} tagName | ||
* @param {Object} attributes | ||
* @param {Array} children | ||
* @param {Object} options | ||
* @return {Array} | ||
* @return {Value} | ||
*/ | ||
function createChildren(children) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
function createValue(tagName, attributes, children) { | ||
var data = attributes.data; | ||
var array = []; | ||
var length = 0; | ||
var document = children.find(slate.Document.isDocument); | ||
var selection = children.find(slate.Selection.isSelection); | ||
var anchor = void 0; | ||
var focus = void 0; | ||
var decorations = []; | ||
var partials = {}; | ||
// When creating the new node, try to preserve a key if one exists. | ||
var firstNodeOrText = children.find(function (c) { | ||
return typeof c !== 'string'; | ||
}); | ||
var firstText = slate.Text.isText(firstNodeOrText) ? firstNodeOrText : null; | ||
var key = options.key ? options.key : firstText ? firstText.key : undefined; | ||
var node = slate.Text.create({ key: key, leaves: [{ text: '', marks: options.marks }] }); | ||
// Search the document's texts to see if any of them have the anchor or | ||
// focus information saved, or decorations applied. | ||
if (document) { | ||
document.getTexts().forEach(function (text) { | ||
if (text.__anchor != null) { | ||
anchor = slate.Point.create({ key: text.key, offset: text.__anchor.offset }); | ||
} | ||
// Create a helper to update the current node while preserving any stored | ||
// anchor or focus information. | ||
function setNode(next) { | ||
var _node = node, | ||
__anchor = _node.__anchor, | ||
__focus = _node.__focus, | ||
__decorations = _node.__decorations; | ||
if (text.__focus != null) { | ||
focus = slate.Point.create({ key: text.key, offset: text.__focus.offset }); | ||
} | ||
if (__anchor != null) next.__anchor = __anchor; | ||
if (__focus != null) next.__focus = __focus; | ||
if (__decorations != null) next.__decorations = __decorations; | ||
node = next; | ||
} | ||
if (text.__decorations != null) { | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
children.forEach(function (child, index) { | ||
var isLast = index === children.length - 1; | ||
try { | ||
for (var _iterator = text.__decorations[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var dec = _step.value; | ||
var id = dec.id; | ||
// If the child is a non-text node, push the current node and the new child | ||
// onto the array, then creating a new node for future selection tracking. | ||
if (slate.Node.isNode(child) && !slate.Text.isText(child)) { | ||
if (node.text.length || node.__anchor != null || node.__focus != null || node.getMarksAtIndex(0).size) { | ||
array.push(node); | ||
var partial = partials[id]; | ||
delete partials[id]; | ||
if (!partial) { | ||
dec.key = text.key; | ||
partials[id] = dec; | ||
continue; | ||
} | ||
var decoration = slate.Decoration.create({ | ||
anchor: { | ||
key: partial.key, | ||
offset: partial.offset | ||
}, | ||
focus: { | ||
key: text.key, | ||
offset: dec.offset | ||
}, | ||
mark: { | ||
type: dec.type, | ||
data: dec.data | ||
} | ||
}); | ||
decorations.push(decoration); | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
array.push(child); | ||
if (Object.keys(partials).length > 0) { | ||
throw new Error('Slate hyperscript must have both a start and an end defined for each decoration using the `key=` prop.'); | ||
} | ||
node = isLast ? null : slate.Text.create({ leaves: [{ text: '', marks: options.marks }] }); | ||
if (anchor && !focus) { | ||
throw new Error('Slate hyperscript ranges must have both `<anchor />` and `<focus />` defined if one is defined, but you only defined `<anchor />`. For collapsed selections, use `<cursor />` instead.'); | ||
} | ||
length = 0; | ||
} | ||
if (!anchor && focus) { | ||
throw new Error('Slate hyperscript ranges must have both `<anchor />` and `<focus />` defined if one is defined, but you only defined `<focus />`. For collapsed selections, use `<cursor />` instead.'); | ||
} | ||
// If the child is a string insert it into the node. | ||
if (typeof child == 'string') { | ||
setNode(node.insertText(node.text.length, child, options.marks)); | ||
length += child.length; | ||
if (anchor || focus) { | ||
if (!selection) { | ||
selection = slate.Selection.create({ anchor: anchor, focus: focus, isFocused: true }); | ||
} else { | ||
selection = selection.setPoints([anchor, focus]); | ||
} | ||
} else if (!selection) { | ||
selection = slate.Selection.create(); | ||
} | ||
// If the node is a `Text` add its text and marks to the existing node. If | ||
// the existing node is empty, and the `key` option wasn't set, preserve the | ||
// child's key when updating the node. | ||
if (slate.Text.isText(child)) { | ||
var __anchor = child.__anchor, | ||
__focus = child.__focus, | ||
__decorations = child.__decorations; | ||
selection = selection.normalize(document); | ||
var i = node.text.length; | ||
if (decorations.length > 0) { | ||
decorations = decorations.map(function (d) { | ||
return d.normalize(document); | ||
}); | ||
} | ||
if (!options.key && node.text.length == 0) { | ||
setNode(node.set('key', child.key)); | ||
} | ||
var value = slate.Value.fromJSON(_extends({ | ||
data: data, | ||
decorations: decorations, | ||
document: document, | ||
selection: selection | ||
}, attributes)); | ||
child.getLeaves().forEach(function (leaf) { | ||
var marks = leaf.marks; | ||
return value; | ||
} | ||
if (options.marks) marks = marks.union(options.marks); | ||
setNode(node.insertText(i, leaf.text, marks)); | ||
i += leaf.text.length; | ||
}); | ||
/** | ||
* Point classes that can be created at different points in the document and | ||
* then searched for afterwards, for creating ranges. | ||
* | ||
* @type {Class} | ||
*/ | ||
if (__anchor != null) { | ||
node.__anchor = new AnchorPoint(); | ||
node.__anchor.offset = __anchor.offset + length; | ||
} | ||
var CursorPoint = function CursorPoint() { | ||
classCallCheck(this, CursorPoint); | ||
if (__focus != null) { | ||
node.__focus = new FocusPoint(); | ||
node.__focus.offset = __focus.offset + length; | ||
} | ||
this.offset = null; | ||
}; | ||
if (__decorations != null) { | ||
__decorations.forEach(function (d) { | ||
if (d instanceof DecorationPoint) { | ||
d.offset += length; | ||
} else { | ||
d.anchorOffset += length; | ||
d.focusOffset += length; | ||
} | ||
}); | ||
var AnchorPoint = function AnchorPoint() { | ||
var attrs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
classCallCheck(this, AnchorPoint); | ||
var _attrs$key = attrs.key, | ||
key = _attrs$key === undefined ? null : _attrs$key, | ||
_attrs$offset = attrs.offset, | ||
offset = _attrs$offset === undefined ? null : _attrs$offset, | ||
_attrs$path = attrs.path, | ||
path = _attrs$path === undefined ? null : _attrs$path; | ||
node.__decorations = node.__decorations || []; | ||
node.__decorations = node.__decorations.concat(__decorations); | ||
} | ||
this.key = key; | ||
this.offset = offset; | ||
this.path = path; | ||
}; | ||
length += child.text.length; | ||
} | ||
var FocusPoint = function FocusPoint() { | ||
var attrs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
classCallCheck(this, FocusPoint); | ||
var _attrs$key2 = attrs.key, | ||
key = _attrs$key2 === undefined ? null : _attrs$key2, | ||
_attrs$offset2 = attrs.offset, | ||
offset = _attrs$offset2 === undefined ? null : _attrs$offset2, | ||
_attrs$path2 = attrs.path, | ||
path = _attrs$path2 === undefined ? null : _attrs$path2; | ||
if (child instanceof AnchorPoint || child instanceof CursorPoint) { | ||
child.offset = length; | ||
node.__anchor = child; | ||
} | ||
this.key = key; | ||
this.offset = offset; | ||
this.path = path; | ||
}; | ||
if (child instanceof FocusPoint || child instanceof CursorPoint) { | ||
child.offset = length; | ||
node.__focus = child; | ||
} | ||
var DecorationPoint = function DecorationPoint(attrs) { | ||
classCallCheck(this, DecorationPoint); | ||
var _attrs$id = attrs.id, | ||
id = _attrs$id === undefined ? null : _attrs$id, | ||
_attrs$data = attrs.data, | ||
data = _attrs$data === undefined ? {} : _attrs$data, | ||
type = attrs.type; | ||
if (child instanceof DecorationPoint) { | ||
child.offset = length; | ||
node.__decorations = node.__decorations || []; | ||
node.__decorations = node.__decorations.concat(child); | ||
} | ||
}); | ||
this.id = id; | ||
this.offset = null; | ||
this.type = type; | ||
this.data = data; | ||
}; | ||
// Make sure the most recent node is added. | ||
if (node != null) { | ||
array.push(node); | ||
/** | ||
* Increment any existing `point` on object by `n`. | ||
* | ||
* @param {Any} object | ||
* @param {Number} n | ||
*/ | ||
function incrementPoint(object, n) { | ||
var __anchor = object.__anchor, | ||
__focus = object.__focus, | ||
__decorations = object.__decorations; | ||
if (__anchor != null) { | ||
__anchor.offset += n; | ||
} | ||
return array; | ||
if (__focus != null && __focus !== __anchor) { | ||
__focus.offset += n; | ||
} | ||
if (__decorations != null) { | ||
__decorations.forEach(function (d) { | ||
return d.offset += n; | ||
}); | ||
} | ||
} | ||
/** | ||
* Resolve a set of hyperscript creators an `options` object. | ||
* Check whether an `object` is a point. | ||
* | ||
* @param {Any} object | ||
* @return {Boolean} | ||
*/ | ||
function isPoint(object) { | ||
return object instanceof AnchorPoint || object instanceof CursorPoint || object instanceof DecorationPoint || object instanceof FocusPoint; | ||
} | ||
/** | ||
* Preserve any point information on an object. | ||
* | ||
* @param {Any} object | ||
* @param {Function} updator | ||
* @return {Any} | ||
*/ | ||
function preservePoint(object, updator) { | ||
var __anchor = object.__anchor, | ||
__focus = object.__focus, | ||
__decorations = object.__decorations; | ||
var next = updator(object); | ||
if (__anchor != null) next.__anchor = __anchor; | ||
if (__focus != null) next.__focus = __focus; | ||
if (__decorations != null) next.__decorations = __decorations; | ||
return next; | ||
} | ||
/** | ||
* Set a `point` on an `object`. | ||
* | ||
* @param {Any} object | ||
* @param {*Point} point | ||
* @param {Number} offset | ||
*/ | ||
function setPoint(object, point, offset) { | ||
if (point instanceof AnchorPoint || point instanceof CursorPoint) { | ||
point.offset = offset; | ||
object.__anchor = point; | ||
} | ||
if (point instanceof FocusPoint || point instanceof CursorPoint) { | ||
point.offset = offset; | ||
object.__focus = point; | ||
} | ||
if (point instanceof DecorationPoint) { | ||
point.offset = offset; | ||
object.__decorations = object.__decorations || []; | ||
object.__decorations = object.__decorations.concat(point); | ||
} | ||
} | ||
/** | ||
* Create a Slate hyperscript function with `options`. | ||
* | ||
* @param {Object} options | ||
* @return {Object} | ||
* @return {Function} | ||
*/ | ||
function resolveCreators(options) { | ||
function createHyperscript() { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var _options$blocks = options.blocks, | ||
@@ -543,78 +653,78 @@ blocks = _options$blocks === undefined ? {} : _options$blocks, | ||
_options$decorations = options.decorations, | ||
decorations = _options$decorations === undefined ? {} : _options$decorations, | ||
schema = options.schema; | ||
decorations = _options$decorations === undefined ? {} : _options$decorations; | ||
var creators = _extends({}, CREATORS, options.creators || {}); | ||
var creators = _extends({ | ||
anchor: createAnchor, | ||
block: createBlock, | ||
cursor: createCursor, | ||
decoration: createDecoration, | ||
document: createDocument, | ||
focus: createFocus, | ||
inline: createInline, | ||
mark: createMark, | ||
node: createNode, | ||
selection: createSelection, | ||
text: createText, | ||
value: createValue | ||
}, options.creators || {}); | ||
Object.keys(blocks).map(function (key) { | ||
creators[key] = normalizeNode(blocks[key], 'block'); | ||
}); | ||
for (var key in blocks) { | ||
creators[key] = normalizeCreator(blocks[key], createBlock); | ||
} | ||
Object.keys(inlines).map(function (key) { | ||
creators[key] = normalizeNode(inlines[key], 'inline'); | ||
}); | ||
for (var _key in inlines) { | ||
creators[_key] = normalizeCreator(inlines[_key], createInline); | ||
} | ||
Object.keys(marks).map(function (key) { | ||
creators[key] = normalizeMark(marks[key]); | ||
}); | ||
for (var _key2 in marks) { | ||
creators[_key2] = normalizeCreator(marks[_key2], createMark); | ||
} | ||
Object.keys(decorations).map(function (key) { | ||
creators[key] = normalizeNode(decorations[key], 'decoration'); | ||
}); | ||
for (var _key3 in decorations) { | ||
creators[_key3] = normalizeCreator(decorations[_key3], createDecoration); | ||
} | ||
creators.value = function (tagName) { | ||
var attributes = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var children = arguments[2]; | ||
function create(tagName, attributes) { | ||
for (var _len = arguments.length, children = Array(_len > 2 ? _len - 2 : 0), _key4 = 2; _key4 < _len; _key4++) { | ||
children[_key4 - 2] = arguments[_key4]; | ||
} | ||
var attrs = _extends({ schema: schema }, attributes); | ||
return CREATORS.value(tagName, attrs, children); | ||
}; | ||
var creator = creators[tagName]; | ||
return creators; | ||
} | ||
if (!creator) { | ||
throw new Error('No hyperscript creator found for tag: "' + tagName + '"'); | ||
} | ||
/** | ||
* Normalize a node creator of `value` and `object`. | ||
* | ||
* @param {Function|Object|String} value | ||
* @param {String} object | ||
* @return {Function} | ||
*/ | ||
if (attributes == null) { | ||
attributes = {}; | ||
} | ||
function normalizeNode(value, object) { | ||
if (typeof value == 'function') { | ||
return value; | ||
} | ||
if (!isPlainObject(attributes)) { | ||
children = [attributes].concat(children); | ||
attributes = {}; | ||
} | ||
if (typeof value == 'string') { | ||
value = { type: value }; | ||
} | ||
children = children.filter(function (child) { | ||
return Boolean(child); | ||
}).reduce(function (memo, child) { | ||
return memo.concat(child); | ||
}, []); | ||
if (isPlainObject(value)) { | ||
return function (tagName, attributes, children) { | ||
var key = attributes.key, | ||
rest = objectWithoutProperties(attributes, ['key']); | ||
var attrs = _extends({}, value, { | ||
object: object, | ||
key: key, | ||
data: _extends({}, value.data || {}, rest) | ||
}); | ||
return CREATORS[object](tagName, attrs, children); | ||
}; | ||
var ret = creator(tagName, attributes, children); | ||
return ret; | ||
} | ||
throw new Error('Slate hyperscript ' + object + ' creators can be either functions, objects or strings, but you passed: ' + value); | ||
return create; | ||
} | ||
/** | ||
* Normalize a mark creator of `value`. | ||
* Normalize a `creator` of `value`. | ||
* | ||
* @param {Function|Object|String} value | ||
* @param {Function} creator | ||
* @return {Function} | ||
*/ | ||
function normalizeMark(value) { | ||
function normalizeCreator(value, creator) { | ||
if (typeof value == 'function') { | ||
@@ -630,11 +740,15 @@ return value; | ||
return function (tagName, attributes, children) { | ||
var key = attributes.key, | ||
rest = objectWithoutProperties(attributes, ['key']); | ||
var attrs = _extends({}, value, { | ||
data: _extends({}, value.data || {}, attributes) | ||
key: key, | ||
data: _extends({}, value.data || {}, rest) | ||
}); | ||
return CREATORS.mark(tagName, attrs, children); | ||
return creator(tagName, attrs, children); | ||
}; | ||
} | ||
throw new Error('Slate hyperscript mark creators can be either functions, objects or strings, but you passed: ' + value); | ||
throw new Error('Slate hyperscript creators can be either functions, objects or strings, but you passed: ' + value); | ||
} | ||
@@ -641,0 +755,0 @@ |
{ | ||
"name": "slate-hyperscript", | ||
"description": "A hyperscript helper for creating Slate documents.", | ||
"version": "0.10.8", | ||
"version": "0.11.0", | ||
"license": "MIT", | ||
@@ -23,3 +23,3 @@ "repository": "git://github.com/ianstormtaylor/slate.git", | ||
"mocha": "^2.5.3", | ||
"slate": "^0.41.3" | ||
"slate": "^0.42.0" | ||
}, | ||
@@ -26,0 +26,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
121607
1895
1