@studysync/draft-js-modifiers 0.4.15 → 0.5.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 +18 -5
- package/package.json +10 -4
- package/flow/stub/__dev__.js +0 -3
- package/script/add-module.js +0 -48
- package/src/addBlock.js +0 -98
- package/src/adjustBlockDepth.js +0 -21
- package/src/atEndOfBlock.js +0 -10
- package/src/atStartOfBlock.js +0 -7
- package/src/changeBlockType.js +0 -19
- package/src/getAllEntities.js +0 -14
- package/src/getEntitiesForBlock.js +0 -29
- package/src/getEntitiesForSelection.js +0 -20
- package/src/getSelectedText.js +0 -11
- package/src/getSimilarAdjacentBlocks.js +0 -53
- package/src/index.js +0 -41
- package/src/insertAtomicBlock.js +0 -23
- package/src/insertBlock.js +0 -40
- package/src/insertEmptyBlock.js +0 -12
- package/src/insertEntity.js +0 -56
- package/src/insertNewBlock.js +0 -61
- package/src/insertText.js +0 -23
- package/src/mergeBlockData.js +0 -13
- package/src/mergeBlockDataByKey.js +0 -12
- package/src/mergeEntityData.js +0 -13
- package/src/modifyBlock.js +0 -20
- package/src/modifyBlockByKey.js +0 -19
- package/src/moveCaretAfterBlock.js +0 -26
- package/src/moveCaretToEnd.js +0 -20
- package/src/removeBlock.js +0 -43
- package/src/removeBlockStyle.js +0 -17
- package/src/removeEntity.js +0 -38
- package/src/removeInlineStyles.js +0 -29
- package/src/resetBlock.js +0 -29
- package/src/selectAll.js +0 -15
- package/src/selectBlockByKey.js +0 -20
- package/src/setBlockData.js +0 -14
- package/src/toggleBlockStyle.js +0 -33
- package/src/toggleBlockType.js +0 -12
- package/src/toggleEntity.js +0 -11
- package/src/toggleInlineStyle.js +0 -11
- package/src/trimEditorState.js +0 -41
- package/src/utils/getCurrentBlock.js +0 -10
- package/src/utils/numbers.js +0 -3
- package/test/adjustBlockDepth.js +0 -45
- package/test/export.js +0 -14
- package/test/fixtures/createEditorState.js +0 -12
- package/test/fixtures/omitBlockKeysFromRawContent.js +0 -9
- package/test/insertAtomicBlock.js +0 -89
- package/test/insertEmptyBlock.js +0 -65
- package/test/insertNewBlock.js +0 -68
- package/test/insertText.js +0 -58
- package/test/mergeBlockData.js +0 -40
- package/test/mergeBlockDataByKey.js +0 -103
- package/test/mergeEntityData.js +0 -49
- package/test/modifyBlock.js +0 -52
- package/test/modifyBlockByKey.js +0 -91
- package/test/removeBlockStyle.js +0 -52
- package/test/removeInlineStyles.js +0 -68
- package/test/resetBlock.js +0 -83
- package/test/toggleBlockType.js +0 -4
- package/test/toggleEntity.js +0 -4
- package/test/toggleInlineStyle.js +0 -4
package/README.md
CHANGED
|
@@ -8,17 +8,30 @@
|
|
|
8
8
|
Modular state modifiers for [Draft.js](https://draftjs.org/)
|
|
9
9
|
|
|
10
10
|
```bash
|
|
11
|
-
yarn add draft-js-modifiers
|
|
11
|
+
yarn add @studysync/draft-js-modifiers draft-js immutable
|
|
12
12
|
|
|
13
13
|
# or
|
|
14
14
|
|
|
15
|
-
npm i draft-js-modifiers
|
|
15
|
+
npm i @studysync/draft-js-modifiers draft-js immutable
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
When using legacy Draft.js packages, force a single patched `immutable` 3.x version at the app root:
|
|
19
|
+
|
|
20
|
+
```json
|
|
21
|
+
{
|
|
22
|
+
"overrides": {
|
|
23
|
+
"immutable": "3.8.3"
|
|
24
|
+
},
|
|
25
|
+
"resolutions": {
|
|
26
|
+
"immutable": "3.8.3"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
16
29
|
```
|
|
17
30
|
|
|
18
31
|
## Usage
|
|
19
32
|
|
|
20
33
|
```js
|
|
21
|
-
import * as Modifiers from 'draft-js-modifiers'
|
|
34
|
+
import * as Modifiers from '@studysync/draft-js-modifiers'
|
|
22
35
|
|
|
23
36
|
const newEditorState = Modifiers.mergeBlockData(currentEditorState, { foo: 1 })
|
|
24
37
|
```
|
|
@@ -26,10 +39,10 @@ const newEditorState = Modifiers.mergeBlockData(currentEditorState, { foo: 1 })
|
|
|
26
39
|
### Moduler importing
|
|
27
40
|
|
|
28
41
|
```js
|
|
29
|
-
import adjustBlockDepth from 'draft-js-modifiers/adjustBlockDepth'
|
|
42
|
+
import adjustBlockDepth from '@studysync/draft-js-modifiers/adjustBlockDepth'
|
|
30
43
|
|
|
31
44
|
// Support Tree Shaking for webpack, rollup.js
|
|
32
|
-
import { insertText } from 'draft-js-modifiers'
|
|
45
|
+
import { insertText } from '@studysync/draft-js-modifiers'
|
|
33
46
|
```
|
|
34
47
|
|
|
35
48
|
## Methods
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@studysync/draft-js-modifiers",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Modular state modifiers for Draft.js",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "es/index.js",
|
|
@@ -40,9 +40,9 @@
|
|
|
40
40
|
"@babel/register"
|
|
41
41
|
]
|
|
42
42
|
},
|
|
43
|
-
"
|
|
43
|
+
"peerDependencies": {
|
|
44
44
|
"draft-js": "^0.11.7",
|
|
45
|
-
"immutable": "
|
|
45
|
+
"immutable": ">=3.8.3 <4"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"@babel/cli": "7.17.0",
|
|
@@ -53,16 +53,22 @@
|
|
|
53
53
|
"ava": "^0.25.0",
|
|
54
54
|
"babel-eslint": "^8.2.3",
|
|
55
55
|
"coveralls": "^3.0.1",
|
|
56
|
+
"draft-js": "0.11.7",
|
|
56
57
|
"eslint": "^4.19.1",
|
|
57
58
|
"eslint-plugin-ava": "^4.5.1",
|
|
58
59
|
"eslint-plugin-babel": "^5.1.0",
|
|
59
60
|
"eslint-plugin-flowtype": "^2.47.1",
|
|
60
61
|
"flow-bin": "^0.70.0",
|
|
62
|
+
"immutable": "3.8.3",
|
|
61
63
|
"nyc": "^11.8.0",
|
|
62
64
|
"react": ">=17",
|
|
63
65
|
"react-dom": ">=17"
|
|
64
66
|
},
|
|
67
|
+
"overrides": {
|
|
68
|
+
"immutable": "3.8.3"
|
|
69
|
+
},
|
|
65
70
|
"resolutions": {
|
|
66
|
-
"graceful-fs": "4.2.4"
|
|
71
|
+
"graceful-fs": "4.2.4",
|
|
72
|
+
"immutable": "3.8.3"
|
|
67
73
|
}
|
|
68
74
|
}
|
package/flow/stub/__dev__.js
DELETED
package/script/add-module.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
const fs = require('fs')
|
|
2
|
-
const path = require('path')
|
|
3
|
-
const n = process.argv[2]
|
|
4
|
-
if (!n) {
|
|
5
|
-
throw new Error('should must be called with name. `npm run add -- someModule`')
|
|
6
|
-
}
|
|
7
|
-
const createFile = (name, dir, template) => {
|
|
8
|
-
const file = path.resolve(__dirname, '..', dir, `${name}.js`)
|
|
9
|
-
fs.writeFileSync(file, template.join('\n'))
|
|
10
|
-
console.log(`Successfully ${dir}/${name}.js created.`)
|
|
11
|
-
}
|
|
12
|
-
const addToIndex = name => {
|
|
13
|
-
const indexFilePath = path.resolve(__dirname, '..', 'src', 'index.js')
|
|
14
|
-
const indexFile = fs.readFileSync(indexFilePath, { encoding: 'utf8' })
|
|
15
|
-
fs.writeFileSync(indexFilePath, indexFile + [
|
|
16
|
-
`export { default as ${name} } from './${name}'`,
|
|
17
|
-
'',
|
|
18
|
-
].join('\n'))
|
|
19
|
-
console.log(`Successfully src/${name}.js updated.`)
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
createFile(n, 'src', [
|
|
23
|
-
'// @flow',
|
|
24
|
-
'',
|
|
25
|
-
'import { EditorState } from \'draft-js\'',
|
|
26
|
-
'',
|
|
27
|
-
`const ${n} = (`,
|
|
28
|
-
' editorState: EditorState',
|
|
29
|
-
'): EditorState => {}',
|
|
30
|
-
'',
|
|
31
|
-
`export default ${n}`,
|
|
32
|
-
'',
|
|
33
|
-
])
|
|
34
|
-
|
|
35
|
-
createFile(n, 'test', [
|
|
36
|
-
'import test from \'ava\'',
|
|
37
|
-
'import { convertToRaw } from \'draft-js\'',
|
|
38
|
-
'import createEditorState from \'./fixtures/createEditorState\'',
|
|
39
|
-
'',
|
|
40
|
-
`import ${n} from '../src/${n}'`,
|
|
41
|
-
'',
|
|
42
|
-
`test.skip('${n}', () => {})`,
|
|
43
|
-
'',
|
|
44
|
-
])
|
|
45
|
-
|
|
46
|
-
addToIndex(n)
|
|
47
|
-
|
|
48
|
-
console.log('Please add documentation to README.md')
|
package/src/addBlock.js
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
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
|
-
// from draft-js-plugins/draft-js-plugins/packages/drag-n-drop/src/modifiers/addBlock.ts
|
|
9
|
-
const addBlock = (
|
|
10
|
-
editorState,
|
|
11
|
-
selection,
|
|
12
|
-
type,
|
|
13
|
-
data,
|
|
14
|
-
entityType,
|
|
15
|
-
text = ' '
|
|
16
|
-
) => {
|
|
17
|
-
const currentContentState = editorState.getCurrentContent()
|
|
18
|
-
const currentSelectionState = selection
|
|
19
|
-
|
|
20
|
-
// in case text is selected it is removed and then the block is appended
|
|
21
|
-
const afterRemovalContentState = Modifier.removeRange(
|
|
22
|
-
currentContentState,
|
|
23
|
-
currentSelectionState,
|
|
24
|
-
'backward'
|
|
25
|
-
)
|
|
26
|
-
|
|
27
|
-
// deciding on the position to split the text
|
|
28
|
-
const targetSelection = afterRemovalContentState.getSelectionAfter()
|
|
29
|
-
const blockKeyForTarget = targetSelection.get('focusKey')
|
|
30
|
-
const block = currentContentState.getBlockForKey(blockKeyForTarget)
|
|
31
|
-
let insertionTargetSelection
|
|
32
|
-
let insertionTargetBlock
|
|
33
|
-
|
|
34
|
-
// In case there are no characters or entity or the selection is at the start it
|
|
35
|
-
// is safe to insert the block in the current block.
|
|
36
|
-
// Otherwise a new block is created (the block is always its own block)
|
|
37
|
-
const isEmptyBlock = block.getLength() === 0 && block.getEntityAt(0) === null
|
|
38
|
-
const selectedFromStart = currentSelectionState.getStartOffset() === 0
|
|
39
|
-
if (isEmptyBlock || selectedFromStart) {
|
|
40
|
-
insertionTargetSelection = targetSelection
|
|
41
|
-
insertionTargetBlock = afterRemovalContentState
|
|
42
|
-
} else {
|
|
43
|
-
// the only way to insert a new seems to be by splitting an existing in to two
|
|
44
|
-
insertionTargetBlock = Modifier.splitBlock(
|
|
45
|
-
afterRemovalContentState,
|
|
46
|
-
targetSelection
|
|
47
|
-
)
|
|
48
|
-
|
|
49
|
-
// the position to insert our blocks
|
|
50
|
-
insertionTargetSelection = insertionTargetBlock.getSelectionAfter()
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// TODO not sure why we need it …
|
|
54
|
-
const newContentStateAfterSplit = Modifier.setBlockType(
|
|
55
|
-
insertionTargetBlock,
|
|
56
|
-
insertionTargetSelection,
|
|
57
|
-
type
|
|
58
|
-
)
|
|
59
|
-
|
|
60
|
-
// creating a new ContentBlock including the entity with data
|
|
61
|
-
// Entity will be created with a specific type, if defined, else will fall back to the ContentBlock type
|
|
62
|
-
const contentStateWithEntity = newContentStateAfterSplit.createEntity(
|
|
63
|
-
entityType || type,
|
|
64
|
-
'IMMUTABLE',
|
|
65
|
-
data
|
|
66
|
-
)
|
|
67
|
-
const entityKey = contentStateWithEntity.getLastCreatedEntityKey()
|
|
68
|
-
const charData = CharacterMetadata.create({ entity: entityKey })
|
|
69
|
-
|
|
70
|
-
const fragmentArray = [
|
|
71
|
-
new ContentBlock({
|
|
72
|
-
key: genKey(),
|
|
73
|
-
type,
|
|
74
|
-
text,
|
|
75
|
-
characterList: List(Repeat(charData, text.length || 1)), // eslint-disable-line new-cap
|
|
76
|
-
}),
|
|
77
|
-
|
|
78
|
-
// new contentblock so we can continue wrting right away after inserting the block
|
|
79
|
-
new ContentBlock({
|
|
80
|
-
key: genKey(),
|
|
81
|
-
type: 'unstyled',
|
|
82
|
-
text: '',
|
|
83
|
-
characterList: List(), // eslint-disable-line new-cap
|
|
84
|
-
}),
|
|
85
|
-
]
|
|
86
|
-
|
|
87
|
-
// create fragment containing the two content blocks
|
|
88
|
-
const fragment = BlockMapBuilder.createFromArray(fragmentArray)
|
|
89
|
-
|
|
90
|
-
// replace the contentblock we reserved for our insert
|
|
91
|
-
return Modifier.replaceWithFragment(
|
|
92
|
-
newContentStateAfterSplit,
|
|
93
|
-
insertionTargetSelection,
|
|
94
|
-
fragment
|
|
95
|
-
)
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export default addBlock
|
package/src/adjustBlockDepth.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
|
|
3
|
-
import { EditorState } from 'draft-js'
|
|
4
|
-
import adjustBlockDepthForContentState from 'draft-js/lib/adjustBlockDepthForContentState'
|
|
5
|
-
|
|
6
|
-
const adjustBlockDepth = (
|
|
7
|
-
editorState: EditorState,
|
|
8
|
-
adjustment: number,
|
|
9
|
-
maxDepth: number
|
|
10
|
-
): EditorState => {
|
|
11
|
-
const content = adjustBlockDepthForContentState(
|
|
12
|
-
editorState.getCurrentContent(),
|
|
13
|
-
editorState.getSelection(),
|
|
14
|
-
adjustment,
|
|
15
|
-
maxDepth
|
|
16
|
-
)
|
|
17
|
-
|
|
18
|
-
return EditorState.push(editorState, content, 'adjust-depth')
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export default adjustBlockDepth
|
package/src/atEndOfBlock.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import getCurrentBlock from "./utils/getCurrentBlock"
|
|
2
|
-
|
|
3
|
-
const atEndOfBlock = (editorState) => {
|
|
4
|
-
const currentBlock = getCurrentBlock(editorState)
|
|
5
|
-
const selection = editorState.getSelection()
|
|
6
|
-
const endOffset = selection.getEndOffset()
|
|
7
|
-
return endOffset === currentBlock.getLength()
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export default atEndOfBlock
|
package/src/atStartOfBlock.js
DELETED
package/src/changeBlockType.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { RichUtils } from "draft-js"
|
|
2
|
-
import toggleBlockStyle from "./toggleBlockStyle"
|
|
3
|
-
import getCurrentBlock from "./utils/getCurrentBlock"
|
|
4
|
-
import setBlockData from './setBlockData'
|
|
5
|
-
|
|
6
|
-
const changeBlockType = (editorState, blockType, data) => {
|
|
7
|
-
const block = getCurrentBlock(editorState)
|
|
8
|
-
const currentBlockType = block.get('type')
|
|
9
|
-
|
|
10
|
-
if (currentBlockType === blockType) {
|
|
11
|
-
return setBlockData(editorState, data )
|
|
12
|
-
} else {
|
|
13
|
-
return data
|
|
14
|
-
? toggleBlockStyle(editorState, { type: blockType, data })
|
|
15
|
-
: RichUtils.toggleBlockType(editorState, blockType)
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export default changeBlockType
|
package/src/getAllEntities.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { getEntitiesForBlock } from "."
|
|
2
|
-
|
|
3
|
-
export const flatten = arr => arr.reduce((a, b) => a.concat(b), [])
|
|
4
|
-
|
|
5
|
-
const getAllEntities = (editorState, type = null) => {
|
|
6
|
-
const content = editorState.getCurrentContent()
|
|
7
|
-
const entities = []
|
|
8
|
-
content.getBlocksAsArray().forEach((contentBlock) => {
|
|
9
|
-
entities.push(getEntitiesForBlock(editorState, contentBlock, type))
|
|
10
|
-
})
|
|
11
|
-
return flatten(entities)
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export default getAllEntities
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
const getEntitiesForBlock = (editorState, contentBlock, type = null) => {
|
|
2
|
-
const content = editorState.getCurrentContent()
|
|
3
|
-
const entities = []
|
|
4
|
-
|
|
5
|
-
let selectedEntity = null
|
|
6
|
-
contentBlock.findEntityRanges(
|
|
7
|
-
(character) => {
|
|
8
|
-
if (character.getEntity() !== null) {
|
|
9
|
-
const entity = content.getEntity(character.getEntity())
|
|
10
|
-
if (!type || (type && entity.getType() === type)) {
|
|
11
|
-
selectedEntity = {
|
|
12
|
-
entityKey: character.getEntity(),
|
|
13
|
-
blockKey: contentBlock.getKey(),
|
|
14
|
-
entity: content.getEntity(character.getEntity()),
|
|
15
|
-
}
|
|
16
|
-
return true
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
return false
|
|
20
|
-
},
|
|
21
|
-
(start, end) => {
|
|
22
|
-
entities.push({ ...selectedEntity, start, end })
|
|
23
|
-
}
|
|
24
|
-
)
|
|
25
|
-
|
|
26
|
-
return entities
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export default getEntitiesForBlock
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { getCurrentBlock, getEntitiesForBlock } from "."
|
|
2
|
-
import { rangesOverlap } from "./utils/numbers"
|
|
3
|
-
|
|
4
|
-
const getEntitiesForCurrentBlock = (editorState, type = null) => getEntitiesForBlock(
|
|
5
|
-
editorState,
|
|
6
|
-
getCurrentBlock(editorState),
|
|
7
|
-
type
|
|
8
|
-
)
|
|
9
|
-
|
|
10
|
-
const getEntitiesForSelection = (editorState, type = null) => {
|
|
11
|
-
const entities = getEntitiesForCurrentBlock(editorState, type)
|
|
12
|
-
const selection = editorState.getSelection()
|
|
13
|
-
const selectionStart = selection.isCollapsed() ? selection.getStartOffset() - 1 : selection.getStartOffset()
|
|
14
|
-
const selectionEnd = selection.getEndOffset()
|
|
15
|
-
|
|
16
|
-
return entities
|
|
17
|
-
.filter(({ start, end }) => rangesOverlap(selectionStart, selectionEnd, start, end))
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export default getEntitiesForSelection
|
package/src/getSelectedText.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
const getSelectedText = (editorState) => {
|
|
2
|
-
const selectionState = editorState.getSelection()
|
|
3
|
-
const anchorKey = selectionState.getAnchorKey()
|
|
4
|
-
const currentContent = editorState.getCurrentContent()
|
|
5
|
-
const currentContentBlock = currentContent.getBlockForKey(anchorKey)
|
|
6
|
-
const start = selectionState.getStartOffset()
|
|
7
|
-
const end = selectionState.getEndOffset()
|
|
8
|
-
return currentContentBlock.getText().slice(start, end)
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export default getSelectedText
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
const getSimilarAdjacentBlocks = (editorState, block, options = {}) => {
|
|
2
|
-
const blockType = block.getType()
|
|
3
|
-
const depth = block.getDepth()
|
|
4
|
-
const variant = block.getData().get('variant')
|
|
5
|
-
|
|
6
|
-
const contentState = editorState.getCurrentContent()
|
|
7
|
-
|
|
8
|
-
const {
|
|
9
|
-
includeDepth = true,
|
|
10
|
-
includeVariant = false,
|
|
11
|
-
onlyForBlockTypes = [],
|
|
12
|
-
} = options
|
|
13
|
-
|
|
14
|
-
const matches = aBlock => aBlock && [
|
|
15
|
-
(aBlock.getType() === blockType),
|
|
16
|
-
(!includeDepth || (aBlock.getDepth() === depth)),
|
|
17
|
-
!includeVariant || (aBlock.getData().get('variant') === variant),
|
|
18
|
-
!onlyForBlockTypes.length || onlyForBlockTypes.includes(aBlock.getType()),
|
|
19
|
-
].every(Boolean)
|
|
20
|
-
|
|
21
|
-
const before = []
|
|
22
|
-
const after = []
|
|
23
|
-
|
|
24
|
-
let cursor = block
|
|
25
|
-
while (cursor) {
|
|
26
|
-
const prev = contentState.getBlockBefore(cursor.getKey())
|
|
27
|
-
if (matches(prev)) {
|
|
28
|
-
before.push(prev)
|
|
29
|
-
cursor = prev
|
|
30
|
-
} else {
|
|
31
|
-
cursor = null
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
cursor = block
|
|
36
|
-
while (cursor) {
|
|
37
|
-
const next = contentState.getBlockAfter(cursor.getKey())
|
|
38
|
-
if (matches(next)) {
|
|
39
|
-
after.push(next)
|
|
40
|
-
cursor = next
|
|
41
|
-
} else {
|
|
42
|
-
cursor = null
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return {
|
|
47
|
-
before: before.reverse(),
|
|
48
|
-
after,
|
|
49
|
-
all: [...before.reverse(), block, ...after],
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export default getSimilarAdjacentBlocks
|
package/src/index.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
export { default as addBlock } from './addBlock'
|
|
2
|
-
export { default as adjustBlockDepth } from './adjustBlockDepth'
|
|
3
|
-
export { default as atEndOfBlock } from './atEndOfBlock'
|
|
4
|
-
export { default as atStartOfBlock } from './atStartOfBlock'
|
|
5
|
-
export { default as changeBlockType } from './changeBlockType'
|
|
6
|
-
export { default as getAllEntities } from './getAllEntities'
|
|
7
|
-
export { default as getCurrentBlock } from './utils/getCurrentBlock'
|
|
8
|
-
export { default as getEntitiesForBlock } from './getEntitiesForBlock'
|
|
9
|
-
export { default as getEntitiesForSelection } from './getEntitiesForSelection'
|
|
10
|
-
export { default as getSelectedText } from './getSelectedText'
|
|
11
|
-
export { default as getSimilarAdjacentBlocks } from './getSimilarAdjacentBlocks'
|
|
12
|
-
export { default as insertAtomicBlock } from './insertAtomicBlock'
|
|
13
|
-
export { default as insertBlock } from './insertBlock'
|
|
14
|
-
export { default as insertEmptyBlock } from './insertEmptyBlock'
|
|
15
|
-
export { default as insertEntity } from './insertEntity'
|
|
16
|
-
export { default as insertNewBlock } from './insertNewBlock'
|
|
17
|
-
export { default as insertText } from './insertText'
|
|
18
|
-
export { default as mergeBlockData } from './mergeBlockData'
|
|
19
|
-
export { default as mergeBlockDataByKey } from './mergeBlockDataByKey'
|
|
20
|
-
export { default as mergeEntityData } from './mergeEntityData'
|
|
21
|
-
export { default as modifyBlock } from './modifyBlock'
|
|
22
|
-
export { default as modifyBlockByKey } from './modifyBlockByKey'
|
|
23
|
-
export { default as moveCaretAfterBlock } from './moveCaretAfterBlock'
|
|
24
|
-
export { default as moveCaretToEnd } from './moveCaretToEnd'
|
|
25
|
-
export { default as removeBlock } from './removeBlock'
|
|
26
|
-
export { default as removeBlockStyle } from './removeBlockStyle'
|
|
27
|
-
export { default as removeEntity } from './removeEntity'
|
|
28
|
-
export { default as removeInlineStyles } from './removeInlineStyles'
|
|
29
|
-
export { default as resetBlock } from './resetBlock'
|
|
30
|
-
export { default as selectAll} from './selectAll'
|
|
31
|
-
export { default as selectBlockByKey } from './selectBlockByKey'
|
|
32
|
-
export { default as setBlockData } from './setBlockData'
|
|
33
|
-
export { default as toggleBlockStyle } from './toggleBlockStyle'
|
|
34
|
-
export { default as toggleBlockType } from './toggleBlockType'
|
|
35
|
-
export { default as toggleEntity } from './toggleEntity'
|
|
36
|
-
export { default as toggleInlineStyle } from './toggleInlineStyle'
|
|
37
|
-
export { default as trimEditorState } from './trimEditorState'
|
|
38
|
-
|
|
39
|
-
export const DRAFTJS_BLOCK_KEY = 'DRAFTJS_BLOCK_KEY'
|
|
40
|
-
export const HANDLED = 'handled'
|
|
41
|
-
export const NOT_HANDLED = 'not-handled'
|
package/src/insertAtomicBlock.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
|
|
3
|
-
import { AtomicBlockUtils } from 'draft-js'
|
|
4
|
-
|
|
5
|
-
import type { EditorState } from 'draft-js'
|
|
6
|
-
import type { DraftEntityType } from 'draft-js/lib/DraftEntityType'
|
|
7
|
-
|
|
8
|
-
const insertAtomicBlock = (
|
|
9
|
-
editorState: EditorState,
|
|
10
|
-
entityType: DraftEntityType,
|
|
11
|
-
mutability: 'IMMUTABLE' | 'MUTABLE' | 'SEGMENTED',
|
|
12
|
-
data?: { [id: string]: any },
|
|
13
|
-
character?: string = ' '
|
|
14
|
-
): EditorState => {
|
|
15
|
-
const entityKey = editorState.getCurrentContent().createEntity(entityType, mutability, data).getLastCreatedEntityKey()
|
|
16
|
-
return AtomicBlockUtils.insertAtomicBlock(
|
|
17
|
-
editorState,
|
|
18
|
-
entityKey,
|
|
19
|
-
character
|
|
20
|
-
)
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export default insertAtomicBlock
|
package/src/insertBlock.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { ContentBlock, EditorState, genKey } from "draft-js"
|
|
2
|
-
import { List } from "immutable"
|
|
3
|
-
|
|
4
|
-
const insertBlock = (direction, editorState, targetBlock, newBlockType) => {
|
|
5
|
-
const selection = editorState.getSelection()
|
|
6
|
-
const contentState = editorState.getCurrentContent()
|
|
7
|
-
const currentBlock = targetBlock || contentState.getBlockForKey(selection.getEndKey())
|
|
8
|
-
|
|
9
|
-
const blockMap = contentState.getBlockMap()
|
|
10
|
-
// Split the blocks
|
|
11
|
-
const blocksBefore = blockMap.toSeq().takeUntil(v => v === currentBlock)
|
|
12
|
-
const blocksAfter = blockMap.toSeq().skipUntil(v => v === currentBlock).rest()
|
|
13
|
-
const newBlockKey = genKey()
|
|
14
|
-
const newBlocks = direction === 'before' ? [
|
|
15
|
-
[newBlockKey, new ContentBlock({
|
|
16
|
-
key: newBlockKey,
|
|
17
|
-
type: newBlockType,
|
|
18
|
-
text: '',
|
|
19
|
-
characterList: List(),
|
|
20
|
-
})],
|
|
21
|
-
[currentBlock.getKey(), currentBlock],
|
|
22
|
-
] : [
|
|
23
|
-
[currentBlock.getKey(), currentBlock],
|
|
24
|
-
[newBlockKey, new ContentBlock({
|
|
25
|
-
key: newBlockKey,
|
|
26
|
-
type: newBlockType,
|
|
27
|
-
text: '',
|
|
28
|
-
characterList: List(),
|
|
29
|
-
})],
|
|
30
|
-
]
|
|
31
|
-
const newBlockMap = blocksBefore.concat(newBlocks, blocksAfter).toOrderedMap()
|
|
32
|
-
const newContentState = contentState.merge({
|
|
33
|
-
blockMap: newBlockMap,
|
|
34
|
-
selectionBefore: selection,
|
|
35
|
-
selectionAfter: selection,
|
|
36
|
-
})
|
|
37
|
-
return EditorState.push(editorState, newContentState, 'insert-fragment')
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export default insertBlock
|
package/src/insertEmptyBlock.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
|
|
3
|
-
import insertNewBlock from './insertNewBlock'
|
|
4
|
-
|
|
5
|
-
import type { EditorState } from 'draft-js'
|
|
6
|
-
import type { DraftBlockType } from 'draft-js/lib/DraftBlockType'
|
|
7
|
-
|
|
8
|
-
const insertEmptyBlock = (editorState: EditorState, blockType?: DraftBlockType = 'unstyled'): EditorState => {
|
|
9
|
-
return insertNewBlock(editorState, blockType)
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export default insertEmptyBlock
|
package/src/insertEntity.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { SelectionState } from "draft-js"
|
|
2
|
-
import { Modifier } from "draft-js"
|
|
3
|
-
import { EditorState } from "draft-js"
|
|
4
|
-
import { getSelectedText } from "."
|
|
5
|
-
|
|
6
|
-
const insertEntity = (editorState, text, type, data, mutability = 'MUTABLE') => {
|
|
7
|
-
const selectedText = getSelectedText(editorState)
|
|
8
|
-
const contentState = editorState.getCurrentContent()
|
|
9
|
-
const selection = editorState.getSelection()
|
|
10
|
-
const textWithSpace = text.concat(' ')
|
|
11
|
-
const newContent = selectedText ? Modifier.replaceText(
|
|
12
|
-
contentState,
|
|
13
|
-
selection,
|
|
14
|
-
text
|
|
15
|
-
) : Modifier.insertText(
|
|
16
|
-
contentState,
|
|
17
|
-
selection,
|
|
18
|
-
textWithSpace
|
|
19
|
-
)
|
|
20
|
-
|
|
21
|
-
const newContentWithEntity = newContent.createEntity(
|
|
22
|
-
type,
|
|
23
|
-
mutability,
|
|
24
|
-
data,
|
|
25
|
-
false
|
|
26
|
-
)
|
|
27
|
-
|
|
28
|
-
const entityKey = newContentWithEntity.getLastCreatedEntityKey()
|
|
29
|
-
|
|
30
|
-
const anchorOffset = selection.getAnchorOffset()
|
|
31
|
-
const newSelection = new SelectionState({
|
|
32
|
-
anchorKey: selection.getAnchorKey(),
|
|
33
|
-
anchorOffset,
|
|
34
|
-
focusKey: selection.getAnchorKey(),
|
|
35
|
-
focusOffset: anchorOffset + text.length,
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
const newContentWithLink = Modifier.applyEntity(
|
|
39
|
-
newContentWithEntity,
|
|
40
|
-
newSelection,
|
|
41
|
-
entityKey
|
|
42
|
-
)
|
|
43
|
-
|
|
44
|
-
const withLinkText = EditorState.push(
|
|
45
|
-
editorState,
|
|
46
|
-
newContentWithLink,
|
|
47
|
-
'insert-characters'
|
|
48
|
-
)
|
|
49
|
-
|
|
50
|
-
return EditorState.forceSelection(
|
|
51
|
-
withLinkText,
|
|
52
|
-
newContent.getSelectionAfter()
|
|
53
|
-
)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export default insertEntity
|
package/src/insertNewBlock.js
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
|
|
3
|
-
import { EditorState, ContentBlock, CharacterMetadata, genKey } from 'draft-js'
|
|
4
|
-
import { Map, List, Repeat } from 'immutable'
|
|
5
|
-
import getCurrentBlock from './utils/getCurrentBlock'
|
|
6
|
-
|
|
7
|
-
import type { DraftBlockType } from 'draft-js/lib/DraftBlockType'
|
|
8
|
-
|
|
9
|
-
const insertNewBlock = (
|
|
10
|
-
editorState: EditorState,
|
|
11
|
-
blockType: DraftBlockType = 'unstyled',
|
|
12
|
-
text: string = '',
|
|
13
|
-
data: Object = {}
|
|
14
|
-
): EditorState => {
|
|
15
|
-
const content = editorState.getCurrentContent()
|
|
16
|
-
const selection = editorState.getSelection()
|
|
17
|
-
const currentBlock = getCurrentBlock(editorState)
|
|
18
|
-
const emptyBlockKey = genKey()
|
|
19
|
-
const emptyBlock = new ContentBlock({
|
|
20
|
-
key: emptyBlockKey,
|
|
21
|
-
type: blockType,
|
|
22
|
-
text,
|
|
23
|
-
characterList: List(Repeat(CharacterMetadata.create(), text.length)),
|
|
24
|
-
data: Map().merge(data),
|
|
25
|
-
})
|
|
26
|
-
const blockMap = content.getBlockMap()
|
|
27
|
-
const blocksBefore = blockMap.toSeq().takeUntil(value => value === currentBlock)
|
|
28
|
-
const blocksAfter = blockMap.toSeq().skipUntil(value => value === currentBlock).rest()
|
|
29
|
-
const augmentedBlocks = [
|
|
30
|
-
[
|
|
31
|
-
currentBlock.getKey(),
|
|
32
|
-
currentBlock,
|
|
33
|
-
],
|
|
34
|
-
[
|
|
35
|
-
emptyBlockKey,
|
|
36
|
-
emptyBlock,
|
|
37
|
-
],
|
|
38
|
-
]
|
|
39
|
-
const newBlocks = blocksBefore.concat(augmentedBlocks, blocksAfter).toOrderedMap()
|
|
40
|
-
const focusKey = emptyBlockKey
|
|
41
|
-
const newContent = content.merge({
|
|
42
|
-
blockMap: newBlocks,
|
|
43
|
-
selectionBefore: selection,
|
|
44
|
-
selectionAfter: selection.merge({
|
|
45
|
-
anchorKey: focusKey,
|
|
46
|
-
anchorOffset: 0,
|
|
47
|
-
focusKey,
|
|
48
|
-
focusOffset: 0,
|
|
49
|
-
isBackward: false,
|
|
50
|
-
}),
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
const newState = EditorState.push(
|
|
54
|
-
editorState,
|
|
55
|
-
newContent,
|
|
56
|
-
'split-block'
|
|
57
|
-
)
|
|
58
|
-
return EditorState.forceSelection(newState, newContent.getSelectionAfter())
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export default insertNewBlock
|