1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-21 14:41:23 +02:00

Hook debug-mutations into lifecycle events (#2841)

This commit is contained in:
Sunny Hirai
2019-05-28 18:10:50 -07:00
committed by GitHub
parent 2265240c04
commit 78264cf117

View File

@@ -60,10 +60,25 @@ function DebugMutationsPlugin({ editor }) {
debug(`${array.length} Mutations`, ...array)
})
// `findDOMNode` does not exist until later so we use `setTimeout`
setTimeout(() => {
/**
* The previously observed DOM node
*
* @type {DOMNode}
*/
let prevRootEl = null
/**
* Start observing the DOM node for mutations if it isn't being observed
*/
function start() {
const rootEl = editor.findDOMNode([])
if (rootEl === prevRootEl) return
debug('start')
observer.observe(rootEl, {
childList: true,
characterData: true,
@@ -71,7 +86,26 @@ function DebugMutationsPlugin({ editor }) {
subtree: true,
characterDataOldValue: true,
})
})
prevRootEl = rootEl
}
/**
* Stop observing the DOM node for mutations
*/
function stop() {
debug('stop')
observer.disconnect()
prevRootEl = null
}
return {
onComponentDidMount: start,
onComponentDidUpdate: start,
onComponentWillUnmount: stop,
}
}
export default DebugMutationsPlugin