UNPKG

3.42 kBJavaScriptView Raw
1'use strict';
2
3function _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
5var Options = require('./options');
6var onEnter = require('./onEnter');
7var onTab = require('./onTab');
8var onBackspace = require('./onBackspace');
9var makeSchema = require('./makeSchema');
10
11var wrapInList = require('./changes/wrapInList');
12var unwrapList = require('./changes/unwrapList');
13var splitListItem = require('./changes/splitListItem');
14var increaseItemDepth = require('./changes/increaseItemDepth');
15var decreaseItemDepth = require('./changes/decreaseItemDepth');
16
17var getItemDepth = require('./getItemDepth');
18var isList = require('./isList');
19var isSelectionInList = require('./isSelectionInList');
20var getCurrentItem = require('./getCurrentItem');
21var getCurrentList = require('./getCurrentList');
22var getItemsAtRange = require('./getItemsAtRange');
23var getPreviousItem = require('./getPreviousItem');
24
25var KEY_ENTER = 'Enter';
26var KEY_TAB = 'Tab';
27var KEY_BACKSPACE = 'Backspace';
28
29/**
30 * A Slate plugin to handle keyboard events in lists.
31 * @param {Options} [opts] Options for the plugin
32 * @return {Object}
33 */
34
35function EditList() {
36 var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
37
38 opts = new Options(opts);
39
40 /**
41 * Bind a change to be only applied in list
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 * User is pressing a key in the editor
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
106module.exports = EditList;
\No newline at end of file