refactor: constantize, remove redundancy
This commit is contained in:
parent
7b9dbf6123
commit
e84e7f4e2d
2 changed files with 46 additions and 20 deletions
19
src/constants.js
Normal file
19
src/constants.js
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
const editorChangeTypes = {
|
||||||
|
INSERT_CHARACTERS: 'insert-characters',
|
||||||
|
BACKSPACE_CHARACTER: 'backspace-character',
|
||||||
|
SPLIT_BLOCK: 'split-block',
|
||||||
|
}
|
||||||
|
|
||||||
|
const commitTypes = {
|
||||||
|
ADD: 'add',
|
||||||
|
DELETE: 'delete',
|
||||||
|
EDIT: 'edit',
|
||||||
|
}
|
||||||
|
|
||||||
|
const TAG = 'tag'
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
editorChangeTypes,
|
||||||
|
commitTypes,
|
||||||
|
TAG,
|
||||||
|
}
|
47
src/utils.js
47
src/utils.js
|
@ -2,9 +2,15 @@ const uuid = require('uuid/v4')
|
||||||
const { ContentState, EditorState } = require('draft-js')
|
const { ContentState, EditorState } = require('draft-js')
|
||||||
const Immutable = require('immutable')
|
const Immutable = require('immutable')
|
||||||
|
|
||||||
|
const { editorChangeTypes, commitTypes, TAG } = require('./constants')
|
||||||
|
|
||||||
|
function getBlockTag(block) {
|
||||||
|
return block.getData().get(TAG)
|
||||||
|
}
|
||||||
|
|
||||||
function reconcileTags(previousBlockMap, currentBlockMap) {
|
function reconcileTags(previousBlockMap, currentBlockMap) {
|
||||||
return previousBlockMap.valueSeq().map(block => {
|
return previousBlockMap.valueSeq().map(block => {
|
||||||
const isTagged = block.getData().has('tag')
|
const isTagged = getBlockTag(block)
|
||||||
|
|
||||||
if (isTagged) return block
|
if (isTagged) return block
|
||||||
|
|
||||||
|
@ -13,9 +19,7 @@ function reconcileTags(previousBlockMap, currentBlockMap) {
|
||||||
|
|
||||||
if (!currentBlock) return block
|
if (!currentBlock) return block
|
||||||
|
|
||||||
const newBlockData = block
|
const newBlockData = block.getData().set(TAG, getBlockTag(currentBlock))
|
||||||
.getData()
|
|
||||||
.set('tag', currentBlock.getData().get('tag'))
|
|
||||||
return isTagged ? block : block.set('data', newBlockData)
|
return isTagged ? block : block.set('data', newBlockData)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -23,12 +27,12 @@ function reconcileTags(previousBlockMap, currentBlockMap) {
|
||||||
function processBlockDeletion(previousBlocks, currentBlocks) {
|
function processBlockDeletion(previousBlocks, currentBlocks) {
|
||||||
const deletedBlocks = previousBlocks
|
const deletedBlocks = previousBlocks
|
||||||
.valueSeq()
|
.valueSeq()
|
||||||
.filter(block => !currentBlocks.has(block.getData().get('tag')))
|
.filter(block => !currentBlocks.has(getBlockTag(block)))
|
||||||
|
|
||||||
return deletedBlocks
|
return deletedBlocks
|
||||||
.map(block => ({
|
.map(block => ({
|
||||||
type: 'delete',
|
type: commitTypes.DELETE,
|
||||||
tag: block.getData().get('tag'),
|
tag: getBlockTag(block),
|
||||||
}))
|
}))
|
||||||
.toJS()
|
.toJS()
|
||||||
}
|
}
|
||||||
|
@ -36,12 +40,12 @@ function processBlockDeletion(previousBlocks, currentBlocks) {
|
||||||
function processBlockCreation(previousBlocks, currentBlocks) {
|
function processBlockCreation(previousBlocks, currentBlocks) {
|
||||||
const addedBlocks = currentBlocks
|
const addedBlocks = currentBlocks
|
||||||
.valueSeq()
|
.valueSeq()
|
||||||
.filter(block => !previousBlocks.has(block.getData().get('tag')))
|
.filter(block => !previousBlocks.has(getBlockTag(block)))
|
||||||
|
|
||||||
return addedBlocks
|
return addedBlocks
|
||||||
.map(block => ({
|
.map(block => ({
|
||||||
type: 'add',
|
type: commitTypes.ADD,
|
||||||
tag: block.getData().get('tag'),
|
tag: getBlockTag(block),
|
||||||
text: block.getText(),
|
text: block.getText(),
|
||||||
}))
|
}))
|
||||||
.toJS()
|
.toJS()
|
||||||
|
@ -49,7 +53,7 @@ function processBlockCreation(previousBlocks, currentBlocks) {
|
||||||
|
|
||||||
function processBlockEdit(previousBlocks, currentBlocks) {
|
function processBlockEdit(previousBlocks, currentBlocks) {
|
||||||
const editedBlocks = currentBlocks.valueSeq().filter(block => {
|
const editedBlocks = currentBlocks.valueSeq().filter(block => {
|
||||||
const currentTag = block.getData().get('tag')
|
const currentTag = getBlockTag(block)
|
||||||
const blockBefore = previousBlocks.get(currentTag)
|
const blockBefore = previousBlocks.get(currentTag)
|
||||||
if (!currentTag || !blockBefore) return false
|
if (!currentTag || !blockBefore) return false
|
||||||
|
|
||||||
|
@ -60,8 +64,8 @@ function processBlockEdit(previousBlocks, currentBlocks) {
|
||||||
|
|
||||||
return editedBlocks
|
return editedBlocks
|
||||||
.map(block => ({
|
.map(block => ({
|
||||||
type: 'edit',
|
type: commitTypes.EDIT,
|
||||||
tag: block.getData().get('tag'),
|
tag: getBlockTag(block),
|
||||||
text: block.getText(),
|
text: block.getText(),
|
||||||
}))
|
}))
|
||||||
.toJS()
|
.toJS()
|
||||||
|
@ -82,13 +86,16 @@ function deriveChangesFromStates(previous, actual) {
|
||||||
: new Immutable.Map()
|
: new Immutable.Map()
|
||||||
const hasLessBlocks = previousBlocks.size > currentBlocks.size
|
const hasLessBlocks = previousBlocks.size > currentBlocks.size
|
||||||
if (
|
if (
|
||||||
changeType === 'insert-characters' ||
|
changeType === editorChangeTypes.INSERT_CHARACTERS ||
|
||||||
(changeType === 'backspace-character' && !hasLessBlocks)
|
(changeType === editorChangeTypes.BACKSPACE_CHARACTER && !hasLessBlocks)
|
||||||
) {
|
) {
|
||||||
return processBlockEdit(previousBlocks, currentBlocks)
|
return processBlockEdit(previousBlocks, currentBlocks)
|
||||||
} else if (changeType === 'split-block') {
|
} else if (changeType === editorChangeTypes.SPLIT_BLOCK) {
|
||||||
return processBlockCreation(previousBlocks, currentBlocks)
|
return processBlockCreation(previousBlocks, currentBlocks)
|
||||||
} else if (changeType === 'backspace-character' && hasLessBlocks) {
|
} else if (
|
||||||
|
changeType === editorChangeTypes.BACKSPACE_CHARACTER &&
|
||||||
|
hasLessBlocks
|
||||||
|
) {
|
||||||
return processBlockDeletion(previousBlocks, currentBlocks)
|
return processBlockDeletion(previousBlocks, currentBlocks)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,8 +111,8 @@ function tagUntaggedBlocks(editorState) {
|
||||||
const blockList = contentState.getBlockMap().valueSeq()
|
const blockList = contentState.getBlockMap().valueSeq()
|
||||||
|
|
||||||
const taggedBlockList = blockList.map(block => {
|
const taggedBlockList = blockList.map(block => {
|
||||||
const isTagged = block.getData().has('tag')
|
const isTagged = getBlockTag(block)
|
||||||
const newBlockData = block.getData().set('tag', uuid())
|
const newBlockData = block.getData().set(TAG, uuid())
|
||||||
return isTagged ? block : block.set('data', newBlockData)
|
return isTagged ? block : block.set('data', newBlockData)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -118,7 +125,7 @@ function tagUntaggedBlocks(editorState) {
|
||||||
|
|
||||||
function getBlockMapByTag(blocks) {
|
function getBlockMapByTag(blocks) {
|
||||||
return blocks.reduce((blockTagMap, block) => {
|
return blocks.reduce((blockTagMap, block) => {
|
||||||
const tag = block.getData().get('tag')
|
const tag = getBlockTag(block)
|
||||||
if (!tag || !block) return blockTagMap
|
if (!tag || !block) return blockTagMap
|
||||||
return blockTagMap.set(tag, block)
|
return blockTagMap.set(tag, block)
|
||||||
}, new Immutable.Map())
|
}, new Immutable.Map())
|
||||||
|
|
Reference in a new issue