@studysync/draft-js-modifiers 0.4.12 → 0.4.14
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/es/insertBlockAfter.js +3 -1
- package/es/insertBlockBefore.js +3 -1
- package/flow/stub/__dev__.js +3 -0
- package/insertBlockAfter.js +3 -1
- package/insertBlockBefore.js +3 -1
- package/package.json +4 -1
- package/script/add-module.js +48 -0
- package/src/addBlock.js +98 -0
- package/src/adjustBlockDepth.js +21 -0
- package/src/atEndOfBlock.js +10 -0
- package/src/atStartOfBlock.js +7 -0
- package/src/changeBlockType.js +19 -0
- package/src/getAllEntities.js +14 -0
- package/src/getEntitiesForBlock.js +29 -0
- package/src/getEntitiesForSelection.js +20 -0
- package/src/getSelectedText.js +11 -0
- package/src/getSimilarAdjacentBlocks.js +53 -0
- package/src/index.js +42 -0
- package/src/insertAtomicBlock.js +23 -0
- package/src/insertBlockAfter.js +42 -0
- package/src/insertBlockBefore.js +42 -0
- package/src/insertEmptyBlock.js +12 -0
- package/src/insertEntity.js +56 -0
- package/src/insertNewBlock.js +61 -0
- package/src/insertText.js +23 -0
- package/src/mergeBlockData.js +13 -0
- package/src/mergeBlockDataByKey.js +12 -0
- package/src/mergeEntityData.js +13 -0
- package/src/modifyBlock.js +20 -0
- package/src/modifyBlockByKey.js +19 -0
- package/src/moveCaretAfterBlock.js +26 -0
- package/src/moveCaretToEnd.js +20 -0
- package/src/removeBlock.js +43 -0
- package/src/removeBlockStyle.js +17 -0
- package/src/removeEntity.js +38 -0
- package/src/removeInlineStyles.js +29 -0
- package/src/resetBlock.js +29 -0
- package/src/selectAll.js +15 -0
- package/src/selectBlockByKey.js +20 -0
- package/src/setBlockData.js +14 -0
- package/src/toggleBlockStyle.js +33 -0
- package/src/toggleBlockType.js +12 -0
- package/src/toggleEntity.js +11 -0
- package/src/toggleInlineStyle.js +11 -0
- package/src/trimEditorState.js +41 -0
- package/src/utils/getCurrentBlock.js +10 -0
- package/src/utils/numbers.js +3 -0
- package/test/adjustBlockDepth.js +45 -0
- package/test/export.js +14 -0
- package/test/fixtures/createEditorState.js +12 -0
- package/test/fixtures/omitBlockKeysFromRawContent.js +9 -0
- package/test/insertAtomicBlock.js +89 -0
- package/test/insertEmptyBlock.js +65 -0
- package/test/insertNewBlock.js +68 -0
- package/test/insertText.js +58 -0
- package/test/mergeBlockData.js +40 -0
- package/test/mergeBlockDataByKey.js +103 -0
- package/test/mergeEntityData.js +49 -0
- package/test/modifyBlock.js +52 -0
- package/test/modifyBlockByKey.js +91 -0
- package/test/removeBlockStyle.js +52 -0
- package/test/removeInlineStyles.js +68 -0
- package/test/resetBlock.js +83 -0
- package/test/toggleBlockType.js +4 -0
- package/test/toggleEntity.js +4 -0
- package/test/toggleInlineStyle.js +4 -0
package/test/export.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import test from 'ava'
|
|
2
|
+
import fs from 'fs'
|
|
3
|
+
import path from 'path'
|
|
4
|
+
import * as Modifiers from '../src'
|
|
5
|
+
|
|
6
|
+
test('export all modules', t => {
|
|
7
|
+
const files = fs.readdirSync(path.resolve(__dirname, '..', 'src'))
|
|
8
|
+
.filter(name => /\.js$/.test(name) && name !== 'index.js')
|
|
9
|
+
.map(name => name.replace(/\.js$/, ''))
|
|
10
|
+
|
|
11
|
+
t.true(
|
|
12
|
+
files.every(file => Object.keys(Modifiers).includes(file))
|
|
13
|
+
)
|
|
14
|
+
})
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import Draft, { EditorState, SelectionState } from 'draft-js'
|
|
2
|
+
|
|
3
|
+
const createEditorState = (rawContentState, rawSelectionState) => {
|
|
4
|
+
return EditorState.forceSelection(
|
|
5
|
+
EditorState.createWithContent(
|
|
6
|
+
Draft.convertFromRaw(rawContentState)
|
|
7
|
+
),
|
|
8
|
+
new SelectionState(rawSelectionState)
|
|
9
|
+
)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export default createEditorState
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import test from 'ava'
|
|
2
|
+
import { convertToRaw } from 'draft-js'
|
|
3
|
+
import createEditorState from './fixtures/createEditorState'
|
|
4
|
+
import omitBlockKeysFromRawContent from './fixtures/omitBlockKeysFromRawContent'
|
|
5
|
+
|
|
6
|
+
import insertAtomicBlock from '../src/insertAtomicBlock'
|
|
7
|
+
|
|
8
|
+
test('insertAtomicBlock', t => {
|
|
9
|
+
const beforeRawContentState = {
|
|
10
|
+
entityMap: {},
|
|
11
|
+
blocks: [{
|
|
12
|
+
key: 'item1',
|
|
13
|
+
text: '',
|
|
14
|
+
type: 'unstyled',
|
|
15
|
+
depth: 0,
|
|
16
|
+
inlineStyleRanges: [],
|
|
17
|
+
entityRanges: [],
|
|
18
|
+
data: {},
|
|
19
|
+
}],
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const afterRawContentState = {
|
|
23
|
+
entityMap: {
|
|
24
|
+
0: {
|
|
25
|
+
data: {
|
|
26
|
+
foo: 'bar',
|
|
27
|
+
},
|
|
28
|
+
mutability: 'IMMUTABLE',
|
|
29
|
+
type: 'buz',
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
blocks: [
|
|
33
|
+
{
|
|
34
|
+
key: 'item1',
|
|
35
|
+
text: '',
|
|
36
|
+
type: 'unstyled',
|
|
37
|
+
depth: 0,
|
|
38
|
+
inlineStyleRanges: [],
|
|
39
|
+
entityRanges: [],
|
|
40
|
+
data: {},
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
key: 'item2',
|
|
44
|
+
text: ' ',
|
|
45
|
+
type: 'atomic',
|
|
46
|
+
depth: 0,
|
|
47
|
+
inlineStyleRanges: [],
|
|
48
|
+
entityRanges: [{
|
|
49
|
+
key: 0,
|
|
50
|
+
length: 1,
|
|
51
|
+
offset: 0,
|
|
52
|
+
}],
|
|
53
|
+
data: {},
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
key: 'item3',
|
|
57
|
+
text: '',
|
|
58
|
+
type: 'unstyled',
|
|
59
|
+
depth: 0,
|
|
60
|
+
inlineStyleRanges: [],
|
|
61
|
+
entityRanges: [],
|
|
62
|
+
data: {},
|
|
63
|
+
},
|
|
64
|
+
],
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const editorState = createEditorState(
|
|
68
|
+
beforeRawContentState,
|
|
69
|
+
{
|
|
70
|
+
anchorKey: 'item1',
|
|
71
|
+
anchorOffset: 0,
|
|
72
|
+
focusKey: 'item1',
|
|
73
|
+
focusOffset: 0,
|
|
74
|
+
isBackward: false,
|
|
75
|
+
hasFocus: true,
|
|
76
|
+
}
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
const newEditorState = insertAtomicBlock(editorState, 'buz', 'IMMUTABLE', { foo: 'bar' })
|
|
80
|
+
t.not(newEditorState, editorState)
|
|
81
|
+
t.deepEqual(
|
|
82
|
+
omitBlockKeysFromRawContent(
|
|
83
|
+
convertToRaw(newEditorState.getCurrentContent())
|
|
84
|
+
),
|
|
85
|
+
omitBlockKeysFromRawContent(
|
|
86
|
+
afterRawContentState
|
|
87
|
+
)
|
|
88
|
+
)
|
|
89
|
+
})
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import test from 'ava'
|
|
2
|
+
import Draft from 'draft-js'
|
|
3
|
+
import createEditorState from './fixtures/createEditorState'
|
|
4
|
+
import omitBlockKeysFromRawContent from './fixtures/omitBlockKeysFromRawContent'
|
|
5
|
+
|
|
6
|
+
import insertEmptyBlock from '../src/insertEmptyBlock'
|
|
7
|
+
|
|
8
|
+
[
|
|
9
|
+
'header-one',
|
|
10
|
+
'unstyled',
|
|
11
|
+
].forEach(type => {
|
|
12
|
+
test(`insertEmptyBlock ${type}`, t => {
|
|
13
|
+
const firstBlock = {
|
|
14
|
+
key: 'item1',
|
|
15
|
+
text: 'asdf',
|
|
16
|
+
type: 'unstyled',
|
|
17
|
+
depth: 0,
|
|
18
|
+
inlineStyleRanges: [{
|
|
19
|
+
length: 2,
|
|
20
|
+
offset: 1,
|
|
21
|
+
style: 'ITALIC',
|
|
22
|
+
}],
|
|
23
|
+
entityRanges: [],
|
|
24
|
+
data: { foo: 'bar' },
|
|
25
|
+
}
|
|
26
|
+
const beforeRawContentState = {
|
|
27
|
+
entityMap: {},
|
|
28
|
+
blocks: [firstBlock],
|
|
29
|
+
}
|
|
30
|
+
const afterRawContentState = {
|
|
31
|
+
entityMap: {},
|
|
32
|
+
blocks: [firstBlock, {
|
|
33
|
+
key: 'item2',
|
|
34
|
+
text: '',
|
|
35
|
+
type,
|
|
36
|
+
depth: 0,
|
|
37
|
+
inlineStyleRanges: [],
|
|
38
|
+
entityRanges: [],
|
|
39
|
+
data: {},
|
|
40
|
+
}],
|
|
41
|
+
}
|
|
42
|
+
const editorState = createEditorState(
|
|
43
|
+
beforeRawContentState,
|
|
44
|
+
{
|
|
45
|
+
anchorKey: 'item1',
|
|
46
|
+
anchorOffset: 4,
|
|
47
|
+
focusKey: 'item1',
|
|
48
|
+
focusOffset: 4,
|
|
49
|
+
isBackward: false,
|
|
50
|
+
hasFocus: true,
|
|
51
|
+
}
|
|
52
|
+
)
|
|
53
|
+
const newEditorState = insertEmptyBlock(editorState, type)
|
|
54
|
+
|
|
55
|
+
t.not(newEditorState, editorState)
|
|
56
|
+
t.deepEqual(
|
|
57
|
+
omitBlockKeysFromRawContent(
|
|
58
|
+
Draft.convertToRaw(newEditorState.getCurrentContent())
|
|
59
|
+
),
|
|
60
|
+
omitBlockKeysFromRawContent(
|
|
61
|
+
afterRawContentState
|
|
62
|
+
)
|
|
63
|
+
)
|
|
64
|
+
})
|
|
65
|
+
})
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import test from 'ava'
|
|
2
|
+
import Draft from 'draft-js'
|
|
3
|
+
import createEditorState from './fixtures/createEditorState'
|
|
4
|
+
import omitBlockKeysFromRawContent from './fixtures/omitBlockKeysFromRawContent'
|
|
5
|
+
|
|
6
|
+
import insertNewBlock from '../src/insertNewBlock'
|
|
7
|
+
|
|
8
|
+
[
|
|
9
|
+
[],
|
|
10
|
+
['header-one'],
|
|
11
|
+
['unstyled', 'Text'],
|
|
12
|
+
['blockquote', 'Quote text', { foo: 'bar' }],
|
|
13
|
+
].forEach(args => {
|
|
14
|
+
test(`insertNewBlock ${args.length} args`, t => {
|
|
15
|
+
const [type = 'unstyled', text = '', data = {}] = args
|
|
16
|
+
const firstBlock = {
|
|
17
|
+
key: 'item1',
|
|
18
|
+
text: 'asdf',
|
|
19
|
+
type: 'unstyled',
|
|
20
|
+
depth: 0,
|
|
21
|
+
inlineStyleRanges: [{
|
|
22
|
+
length: 2,
|
|
23
|
+
offset: 1,
|
|
24
|
+
style: 'ITALIC',
|
|
25
|
+
}],
|
|
26
|
+
entityRanges: [],
|
|
27
|
+
data: {},
|
|
28
|
+
}
|
|
29
|
+
const beforeRawContentState = {
|
|
30
|
+
entityMap: {},
|
|
31
|
+
blocks: [firstBlock],
|
|
32
|
+
}
|
|
33
|
+
const afterRawContentState = {
|
|
34
|
+
entityMap: {},
|
|
35
|
+
blocks: [firstBlock, {
|
|
36
|
+
key: 'item2',
|
|
37
|
+
text,
|
|
38
|
+
type,
|
|
39
|
+
depth: 0,
|
|
40
|
+
inlineStyleRanges: [],
|
|
41
|
+
entityRanges: [],
|
|
42
|
+
data,
|
|
43
|
+
}],
|
|
44
|
+
}
|
|
45
|
+
const editorState = createEditorState(
|
|
46
|
+
beforeRawContentState,
|
|
47
|
+
{
|
|
48
|
+
anchorKey: 'item1',
|
|
49
|
+
anchorOffset: 4,
|
|
50
|
+
focusKey: 'item1',
|
|
51
|
+
focusOffset: 4,
|
|
52
|
+
isBackward: false,
|
|
53
|
+
hasFocus: true,
|
|
54
|
+
}
|
|
55
|
+
)
|
|
56
|
+
const newEditorState = insertNewBlock(editorState, ...args)
|
|
57
|
+
|
|
58
|
+
t.not(newEditorState, editorState)
|
|
59
|
+
t.deepEqual(
|
|
60
|
+
omitBlockKeysFromRawContent(
|
|
61
|
+
Draft.convertToRaw(newEditorState.getCurrentContent())
|
|
62
|
+
),
|
|
63
|
+
omitBlockKeysFromRawContent(
|
|
64
|
+
afterRawContentState
|
|
65
|
+
)
|
|
66
|
+
)
|
|
67
|
+
})
|
|
68
|
+
})
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import test from 'ava'
|
|
2
|
+
import Draft from 'draft-js'
|
|
3
|
+
import createEditorState from './fixtures/createEditorState'
|
|
4
|
+
import omitBlockKeysFromRawContent from './fixtures/omitBlockKeysFromRawContent'
|
|
5
|
+
|
|
6
|
+
import insertText from '../src/insertText'
|
|
7
|
+
|
|
8
|
+
test('insertText', t => {
|
|
9
|
+
const beforeRawContentState = {
|
|
10
|
+
entityMap: {},
|
|
11
|
+
blocks: [{
|
|
12
|
+
key: 'item1',
|
|
13
|
+
text: '',
|
|
14
|
+
type: 'unstyled',
|
|
15
|
+
depth: 0,
|
|
16
|
+
inlineStyleRanges: [],
|
|
17
|
+
entityRanges: [],
|
|
18
|
+
data: {},
|
|
19
|
+
}],
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const afterRawContentState = {
|
|
23
|
+
entityMap: {},
|
|
24
|
+
blocks: [{
|
|
25
|
+
key: 'item1',
|
|
26
|
+
text: 'jkl;',
|
|
27
|
+
type: 'unstyled',
|
|
28
|
+
depth: 0,
|
|
29
|
+
inlineStyleRanges: [],
|
|
30
|
+
entityRanges: [],
|
|
31
|
+
data: {},
|
|
32
|
+
}],
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const editorState = createEditorState(
|
|
36
|
+
beforeRawContentState,
|
|
37
|
+
{
|
|
38
|
+
anchorKey: 'item1',
|
|
39
|
+
anchorOffset: 0,
|
|
40
|
+
focusKey: 'item1',
|
|
41
|
+
focusOffset: 0,
|
|
42
|
+
isBackward: false,
|
|
43
|
+
hasFocus: true,
|
|
44
|
+
}
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
const newEditorState = insertText(editorState, 'jkl;')
|
|
48
|
+
|
|
49
|
+
t.not(newEditorState, editorState)
|
|
50
|
+
t.deepEqual(
|
|
51
|
+
omitBlockKeysFromRawContent(
|
|
52
|
+
Draft.convertToRaw(newEditorState.getCurrentContent())
|
|
53
|
+
),
|
|
54
|
+
omitBlockKeysFromRawContent(
|
|
55
|
+
afterRawContentState
|
|
56
|
+
)
|
|
57
|
+
)
|
|
58
|
+
})
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import test from 'ava'
|
|
2
|
+
import Draft from 'draft-js'
|
|
3
|
+
|
|
4
|
+
import mergeBlockData from '../src/mergeBlockData'
|
|
5
|
+
|
|
6
|
+
let editorState
|
|
7
|
+
const KEY = 'item0'
|
|
8
|
+
|
|
9
|
+
test.beforeEach(() => {
|
|
10
|
+
const content = Draft.ContentState.createFromBlockArray([new Draft.ContentBlock({ key: KEY })])
|
|
11
|
+
editorState = Draft.EditorState.createWithContent(content)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
test('mergeBlockData Add', t => {
|
|
15
|
+
const newEditorState = mergeBlockData(editorState, { foo: 1 })
|
|
16
|
+
const block = newEditorState.getCurrentContent().getBlockForKey(KEY)
|
|
17
|
+
t.is(block.getData().get('foo'), 1)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
test('mergeBlockData Update', t => {
|
|
21
|
+
let newEditorState = mergeBlockData(editorState, { foo: 1 })
|
|
22
|
+
newEditorState = mergeBlockData(newEditorState, { foo: 2 })
|
|
23
|
+
const block = newEditorState.getCurrentContent().getBlockForKey(KEY)
|
|
24
|
+
t.is(block.getData().get('foo'), 2)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
test('mergeBlockData Remove', t => {
|
|
28
|
+
let newEditorState = mergeBlockData(editorState, { foo: 1 })
|
|
29
|
+
newEditorState = mergeBlockData(newEditorState, { foo: void 0 })
|
|
30
|
+
const block = newEditorState.getCurrentContent().getBlockForKey(KEY)
|
|
31
|
+
t.is(block.getData().get('foo'), undefined)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
test('mergeBlockData Merge', t => {
|
|
35
|
+
let newEditorState = mergeBlockData(editorState, { foo: 1 })
|
|
36
|
+
newEditorState = mergeBlockData(newEditorState, { bar: 2 })
|
|
37
|
+
const block = newEditorState.getCurrentContent().getBlockForKey(KEY)
|
|
38
|
+
t.is(block.getData().get('foo'), 1)
|
|
39
|
+
t.is(block.getData().get('bar'), 2)
|
|
40
|
+
})
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import test from 'ava'
|
|
2
|
+
import { convertToRaw } from 'draft-js'
|
|
3
|
+
import createEditorState from './fixtures/createEditorState'
|
|
4
|
+
import omitBlockKeysFromRawContent from './fixtures/omitBlockKeysFromRawContent'
|
|
5
|
+
|
|
6
|
+
import mergeBlockDataByKey from '../src/mergeBlockDataByKey'
|
|
7
|
+
|
|
8
|
+
test('mergeBlockDataByKey', t => {
|
|
9
|
+
const beforeRawContentState = {
|
|
10
|
+
entityMap: {
|
|
11
|
+
0: {
|
|
12
|
+
data: {
|
|
13
|
+
foo: 'bar',
|
|
14
|
+
},
|
|
15
|
+
mutability: 'IMMUTABLE',
|
|
16
|
+
type: 'buz',
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
blocks: [
|
|
20
|
+
{
|
|
21
|
+
key: 'item1',
|
|
22
|
+
text: '',
|
|
23
|
+
type: 'unstyled',
|
|
24
|
+
depth: 0,
|
|
25
|
+
inlineStyleRanges: [],
|
|
26
|
+
entityRanges: [],
|
|
27
|
+
data: {},
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
key: 'item2',
|
|
31
|
+
text: ' ',
|
|
32
|
+
type: 'atomic',
|
|
33
|
+
depth: 0,
|
|
34
|
+
inlineStyleRanges: [],
|
|
35
|
+
entityRanges: [{
|
|
36
|
+
key: 0,
|
|
37
|
+
length: 1,
|
|
38
|
+
offset: 0,
|
|
39
|
+
}],
|
|
40
|
+
data: {},
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const afterRawContentState = {
|
|
46
|
+
entityMap: {
|
|
47
|
+
0: {
|
|
48
|
+
data: {
|
|
49
|
+
foo: 'bar',
|
|
50
|
+
},
|
|
51
|
+
mutability: 'IMMUTABLE',
|
|
52
|
+
type: 'buz',
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
blocks: [
|
|
56
|
+
{
|
|
57
|
+
key: 'item1',
|
|
58
|
+
text: '',
|
|
59
|
+
type: 'unstyled',
|
|
60
|
+
depth: 0,
|
|
61
|
+
inlineStyleRanges: [],
|
|
62
|
+
entityRanges: [],
|
|
63
|
+
data: {},
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
key: 'item2',
|
|
67
|
+
text: ' ',
|
|
68
|
+
type: 'atomic',
|
|
69
|
+
depth: 0,
|
|
70
|
+
inlineStyleRanges: [],
|
|
71
|
+
entityRanges: [{
|
|
72
|
+
key: 0,
|
|
73
|
+
length: 1,
|
|
74
|
+
offset: 0,
|
|
75
|
+
}],
|
|
76
|
+
data: { foo: 'bar' },
|
|
77
|
+
},
|
|
78
|
+
],
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const editorState = createEditorState(
|
|
82
|
+
beforeRawContentState,
|
|
83
|
+
{
|
|
84
|
+
anchorKey: 'item1',
|
|
85
|
+
anchorOffset: 0,
|
|
86
|
+
focusKey: 'item1',
|
|
87
|
+
focusOffset: 0,
|
|
88
|
+
isBackward: false,
|
|
89
|
+
hasFocus: true,
|
|
90
|
+
}
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
const newEditorState = mergeBlockDataByKey(editorState, 'item2', { foo: 'bar' })
|
|
94
|
+
t.not(newEditorState, editorState)
|
|
95
|
+
t.deepEqual(
|
|
96
|
+
omitBlockKeysFromRawContent(
|
|
97
|
+
convertToRaw(newEditorState.getCurrentContent())
|
|
98
|
+
),
|
|
99
|
+
omitBlockKeysFromRawContent(
|
|
100
|
+
afterRawContentState
|
|
101
|
+
)
|
|
102
|
+
)
|
|
103
|
+
})
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import test from 'ava'
|
|
2
|
+
import { EditorState, ContentState, SelectionState, ContentBlock } from 'draft-js'
|
|
3
|
+
|
|
4
|
+
import mergeEntityData from '../src/mergeEntityData'
|
|
5
|
+
|
|
6
|
+
let editorState
|
|
7
|
+
let entityKey
|
|
8
|
+
|
|
9
|
+
test.beforeEach(() => {
|
|
10
|
+
const content = ContentState.createFromBlockArray([new ContentBlock({ key: 'item1', text: ' ' })])
|
|
11
|
+
const selection = new SelectionState({
|
|
12
|
+
anchorKey: 'item1',
|
|
13
|
+
anchorOffset: 0,
|
|
14
|
+
focusKey: 'item1',
|
|
15
|
+
focusOffset: 1,
|
|
16
|
+
isBackward: false,
|
|
17
|
+
hasFocus: true,
|
|
18
|
+
})
|
|
19
|
+
editorState = EditorState.forceSelection(
|
|
20
|
+
EditorState.createWithContent(content),
|
|
21
|
+
selection
|
|
22
|
+
)
|
|
23
|
+
const newContent = editorState.getCurrentContent().createEntity('LINK', 'IMMUTABLE', {})
|
|
24
|
+
entityKey = newContent.getLastCreatedEntityKey()
|
|
25
|
+
editorState = EditorState.createWithContent(newContent)
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
test('mergeEntityData Add', t => {
|
|
29
|
+
const newEditorState = mergeEntityData(editorState, entityKey, { foo: 1 })
|
|
30
|
+
t.is(newEditorState.getCurrentContent().getEntity(entityKey).getData().foo, 1)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
test('mergeEntityData Update', t => {
|
|
34
|
+
let newEditorState = mergeEntityData(editorState, entityKey, { foo: 1 })
|
|
35
|
+
newEditorState = mergeEntityData(newEditorState, entityKey, { foo: 2 })
|
|
36
|
+
t.is(newEditorState.getCurrentContent().getEntity(entityKey).getData().foo, 2)
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
test('mergeEntityData Remove', t => {
|
|
40
|
+
let newEditorState = mergeEntityData(editorState, entityKey, { foo: 1 })
|
|
41
|
+
newEditorState = mergeEntityData(newEditorState, entityKey, { foo: void 0 })
|
|
42
|
+
t.is(newEditorState.getCurrentContent().getEntity(entityKey).getData().foo, undefined)
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
test('mergeEntityData Merge', t => {
|
|
46
|
+
let newEditorState = mergeEntityData(editorState, entityKey, { foo: 1 })
|
|
47
|
+
newEditorState = mergeEntityData(newEditorState, entityKey, { bar: 2 })
|
|
48
|
+
t.deepEqual(newEditorState.getCurrentContent().getEntity(entityKey).getData(), { foo: 1, bar: 2 })
|
|
49
|
+
})
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import test from 'ava'
|
|
2
|
+
import { convertToRaw } from 'draft-js'
|
|
3
|
+
import createEditorState from './fixtures/createEditorState'
|
|
4
|
+
|
|
5
|
+
import modifyBlock from '../src/modifyBlock'
|
|
6
|
+
|
|
7
|
+
test('modifyBlock', t => {
|
|
8
|
+
const beforeRawContentState = {
|
|
9
|
+
entityMap: {},
|
|
10
|
+
blocks: [{
|
|
11
|
+
key: 'item1',
|
|
12
|
+
text: '',
|
|
13
|
+
type: 'unstyled',
|
|
14
|
+
depth: 0,
|
|
15
|
+
inlineStyleRanges: [],
|
|
16
|
+
entityRanges: [],
|
|
17
|
+
data: {},
|
|
18
|
+
}],
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const afterRawContentState = {
|
|
22
|
+
entityMap: {},
|
|
23
|
+
blocks: [{
|
|
24
|
+
key: 'item1',
|
|
25
|
+
text: 'jkl;',
|
|
26
|
+
type: 'header-one',
|
|
27
|
+
depth: 0,
|
|
28
|
+
inlineStyleRanges: [],
|
|
29
|
+
entityRanges: [],
|
|
30
|
+
data: { foo: 1 },
|
|
31
|
+
}],
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const editorState = createEditorState(
|
|
35
|
+
beforeRawContentState,
|
|
36
|
+
{
|
|
37
|
+
anchorKey: 'item1',
|
|
38
|
+
anchorOffset: 0,
|
|
39
|
+
focusKey: 'item1',
|
|
40
|
+
focusOffset: 0,
|
|
41
|
+
isBackward: false,
|
|
42
|
+
hasFocus: true,
|
|
43
|
+
}
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
const newEditorState = modifyBlock(editorState, { text: 'jkl;', type: 'header-one', data: { foo: 1 }})
|
|
47
|
+
t.not(newEditorState, editorState)
|
|
48
|
+
t.deepEqual(
|
|
49
|
+
convertToRaw(newEditorState.getCurrentContent()),
|
|
50
|
+
afterRawContentState
|
|
51
|
+
)
|
|
52
|
+
})
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import test from 'ava'
|
|
2
|
+
import { convertToRaw, ContentBlock } from 'draft-js'
|
|
3
|
+
import { Map, List } from 'immutable'
|
|
4
|
+
import createEditorState from './fixtures/createEditorState'
|
|
5
|
+
import omitBlockKeysFromRawContent from './fixtures/omitBlockKeysFromRawContent'
|
|
6
|
+
|
|
7
|
+
import modifyBlockByKey from '../src/modifyBlockByKey'
|
|
8
|
+
|
|
9
|
+
test('modifyBlockByKey', t => {
|
|
10
|
+
const beforeRawContentState = {
|
|
11
|
+
entityMap: {},
|
|
12
|
+
blocks: [
|
|
13
|
+
{
|
|
14
|
+
key: 'item1',
|
|
15
|
+
text: '',
|
|
16
|
+
type: 'unstyled',
|
|
17
|
+
depth: 0,
|
|
18
|
+
inlineStyleRanges: [],
|
|
19
|
+
entityRanges: [],
|
|
20
|
+
data: {},
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
key: 'item2',
|
|
24
|
+
text: '',
|
|
25
|
+
type: 'header-one',
|
|
26
|
+
depth: 0,
|
|
27
|
+
inlineStyleRanges: [],
|
|
28
|
+
entityRanges: [],
|
|
29
|
+
data: { foo: 'bar' },
|
|
30
|
+
},
|
|
31
|
+
],
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const afterRawContentState = {
|
|
35
|
+
entityMap: {},
|
|
36
|
+
blocks: [
|
|
37
|
+
{
|
|
38
|
+
key: 'item1',
|
|
39
|
+
text: '',
|
|
40
|
+
type: 'unstyled',
|
|
41
|
+
depth: 0,
|
|
42
|
+
inlineStyleRanges: [],
|
|
43
|
+
entityRanges: [],
|
|
44
|
+
data: {},
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
key: 'item2',
|
|
48
|
+
text: 'H1',
|
|
49
|
+
type: 'header-one',
|
|
50
|
+
depth: 0,
|
|
51
|
+
inlineStyleRanges: [],
|
|
52
|
+
entityRanges: [],
|
|
53
|
+
data: { foo: 'buz' },
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const editorState = createEditorState(
|
|
59
|
+
beforeRawContentState,
|
|
60
|
+
{
|
|
61
|
+
anchorKey: 'item1',
|
|
62
|
+
anchorOffset: 0,
|
|
63
|
+
focusKey: 'item1',
|
|
64
|
+
focusOffset: 0,
|
|
65
|
+
isBackward: false,
|
|
66
|
+
hasFocus: true,
|
|
67
|
+
}
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
const newEditorState = modifyBlockByKey(
|
|
71
|
+
editorState,
|
|
72
|
+
'item2',
|
|
73
|
+
new ContentBlock({
|
|
74
|
+
key: 'item2',
|
|
75
|
+
text: 'H1',
|
|
76
|
+
type: 'header-one',
|
|
77
|
+
characterList: List(),
|
|
78
|
+
depth: 0,
|
|
79
|
+
data: Map({ foo: 'buz' }),
|
|
80
|
+
})
|
|
81
|
+
)
|
|
82
|
+
t.not(newEditorState, editorState)
|
|
83
|
+
t.deepEqual(
|
|
84
|
+
omitBlockKeysFromRawContent(
|
|
85
|
+
convertToRaw(newEditorState.getCurrentContent())
|
|
86
|
+
),
|
|
87
|
+
omitBlockKeysFromRawContent(
|
|
88
|
+
afterRawContentState
|
|
89
|
+
)
|
|
90
|
+
)
|
|
91
|
+
})
|