mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-20 22:21:20 +02:00
refactor transform tests, add selection transform tests
This commit is contained in:
@@ -262,9 +262,20 @@ export function setMarkByKey(transform, key, offset, length, mark, properties) {
|
||||
const { state } = transform
|
||||
const { document } = state
|
||||
const path = document.getPath(key)
|
||||
const prevProps = {}
|
||||
|
||||
// TODO!
|
||||
const inverse = {}
|
||||
for (const k in properties) {
|
||||
prevProps[k] = mark[k]
|
||||
}
|
||||
|
||||
const inverse = {
|
||||
type: 'set_mark',
|
||||
path,
|
||||
offset,
|
||||
length,
|
||||
mark,
|
||||
properties: prevProps,
|
||||
}
|
||||
|
||||
const operation = {
|
||||
type: 'set_mark',
|
||||
@@ -273,6 +284,7 @@ export function setMarkByKey(transform, key, offset, length, mark, properties) {
|
||||
length,
|
||||
mark,
|
||||
properties,
|
||||
inverse,
|
||||
}
|
||||
|
||||
return transform.operate(operation)
|
||||
|
@@ -394,18 +394,24 @@ export function moveToRangeOf(transform, start, end) {
|
||||
* @return {Transform}
|
||||
*/
|
||||
|
||||
export function setSelection(transform, selection) {
|
||||
selection = Normalize.selection(selection)
|
||||
export function setSelection(transform, properties) {
|
||||
properties = Normalize.selectionProperties(properties)
|
||||
const { state } = transform
|
||||
const { selection } = state
|
||||
const prevProps = {}
|
||||
|
||||
for (const k in properties) {
|
||||
prevProps[k] = selection[k]
|
||||
}
|
||||
|
||||
const inverse = {
|
||||
type: 'set_selection',
|
||||
selection: state.selection,
|
||||
properties: prevProps
|
||||
}
|
||||
|
||||
const operation = {
|
||||
type: 'set_selection',
|
||||
selection,
|
||||
properties,
|
||||
inverse,
|
||||
}
|
||||
|
||||
|
@@ -2,7 +2,7 @@
|
||||
import uid from '../utils/uid'
|
||||
|
||||
/**
|
||||
* Add mark to text at `offset` and `length` in node by `key`.
|
||||
* Add mark to text at `offset` and `length` in node by `path`.
|
||||
*
|
||||
* @param {State} state
|
||||
* @param {Object} operation
|
||||
@@ -20,7 +20,7 @@ function addMark(state, operation) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a `node` at `index` in a node by `key`.
|
||||
* Insert a `node` at `index` in a node by `path`.
|
||||
*
|
||||
* @param {State} state
|
||||
* @param {Object} operation
|
||||
@@ -40,7 +40,7 @@ function insertNode(state, operation) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert `text` at `offset` in node by `key`.
|
||||
* Insert `text` at `offset` in node by `path`.
|
||||
*
|
||||
* @param {State} state
|
||||
* @param {Object} operation
|
||||
@@ -58,7 +58,7 @@ function insertText(state, operation) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Move a node by `key` to a new parent by `key` and `index`.
|
||||
* Move a node by `path` to a new parent by `path` and `index`.
|
||||
*
|
||||
* @param {State} state
|
||||
* @param {Object} operation
|
||||
@@ -86,7 +86,7 @@ function moveNode(state, operation) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove mark from text at `offset` and `length` in node by `key`.
|
||||
* Remove mark from text at `offset` and `length` in node by `path`.
|
||||
*
|
||||
* @param {State} state
|
||||
* @param {Object} operation
|
||||
@@ -104,7 +104,7 @@ function removeMark(state, operation) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a node by `key`.
|
||||
* Remove a node by `path`.
|
||||
*
|
||||
* @param {State} state
|
||||
* @param {Object} operation
|
||||
@@ -126,7 +126,7 @@ function removeNode(state, operation) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove text at `offset` and `length` in node by `key`.
|
||||
* Remove text at `offset` and `length` in node by `path`.
|
||||
*
|
||||
* @param {State} state
|
||||
* @param {Object} operation
|
||||
@@ -145,7 +145,7 @@ function removeText(state, operation) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set `properties` on mark on text at `offset` and `length` in node by `key`.
|
||||
* Set `properties` on mark on text at `offset` and `length` in node by `path`.
|
||||
*
|
||||
* @param {State} state
|
||||
* @param {Object} operation
|
||||
@@ -163,7 +163,7 @@ function setMark(state, operation) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set `properties` on a node by `key`.
|
||||
* Set `properties` on a node by `path`.
|
||||
*
|
||||
* @param {State} state
|
||||
* @param {Object} operation
|
||||
@@ -182,7 +182,7 @@ function setNode(state, operation) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the selection to a new `selection`.
|
||||
* Set `properties` on the selection.
|
||||
*
|
||||
* @param {State} state
|
||||
* @param {Object} operation
|
||||
@@ -190,14 +190,15 @@ function setNode(state, operation) {
|
||||
*/
|
||||
|
||||
function setSelection(state, operation) {
|
||||
let { selection } = operation
|
||||
selection = state.selection.merge(selection)
|
||||
let { properties } = operation
|
||||
let { selection } = state
|
||||
selection = selection.merge(properties)
|
||||
state = state.merge({ selection })
|
||||
return state
|
||||
}
|
||||
|
||||
/**
|
||||
* Split a node by `key` at `offset`.
|
||||
* Split a node by `path` at `offset`.
|
||||
*
|
||||
* @param {State} state
|
||||
* @param {Object} operation
|
||||
|
@@ -92,7 +92,7 @@ function mark(value) {
|
||||
/**
|
||||
* Normalize a mark properties argument `value`.
|
||||
*
|
||||
* @param {String || Object} value
|
||||
* @param {String || Object || Mark} value
|
||||
* @return {Object}
|
||||
*/
|
||||
|
||||
@@ -115,7 +115,7 @@ function markProperties(value = {}) {
|
||||
break
|
||||
}
|
||||
default: {
|
||||
throw new Error(`Invalid mark \`properties\` argument! It must be an object or a string. You passed: "${value}".`)
|
||||
throw new Error(`Invalid mark \`properties\` argument! It must be an object, a string or a mark. You passed: "${value}".`)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,7 +125,7 @@ function markProperties(value = {}) {
|
||||
/**
|
||||
* Normalize a node properties argument `value`.
|
||||
*
|
||||
* @param {String || Object} value
|
||||
* @param {String || Object || Node} value
|
||||
* @return {Object}
|
||||
*/
|
||||
|
||||
@@ -149,7 +149,7 @@ function nodeProperties(value = {}) {
|
||||
break
|
||||
}
|
||||
default: {
|
||||
throw new Error(`Invalid node \`properties\` argument! It must be an object or a string. You passed: "${value}".`)
|
||||
throw new Error(`Invalid node \`properties\` argument! It must be an object, a string or a node. You passed: "${value}".`)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,6 +176,34 @@ function selection(value) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize a selection properties argument `value`.
|
||||
*
|
||||
* @param {Object || Selection} value
|
||||
* @return {Object}
|
||||
*/
|
||||
|
||||
function selectionProperties(value = {}) {
|
||||
const ret = {}
|
||||
|
||||
switch (typeOf(value)) {
|
||||
case 'object': {
|
||||
if (value.isFocused != null) ret.isFocused = !!value.isFocused
|
||||
if (value.isBackward != null) ret.isBackward = !!value.isBackward
|
||||
if (value.anchorKey != null) ret.anchorKey = value.anchorKey
|
||||
if (value.anchorOffset != null) ret.anchorOffset = value.anchorOffset
|
||||
if (value.focusKey != null) ret.focusKey = value.focusKey
|
||||
if (value.focusOffset != null) ret.focusOffset = value.focusOffset
|
||||
break
|
||||
}
|
||||
default: {
|
||||
throw new Error(`Invalid selection \`properties\` argument! It must be an object or a selection. You passed: "${value}".`)
|
||||
}
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
/**
|
||||
* Export.
|
||||
*
|
||||
@@ -190,5 +218,6 @@ export default {
|
||||
markProperties,
|
||||
nodeProperties,
|
||||
selection,
|
||||
selectionProperties,
|
||||
}
|
||||
|
||||
|
@@ -1,5 +1,5 @@
|
||||
|
||||
import { Mark } from '../../../../..'
|
||||
import { Mark } from '../../../../../..'
|
||||
import assert from 'assert'
|
||||
|
||||
export default function (state) {
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user