UNPKG

1.23 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7require('slate');
8
9var _immutable = require('immutable');
10
11// Return the index of the first character that differs between both string, or
12// the smallest string length otherwise.
13function firstDifferentCharacter(a, b) {
14 if (a.length > b.length) {
15 return firstDifferentCharacter(b, a);
16 }
17
18 var index = (0, _immutable.Range)(0, a.length).find(function (i) {
19 return a[i] !== b[i];
20 });
21
22 return index == null ? a.length : index;
23}
24
25/**
26 * Dedent all lines in selection
27 */
28function dedentLines(opts, change,
29// Indent to remove
30indent) {
31 var value = change.value;
32 var document = value.document,
33 selection = value.selection;
34
35 var lines = document.getBlocksAtRange(selection).filter(function (node) {
36 return node.type === opts.lineType;
37 });
38
39 return lines.reduce(function (c, line) {
40 // Remove a level of indent from the start of line
41 var textNode = line.nodes.first();
42 var lengthToRemove = firstDifferentCharacter(textNode.text, indent);
43 return c.removeTextByKey(textNode.key, 0, lengthToRemove);
44 }, change);
45}
46
47exports.default = dedentLines;
\No newline at end of file