1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-02-24 09:13:24 +01:00
slate/lib/transforms/by-key.js
2016-08-30 14:36:31 -07:00

182 lines
4.5 KiB
JavaScript

import Normalize from '../utils/normalize'
import uid from '../utils/uid'
/**
* Add mark to text at `offset` and `length` in node by `key`.
*
* @param {Transform} transform
* @param {String} key
* @param {Number} offset
* @param {Number} length
* @param {Mixed} mark
* @return {Transform}
*/
export function addMarkByKey(transform, key, offset, length, mark) {
mark = Normalize.mark(mark)
const { state } = transform
const { document } = state
const path = document.getPath(key)
return transform.addMarkOperation(path, offset, length, mark)
}
/**
* Insert a `node` at `index` in a node by `key`.
*
* @param {Transform} transform
* @param {String} key
* @param {Number} index
* @param {Node} node
* @return {Transform}
*/
export function insertNodeByKey(transform, key, index, node) {
const { state } = transform
const { document } = state
const path = document.getPath(key)
const newPath = path.slice().push(index)
return transform.insertNodeOperation(path, index, node)
}
/**
* Insert `text` at `offset` in node by `key`.
*
* @param {Transform} transform
* @param {String} key
* @param {Number} offset
* @param {String} text
* @param {Set} marks (optional)
* @return {Transform}
*/
export function insertTextByKey(transform, key, offset, text, marks) {
const { state } = transform
const { document } = state
const path = document.getPath(key)
return transform.insertTextOperation(path, offset, text, marks)
}
/**
* Move a node by `key` to a new parent by `key` and `index`.
*
* @param {Transform} transform
* @param {String} key
* @param {String} newKey
* @param {Number} index
* @return {Transform}
*/
export function moveNodeByKey(transform, key, newKey, newIndex) {
const { state } = transform
const { document } = state
const path = document.getPath(key)
const newPath = document.getPath(newKey)
return transform.moveNodeOperation(path, newPath, newIndex)
}
/**
* Remove mark from text at `offset` and `length` in node by `key`.
*
* @param {Transform} transform
* @param {String} key
* @param {Number} offset
* @param {Number} length
* @param {Mark} mark
* @return {Transform}
*/
export function removeMarkByKey(transform, key, offset, length, mark) {
mark = Normalize.mark(mark)
const { state } = transform
const { document } = state
const path = document.getPath(key)
return transform.removeMarkOperation(path, offset, length, mark)
}
/**
* Remove a node by `key`.
*
* @param {Transform} transform
* @param {String} key
* @return {Transform}
*/
export function removeNodeByKey(transform, key) {
const { state } = transform
const { document } = state
const path = document.getPath(key)
return transform.removeNodeOperation(path)
}
/**
* Remove text at `offset` and `length` in node by `key`.
*
* @param {Transform} transform
* @param {String} key
* @param {Number} offset
* @param {Number} length
* @return {Transform}
*/
export function removeTextByKey(transform, key, offset, length) {
const { state } = transform
const { document } = state
const path = document.getPath(key)
return transform.removeTextOperation(path, offset, length)
}
/**
* Set `properties` on mark on text at `offset` and `length` in node by `key`.
*
* @param {Transform} transform
* @param {String} key
* @param {Number} offset
* @param {Number} length
* @param {Mark} mark
* @return {Transform}
*/
export function setMarkByKey(transform, key, offset, length, mark, properties) {
mark = Normalize.mark(mark)
properties = Normalize.markProperties(properties)
const { state } = transform
const { document } = state
const path = document.getPath(key)
return transform.setMarkOperation(path, offset, length, mark, properties)
}
/**
* Set `properties` on a node by `key`.
*
* @param {Transform} transform
* @param {String} key
* @param {Object || String} properties
* @return {Transform}
*/
export function setNodeByKey(transform, key, properties) {
properties = Normalize.nodeProperties(properties)
const { state } = transform
const { document } = state
const node = document.assertDescendant(key)
const path = document.getPath(key)
return transform.setNodeOperation(path, properties)
}
/**
* Split a node by `key` at `offset`.
*
* @param {Transform} transform
* @param {String} key
* @param {Number} offset
* @return {Transform}
*/
export function splitNodeByKey(transform, key, offset) {
const { state } = transform
const { document } = state
const path = document.getPath(key)
return transform.splitNodeOperation(path, offset)
}