1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.createContentBlock = createContentBlock;
|
7 | exports.default = replaceTextRegex;
|
8 |
|
9 | var _draftJs = require('draft-js');
|
10 |
|
11 | var _rules = require('./rules');
|
12 |
|
13 | 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); } }
|
14 |
|
15 | var optionRules = {
|
16 | extraSpaces: [{ reg: new RegExp(/\s+/g), shift: ' ' },
|
17 | { reg: new RegExp(/(^\s*)|(\s*)$/g), shift: '' }],
|
18 | spacesAfterPunctuationMarks: [{ reg: new RegExp(/(,|\.)(\S)/g), shift: '$1 $2' }]
|
19 | };
|
20 |
|
21 | function createSelectionState(key) {
|
22 | var start = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
23 | var end = arguments[2];
|
24 |
|
25 | return new _draftJs.SelectionState({
|
26 | anchorKey: key,
|
27 | anchorOffset: start,
|
28 | focusKey: key,
|
29 | focusOffset: end || start
|
30 | });
|
31 | }
|
32 |
|
33 | function createContentBlock(block, options) {
|
34 | var _ref = options || {},
|
35 | text = _ref.text,
|
36 | key = _ref.key,
|
37 | type = _ref.type,
|
38 | characterList = _ref.characterList;
|
39 |
|
40 | return new _draftJs.ContentBlock({
|
41 | text: text || block.getText(),
|
42 | key: key || block.getKey(),
|
43 | type: type || block.getType(),
|
44 | characterList: characterList || block.getCharacterList()
|
45 | });
|
46 | }
|
47 |
|
48 | function prepareOptionableRules(options) {
|
49 | var acc = [];
|
50 | Object.keys(options).forEach(function (key) {
|
51 | acc.push.apply(acc, _toConsumableArray(optionRules[key]));
|
52 | return acc;
|
53 | });
|
54 | if (acc.length > 0) return acc;
|
55 | return [];
|
56 | }
|
57 |
|
58 | function replaceTextRegex(editorState) {
|
59 | var rulesArray = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _rules.typoRules;
|
60 | var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : { extraSpaces: true };
|
61 |
|
62 | var CS = editorState.getCurrentContent();
|
63 | var CSMap = CS.getBlockMap();
|
64 | var CSText = CS.getPlainText();
|
65 |
|
66 | var optionableRules = [];
|
67 | if (options) {
|
68 | var preparedRules = prepareOptionableRules(options);
|
69 | optionableRules.push.apply(optionableRules, _toConsumableArray(preparedRules));
|
70 | }
|
71 | var rules = options ? rulesArray.concat(optionableRules) : rulesArray;
|
72 |
|
73 | var rule = void 0;
|
74 | var match = rules.some(function (typoRule) {
|
75 | rule = typoRule;
|
76 | return typoRule && typoRule.reg.exec(CSText);
|
77 | });
|
78 |
|
79 | if (match) {
|
80 | var _ref2 = rule || {},
|
81 | _reg = _ref2.reg,
|
82 | _shift = _ref2.shift;
|
83 |
|
84 | var SS = editorState.getSelection();
|
85 | var _key = SS.getAnchorKey();
|
86 | var anchorOffset = SS.getAnchorOffset();
|
87 | var currentContentBlock = CS.getBlockForKey(_key);
|
88 | var text = currentContentBlock.getText().replace(_reg, _shift);
|
89 | var newBlock = createContentBlock(currentContentBlock, {
|
90 | text: text,
|
91 | key: _key
|
92 | });
|
93 |
|
94 | var diffInLengths = currentContentBlock.getLength() - newBlock.getLength();
|
95 | var newSS = createSelectionState(_key, anchorOffset - diffInLengths);
|
96 |
|
97 | var newBlockMap = CSMap.set(_key, newBlock);
|
98 | var newCS = new _draftJs.ContentState({ blockMap: newBlockMap });
|
99 | var ES = _draftJs.EditorState.createWithContent(newCS);
|
100 | var forceES = _draftJs.EditorState.forceSelection(ES, newSS);
|
101 | var forceESText = forceES.getCurrentContent().getPlainText();
|
102 |
|
103 | if (forceESText !== CSText) {
|
104 | return forceES;
|
105 | }
|
106 | return editorState;
|
107 | }
|
108 | return editorState;
|
109 | } |
\ | No newline at end of file |