1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-07-31 20:40:19 +02:00

add debug points, remove unused deps, fix composition

This commit is contained in:
Ian Storm Taylor
2016-08-01 13:45:46 -07:00
parent 53de6bb404
commit 560b28ce32
7 changed files with 132 additions and 24 deletions

View File

@@ -307,6 +307,7 @@ class RichText extends React.Component {
renderMark={this.renderMark} renderMark={this.renderMark}
onChange={this.onChange} onChange={this.onChange}
onKeyDown={this.onKeyDown} onKeyDown={this.onKeyDown}
style={{}}
/> />
</div> </div>
) )

View File

@@ -1,5 +1,6 @@
import Base64 from '../serializers/base-64' import Base64 from '../serializers/base-64'
import Debug from 'debug'
import Node from './node' import Node from './node'
import OffsetKey from '../utils/offset-key' import OffsetKey from '../utils/offset-key'
import React from 'react' import React from 'react'
@@ -9,6 +10,14 @@ import includes from 'lodash/includes'
import keycode from 'keycode' import keycode from 'keycode'
import { IS_FIREFOX, IS_MAC } from '../utils/environment' import { IS_FIREFOX, IS_MAC } from '../utils/environment'
/**
* Debug.
*
* @type {Function}
*/
const debug = Debug('slate:content')
/** /**
* Noop. * Noop.
* *
@@ -101,7 +110,7 @@ class Content extends React.Component {
* While rendering, set the `isRendering` flag. * While rendering, set the `isRendering` flag.
* *
* @param {Object} props * @param {Object} props
* @param {Object} state] * @param {Object} state
*/ */
componentWillUpdate = (props, state) => { componentWillUpdate = (props, state) => {
@@ -112,7 +121,7 @@ class Content extends React.Component {
* When finished rendering, move the `isRendering` flag on next tick. * When finished rendering, move the `isRendering` flag on next tick.
* *
* @param {Object} props * @param {Object} props
* @param {Object} state] * @param {Object} state
*/ */
componentDidUpdate = (props, state) => { componentDidUpdate = (props, state) => {
@@ -162,6 +171,8 @@ class Content extends React.Component {
if (isNonEditable(e)) return if (isNonEditable(e)) return
const data = {} const data = {}
debug('onBeforeInput', data)
this.props.onBeforeInput(e, data) this.props.onBeforeInput(e, data)
} }
@@ -177,6 +188,8 @@ class Content extends React.Component {
if (isNonEditable(e)) return if (isNonEditable(e)) return
const data = {} const data = {}
debug('onBlur', data)
this.props.onBlur(e, data) this.props.onBlur(e, data)
} }
@@ -187,6 +200,7 @@ class Content extends React.Component {
*/ */
onChange = (state) => { onChange = (state) => {
debug('onChange', state)
this.props.onChange(state) this.props.onChange(state)
} }
@@ -201,6 +215,8 @@ class Content extends React.Component {
this.tmp.isComposing = true this.tmp.isComposing = true
this.tmp.compositions++ this.tmp.compositions++
debug('onCompositionStart')
} }
/** /**
@@ -224,6 +240,8 @@ class Content extends React.Component {
if (this.tmp.compositions > count) return if (this.tmp.compositions > count) return
this.tmp.isComposing = false this.tmp.isComposing = false
}) })
debug('onCompositionEnd')
} }
/** /**
@@ -244,6 +262,8 @@ class Content extends React.Component {
const data = {} const data = {}
data.type = 'fragment' data.type = 'fragment'
data.fragment = state.fragment data.fragment = state.fragment
debug('onCopy', data)
this.props.onCopy(e, data) this.props.onCopy(e, data)
} }
@@ -266,6 +286,8 @@ class Content extends React.Component {
const data = {} const data = {}
data.type = 'fragment' data.type = 'fragment'
data.fragment = state.fragment data.fragment = state.fragment
debug('onCut', data)
this.props.onCut(e, data) this.props.onCut(e, data)
} }
@@ -280,6 +302,8 @@ class Content extends React.Component {
this.tmp.isDragging = false this.tmp.isDragging = false
this.tmp.isInternalDrag = null this.tmp.isInternalDrag = null
debug('onDragEnd')
} }
/** /**
@@ -303,6 +327,8 @@ class Content extends React.Component {
if (this.tmp.isDragging) return if (this.tmp.isDragging) return
this.tmp.isDragging = true this.tmp.isDragging = true
this.tmp.isInternalDrag = false this.tmp.isInternalDrag = false
debug('onDragOver')
} }
/** /**
@@ -327,6 +353,8 @@ class Content extends React.Component {
const { fragment } = state const { fragment } = state
const encoded = Base64.serializeNode(fragment) const encoded = Base64.serializeNode(fragment)
data.setData(TYPES.FRAGMENT, encoded) data.setData(TYPES.FRAGMENT, encoded)
debug('onDragStart')
} }
/** /**
@@ -413,6 +441,8 @@ class Content extends React.Component {
data.target = target data.target = target
data.effect = dataTransfer.dropEffect data.effect = dataTransfer.dropEffect
debug('onDrop', data)
this.props.onDrop(e, data) this.props.onDrop(e, data)
} }
@@ -424,8 +454,9 @@ class Content extends React.Component {
*/ */
onInput = (e) => { onInput = (e) => {
if (this.isComposing) return if (this.tmp.isComposing) return
if (isNonEditable(e)) return if (isNonEditable(e)) return
debug('onInput')
let { state, renderDecorations } = this.props let { state, renderDecorations } = this.props
const { selection } = state const { selection } = state
@@ -483,6 +514,17 @@ class Content extends React.Component {
const key = keycode(e.which) const key = keycode(e.which)
const data = {} const data = {}
// When composing, these characters commit the composition but also move the
// selection before we're able to handle it, so prevent their default,
// selection-moving behavior.
if (
this.tmp.isComposing &&
(key == 'left' || key == 'right' || key == 'up' || key == 'down')
) {
e.preventDefault()
return
}
// Add helpful properties for handling hotkeys to the data object. // Add helpful properties for handling hotkeys to the data object.
data.code = e.which data.code = e.which
data.key = key data.key = key
@@ -495,17 +537,6 @@ class Content extends React.Component {
data.isShift = e.shiftKey data.isShift = e.shiftKey
data.isWord = IS_MAC ? e.altKey : e.ctrlKey data.isWord = IS_MAC ? e.altKey : e.ctrlKey
// When composing, these characters commit the composition but also move the
// selection before we're able to handle it, so prevent their default,
// selection-moving behavior.
if (
this.tmp.isComposing &&
(key == 'left' || key == 'right' || key == 'up' || key == 'down')
) {
e.preventDefault()
return
}
// These key commands have native behavior in contenteditable elements which // These key commands have native behavior in contenteditable elements which
// will cause our state to be out of sync, so prevent them. // will cause our state to be out of sync, so prevent them.
if ( if (
@@ -520,6 +551,7 @@ class Content extends React.Component {
e.preventDefault() e.preventDefault()
} }
debug('onKeyDown', data)
this.props.onKeyDown(e, data) this.props.onKeyDown(e, data)
} }
@@ -571,6 +603,7 @@ class Content extends React.Component {
data.fragment = Base64.deserializeNode(encoded) data.fragment = Base64.deserializeNode(encoded)
} }
debug('onPaste', data)
this.props.onPaste(e, data) this.props.onPaste(e, data)
} }
@@ -631,6 +664,7 @@ class Content extends React.Component {
.normalize(document) .normalize(document)
} }
debug('onSelect', data)
this.props.onSelect(e, data) this.props.onSelect(e, data)
} }
@@ -641,6 +675,8 @@ class Content extends React.Component {
*/ */
render = () => { render = () => {
debug('render')
const { className, readOnly, state } = this.props const { className, readOnly, state } = this.props
const { document } = state const { document } = state
const children = document.nodes const children = document.nodes

View File

@@ -3,8 +3,15 @@ import Content from './content'
import CorePlugin from '../plugins/core' import CorePlugin from '../plugins/core'
import React from 'react' import React from 'react'
import State from '../models/state' import State from '../models/state'
import Debug from 'debug'
import typeOf from 'type-of' import typeOf from 'type-of'
/**
* Debug.
*/
const debug = Debug('slate:editor')
/** /**
* Noop. * Noop.
* *
@@ -197,6 +204,8 @@ class Editor extends React.Component {
*/ */
render = () => { render = () => {
debug('render')
const handlers = {} const handlers = {}
for (const property of EVENT_HANDLERS) { for (const property of EVENT_HANDLERS) {

View File

@@ -1,16 +1,29 @@
import Debug from 'debug'
import OffsetKey from '../utils/offset-key' import OffsetKey from '../utils/offset-key'
import React from 'react' import React from 'react'
import ReactDOM from 'react-dom' import ReactDOM from 'react-dom'
/**
* Debugger.
*
* @type {Function}
*/
const debug = Debug('slate:leaf')
/** /**
* Leaf. * Leaf.
*
* @type {Component}
*/ */
class Leaf extends React.Component { class Leaf extends React.Component {
/** /**
* Properties. * Property types.
*
* @type {Object}
*/ */
static propTypes = { static propTypes = {
@@ -35,6 +48,17 @@ class Leaf extends React.Component {
this.tmp.renders = 0 this.tmp.renders = 0
} }
/**
* Debug.
*
* @param {String} message
* @param {Mixed} ...args
*/
debug = (message, ...args) => {
debug(message, `${this.props.node.key}-${this.props.index}`, ...args)
}
/** /**
* Should component update? * Should component update?
* *
@@ -43,9 +67,6 @@ class Leaf extends React.Component {
*/ */
shouldComponentUpdate(props) { shouldComponentUpdate(props) {
const { index, node, state } = props
const { selection } = state
if ( if (
props.index != this.props.index || props.index != this.props.index ||
props.marks != this.props.marks || props.marks != this.props.marks ||
@@ -55,9 +76,13 @@ class Leaf extends React.Component {
return true return true
} }
if (state.isBlurred) return false if (props.state.isBlurred) {
return false
}
const { index, node, state } = props
const { start, end } = OffsetKey.findBounds(index, props.ranges) const { start, end } = OffsetKey.findBounds(index, props.ranges)
return selection.hasEdgeBetween(node, start, end) return state.selection.hasEdgeBetween(node, start, end)
} }
componentDidMount() { componentDidMount() {
@@ -132,9 +157,13 @@ class Leaf extends React.Component {
native.extend(endNode, endOffset) native.extend(endNode, endOffset)
} }
} }
this.debug('updateSelection')
} }
render() { render() {
this.debug('render')
const { node, index } = this.props const { node, index } = this.props
const offsetKey = OffsetKey.stringify({ const offsetKey = OffsetKey.stringify({
key: node.key, key: node.key,

View File

@@ -1,9 +1,18 @@
import Base64 from '../serializers/base-64' import Base64 from '../serializers/base-64'
import Debug from 'debug'
import React from 'react' import React from 'react'
import TYPES from '../utils/types' import TYPES from '../utils/types'
import Text from './text' import Text from './text'
/**
* Debugger.
*
* @type {Function}
*/
const debug = Debug('slate:node')
/** /**
* Node. * Node.
* *
@@ -25,6 +34,28 @@ class Node extends React.Component {
style: {} style: {}
} }
/**
* Debug.
*
* @param {String} message
* @param {Mixed} ...args
*/
debug = (message, ...args) => {
const { node } = this.props
const { key, kind, type } = node
let id = kind == 'text' ? `${key} (${kind})` : `${key} (${type})`
debug(message, `${id}`, ...args)
}
/**
* Should the node update?
*
* @param {Object} props
* @param {Object} state
* @return {Boolean}
*/
shouldComponentUpdate = (props) => { shouldComponentUpdate = (props) => {
return ( return (
props.node != this.props.node || props.node != this.props.node ||
@@ -43,6 +74,8 @@ class Node extends React.Component {
const encoded = Base64.serializeNode(node) const encoded = Base64.serializeNode(node)
const data = e.nativeEvent.dataTransfer const data = e.nativeEvent.dataTransfer
data.setData(TYPES.NODE, encoded) data.setData(TYPES.NODE, encoded)
this.debug('onDragStart', e)
} }
/** /**
@@ -52,6 +85,8 @@ class Node extends React.Component {
*/ */
render = () => { render = () => {
this.debug('render')
const { node } = this.props const { node } = this.props
return node.kind == 'text' return node.kind == 'text'
? this.renderText() ? this.renderText()

View File

@@ -7,6 +7,7 @@
"main": "./dist/index.js", "main": "./dist/index.js",
"dependencies": { "dependencies": {
"cheerio": "^0.20.0", "cheerio": "^0.20.0",
"debug": "^2.2.0",
"detect-browser": "^1.3.3", "detect-browser": "^1.3.3",
"direction": "^0.1.5", "direction": "^0.1.5",
"esrever": "^0.2.0", "esrever": "^0.2.0",
@@ -27,20 +28,17 @@
"babel-cli": "^6.10.1", "babel-cli": "^6.10.1",
"babel-core": "^6.9.1", "babel-core": "^6.9.1",
"babel-eslint": "^6.1.0", "babel-eslint": "^6.1.0",
"babel-polyfill": "^6.9.1",
"babel-preset-es2015": "^6.9.0", "babel-preset-es2015": "^6.9.0",
"babel-preset-react": "^6.5.0", "babel-preset-react": "^6.5.0",
"babel-preset-stage-0": "^6.5.0", "babel-preset-stage-0": "^6.5.0",
"babelify": "^7.3.0", "babelify": "^7.3.0",
"browserify": "^13.0.1", "browserify": "^13.0.1",
"browserify-shim": "^3.8.12", "browserify-shim": "^3.8.12",
"component-type": "^1.2.1",
"disc": "^1.3.2", "disc": "^1.3.2",
"envify": "^3.4.1", "envify": "^3.4.1",
"eslint": "^3.0.1", "eslint": "^3.0.1",
"eslint-plugin-import": "^1.10.2", "eslint-plugin-import": "^1.10.2",
"eslint-plugin-react": "^5.2.2", "eslint-plugin-react": "^5.2.2",
"exorcist": "^0.4.0",
"gh-pages": "^0.11.0", "gh-pages": "^0.11.0",
"http-server": "^0.9.0", "http-server": "^0.9.0",
"is-image": "^1.0.1", "is-image": "^1.0.1",

View File

@@ -1,6 +1,6 @@
import assert from 'assert' import assert from 'assert'
import type from 'component-type' import type from 'type-of'
/** /**
* Assertion error. * Assertion error.