1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-24 16:02:55 +02:00

fix onBeforeInput decorations logic, fix initial onSelect logic

This commit is contained in:
Ian Storm Taylor
2016-08-01 18:09:30 -07:00
parent 13e7b96dc8
commit 4e6ca3953b
2 changed files with 59 additions and 10 deletions

View File

@@ -127,7 +127,7 @@ class Content extends React.Component {
componentDidUpdate = (props, state) => { componentDidUpdate = (props, state) => {
setTimeout(() => { setTimeout(() => {
this.tmp.isRendering = false this.tmp.isRendering = false
}) }, 1)
} }
/** /**
@@ -454,6 +454,7 @@ class Content extends React.Component {
*/ */
onInput = (e) => { onInput = (e) => {
if (this.tmp.isRendering) return
if (this.tmp.isComposing) return if (this.tmp.isComposing) return
if (isNonEditable(e)) return if (isNonEditable(e)) return
debug('onInput') debug('onInput')
@@ -648,7 +649,8 @@ class Content extends React.Component {
anchor.key == selection.anchorKey && anchor.key == selection.anchorKey &&
anchor.offset == selection.anchorOffset && anchor.offset == selection.anchorOffset &&
focus.key == selection.focusKey && focus.key == selection.focusKey &&
focus.offset == selection.focusOffset focus.offset == selection.focusOffset &&
selection.isFocused
) { ) {
return return
} }
@@ -667,7 +669,7 @@ class Content extends React.Component {
.normalize(document) .normalize(document)
} }
debug('onSelect', data) debug('onSelect', { data, selection: data.selection.toJS() })
this.props.onSelect(e, data) this.props.onSelect(e, data)
} }

View File

