diff --git a/examples/plain-text/index.js b/examples/plain-text/index.js index 0962a2398..fca3481bd 100644 --- a/examples/plain-text/index.js +++ b/examples/plain-text/index.js @@ -12,21 +12,17 @@ import state from './state.json' */ function deserialize(string) { - const characters = string - .split('') - .reduce((list, char) => { - return list.push(Character.create({ text: char })) - }, Character.createList()) - - const text = Text.create({ characters }) - const texts = Block.createMap([text]) - const node = Block.create({ - type: 'paragraph', - nodes: texts, + const characters = string.split('').map(char => { + return { text: char } }) - const nodes = Block.createMap([node]) - const document = Document.create({ nodes }) + const text = Text.create({ characters }) + const block = Block.create({ + type: 'paragraph', + nodes: [text] + }) + + const document = Document.create({ nodes: [block] }) const state = State.create({ document }) return state } diff --git a/examples/rich-text/index.js b/examples/rich-text/index.js index bb284dca2..28a42cefe 100644 --- a/examples/rich-text/index.js +++ b/examples/rich-text/index.js @@ -16,14 +16,14 @@ class App extends React.Component { hasMark(type) { const { state } = this.state - const { currentMarks } = state - return currentMarks.some(mark => mark.type == type) + const { marks } = state + return marks.some(mark => mark.type == type) } hasBlock(type) { const { state } = this.state - const { currentBlockNodes } = state - return currentBlockNodes.some(node => node.type == type) + const { blocks } = state + return blocks.some(node => node.type == type) } onClickMark(e, type) { @@ -46,7 +46,7 @@ class App extends React.Component { state = state .transform() - .setType(isActive ? 'paragraph' : type) + .setBlock(isActive ? 'paragraph' : type) .apply() this.setState({ state }) diff --git a/examples/table/index.js b/examples/table/index.js index 38451003d..08fd27bff 100644 --- a/examples/table/index.js +++ b/examples/table/index.js @@ -100,9 +100,7 @@ class App extends React.Component { */ onKeyDown(e, state) { - if (state.isCurrentlyExpanded) return - const node = state.currentBlockNodes.first() - if (node.type != 'table-cell') return + if (state.startNode.type != 'table-cell') return const key = keycode(e.which) switch (key) { @@ -121,7 +119,7 @@ class App extends React.Component { */ onBackspace(e, state) { - if (state.currentStartOffset != 0) return + if (state.startOffset != 0) return e.preventDefault() return state } @@ -135,8 +133,7 @@ class App extends React.Component { */ onDelete(e, state) { - const node = state.currentBlockNodes.first() - if (state.currentEndOffset != node.length) return + if (state.endOffset != state.startNode.length) return e.preventDefault() return state } diff --git a/lib/models/node.js b/lib/models/node.js index 944aae1e7..7dced610c 100644 --- a/lib/models/node.js +++ b/lib/models/node.js @@ -412,6 +412,7 @@ const Node = { // If the range is collapsed at the start of the node, check the previous. if (range.isCollapsed && startOffset == 0) { + const text = this.getDescendant(startKey) const previous = this.getPreviousText(startKey) if (!previous) return marks const char = text.characters.get(previous.length - 1) @@ -426,11 +427,9 @@ const Node = { } // Otherwise, get a set of the marks for each character in the range. - this + return this .getCharactersAtRange(range) - .reduce((marks, char) => { - return marks.union(char.marks) - }, marks) + .reduce((marks, char) => marks.union(char.marks), marks) }, /**