1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-22 06:53:25 +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) => { document.filterDescendantsDeep((node) => {
if (failure = node.validate(schema)) { if (failure = node.validate(schema)) {
const { value, rule } = failure const { value, rule } = failure
transform = rule.normalize(transform, node, value) rule.normalize(transform, node, value)
} }
}) })
if (failure = document.validate(schema)) { if (failure = document.validate(schema)) {
const { value, rule } = failure const { value, rule } = failure
transform = rule.normalize(transform, document, value) rule.normalize(transform, document, value)
} }
return transform.apply({ snapshot: false }) return transform.apply({ snapshot: false })

View File

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