@studysync/draft-js-modifiers 0.4.15 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/README.md +23 -5
  2. package/package.json +11 -4
  3. package/flow/stub/__dev__.js +0 -3
  4. package/script/add-module.js +0 -48
  5. package/src/addBlock.js +0 -98
  6. package/src/adjustBlockDepth.js +0 -21
  7. package/src/atEndOfBlock.js +0 -10
  8. package/src/atStartOfBlock.js +0 -7
  9. package/src/changeBlockType.js +0 -19
  10. package/src/getAllEntities.js +0 -14
  11. package/src/getEntitiesForBlock.js +0 -29
  12. package/src/getEntitiesForSelection.js +0 -20
  13. package/src/getSelectedText.js +0 -11
  14. package/src/getSimilarAdjacentBlocks.js +0 -53
  15. package/src/index.js +0 -41
  16. package/src/insertAtomicBlock.js +0 -23
  17. package/src/insertBlock.js +0 -40
  18. package/src/insertEmptyBlock.js +0 -12
  19. package/src/insertEntity.js +0 -56
  20. package/src/insertNewBlock.js +0 -61
  21. package/src/insertText.js +0 -23
  22. package/src/mergeBlockData.js +0 -13
  23. package/src/mergeBlockDataByKey.js +0 -12
  24. package/src/mergeEntityData.js +0 -13
  25. package/src/modifyBlock.js +0 -20
  26. package/src/modifyBlockByKey.js +0 -19
  27. package/src/moveCaretAfterBlock.js +0 -26
  28. package/src/moveCaretToEnd.js +0 -20
  29. package/src/removeBlock.js +0 -43
  30. package/src/removeBlockStyle.js +0 -17
  31. package/src/removeEntity.js +0 -38
  32. package/src/removeInlineStyles.js +0 -29
  33. package/src/resetBlock.js +0 -29
  34. package/src/selectAll.js +0 -15
  35. package/src/selectBlockByKey.js +0 -20
  36. package/src/setBlockData.js +0 -14
  37. package/src/toggleBlockStyle.js +0 -33
  38. package/src/toggleBlockType.js +0 -12
  39. package/src/toggleEntity.js +0 -11
  40. package/src/toggleInlineStyle.js +0 -11
  41. package/src/trimEditorState.js +0 -41
  42. package/src/utils/getCurrentBlock.js +0 -10
  43. package/src/utils/numbers.js +0 -3
  44. package/test/adjustBlockDepth.js +0 -45
  45. package/test/export.js +0 -14
  46. package/test/fixtures/createEditorState.js +0 -12
  47. package/test/fixtures/omitBlockKeysFromRawContent.js +0 -9
  48. package/test/insertAtomicBlock.js +0 -89
  49. package/test/insertEmptyBlock.js +0 -65
  50. package/test/insertNewBlock.js +0 -68
  51. package/test/insertText.js +0 -58
  52. package/test/mergeBlockData.js +0 -40
  53. package/test/mergeBlockDataByKey.js +0 -103
  54. package/test/mergeEntityData.js +0 -49
  55. package/test/modifyBlock.js +0 -52
  56. package/test/modifyBlockByKey.js +0 -91
  57. package/test/removeBlockStyle.js +0 -52
  58. package/test/removeInlineStyles.js +0 -68
  59. package/test/resetBlock.js +0 -83
  60. package/test/toggleBlockType.js +0 -4
  61. package/test/toggleEntity.js +0 -4
  62. package/test/toggleInlineStyle.js +0 -4
package/README.md CHANGED
@@ -8,17 +8,35 @@
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
12
12
 
13
13
  # or
14
14
 
15
- npm i draft-js-modifiers
15
+ npm i @studysync/draft-js-modifiers draft-js
16
+ ```
17
+
18
+ This package ships with `immutable@3.8.3`, the patched `3.x` line for the prototype-pollution advisory described in the GitHub advisory for [`immutable`](https://github.com/advisories/GHSA-wf6x-7x77-mvgw).
19
+
20
+ When your app also uses other legacy Draft.js packages, force a single patched `immutable` 3.x version at the app root:
21
+
22
+ ```json
23
+ {
24
+ "overrides": {
25
+ "immutable": "3.8.3",
26
+ "draft-js": {
27
+ "immutable": "3.8.3"
28
+ }
29
+ },
30
+ "resolutions": {
31
+ "immutable": "3.8.3"
32
+ }
33
+ }
16
34
  ```
17
35
 
18
36
  ## Usage
19
37
 
20
38
  ```js
21
- import * as Modifiers from 'draft-js-modifiers'
39
+ import * as Modifiers from '@studysync/draft-js-modifiers'
22
40
 
23
41
  const newEditorState = Modifiers.mergeBlockData(currentEditorState, { foo: 1 })
24
42
  ```
@@ -26,10 +44,10 @@ const newEditorState = Modifiers.mergeBlockData(currentEditorState, { foo: 1 })
26
44
  ### Moduler importing
27
45
 
28
46
  ```js
29
- import adjustBlockDepth from 'draft-js-modifiers/adjustBlockDepth'
47
+ import adjustBlockDepth from '@studysync/draft-js-modifiers/adjustBlockDepth'
30
48
 
31
49
  // Support Tree Shaking for webpack, rollup.js
32
- import { insertText } from 'draft-js-modifiers'
50
+ import { insertText } from '@studysync/draft-js-modifiers'
33
51
  ```
34
52
 
35
53
  ## Methods
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@studysync/draft-js-modifiers",
3
- "version": "0.4.15",
3
+ "version": "0.5.1",
4
4
  "description": "Modular state modifiers for Draft.js",
5
5
  "main": "index.js",
6
6
  "module": "es/index.js",
@@ -40,9 +40,11 @@
40
40
  "@babel/register"
41
41
  ]
42
42
  },
43
+ "peerDependencies": {
44
+ "draft-js": "^0.11.7"
45
+ },
43
46
  "dependencies": {
44
- "draft-js": "^0.11.7",
45
- "immutable": "~3.7.4"
47
+ "immutable": "3.8.3"
46
48
  },
47
49
  "devDependencies": {
48
50
  "@babel/cli": "7.17.0",
@@ -53,6 +55,7 @@
53
55
  "ava": "^0.25.0",
54
56
  "babel-eslint": "^8.2.3",
55
57
  "coveralls": "^3.0.1",
58
+ "draft-js": "0.11.7",
56
59
  "eslint": "^4.19.1",
57
60
  "eslint-plugin-ava": "^4.5.1",
58
61
  "eslint-plugin-babel": "^5.1.0",
@@ -62,7 +65,11 @@
62
65
  "react": ">=17",
63
66
  "react-dom": ">=17"
64
67
  },
68
+ "overrides": {
69
+ "immutable": "3.8.3"
70
+ },
65
71
  "resolutions": {
66
- "graceful-fs": "4.2.4"
72
+ "graceful-fs": "4.2.4",
73
+ "immutable": "3.8.3"
67
74
  }
68
75
  }
@@ -1,3 +0,0 @@
1
- // @flow
2
-
3
- declare var __DEV__: boolean
@@ -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
@@ -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,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'
@@ -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,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
@@ -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