1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-21 06:31:28 +02:00

make transforms mutable

This commit is contained in:
Ian Storm Taylor
2016-08-16 17:50:47 -07:00
parent c297972539
commit 18d567672c
2 changed files with 27 additions and 38 deletions

View File

@@ -374,13 +374,13 @@ class State extends new Record(DEFAULTS) {
document.filterDescendantsDeep((node) => {
if (failure = node.validate(schema)) {
const { value, rule } = failure
transform = rule.normalize(transform, node, value)
rule.normalize(transform, node, value)
}
})
if (failure = document.validate(schema)) {
const { value, rule } = failure
transform = rule.normalize(transform, document, value)
rule.normalize(transform, document, value)
}
return transform.apply({ snapshot: false })

View File

@@ -14,24 +14,6 @@ const Snapshot = new Record({
operations: new List()
})
/**
* Operation.
*/
const Operation = new Record({
type: null,
args: null
})
/**
* Defaults.
*/
const DEFAULT_PROPERTIES = {
state: null,
operations: new List()
}
/**
* Selection transforms.
*/
@@ -66,9 +48,23 @@ const SELECTION_TRANSFORMS = [
/**
* Transform.
*
* @type {Transform}
*/
class Transform extends new Record(DEFAULT_PROPERTIES) {
class Transform {
/**
* Constructor.
*
* @param {Object} properties
*/
constructor(properties) {
const { state } = properties
this.state = state
this.operations = []
}
/**
* Get the kind.
@@ -90,8 +86,7 @@ class Transform extends new Record(DEFAULT_PROPERTIES) {
*/
apply(options = {}) {
const transform = this
let { state, operations } = transform
let { state, operations } = this
let { cursorMarks, history, selection } = state
let { undos, redos } = history
@@ -102,7 +97,7 @@ class Transform extends new Record(DEFAULT_PROPERTIES) {
// If we should, save a snapshot into the history before transforming.
if (shouldSnapshot) {
const snapshot = transform.snapshot()
const snapshot = this.snapshot()
undos = undos.push(snapshot)
if (undos.size > 100) undos = undos.take(100)
redos = redos.clear()
@@ -133,8 +128,7 @@ class Transform extends new Record(DEFAULT_PROPERTIES) {
*/
shouldSnapshot() {
const transform = this
const { state, operations } = transform
const { state, operations } = this
const { cursorMarks, history, selection } = state
const { undos, redos } = history
const previous = undos.peek()
@@ -184,8 +178,7 @@ class Transform extends new Record(DEFAULT_PROPERTIES) {
*/
undo() {
const transform = this
let { state } = transform
let { state } = this
let { history } = state
let { undos, redos } = history
@@ -197,7 +190,7 @@ class Transform extends new Record(DEFAULT_PROPERTIES) {
undos = undos.pop()
// Snapshot the current state, and move it into the redos stack.
let snapshot = transform.snapshot()
let snapshot = this.snapshot()
redos = redos.push(snapshot)
// Return the previous state, with the updated history.
@@ -220,8 +213,7 @@ class Transform extends new Record(DEFAULT_PROPERTIES) {
*/
redo() {
const transform = this
let { state } = transform
let { state } = this
let { history } = state
let { undos, redos } = history
@@ -233,7 +225,7 @@ class Transform extends new Record(DEFAULT_PROPERTIES) {
redos = redos.pop()
// Snapshot the current state, and move it into the undos stack.
let snapshot = transform.snapshot()
let snapshot = this.snapshot()
undos = undos.push(snapshot)
// Return the next state, with the updated history.
@@ -257,12 +249,9 @@ class Transform extends new Record(DEFAULT_PROPERTIES) {
Object.keys(Transforms).forEach((type) => {
Transform.prototype[type] = function (...args) {
let transform = this
let { operations, state } = transform
operations = operations.push(new Operation({ type, args }))
state = Transforms[type](state, ...args)
transform = transform.merge({ operations, state })
return transform
this.operations.push({ type, args })
this.state = Transforms[type](this.state, ...args)
return this
}
})