1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-11 01:33:58 +02:00

fix examples

This commit is contained in:
Ian Storm Taylor
2016-06-23 23:59:22 -07:00
parent 9d62948b1a
commit 69d2a55d33
4 changed files with 20 additions and 28 deletions

View File

@@ -12,21 +12,17 @@ import state from './state.json'
*/ */
function deserialize(string) { function deserialize(string) {
const characters = string const characters = string.split('').map(char => {
.split('') return { text: char }
.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 nodes = Block.createMap([node]) const text = Text.create({ characters })
const document = Document.create({ nodes }) const block = Block.create({
type: 'paragraph',
nodes: [text]
})
const document = Document.create({ nodes: [block] })
const state = State.create({ document }) const state = State.create({ document })
return state return state
} }

View File

@@ -16,14 +16,14 @@ class App extends React.Component {
hasMark(type) { hasMark(type) {
const { state } = this.state const { state } = this.state
const { currentMarks } = state const { marks } = state
return currentMarks.some(mark => mark.type == type) return marks.some(mark => mark.type == type)
} }
hasBlock(type) { hasBlock(type) {
const { state } = this.state const { state } = this.state
const { currentBlockNodes } = state const { blocks } = state
return currentBlockNodes.some(node => node.type == type) return blocks.some(node => node.type == type)
} }
onClickMark(e, type) { onClickMark(e, type) {
@@ -46,7 +46,7 @@ class App extends React.Component {
state = state state = state
.transform() .transform()
.setType(isActive ? 'paragraph' : type) .setBlock(isActive ? 'paragraph' : type)
.apply() .apply()
this.setState({ state }) this.setState({ state })

View File

@@ -100,9 +100,7 @@ class App extends React.Component {
*/ */
onKeyDown(e, state) { onKeyDown(e, state) {
if (state.isCurrentlyExpanded) return if (state.startNode.type != 'table-cell') return
const node = state.currentBlockNodes.first()
if (node.type != 'table-cell') return
const key = keycode(e.which) const key = keycode(e.which)
switch (key) { switch (key) {
@@ -121,7 +119,7 @@ class App extends React.Component {
*/ */
onBackspace(e, state) { onBackspace(e, state) {
if (state.currentStartOffset != 0) return if (state.startOffset != 0) return
e.preventDefault() e.preventDefault()
return state return state
} }
@@ -135,8 +133,7 @@ class App extends React.Component {
*/ */
onDelete(e, state) { onDelete(e, state) {
const node = state.currentBlockNodes.first() if (state.endOffset != state.startNode.length) return
if (state.currentEndOffset != node.length) return
e.preventDefault() e.preventDefault()
return state return state
} }

View File

@@ -412,6 +412,7 @@ const Node = {
// If the range is collapsed at the start of the node, check the previous. // If the range is collapsed at the start of the node, check the previous.
if (range.isCollapsed && startOffset == 0) { if (range.isCollapsed && startOffset == 0) {
const text = this.getDescendant(startKey)
const previous = this.getPreviousText(startKey) const previous = this.getPreviousText(startKey)
if (!previous) return marks if (!previous) return marks
const char = text.characters.get(previous.length - 1) 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. // Otherwise, get a set of the marks for each character in the range.
this return this
.getCharactersAtRange(range) .getCharactersAtRange(range)
.reduce((marks, char) => { .reduce((marks, char) => marks.union(char.marks), marks)
return marks.union(char.marks)
}, marks)
}, },
/** /**