@studysync/draft-js-modifiers 0.4.14 → 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.
Files changed (71) hide show
  1. package/README.md +18 -5
  2. package/es/index.js +3 -11
  3. package/es/insertBlock.js +46 -0
  4. package/index.js +3 -11
  5. package/insertBlock.js +46 -0
  6. package/package.json +10 -4
  7. package/es/insertBlockAfter.js +0 -72
  8. package/es/insertBlockBefore.js +0 -72
  9. package/flow/stub/__dev__.js +0 -3
  10. package/insertBlockAfter.js +0 -72
  11. package/insertBlockBefore.js +0 -72
  12. package/script/add-module.js +0 -48
  13. package/src/addBlock.js +0 -98
  14. package/src/adjustBlockDepth.js +0 -21
  15. package/src/atEndOfBlock.js +0 -10
  16. package/src/atStartOfBlock.js +0 -7
  17. package/src/changeBlockType.js +0 -19
  18. package/src/getAllEntities.js +0 -14
  19. package/src/getEntitiesForBlock.js +0 -29
  20. package/src/getEntitiesForSelection.js +0 -20
  21. package/src/getSelectedText.js +0 -11
  22. package/src/getSimilarAdjacentBlocks.js +0 -53
  23. package/src/index.js +0 -42
  24. package/src/insertAtomicBlock.js +0 -23
  25. package/src/insertBlockAfter.js +0 -42
  26. package/src/insertBlockBefore.js +0 -42
  27. package/src/insertEmptyBlock.js +0 -12
  28. package/src/insertEntity.js +0 -56
  29. package/src/insertNewBlock.js +0 -61
  30. package/src/insertText.js +0 -23
  31. package/src/mergeBlockData.js +0 -13
  32. package/src/mergeBlockDataByKey.js +0 -12
  33. package/src/mergeEntityData.js +0 -13
  34. package/src/modifyBlock.js +0 -20
  35. package/src/modifyBlockByKey.js +0 -19
  36. package/src/moveCaretAfterBlock.js +0 -26
  37. package/src/moveCaretToEnd.js +0 -20
  38. package/src/removeBlock.js +0 -43
  39. package/src/removeBlockStyle.js +0 -17
  40. package/src/removeEntity.js +0 -38
  41. package/src/removeInlineStyles.js +0 -29
  42. package/src/resetBlock.js +0 -29
  43. package/src/selectAll.js +0 -15
  44. package/src/selectBlockByKey.js +0 -20
  45. package/src/setBlockData.js +0 -14
  46. package/src/toggleBlockStyle.js +0 -33
  47. package/src/toggleBlockType.js +0 -12
  48. package/src/toggleEntity.js +0 -11
  49. package/src/toggleInlineStyle.js +0 -11
  50. package/src/trimEditorState.js +0 -41
  51. package/src/utils/getCurrentBlock.js +0 -10
  52. package/src/utils/numbers.js +0 -3
  53. package/test/adjustBlockDepth.js +0 -45
  54. package/test/export.js +0 -14
  55. package/test/fixtures/createEditorState.js +0 -12
  56. package/test/fixtures/omitBlockKeysFromRawContent.js +0 -9
  57. package/test/insertAtomicBlock.js +0 -89
  58. package/test/insertEmptyBlock.js +0 -65
  59. package/test/insertNewBlock.js +0 -68
  60. package/test/insertText.js +0 -58
  61. package/test/mergeBlockData.js +0 -40
  62. package/test/mergeBlockDataByKey.js +0 -103
  63. package/test/mergeEntityData.js +0 -49
  64. package/test/modifyBlock.js +0 -52
  65. package/test/modifyBlockByKey.js +0 -91
  66. package/test/removeBlockStyle.js +0 -52
  67. package/test/removeInlineStyles.js +0 -68
  68. package/test/resetBlock.js +0 -83
  69. package/test/toggleBlockType.js +0 -4
  70. package/test/toggleEntity.js +0 -4
  71. package/test/toggleInlineStyle.js +0 -4
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
@@ -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
@@ -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
@@ -1,7 +0,0 @@
1
- const atStartOfBlock = (editorState) => {
2
- const selection = editorState.getSelection()
3
- const endOffset = selection.getEndOffset()
4
- return endOffset === 0
5
- }
6
-
7
- export default atStartOfBlock
@@ -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
@@ -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
@@ -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,42 +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 insertBlockAfter } from './insertBlockAfter'
14
- export { default as insertBlockBefore } from './insertBlockBefore'
15
- export { default as insertEmptyBlock } from './insertEmptyBlock'
16
- export { default as insertEntity } from './insertEntity'
17
- export { default as insertNewBlock } from './insertNewBlock'
18
- export { default as insertText } from './insertText'
19
- export { default as mergeBlockData } from './mergeBlockData'
20
- export { default as mergeBlockDataByKey } from './mergeBlockDataByKey'
21
- export { default as mergeEntityData } from './mergeEntityData'
22
- export { default as modifyBlock } from './modifyBlock'
23
- export { default as modifyBlockByKey } from './modifyBlockByKey'
24
- export { default as moveCaretAfterBlock } from './moveCaretAfterBlock'
25
- export { default as moveCaretToEnd } from './moveCaretToEnd'
26
- export { default as removeBlock } from './removeBlock'
27
- export { default as removeBlockStyle } from './removeBlockStyle'
28
- export { default as removeEntity } from './removeEntity'
29
- export { default as removeInlineStyles } from './removeInlineStyles'
30
- export { default as resetBlock } from './resetBlock'
31
- export { default as selectAll} from './selectAll'
32
- export { default as selectBlockByKey } from './selectBlockByKey'
33
- export { default as setBlockData } from './setBlockData'
34
- export { default as toggleBlockStyle } from './toggleBlockStyle'
35
- export { default as toggleBlockType } from './toggleBlockType'
36
- export { default as toggleEntity } from './toggleEntity'
37
- export { default as toggleInlineStyle } from './toggleInlineStyle'
38
- export { default as trimEditorState } from './trimEditorState'
39
-
40
- export const DRAFTJS_BLOCK_KEY = 'DRAFTJS_BLOCK_KEY'
41
- export const HANDLED = 'handled'
42
- export const NOT_HANDLED = 'not-handled'
@@ -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
@@ -1,42 +0,0 @@
1
- import { EditorState } from "draft-js"
2
- import { genKey } from "draft-js"
3
- import { ContentBlock } from "draft-js"
4
- import { ContentState } from "draft-js"
5
- import Immutable from "immutable"
6
-
7
- const insertBlockAfter = (editorState, keyBefore, blockParams) => {
8
- const newBlock = new ContentBlock({
9
- key: genKey(),
10
- type: 'unstyled',
11
- text: '',
12
- ...blockParams,
13
- })
14
-
15
- let modified = false
16
-
17
- const contentState = editorState.getCurrentContent()
18
- const oldBlockMap = contentState.getBlockMap()
19
- const newBlockMap = Immutable.OrderedMap().withMutations((map) => {
20
- for (const [k, v] of oldBlockMap.entries()) {
21
- map.set(k, v)
22
-
23
- if (keyBefore === k) {
24
- map.set(newBlock.key, newBlock)
25
- modified=true
26
- }
27
- }
28
- })
29
-
30
- return modified
31
- ? EditorState.push(
32
- editorState,
33
- ContentState
34
- .createFromBlockArray(Array.from(newBlockMap.values()))
35
- .set('selectionBefore', contentState.getSelectionBefore())
36
- .set('selectionAfter', contentState.getSelectionAfter()),
37
- 'insert-fragment'
38
- )
39
- : editorState
40
- }
41
-
42
- export default insertBlockAfter
@@ -1,42 +0,0 @@
1
- import { EditorState } from "draft-js"
2
- import { genKey } from "draft-js"
3
- import { ContentBlock } from "draft-js"
4
- import { ContentState } from "draft-js"
5
- import Immutable from "immutable"
6
-
7
- const insertBlockBefore = (editorState, keyBefore, blockParams) => {
8
- const newBlock = new ContentBlock({
9
- key: genKey(),
10
- type: 'unstyled',
11
- text: '',
12
- ...blockParams,
13
- })
14
-
15
- let modified = false
16
-
17
- const contentState = editorState.getCurrentContent()
18
- const oldBlockMap = contentState.getBlockMap()
19
- const newBlockMap = Immutable.OrderedMap().withMutations((map) => {
20
- for (const [k, v] of oldBlockMap.entries()) {
21
- if (keyBefore === k) {
22
- map.set(newBlock.key, newBlock)
23
- modified = true
24
- }
25
-
26
- map.set(k, v)
27
- }
28
- })
29
-
30
- return modified
31
- ? EditorState.push(
32
- editorState,
33
- ContentState
34
- .createFromBlockArray(Array.from(newBlockMap.values()))
35
- .set('selectionBefore', contentState.getSelectionBefore())
36
- .set('selectionAfter', contentState.getSelectionAfter()),
37
- 'insert-fragment'
38
- )
39
- : editorState
40
- }
41
-
42
- export default insertBlockBefore
@@ -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
@@ -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
@@ -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
package/src/insertText.js DELETED
@@ -1,23 +0,0 @@
1
- // @flow
2
-
3
- import { EditorState, Modifier } from 'draft-js'
4
-
5
- const insertText = (editorState: EditorState, text: string, entity?: ?string = null): EditorState => {
6
- const selection = editorState.getSelection()
7
- const content = editorState.getCurrentContent()
8
- const newContent = Modifier[selection.isCollapsed() ? 'insertText' : 'replaceText'](
9
- content,
10
- selection,
11
- text,
12
- editorState.getCurrentInlineStyle(),
13
- entity
14
- )
15
-
16
- return EditorState.push(
17
- editorState,
18
- newContent,
19
- 'insert-fragment'
20
- )
21
- }
22
-
23
- export default insertText
@@ -1,13 +0,0 @@
1
- // @flow
2
-
3
- import { EditorState, Modifier } from 'draft-js'
4
- import { Map } from 'immutable'
5
-
6
- const mergeBlockData = (editorState: EditorState, data: { [id: string]: any }): EditorState => {
7
- const content = editorState.getCurrentContent()
8
- const selection = editorState.getSelection()
9
- const newContent = Modifier.mergeBlockData(content, selection, Map(data))
10
- return EditorState.push(editorState, newContent, 'change-block-data')
11
- }
12
-
13
- export default mergeBlockData
@@ -1,12 +0,0 @@
1
- // @flow
2
-
3
- import { EditorState } from 'draft-js'
4
-
5
- const mergeBlockDataByKey = (editorState: EditorState, blockKey: string, data: { [id: string]: any }): EditorState => {
6
- const content = editorState.getCurrentContent()
7
- const updatedBlock = content.getBlockForKey(blockKey).mergeIn(['data'], data)
8
- const blockMap = content.getBlockMap().merge({ [blockKey]: updatedBlock })
9
- return EditorState.push(editorState, content.merge({ blockMap }), 'change-block-data')
10
- }
11
-
12
- export default mergeBlockDataByKey
@@ -1,13 +0,0 @@
1
- // @flow
2
-
3
- import { EditorState } from 'draft-js'
4
-
5
- const mergeEntityData = (editorState: EditorState, entityKey: string, data: Object): EditorState => {
6
- const newContentState = editorState.getCurrentContent().mergeEntityData(entityKey, data)
7
- return EditorState.forceSelection(
8
- EditorState.push(editorState, newContentState, 'apply-entity'),
9
- editorState.getSelection()
10
- )
11
- }
12
-
13
- export default mergeEntityData
@@ -1,20 +0,0 @@
1
- // @flow
2
-
3
- import { EditorState } from 'draft-js'
4
- import modifyBlockForContentState from 'draft-js/lib/modifyBlockForContentState'
5
-
6
- import type { ContentBlock } from 'draft-js'
7
-
8
- const modifyBlock = (editorState: EditorState, blockData: ContentBlock): EditorState => {
9
- const content = editorState.getCurrentContent()
10
- const selection = editorState.getSelection()
11
- const newContent = modifyBlockForContentState(content, selection, block => block.merge(blockData))
12
-
13
- return EditorState.push(
14
- editorState,
15
- newContent,
16
- 'split-block' // TODO: will this do ?
17
- )
18
- }
19
-
20
- export default modifyBlock