mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-20 14:11:35 +02:00
Merge pull request #447 from ianstormtaylor/refactor-transforms
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
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@@ -40,7 +40,6 @@ const OPERATIONS = {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Object} operation
|
* @param {Object} operation
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function applyOperation(transform, operation) {
|
export function applyOperation(transform, operation) {
|
||||||
@@ -53,11 +52,8 @@ export function applyOperation(transform, operation) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
debug(type, operation)
|
debug(type, operation)
|
||||||
|
|
||||||
transform.state = fn(state, operation)
|
transform.state = fn(state, operation)
|
||||||
transform.operations = operations.concat([operation])
|
transform.operations = operations.concat([operation])
|
||||||
|
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -289,16 +285,11 @@ function removeNode(state, operation) {
|
|||||||
|
|
||||||
function removeText(state, operation) {
|
function removeText(state, operation) {
|
||||||
const { path, offset, length } = operation
|
const { path, offset, length } = operation
|
||||||
|
const rangeOffset = offset + length
|
||||||
let { document, selection } = state
|
let { document, selection } = state
|
||||||
const { startKey, endKey, startOffset, endOffset } = selection
|
const { startKey, endKey, startOffset, endOffset } = selection
|
||||||
let node = document.assertPath(path)
|
let node = document.assertPath(path)
|
||||||
|
|
||||||
const rangeOffset = offset + length
|
|
||||||
|
|
||||||
// Update the document
|
|
||||||
node = node.removeText(offset, length)
|
|
||||||
document = document.updateDescendant(node)
|
|
||||||
|
|
||||||
// Update the selection
|
// Update the selection
|
||||||
if (startKey == node.key && startOffset >= rangeOffset) {
|
if (startKey == node.key && startOffset >= rangeOffset) {
|
||||||
selection = selection.moveStartOffset(-length)
|
selection = selection.moveStartOffset(-length)
|
||||||
@@ -307,6 +298,8 @@ function removeText(state, operation) {
|
|||||||
selection = selection.moveEndOffset(-length)
|
selection = selection.moveEndOffset(-length)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
node = node.removeText(offset, length)
|
||||||
|
document = document.updateDescendant(node)
|
||||||
state = state.merge({ document, selection })
|
state = state.merge({ document, selection })
|
||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
|
@@ -6,99 +6,48 @@ import Normalize from '../utils/normalize'
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Mark} mark
|
* @param {Mark} mark
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function addMark(transform, mark) {
|
export function addMark(transform, mark) {
|
||||||
mark = Normalize.mark(mark)
|
mark = Normalize.mark(mark)
|
||||||
|
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { document, selection } = state
|
const { document, selection } = state
|
||||||
|
|
||||||
if (selection.isExpanded) {
|
if (selection.isExpanded) {
|
||||||
return transform.addMarkAtRange(selection, mark)
|
transform.addMarkAtRange(selection, mark)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (selection.marks) {
|
if (selection.marks) {
|
||||||
const marks = selection.marks.add(mark)
|
const marks = selection.marks.add(mark)
|
||||||
const sel = selection.merge({ marks })
|
const sel = selection.merge({ marks })
|
||||||
return transform.moveTo(sel)
|
transform.moveTo(sel)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
|
||||||
const marks = document.getMarksAtRange(selection).add(mark)
|
const marks = document.getMarksAtRange(selection).add(mark)
|
||||||
const sel = selection.merge({ marks })
|
const sel = selection.merge({ marks })
|
||||||
return transform.moveTo(sel)
|
transform.moveTo(sel)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete at the current selection.
|
* Delete at the current selection.
|
||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function _delete(transform) {
|
export function _delete(transform) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { document, selection } = state
|
const { selection } = state
|
||||||
|
if (selection.isCollapsed) return
|
||||||
|
|
||||||
// If the selection is collapsed, there's nothing to delete.
|
transform
|
||||||
if (selection.isCollapsed) return transform
|
.snapshotSelection()
|
||||||
|
|
||||||
const { startText } = state
|
|
||||||
const { startKey, startOffset, endKey, endOffset } = selection
|
|
||||||
const block = document.getClosestBlock(startKey)
|
|
||||||
const highest = block.getHighestChild(startKey)
|
|
||||||
const previous = block.getPreviousSibling(highest.key)
|
|
||||||
const next = block.getNextSibling(highest.key)
|
|
||||||
let after
|
|
||||||
|
|
||||||
// If there's a previous node, and we're at the start of the current node,
|
|
||||||
// and the selection encompasses the entire current node, it won't exist after
|
|
||||||
// deleting, so we need to update the selection's keys.
|
|
||||||
if (
|
|
||||||
previous &&
|
|
||||||
startOffset == 0 &&
|
|
||||||
(endKey != startKey || endOffset == startText.length)
|
|
||||||
) {
|
|
||||||
|
|
||||||
// If the nodes on either sides are text nodes, they will end up being
|
|
||||||
// combined, so we need to set the selection to right in between them.
|
|
||||||
if (previous.kind == 'text' && next && next.kind == 'text') {
|
|
||||||
after = selection.merge({
|
|
||||||
anchorKey: previous.key,
|
|
||||||
anchorOffset: previous.length,
|
|
||||||
focusKey: previous.key,
|
|
||||||
focusOffset: previous.length
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, if only the previous node is a text node, it won't be merged,
|
|
||||||
// so collapse to the end of it.
|
|
||||||
else if (previous.kind == 'text') {
|
|
||||||
after = selection.collapseToEndOf(previous)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, if the previous node isn't a text node, we need to get the
|
|
||||||
// last text node inside of it and collapse to the end of that.
|
|
||||||
else {
|
|
||||||
const last = previous.getLastText()
|
|
||||||
after = selection.collapseToEndOf(last)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, if the inline is an online child
|
|
||||||
|
|
||||||
// Otherwise simply collapse the selection.
|
|
||||||
else {
|
|
||||||
after = selection.collapseToStart()
|
|
||||||
}
|
|
||||||
|
|
||||||
return transform
|
|
||||||
.unsetSelection()
|
|
||||||
.deleteAtRange(selection)
|
.deleteAtRange(selection)
|
||||||
.moveTo(after)
|
// Ensure that the selection is collapsed to the start, because in certain
|
||||||
|
// cases when deleting across inline nodes this isn't guaranteed.
|
||||||
|
.collapseToStart()
|
||||||
|
.snapshotSelection()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -106,16 +55,12 @@ export function _delete(transform) {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Number} n (optional)
|
* @param {Number} n (optional)
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function deleteBackward(transform, n = 1) {
|
export function deleteBackward(transform, n = 1) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { selection } = state
|
const { selection } = state
|
||||||
|
transform.deleteBackwardAtRange(selection, n)
|
||||||
return transform
|
|
||||||
.deleteBackwardAtRange(selection, n)
|
|
||||||
.collapseToEnd()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -123,16 +68,12 @@ export function deleteBackward(transform, n = 1) {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Number} n (optional)
|
* @param {Number} n (optional)
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function deleteForward(transform, n = 1) {
|
export function deleteForward(transform, n = 1) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { selection } = state
|
const { selection } = state
|
||||||
|
transform.deleteForwardAtRange(selection, n)
|
||||||
return transform
|
|
||||||
.deleteForwardAtRange(selection, n)
|
|
||||||
.collapseToEnd()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -140,23 +81,17 @@ export function deleteForward(transform, n = 1) {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {String|Object|Block} block
|
* @param {String|Object|Block} block
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function insertBlock(transform, block) {
|
export function insertBlock(transform, block) {
|
||||||
let { state } = transform
|
block = Normalize.block(block)
|
||||||
let { document, selection } = state
|
const { state } = transform
|
||||||
const keys = document.getTexts().map(text => text.key)
|
const { selection } = state
|
||||||
|
|
||||||
transform.unsetSelection()
|
|
||||||
transform.insertBlockAtRange(selection, block)
|
transform.insertBlockAtRange(selection, block)
|
||||||
state = transform.state
|
|
||||||
document = state.document
|
|
||||||
|
|
||||||
const text = document.getTexts().find(n => !keys.includes(n.key))
|
// If the node was successfully inserted, update the selection.
|
||||||
const after = selection.collapseToEndOf(text)
|
const node = transform.state.document.getNode(block.key)
|
||||||
|
if (node) transform.collapseToEndOf(node)
|
||||||
return transform.moveTo(after)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -164,14 +99,13 @@ export function insertBlock(transform, block) {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Document} fragment
|
* @param {Document} fragment
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function insertFragment(transform, fragment) {
|
export function insertFragment(transform, fragment) {
|
||||||
let { state } = transform
|
let { state } = transform
|
||||||
let { document, selection } = state
|
let { document, selection } = state
|
||||||
|
|
||||||
if (!fragment.length) return transform
|
if (!fragment.length) return
|
||||||
|
|
||||||
const { startText, endText } = state
|
const { startText, endText } = state
|
||||||
const lastText = fragment.getLastText()
|
const lastText = fragment.getLastText()
|
||||||
@@ -207,7 +141,7 @@ export function insertFragment(transform, fragment) {
|
|||||||
.moveForward(lastText.length)
|
.moveForward(lastText.length)
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform.moveTo(after)
|
transform.moveTo(after)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -215,38 +149,17 @@ export function insertFragment(transform, fragment) {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {String|Object|Block} inline
|
* @param {String|Object|Block} inline
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function insertInline(transform, inline) {
|
export function insertInline(transform, inline) {
|
||||||
let { state } = transform
|
inline = Normalize.inline(inline)
|
||||||
let { document, selection, startText } = state
|
const { state } = transform
|
||||||
let after
|
const { selection } = state
|
||||||
|
|
||||||
const hasVoid = document.hasVoidParent(startText.key)
|
|
||||||
const keys = document.getTexts().map(text => text.key)
|
|
||||||
|
|
||||||
transform.unsetSelection()
|
|
||||||
transform.insertInlineAtRange(selection, inline)
|
transform.insertInlineAtRange(selection, inline)
|
||||||
state = transform.state
|
|
||||||
document = state.document
|
|
||||||
|
|
||||||
if (hasVoid) {
|
// If the node was successfully inserted, update the selection.
|
||||||
after = selection
|
const node = transform.state.document.getNode(inline.key)
|
||||||
}
|
if (node) transform.collapseToEndOf(node)
|
||||||
|
|
||||||
else {
|
|
||||||
const text = document.getTexts().find((n) => {
|
|
||||||
if (keys.includes(n.key)) return false
|
|
||||||
const parent = document.getParent(n.key)
|
|
||||||
if (parent.kind != 'inline') return false
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
|
|
||||||
after = selection.collapseToEndOf(text)
|
|
||||||
}
|
|
||||||
|
|
||||||
return transform.moveTo(after)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -255,34 +168,19 @@ export function insertInline(transform, inline) {
|
|||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {String} text
|
* @param {String} text
|
||||||
* @param {Set<Mark>} marks (optional)
|
* @param {Set<Mark>} marks (optional)
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function insertText(transform, text, marks) {
|
export function insertText(transform, text, marks) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { document, selection } = state
|
const { document, selection } = state
|
||||||
const { startKey } = selection
|
|
||||||
const isVoid = document.hasVoidParent(startKey)
|
|
||||||
let after
|
|
||||||
|
|
||||||
if (isVoid) {
|
|
||||||
after = selection
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (selection.isExpanded) {
|
|
||||||
after = selection.collapseToStart().moveForward(text.length)
|
|
||||||
}
|
|
||||||
|
|
||||||
else {
|
|
||||||
after = selection.moveForward(text.length)
|
|
||||||
}
|
|
||||||
|
|
||||||
marks = marks || selection.marks
|
marks = marks || selection.marks
|
||||||
|
transform.insertTextAtRange(selection, text, marks)
|
||||||
|
|
||||||
return transform
|
// If the text was successfully inserted, and the selection had marks on it,
|
||||||
.unsetSelection()
|
// unset the selection's marks.
|
||||||
.insertTextAtRange(selection, text, marks)
|
if (selection.marks && document != transform.state.document) {
|
||||||
.moveTo(after)
|
transform.unsetMarks()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -290,13 +188,12 @@ export function insertText(transform, text, marks) {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Object} properties
|
* @param {Object} properties
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function setBlock(transform, properties) {
|
export function setBlock(transform, properties) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { selection } = state
|
const { selection } = state
|
||||||
return transform.setBlockAtRange(selection, properties)
|
transform.setBlockAtRange(selection, properties)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -304,13 +201,12 @@ export function setBlock(transform, properties) {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Object} properties
|
* @param {Object} properties
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function setInline(transform, properties) {
|
export function setInline(transform, properties) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { selection } = state
|
const { selection } = state
|
||||||
return transform.setInlineAtRange(selection, properties)
|
transform.setInlineAtRange(selection, properties)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -318,43 +214,16 @@ export function setInline(transform, properties) {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Number} depth (optional)
|
* @param {Number} depth (optional)
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function splitBlock(transform, depth = 1) {
|
export function splitBlock(transform, depth = 1) {
|
||||||
let { state } = transform
|
let { state } = transform
|
||||||
let { document, selection } = state
|
let { selection } = state
|
||||||
|
transform
|
||||||
transform.unsetSelection()
|
.snapshotSelection()
|
||||||
transform.splitBlockAtRange(selection, depth)
|
.splitBlockAtRange(selection, depth)
|
||||||
|
.collapseToEnd()
|
||||||
state = transform.state
|
.snapshotSelection()
|
||||||
document = state.document
|
|
||||||
|
|
||||||
const { startKey, startOffset } = selection
|
|
||||||
const startText = document.getNode(startKey)
|
|
||||||
const startBlock = document.getClosestBlock(startKey)
|
|
||||||
const startInline = startBlock.getFurthestInline(startKey)
|
|
||||||
const nextText = document.getNextText(startText.key)
|
|
||||||
let after
|
|
||||||
|
|
||||||
// If the selection is at the start of the highest inline child inside the
|
|
||||||
// block, the starting text node won't need to be split.
|
|
||||||
if (
|
|
||||||
(startOffset == 0) &&
|
|
||||||
(startBlock.text != '') &&
|
|
||||||
(!startInline || startInline.getOffset(startText.key) == 0)
|
|
||||||
) {
|
|
||||||
after = selection.collapseToStartOf(startText)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, we'll need to move the selection forward one to account for the
|
|
||||||
// text node that was split.
|
|
||||||
else {
|
|
||||||
after = selection.collapseToStartOf(nextText)
|
|
||||||
}
|
|
||||||
|
|
||||||
return transform.moveTo(after)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -362,49 +231,15 @@ export function splitBlock(transform, depth = 1) {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Number} depth (optional)
|
* @param {Number} depth (optional)
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function splitInline(transform, depth = Infinity) {
|
export function splitInline(transform, depth = Infinity) {
|
||||||
let { state } = transform
|
let { state } = transform
|
||||||
let { document, selection } = state
|
let { selection } = state
|
||||||
|
transform
|
||||||
// If the selection is expanded, remove it first.
|
.snapshotSelection()
|
||||||
if (selection.isExpanded) {
|
.splitInlineAtRange(selection, depth)
|
||||||
transform.delete()
|
.snapshotSelection()
|
||||||
state = transform.state
|
|
||||||
document = state.document
|
|
||||||
selection = state.selection
|
|
||||||
}
|
|
||||||
|
|
||||||
let after = selection
|
|
||||||
const { startKey, startOffset } = selection
|
|
||||||
let startNode = document.assertDescendant(startKey)
|
|
||||||
const furthestInline = document.getFurthestInline(startKey)
|
|
||||||
const offset = furthestInline.getOffset(startNode.key)
|
|
||||||
|
|
||||||
// If the selection is at the start of end of the furthest inline, there isn't
|
|
||||||
// anything to split, so abort.
|
|
||||||
if (
|
|
||||||
(offset + startOffset == 0) ||
|
|
||||||
(offset + startNode.length == startOffset)
|
|
||||||
) {
|
|
||||||
return transform
|
|
||||||
}
|
|
||||||
|
|
||||||
transform.unsetSelection()
|
|
||||||
transform.splitInlineAtRange(selection, depth)
|
|
||||||
state = transform.state
|
|
||||||
document = state.document
|
|
||||||
const closestInline = document.getClosestInline(startKey)
|
|
||||||
|
|
||||||
if (closestInline) {
|
|
||||||
startNode = document.getDescendant(startKey)
|
|
||||||
const nextNode = document.getNextText(startNode.key)
|
|
||||||
after = selection.collapseToStartOf(nextNode)
|
|
||||||
}
|
|
||||||
|
|
||||||
return transform.moveTo(after)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -412,30 +247,28 @@ export function splitInline(transform, depth = Infinity) {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Mark} mark
|
* @param {Mark} mark
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function removeMark(transform, mark) {
|
export function removeMark(transform, mark) {
|
||||||
mark = Normalize.mark(mark)
|
mark = Normalize.mark(mark)
|
||||||
|
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { document, selection } = state
|
const { document, selection } = state
|
||||||
|
|
||||||
if (selection.isExpanded) {
|
if (selection.isExpanded) {
|
||||||
return transform.removeMarkAtRange(selection, mark)
|
transform.removeMarkAtRange(selection, mark)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (selection.marks) {
|
if (selection.marks) {
|
||||||
const marks = selection.marks.remove(mark)
|
const marks = selection.marks.remove(mark)
|
||||||
const sel = selection.merge({ marks })
|
const sel = selection.merge({ marks })
|
||||||
return transform.moveTo(sel)
|
transform.moveTo(sel)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
|
||||||
const marks = document.getMarksAtRange(selection).remove(mark)
|
const marks = document.getMarksAtRange(selection).remove(mark)
|
||||||
const sel = selection.merge({ marks })
|
const sel = selection.merge({ marks })
|
||||||
return transform.moveTo(sel)
|
transform.moveTo(sel)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -444,19 +277,17 @@ export function removeMark(transform, mark) {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Mark} mark
|
* @param {Mark} mark
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function toggleMark(transform, mark) {
|
export function toggleMark(transform, mark) {
|
||||||
mark = Normalize.mark(mark)
|
mark = Normalize.mark(mark)
|
||||||
|
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const exists = state.marks.some(m => m.equals(mark))
|
const exists = state.marks.some(m => m.equals(mark))
|
||||||
|
|
||||||
if (exists) {
|
if (exists) {
|
||||||
return transform.removeMark(mark)
|
transform.removeMark(mark)
|
||||||
} else {
|
} else {
|
||||||
return transform.addMark(mark)
|
transform.addMark(mark)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -465,13 +296,12 @@ export function toggleMark(transform, mark) {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Object|String} properties
|
* @param {Object|String} properties
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function unwrapBlock(transform, properties) {
|
export function unwrapBlock(transform, properties) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { selection } = state
|
const { selection } = state
|
||||||
return transform.unwrapBlockAtRange(selection, properties)
|
transform.unwrapBlockAtRange(selection, properties)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -479,13 +309,12 @@ export function unwrapBlock(transform, properties) {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Object|String} properties
|
* @param {Object|String} properties
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function unwrapInline(transform, properties) {
|
export function unwrapInline(transform, properties) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { selection } = state
|
const { selection } = state
|
||||||
return transform.unwrapInlineAtRange(selection, properties)
|
transform.unwrapInlineAtRange(selection, properties)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -494,13 +323,12 @@ export function unwrapInline(transform, properties) {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Object|String} properties
|
* @param {Object|String} properties
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function wrapBlock(transform, properties) {
|
export function wrapBlock(transform, properties) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { selection } = state
|
const { selection } = state
|
||||||
return transform.wrapBlockAtRange(selection, properties)
|
transform.wrapBlockAtRange(selection, properties)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -508,7 +336,6 @@ export function wrapBlock(transform, properties) {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Object|String} properties
|
* @param {Object|String} properties
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function wrapInline(transform, properties) {
|
export function wrapInline(transform, properties) {
|
||||||
@@ -551,7 +378,7 @@ export function wrapInline(transform, properties) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
after = after.normalize(document)
|
after = after.normalize(document)
|
||||||
return transform.moveTo(after)
|
transform.moveTo(after)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -560,28 +387,19 @@ export function wrapInline(transform, properties) {
|
|||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {String} prefix
|
* @param {String} prefix
|
||||||
* @param {String} suffix
|
* @param {String} suffix
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function wrapText(transform, prefix, suffix = prefix) {
|
export function wrapText(transform, prefix, suffix = prefix) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { selection } = state
|
const { selection } = state
|
||||||
const { anchorOffset, anchorKey, focusOffset, focusKey, isBackward } = selection
|
transform.wrapTextAtRange(selection, prefix, suffix)
|
||||||
let after
|
|
||||||
|
|
||||||
if (anchorKey == focusKey) {
|
// Adding the suffix will have pushed the end of the selection further on, so
|
||||||
after = selection.moveForward(prefix.length)
|
// we need to move it back to account for this.
|
||||||
|
transform.moveEndOffset(0 - suffix.length)
|
||||||
|
|
||||||
|
// If the selection was collapsed, it will have moved the start offset too.
|
||||||
|
if (selection.isCollapsed) {
|
||||||
|
transform.moveStartOffset(0 - prefix.length)
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
|
||||||
after = selection.merge({
|
|
||||||
anchorOffset: isBackward ? anchorOffset : anchorOffset + prefix.length,
|
|
||||||
focusOffset: isBackward ? focusOffset + prefix.length : focusOffset
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return transform
|
|
||||||
.unsetSelection()
|
|
||||||
.wrapTextAtRange(selection, prefix, suffix)
|
|
||||||
.moveTo(after)
|
|
||||||
}
|
}
|
||||||
|
@@ -4,6 +4,16 @@ import Normalize from '../utils/normalize'
|
|||||||
import SCHEMA from '../schemas/core'
|
import SCHEMA from '../schemas/core'
|
||||||
import { List } from 'immutable'
|
import { List } from 'immutable'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An options object with normalize set to `false`.
|
||||||
|
*
|
||||||
|
* @type {Object}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const OPTS = {
|
||||||
|
normalize: false
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a new `mark` to the characters at `range`.
|
* Add a new `mark` to the characters at `range`.
|
||||||
*
|
*
|
||||||
@@ -12,11 +22,10 @@ import { List } from 'immutable'
|
|||||||
* @param {Mixed} mark
|
* @param {Mixed} mark
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @property {Boolean} normalize
|
* @property {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function addMarkAtRange(transform, range, mark, options = {}) {
|
export function addMarkAtRange(transform, range, mark, options = {}) {
|
||||||
if (range.isCollapsed) return transform
|
if (range.isCollapsed) return
|
||||||
|
|
||||||
const { normalize = true } = options
|
const { normalize = true } = options
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
@@ -35,8 +44,6 @@ export function addMarkAtRange(transform, range, mark, options = {}) {
|
|||||||
|
|
||||||
transform.addMarkByKey(key, index, length, mark, { normalize })
|
transform.addMarkByKey(key, index, length, mark, { normalize })
|
||||||
})
|
})
|
||||||
|
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -46,11 +53,10 @@ export function addMarkAtRange(transform, range, mark, options = {}) {
|
|||||||
* @param {Selection} range
|
* @param {Selection} range
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @property {Boolean} normalize
|
* @property {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function deleteAtRange(transform, range, options = {}) {
|
export function deleteAtRange(transform, range, options = {}) {
|
||||||
if (range.isCollapsed) return transform
|
if (range.isCollapsed) return
|
||||||
|
|
||||||
const { normalize = true } = options
|
const { normalize = true } = options
|
||||||
const { startKey, startOffset, endKey, endOffset } = range
|
const { startKey, startOffset, endKey, endOffset } = range
|
||||||
@@ -58,7 +64,8 @@ export function deleteAtRange(transform, range, options = {}) {
|
|||||||
if (startKey == endKey) {
|
if (startKey == endKey) {
|
||||||
const index = startOffset
|
const index = startOffset
|
||||||
const length = endOffset - startOffset
|
const length = endOffset - startOffset
|
||||||
return transform.removeTextByKey(startKey, index, length, { normalize })
|
transform.removeTextByKey(startKey, index, length, { normalize })
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let { state } = transform
|
let { state } = transform
|
||||||
@@ -71,8 +78,8 @@ export function deleteAtRange(transform, range, options = {}) {
|
|||||||
const startOff = (startChild.kind == 'text' ? 0 : startChild.getOffset(startKey)) + startOffset
|
const startOff = (startChild.kind == 'text' ? 0 : startChild.getOffset(startKey)) + startOffset
|
||||||
const endOff = (endChild.kind == 'text' ? 0 : endChild.getOffset(endKey)) + endOffset
|
const endOff = (endChild.kind == 'text' ? 0 : endChild.getOffset(endKey)) + endOffset
|
||||||
|
|
||||||
transform.splitNodeByKey(startChild.key, startOff, { normalize: false })
|
transform.splitNodeByKey(startChild.key, startOff, OPTS)
|
||||||
transform.splitNodeByKey(endChild.key, endOff, { normalize: false })
|
transform.splitNodeByKey(endChild.key, endOff, OPTS)
|
||||||
|
|
||||||
state = transform.state
|
state = transform.state
|
||||||
document = state.document
|
document = state.document
|
||||||
@@ -90,7 +97,7 @@ export function deleteAtRange(transform, range, options = {}) {
|
|||||||
if (middles.size) {
|
if (middles.size) {
|
||||||
// remove first nodes directly so the document is not normalized
|
// remove first nodes directly so the document is not normalized
|
||||||
middles.forEach(child => {
|
middles.forEach(child => {
|
||||||
transform.removeNodeByKey(child.key, { normalize: false })
|
transform.removeNodeByKey(child.key, OPTS)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,18 +105,16 @@ export function deleteAtRange(transform, range, options = {}) {
|
|||||||
endBlock.nodes.forEach((child, i) => {
|
endBlock.nodes.forEach((child, i) => {
|
||||||
const newKey = startBlock.key
|
const newKey = startBlock.key
|
||||||
const newIndex = startBlock.nodes.size + i
|
const newIndex = startBlock.nodes.size + i
|
||||||
transform.moveNodeByKey(child.key, newKey, newIndex, { normalize: false })
|
transform.moveNodeByKey(child.key, newKey, newIndex, OPTS)
|
||||||
})
|
})
|
||||||
|
|
||||||
const lonely = document.getFurthest(endBlock.key, p => p.nodes.size == 1) || endBlock
|
const lonely = document.getFurthest(endBlock.key, p => p.nodes.size == 1) || endBlock
|
||||||
transform.removeNodeByKey(lonely.key, { normalize: false })
|
transform.removeNodeByKey(lonely.key, OPTS)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (normalize) {
|
if (normalize) {
|
||||||
transform.normalizeNodeByKey(ancestor.key, SCHEMA)
|
transform.normalizeNodeByKey(ancestor.key, SCHEMA)
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -120,7 +125,6 @@ export function deleteAtRange(transform, range, options = {}) {
|
|||||||
* @param {Number} n (optional)
|
* @param {Number} n (optional)
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @property {Boolean} normalize
|
* @property {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function deleteBackwardAtRange(transform, range, n = 1, options = {}) {
|
export function deleteBackwardAtRange(transform, range, n = 1, options = {}) {
|
||||||
@@ -130,21 +134,24 @@ export function deleteBackwardAtRange(transform, range, n = 1, options = {}) {
|
|||||||
const { startKey, focusOffset } = range
|
const { startKey, focusOffset } = range
|
||||||
|
|
||||||
if (range.isExpanded) {
|
if (range.isExpanded) {
|
||||||
return transform.deleteAtRange(range, { normalize })
|
transform.deleteAtRange(range, { normalize })
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const block = document.getClosestBlock(startKey)
|
const block = document.getClosestBlock(startKey)
|
||||||
if (block && block.isVoid) {
|
if (block && block.isVoid) {
|
||||||
return transform.removeNodeByKey(block.key, { normalize })
|
transform.removeNodeByKey(block.key, { normalize })
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const inline = document.getClosestInline(startKey)
|
const inline = document.getClosestInline(startKey)
|
||||||
if (inline && inline.isVoid) {
|
if (inline && inline.isVoid) {
|
||||||
return transform.removeNodeByKey(inline.key, { normalize })
|
transform.removeNodeByKey(inline.key, { normalize })
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (range.isAtStartOf(document)) {
|
if (range.isAtStartOf(document)) {
|
||||||
return transform
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const text = document.getDescendant(startKey)
|
const text = document.getDescendant(startKey)
|
||||||
@@ -154,11 +161,13 @@ export function deleteBackwardAtRange(transform, range, n = 1, options = {}) {
|
|||||||
const prevInline = document.getClosestInline(prev.key)
|
const prevInline = document.getClosestInline(prev.key)
|
||||||
|
|
||||||
if (prevBlock && prevBlock.isVoid) {
|
if (prevBlock && prevBlock.isVoid) {
|
||||||
return transform.removeNodeByKey(prevBlock.key, { normalize })
|
transform.removeNodeByKey(prevBlock.key, { normalize })
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prevInline && prevInline.isVoid) {
|
if (prevInline && prevInline.isVoid) {
|
||||||
return transform.removeNodeByKey(prevInline.key, { normalize })
|
transform.removeNodeByKey(prevInline.key, { normalize })
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
range = range.merge({
|
range = range.merge({
|
||||||
@@ -166,7 +175,8 @@ export function deleteBackwardAtRange(transform, range, n = 1, options = {}) {
|
|||||||
anchorOffset: prev.length,
|
anchorOffset: prev.length,
|
||||||
})
|
})
|
||||||
|
|
||||||
return transform.deleteAtRange(range, { normalize })
|
transform.deleteAtRange(range, { normalize })
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
range = range.merge({
|
range = range.merge({
|
||||||
@@ -174,7 +184,7 @@ export function deleteBackwardAtRange(transform, range, n = 1, options = {}) {
|
|||||||
isBackward: true,
|
isBackward: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
return transform.deleteAtRange(range, { normalize })
|
transform.deleteAtRange(range, { normalize })
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -185,7 +195,6 @@ export function deleteBackwardAtRange(transform, range, n = 1, options = {}) {
|
|||||||
* @param {Number} n (optional)
|
* @param {Number} n (optional)
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @property {Boolean} normalize
|
* @property {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function deleteForwardAtRange(transform, range, n = 1, options = {}) {
|
export function deleteForwardAtRange(transform, range, n = 1, options = {}) {
|
||||||
@@ -195,21 +204,24 @@ export function deleteForwardAtRange(transform, range, n = 1, options = {}) {
|
|||||||
const { startKey, focusOffset } = range
|
const { startKey, focusOffset } = range
|
||||||
|
|
||||||
if (range.isExpanded) {
|
if (range.isExpanded) {
|
||||||
return transform.deleteAtRange(range, { normalize })
|
transform.deleteAtRange(range, { normalize })
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const block = document.getClosestBlock(startKey)
|
const block = document.getClosestBlock(startKey)
|
||||||
if (block && block.isVoid) {
|
if (block && block.isVoid) {
|
||||||
return transform.removeNodeByKey(block.key, { normalize })
|
transform.removeNodeByKey(block.key, { normalize })
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const inline = document.getClosestInline(startKey)
|
const inline = document.getClosestInline(startKey)
|
||||||
if (inline && inline.isVoid) {
|
if (inline && inline.isVoid) {
|
||||||
return transform.removeNodeByKey(inline.key, { normalize })
|
transform.removeNodeByKey(inline.key, { normalize })
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (range.isAtEndOf(document)) {
|
if (range.isAtEndOf(document)) {
|
||||||
return transform
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const text = document.getDescendant(startKey)
|
const text = document.getDescendant(startKey)
|
||||||
@@ -219,11 +231,13 @@ export function deleteForwardAtRange(transform, range, n = 1, options = {}) {
|
|||||||
const nextInline = document.getClosestInline(next.key)
|
const nextInline = document.getClosestInline(next.key)
|
||||||
|
|
||||||
if (nextBlock && nextBlock.isVoid) {
|
if (nextBlock && nextBlock.isVoid) {
|
||||||
return transform.removeNodeByKey(nextBlock.key, { normalize })
|
transform.removeNodeByKey(nextBlock.key, { normalize })
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nextInline && nextInline.isVoid) {
|
if (nextInline && nextInline.isVoid) {
|
||||||
return transform.removeNodeByKey(nextInline.key, { normalize })
|
transform.removeNodeByKey(nextInline.key, { normalize })
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
range = range.merge({
|
range = range.merge({
|
||||||
@@ -231,14 +245,15 @@ export function deleteForwardAtRange(transform, range, n = 1, options = {}) {
|
|||||||
focusOffset: 0
|
focusOffset: 0
|
||||||
})
|
})
|
||||||
|
|
||||||
return transform.deleteAtRange(range, { normalize })
|
transform.deleteAtRange(range, { normalize })
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
range = range.merge({
|
range = range.merge({
|
||||||
focusOffset: focusOffset + n
|
focusOffset: focusOffset + n
|
||||||
})
|
})
|
||||||
|
|
||||||
return transform.deleteAtRange(range, { normalize })
|
transform.deleteAtRange(range, { normalize })
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -249,7 +264,6 @@ export function deleteForwardAtRange(transform, range, n = 1, options = {}) {
|
|||||||
* @param {Block|String|Object} block
|
* @param {Block|String|Object} block
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @property {Boolean} normalize
|
* @property {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function insertBlockAtRange(transform, range, block, options = {}) {
|
export function insertBlockAtRange(transform, range, block, options = {}) {
|
||||||
@@ -295,8 +309,6 @@ export function insertBlockAtRange(transform, range, block, options = {}) {
|
|||||||
if (normalize) {
|
if (normalize) {
|
||||||
transform.normalizeNodeByKey(parent.key, SCHEMA)
|
transform.normalizeNodeByKey(parent.key, SCHEMA)
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -307,7 +319,6 @@ export function insertBlockAtRange(transform, range, block, options = {}) {
|
|||||||
* @param {Document} fragment
|
* @param {Document} fragment
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @property {Boolean} normalize
|
* @property {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function insertFragmentAtRange(transform, range, fragment, options = {}) {
|
export function insertFragmentAtRange(transform, range, fragment, options = {}) {
|
||||||
@@ -315,14 +326,12 @@ export function insertFragmentAtRange(transform, range, fragment, options = {})
|
|||||||
|
|
||||||
// If the range is expanded, delete it first.
|
// If the range is expanded, delete it first.
|
||||||
if (range.isExpanded) {
|
if (range.isExpanded) {
|
||||||
transform.deleteAtRange(range, { normalize: false })
|
transform.deleteAtRange(range, OPTS)
|
||||||
range = range.collapseToStart()
|
range = range.collapseToStart()
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the fragment is empty, there's nothing to do after deleting.
|
// If the fragment is empty, there's nothing to do after deleting.
|
||||||
if (!fragment.length) {
|
if (!fragment.length) return
|
||||||
return transform
|
|
||||||
}
|
|
||||||
|
|
||||||
// Regenerate the keys for all of the fragments nodes, so that they're
|
// Regenerate the keys for all of the fragments nodes, so that they're
|
||||||
// guaranteed not to collide with the existing keys in the document. Otherwise
|
// guaranteed not to collide with the existing keys in the document. Otherwise
|
||||||
@@ -358,13 +367,13 @@ export function insertFragmentAtRange(transform, range, fragment, options = {})
|
|||||||
|
|
||||||
fragment.nodes.forEach((node, i) => {
|
fragment.nodes.forEach((node, i) => {
|
||||||
const newIndex = startIndex + i + 1
|
const newIndex = startIndex + i + 1
|
||||||
transform.insertNodeByKey(parent.key, newIndex, node, { normalize: false })
|
transform.insertNodeByKey(parent.key, newIndex, node, OPTS)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if we need to split the node.
|
// Check if we need to split the node.
|
||||||
if (startOffset != 0) {
|
if (startOffset != 0) {
|
||||||
transform.splitNodeByKey(startChild.key, offset, { normalize: false })
|
transform.splitNodeByKey(startChild.key, offset, OPTS)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update our variables with the new state.
|
// Update our variables with the new state.
|
||||||
@@ -384,15 +393,15 @@ export function insertFragmentAtRange(transform, range, fragment, options = {})
|
|||||||
|
|
||||||
nextNodes.forEach((node, i) => {
|
nextNodes.forEach((node, i) => {
|
||||||
const newIndex = lastIndex + i
|
const newIndex = lastIndex + i
|
||||||
transform.moveNodeByKey(node.key, lastBlock.key, newIndex, { normalize: false })
|
transform.moveNodeByKey(node.key, lastBlock.key, newIndex, OPTS)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the starting block is empty, we replace it entirely with the first block
|
// If the starting block is empty, we replace it entirely with the first block
|
||||||
// of the fragment, since this leads to a more expected behavior for the user.
|
// of the fragment, since this leads to a more expected behavior for the user.
|
||||||
if (startBlock.isEmpty) {
|
if (startBlock.isEmpty) {
|
||||||
transform.removeNodeByKey(startBlock.key, { normalize: false })
|
transform.removeNodeByKey(startBlock.key, OPTS)
|
||||||
transform.insertNodeByKey(parent.key, index, firstBlock, { normalize: false })
|
transform.insertNodeByKey(parent.key, index, firstBlock, OPTS)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, we maintain the starting block, and insert all of the first
|
// Otherwise, we maintain the starting block, and insert all of the first
|
||||||
@@ -404,7 +413,7 @@ export function insertFragmentAtRange(transform, range, fragment, options = {})
|
|||||||
firstBlock.nodes.forEach((inline, i) => {
|
firstBlock.nodes.forEach((inline, i) => {
|
||||||
const o = startOffset == 0 ? 0 : 1
|
const o = startOffset == 0 ? 0 : 1
|
||||||
const newIndex = inlineIndex + i + o
|
const newIndex = inlineIndex + i + o
|
||||||
transform.insertNodeByKey(startBlock.key, newIndex, inline, { normalize: false })
|
transform.insertNodeByKey(startBlock.key, newIndex, inline, OPTS)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -412,8 +421,6 @@ export function insertFragmentAtRange(transform, range, fragment, options = {})
|
|||||||
if (normalize) {
|
if (normalize) {
|
||||||
transform.normalizeNodeByKey(parent.key, SCHEMA)
|
transform.normalizeNodeByKey(parent.key, SCHEMA)
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -424,7 +431,6 @@ export function insertFragmentAtRange(transform, range, fragment, options = {})
|
|||||||
* @param {Inline|String|Object} inline
|
* @param {Inline|String|Object} inline
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @property {Boolean} normalize
|
* @property {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function insertInlineAtRange(transform, range, inline, options = {}) {
|
export function insertInlineAtRange(transform, range, inline, options = {}) {
|
||||||
@@ -432,7 +438,7 @@ export function insertInlineAtRange(transform, range, inline, options = {}) {
|
|||||||
inline = Normalize.inline(inline)
|
inline = Normalize.inline(inline)
|
||||||
|
|
||||||
if (range.isExpanded) {
|
if (range.isExpanded) {
|
||||||
transform.deleteAtRange(range, { normalize: false })
|
transform.deleteAtRange(range, OPTS)
|
||||||
range = range.collapseToStart()
|
range = range.collapseToStart()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -443,18 +449,14 @@ export function insertInlineAtRange(transform, range, inline, options = {}) {
|
|||||||
const startText = document.assertDescendant(startKey)
|
const startText = document.assertDescendant(startKey)
|
||||||
const index = parent.nodes.indexOf(startText)
|
const index = parent.nodes.indexOf(startText)
|
||||||
|
|
||||||
if (parent.isVoid) {
|
if (parent.isVoid) return
|
||||||
return transform
|
|
||||||
}
|
|
||||||
|
|
||||||
transform.splitNodeByKey(startKey, startOffset, { normalize: false })
|
transform.splitNodeByKey(startKey, startOffset, OPTS)
|
||||||
transform.insertNodeByKey(parent.key, index + 1, inline, { normalize: false })
|
transform.insertNodeByKey(parent.key, index + 1, inline, OPTS)
|
||||||
|
|
||||||
if (normalize) {
|
if (normalize) {
|
||||||
transform.normalizeNodeByKey(parent.key, SCHEMA)
|
transform.normalizeNodeByKey(parent.key, SCHEMA)
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -466,7 +468,6 @@ export function insertInlineAtRange(transform, range, inline, options = {}) {
|
|||||||
* @param {Set<Mark>} marks (optional)
|
* @param {Set<Mark>} marks (optional)
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @property {Boolean} normalize
|
* @property {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function insertTextAtRange(transform, range, text, marks, options = {}) {
|
export function insertTextAtRange(transform, range, text, marks, options = {}) {
|
||||||
@@ -476,12 +477,10 @@ export function insertTextAtRange(transform, range, text, marks, options = {}) {
|
|||||||
const { startKey, startOffset } = range
|
const { startKey, startOffset } = range
|
||||||
const parent = document.getParent(startKey)
|
const parent = document.getParent(startKey)
|
||||||
|
|
||||||
if (parent.isVoid) {
|
if (parent.isVoid) return
|
||||||
return transform
|
|
||||||
}
|
|
||||||
|
|
||||||
if (range.isExpanded) {
|
if (range.isExpanded) {
|
||||||
transform.deleteAtRange(range, { normalize: false })
|
transform.deleteAtRange(range, OPTS)
|
||||||
}
|
}
|
||||||
|
|
||||||
// PERF: Unless specified, don't normalize if only inserting text.
|
// PERF: Unless specified, don't normalize if only inserting text.
|
||||||
@@ -489,7 +488,7 @@ export function insertTextAtRange(transform, range, text, marks, options = {}) {
|
|||||||
normalize = range.isExpanded
|
normalize = range.isExpanded
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform.insertTextByKey(startKey, startOffset, text, marks, { normalize })
|
transform.insertTextByKey(startKey, startOffset, text, marks, { normalize })
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -500,11 +499,10 @@ export function insertTextAtRange(transform, range, text, marks, options = {}) {
|
|||||||
* @param {Mark|String} mark (optional)
|
* @param {Mark|String} mark (optional)
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @property {Boolean} normalize
|
* @property {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function removeMarkAtRange(transform, range, mark, options = {}) {
|
export function removeMarkAtRange(transform, range, mark, options = {}) {
|
||||||
if (range.isCollapsed) return transform
|
if (range.isCollapsed) return
|
||||||
|
|
||||||
const { normalize = true } = options
|
const { normalize = true } = options
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
@@ -523,8 +521,6 @@ export function removeMarkAtRange(transform, range, mark, options = {}) {
|
|||||||
|
|
||||||
transform.removeMarkByKey(key, index, length, mark, { normalize })
|
transform.removeMarkByKey(key, index, length, mark, { normalize })
|
||||||
})
|
})
|
||||||
|
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -535,7 +531,6 @@ export function removeMarkAtRange(transform, range, mark, options = {}) {
|
|||||||
* @param {Object|String} properties
|
* @param {Object|String} properties
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @property {Boolean} normalize
|
* @property {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function setBlockAtRange(transform, range, properties, options = {}) {
|
export function setBlockAtRange(transform, range, properties, options = {}) {
|
||||||
@@ -547,8 +542,6 @@ export function setBlockAtRange(transform, range, properties, options = {}) {
|
|||||||
blocks.forEach((block) => {
|
blocks.forEach((block) => {
|
||||||
transform.setNodeByKey(block.key, properties, { normalize })
|
transform.setNodeByKey(block.key, properties, { normalize })
|
||||||
})
|
})
|
||||||
|
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -559,7 +552,6 @@ export function setBlockAtRange(transform, range, properties, options = {}) {
|
|||||||
* @param {Object|String} properties
|
* @param {Object|String} properties
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @property {Boolean} normalize
|
* @property {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function setInlineAtRange(transform, range, properties, options = {}) {
|
export function setInlineAtRange(transform, range, properties, options = {}) {
|
||||||
@@ -571,8 +563,6 @@ export function setInlineAtRange(transform, range, properties, options = {}) {
|
|||||||
inlines.forEach((inline) => {
|
inlines.forEach((inline) => {
|
||||||
transform.setNodeByKey(inline.key, properties, { normalize})
|
transform.setNodeByKey(inline.key, properties, { normalize})
|
||||||
})
|
})
|
||||||
|
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -583,7 +573,6 @@ export function setInlineAtRange(transform, range, properties, options = {}) {
|
|||||||
* @param {Number} height (optional)
|
* @param {Number} height (optional)
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @property {Boolean} normalize
|
* @property {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function splitBlockAtRange(transform, range, height = 1, options = {}) {
|
export function splitBlockAtRange(transform, range, height = 1, options = {}) {
|
||||||
@@ -609,7 +598,7 @@ export function splitBlockAtRange(transform, range, height = 1, options = {}) {
|
|||||||
h++
|
h++
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform.splitNodeByKey(node.key, offset, { normalize })
|
transform.splitNodeByKey(node.key, offset, { normalize })
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -620,7 +609,6 @@ export function splitBlockAtRange(transform, range, height = 1, options = {}) {
|
|||||||
* @param {Number} height (optional)
|
* @param {Number} height (optional)
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @property {Boolean} normalize
|
* @property {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function splitInlineAtRange(transform, range, height = Infinity, options = {}) {
|
export function splitInlineAtRange(transform, range, height = Infinity, options = {}) {
|
||||||
@@ -646,7 +634,7 @@ export function splitInlineAtRange(transform, range, height = Infinity, options
|
|||||||
h++
|
h++
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform.splitNodeByKey(node.key, offset, { normalize })
|
transform.splitNodeByKey(node.key, offset, { normalize })
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -658,11 +646,10 @@ export function splitInlineAtRange(transform, range, height = Infinity, options
|
|||||||
* @param {Mixed} mark
|
* @param {Mixed} mark
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @property {Boolean} normalize
|
* @property {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function toggleMarkAtRange(transform, range, mark, options = {}) {
|
export function toggleMarkAtRange(transform, range, mark, options = {}) {
|
||||||
if (range.isCollapsed) return transform
|
if (range.isCollapsed) return
|
||||||
|
|
||||||
mark = Normalize.mark(mark)
|
mark = Normalize.mark(mark)
|
||||||
|
|
||||||
@@ -677,8 +664,6 @@ export function toggleMarkAtRange(transform, range, mark, options = {}) {
|
|||||||
} else {
|
} else {
|
||||||
transform.addMarkAtRange(range, mark, { normalize })
|
transform.addMarkAtRange(range, mark, { normalize })
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -689,7 +674,6 @@ export function toggleMarkAtRange(transform, range, mark, options = {}) {
|
|||||||
* @param {String|Object} properties
|
* @param {String|Object} properties
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @property {Boolean} normalize
|
* @property {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function unwrapBlockAtRange(transform, range, properties, options = {}) {
|
export function unwrapBlockAtRange(transform, range, properties, options = {}) {
|
||||||
@@ -728,17 +712,17 @@ export function unwrapBlockAtRange(transform, range, properties, options = {}) {
|
|||||||
|
|
||||||
if (first == firstMatch && last == lastMatch) {
|
if (first == firstMatch && last == lastMatch) {
|
||||||
block.nodes.forEach((child, i) => {
|
block.nodes.forEach((child, i) => {
|
||||||
transform.moveNodeByKey(child.key, parent.key, index + i, { normalize: false })
|
transform.moveNodeByKey(child.key, parent.key, index + i, OPTS)
|
||||||
})
|
})
|
||||||
|
|
||||||
transform.removeNodeByKey(block.key, { normalize: false })
|
transform.removeNodeByKey(block.key, OPTS)
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (last == lastMatch) {
|
else if (last == lastMatch) {
|
||||||
block.nodes
|
block.nodes
|
||||||
.skipUntil(n => n == firstMatch)
|
.skipUntil(n => n == firstMatch)
|
||||||
.forEach((child, i) => {
|
.forEach((child, i) => {
|
||||||
transform.moveNodeByKey(child.key, parent.key, index + 1 + i, { normalize: false })
|
transform.moveNodeByKey(child.key, parent.key, index + 1 + i, OPTS)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -747,23 +731,23 @@ export function unwrapBlockAtRange(transform, range, properties, options = {}) {
|
|||||||
.takeUntil(n => n == lastMatch)
|
.takeUntil(n => n == lastMatch)
|
||||||
.push(lastMatch)
|
.push(lastMatch)
|
||||||
.forEach((child, i) => {
|
.forEach((child, i) => {
|
||||||
transform.moveNodeByKey(child.key, parent.key, index + i, { normalize: false })
|
transform.moveNodeByKey(child.key, parent.key, index + i, OPTS)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
const offset = block.getOffset(firstMatch.key)
|
const offset = block.getOffset(firstMatch.key)
|
||||||
|
|
||||||
transform.splitNodeByKey(block.key, offset, { normalize: false })
|
transform.splitNodeByKey(block.key, offset, OPTS)
|
||||||
state = transform.state
|
state = transform.state
|
||||||
document = state.document
|
document = state.document
|
||||||
const extra = document.getPreviousSibling(firstMatch.key)
|
const extra = document.getPreviousSibling(firstMatch.key)
|
||||||
|
|
||||||
children.forEach((child, i) => {
|
children.forEach((child, i) => {
|
||||||
transform.moveNodeByKey(child.key, parent.key, index + 1 + i, { normalize: false })
|
transform.moveNodeByKey(child.key, parent.key, index + 1 + i, OPTS)
|
||||||
})
|
})
|
||||||
|
|
||||||
transform.removeNodeByKey(extra.key, { normalize: false })
|
transform.removeNodeByKey(extra.key, OPTS)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -771,8 +755,6 @@ export function unwrapBlockAtRange(transform, range, properties, options = {}) {
|
|||||||
if (normalize) {
|
if (normalize) {
|
||||||
transform.normalizeDocument(SCHEMA)
|
transform.normalizeDocument(SCHEMA)
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -783,7 +765,6 @@ export function unwrapBlockAtRange(transform, range, properties, options = {}) {
|
|||||||
* @param {String|Object} properties
|
* @param {String|Object} properties
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @property {Boolean} normalize
|
* @property {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function unwrapInlineAtRange(transform, range, properties, options = {}) {
|
export function unwrapInlineAtRange(transform, range, properties, options = {}) {
|
||||||
@@ -812,7 +793,7 @@ export function unwrapInlineAtRange(transform, range, properties, options = {})
|
|||||||
const index = parent.nodes.indexOf(inline)
|
const index = parent.nodes.indexOf(inline)
|
||||||
|
|
||||||
inline.nodes.forEach((child, i) => {
|
inline.nodes.forEach((child, i) => {
|
||||||
transform.moveNodeByKey(child.key, parent.key, index + i, { normalize: false })
|
transform.moveNodeByKey(child.key, parent.key, index + i, OPTS)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -820,8 +801,6 @@ export function unwrapInlineAtRange(transform, range, properties, options = {})
|
|||||||
if (normalize) {
|
if (normalize) {
|
||||||
transform.normalizeDocument(SCHEMA)
|
transform.normalizeDocument(SCHEMA)
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -832,7 +811,6 @@ export function unwrapInlineAtRange(transform, range, properties, options = {})
|
|||||||
* @param {Block|Object|String} block
|
* @param {Block|Object|String} block
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @property {Boolean} normalize
|
* @property {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function wrapBlockAtRange(transform, range, block, options = {}) {
|
export function wrapBlockAtRange(transform, range, block, options = {}) {
|
||||||
@@ -882,18 +860,16 @@ export function wrapBlockAtRange(transform, range, block, options = {}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// inject the new block node into the parent
|
// inject the new block node into the parent
|
||||||
transform.insertNodeByKey(parent.key, index, block, { normalize: false })
|
transform.insertNodeByKey(parent.key, index, block, OPTS)
|
||||||
|
|
||||||
// move the sibling nodes into the new block node
|
// move the sibling nodes into the new block node
|
||||||
siblings.forEach((node, i) => {
|
siblings.forEach((node, i) => {
|
||||||
transform.moveNodeByKey(node.key, block.key, i, { normalize: false })
|
transform.moveNodeByKey(node.key, block.key, i, OPTS)
|
||||||
})
|
})
|
||||||
|
|
||||||
if (normalize) {
|
if (normalize) {
|
||||||
transform.normalizeNodeByKey(parent.key, SCHEMA)
|
transform.normalizeNodeByKey(parent.key, SCHEMA)
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -904,11 +880,10 @@ export function wrapBlockAtRange(transform, range, block, options = {}) {
|
|||||||
* @param {Inline|Object|String} inline
|
* @param {Inline|Object|String} inline
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @property {Boolean} normalize
|
* @property {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function wrapInlineAtRange(transform, range, inline, options = {}) {
|
export function wrapInlineAtRange(transform, range, inline, options = {}) {
|
||||||
if (range.isCollapsed) return transform
|
if (range.isCollapsed) return
|
||||||
|
|
||||||
inline = Normalize.inline(inline)
|
inline = Normalize.inline(inline)
|
||||||
inline = inline.merge({ nodes: inline.nodes.clear() })
|
inline = inline.merge({ nodes: inline.nodes.clear() })
|
||||||
@@ -935,11 +910,11 @@ export function wrapInlineAtRange(transform, range, inline, options = {}) {
|
|||||||
|
|
||||||
if (startBlock == endBlock) {
|
if (startBlock == endBlock) {
|
||||||
if (endOff != endChild.length) {
|
if (endOff != endChild.length) {
|
||||||
transform.splitNodeByKey(endChild.key, endOff, { normalize: false })
|
transform.splitNodeByKey(endChild.key, endOff, OPTS)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (startOff != 0) {
|
if (startOff != 0) {
|
||||||
transform.splitNodeByKey(startChild.key, startOff, { normalize: false })
|
transform.splitNodeByKey(startChild.key, startOff, OPTS)
|
||||||
}
|
}
|
||||||
|
|
||||||
state = transform.state
|
state = transform.state
|
||||||
@@ -961,10 +936,10 @@ export function wrapInlineAtRange(transform, range, inline, options = {}) {
|
|||||||
|
|
||||||
const node = inline.regenerateKey()
|
const node = inline.regenerateKey()
|
||||||
|
|
||||||
transform.insertNodeByKey(startBlock.key, startInnerIndex, node, { normalize: false })
|
transform.insertNodeByKey(startBlock.key, startInnerIndex, node, OPTS)
|
||||||
|
|
||||||
inlines.forEach((child, i) => {
|
inlines.forEach((child, i) => {
|
||||||
transform.moveNodeByKey(child.key, node.key, i, { normalize: false })
|
transform.moveNodeByKey(child.key, node.key, i, OPTS)
|
||||||
})
|
})
|
||||||
|
|
||||||
if (normalize) {
|
if (normalize) {
|
||||||
@@ -973,8 +948,8 @@ export function wrapInlineAtRange(transform, range, inline, options = {}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
transform.splitNodeByKey(startChild.key, startOff, { normalize: false })
|
transform.splitNodeByKey(startChild.key, startOff, OPTS)
|
||||||
transform.splitNodeByKey(endChild.key, endOff, { normalize: false })
|
transform.splitNodeByKey(endChild.key, endOff, OPTS)
|
||||||
|
|
||||||
state = transform.state
|
state = transform.state
|
||||||
document = state.document
|
document = state.document
|
||||||
@@ -986,15 +961,15 @@ export function wrapInlineAtRange(transform, range, inline, options = {}) {
|
|||||||
const startNode = inline.regenerateKey()
|
const startNode = inline.regenerateKey()
|
||||||
const endNode = inline.regenerateKey()
|
const endNode = inline.regenerateKey()
|
||||||
|
|
||||||
transform.insertNodeByKey(startBlock.key, startIndex - 1, startNode, { normalize: false })
|
transform.insertNodeByKey(startBlock.key, startIndex - 1, startNode, OPTS)
|
||||||
transform.insertNodeByKey(endBlock.key, endIndex, endNode, { normalize: false })
|
transform.insertNodeByKey(endBlock.key, endIndex, endNode, OPTS)
|
||||||
|
|
||||||
startInlines.forEach((child, i) => {
|
startInlines.forEach((child, i) => {
|
||||||
transform.moveNodeByKey(child.key, startNode.key, i, { normalize: false })
|
transform.moveNodeByKey(child.key, startNode.key, i, OPTS)
|
||||||
})
|
})
|
||||||
|
|
||||||
endInlines.forEach((child, i) => {
|
endInlines.forEach((child, i) => {
|
||||||
transform.moveNodeByKey(child.key, endNode.key, i, { normalize: false })
|
transform.moveNodeByKey(child.key, endNode.key, i, OPTS)
|
||||||
})
|
})
|
||||||
|
|
||||||
if (normalize) {
|
if (normalize) {
|
||||||
@@ -1005,10 +980,10 @@ export function wrapInlineAtRange(transform, range, inline, options = {}) {
|
|||||||
|
|
||||||
blocks.slice(1, -1).forEach((block) => {
|
blocks.slice(1, -1).forEach((block) => {
|
||||||
const node = inline.regenerateKey()
|
const node = inline.regenerateKey()
|
||||||
transform.insertNodeByKey(block.key, 0, node, { normalize: false })
|
transform.insertNodeByKey(block.key, 0, node, OPTS)
|
||||||
|
|
||||||
block.nodes.forEach((child, i) => {
|
block.nodes.forEach((child, i) => {
|
||||||
transform.moveNodeByKey(child.key, node.key, i, { normalize: false })
|
transform.moveNodeByKey(child.key, node.key, i, OPTS)
|
||||||
})
|
})
|
||||||
|
|
||||||
if (normalize) {
|
if (normalize) {
|
||||||
@@ -1016,8 +991,6 @@ export function wrapInlineAtRange(transform, range, inline, options = {}) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1029,7 +1002,6 @@ export function wrapInlineAtRange(transform, range, inline, options = {}) {
|
|||||||
* @param {String} suffix (optional)
|
* @param {String} suffix (optional)
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @property {Boolean} normalize
|
* @property {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function wrapTextAtRange(transform, range, prefix, suffix = prefix, options = {}) {
|
export function wrapTextAtRange(transform, range, prefix, suffix = prefix, options = {}) {
|
||||||
@@ -1042,7 +1014,6 @@ export function wrapTextAtRange(transform, range, prefix, suffix = prefix, optio
|
|||||||
end = end.moveForward(prefix.length)
|
end = end.moveForward(prefix.length)
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform
|
transform.insertTextAtRange(start, prefix, [], { normalize })
|
||||||
.insertTextAtRange(start, prefix, [], { normalize })
|
transform.insertTextAtRange(end, suffix, [], { normalize })
|
||||||
.insertTextAtRange(end, suffix, [], { normalize })
|
|
||||||
}
|
}
|
||||||
|
@@ -11,8 +11,7 @@ import SCHEMA from '../schemas/core'
|
|||||||
* @param {Number} length
|
* @param {Number} length
|
||||||
* @param {Mixed} mark
|
* @param {Mixed} mark
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @param {Boolean} normalize
|
* @property {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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -40,8 +37,7 @@ export function addMarkByKey(transform, key, offset, length, mark, options = {})
|
|||||||
* @param {Number} index
|
* @param {Number} index
|
||||||
* @param {Node} node
|
* @param {Node} node
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @param {Boolean} normalize
|
* @property {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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -68,8 +62,7 @@ export function insertNodeByKey(transform, key, index, node, options = {}) {
|
|||||||
* @param {String} text
|
* @param {String} text
|
||||||
* @param {Set<Mark>} marks (optional)
|
* @param {Set<Mark>} marks (optional)
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @param {Boolean} normalize
|
* @property {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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -95,8 +86,7 @@ export function insertTextByKey(transform, key, offset, text, marks, options = {
|
|||||||
* @param {String} key
|
* @param {String} key
|
||||||
* @param {String} withKey
|
* @param {String} withKey
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @param {Boolean} normalize
|
* @property {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function joinNodeByKey(transform, key, withKey, options = {}) {
|
export function joinNodeByKey(transform, key, withKey, options = {}) {
|
||||||
@@ -110,14 +100,8 @@ 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -129,8 +113,7 @@ export function joinNodeByKey(transform, key, withKey, options = {}) {
|
|||||||
* @param {String} newKey
|
* @param {String} newKey
|
||||||
* @param {Number} index
|
* @param {Number} index
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @param {Boolean} normalize
|
* @property {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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -159,8 +140,7 @@ export function moveNodeByKey(transform, key, newKey, newIndex, options = {}) {
|
|||||||
* @param {Number} length
|
* @param {Number} length
|
||||||
* @param {Mark} mark
|
* @param {Mark} mark
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @param {Boolean} normalize
|
* @property {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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -186,8 +164,7 @@ export function removeMarkByKey(transform, key, offset, length, mark, options =
|
|||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {String} key
|
* @param {String} key
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @param {Boolean} normalize
|
* @property {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function removeNodeByKey(transform, key, options = {}) {
|
export function removeNodeByKey(transform, key, options = {}) {
|
||||||
@@ -200,14 +177,8 @@ 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -218,8 +189,7 @@ export function removeNodeByKey(transform, key, options = {}) {
|
|||||||
* @param {Number} offset
|
* @param {Number} offset
|
||||||
* @param {Number} length
|
* @param {Number} length
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @param {Boolean} normalize
|
* @property {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function removeTextByKey(transform, key, offset, length, options = {}) {
|
export function removeTextByKey(transform, key, offset, length, options = {}) {
|
||||||
@@ -231,11 +201,9 @@ export function removeTextByKey(transform, key, offset, length, options = {}) {
|
|||||||
transform.removeTextOperation(path, offset, length)
|
transform.removeTextOperation(path, offset, length)
|
||||||
|
|
||||||
if (normalize) {
|
if (normalize) {
|
||||||
const parent = document.getParent(key)
|
const block = document.getClosestBlock(key)
|
||||||
transform.normalizeParentsByKey(parent.key, SCHEMA)
|
transform.normalizeNodeByKey(block.key, SCHEMA)
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -247,8 +215,7 @@ export function removeTextByKey(transform, key, offset, length, options = {}) {
|
|||||||
* @param {Number} length
|
* @param {Number} length
|
||||||
* @param {Mark} mark
|
* @param {Mark} mark
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @param {Boolean} normalize
|
* @property {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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -277,8 +242,7 @@ export function setMarkByKey(transform, key, offset, length, mark, properties, o
|
|||||||
* @param {String} key
|
* @param {String} key
|
||||||
* @param {Object|String} properties
|
* @param {Object|String} properties
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @param {Boolean} normalize
|
* @property {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function setNodeByKey(transform, key, properties, options = {}) {
|
export function setNodeByKey(transform, key, properties, options = {}) {
|
||||||
@@ -292,14 +256,8 @@ 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -309,8 +267,7 @@ export function setNodeByKey(transform, key, properties, options = {}) {
|
|||||||
* @param {String} key
|
* @param {String} key
|
||||||
* @param {Number} offset
|
* @param {Number} offset
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @param {Boolean} normalize
|
* @property {Boolean} normalize
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function splitNodeByKey(transform, key, offset, options = {}) {
|
export function splitNodeByKey(transform, key, offset, options = {}) {
|
||||||
@@ -323,14 +280,8 @@ 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -340,16 +291,17 @@ export function splitNodeByKey(transform, key, offset, options = {}) {
|
|||||||
* @param {String} key
|
* @param {String} key
|
||||||
* @param {Object|String} properties
|
* @param {Object|String} properties
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @param {Boolean} normalize
|
* @property {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)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -359,16 +311,17 @@ export function unwrapInlineByKey(transform, key, properties, options) {
|
|||||||
* @param {String} key
|
* @param {String} key
|
||||||
* @param {Object|String} properties
|
* @param {Object|String} properties
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @param {Boolean} normalize
|
* @property {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)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -378,8 +331,7 @@ export function unwrapBlockByKey(transform, key, properties, options) {
|
|||||||
* @param {String} key The node to wrap
|
* @param {String} key The node to wrap
|
||||||
* @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
|
* @property {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)
|
|
||||||
}
|
}
|
||||||
|
@@ -126,10 +126,14 @@ import {
|
|||||||
focus,
|
focus,
|
||||||
moveBackward,
|
moveBackward,
|
||||||
moveForward,
|
moveForward,
|
||||||
|
moveEndOffset,
|
||||||
|
moveStartOffset,
|
||||||
moveTo,
|
moveTo,
|
||||||
moveToOffsets,
|
moveToOffsets,
|
||||||
moveToRangeOf,
|
moveToRangeOf,
|
||||||
|
unsetMarks,
|
||||||
unsetSelection,
|
unsetSelection,
|
||||||
|
snapshotSelection,
|
||||||
} from './on-selection'
|
} from './on-selection'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -151,7 +155,6 @@ import {
|
|||||||
normalizeDocument,
|
normalizeDocument,
|
||||||
normalizeSelection,
|
normalizeSelection,
|
||||||
normalizeNodeByKey,
|
normalizeNodeByKey,
|
||||||
normalizeParentsByKey,
|
|
||||||
} from './normalize'
|
} from './normalize'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -278,10 +281,14 @@ export default {
|
|||||||
focus,
|
focus,
|
||||||
moveBackward,
|
moveBackward,
|
||||||
moveForward,
|
moveForward,
|
||||||
|
moveEndOffset,
|
||||||
|
moveStartOffset,
|
||||||
moveTo,
|
moveTo,
|
||||||
moveToOffsets,
|
moveToOffsets,
|
||||||
moveToRangeOf,
|
moveToRangeOf,
|
||||||
|
unsetMarks,
|
||||||
unsetSelection,
|
unsetSelection,
|
||||||
|
snapshotSelection,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* History.
|
* History.
|
||||||
@@ -299,5 +306,4 @@ export default {
|
|||||||
normalizeDocument,
|
normalizeDocument,
|
||||||
normalizeSelection,
|
normalizeSelection,
|
||||||
normalizeNodeByKey,
|
normalizeNodeByKey,
|
||||||
normalizeParentsByKey,
|
|
||||||
}
|
}
|
||||||
|
@@ -8,13 +8,11 @@ import warn from '../utils/warn'
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Schema} schema
|
* @param {Schema} schema
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function normalize(transform, schema) {
|
export function normalize(transform, schema) {
|
||||||
transform.normalizeDocument(schema)
|
transform.normalizeDocument(schema)
|
||||||
transform.normalizeSelection(schema)
|
transform.normalizeSelection(schema)
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -22,14 +20,12 @@ export function normalize(transform, schema) {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Schema} schema
|
* @param {Schema} schema
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function normalizeDocument(transform, schema) {
|
export function normalizeDocument(transform, schema) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { document } = state
|
const { document } = state
|
||||||
transform.normalizeNodeByKey(document.key, schema)
|
transform.normalizeNodeByKey(document.key, schema)
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -38,57 +34,26 @@ export function normalizeDocument(transform, schema) {
|
|||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Node|String} key
|
* @param {Node|String} key
|
||||||
* @param {Schema} schema
|
* @param {Schema} schema
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function normalizeNodeByKey(transform, key, schema) {
|
export function normalizeNodeByKey(transform, key, schema) {
|
||||||
assertSchema(schema)
|
assertSchema(schema)
|
||||||
key = Normalize.key(key)
|
|
||||||
|
|
||||||
// If the schema has no validation rules, there's nothing to normalize.
|
// If the schema has no validation rules, there's nothing to normalize.
|
||||||
if (!schema.hasValidators) {
|
if (!schema.hasValidators) return
|
||||||
return transform
|
|
||||||
}
|
|
||||||
|
|
||||||
|
key = Normalize.key(key)
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { document } = state
|
const { document } = state
|
||||||
const node = document.assertNode(key)
|
const node = document.assertNode(key)
|
||||||
|
|
||||||
normalizeNodeWith(transform, node, schema)
|
normalizeNodeAndChildren(transform, node, schema)
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Normalize a `node` and its parents with a `schema`.
|
* Normalize the selection.
|
||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Node|String} key
|
|
||||||
* @param {Schema} schema
|
|
||||||
* @return {Transform}
|
|
||||||
*/
|
|
||||||
|
|
||||||
export function normalizeParentsByKey(transform, key, schema) {
|
|
||||||
assertSchema(schema)
|
|
||||||
key = Normalize.key(key)
|
|
||||||
|
|
||||||
// If the schema has no validation rules, there's nothing to normalize.
|
|
||||||
if (!schema.hasValidators) {
|
|
||||||
return transform
|
|
||||||
}
|
|
||||||
|
|
||||||
const { state } = transform
|
|
||||||
const { document } = state
|
|
||||||
const node = document.assertNode(key)
|
|
||||||
|
|
||||||
normalizeParentsWith(transform, node, schema)
|
|
||||||
return transform
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Normalize only the selection.
|
|
||||||
*
|
|
||||||
* @param {Transform} transform
|
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function normalizeSelection(transform) {
|
export function normalizeSelection(transform) {
|
||||||
@@ -117,7 +82,6 @@ export function normalizeSelection(transform) {
|
|||||||
|
|
||||||
state = state.merge({ selection })
|
state = state.merge({ selection })
|
||||||
transform.state = state
|
transform.state = state
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -126,58 +90,29 @@ export function normalizeSelection(transform) {
|
|||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Node} node
|
* @param {Node} node
|
||||||
* @param {Schema} schema
|
* @param {Schema} schema
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function normalizeNodeWith(transform, node, schema) {
|
function normalizeNodeAndChildren(transform, node, schema) {
|
||||||
// For performance considerations, we will check if the transform was changed.
|
// For performance considerations, we will check if the transform has actually
|
||||||
const opCount = transform.operations.length
|
// added operations to the queue.
|
||||||
|
const count = transform.operations.length
|
||||||
|
|
||||||
// Iterate over its children.
|
// Iterate over its children.
|
||||||
normalizeChildrenWith(transform, node, schema)
|
if (node.kind != 'text') {
|
||||||
|
node.nodes.forEach((child) => {
|
||||||
|
normalizeNodeAndChildren(transform, child, schema)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// Re-find the node reference if necessary.
|
// Re-find the node reference if necessary.
|
||||||
if (transform.operations.length != opCount) {
|
if (transform.operations.length != count) {
|
||||||
node = refindNode(transform, node)
|
node = refindNode(transform, node)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now normalize the node itself if it still exists.
|
// Now normalize the node itself if it still exists.
|
||||||
if (node) {
|
if (node) {
|
||||||
normalizeNodeOnly(transform, node, schema)
|
normalizeNode(transform, node, schema)
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Normalize a `node` and its parents with a `schema`.
|
|
||||||
*
|
|
||||||
* @param {Transform} transform
|
|
||||||
* @param {Node} node
|
|
||||||
* @param {Schema} schema
|
|
||||||
* @return {Transform}
|
|
||||||
*/
|
|
||||||
|
|
||||||
function normalizeParentsWith(transform, node, schema) {
|
|
||||||
normalizeNodeOnly(transform, node, schema)
|
|
||||||
|
|
||||||
// Normalize went back up to the very top of the document.
|
|
||||||
if (node.kind == 'document') {
|
|
||||||
return transform
|
|
||||||
}
|
|
||||||
|
|
||||||
// Re-find the node first.
|
|
||||||
node = refindNode(transform, node)
|
|
||||||
|
|
||||||
if (!node) {
|
|
||||||
return transform
|
|
||||||
}
|
|
||||||
|
|
||||||
const { state } = transform
|
|
||||||
const { document } = state
|
|
||||||
const parent = document.getParent(node.key)
|
|
||||||
|
|
||||||
return normalizeParentsWith(transform, parent, schema)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -197,51 +132,30 @@ function refindNode(transform, node) {
|
|||||||
: document.getDescendant(node.key)
|
: document.getDescendant(node.key)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Normalize the children of a `node` with a `schema`.
|
|
||||||
*
|
|
||||||
* @param {Transform} transform
|
|
||||||
* @param {Node} node
|
|
||||||
* @param {Schema} schema
|
|
||||||
* @return {Transform}
|
|
||||||
*/
|
|
||||||
|
|
||||||
function normalizeChildrenWith(transform, node, schema) {
|
|
||||||
if (node.kind == 'text') return transform
|
|
||||||
|
|
||||||
node.nodes.forEach((child) => {
|
|
||||||
normalizeNodeWith(transform, child, schema)
|
|
||||||
})
|
|
||||||
|
|
||||||
return transform
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Normalize a `node` with a `schema`, but not its children.
|
* Normalize a `node` with a `schema`, but not its children.
|
||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Node} node
|
* @param {Node} node
|
||||||
* @param {Schema} schema
|
* @param {Schema} schema
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function normalizeNodeOnly(transform, node, schema) {
|
function normalizeNode(transform, node, schema) {
|
||||||
let max = schema.rules.length
|
let max = schema.rules.length
|
||||||
let iterations = 0
|
let iterations = 0
|
||||||
|
|
||||||
function iterate(t, n) {
|
function iterate(t, n) {
|
||||||
const failure = n.validate(schema)
|
const failure = n.validate(schema)
|
||||||
if (!failure) return t
|
if (!failure) return
|
||||||
|
|
||||||
|
// Run the `normalize` function for the rule with the invalid value.
|
||||||
const { value, rule } = failure
|
const { value, rule } = failure
|
||||||
|
|
||||||
// Rule the `normalize` function for the rule with the invalid value.
|
|
||||||
rule.normalize(t, n, value)
|
rule.normalize(t, n, value)
|
||||||
|
|
||||||
// Re-find the node reference, in case it was updated. If the node no longer
|
// Re-find the node reference, in case it was updated. If the node no longer
|
||||||
// exists, we're done for this branch.
|
// exists, we're done for this branch.
|
||||||
n = refindNode(t, n)
|
n = refindNode(t, n)
|
||||||
if (!n) return t
|
if (!n) return
|
||||||
|
|
||||||
// Increment the iterations counter, and check to make sure that we haven't
|
// Increment the iterations counter, and check to make sure that we haven't
|
||||||
// exceeded the max. Without this check, it's easy for the `validate` or
|
// exceeded the max. Without this check, it's easy for the `validate` or
|
||||||
@@ -254,10 +168,10 @@ function normalizeNodeOnly(transform, node, schema) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, iterate again.
|
// Otherwise, iterate again.
|
||||||
return iterate(t, n)
|
iterate(t, n)
|
||||||
}
|
}
|
||||||
|
|
||||||
return iterate(transform, node)
|
iterate(transform, node)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -267,9 +181,9 @@ function normalizeNodeOnly(transform, node, schema) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
function assertSchema(schema) {
|
function assertSchema(schema) {
|
||||||
if (schema instanceof Schema) return
|
if (schema instanceof Schema) {
|
||||||
|
return
|
||||||
if (schema == null) {
|
} else if (schema == null) {
|
||||||
throw new Error('You must pass a `schema` object.')
|
throw new Error('You must pass a `schema` object.')
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`You passed an invalid \`schema\` object: ${schema}.`)
|
throw new Error(`You passed an invalid \`schema\` object: ${schema}.`)
|
||||||
|
@@ -3,7 +3,6 @@
|
|||||||
* Redo to the next state in the history.
|
* Redo to the next state in the history.
|
||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function redo(transform) {
|
export function redo(transform) {
|
||||||
@@ -13,7 +12,7 @@ export function redo(transform) {
|
|||||||
|
|
||||||
// If there's no next snapshot, abort.
|
// If there's no next snapshot, abort.
|
||||||
let next = redos.peek()
|
let next = redos.peek()
|
||||||
if (!next) return transform
|
if (!next) return
|
||||||
|
|
||||||
// Shift the next state into the undo stack.
|
// Shift the next state into the undo stack.
|
||||||
redos = redos.pop()
|
redos = redos.pop()
|
||||||
@@ -31,7 +30,6 @@ export function redo(transform) {
|
|||||||
|
|
||||||
// Update the transform.
|
// Update the transform.
|
||||||
transform.state = state
|
transform.state = state
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -39,7 +37,6 @@ export function redo(transform) {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function save(transform, options = {}) {
|
export function save(transform, options = {}) {
|
||||||
@@ -49,7 +46,7 @@ export function save(transform, options = {}) {
|
|||||||
let { undos, redos } = history
|
let { undos, redos } = history
|
||||||
|
|
||||||
// If there are no operations, abort.
|
// If there are no operations, abort.
|
||||||
if (!operations.length) return transform
|
if (!operations.length) return
|
||||||
|
|
||||||
// Create a new save point or merge the operations into the previous one.
|
// Create a new save point or merge the operations into the previous one.
|
||||||
if (merge) {
|
if (merge) {
|
||||||
@@ -71,14 +68,12 @@ export function save(transform, options = {}) {
|
|||||||
|
|
||||||
// Update the transform.
|
// Update the transform.
|
||||||
transform.state = state
|
transform.state = state
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Undo the previous operations in the history.
|
* Undo the previous operations in the history.
|
||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function undo(transform) {
|
export function undo(transform) {
|
||||||
@@ -88,7 +83,7 @@ export function undo(transform) {
|
|||||||
|
|
||||||
// If there's no previous snapshot, abort.
|
// If there's no previous snapshot, abort.
|
||||||
let previous = undos.peek()
|
let previous = undos.peek()
|
||||||
if (!previous) return transform
|
if (!previous) return
|
||||||
|
|
||||||
// Shift the previous operations into the redo stack.
|
// Shift the previous operations into the redo stack.
|
||||||
undos = undos.pop()
|
undos = undos.pop()
|
||||||
@@ -108,5 +103,4 @@ export function undo(transform) {
|
|||||||
|
|
||||||
// Update the transform.
|
// Update the transform.
|
||||||
transform.state = state
|
transform.state = state
|
||||||
return transform
|
|
||||||
}
|
}
|
||||||
|
@@ -3,70 +3,65 @@
|
|||||||
* Blur the selection.
|
* Blur the selection.
|
||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function blur(transform) {
|
export function blur(transform) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { selection } = state
|
const { selection } = state
|
||||||
const sel = selection.blur()
|
const sel = selection.blur()
|
||||||
return transform.setSelectionOperation(sel)
|
transform.setSelectionOperation(sel)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move the focus point to the anchor point.
|
* Move the focus point to the anchor point.
|
||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function collapseToAnchor(transform) {
|
export function collapseToAnchor(transform) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { selection } = state
|
const { selection } = state
|
||||||
const sel = selection.collapseToAnchor()
|
const sel = selection.collapseToAnchor()
|
||||||
return transform.setSelectionOperation(sel)
|
transform.setSelectionOperation(sel)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move the start point to the end point.
|
* Move the start point to the end point.
|
||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function collapseToEnd(transform) {
|
export function collapseToEnd(transform) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { selection } = state
|
const { selection } = state
|
||||||
const sel = selection.collapseToEnd()
|
const sel = selection.collapseToEnd()
|
||||||
return transform.setSelectionOperation(sel)
|
transform.setSelectionOperation(sel)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move the anchor point to the focus point.
|
* Move the anchor point to the focus point.
|
||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function collapseToFocus(transform) {
|
export function collapseToFocus(transform) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { selection } = state
|
const { selection } = state
|
||||||
const sel = selection.collapseToFocus()
|
const sel = selection.collapseToFocus()
|
||||||
return transform.setSelectionOperation(sel)
|
transform.setSelectionOperation(sel)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move the end point to the start point.
|
* Move the end point to the start point.
|
||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function collapseToStart(transform) {
|
export function collapseToStart(transform) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { selection } = state
|
const { selection } = state
|
||||||
const sel = selection.collapseToStart()
|
const sel = selection.collapseToStart()
|
||||||
return transform.setSelectionOperation(sel)
|
transform.setSelectionOperation(sel)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -74,21 +69,19 @@ export function collapseToStart(transform) {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Node} node
|
* @param {Node} node
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function collapseToEndOf(transform, node) {
|
export function collapseToEndOf(transform, node) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { selection } = state
|
const { selection } = state
|
||||||
const sel = selection.collapseToEndOf(node)
|
const sel = selection.collapseToEndOf(node)
|
||||||
return transform.setSelectionOperation(sel)
|
transform.setSelectionOperation(sel)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move the selection to the end of the next block.
|
* Move the selection to the end of the next block.
|
||||||
*
|
*
|
||||||
* @param {Transform} tansform
|
* @param {Transform} tansform
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function collapseToEndOfNextBlock(transform) {
|
export function collapseToEndOfNextBlock(transform) {
|
||||||
@@ -97,17 +90,16 @@ export function collapseToEndOfNextBlock(transform) {
|
|||||||
const blocks = document.getBlocksAtRange(selection)
|
const blocks = document.getBlocksAtRange(selection)
|
||||||
const last = blocks.last()
|
const last = blocks.last()
|
||||||
const next = document.getNextBlock(last)
|
const next = document.getNextBlock(last)
|
||||||
if (!next) return transform
|
if (!next) return
|
||||||
|
|
||||||
const sel = selection.collapseToEndOf(next)
|
const sel = selection.collapseToEndOf(next)
|
||||||
return transform.setSelectionOperation(sel)
|
transform.setSelectionOperation(sel)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move the selection to the end of the next text.
|
* Move the selection to the end of the next text.
|
||||||
*
|
*
|
||||||
* @param {Transform} tansform
|
* @param {Transform} tansform
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function collapseToEndOfNextText(transform) {
|
export function collapseToEndOfNextText(transform) {
|
||||||
@@ -116,17 +108,16 @@ export function collapseToEndOfNextText(transform) {
|
|||||||
const texts = document.getTextsAtRange(selection)
|
const texts = document.getTextsAtRange(selection)
|
||||||
const last = texts.last()
|
const last = texts.last()
|
||||||
const next = document.getNextText(last)
|
const next = document.getNextText(last)
|
||||||
if (!next) return transform
|
if (!next) return
|
||||||
|
|
||||||
const sel = selection.collapseToEndOf(next)
|
const sel = selection.collapseToEndOf(next)
|
||||||
return transform.setSelectionOperation(sel)
|
transform.setSelectionOperation(sel)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move the selection to the end of the previous block.
|
* Move the selection to the end of the previous block.
|
||||||
*
|
*
|
||||||
* @param {Transform} tansform
|
* @param {Transform} tansform
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function collapseToEndOfPreviousBlock(transform) {
|
export function collapseToEndOfPreviousBlock(transform) {
|
||||||
@@ -135,17 +126,16 @@ export function collapseToEndOfPreviousBlock(transform) {
|
|||||||
const blocks = document.getBlocksAtRange(selection)
|
const blocks = document.getBlocksAtRange(selection)
|
||||||
const first = blocks.first()
|
const first = blocks.first()
|
||||||
const previous = document.getPreviousBlock(first)
|
const previous = document.getPreviousBlock(first)
|
||||||
if (!previous) return transform
|
if (!previous) return
|
||||||
|
|
||||||
const sel = selection.collapseToEndOf(previous)
|
const sel = selection.collapseToEndOf(previous)
|
||||||
return transform.setSelectionOperation(sel)
|
transform.setSelectionOperation(sel)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move the selection to the end of the previous text.
|
* Move the selection to the end of the previous text.
|
||||||
*
|
*
|
||||||
* @param {Transform} tansform
|
* @param {Transform} tansform
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function collapseToEndOfPreviousText(transform) {
|
export function collapseToEndOfPreviousText(transform) {
|
||||||
@@ -154,10 +144,10 @@ export function collapseToEndOfPreviousText(transform) {
|
|||||||
const texts = document.getTextsAtRange(selection)
|
const texts = document.getTextsAtRange(selection)
|
||||||
const first = texts.first()
|
const first = texts.first()
|
||||||
const previous = document.getPreviousText(first)
|
const previous = document.getPreviousText(first)
|
||||||
if (!previous) return transform
|
if (!previous) return
|
||||||
|
|
||||||
const sel = selection.collapseToEndOf(previous)
|
const sel = selection.collapseToEndOf(previous)
|
||||||
return transform.setSelectionOperation(sel)
|
transform.setSelectionOperation(sel)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -165,21 +155,19 @@ export function collapseToEndOfPreviousText(transform) {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Node} node
|
* @param {Node} node
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function collapseToStartOf(transform, node) {
|
export function collapseToStartOf(transform, node) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { selection } = state
|
const { selection } = state
|
||||||
const sel = selection.collapseToStartOf(node)
|
const sel = selection.collapseToStartOf(node)
|
||||||
return transform.setSelectionOperation(sel)
|
transform.setSelectionOperation(sel)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move the selection to the start of the next block.
|
* Move the selection to the start of the next block.
|
||||||
*
|
*
|
||||||
* @param {Transform} tansform
|
* @param {Transform} tansform
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function collapseToStartOfNextBlock(transform) {
|
export function collapseToStartOfNextBlock(transform) {
|
||||||
@@ -188,17 +176,16 @@ export function collapseToStartOfNextBlock(transform) {
|
|||||||
const blocks = document.getBlocksAtRange(selection)
|
const blocks = document.getBlocksAtRange(selection)
|
||||||
const last = blocks.last()
|
const last = blocks.last()
|
||||||
const next = document.getNextBlock(last)
|
const next = document.getNextBlock(last)
|
||||||
if (!next) return transform
|
if (!next) return
|
||||||
|
|
||||||
const sel = selection.collapseToStartOf(next)
|
const sel = selection.collapseToStartOf(next)
|
||||||
return transform.setSelectionOperation(sel)
|
transform.setSelectionOperation(sel)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move the selection to the start of the next text.
|
* Move the selection to the start of the next text.
|
||||||
*
|
*
|
||||||
* @param {Transform} tansform
|
* @param {Transform} tansform
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function collapseToStartOfNextText(transform) {
|
export function collapseToStartOfNextText(transform) {
|
||||||
@@ -207,17 +194,16 @@ export function collapseToStartOfNextText(transform) {
|
|||||||
const texts = document.getTextsAtRange(selection)
|
const texts = document.getTextsAtRange(selection)
|
||||||
const last = texts.last()
|
const last = texts.last()
|
||||||
const next = document.getNextText(last)
|
const next = document.getNextText(last)
|
||||||
if (!next) return transform
|
if (!next) return
|
||||||
|
|
||||||
const sel = selection.collapseToStartOf(next)
|
const sel = selection.collapseToStartOf(next)
|
||||||
return transform.setSelectionOperation(sel)
|
transform.setSelectionOperation(sel)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move the selection to the start of the previous block.
|
* Move the selection to the start of the previous block.
|
||||||
*
|
*
|
||||||
* @param {Transform} tansform
|
* @param {Transform} tansform
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function collapseToStartOfPreviousBlock(transform) {
|
export function collapseToStartOfPreviousBlock(transform) {
|
||||||
@@ -226,17 +212,16 @@ export function collapseToStartOfPreviousBlock(transform) {
|
|||||||
const blocks = document.getBlocksAtRange(selection)
|
const blocks = document.getBlocksAtRange(selection)
|
||||||
const first = blocks.first()
|
const first = blocks.first()
|
||||||
const previous = document.getPreviousBlock(first)
|
const previous = document.getPreviousBlock(first)
|
||||||
if (!previous) return transform
|
if (!previous) return
|
||||||
|
|
||||||
const sel = selection.collapseToStartOf(previous)
|
const sel = selection.collapseToStartOf(previous)
|
||||||
return transform.setSelectionOperation(sel)
|
transform.setSelectionOperation(sel)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move the selection to the start of the previous text.
|
* Move the selection to the start of the previous text.
|
||||||
*
|
*
|
||||||
* @param {Transform} tansform
|
* @param {Transform} tansform
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function collapseToStartOfPreviousText(transform) {
|
export function collapseToStartOfPreviousText(transform) {
|
||||||
@@ -245,10 +230,10 @@ export function collapseToStartOfPreviousText(transform) {
|
|||||||
const texts = document.getTextsAtRange(selection)
|
const texts = document.getTextsAtRange(selection)
|
||||||
const first = texts.first()
|
const first = texts.first()
|
||||||
const previous = document.getPreviousText(first)
|
const previous = document.getPreviousText(first)
|
||||||
if (!previous) return transform
|
if (!previous) return
|
||||||
|
|
||||||
const sel = selection.collapseToStartOf(previous)
|
const sel = selection.collapseToStartOf(previous)
|
||||||
return transform.setSelectionOperation(sel)
|
transform.setSelectionOperation(sel)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -256,14 +241,13 @@ export function collapseToStartOfPreviousText(transform) {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Number} n (optional)
|
* @param {Number} n (optional)
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function extendBackward(transform, n) {
|
export function extendBackward(transform, n) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { document, selection } = state
|
const { document, selection } = state
|
||||||
const sel = selection.extendBackward(n).normalize(document)
|
const sel = selection.extendBackward(n).normalize(document)
|
||||||
return transform.setSelectionOperation(sel)
|
transform.setSelectionOperation(sel)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -271,14 +255,13 @@ export function extendBackward(transform, n) {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Number} n (optional)
|
* @param {Number} n (optional)
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function extendForward(transform, n) {
|
export function extendForward(transform, n) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { document, selection } = state
|
const { document, selection } = state
|
||||||
const sel = selection.extendForward(n).normalize(document)
|
const sel = selection.extendForward(n).normalize(document)
|
||||||
return transform.setSelectionOperation(sel)
|
transform.setSelectionOperation(sel)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -286,14 +269,13 @@ export function extendForward(transform, n) {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Node} node
|
* @param {Node} node
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function extendToEndOf(transform, node) {
|
export function extendToEndOf(transform, node) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { document, selection } = state
|
const { document, selection } = state
|
||||||
const sel = selection.extendToEndOf(node).normalize(document)
|
const sel = selection.extendToEndOf(node).normalize(document)
|
||||||
return transform.setSelectionOperation(sel)
|
transform.setSelectionOperation(sel)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -301,28 +283,26 @@ export function extendToEndOf(transform, node) {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Node} node
|
* @param {Node} node
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function extendToStartOf(transform, node) {
|
export function extendToStartOf(transform, node) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { document, selection } = state
|
const { document, selection } = state
|
||||||
const sel = selection.extendToStartOf(node).normalize(document)
|
const sel = selection.extendToStartOf(node).normalize(document)
|
||||||
return transform.setSelectionOperation(sel)
|
transform.setSelectionOperation(sel)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Focus the selection.
|
* Focus the selection.
|
||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function focus(transform) {
|
export function focus(transform) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { selection } = state
|
const { selection } = state
|
||||||
const sel = selection.focus()
|
const sel = selection.focus()
|
||||||
return transform.setSelectionOperation(sel)
|
transform.setSelectionOperation(sel)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -330,14 +310,13 @@ export function focus(transform) {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Number} n (optional)
|
* @param {Number} n (optional)
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function moveBackward(transform, n) {
|
export function moveBackward(transform, n) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { document, selection } = state
|
const { document, selection } = state
|
||||||
const sel = selection.moveBackward(n).normalize(document)
|
const sel = selection.moveBackward(n).normalize(document)
|
||||||
return transform.setSelectionOperation(sel)
|
transform.setSelectionOperation(sel)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -345,14 +324,13 @@ export function moveBackward(transform, n) {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Number} n (optional)
|
* @param {Number} n (optional)
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function moveForward(transform, n) {
|
export function moveForward(transform, n) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { document, selection } = state
|
const { document, selection } = state
|
||||||
const sel = selection.moveForward(n).normalize(document)
|
const sel = selection.moveForward(n).normalize(document)
|
||||||
return transform.setSelectionOperation(sel)
|
transform.setSelectionOperation(sel)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -360,11 +338,10 @@ export function moveForward(transform, n) {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Object} properties
|
* @param {Object} properties
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function moveTo(transform, properties) {
|
export function moveTo(transform, properties) {
|
||||||
return transform.setSelectionOperation(properties)
|
transform.setSelectionOperation(properties)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -373,14 +350,13 @@ export function moveTo(transform, properties) {
|
|||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Number} anchor
|
* @param {Number} anchor
|
||||||
* @param {Number} focus (optional)
|
* @param {Number} focus (optional)
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function moveToOffsets(transform, anchor, fokus) {
|
export function moveToOffsets(transform, anchor, _focus) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { selection } = state
|
const { selection } = state
|
||||||
const sel = selection.moveToOffsets(anchor, fokus)
|
const sel = selection.moveToOffsets(anchor, _focus)
|
||||||
return transform.setSelectionOperation(sel)
|
transform.setSelectionOperation(sel)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -389,25 +365,73 @@ export function moveToOffsets(transform, anchor, fokus) {
|
|||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Node} start
|
* @param {Node} start
|
||||||
* @param {Node} end (optional)
|
* @param {Node} end (optional)
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function moveToRangeOf(transform, start, end) {
|
export function moveToRangeOf(transform, start, end) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { document, selection } = state
|
const { document, selection } = state
|
||||||
const sel = selection.moveToRangeOf(start, end).normalize(document)
|
const sel = selection.moveToRangeOf(start, end).normalize(document)
|
||||||
return transform.setSelectionOperation(sel)
|
transform.setSelectionOperation(sel)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Move the start offset by `n`.
|
||||||
|
*
|
||||||
|
* @param {Transform} transform
|
||||||
|
* @param {Number} n
|
||||||
|
*/
|
||||||
|
|
||||||
|
export function moveStartOffset(transform, n) {
|
||||||
|
const { state } = transform
|
||||||
|
const { document, selection } = state
|
||||||
|
const sel = selection.moveStartOffset(n).normalize(document)
|
||||||
|
transform.setSelectionOperation(sel)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Move the end offset by `n`.
|
||||||
|
*
|
||||||
|
* @param {Transform} transform
|
||||||
|
* @param {Number} n
|
||||||
|
*/
|
||||||
|
|
||||||
|
export function moveEndOffset(transform, n) {
|
||||||
|
const { state } = transform
|
||||||
|
const { document, selection } = state
|
||||||
|
const sel = selection.moveEndOffset(n).normalize(document)
|
||||||
|
transform.setSelectionOperation(sel)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unset the selection's marks.
|
||||||
|
*
|
||||||
|
* @param {Transform} transform
|
||||||
|
*/
|
||||||
|
|
||||||
|
export function unsetMarks(transform) {
|
||||||
|
transform.setSelectionOperation({ marks: null })
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Snapshot the current selection.
|
||||||
|
*
|
||||||
|
* @param {Transform} transform
|
||||||
|
*/
|
||||||
|
|
||||||
|
export function snapshotSelection(transform) {
|
||||||
|
const { state } = transform
|
||||||
|
const { selection } = state
|
||||||
|
transform.setSelectionOperation(selection, { snapshot: true })
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unset the selection, removing an association to a node.
|
* Unset the selection, removing an association to a node.
|
||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function unsetSelection(transform) {
|
export function unsetSelection(transform) {
|
||||||
return transform.setSelectionOperation({
|
transform.setSelectionOperation({
|
||||||
anchorKey: null,
|
anchorKey: null,
|
||||||
anchorOffset: 0,
|
anchorOffset: 0,
|
||||||
focusKey: null,
|
focusKey: null,
|
||||||
|
@@ -9,7 +9,6 @@ import Normalize from '../utils/normalize'
|
|||||||
* @param {Number} offset
|
* @param {Number} offset
|
||||||
* @param {Number} length
|
* @param {Number} length
|
||||||
* @param {Mixed} mark
|
* @param {Mixed} mark
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function addMarkOperation(transform, path, offset, length, mark) {
|
export function addMarkOperation(transform, path, offset, length, mark) {
|
||||||
@@ -30,7 +29,7 @@ export function addMarkOperation(transform, path, offset, length, mark) {
|
|||||||
inverse,
|
inverse,
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform.applyOperation(operation)
|
transform.applyOperation(operation)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -40,7 +39,6 @@ export function addMarkOperation(transform, path, offset, length, mark) {
|
|||||||
* @param {Array} path
|
* @param {Array} path
|
||||||
* @param {Number} index
|
* @param {Number} index
|
||||||
* @param {Node} node
|
* @param {Node} node
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function insertNodeOperation(transform, path, index, node) {
|
export function insertNodeOperation(transform, path, index, node) {
|
||||||
@@ -58,7 +56,7 @@ export function insertNodeOperation(transform, path, index, node) {
|
|||||||
inverse,
|
inverse,
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform.applyOperation(operation)
|
transform.applyOperation(operation)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -69,7 +67,6 @@ export function insertNodeOperation(transform, path, index, node) {
|
|||||||
* @param {Number} offset
|
* @param {Number} offset
|
||||||
* @param {String} text
|
* @param {String} text
|
||||||
* @param {Set<Mark>} marks (optional)
|
* @param {Set<Mark>} marks (optional)
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function insertTextOperation(transform, path, offset, text, marks) {
|
export function insertTextOperation(transform, path, offset, text, marks) {
|
||||||
@@ -90,7 +87,7 @@ export function insertTextOperation(transform, path, offset, text, marks) {
|
|||||||
inverse,
|
inverse,
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform.applyOperation(operation)
|
transform.applyOperation(operation)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -99,7 +96,6 @@ export function insertTextOperation(transform, path, offset, text, marks) {
|
|||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Array} path
|
* @param {Array} path
|
||||||
* @param {Array} withPath
|
* @param {Array} withPath
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function joinNodeOperation(transform, path, withPath) {
|
export function joinNodeOperation(transform, path, withPath) {
|
||||||
@@ -134,7 +130,7 @@ export function joinNodeOperation(transform, path, withPath) {
|
|||||||
inverse,
|
inverse,
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform.applyOperation(operation)
|
transform.applyOperation(operation)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -144,7 +140,6 @@ export function joinNodeOperation(transform, path, withPath) {
|
|||||||
* @param {Array} path
|
* @param {Array} path
|
||||||
* @param {Array} newPath
|
* @param {Array} newPath
|
||||||
* @param {Number} newIndex
|
* @param {Number} newIndex
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function moveNodeOperation(transform, path, newPath, newIndex) {
|
export function moveNodeOperation(transform, path, newPath, newIndex) {
|
||||||
@@ -167,7 +162,7 @@ export function moveNodeOperation(transform, path, newPath, newIndex) {
|
|||||||
inverse,
|
inverse,
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform.applyOperation(operation)
|
transform.applyOperation(operation)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -178,7 +173,6 @@ export function moveNodeOperation(transform, path, newPath, newIndex) {
|
|||||||
* @param {Number} offset
|
* @param {Number} offset
|
||||||
* @param {Number} length
|
* @param {Number} length
|
||||||
* @param {Mark} mark
|
* @param {Mark} mark
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function removeMarkOperation(transform, path, offset, length, mark) {
|
export function removeMarkOperation(transform, path, offset, length, mark) {
|
||||||
@@ -199,7 +193,7 @@ export function removeMarkOperation(transform, path, offset, length, mark) {
|
|||||||
inverse,
|
inverse,
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform.applyOperation(operation)
|
transform.applyOperation(operation)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -207,7 +201,6 @@ export function removeMarkOperation(transform, path, offset, length, mark) {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Array} path
|
* @param {Array} path
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function removeNodeOperation(transform, path) {
|
export function removeNodeOperation(transform, path) {
|
||||||
@@ -230,7 +223,7 @@ export function removeNodeOperation(transform, path) {
|
|||||||
inverse,
|
inverse,
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform.applyOperation(operation)
|
transform.applyOperation(operation)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -240,7 +233,6 @@ export function removeNodeOperation(transform, path) {
|
|||||||
* @param {Array} path
|
* @param {Array} path
|
||||||
* @param {Number} offset
|
* @param {Number} offset
|
||||||
* @param {Number} length
|
* @param {Number} length
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function removeTextOperation(transform, path, offset, length) {
|
export function removeTextOperation(transform, path, offset, length) {
|
||||||
@@ -282,7 +274,7 @@ export function removeTextOperation(transform, path, offset, length) {
|
|||||||
inverse,
|
inverse,
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform.applyOperation(operation)
|
transform.applyOperation(operation)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -294,7 +286,6 @@ export function removeTextOperation(transform, path, offset, length) {
|
|||||||
* @param {Number} length
|
* @param {Number} length
|
||||||
* @param {Mark} mark
|
* @param {Mark} mark
|
||||||
* @param {Mark} newMark
|
* @param {Mark} newMark
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function setMarkOperation(transform, path, offset, length, mark, newMark) {
|
export function setMarkOperation(transform, path, offset, length, mark, newMark) {
|
||||||
@@ -317,7 +308,7 @@ export function setMarkOperation(transform, path, offset, length, mark, newMark)
|
|||||||
inverse,
|
inverse,
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform.applyOperation(operation)
|
transform.applyOperation(operation)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -326,7 +317,6 @@ export function setMarkOperation(transform, path, offset, length, mark, newMark)
|
|||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Array} path
|
* @param {Array} path
|
||||||
* @param {Object} properties
|
* @param {Object} properties
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function setNodeOperation(transform, path, properties) {
|
export function setNodeOperation(transform, path, properties) {
|
||||||
@@ -352,7 +342,7 @@ export function setNodeOperation(transform, path, properties) {
|
|||||||
inverse,
|
inverse,
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform.applyOperation(operation)
|
transform.applyOperation(operation)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -360,10 +350,9 @@ export function setNodeOperation(transform, path, properties) {
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Mixed} selection
|
* @param {Mixed} selection
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function setSelectionOperation(transform, properties) {
|
export function setSelectionOperation(transform, properties, options = {}) {
|
||||||
properties = Normalize.selectionProperties(properties)
|
properties = Normalize.selectionProperties(properties)
|
||||||
|
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
@@ -375,7 +364,7 @@ export function setSelectionOperation(transform, properties) {
|
|||||||
// create a dictionary of the previous values for all of the properties that
|
// create a dictionary of the previous values for all of the properties that
|
||||||
// are being changed, for the inverse operation.
|
// are being changed, for the inverse operation.
|
||||||
for (const k in properties) {
|
for (const k in properties) {
|
||||||
if (properties[k] == selection[k]) continue
|
if (!options.snapshot && properties[k] == selection[k]) continue
|
||||||
props[k] = properties[k]
|
props[k] = properties[k]
|
||||||
prevProps[k] = selection[k]
|
prevProps[k] = selection[k]
|
||||||
}
|
}
|
||||||
@@ -432,7 +421,7 @@ export function setSelectionOperation(transform, properties) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Apply the operation.
|
// Apply the operation.
|
||||||
return transform.applyOperation(operation)
|
transform.applyOperation(operation)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -441,7 +430,6 @@ export function setSelectionOperation(transform, properties) {
|
|||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Array} path
|
* @param {Array} path
|
||||||
* @param {Number} offset
|
* @param {Number} offset
|
||||||
* @return {Transform}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function splitNodeOperation(transform, path, offset) {
|
export function splitNodeOperation(transform, path, offset) {
|
||||||
@@ -462,5 +450,5 @@ export function splitNodeOperation(transform, path, offset) {
|
|||||||
inverse,
|
inverse,
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform.applyOperation(operation)
|
transform.applyOperation(operation)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user