@@ -1,10 +1,19 @@
import Base64 from '../serializers/base-64' import Base64 from '../serializers/base-64'
import Character from '../models/character' import Character from '../models/character'
import Debug from 'debug'
import Placeholder from '../components/placeholder' import Placeholder from '../components/placeholder'
import React from 'react' import React from 'react'
import String from '../utils/string' import String from '../utils/string'
/**
* Debug.
*
* @type {Function}
*/
const debug = Debug('slate:core')
/** /**
* The default plugin. * The default plugin.
* *
@@ -79,12 +88,17 @@ function Plugin(options = {}) {
const { startOffset, startText, startBlock } = state const { startOffset, startText, startBlock } = state
// Determine what the characters would be if natively inserted. // Determine what the characters would be if natively inserted.
const prev = startText.getDecoratedCharacters(startBlock, renderDecorations) const prevChars = startText.getDecoratedCharacters(startBlock, renderDecorations)
const char = prev.get(startOffset) const prevChar = prevChars.get(startOffset - 1)
const chars = prev const char = Character.create({
text: e.data,
marks: prevChar && prevChar.marks
})
const chars = prevChars
.slice(0, startOffset) .slice(0, startOffset)
.push(Character.create({ text: e.data, marks: char && char.marks })) .push(char)
.concat(prev.slice(startOffset)) .concat(prevChars.slice(startOffset))
// Determine what the characters should be, if not natively inserted. // Determine what the characters should be, if not natively inserted.
let next = state let next = state
@@ -114,7 +128,7 @@ function Plugin(options = {}) {
// If not native, prevent default so that the DOM remains untouched. // If not native, prevent default so that the DOM remains untouched.
if (!isNative) e.preventDefault() if (!isNative) e.preventDefault()
// Return the new state. debug('onBeforeInput', { data, isNative })
return next return next
} }
@@ -128,10 +142,14 @@ function Plugin(options = {}) {
*/ */
function onBlur(e, data, state) { function onBlur(e, data, state) {
const isNative = true
debug('onBlur', { data, isNative })
return state return state
.transform() .transform()
.blur() .blur()
.apply({ isNative: true }) .apply({ isNative })
} }
/** /**
@@ -144,6 +162,7 @@ function Plugin(options = {}) {
*/ */
function onCopy(e, data, state) { function onCopy(e, data, state) {
debug('onCopy', data)
onCutOrCopy(e, data, state) onCutOrCopy(e, data, state)
} }
@@ -158,6 +177,7 @@ function Plugin(options = {}) {
*/ */
function onCut(e, data, state, editor) { function onCut(e, data, state, editor) {
debug('onCut', data)
onCutOrCopy(e, data, state) onCutOrCopy(e, data, state)
// Once the fake cut content has successfully been added to the clipboard, // Once the fake cut content has successfully been added to the clipboard,
@@ -238,6 +258,8 @@ function Plugin(options = {}) {
*/ */
function onDrop(e, data, state) { function onDrop(e, data, state) {
debug('onDrop', { data })
switch (data.type) { switch (data.type) {
case 'text': case 'text':
case 'html': case 'html':
@@ -257,6 +279,8 @@ function Plugin(options = {}) {
*/ */
function onDropFragment(e, data, state) { function onDropFragment(e, data, state) {
debug('onDropFragment', { data })
const { selection } = state const { selection } = state
let { fragment, target, isInternal } = data let { fragment, target, isInternal } = data
@@ -292,6 +316,8 @@ function Plugin(options = {}) {
*/ */
function onDropText(e, data, state) { function onDropText(e, data, state) {
debug('onDropText', { data })
const { text, target } = data const { text, target } = data
let transform = state let transform = state
.transform() .transform()
@@ -317,6 +343,8 @@ function Plugin(options = {}) {
*/ */
function onKeyDown(e, data, state) { function onKeyDown(e, data, state) {
debug('onKeyDown', { data })
switch (data.key) { switch (data.key) {
case 'enter': return onKeyDownEnter(e, data, state) case 'enter': return onKeyDownEnter(e, data, state)
case 'backspace': return onKeyDownBackspace(e, data, state) case 'backspace': return onKeyDownBackspace(e, data, state)
@@ -336,6 +364,8 @@ function Plugin(options = {}) {
*/ */
function onKeyDownEnter(e, data, state) { function onKeyDownEnter(e, data, state) {
debug('onKeyDownEnter', { data })
const { document, startKey, startBlock } = state const { document, startKey, startBlock } = state
// For void blocks, we don't want to split. Instead we just move to the // For void blocks, we don't want to split. Instead we just move to the
@@ -365,6 +395,7 @@ function Plugin(options = {}) {
*/ */
function onKeyDownBackspace(e, data, state) { function onKeyDownBackspace(e, data, state) {
debug('onKeyDownBackspace', { data })
// If expanded, delete regularly. // If expanded, delete regularly.
if (state.isExpanded) { if (state.isExpanded) {
@@ -407,6 +438,7 @@ function Plugin(options = {}) {
*/ */
function onKeyDownDelete(e, data, state) { function onKeyDownDelete(e, data, state) {
debug('onKeyDownDelete', { data })
// If expanded, delete regularly. // If expanded, delete regularly.
if (state.isExpanded) { if (state.isExpanded) {
@@ -450,6 +482,9 @@ function Plugin(options = {}) {
function onKeyDownY(e, data, state) { function onKeyDownY(e, data, state) {
if (!data.isMod) return if (!data.isMod) return
debug('onKeyDownY', { data })
return state return state
.transform() .transform()
.redo() .redo()
@@ -466,6 +501,9 @@ function Plugin(options = {}) {
function onKeyDownZ(e, data, state) { function onKeyDownZ(e, data, state) {
if (!data.isMod) return if (!data.isMod) return
debug('onKeyDownZ', { data })
return state return state
.transform() .transform()
[data.isShift ? 'redo' : 'undo']() [data.isShift ? 'redo' : 'undo']()
@@ -481,6 +519,8 @@ function Plugin(options = {}) {
*/ */
function onPaste(e, data, state) { function onPaste(e, data, state) {
debug('onPaste', { data })
switch (data.type) { switch (data.type) {
case 'fragment': case 'fragment':
return onPasteFragment(e, data, state) return onPasteFragment(e, data, state)
@@ -500,6 +540,8 @@ function Plugin(options = {}) {
*/ */
function onPasteFragment(e, data, state) { function onPasteFragment(e, data, state) {
debug('onPasteFragment', { data })
return state return state
.transform() .transform()
.insertFragment(data.fragment) .insertFragment(data.fragment)
@@ -516,6 +558,8 @@ function Plugin(options = {}) {
*/ */
function onPasteText(e, data, state) { function onPasteText(e, data, state) {
debug('onPasteText', { data })
let transform = state.transform() let transform = state.transform()
data.text data.text
@@ -539,6 +583,9 @@ function Plugin(options = {}) {
function onSelect(e, data, state) { function onSelect(e, data, state) {
const { selection } = data const { selection } = data
debug('onSelect', { data, selection: selection.toJS() })
return state return state
.transform() .transform()
.moveTo(selection) .moveTo(selection)