@studysync/draft-js-modifiers 0.3.1 → 0.3.2

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
@@ -52,6 +52,12 @@ addBlock(
52
52
  adjustBlockDepth(editorState: EditorState, adjustment: number, maxDepth: number)
53
53
  ```
54
54
 
55
+ ### `getCurrentBlock`
56
+
57
+ ```js
58
+ getCurrentBlock(editorState): EditorState
59
+ ```
60
+
55
61
  ### `insertAtomicBlock`
56
62
 
57
63
  ```js
@@ -117,6 +123,18 @@ modifyBlock(editorState: EditorState, blockData: ContentBlock)
117
123
  modifyBlockByKey(editorState: EditorState, blockKey: string, blockData: ContentBlock)
118
124
  ```
119
125
 
126
+ ### `moveCaretAfterBlock`
127
+
128
+ ```js
129
+ moveCaretAfterBlock(editorState: EditorState, block: ContentBlock)
130
+ ```
131
+
132
+ ### `removeBlock`
133
+
134
+ ```js
135
+ removeBlock(contentState, blockKey)
136
+ ```
137
+
120
138
  ### `removeBlockStyle`
121
139
 
122
140
  ```js
@@ -135,6 +153,18 @@ removeInlineStyles(editorState: EditorState, inlineStyles: Array<string> = [])
135
153
  resetBlock(editorState: EditorState, block: ContentBlock)
136
154
  ```
137
155
 
156
+ ### `selectBlockByKey`
157
+
158
+ ```js
159
+ selectBlockByKey(editorState: EditorState, key: string)
160
+ ```
161
+
162
+ ### `toggleBlockStyle`
163
+
164
+ ```js
165
+ toggleBlockStyle(editorState, { type: blockStyle, data })
166
+ ```
167
+
138
168
  ### `toggleBlockType`
139
169
 
140
170
  ```js
package/addBlock.js CHANGED
@@ -9,6 +9,7 @@ var _draftJs = require("draft-js");
9
9
 
10
10
  var _immutable = require("immutable");
11
11
 
12
+ // from draft-js-plugins/draft-js-plugins/packages/drag-n-drop/src/modifiers/addBlock.ts
12
13
  var addBlock = function addBlock(editorState, selection, type, data, entityType) {
13
14
  var text = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : ' ';
14
15
  var currentContentState = editorState.getCurrentContent();
package/es/addBlock.js CHANGED
@@ -3,7 +3,7 @@ import { ContentBlock } from "draft-js";
3
3
  import { genKey } from "draft-js";
4
4
  import { CharacterMetadata } from "draft-js";
5
5
  import { BlockMapBuilder } from "draft-js";
6
- import { List, Repeat } from 'immutable';
6
+ import { List, Repeat } from 'immutable'; // from draft-js-plugins/draft-js-plugins/packages/drag-n-drop/src/modifiers/addBlock.ts
7
7
 
8
8
  var addBlock = function addBlock(editorState, selection, type, data, entityType) {
9
9
  var text = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : ' ';
@@ -0,0 +1,6 @@
1
+ var getCurrentBlock = function getCurrentBlock(editorState) {
2
+ var currentKey = editorState.getSelection().getAnchorKey();
3
+ return editorState.getCurrentContent().getBlockForKey(currentKey);
4
+ };
5
+
6
+ export default getCurrentBlock;
package/es/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export { default as addBlock } from './addBlock';
2
2
  export { default as adjustBlockDepth } from './adjustBlockDepth';
3
+ export { default as getCurrentBlock } from './getCurrentBlock';
3
4
  export { default as insertAtomicBlock } from './insertAtomicBlock';
4
5
  export { default as insertEmptyBlock } from './insertEmptyBlock';
5
6
  export { default as insertNewBlock } from './insertNewBlock';
@@ -9,9 +10,14 @@ export { default as mergeBlockDataByKey } from './mergeBlockDataByKey';
9
10
  export { default as mergeEntityData } from './mergeEntityData';
10
11
  export { default as modifyBlock } from './modifyBlock';
11
12
  export { default as modifyBlockByKey } from './modifyBlockByKey';
13
+ export { default as moveCaretAfterBlock } from './moveCaretAfterBlock';
14
+ export { default as removeBlock } from './removeBlock';
12
15
  export { default as removeBlockStyle } from './removeBlockStyle';
13
16
  export { default as removeInlineStyles } from './removeInlineStyles';
14
17
  export { default as resetBlock } from './resetBlock';
18
+ export { default as selectBlockByKey } from './selectBlockByKey';
19
+ export { default as toggleBlockStyle } from './toggleBlockStyle';
15
20
  export { default as toggleBlockType } from './toggleBlockType';
16
21
  export { default as toggleEntity } from './toggleEntity';
17
- export { default as toggleInlineStyle } from './toggleInlineStyle';
22
+ export { default as toggleInlineStyle } from './toggleInlineStyle';
23
+ export var DRAFTJS_BLOCK_KEY = 'DRAFTJS_BLOCK_KEY';
@@ -0,0 +1,23 @@
1
+ import { EditorState } from "draft-js";
2
+ import { SelectionState } from "draft-js";
3
+
4
+ var moveCaretAfterBlock = function moveCaretAfterBlock(editorState, block) {
5
+ var content = editorState.getCurrentContent();
6
+ var nextBlock = content.getBlockAfter(block.getKey());
7
+ var key = nextBlock ? nextBlock.getKey() : undefined;
8
+
9
+ if (key) {
10
+ var selectionState = SelectionState.createEmpty();
11
+ var newSelectionState = selectionState.merge({
12
+ anchorKey: key,
13
+ anchorOffset: 0,
14
+ focusKey: key,
15
+ focusOffset: 0
16
+ });
17
+ return EditorState.forceSelection(editorState, newSelectionState);
18
+ }
19
+
20
+ return editorState;
21
+ };
22
+
23
+ export default moveCaretAfterBlock;
@@ -0,0 +1,32 @@
1
+ import { SelectionState } from "draft-js";
2
+ import { Modifier } from "draft-js"; // from draft-js-plugins/draft-js-plugins/packages/drag-n-drop/src/modifiers/addBlock.ts
3
+
4
+ var removeBlock = function removeBlock(contentState, blockKey) {
5
+ var afterKey = contentState.getKeyAfter(blockKey);
6
+ var afterBlock = contentState.getBlockForKey(afterKey);
7
+ var targetRange; // Only if the following block the last with no text then the whole block
8
+ // should be removed. Otherwise the block should be reduced to an unstyled block
9
+ // without any characters.
10
+
11
+ if (afterBlock && afterBlock.getType() === 'unstyled' && afterBlock.getLength() === 0 && afterBlock === contentState.getBlockMap().last()) {
12
+ targetRange = new SelectionState({
13
+ anchorKey: blockKey,
14
+ anchorOffset: 0,
15
+ focusKey: afterKey,
16
+ focusOffset: 0
17
+ });
18
+ } else {
19
+ targetRange = new SelectionState({
20
+ anchorKey: blockKey,
21
+ anchorOffset: 0,
22
+ focusKey: blockKey,
23
+ focusOffset: 1
24
+ });
25
+ } // change the blocktype and remove the characterList entry with the block
26
+
27
+
28
+ var newContentState = Modifier.setBlockType(contentState, targetRange, 'unstyled');
29
+ return Modifier.removeRange(newContentState, targetRange, 'backward');
30
+ };
31
+
32
+ export default removeBlock;
@@ -0,0 +1,16 @@
1
+ import { SelectionState } from "draft-js";
2
+ import { EditorState } from "draft-js";
3
+
4
+ var selectBlockByKey = function selectBlockByKey(editorState, key) {
5
+ var currentBlock = editorState.getCurrentContent().getBlockForKey(key);
6
+ var selectionState = SelectionState.createEmpty();
7
+ var entireBlockSelectionState = selectionState.merge({
8
+ anchorKey: key,
9
+ anchorOffset: 0,
10
+ focusKey: key,
11
+ focusOffset: currentBlock.getText().length
12
+ });
13
+ return EditorState.forceSelection(editorState, entireBlockSelectionState);
14
+ };
15
+
16
+ export default selectBlockByKey;
@@ -0,0 +1,25 @@
1
+ import { RichUtils } from "draft-js";
2
+ import { EditorState } from "draft-js";
3
+ import { Modifier } from "draft-js";
4
+ import { Map } from 'immutable';
5
+
6
+ var toggleBlockStyle = function toggleBlockStyle(editorState, _ref) {
7
+ var blockStyle = _ref.type,
8
+ data = _ref.data;
9
+
10
+ /* istanbul ignore else */
11
+ if (data) {
12
+ var selection = editorState.getSelection();
13
+ var newContentState = Modifier.mergeBlockData(editorState.getCurrentContent(), selection, Map(data));
14
+ var newEditorState = EditorState.push(editorState, newContentState, 'change-block-data');
15
+ return RichUtils.toggleBlockType(newEditorState, blockStyle);
16
+ } // We currently always have data, but just in case we add new types in the
17
+ // future, the default action is to just blindly toggle it.
18
+
19
+ /* istanbul ignore next */
20
+
21
+
22
+ return RichUtils.toggleBlockType(editorState, blockStyle);
23
+ };
24
+
25
+ export default toggleBlockStyle;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var getCurrentBlock = function getCurrentBlock(editorState) {
9
+ var currentKey = editorState.getSelection().getAnchorKey();
10
+ return editorState.getCurrentContent().getBlockForKey(currentKey);
11
+ };
12
+
13
+ var _default = getCurrentBlock;
14
+ exports.default = _default;
package/index.js CHANGED
@@ -15,6 +15,12 @@ Object.defineProperty(exports, "adjustBlockDepth", {
15
15
  return _adjustBlockDepth.default;
16
16
  }
17
17
  });
18
+ Object.defineProperty(exports, "getCurrentBlock", {
19
+ enumerable: true,
20
+ get: function get() {
21
+ return _getCurrentBlock.default;
22
+ }
23
+ });
18
24
  Object.defineProperty(exports, "insertAtomicBlock", {
19
25
  enumerable: true,
20
26
  get: function get() {
@@ -69,6 +75,18 @@ Object.defineProperty(exports, "modifyBlockByKey", {
69
75
  return _modifyBlockByKey.default;
70
76
  }
71
77
  });
78
+ Object.defineProperty(exports, "moveCaretAfterBlock", {
79
+ enumerable: true,
80
+ get: function get() {
81
+ return _moveCaretAfterBlock.default;
82
+ }
83
+ });
84
+ Object.defineProperty(exports, "removeBlock", {
85
+ enumerable: true,
86
+ get: function get() {
87
+ return _removeBlock.default;
88
+ }
89
+ });
72
90
  Object.defineProperty(exports, "removeBlockStyle", {
73
91
  enumerable: true,
74
92
  get: function get() {
@@ -87,6 +105,18 @@ Object.defineProperty(exports, "resetBlock", {
87
105
  return _resetBlock.default;
88
106
  }
89
107
  });
108
+ Object.defineProperty(exports, "selectBlockByKey", {
109
+ enumerable: true,
110
+ get: function get() {
111
+ return _selectBlockByKey.default;
112
+ }
113
+ });
114
+ Object.defineProperty(exports, "toggleBlockStyle", {
115
+ enumerable: true,
116
+ get: function get() {
117
+ return _toggleBlockStyle.default;
118
+ }
119
+ });
90
120
  Object.defineProperty(exports, "toggleBlockType", {
91
121
  enumerable: true,
92
122
  get: function get() {
@@ -105,11 +135,14 @@ Object.defineProperty(exports, "toggleInlineStyle", {
105
135
  return _toggleInlineStyle.default;
106
136
  }
107
137
  });
138
+ exports.DRAFTJS_BLOCK_KEY = void 0;
108
139
 
109
140
  var _addBlock = _interopRequireDefault(require("./addBlock"));
110
141
 
111
142
  var _adjustBlockDepth = _interopRequireDefault(require("./adjustBlockDepth"));
112
143
 
144
+ var _getCurrentBlock = _interopRequireDefault(require("./getCurrentBlock"));
145
+
113
146
  var _insertAtomicBlock = _interopRequireDefault(require("./insertAtomicBlock"));
114
147
 
115
148
  var _insertEmptyBlock = _interopRequireDefault(require("./insertEmptyBlock"));
@@ -128,16 +161,27 @@ var _modifyBlock = _interopRequireDefault(require("./modifyBlock"));
128
161
 
129
162
  var _modifyBlockByKey = _interopRequireDefault(require("./modifyBlockByKey"));
130
163
 
164
+ var _moveCaretAfterBlock = _interopRequireDefault(require("./moveCaretAfterBlock"));
165
+
166
+ var _removeBlock = _interopRequireDefault(require("./removeBlock"));
167
+
131
168
  var _removeBlockStyle = _interopRequireDefault(require("./removeBlockStyle"));
132
169
 
133
170
  var _removeInlineStyles = _interopRequireDefault(require("./removeInlineStyles"));
134
171
 
135
172
  var _resetBlock = _interopRequireDefault(require("./resetBlock"));
136
173
 
174
+ var _selectBlockByKey = _interopRequireDefault(require("./selectBlockByKey"));
175
+
176
+ var _toggleBlockStyle = _interopRequireDefault(require("./toggleBlockStyle"));
177
+
137
178
  var _toggleBlockType = _interopRequireDefault(require("./toggleBlockType"));
138
179
 
139
180
  var _toggleEntity = _interopRequireDefault(require("./toggleEntity"));
140
181
 
141
182
  var _toggleInlineStyle = _interopRequireDefault(require("./toggleInlineStyle"));
142
183
 
143
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
184
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
185
+
186
+ var DRAFTJS_BLOCK_KEY = 'DRAFTJS_BLOCK_KEY';
187
+ exports.DRAFTJS_BLOCK_KEY = DRAFTJS_BLOCK_KEY;
@@ -0,0 +1,31 @@
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 moveCaretAfterBlock = function moveCaretAfterBlock(editorState, block) {
11
+ var content = editorState.getCurrentContent();
12
+ var nextBlock = content.getBlockAfter(block.getKey());
13
+ var key = nextBlock ? nextBlock.getKey() : undefined;
14
+
15
+ if (key) {
16
+ var selectionState = _draftJs.SelectionState.createEmpty();
17
+
18
+ var newSelectionState = selectionState.merge({
19
+ anchorKey: key,
20
+ anchorOffset: 0,
21
+ focusKey: key,
22
+ focusOffset: 0
23
+ });
24
+ return _draftJs.EditorState.forceSelection(editorState, newSelectionState);
25
+ }
26
+
27
+ return editorState;
28
+ };
29
+
30
+ var _default = moveCaretAfterBlock;
31
+ exports.default = _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@studysync/draft-js-modifiers",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "description": "Modular state modifiers for Draft.js",
5
5
  "main": "index.js",
6
6
  "module": "es/index.js",
package/removeBlock.js ADDED
@@ -0,0 +1,41 @@
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
+ // from draft-js-plugins/draft-js-plugins/packages/drag-n-drop/src/modifiers/addBlock.ts
11
+ var removeBlock = function removeBlock(contentState, blockKey) {
12
+ var afterKey = contentState.getKeyAfter(blockKey);
13
+ var afterBlock = contentState.getBlockForKey(afterKey);
14
+ var targetRange; // Only if the following block the last with no text then the whole block
15
+ // should be removed. Otherwise the block should be reduced to an unstyled block
16
+ // without any characters.
17
+
18
+ if (afterBlock && afterBlock.getType() === 'unstyled' && afterBlock.getLength() === 0 && afterBlock === contentState.getBlockMap().last()) {
19
+ targetRange = new _draftJs.SelectionState({
20
+ anchorKey: blockKey,
21
+ anchorOffset: 0,
22
+ focusKey: afterKey,
23
+ focusOffset: 0
24
+ });
25
+ } else {
26
+ targetRange = new _draftJs.SelectionState({
27
+ anchorKey: blockKey,
28
+ anchorOffset: 0,
29
+ focusKey: blockKey,
30
+ focusOffset: 1
31
+ });
32
+ } // change the blocktype and remove the characterList entry with the block
33
+
34
+
35
+ var newContentState = _draftJs.Modifier.setBlockType(contentState, targetRange, 'unstyled');
36
+
37
+ return _draftJs.Modifier.removeRange(newContentState, targetRange, 'backward');
38
+ };
39
+
40
+ var _default = removeBlock;
41
+ exports.default = _default;
@@ -0,0 +1,25 @@
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 selectBlockByKey = function selectBlockByKey(editorState, key) {
11
+ var currentBlock = editorState.getCurrentContent().getBlockForKey(key);
12
+
13
+ var selectionState = _draftJs.SelectionState.createEmpty();
14
+
15
+ var entireBlockSelectionState = selectionState.merge({
16
+ anchorKey: key,
17
+ anchorOffset: 0,
18
+ focusKey: key,
19
+ focusOffset: currentBlock.getText().length
20
+ });
21
+ return _draftJs.EditorState.forceSelection(editorState, entireBlockSelectionState);
22
+ };
23
+
24
+ var _default = selectBlockByKey;
25
+ exports.default = _default;
@@ -0,0 +1,35 @@
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 toggleBlockStyle = function toggleBlockStyle(editorState, _ref) {
13
+ var blockStyle = _ref.type,
14
+ data = _ref.data;
15
+
16
+ /* istanbul ignore else */
17
+ if (data) {
18
+ var selection = editorState.getSelection();
19
+
20
+ var newContentState = _draftJs.Modifier.mergeBlockData(editorState.getCurrentContent(), selection, (0, _immutable.Map)(data));
21
+
22
+ var newEditorState = _draftJs.EditorState.push(editorState, newContentState, 'change-block-data');
23
+
24
+ return _draftJs.RichUtils.toggleBlockType(newEditorState, blockStyle);
25
+ } // We currently always have data, but just in case we add new types in the
26
+ // future, the default action is to just blindly toggle it.
27
+
28
+ /* istanbul ignore next */
29
+
30
+
31
+ return _draftJs.RichUtils.toggleBlockType(editorState, blockStyle);
32
+ };
33
+
34
+ var _default = toggleBlockStyle;
35
+ exports.default = _default;