From cc57ba8dfcce4b7b6b2d785dd527e02d988fd0aa Mon Sep 17 00:00:00 2001 From: Jocs Date: Thu, 7 Dec 2017 10:58:59 +0800 Subject: [PATCH] opti: make createBlock is a method to contentState.prototype --- src/editor/contentState/enterCtrl.js | 25 +++++++++++---------- src/editor/contentState/index.js | 31 +++++++++++++-------------- src/editor/contentState/updateCtrl.js | 5 ++--- 3 files changed, 29 insertions(+), 32 deletions(-) diff --git a/src/editor/contentState/enterCtrl.js b/src/editor/contentState/enterCtrl.js index c2e01e5d..a17ba4f0 100644 --- a/src/editor/contentState/enterCtrl.js +++ b/src/editor/contentState/enterCtrl.js @@ -1,12 +1,11 @@ import selection from '../selection' import { findNearestParagraph } from '../utils/domManipulate' -import { newABlock } from './index' const enterCtrl = ContentState => { ContentState.prototype.chopBlock = function (block) { const parent = this.getParent(block) const type = parent.type - const container = newABlock(this.keys, null, null, null, '', block.depth - 1, type) + const container = this.createBlock(type) const index = this.findIndex(parent.children, block) const partChildren = parent.children.splice(index + 1) block.nextSibling = null @@ -14,9 +13,9 @@ const enterCtrl = ContentState => { this.insertAfter(container, parent) } - ContentState.prototype.createBlockLi = function (text, depth) { - const liBlock = newABlock(this.keys, null, null, null, '', depth, 'li') - const pBlock = newABlock(this.keys, liBlock.key, null, null, text, depth + 1, 'p') + ContentState.prototype.createBlockLi = function (text = '') { + const liBlock = this.createBlock('li') + const pBlock = this.createBlock('p', text) this.appendChild(liBlock, pBlock) return liBlock } @@ -47,16 +46,16 @@ const enterCtrl = ContentState => { } if (type === 'li') { - newBlock = this.createBlockLi(post, block.depth) + newBlock = this.createBlockLi(post) } else { block.text = pre - newBlock = newABlock(this.keys, block.parent, block.key, null, post, block.depth, type) + newBlock = this.createBlock(type, post) } this.insertAfter(newBlock, block) break case left === 0 && right === 0: // paragraph is empty if (parent.type === 'blockquote' || parent.type === 'ul') { - newBlock = newABlock(this.keys, null, null, null, '', block.depth - 1, 'p') + newBlock = this.createBlock('p') if (this.isOnlyChild(block)) { this.insertAfter(newBlock, parent) @@ -72,7 +71,7 @@ const enterCtrl = ContentState => { this.removeBlock(block) } else if (parent.type === 'li') { - newBlock = this.createBlockLi('', block.depth - 1) + newBlock = this.createBlockLi() this.insertAfter(newBlock, parent) const index = this.findIndex(parent.children, block) const partChildren = parent.children.splice(index + 1) @@ -80,7 +79,7 @@ const enterCtrl = ContentState => { this.removeBlock(block) } else { - newBlock = newABlock(this.keys, null, null, null, '', block.depth, 'p') + newBlock = this.createBlock('p') if (preType === 'li') { const parent = this.getParent(block) this.insertAfter(newBlock, parent) @@ -96,8 +95,8 @@ const enterCtrl = ContentState => { if (preType === 'li') type = 'li' else type = 'p' // insert after or before newBlock = type === 'li' - ? this.createBlockLi('', block.depth) - : newABlock(this.keys, null, null, null, '', block.depth, 'p') + ? this.createBlockLi() + : this.createBlock('p') if (left === 0 && right !== 0) { this.insertBefore(newBlock, block) } else { @@ -105,7 +104,7 @@ const enterCtrl = ContentState => { } break default: - newBlock = newABlock(this.keys, null, null, null, '', block.depth, 'p') + newBlock = this.createBlock('p') this.insertAfter(newBlock, block) break } diff --git a/src/editor/contentState/index.js b/src/editor/contentState/index.js index bc92774a..c687b5ef 100644 --- a/src/editor/contentState/index.js +++ b/src/editor/contentState/index.js @@ -12,20 +12,6 @@ const ctrls = [ backspaceCtrl ] -export const newABlock = (set, parent = null, preSibling = null, nextSibling = null, text = '', depth = 0, type = 'p') => { - const key = getUniqueId(set) - return { - key, - parent, - preSibling, - nextSibling, - text, - children: [], - depth, - type - } -} - // deep first search const convertBlocksToArray = blocks => { const result = [] @@ -39,9 +25,9 @@ const convertBlocksToArray = blocks => { } class ContentState { - constructor (blocks) { + constructor () { this.keys = new Set() - this.blocks = blocks || [ newABlock(this.keys) ] + this.blocks = [ this.createBlock() ] this.stateRender = new StateRender() const lastBlock = this.getLastBlock() this.cursor = { @@ -59,6 +45,19 @@ class ContentState { return this.stateRender.render(blocks, cursor, activeBlock) } + createBlock (type = 'p', text = '') { + const key = getUniqueId(this.keys) + return { + key, + type, + text, + parent: null, + preSibling: null, + nextSibling: null, + children: [] + } + } + // getBlocks getBlocks () { return this.blocks diff --git a/src/editor/contentState/updateCtrl.js b/src/editor/contentState/updateCtrl.js index bc4ef9cc..d8fc1525 100644 --- a/src/editor/contentState/updateCtrl.js +++ b/src/editor/contentState/updateCtrl.js @@ -2,7 +2,6 @@ import selection from '../selection' import { findNearestParagraph } from '../utils/domManipulate' import { tokenizer } from '../parser/parse' import { conflict } from '../utils' -import { newABlock } from './index' const INLINE_UPDATE_REG = /^([*+-]\s(\[\s\]\s)?)|^(\d+\.\s)|^(#{1,6})[^#]+|^(>).+/ @@ -114,10 +113,10 @@ const updateCtrl = ContentState => { ContentState.prototype.updateBlockQuote = function (block) { const newText = block.text.substring(1).trim() - const newPblock = newABlock(this.keys, block.key, null, null, newText, block.depth + 1, 'p') + const newPblock = this.createBlock('p', newText) block.type = 'blockquote' block.text = '' - block.children = [ newPblock ] + this.appendChild(block, newPblock) const { start, end } = this.cursor.range this.cursor = { key: newPblock.key,