mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-09-02 11:42:53 +02:00
refactor transforms
This commit is contained in:
@@ -387,6 +387,9 @@ const Node = {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
getCommonAncestor(one, two) {
|
getCommonAncestor(one, two) {
|
||||||
|
if (one == this.key) return this
|
||||||
|
if (two == this.key) return this
|
||||||
|
|
||||||
this.assertDescendant(one)
|
this.assertDescendant(one)
|
||||||
this.assertDescendant(two)
|
this.assertDescendant(two)
|
||||||
let ancestors = new List()
|
let ancestors = new List()
|
||||||
|
@@ -99,7 +99,8 @@ class Transform {
|
|||||||
Object.keys(Transforms).forEach((type) => {
|
Object.keys(Transforms).forEach((type) => {
|
||||||
Transform.prototype[type] = function (...args) {
|
Transform.prototype[type] = function (...args) {
|
||||||
debug(type, { args })
|
debug(type, { args })
|
||||||
return Transforms[type](this, ...args)
|
Transforms[type](this, ...args)
|
||||||
|
return this
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@@ -12,7 +12,6 @@ import SCHEMA from '../schemas/core'
|
|||||||
* @param {Mixed} mark
|
* @param {Mixed} mark
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @param {Boolean} normalize
|
* @param {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function addMarkByKey(transform, key, offset, length, mark, options = {}) {
|
export function addMarkByKey(transform, key, offset, length, mark, options = {}) {
|
||||||
@@ -28,8 +27,6 @@ export function addMarkByKey(transform, key, offset, length, mark, options = {})
|
|||||||
const parent = document.getParent(key)
|
const parent = document.getParent(key)
|
||||||
transform.normalizeNodeByKey(parent.key, SCHEMA)
|
transform.normalizeNodeByKey(parent.key, SCHEMA)
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -41,7 +38,6 @@ export function addMarkByKey(transform, key, offset, length, mark, options = {})
|
|||||||
* @param {Node} node
|
* @param {Node} node
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @param {Boolean} normalize
|
* @param {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function insertNodeByKey(transform, key, index, node, options = {}) {
|
export function insertNodeByKey(transform, key, index, node, options = {}) {
|
||||||
@@ -55,8 +51,6 @@ export function insertNodeByKey(transform, key, index, node, options = {}) {
|
|||||||
if (normalize) {
|
if (normalize) {
|
||||||
transform.normalizeNodeByKey(key, SCHEMA)
|
transform.normalizeNodeByKey(key, SCHEMA)
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -69,7 +63,6 @@ export function insertNodeByKey(transform, key, index, node, options = {}) {
|
|||||||
* @param {Set<Mark>} marks (optional)
|
* @param {Set<Mark>} marks (optional)
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @param {Boolean} normalize
|
* @param {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function insertTextByKey(transform, key, offset, text, marks, options = {}) {
|
export function insertTextByKey(transform, key, offset, text, marks, options = {}) {
|
||||||
@@ -84,8 +77,6 @@ export function insertTextByKey(transform, key, offset, text, marks, options = {
|
|||||||
const parent = document.getParent(key)
|
const parent = document.getParent(key)
|
||||||
transform.normalizeNodeByKey(parent.key, SCHEMA)
|
transform.normalizeNodeByKey(parent.key, SCHEMA)
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -96,7 +87,6 @@ export function insertTextByKey(transform, key, offset, text, marks, options = {
|
|||||||
* @param {String} withKey
|
* @param {String} withKey
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @param {Boolean} normalize
|
* @param {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function joinNodeByKey(transform, key, withKey, options = {}) {
|
export function joinNodeByKey(transform, key, withKey, options = {}) {
|
||||||
@@ -110,16 +100,10 @@ export function joinNodeByKey(transform, key, withKey, options = {}) {
|
|||||||
|
|
||||||
if (normalize) {
|
if (normalize) {
|
||||||
const parent = document.getCommonAncestor(key, withKey)
|
const parent = document.getCommonAncestor(key, withKey)
|
||||||
if (parent) {
|
|
||||||
transform.normalizeNodeByKey(parent.key, SCHEMA)
|
transform.normalizeNodeByKey(parent.key, SCHEMA)
|
||||||
} else {
|
|
||||||
transform.normalizeDocument(SCHEMA)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move a node by `key` to a new parent by `newKey` and `index`.
|
* Move a node by `key` to a new parent by `newKey` and `index`.
|
||||||
* `newKey` is the key of the container (it can be the document itself)
|
* `newKey` is the key of the container (it can be the document itself)
|
||||||
@@ -130,7 +114,6 @@ export function joinNodeByKey(transform, key, withKey, options = {}) {
|
|||||||
* @param {Number} index
|
* @param {Number} index
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @param {Boolean} normalize
|
* @param {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function moveNodeByKey(transform, key, newKey, newIndex, options = {}) {
|
export function moveNodeByKey(transform, key, newKey, newIndex, options = {}) {
|
||||||
@@ -143,11 +126,9 @@ export function moveNodeByKey(transform, key, newKey, newIndex, options = {}) {
|
|||||||
transform.moveNodeOperation(path, newPath, newIndex)
|
transform.moveNodeOperation(path, newPath, newIndex)
|
||||||
|
|
||||||
if (normalize) {
|
if (normalize) {
|
||||||
const parent = document.key == newKey ? document : document.getCommonAncestor(key, newKey)
|
const parent = document.getCommonAncestor(key, newKey)
|
||||||
transform.normalizeNodeByKey(parent.key, SCHEMA)
|
transform.normalizeNodeByKey(parent.key, SCHEMA)
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -160,7 +141,6 @@ export function moveNodeByKey(transform, key, newKey, newIndex, options = {}) {
|
|||||||
* @param {Mark} mark
|
* @param {Mark} mark
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @param {Boolean} normalize
|
* @param {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function removeMarkByKey(transform, key, offset, length, mark, options = {}) {
|
export function removeMarkByKey(transform, key, offset, length, mark, options = {}) {
|
||||||
@@ -176,8 +156,6 @@ export function removeMarkByKey(transform, key, offset, length, mark, options =
|
|||||||
const parent = document.getParent(key)
|
const parent = document.getParent(key)
|
||||||
transform.normalizeNodeByKey(parent.key, SCHEMA)
|
transform.normalizeNodeByKey(parent.key, SCHEMA)
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -187,7 +165,6 @@ export function removeMarkByKey(transform, key, offset, length, mark, options =
|
|||||||
* @param {String} key
|
* @param {String} key
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @param {Boolean} normalize
|
* @param {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function removeNodeByKey(transform, key, options = {}) {
|
export function removeNodeByKey(transform, key, options = {}) {
|
||||||
@@ -200,16 +177,10 @@ export function removeNodeByKey(transform, key, options = {}) {
|
|||||||
|
|
||||||
if (normalize) {
|
if (normalize) {
|
||||||
const parent = document.getParent(key)
|
const parent = document.getParent(key)
|
||||||
if (parent) {
|
|
||||||
transform.normalizeNodeByKey(parent.key, SCHEMA)
|
transform.normalizeNodeByKey(parent.key, SCHEMA)
|
||||||
} else {
|
|
||||||
transform.normalizeDocument(SCHEMA)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove text at `offset` and `length` in node by `key`.
|
* Remove text at `offset` and `length` in node by `key`.
|
||||||
*
|
*
|
||||||
@@ -219,7 +190,6 @@ export function removeNodeByKey(transform, key, options = {}) {
|
|||||||
* @param {Number} length
|
* @param {Number} length
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @param {Boolean} normalize
|
* @param {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function removeTextByKey(transform, key, offset, length, options = {}) {
|
export function removeTextByKey(transform, key, offset, length, options = {}) {
|
||||||
@@ -234,8 +204,6 @@ export function removeTextByKey(transform, key, offset, length, options = {}) {
|
|||||||
const parent = document.getParent(key)
|
const parent = document.getParent(key)
|
||||||
transform.normalizeParentsByKey(parent.key, SCHEMA)
|
transform.normalizeParentsByKey(parent.key, SCHEMA)
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -248,7 +216,6 @@ export function removeTextByKey(transform, key, offset, length, options = {}) {
|
|||||||
* @param {Mark} mark
|
* @param {Mark} mark
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @param {Boolean} normalize
|
* @param {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function setMarkByKey(transform, key, offset, length, mark, properties, options = {}) {
|
export function setMarkByKey(transform, key, offset, length, mark, properties, options = {}) {
|
||||||
@@ -266,8 +233,6 @@ export function setMarkByKey(transform, key, offset, length, mark, properties, o
|
|||||||
const parent = document.getParent(key)
|
const parent = document.getParent(key)
|
||||||
transform.normalizeNodeByKey(parent.key, SCHEMA)
|
transform.normalizeNodeByKey(parent.key, SCHEMA)
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -278,7 +243,6 @@ export function setMarkByKey(transform, key, offset, length, mark, properties, o
|
|||||||
* @param {Object|String} properties
|
* @param {Object|String} properties
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @param {Boolean} normalize
|
* @param {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function setNodeByKey(transform, key, properties, options = {}) {
|
export function setNodeByKey(transform, key, properties, options = {}) {
|
||||||
@@ -292,16 +256,10 @@ export function setNodeByKey(transform, key, properties, options = {}) {
|
|||||||
|
|
||||||
if (normalize) {
|
if (normalize) {
|
||||||
const parent = document.getParent(key)
|
const parent = document.getParent(key)
|
||||||
if (parent) {
|
|
||||||
transform.normalizeNodeByKey(parent.key, SCHEMA)
|
transform.normalizeNodeByKey(parent.key, SCHEMA)
|
||||||
} else {
|
|
||||||
transform.normalizeDocument(SCHEMA)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Split a node by `key` at `offset`.
|
* Split a node by `key` at `offset`.
|
||||||
*
|
*
|
||||||
@@ -310,7 +268,6 @@ export function setNodeByKey(transform, key, properties, options = {}) {
|
|||||||
* @param {Number} offset
|
* @param {Number} offset
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @param {Boolean} normalize
|
* @param {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function splitNodeByKey(transform, key, offset, options = {}) {
|
export function splitNodeByKey(transform, key, offset, options = {}) {
|
||||||
@@ -323,16 +280,10 @@ export function splitNodeByKey(transform, key, offset, options = {}) {
|
|||||||
|
|
||||||
if (normalize) {
|
if (normalize) {
|
||||||
const parent = document.getParent(key)
|
const parent = document.getParent(key)
|
||||||
if (parent) {
|
|
||||||
transform.normalizeNodeByKey(parent.key, SCHEMA)
|
transform.normalizeNodeByKey(parent.key, SCHEMA)
|
||||||
} else {
|
|
||||||
transform.normalizeDocument(SCHEMA)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unwrap content from an inline parent with `properties`.
|
* Unwrap content from an inline parent with `properties`.
|
||||||
*
|
*
|
||||||
@@ -341,15 +292,16 @@ export function splitNodeByKey(transform, key, offset, options = {}) {
|
|||||||
* @param {Object|String} properties
|
* @param {Object|String} properties
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @param {Boolean} normalize
|
* @param {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function unwrapInlineByKey(transform, key, properties, options) {
|
export function unwrapInlineByKey(transform, key, properties, options) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { document, selection } = state
|
const { document, selection } = state
|
||||||
const node = document.assertDescendant(key)
|
const node = document.assertDescendant(key)
|
||||||
const range = selection.moveToRangeOf(node.getFirstText(), node.getLastText())
|
const first = node.getFirstText()
|
||||||
return transform.unwrapInlineAtRange(range, properties, options)
|
const last = node.getLastText()
|
||||||
|
const range = selection.moveToRangeOf(first, last)
|
||||||
|
transform.unwrapInlineAtRange(range, properties, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -360,15 +312,16 @@ export function unwrapInlineByKey(transform, key, properties, options) {
|
|||||||
* @param {Object|String} properties
|
* @param {Object|String} properties
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @param {Boolean} normalize
|
* @param {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function unwrapBlockByKey(transform, key, properties, options) {
|
export function unwrapBlockByKey(transform, key, properties, options) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { document, selection } = state
|
const { document, selection } = state
|
||||||
const node = document.assertDescendant(key)
|
const node = document.assertDescendant(key)
|
||||||
const range = selection.moveToRangeOf(node.getFirstText(), node.getLastText())
|
const first = node.getFirstText()
|
||||||
return transform.unwrapBlockAtRange(range, properties, options)
|
const last = node.getLastText()
|
||||||
|
const range = selection.moveToRangeOf(first, last)
|
||||||
|
transform.unwrapBlockAtRange(range, properties, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -379,7 +332,6 @@ export function unwrapBlockByKey(transform, key, properties, options) {
|
|||||||
* @param {Block|Object|String} block The wrapping block (its children are discarded)
|
* @param {Block|Object|String} block The wrapping block (its children are discarded)
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @param {Boolean} normalize
|
* @param {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function wrapBlockByKey(transform, key, block, options) {
|
export function wrapBlockByKey(transform, key, block, options) {
|
||||||
@@ -391,7 +343,6 @@ export function wrapBlockByKey(transform, key, block, options) {
|
|||||||
const parent = document.getParent(node.key)
|
const parent = document.getParent(node.key)
|
||||||
const index = parent.nodes.indexOf(node)
|
const index = parent.nodes.indexOf(node)
|
||||||
|
|
||||||
return transform
|
transform.insertNodeByKey(parent.key, index, block, { normalize: false })
|
||||||
.insertNodeByKey(parent.key, index, block, { normalize: false })
|
transform.moveNodeByKey(node.key, block.key, 0, options)
|
||||||
.moveNodeByKey(node.key, block.key, 0, options)
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user