@studysync/draft-js-modifiers 0.3.0 → 0.3.1

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/README.md CHANGED
@@ -34,6 +34,18 @@ import { insertText } from 'draft-js-modifiers'
34
34
 
35
35
  ## Methods
36
36
 
37
+ ### `addBlock`
38
+
39
+ ```js
40
+ addBlock(
41
+ editorState: EditorState,
42
+ selection,
43
+ type: string,
44
+ data: { [id: string]: any },
45
+ entityType: string,
46
+ text?: ?string = ' '
47
+ ```
48
+
37
49
  ### `adjustBlockDepth`
38
50
 
39
51
  ```js
package/addBlock.js ADDED
@@ -0,0 +1,75 @@
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
+ var _immutable = require("immutable");
11
+
12
+ var addBlock = function addBlock(editorState, selection, type, data, entityType) {
13
+ var text = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : ' ';
14
+ var currentContentState = editorState.getCurrentContent();
15
+ var currentSelectionState = selection; // in case text is selected it is removed and then the block is appended
16
+
17
+ var afterRemovalContentState = _draftJs.Modifier.removeRange(currentContentState, currentSelectionState, 'backward'); // deciding on the position to split the text
18
+
19
+
20
+ var targetSelection = afterRemovalContentState.getSelectionAfter();
21
+ var blockKeyForTarget = targetSelection.get('focusKey');
22
+ var block = currentContentState.getBlockForKey(blockKeyForTarget);
23
+ var insertionTargetSelection;
24
+ var insertionTargetBlock; // In case there are no characters or entity or the selection is at the start it
25
+ // is safe to insert the block in the current block.
26
+ // Otherwise a new block is created (the block is always its own block)
27
+
28
+ var isEmptyBlock = block.getLength() === 0 && block.getEntityAt(0) === null;
29
+ var selectedFromStart = currentSelectionState.getStartOffset() === 0;
30
+
31
+ if (isEmptyBlock || selectedFromStart) {
32
+ insertionTargetSelection = targetSelection;
33
+ insertionTargetBlock = afterRemovalContentState;
34
+ } else {
35
+ // the only way to insert a new seems to be by splitting an existing in to two
36
+ insertionTargetBlock = _draftJs.Modifier.splitBlock(afterRemovalContentState, targetSelection); // the position to insert our blocks
37
+
38
+ insertionTargetSelection = insertionTargetBlock.getSelectionAfter();
39
+ } // TODO not sure why we need it …
40
+
41
+
42
+ var newContentStateAfterSplit = _draftJs.Modifier.setBlockType(insertionTargetBlock, insertionTargetSelection, type); // creating a new ContentBlock including the entity with data
43
+ // Entity will be created with a specific type, if defined, else will fall back to the ContentBlock type
44
+
45
+
46
+ var contentStateWithEntity = newContentStateAfterSplit.createEntity(entityType || type, 'IMMUTABLE', data);
47
+ var entityKey = contentStateWithEntity.getLastCreatedEntityKey();
48
+
49
+ var charData = _draftJs.CharacterMetadata.create({
50
+ entity: entityKey
51
+ });
52
+
53
+ var fragmentArray = [new _draftJs.ContentBlock({
54
+ key: (0, _draftJs.genKey)(),
55
+ type: type,
56
+ text: text,
57
+ characterList: (0, _immutable.List)((0, _immutable.Repeat)(charData, text.length || 1)) // eslint-disable-line new-cap
58
+
59
+ }), // new contentblock so we can continue wrting right away after inserting the block
60
+ new _draftJs.ContentBlock({
61
+ key: (0, _draftJs.genKey)(),
62
+ type: 'unstyled',
63
+ text: '',
64
+ characterList: (0, _immutable.List)() // eslint-disable-line new-cap
65
+
66
+ })]; // create fragment containing the two content blocks
67
+
68
+ var fragment = _draftJs.BlockMapBuilder.createFromArray(fragmentArray); // replace the contentblock we reserved for our insert
69
+
70
+
71
+ return _draftJs.Modifier.replaceWithFragment(newContentStateAfterSplit, insertionTargetSelection, fragment);
72
+ };
73
+
74
+ var _default = addBlock;
75
+ exports.default = _default;
package/es/addBlock.js ADDED
@@ -0,0 +1,65 @@
1
+ import { Modifier } from "draft-js";
2
+ import { ContentBlock } from "draft-js";
3
+ import { genKey } from "draft-js";
4
+ import { CharacterMetadata } from "draft-js";
5
+ import { BlockMapBuilder } from "draft-js";
6
+ import { List, Repeat } from 'immutable';
7
+
8
+ var addBlock = function addBlock(editorState, selection, type, data, entityType) {
9
+ var text = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : ' ';
10
+ var currentContentState = editorState.getCurrentContent();
11
+ var currentSelectionState = selection; // in case text is selected it is removed and then the block is appended
12
+
13
+ var afterRemovalContentState = Modifier.removeRange(currentContentState, currentSelectionState, 'backward'); // deciding on the position to split the text
14
+
15
+ var targetSelection = afterRemovalContentState.getSelectionAfter();
16
+ var blockKeyForTarget = targetSelection.get('focusKey');
17
+ var block = currentContentState.getBlockForKey(blockKeyForTarget);
18
+ var insertionTargetSelection;
19
+ var insertionTargetBlock; // In case there are no characters or entity or the selection is at the start it
20
+ // is safe to insert the block in the current block.
21
+ // Otherwise a new block is created (the block is always its own block)
22
+
23
+ var isEmptyBlock = block.getLength() === 0 && block.getEntityAt(0) === null;
24
+ var selectedFromStart = currentSelectionState.getStartOffset() === 0;
25
+
26
+ if (isEmptyBlock || selectedFromStart) {
27
+ insertionTargetSelection = targetSelection;
28
+ insertionTargetBlock = afterRemovalContentState;
29
+ } else {
30
+ // the only way to insert a new seems to be by splitting an existing in to two
31
+ insertionTargetBlock = Modifier.splitBlock(afterRemovalContentState, targetSelection); // the position to insert our blocks
32
+
33
+ insertionTargetSelection = insertionTargetBlock.getSelectionAfter();
34
+ } // TODO not sure why we need it …
35
+
36
+
37
+ var newContentStateAfterSplit = Modifier.setBlockType(insertionTargetBlock, insertionTargetSelection, type); // creating a new ContentBlock including the entity with data
38
+ // Entity will be created with a specific type, if defined, else will fall back to the ContentBlock type
39
+
40
+ var contentStateWithEntity = newContentStateAfterSplit.createEntity(entityType || type, 'IMMUTABLE', data);
41
+ var entityKey = contentStateWithEntity.getLastCreatedEntityKey();
42
+ var charData = CharacterMetadata.create({
43
+ entity: entityKey
44
+ });
45
+ var fragmentArray = [new ContentBlock({
46
+ key: genKey(),
47
+ type: type,
48
+ text: text,
49
+ characterList: List(Repeat(charData, text.length || 1)) // eslint-disable-line new-cap
50
+
51
+ }), // new contentblock so we can continue wrting right away after inserting the block
52
+ new ContentBlock({
53
+ key: genKey(),
54
+ type: 'unstyled',
55
+ text: '',
56
+ characterList: List() // eslint-disable-line new-cap
57
+
58
+ })]; // create fragment containing the two content blocks
59
+
60
+ var fragment = BlockMapBuilder.createFromArray(fragmentArray); // replace the contentblock we reserved for our insert
61
+
62
+ return Modifier.replaceWithFragment(newContentStateAfterSplit, insertionTargetSelection, fragment);
63
+ };
64
+
65
+ export default addBlock;
package/es/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ export { default as addBlock } from './addBlock';
1
2
  export { default as adjustBlockDepth } from './adjustBlockDepth';
2
3
  export { default as insertAtomicBlock } from './insertAtomicBlock';
3
4
  export { default as insertEmptyBlock } from './insertEmptyBlock';
package/index.js CHANGED
@@ -3,6 +3,12 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ Object.defineProperty(exports, "addBlock", {
7
+ enumerable: true,
8
+ get: function get() {
9
+ return _addBlock.default;
10
+ }
11
+ });
6
12
  Object.defineProperty(exports, "adjustBlockDepth", {
7
13
  enumerable: true,
8
14
  get: function get() {
@@ -100,6 +106,8 @@ Object.defineProperty(exports, "toggleInlineStyle", {
100
106
  }
101
107
  });
102
108
 
109
+ var _addBlock = _interopRequireDefault(require("./addBlock"));
110
+
103
111
  var _adjustBlockDepth = _interopRequireDefault(require("./adjustBlockDepth"));
104
112
 
105
113
  var _insertAtomicBlock = _interopRequireDefault(require("./insertAtomicBlock"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@studysync/draft-js-modifiers",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "Modular state modifiers for Draft.js",
5
5
  "main": "index.js",
6
6
  "module": "es/index.js",