mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-17 20:51:20 +02:00
Reconcile node (#2802)
* Add reconcileNode command * Fix lint errors * Remove node.path shortcut as path is not a property anymore
This commit is contained in:
@@ -6,21 +6,20 @@
|
|||||||
|
|
||||||
function CommandsPlugin() {
|
function CommandsPlugin() {
|
||||||
/**
|
/**
|
||||||
* reconcileDOMNode takes text from inside the `domNode` and uses it to set
|
* Takes a `node`, find the matching `domNode` and uses it to set the text
|
||||||
* the text inside the matching `node` in Slate.
|
* in the `node`.
|
||||||
*
|
*
|
||||||
* @param {Window} window
|
|
||||||
* @param {Editor} editor
|
* @param {Editor} editor
|
||||||
* @param {Node} domNode
|
* @param {Node} node
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function reconcileDOMNode(editor, domNode) {
|
function reconcileNode(editor, node) {
|
||||||
const { value } = editor
|
const { value } = editor
|
||||||
const { document, selection } = value
|
const { document, selection } = value
|
||||||
const domElement = domNode.parentElement.closest('[data-key]')
|
const path = document.getPath(node.key)
|
||||||
const point = editor.findPoint(domElement, 0)
|
|
||||||
const node = document.getDescendant(point.path)
|
const domElement = editor.findDOMNode(path)
|
||||||
const block = document.getClosestBlock(point.path)
|
const block = document.getClosestBlock(path)
|
||||||
|
|
||||||
// Get text information
|
// Get text information
|
||||||
const { text } = node
|
const { text } = node
|
||||||
@@ -39,9 +38,7 @@ function CommandsPlugin() {
|
|||||||
// If the text is no different, abort.
|
// If the text is no different, abort.
|
||||||
if (text === domText) return
|
if (text === domText) return
|
||||||
|
|
||||||
let entire = selection
|
let entire = selection.moveAnchorTo(path, 0).moveFocusTo(path, text.length)
|
||||||
.moveAnchorTo(point.path, 0)
|
|
||||||
.moveFocusTo(point.path, text.length)
|
|
||||||
|
|
||||||
entire = document.resolveRange(entire)
|
entire = document.resolveRange(entire)
|
||||||
|
|
||||||
@@ -50,8 +47,23 @@ function CommandsPlugin() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Takes text from the `domNode` and uses it to set the text in the matching
|
||||||
|
* `node` in Slate.
|
||||||
|
*
|
||||||
|
* @param {Editor} editor
|
||||||
|
* @param {DOMNode} domNode
|
||||||
|
*/
|
||||||
|
|
||||||
|
function reconcileDOMNode(editor, domNode) {
|
||||||
|
const domElement = domNode.parentElement.closest('[data-key]')
|
||||||
|
const node = editor.findNode(domElement)
|
||||||
|
editor.reconcileNode(node)
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
commands: {
|
commands: {
|
||||||
|
reconcileNode,
|
||||||
reconcileDOMNode,
|
reconcileDOMNode,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user