1 | 'use strict';
|
2 |
|
3 | var endsWith = require('ends-with');
|
4 |
|
5 | var getCurrentIndent = require('./getCurrentIndent');
|
6 | var getCurrentCode = require('./getCurrentCode');
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | function onBackspace(event, data, change, opts) {
|
13 | var state = change.state;
|
14 |
|
15 | if (state.isExpanded) {
|
16 | return;
|
17 | }
|
18 |
|
19 | var startOffset = state.startOffset,
|
20 | startText = state.startText;
|
21 |
|
22 |
|
23 | var currentLine = state.startBlock;
|
24 |
|
25 |
|
26 | var indent = getCurrentIndent(opts, state);
|
27 | var beforeSelection = currentLine.text.slice(0, startOffset);
|
28 |
|
29 |
|
30 | if (endsWith(beforeSelection, indent)) {
|
31 |
|
32 | event.preventDefault();
|
33 |
|
34 | return change.deleteBackward(indent.length).focus();
|
35 | }
|
36 |
|
37 |
|
38 | else if (opts.exitBlockType) {
|
39 | var currentCode = getCurrentCode(opts, state);
|
40 | var isStartOfCode = startOffset === 0 && currentCode.getFirstText() === startText;
|
41 |
|
42 | var isEmpty = currentCode.nodes.size === 1 && currentLine.text.length === 0;
|
43 |
|
44 | if (isStartOfCode && isEmpty) {
|
45 |
|
46 | event.preventDefault();
|
47 |
|
48 | return change.setBlock(opts.exitBlockType).unwrapNodeByKey(currentLine.key);
|
49 | }
|
50 | }
|
51 | }
|
52 |
|
53 | module.exports = onBackspace; |
\ | No newline at end of file |