UNPKG

1.5 kBJavaScriptView Raw
1'use strict';
2
3var _require = require('immutable'),
4 List = _require.List;
5
6var isList = require('./isList');
7var getCurrentItem = require('./getCurrentItem');
8
9/**
10 * Return the list of items at the given range. The returned items are
11 * the highest list item blocks that cover the range.
12 *
13 * @param {PluginOptions} opts
14 * @param {Slate.Node} value
15 * @param {Slate.Selection} [range]
16 * @return {List<Slate.Block>} Empty if no list of items can cover the range
17 */
18function getItemsAtRange(opts, value, range) {
19 range = range || value.selection;
20
21 if (!range.startKey) {
22 return List();
23 }
24
25 var document = value.document;
26
27
28 var startBlock = document.getClosestBlock(range.startKey);
29 var endBlock = document.getClosestBlock(range.endKey);
30
31 if (startBlock === endBlock) {
32 var item = getCurrentItem(opts, value, startBlock);
33 return item ? List([item]) : List();
34 }
35
36 var ancestor = document.getCommonAncestor(startBlock.key, endBlock.key);
37
38 if (isList(opts, ancestor)) {
39 var startPath = ancestor.getPath(startBlock.key);
40 var endPath = ancestor.getPath(endBlock.key);
41
42 return ancestor.nodes.slice(startPath[0], endPath[0] + 1);
43 } else if (ancestor.type === opts.typeItem) {
44 // The ancestor is the highest list item that covers the range
45 return List([ancestor]);
46 } else {
47 // No list of items can cover the range
48 return List();
49 }
50}
51
52module.exports = getItemsAtRange;
\No newline at end of file