@studysync/draft-js-modifiers 0.4.6 → 0.4.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/getSimilarAdjacentBlocks.js +2 -1
- package/es/index.js +8 -0
- package/es/trimEditorState.js +45 -0
- package/getSimilarAdjacentBlocks.js +2 -1
- package/index.js +8 -0
- package/package.json +1 -1
- package/trimEditorState.js +45 -0
|
@@ -18,13 +18,14 @@ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToAr
|
|
|
18
18
|
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
19
19
|
|
|
20
20
|
var getSimilarAdjacentBlocks = function getSimilarAdjacentBlocks(editorState, block) {
|
|
21
|
+
var includeDepth = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
|
21
22
|
var blockType = block.getType();
|
|
22
23
|
var depth = block.getDepth();
|
|
23
24
|
var variant = block.getData().get('variant');
|
|
24
25
|
var contentState = editorState.getCurrentContent();
|
|
25
26
|
|
|
26
27
|
var matches = function matches(aBlock) {
|
|
27
|
-
return aBlock.getType() === blockType && aBlock.getDepth() === depth && aBlock.getData().get('variant') === variant;
|
|
28
|
+
return aBlock && aBlock.getType() === blockType && (!includeDepth || aBlock.getDepth() === depth) && aBlock.getData().get('variant') === variant;
|
|
28
29
|
};
|
|
29
30
|
|
|
30
31
|
var before = [];
|
package/es/index.js
CHANGED
|
@@ -226,6 +226,12 @@ Object.defineProperty(exports, "toggleInlineStyle", {
|
|
|
226
226
|
return _toggleInlineStyle["default"];
|
|
227
227
|
}
|
|
228
228
|
});
|
|
229
|
+
Object.defineProperty(exports, "trimEditorState", {
|
|
230
|
+
enumerable: true,
|
|
231
|
+
get: function get() {
|
|
232
|
+
return _trimEditorState["default"];
|
|
233
|
+
}
|
|
234
|
+
});
|
|
229
235
|
|
|
230
236
|
var _addBlock = _interopRequireDefault(require("./addBlock"));
|
|
231
237
|
|
|
@@ -301,6 +307,8 @@ var _toggleEntity = _interopRequireDefault(require("./toggleEntity"));
|
|
|
301
307
|
|
|
302
308
|
var _toggleInlineStyle = _interopRequireDefault(require("./toggleInlineStyle"));
|
|
303
309
|
|
|
310
|
+
var _trimEditorState = _interopRequireDefault(require("./trimEditorState"));
|
|
311
|
+
|
|
304
312
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
305
313
|
|
|
306
314
|
var DRAFTJS_BLOCK_KEY = 'DRAFTJS_BLOCK_KEY';
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports["default"] = void 0;
|
|
7
|
+
|
|
8
|
+
var _draftJs = require("draft-js");
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Taken from:
|
|
12
|
+
* https://stackoverflow.com/questions/46802855/draft-js-how-to-trim-contents
|
|
13
|
+
*
|
|
14
|
+
* This *might* mess up entities, but so far I haven't seen any issues
|
|
15
|
+
*/
|
|
16
|
+
var trimEditorState = function trimEditorState(currentEditorState) {
|
|
17
|
+
var currentContent = currentEditorState.getCurrentContent();
|
|
18
|
+
var newContent = currentContent.getBlockMap().reduce(function (accumulator, block) {
|
|
19
|
+
var key = block.getKey();
|
|
20
|
+
var text = block.getText();
|
|
21
|
+
var trimmedLeft = text.trimLeft();
|
|
22
|
+
var trimmedRight = text.trimRight();
|
|
23
|
+
var offset = text.length - trimmedLeft.length;
|
|
24
|
+
var textToReplaceLeft = new _draftJs.SelectionState({
|
|
25
|
+
anchorKey: key,
|
|
26
|
+
focusKey: key,
|
|
27
|
+
anchorOffset: 0,
|
|
28
|
+
focusOffset: offset
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
var leftTrimmedContent = _draftJs.Modifier.replaceText(accumulator, textToReplaceLeft, '');
|
|
32
|
+
|
|
33
|
+
var textToReplaceRight = new _draftJs.SelectionState({
|
|
34
|
+
anchorKey: key,
|
|
35
|
+
focusKey: key,
|
|
36
|
+
anchorOffset: trimmedRight.length - offset,
|
|
37
|
+
focusOffset: text.length - offset
|
|
38
|
+
});
|
|
39
|
+
return _draftJs.Modifier.replaceText(leftTrimmedContent, textToReplaceRight, '');
|
|
40
|
+
}, currentContent);
|
|
41
|
+
return _draftJs.EditorState.push(currentEditorState, newContent, 'remove-range');
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
var _default = trimEditorState;
|
|
45
|
+
exports["default"] = _default;
|
|
@@ -18,13 +18,14 @@ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToAr
|
|
|
18
18
|
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
19
19
|
|
|
20
20
|
var getSimilarAdjacentBlocks = function getSimilarAdjacentBlocks(editorState, block) {
|
|
21
|
+
var includeDepth = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
|
21
22
|
var blockType = block.getType();
|
|
22
23
|
var depth = block.getDepth();
|
|
23
24
|
var variant = block.getData().get('variant');
|
|
24
25
|
var contentState = editorState.getCurrentContent();
|
|
25
26
|
|
|
26
27
|
var matches = function matches(aBlock) {
|
|
27
|
-
return aBlock.getType() === blockType && aBlock.getDepth() === depth && aBlock.getData().get('variant') === variant;
|
|
28
|
+
return aBlock && aBlock.getType() === blockType && (!includeDepth || aBlock.getDepth() === depth) && aBlock.getData().get('variant') === variant;
|
|
28
29
|
};
|
|
29
30
|
|
|
30
31
|
var before = [];
|
package/index.js
CHANGED
|
@@ -226,6 +226,12 @@ Object.defineProperty(exports, "toggleInlineStyle", {
|
|
|
226
226
|
return _toggleInlineStyle["default"];
|
|
227
227
|
}
|
|
228
228
|
});
|
|
229
|
+
Object.defineProperty(exports, "trimEditorState", {
|
|
230
|
+
enumerable: true,
|
|
231
|
+
get: function get() {
|
|
232
|
+
return _trimEditorState["default"];
|
|
233
|
+
}
|
|
234
|
+
});
|
|
229
235
|
|
|
230
236
|
var _addBlock = _interopRequireDefault(require("./addBlock"));
|
|
231
237
|
|
|
@@ -301,6 +307,8 @@ var _toggleEntity = _interopRequireDefault(require("./toggleEntity"));
|
|
|
301
307
|
|
|
302
308
|
var _toggleInlineStyle = _interopRequireDefault(require("./toggleInlineStyle"));
|
|
303
309
|
|
|
310
|
+
var _trimEditorState = _interopRequireDefault(require("./trimEditorState"));
|
|
311
|
+
|
|
304
312
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
305
313
|
|
|
306
314
|
var DRAFTJS_BLOCK_KEY = 'DRAFTJS_BLOCK_KEY';
|
package/package.json
CHANGED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports["default"] = void 0;
|
|
7
|
+
|
|
8
|
+
var _draftJs = require("draft-js");
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Taken from:
|
|
12
|
+
* https://stackoverflow.com/questions/46802855/draft-js-how-to-trim-contents
|
|
13
|
+
*
|
|
14
|
+
* This *might* mess up entities, but so far I haven't seen any issues
|
|
15
|
+
*/
|
|
16
|
+
var trimEditorState = function trimEditorState(currentEditorState) {
|
|
17
|
+
var currentContent = currentEditorState.getCurrentContent();
|
|
18
|
+
var newContent = currentContent.getBlockMap().reduce(function (accumulator, block) {
|
|
19
|
+
var key = block.getKey();
|
|
20
|
+
var text = block.getText();
|
|
21
|
+
var trimmedLeft = text.trimLeft();
|
|
22
|
+
var trimmedRight = text.trimRight();
|
|
23
|
+
var offset = text.length - trimmedLeft.length;
|
|
24
|
+
var textToReplaceLeft = new _draftJs.SelectionState({
|
|
25
|
+
anchorKey: key,
|
|
26
|
+
focusKey: key,
|
|
27
|
+
anchorOffset: 0,
|
|
28
|
+
focusOffset: offset
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
var leftTrimmedContent = _draftJs.Modifier.replaceText(accumulator, textToReplaceLeft, '');
|
|
32
|
+
|
|
33
|
+
var textToReplaceRight = new _draftJs.SelectionState({
|
|
34
|
+
anchorKey: key,
|
|
35
|
+
focusKey: key,
|
|
36
|
+
anchorOffset: trimmedRight.length - offset,
|
|
37
|
+
focusOffset: text.length - offset
|
|
38
|
+
});
|
|
39
|
+
return _draftJs.Modifier.replaceText(leftTrimmedContent, textToReplaceRight, '');
|
|
40
|
+
}, currentContent);
|
|
41
|
+
return _draftJs.EditorState.push(currentEditorState, newContent, 'remove-range');
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
var _default = trimEditorState;
|
|
45
|
+
exports["default"] = _default;
|