1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-17 20:51:20 +02:00

Debug mutations (#2815)

* Add noop plugin

* Add debug mutations
This commit is contained in:
Sunny Hirai
2019-05-21 17:34:22 -07:00
committed by GitHub
parent b71fdbbb3d
commit 5948738092
3 changed files with 92 additions and 0 deletions

View File

@@ -219,6 +219,7 @@ class Editor extends React.Component {
this.tmp.resolves++ this.tmp.resolves++
const react = ReactPlugin({ const react = ReactPlugin({
...this.props, ...this.props,
__editor__: this,
value: this.props.value || this.state.value, value: this.props.value || this.state.value,
}) })

View File

@@ -0,0 +1,86 @@
import Debug from 'debug'
const debug = Debug('slate:mutations')
/**
* Options for MutationObserver
*
* @type {Object}
*/
const observeOptions = {
childList: true,
characterData: true,
attributes: true,
subtree: true,
characterDataOldValue: true,
}
/**
* Properties on a MutationRecord
*
* @type {Object}
*/
const keys = [
'type',
'oldValue',
'target',
'addedNodes',
'removedNodes',
'attributeName',
'attributeNamespace',
'nextSibling',
'previousSibling',
]
/**
* Takes a mutation record and turns it into an Object that is easy to log to
* the console. Notably, it removes `null` and `undefined` values as well as
* values that are a `NodeList` that has no entries.
*
* @param {MutationRecord} mutationRecord
*/
function normalize(mutationRecord) {
const object = {}
keys.forEach(key => {
const value = mutationRecord[key]
if (value == null) return
if (value instanceof window.NodeList && value.length === 0) return
object[key] = value
})
return object
}
/**
* A plugin that sends short easy to digest debug info about each dom mutation
* to browser.
*
* More information about mutations here:
*
* <https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver>
* <https://developer.mozilla.org/en-US/docs/Web/API/MutationRecord>
*
* @param {Object} options
*/
function DebugMutationsPlugin({ __editor__ }) {
const observer = new window.MutationObserver(flush)
function flush(mutations) {
const array = Array.from(mutations).map(normalize)
debug(`${array.length} MutationRecord`)
// Used `console` because `debug` was not easy to view succinctly
window.console.log(...array)
}
// `findDOMNode` does not exist until later so we use `setTimeout`
setTimeout(() => {
const rootEl = __editor__.findDOMNode([])
observer.observe(rootEl, observeOptions)
})
}
export default DebugMutationsPlugin

View File

@@ -9,6 +9,7 @@ import DOMPlugin from '../dom'
import RestoreDOMPlugin from './restore-dom' import RestoreDOMPlugin from './restore-dom'
import DebugEventsPlugin from '../debug/debug-events' import DebugEventsPlugin from '../debug/debug-events'
import DebugBatchEventsPlugin from '../debug/debug-batch-events' import DebugBatchEventsPlugin from '../debug/debug-batch-events'
import DebugMutationsPlugin from '../debug/debug-mutations'
import NoopPlugin from '../debug/noop' import NoopPlugin from '../debug/noop'
/** /**
@@ -26,6 +27,9 @@ function ReactPlugin(options = {}) {
const debugBatchEventsPlugin = Debug.enabled('slate:batch-events') const debugBatchEventsPlugin = Debug.enabled('slate:batch-events')
? DebugBatchEventsPlugin(options) ? DebugBatchEventsPlugin(options)
: null : null
const debugMutationsPlugin = Debug.enabled('slate:mutations')
? DebugMutationsPlugin(options)
: null
const noopPlugin = Debug.enabled('slate:noop') ? NoopPlugin(options) : null const noopPlugin = Debug.enabled('slate:noop') ? NoopPlugin(options) : null
const renderingPlugin = RenderingPlugin(options) const renderingPlugin = RenderingPlugin(options)
const commandsPlugin = CommandsPlugin(options) const commandsPlugin = CommandsPlugin(options)
@@ -47,6 +51,7 @@ function ReactPlugin(options = {}) {
return [ return [
debugEventsPlugin, debugEventsPlugin,
debugBatchEventsPlugin, debugBatchEventsPlugin,
debugMutationsPlugin,
noopPlugin, noopPlugin,
domPlugin, domPlugin,
restoreDomPlugin, restoreDomPlugin,