@studysync/draft-js-modifiers 0.3.0
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 +167 -0
- package/adjustBlockDepth.js +20 -0
- package/es/adjustBlockDepth.js +9 -0
- package/es/index.js +16 -0
- package/es/insertAtomicBlock.js +9 -0
- package/es/insertEmptyBlock.js +8 -0
- package/es/insertNewBlock.js +45 -0
- package/es/insertText.js +11 -0
- package/es/mergeBlockData.js +11 -0
- package/es/mergeBlockDataByKey.js +14 -0
- package/es/mergeEntityData.js +8 -0
- package/es/modifyBlock.js +14 -0
- package/es/modifyBlockByKey.js +15 -0
- package/es/removeBlockStyle.js +13 -0
- package/es/removeInlineStyles.js +32 -0
- package/es/resetBlock.js +21 -0
- package/es/toggleBlockType.js +7 -0
- package/es/toggleEntity.js +7 -0
- package/es/toggleInlineStyle.js +7 -0
- package/es/utils/getCurrentBlock.js +6 -0
- package/index.js +135 -0
- package/insertAtomicBlock.js +17 -0
- package/insertEmptyBlock.js +18 -0
- package/insertNewBlock.js +59 -0
- package/insertText.js +21 -0
- package/mergeBlockData.js +22 -0
- package/mergeBlockDataByKey.js +22 -0
- package/mergeEntityData.js +16 -0
- package/modifyBlock.js +25 -0
- package/modifyBlockByKey.js +23 -0
- package/package.json +67 -0
- package/removeBlockStyle.js +21 -0
- package/removeInlineStyles.js +43 -0
- package/resetBlock.js +33 -0
- package/toggleBlockType.js +15 -0
- package/toggleEntity.js +15 -0
- package/toggleInlineStyle.js +15 -0
- package/utils/getCurrentBlock.js +14 -0
package/README.md
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
# draft-js-modifiers
|
|
2
|
+
|
|
3
|
+
[![CircleCI][circleci-image]][circleci-url]
|
|
4
|
+
[![Coverage Status][coveralls-image]][coveralls-url]
|
|
5
|
+
[![npm version][npm-image]][npm-url]
|
|
6
|
+
[![License][license-image]][license-url]
|
|
7
|
+
|
|
8
|
+
Modular state modifiers for [Draft.js](https://draftjs.org/)
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
yarn add draft-js-modifiers
|
|
12
|
+
|
|
13
|
+
# or
|
|
14
|
+
|
|
15
|
+
npm i draft-js-modifiers
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
import * as Modifiers from 'draft-js-modifiers'
|
|
22
|
+
|
|
23
|
+
const newEditorState = Modifiers.mergeBlockData(currentEditorState, { foo: 1 })
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### Moduler importing
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
import adjustBlockDepth from 'draft-js-modifiers/adjustBlockDepth'
|
|
30
|
+
|
|
31
|
+
// Support Tree Shaking for webpack, rollup.js
|
|
32
|
+
import { insertText } from 'draft-js-modifiers'
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Methods
|
|
36
|
+
|
|
37
|
+
### `adjustBlockDepth`
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
adjustBlockDepth(editorState: EditorState, adjustment: number, maxDepth: number)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### `insertAtomicBlock`
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
insertAtomicBlock(
|
|
47
|
+
editorState: EditorState,
|
|
48
|
+
entityType: string,
|
|
49
|
+
mutability: 'IMMUTABLE' | 'MUTABLE' | 'SEGMENTED',
|
|
50
|
+
data?: { [id: string]: any },
|
|
51
|
+
character?: ?string = ' '
|
|
52
|
+
)
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### `insertEmptyBlock`
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
insertEmptyBlock(editorState: EditorState, blockType?: DraftBlockType = 'unstyled')
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### `insertNewBlock`
|
|
62
|
+
|
|
63
|
+
```js
|
|
64
|
+
insertNewBlock(
|
|
65
|
+
editorState: EditorState,
|
|
66
|
+
blockType?: DraftBlockType = 'unstyled',
|
|
67
|
+
text?: string = '',
|
|
68
|
+
data?: { [id: string]: any } = {}
|
|
69
|
+
)
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### `insertText`
|
|
73
|
+
|
|
74
|
+
```js
|
|
75
|
+
insertText(editorState: EditorState, text: string, entity?: ?string = null)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### `mergeBlockData`
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
mergeBlockData(editorState: EditorState, data: { [id: string]: any })
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### `mergeBlockDataByKey`
|
|
85
|
+
|
|
86
|
+
```js
|
|
87
|
+
mergeBlockDataByKey(editorState: EditorState, blockKey: string, data: { [id: string]: any })
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### `mergeEntityData`
|
|
91
|
+
|
|
92
|
+
```js
|
|
93
|
+
mergeEntityData(editorState: EditorState, entityKey: string, data: { [id: string]: any })
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### `modifyBlock`
|
|
97
|
+
|
|
98
|
+
```js
|
|
99
|
+
modifyBlock(editorState: EditorState, blockData: ContentBlock)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### `modifyBlockByKey`
|
|
103
|
+
|
|
104
|
+
```js
|
|
105
|
+
modifyBlockByKey(editorState: EditorState, blockKey: string, blockData: ContentBlock)
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### `removeBlockStyle`
|
|
109
|
+
|
|
110
|
+
```js
|
|
111
|
+
removeBlockStyle(editorState: EditorState)
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### `removeInlineStyles`
|
|
115
|
+
|
|
116
|
+
```js
|
|
117
|
+
removeInlineStyles(editorState: EditorState, inlineStyles: Array<string> = [])
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### `resetBlock`
|
|
121
|
+
|
|
122
|
+
```js
|
|
123
|
+
resetBlock(editorState: EditorState, block: ContentBlock)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### `toggleBlockType`
|
|
127
|
+
|
|
128
|
+
```js
|
|
129
|
+
toggleBlockType(editorState: EditorState, blockType: string)
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### `toggleEntity`
|
|
133
|
+
|
|
134
|
+
```js
|
|
135
|
+
toggleEntity(editorState: EditorState, entityKey: ?string)
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### `toggleInlineStyle`
|
|
139
|
+
|
|
140
|
+
```js
|
|
141
|
+
toggleInlineStyle(editorState: EditorState, inlineStyle: string)
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## How to add module
|
|
145
|
+
|
|
146
|
+
```bash
|
|
147
|
+
yarn run add -- moduleName
|
|
148
|
+
|
|
149
|
+
# or
|
|
150
|
+
|
|
151
|
+
npm run add -- moduleName
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## License
|
|
155
|
+
|
|
156
|
+
[MIT][license-url]
|
|
157
|
+
|
|
158
|
+
© sugarshin
|
|
159
|
+
|
|
160
|
+
[circleci-image]: https://circleci.com/gh/sugarshin/draft-js-modifiers/tree/master.svg?style=svg&circle-token=f80707ebb99977ec63649c41cb76202f05aa75e1
|
|
161
|
+
[circleci-url]: https://circleci.com/gh/sugarshin/draft-js-modifiers/tree/master
|
|
162
|
+
[coveralls-image]: https://coveralls.io/repos/github/sugarshin/draft-js-modifiers/badge.svg?branch=master
|
|
163
|
+
[coveralls-url]: https://coveralls.io/github/sugarshin/draft-js-modifiers?branch=master
|
|
164
|
+
[npm-image]: https://img.shields.io/npm/v/draft-js-modifiers.svg?style=flat-square
|
|
165
|
+
[npm-url]: https://www.npmjs.org/package/draft-js-modifiers
|
|
166
|
+
[license-image]: https://img.shields.io/:license-mit-blue.svg?style=flat-square
|
|
167
|
+
[license-url]: https://sugarshin.mit-license.org/
|
|
@@ -0,0 +1,20 @@
|
|
|
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 _adjustBlockDepthForContentState = _interopRequireDefault(require("draft-js/lib/adjustBlockDepthForContentState"));
|
|
11
|
+
|
|
12
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
|
+
|
|
14
|
+
var adjustBlockDepth = function adjustBlockDepth(editorState, adjustment, maxDepth) {
|
|
15
|
+
var content = (0, _adjustBlockDepthForContentState.default)(editorState.getCurrentContent(), editorState.getSelection(), adjustment, maxDepth);
|
|
16
|
+
return _draftJs.EditorState.push(editorState, content, 'adjust-depth');
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
var _default = adjustBlockDepth;
|
|
20
|
+
exports.default = _default;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { EditorState } from 'draft-js';
|
|
2
|
+
import adjustBlockDepthForContentState from 'draft-js/lib/adjustBlockDepthForContentState';
|
|
3
|
+
|
|
4
|
+
var adjustBlockDepth = function adjustBlockDepth(editorState, adjustment, maxDepth) {
|
|
5
|
+
var content = adjustBlockDepthForContentState(editorState.getCurrentContent(), editorState.getSelection(), adjustment, maxDepth);
|
|
6
|
+
return EditorState.push(editorState, content, 'adjust-depth');
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export default adjustBlockDepth;
|
package/es/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export { default as adjustBlockDepth } from './adjustBlockDepth';
|
|
2
|
+
export { default as insertAtomicBlock } from './insertAtomicBlock';
|
|
3
|
+
export { default as insertEmptyBlock } from './insertEmptyBlock';
|
|
4
|
+
export { default as insertNewBlock } from './insertNewBlock';
|
|
5
|
+
export { default as insertText } from './insertText';
|
|
6
|
+
export { default as mergeBlockData } from './mergeBlockData';
|
|
7
|
+
export { default as mergeBlockDataByKey } from './mergeBlockDataByKey';
|
|
8
|
+
export { default as mergeEntityData } from './mergeEntityData';
|
|
9
|
+
export { default as modifyBlock } from './modifyBlock';
|
|
10
|
+
export { default as modifyBlockByKey } from './modifyBlockByKey';
|
|
11
|
+
export { default as removeBlockStyle } from './removeBlockStyle';
|
|
12
|
+
export { default as removeInlineStyles } from './removeInlineStyles';
|
|
13
|
+
export { default as resetBlock } from './resetBlock';
|
|
14
|
+
export { default as toggleBlockType } from './toggleBlockType';
|
|
15
|
+
export { default as toggleEntity } from './toggleEntity';
|
|
16
|
+
export { default as toggleInlineStyle } from './toggleInlineStyle';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { AtomicBlockUtils } from 'draft-js';
|
|
2
|
+
|
|
3
|
+
var insertAtomicBlock = function insertAtomicBlock(editorState, entityType, mutability, data) {
|
|
4
|
+
var character = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : ' ';
|
|
5
|
+
var entityKey = editorState.getCurrentContent().createEntity(entityType, mutability, data).getLastCreatedEntityKey();
|
|
6
|
+
return AtomicBlockUtils.insertAtomicBlock(editorState, entityKey, character);
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export default insertAtomicBlock;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import insertNewBlock from './insertNewBlock';
|
|
2
|
+
|
|
3
|
+
var insertEmptyBlock = function insertEmptyBlock(editorState) {
|
|
4
|
+
var blockType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'unstyled';
|
|
5
|
+
return insertNewBlock(editorState, blockType);
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export default insertEmptyBlock;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { EditorState, ContentBlock, CharacterMetadata, genKey } from 'draft-js';
|
|
2
|
+
import { Map, List, Repeat } from 'immutable';
|
|
3
|
+
import getCurrentBlock from './utils/getCurrentBlock';
|
|
4
|
+
|
|
5
|
+
var insertNewBlock = function insertNewBlock(editorState) {
|
|
6
|
+
var blockType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'unstyled';
|
|
7
|
+
var text = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
|
|
8
|
+
var data = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
9
|
+
var content = editorState.getCurrentContent();
|
|
10
|
+
var selection = editorState.getSelection();
|
|
11
|
+
var currentBlock = getCurrentBlock(editorState);
|
|
12
|
+
var emptyBlockKey = genKey();
|
|
13
|
+
var emptyBlock = new ContentBlock({
|
|
14
|
+
key: emptyBlockKey,
|
|
15
|
+
type: blockType,
|
|
16
|
+
text: text,
|
|
17
|
+
characterList: List(Repeat(CharacterMetadata.create(), text.length)),
|
|
18
|
+
data: Map().merge(data)
|
|
19
|
+
});
|
|
20
|
+
var blockMap = content.getBlockMap();
|
|
21
|
+
var blocksBefore = blockMap.toSeq().takeUntil(function (value) {
|
|
22
|
+
return value === currentBlock;
|
|
23
|
+
});
|
|
24
|
+
var blocksAfter = blockMap.toSeq().skipUntil(function (value) {
|
|
25
|
+
return value === currentBlock;
|
|
26
|
+
}).rest();
|
|
27
|
+
var augmentedBlocks = [[currentBlock.getKey(), currentBlock], [emptyBlockKey, emptyBlock]];
|
|
28
|
+
var newBlocks = blocksBefore.concat(augmentedBlocks, blocksAfter).toOrderedMap();
|
|
29
|
+
var focusKey = emptyBlockKey;
|
|
30
|
+
var newContent = content.merge({
|
|
31
|
+
blockMap: newBlocks,
|
|
32
|
+
selectionBefore: selection,
|
|
33
|
+
selectionAfter: selection.merge({
|
|
34
|
+
anchorKey: focusKey,
|
|
35
|
+
anchorOffset: 0,
|
|
36
|
+
focusKey: focusKey,
|
|
37
|
+
focusOffset: 0,
|
|
38
|
+
isBackward: false
|
|
39
|
+
})
|
|
40
|
+
});
|
|
41
|
+
var newState = EditorState.push(editorState, newContent, 'split-block');
|
|
42
|
+
return EditorState.forceSelection(newState, newContent.getSelectionAfter());
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export default insertNewBlock;
|
package/es/insertText.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { EditorState, Modifier } from 'draft-js';
|
|
2
|
+
|
|
3
|
+
var insertText = function insertText(editorState, text) {
|
|
4
|
+
var entity = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
|
|
5
|
+
var selection = editorState.getSelection();
|
|
6
|
+
var content = editorState.getCurrentContent();
|
|
7
|
+
var newContent = Modifier[selection.isCollapsed() ? 'insertText' : 'replaceText'](content, selection, text, editorState.getCurrentInlineStyle(), entity);
|
|
8
|
+
return EditorState.push(editorState, newContent, 'insert-fragment');
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export default insertText;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { EditorState, Modifier } from 'draft-js';
|
|
2
|
+
import { Map } from 'immutable';
|
|
3
|
+
|
|
4
|
+
var mergeBlockData = function mergeBlockData(editorState, data) {
|
|
5
|
+
var content = editorState.getCurrentContent();
|
|
6
|
+
var selection = editorState.getSelection();
|
|
7
|
+
var newContent = Modifier.mergeBlockData(content, selection, Map(data));
|
|
8
|
+
return EditorState.push(editorState, newContent, 'change-block-data');
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export default mergeBlockData;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
2
|
+
|
|
3
|
+
import { EditorState } from 'draft-js';
|
|
4
|
+
|
|
5
|
+
var mergeBlockDataByKey = function mergeBlockDataByKey(editorState, blockKey, data) {
|
|
6
|
+
var content = editorState.getCurrentContent();
|
|
7
|
+
var updatedBlock = content.getBlockForKey(blockKey).mergeIn(['data'], data);
|
|
8
|
+
var blockMap = content.getBlockMap().merge(_defineProperty({}, blockKey, updatedBlock));
|
|
9
|
+
return EditorState.push(editorState, content.merge({
|
|
10
|
+
blockMap: blockMap
|
|
11
|
+
}), 'change-block-data');
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export default mergeBlockDataByKey;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { EditorState } from 'draft-js';
|
|
2
|
+
|
|
3
|
+
var mergeEntityData = function mergeEntityData(editorState, entityKey, data) {
|
|
4
|
+
var newContentState = editorState.getCurrentContent().mergeEntityData(entityKey, data);
|
|
5
|
+
return EditorState.forceSelection(EditorState.push(editorState, newContentState, 'apply-entity'), editorState.getSelection());
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export default mergeEntityData;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { EditorState } from 'draft-js';
|
|
2
|
+
import modifyBlockForContentState from 'draft-js/lib/modifyBlockForContentState';
|
|
3
|
+
|
|
4
|
+
var modifyBlock = function modifyBlock(editorState, blockData) {
|
|
5
|
+
var content = editorState.getCurrentContent();
|
|
6
|
+
var selection = editorState.getSelection();
|
|
7
|
+
var newContent = modifyBlockForContentState(content, selection, function (block) {
|
|
8
|
+
return block.merge(blockData);
|
|
9
|
+
});
|
|
10
|
+
return EditorState.push(editorState, newContent, 'split-block' // TODO: will this do ?
|
|
11
|
+
);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export default modifyBlock;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { EditorState } from 'draft-js';
|
|
2
|
+
|
|
3
|
+
var modifyBlockByKey = function modifyBlockByKey(editorState, blockKey, blockData) {
|
|
4
|
+
var content = editorState.getCurrentContent();
|
|
5
|
+
var blockMap = content.getBlockMap().map(function (b) {
|
|
6
|
+
return b.key === blockKey ? b.merge(blockData) : b;
|
|
7
|
+
});
|
|
8
|
+
var newContent = content.merge({
|
|
9
|
+
blockMap: blockMap
|
|
10
|
+
});
|
|
11
|
+
return EditorState.push(editorState, newContent, 'split-block' // TODO: will this do ?
|
|
12
|
+
);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export default modifyBlockByKey;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { EditorState, RichUtils } from 'draft-js';
|
|
2
|
+
|
|
3
|
+
var removeBlockStyle = function removeBlockStyle(editorState) {
|
|
4
|
+
var withoutBlockStyle = RichUtils.tryToRemoveBlockStyle(editorState);
|
|
5
|
+
|
|
6
|
+
if (withoutBlockStyle) {
|
|
7
|
+
return EditorState.push(editorState, withoutBlockStyle, 'change-block-type');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return editorState;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export default removeBlockStyle;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
2
|
+
|
|
3
|
+
import { EditorState, ContentState, CharacterMetadata, Modifier } from 'draft-js';
|
|
4
|
+
import getCurrentBlock from './utils/getCurrentBlock';
|
|
5
|
+
|
|
6
|
+
var removeInlineStyles = function removeInlineStyles(editorState) {
|
|
7
|
+
var inlineStyles = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
|
8
|
+
var selection = editorState.getSelection();
|
|
9
|
+
var content = editorState.getCurrentContent();
|
|
10
|
+
var newContent;
|
|
11
|
+
|
|
12
|
+
if (selection.isCollapsed()) {
|
|
13
|
+
var block = getCurrentBlock(editorState);
|
|
14
|
+
var updatedCharacterList = block.getCharacterList().map(function (c) {
|
|
15
|
+
return inlineStyles.reduce(function (characterMetadata, style) {
|
|
16
|
+
return CharacterMetadata.removeStyle(characterMetadata, style);
|
|
17
|
+
}, c);
|
|
18
|
+
});
|
|
19
|
+
var updatedBlock = block.set('characterList', updatedCharacterList);
|
|
20
|
+
newContent = content.merge({
|
|
21
|
+
blockMap: content.getBlockMap().merge(_defineProperty({}, block.getKey(), updatedBlock))
|
|
22
|
+
});
|
|
23
|
+
} else {
|
|
24
|
+
newContent = inlineStyles.reduce(function (contentState, style) {
|
|
25
|
+
return Modifier.removeInlineStyle(contentState, selection, style);
|
|
26
|
+
}, content);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return EditorState.push(editorState, newContent, 'change-inline-style');
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export default removeInlineStyles;
|
package/es/resetBlock.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { EditorState, SelectionState, Modifier } from 'draft-js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Remove range also change block type to 'unstyled'
|
|
5
|
+
*/
|
|
6
|
+
var resetBlock = function resetBlock(editorState, block) {
|
|
7
|
+
var content = editorState.getCurrentContent();
|
|
8
|
+
var key = block.getKey();
|
|
9
|
+
var targetRange = new SelectionState({
|
|
10
|
+
anchorKey: key,
|
|
11
|
+
anchorOffset: 0,
|
|
12
|
+
focusKey: key,
|
|
13
|
+
focusOffset: block.getLength()
|
|
14
|
+
});
|
|
15
|
+
var withoutTargetContent = Modifier.removeRange(content, targetRange, 'backward');
|
|
16
|
+
var resetBlock = Modifier.setBlockType(withoutTargetContent, withoutTargetContent.getSelectionAfter(), 'unstyled');
|
|
17
|
+
var newState = EditorState.push(editorState, resetBlock, 'remove-range');
|
|
18
|
+
return EditorState.forceSelection(newState, resetBlock.getSelectionAfter());
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export default resetBlock;
|
package/index.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "adjustBlockDepth", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function get() {
|
|
9
|
+
return _adjustBlockDepth.default;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, "insertAtomicBlock", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function get() {
|
|
15
|
+
return _insertAtomicBlock.default;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
Object.defineProperty(exports, "insertEmptyBlock", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: function get() {
|
|
21
|
+
return _insertEmptyBlock.default;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
Object.defineProperty(exports, "insertNewBlock", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
get: function get() {
|
|
27
|
+
return _insertNewBlock.default;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
Object.defineProperty(exports, "insertText", {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
get: function get() {
|
|
33
|
+
return _insertText.default;
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
Object.defineProperty(exports, "mergeBlockData", {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
get: function get() {
|
|
39
|
+
return _mergeBlockData.default;
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
Object.defineProperty(exports, "mergeBlockDataByKey", {
|
|
43
|
+
enumerable: true,
|
|
44
|
+
get: function get() {
|
|
45
|
+
return _mergeBlockDataByKey.default;
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
Object.defineProperty(exports, "mergeEntityData", {
|
|
49
|
+
enumerable: true,
|
|
50
|
+
get: function get() {
|
|
51
|
+
return _mergeEntityData.default;
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
Object.defineProperty(exports, "modifyBlock", {
|
|
55
|
+
enumerable: true,
|
|
56
|
+
get: function get() {
|
|
57
|
+
return _modifyBlock.default;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
Object.defineProperty(exports, "modifyBlockByKey", {
|
|
61
|
+
enumerable: true,
|
|
62
|
+
get: function get() {
|
|
63
|
+
return _modifyBlockByKey.default;
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
Object.defineProperty(exports, "removeBlockStyle", {
|
|
67
|
+
enumerable: true,
|
|
68
|
+
get: function get() {
|
|
69
|
+
return _removeBlockStyle.default;
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
Object.defineProperty(exports, "removeInlineStyles", {
|
|
73
|
+
enumerable: true,
|
|
74
|
+
get: function get() {
|
|
75
|
+
return _removeInlineStyles.default;
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
Object.defineProperty(exports, "resetBlock", {
|
|
79
|
+
enumerable: true,
|
|
80
|
+
get: function get() {
|
|
81
|
+
return _resetBlock.default;
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
Object.defineProperty(exports, "toggleBlockType", {
|
|
85
|
+
enumerable: true,
|
|
86
|
+
get: function get() {
|
|
87
|
+
return _toggleBlockType.default;
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
Object.defineProperty(exports, "toggleEntity", {
|
|
91
|
+
enumerable: true,
|
|
92
|
+
get: function get() {
|
|
93
|
+
return _toggleEntity.default;
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
Object.defineProperty(exports, "toggleInlineStyle", {
|
|
97
|
+
enumerable: true,
|
|
98
|
+
get: function get() {
|
|
99
|
+
return _toggleInlineStyle.default;
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
var _adjustBlockDepth = _interopRequireDefault(require("./adjustBlockDepth"));
|
|
104
|
+
|
|
105
|
+
var _insertAtomicBlock = _interopRequireDefault(require("./insertAtomicBlock"));
|
|
106
|
+
|
|
107
|
+
var _insertEmptyBlock = _interopRequireDefault(require("./insertEmptyBlock"));
|
|
108
|
+
|
|
109
|
+
var _insertNewBlock = _interopRequireDefault(require("./insertNewBlock"));
|
|
110
|
+
|
|
111
|
+
var _insertText = _interopRequireDefault(require("./insertText"));
|
|
112
|
+
|
|
113
|
+
var _mergeBlockData = _interopRequireDefault(require("./mergeBlockData"));
|
|
114
|
+
|
|
115
|
+
var _mergeBlockDataByKey = _interopRequireDefault(require("./mergeBlockDataByKey"));
|
|
116
|
+
|
|
117
|
+
var _mergeEntityData = _interopRequireDefault(require("./mergeEntityData"));
|
|
118
|
+
|
|
119
|
+
var _modifyBlock = _interopRequireDefault(require("./modifyBlock"));
|
|
120
|
+
|
|
121
|
+
var _modifyBlockByKey = _interopRequireDefault(require("./modifyBlockByKey"));
|
|
122
|
+
|
|
123
|
+
var _removeBlockStyle = _interopRequireDefault(require("./removeBlockStyle"));
|
|
124
|
+
|
|
125
|
+
var _removeInlineStyles = _interopRequireDefault(require("./removeInlineStyles"));
|
|
126
|
+
|
|
127
|
+
var _resetBlock = _interopRequireDefault(require("./resetBlock"));
|
|
128
|
+
|
|
129
|
+
var _toggleBlockType = _interopRequireDefault(require("./toggleBlockType"));
|
|
130
|
+
|
|
131
|
+
var _toggleEntity = _interopRequireDefault(require("./toggleEntity"));
|
|
132
|
+
|
|
133
|
+
var _toggleInlineStyle = _interopRequireDefault(require("./toggleInlineStyle"));
|
|
134
|
+
|
|
135
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
@@ -0,0 +1,17 @@
|
|
|
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 insertAtomicBlock = function insertAtomicBlock(editorState, entityType, mutability, data) {
|
|
11
|
+
var character = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : ' ';
|
|
12
|
+
var entityKey = editorState.getCurrentContent().createEntity(entityType, mutability, data).getLastCreatedEntityKey();
|
|
13
|
+
return _draftJs.AtomicBlockUtils.insertAtomicBlock(editorState, entityKey, character);
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
var _default = insertAtomicBlock;
|
|
17
|
+
exports.default = _default;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _insertNewBlock = _interopRequireDefault(require("./insertNewBlock"));
|
|
9
|
+
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
|
|
12
|
+
var insertEmptyBlock = function insertEmptyBlock(editorState) {
|
|
13
|
+
var blockType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'unstyled';
|
|
14
|
+
return (0, _insertNewBlock.default)(editorState, blockType);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
var _default = insertEmptyBlock;
|
|
18
|
+
exports.default = _default;
|
|
@@ -0,0 +1,59 @@
|
|
|
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 _getCurrentBlock = _interopRequireDefault(require("./utils/getCurrentBlock"));
|
|
13
|
+
|
|
14
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
+
|
|
16
|
+
var insertNewBlock = function insertNewBlock(editorState) {
|
|
17
|
+
var blockType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'unstyled';
|
|
18
|
+
var text = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
|
|
19
|
+
var data = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
20
|
+
var content = editorState.getCurrentContent();
|
|
21
|
+
var selection = editorState.getSelection();
|
|
22
|
+
var currentBlock = (0, _getCurrentBlock.default)(editorState);
|
|
23
|
+
var emptyBlockKey = (0, _draftJs.genKey)();
|
|
24
|
+
var emptyBlock = new _draftJs.ContentBlock({
|
|
25
|
+
key: emptyBlockKey,
|
|
26
|
+
type: blockType,
|
|
27
|
+
text: text,
|
|
28
|
+
characterList: (0, _immutable.List)((0, _immutable.Repeat)(_draftJs.CharacterMetadata.create(), text.length)),
|
|
29
|
+
data: (0, _immutable.Map)().merge(data)
|
|
30
|
+
});
|
|
31
|
+
var blockMap = content.getBlockMap();
|
|
32
|
+
var blocksBefore = blockMap.toSeq().takeUntil(function (value) {
|
|
33
|
+
return value === currentBlock;
|
|
34
|
+
});
|
|
35
|
+
var blocksAfter = blockMap.toSeq().skipUntil(function (value) {
|
|
36
|
+
return value === currentBlock;
|
|
37
|
+
}).rest();
|
|
38
|
+
var augmentedBlocks = [[currentBlock.getKey(), currentBlock], [emptyBlockKey, emptyBlock]];
|
|
39
|
+
var newBlocks = blocksBefore.concat(augmentedBlocks, blocksAfter).toOrderedMap();
|
|
40
|
+
var focusKey = emptyBlockKey;
|
|
41
|
+
var newContent = content.merge({
|
|
42
|
+
blockMap: newBlocks,
|
|
43
|
+
selectionBefore: selection,
|
|
44
|
+
selectionAfter: selection.merge({
|
|
45
|
+
anchorKey: focusKey,
|
|
46
|
+
anchorOffset: 0,
|
|
47
|
+
focusKey: focusKey,
|
|
48
|
+
focusOffset: 0,
|
|
49
|
+
isBackward: false
|
|
50
|
+
})
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
var newState = _draftJs.EditorState.push(editorState, newContent, 'split-block');
|
|
54
|
+
|
|
55
|
+
return _draftJs.EditorState.forceSelection(newState, newContent.getSelectionAfter());
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
var _default = insertNewBlock;
|
|
59
|
+
exports.default = _default;
|
package/insertText.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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 insertText = function insertText(editorState, text) {
|
|
11
|
+
var entity = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
|
|
12
|
+
var selection = editorState.getSelection();
|
|
13
|
+
var content = editorState.getCurrentContent();
|
|
14
|
+
|
|
15
|
+
var newContent = _draftJs.Modifier[selection.isCollapsed() ? 'insertText' : 'replaceText'](content, selection, text, editorState.getCurrentInlineStyle(), entity);
|
|
16
|
+
|
|
17
|
+
return _draftJs.EditorState.push(editorState, newContent, 'insert-fragment');
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
var _default = insertText;
|
|
21
|
+
exports.default = _default;
|
|
@@ -0,0 +1,22 @@
|
|
|
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 mergeBlockData = function mergeBlockData(editorState, data) {
|
|
13
|
+
var content = editorState.getCurrentContent();
|
|
14
|
+
var selection = editorState.getSelection();
|
|
15
|
+
|
|
16
|
+
var newContent = _draftJs.Modifier.mergeBlockData(content, selection, (0, _immutable.Map)(data));
|
|
17
|
+
|
|
18
|
+
return _draftJs.EditorState.push(editorState, newContent, 'change-block-data');
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
var _default = mergeBlockData;
|
|
22
|
+
exports.default = _default;
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
11
|
+
|
|
12
|
+
var mergeBlockDataByKey = function mergeBlockDataByKey(editorState, blockKey, data) {
|
|
13
|
+
var content = editorState.getCurrentContent();
|
|
14
|
+
var updatedBlock = content.getBlockForKey(blockKey).mergeIn(['data'], data);
|
|
15
|
+
var blockMap = content.getBlockMap().merge(_defineProperty({}, blockKey, updatedBlock));
|
|
16
|
+
return _draftJs.EditorState.push(editorState, content.merge({
|
|
17
|
+
blockMap: blockMap
|
|
18
|
+
}), 'change-block-data');
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
var _default = mergeBlockDataByKey;
|
|
22
|
+
exports.default = _default;
|
|
@@ -0,0 +1,16 @@
|
|
|
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 mergeEntityData = function mergeEntityData(editorState, entityKey, data) {
|
|
11
|
+
var newContentState = editorState.getCurrentContent().mergeEntityData(entityKey, data);
|
|
12
|
+
return _draftJs.EditorState.forceSelection(_draftJs.EditorState.push(editorState, newContentState, 'apply-entity'), editorState.getSelection());
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
var _default = mergeEntityData;
|
|
16
|
+
exports.default = _default;
|
package/modifyBlock.js
ADDED
|
@@ -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 _modifyBlockForContentState = _interopRequireDefault(require("draft-js/lib/modifyBlockForContentState"));
|
|
11
|
+
|
|
12
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
|
+
|
|
14
|
+
var modifyBlock = function modifyBlock(editorState, blockData) {
|
|
15
|
+
var content = editorState.getCurrentContent();
|
|
16
|
+
var selection = editorState.getSelection();
|
|
17
|
+
var newContent = (0, _modifyBlockForContentState.default)(content, selection, function (block) {
|
|
18
|
+
return block.merge(blockData);
|
|
19
|
+
});
|
|
20
|
+
return _draftJs.EditorState.push(editorState, newContent, 'split-block' // TODO: will this do ?
|
|
21
|
+
);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
var _default = modifyBlock;
|
|
25
|
+
exports.default = _default;
|
|
@@ -0,0 +1,23 @@
|
|
|
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 modifyBlockByKey = function modifyBlockByKey(editorState, blockKey, blockData) {
|
|
11
|
+
var content = editorState.getCurrentContent();
|
|
12
|
+
var blockMap = content.getBlockMap().map(function (b) {
|
|
13
|
+
return b.key === blockKey ? b.merge(blockData) : b;
|
|
14
|
+
});
|
|
15
|
+
var newContent = content.merge({
|
|
16
|
+
blockMap: blockMap
|
|
17
|
+
});
|
|
18
|
+
return _draftJs.EditorState.push(editorState, newContent, 'split-block' // TODO: will this do ?
|
|
19
|
+
);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
var _default = modifyBlockByKey;
|
|
23
|
+
exports.default = _default;
|
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@studysync/draft-js-modifiers",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Modular state modifiers for Draft.js",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"module": "es/index.js",
|
|
7
|
+
"jsnext:main": "es/index.js",
|
|
8
|
+
"files": [
|
|
9
|
+
"es",
|
|
10
|
+
"utils",
|
|
11
|
+
"*.js"
|
|
12
|
+
],
|
|
13
|
+
"keywords": [
|
|
14
|
+
"draft-js",
|
|
15
|
+
"draft-js-modifiers"
|
|
16
|
+
],
|
|
17
|
+
"repository": "https://github.com/dkadrios/draft-js-modifiers.git",
|
|
18
|
+
"author": "sugarshin",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"scripts": {
|
|
21
|
+
"add": "node ./script/add-module",
|
|
22
|
+
"start": "npm run watch:test",
|
|
23
|
+
"lint": "eslint src test",
|
|
24
|
+
"type": "flow src",
|
|
25
|
+
"check": "npm run lint && npm run type",
|
|
26
|
+
"test": "ava -v",
|
|
27
|
+
"test:coverage": "nyc npm test",
|
|
28
|
+
"test:coverage:report": "npm run test:coverage && nyc report --reporter=text-lcov | coveralls",
|
|
29
|
+
"test:ci": "npm run check && npm run test:coverage:report",
|
|
30
|
+
"watch:test": "npm test -- --watch",
|
|
31
|
+
"clean:build": "rm -rf ./*.js ./utils ./es",
|
|
32
|
+
"prebuild": "npm run clean:build",
|
|
33
|
+
"build:commonjs": "BABEL_ENV=commonjs babel -d . src",
|
|
34
|
+
"build:es": "BABEL_ENV=es babel -d es src",
|
|
35
|
+
"build": "npm run build:commonjs && npm run build:es",
|
|
36
|
+
"prepublishOnly": "npm run check && npm run test:coverage && npm run build"
|
|
37
|
+
},
|
|
38
|
+
"ava": {
|
|
39
|
+
"require": [
|
|
40
|
+
"@babel/register"
|
|
41
|
+
]
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"draft-js": "^0.11.7",
|
|
45
|
+
"immutable": "~3.7.4"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@babel/cli": "^7.0.0-beta.47",
|
|
49
|
+
"@babel/core": "^7.0.0-beta.47",
|
|
50
|
+
"@babel/plugin-transform-flow-strip-types": "^7.0.0-beta.47",
|
|
51
|
+
"@babel/preset-es2015": "^7.0.0-beta.47",
|
|
52
|
+
"@babel/preset-es2016": "^7.0.0-beta.47",
|
|
53
|
+
"@babel/preset-es2017": "^7.0.0-beta.47",
|
|
54
|
+
"@babel/register": "^7.0.0-beta.47",
|
|
55
|
+
"ava": "^0.25.0",
|
|
56
|
+
"babel-eslint": "^8.2.3",
|
|
57
|
+
"coveralls": "^3.0.1",
|
|
58
|
+
"eslint": "^4.19.1",
|
|
59
|
+
"eslint-plugin-ava": "^4.5.1",
|
|
60
|
+
"eslint-plugin-babel": "^5.1.0",
|
|
61
|
+
"eslint-plugin-flowtype": "^2.47.1",
|
|
62
|
+
"flow-bin": "^0.70.0",
|
|
63
|
+
"nyc": "^11.8.0",
|
|
64
|
+
"react": "^16.3.2",
|
|
65
|
+
"react-dom": "^16.3.2"
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
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 removeBlockStyle = function removeBlockStyle(editorState) {
|
|
11
|
+
var withoutBlockStyle = _draftJs.RichUtils.tryToRemoveBlockStyle(editorState);
|
|
12
|
+
|
|
13
|
+
if (withoutBlockStyle) {
|
|
14
|
+
return _draftJs.EditorState.push(editorState, withoutBlockStyle, 'change-block-type');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return editorState;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
var _default = removeBlockStyle;
|
|
21
|
+
exports.default = _default;
|
|
@@ -0,0 +1,43 @@
|
|
|
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 _getCurrentBlock = _interopRequireDefault(require("./utils/getCurrentBlock"));
|
|
11
|
+
|
|
12
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
|
+
|
|
14
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
15
|
+
|
|
16
|
+
var removeInlineStyles = function removeInlineStyles(editorState) {
|
|
17
|
+
var inlineStyles = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
|
18
|
+
var selection = editorState.getSelection();
|
|
19
|
+
var content = editorState.getCurrentContent();
|
|
20
|
+
var newContent;
|
|
21
|
+
|
|
22
|
+
if (selection.isCollapsed()) {
|
|
23
|
+
var block = (0, _getCurrentBlock.default)(editorState);
|
|
24
|
+
var updatedCharacterList = block.getCharacterList().map(function (c) {
|
|
25
|
+
return inlineStyles.reduce(function (characterMetadata, style) {
|
|
26
|
+
return _draftJs.CharacterMetadata.removeStyle(characterMetadata, style);
|
|
27
|
+
}, c);
|
|
28
|
+
});
|
|
29
|
+
var updatedBlock = block.set('characterList', updatedCharacterList);
|
|
30
|
+
newContent = content.merge({
|
|
31
|
+
blockMap: content.getBlockMap().merge(_defineProperty({}, block.getKey(), updatedBlock))
|
|
32
|
+
});
|
|
33
|
+
} else {
|
|
34
|
+
newContent = inlineStyles.reduce(function (contentState, style) {
|
|
35
|
+
return _draftJs.Modifier.removeInlineStyle(contentState, selection, style);
|
|
36
|
+
}, content);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return _draftJs.EditorState.push(editorState, newContent, 'change-inline-style');
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
var _default = removeInlineStyles;
|
|
43
|
+
exports.default = _default;
|
package/resetBlock.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
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
|
+
* Remove range also change block type to 'unstyled'
|
|
12
|
+
*/
|
|
13
|
+
var resetBlock = function resetBlock(editorState, block) {
|
|
14
|
+
var content = editorState.getCurrentContent();
|
|
15
|
+
var key = block.getKey();
|
|
16
|
+
var targetRange = new _draftJs.SelectionState({
|
|
17
|
+
anchorKey: key,
|
|
18
|
+
anchorOffset: 0,
|
|
19
|
+
focusKey: key,
|
|
20
|
+
focusOffset: block.getLength()
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
var withoutTargetContent = _draftJs.Modifier.removeRange(content, targetRange, 'backward');
|
|
24
|
+
|
|
25
|
+
var resetBlock = _draftJs.Modifier.setBlockType(withoutTargetContent, withoutTargetContent.getSelectionAfter(), 'unstyled');
|
|
26
|
+
|
|
27
|
+
var newState = _draftJs.EditorState.push(editorState, resetBlock, 'remove-range');
|
|
28
|
+
|
|
29
|
+
return _draftJs.EditorState.forceSelection(newState, resetBlock.getSelectionAfter());
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
var _default = resetBlock;
|
|
33
|
+
exports.default = _default;
|
|
@@ -0,0 +1,15 @@
|
|
|
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 toggleBlockType = function toggleBlockType(editorState, blockType) {
|
|
11
|
+
return _draftJs.RichUtils.toggleBlockType(editorState, blockType);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
var _default = toggleBlockType;
|
|
15
|
+
exports.default = _default;
|
package/toggleEntity.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
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 toggleEntity = function toggleEntity(editorState, entityKey) {
|
|
11
|
+
return _draftJs.RichUtils.toggleLink(editorState, editorState.getSelection(), entityKey);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
var _default = toggleEntity;
|
|
15
|
+
exports.default = _default;
|
|
@@ -0,0 +1,15 @@
|
|
|
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 toggleInlineStyle = function toggleInlineStyle(editorState, inlineStyle) {
|
|
11
|
+
return _draftJs.RichUtils.toggleInlineStyle(editorState, inlineStyle);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
var _default = toggleInlineStyle;
|
|
15
|
+
exports.default = _default;
|
|
@@ -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 selection = editorState.getSelection();
|
|
10
|
+
return editorState.getCurrentContent().getBlockForKey(selection.getStartKey());
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
var _default = getCurrentBlock;
|
|
14
|
+
exports.default = _default;
|