1 | 'use strict';
|
2 |
|
3 | function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
|
4 |
|
5 | var Options = require('./options');
|
6 | var onEnter = require('./onEnter');
|
7 | var onTab = require('./onTab');
|
8 | var onBackspace = require('./onBackspace');
|
9 | var makeSchema = require('./makeSchema');
|
10 |
|
11 | var wrapInList = require('./changes/wrapInList');
|
12 | var unwrapList = require('./changes/unwrapList');
|
13 | var splitListItem = require('./changes/splitListItem');
|
14 | var increaseItemDepth = require('./changes/increaseItemDepth');
|
15 | var decreaseItemDepth = require('./changes/decreaseItemDepth');
|
16 |
|
17 | var getItemDepth = require('./getItemDepth');
|
18 | var isList = require('./isList');
|
19 | var isSelectionInList = require('./isSelectionInList');
|
20 | var getCurrentItem = require('./getCurrentItem');
|
21 | var getCurrentList = require('./getCurrentList');
|
22 | var getItemsAtRange = require('./getItemsAtRange');
|
23 | var getPreviousItem = require('./getPreviousItem');
|
24 |
|
25 | var KEY_ENTER = 'Enter';
|
26 | var KEY_TAB = 'Tab';
|
27 | var KEY_BACKSPACE = 'Backspace';
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | function EditList() {
|
36 | var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
37 |
|
38 | opts = new Options(opts);
|
39 |
|
40 | |
41 |
|
42 |
|
43 | function bindChange(fn) {
|
44 | return function (change) {
|
45 | var value = change.value;
|
46 |
|
47 |
|
48 | if (!isSelectionInList(opts, value)) {
|
49 | return change;
|
50 | }
|
51 |
|
52 | for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
53 | args[_key - 1] = arguments[_key];
|
54 | }
|
55 |
|
56 | return fn.apply(undefined, _toConsumableArray([opts, change].concat(args)));
|
57 | };
|
58 | }
|
59 |
|
60 | |
61 |
|
62 |
|
63 | function onKeyDown(event, change, editor) {
|
64 | var args = [event, change, editor, opts];
|
65 |
|
66 | switch (event.key) {
|
67 | case KEY_ENTER:
|
68 | return onEnter.apply(undefined, args);
|
69 | case KEY_TAB:
|
70 | return onTab.apply(undefined, args);
|
71 | case KEY_BACKSPACE:
|
72 | return onBackspace.apply(undefined, args);
|
73 | }
|
74 | }
|
75 |
|
76 | var _makeSchema = makeSchema(opts),
|
77 | schema = _makeSchema.schema,
|
78 | validateNode = _makeSchema.validateNode;
|
79 |
|
80 | return {
|
81 | onKeyDown: onKeyDown,
|
82 |
|
83 | schema: schema,
|
84 | validateNode: validateNode,
|
85 |
|
86 | utils: {
|
87 | getCurrentItem: getCurrentItem.bind(null, opts),
|
88 | getCurrentList: getCurrentList.bind(null, opts),
|
89 | getItemDepth: getItemDepth.bind(null, opts),
|
90 | getItemsAtRange: getItemsAtRange.bind(null, opts),
|
91 | getPreviousItem: getPreviousItem.bind(null, opts),
|
92 | isList: isList.bind(null, opts),
|
93 | isSelectionInList: isSelectionInList.bind(null, opts)
|
94 | },
|
95 |
|
96 | changes: {
|
97 | decreaseItemDepth: bindChange(decreaseItemDepth),
|
98 | increaseItemDepth: bindChange(increaseItemDepth),
|
99 | splitListItem: bindChange(splitListItem),
|
100 | unwrapList: bindChange(unwrapList),
|
101 | wrapInList: wrapInList.bind(null, opts)
|
102 | }
|
103 | };
|
104 | }
|
105 |
|
106 | module.exports = EditList; |
\ | No newline at end of file |