forked from ianstormtaylor/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform.js
More file actions
172 lines (137 loc) · 3.92 KB
/
transform.js
File metadata and controls
172 lines (137 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import Debug from 'debug'
import Transforms from '../transforms'
/**
* Debug.
*
* @type {Function}
*/
const debug = Debug('slate:transform')
/**
* Transform.
*
* @type {Transform}
*/
class Transform {
/**
* Constructor.
*
* @param {Object} properties
* @property {State} state
*/
constructor(properties) {
const { state } = properties
this.state = state
this.operations = []
}
/**
* Get the kind.
*
* @return {String}
*/
get kind() {
return 'transform'
}
/**
* Apply the transform and return the new state.
*
* @param {Object} options
* @property {Boolean} isNative
* @property {Boolean} merge
* @property {Boolean} save
* @return {State}
*/
apply(options = {}) {
const transform = this
let { merge, save, isNative = false } = options
// Ensure that the selection is normalized.
transform.normalizeSelection()
const { state, operations } = transform
const { history } = state
const { undos } = history
const previous = undos.peek()
// If there are no operations, abort early.
if (!operations.length) return state
// If there's a previous save point, determine if the new operations should
// be merged into the previous ones.
if (previous && merge == null) {
merge = (
isOnlySelections(operations) ||
isContiguousInserts(operations, previous) ||
isContiguousRemoves(operations, previous)
)
}
// If the save flag isn't set, determine whether we should save.
if (save == null) {
save = !isOnlySelections(operations)
}
// Save the new operations.
if (save) this.save({ merge })
// Return the new state with the `isNative` flag set.
return this.state.set('isNative', !!isNative)
}
}
/**
* Add a transform method for each of the transforms.
*/
Object.keys(Transforms).forEach((type) => {
Transform.prototype[type] = function (...args) {
debug(type, { args })
Transforms[type](this, ...args)
return this
}
})
/**
* Check whether a list of `operations` only contains selection operations.
*
* @param {Array} operations
* @return {Boolean}
*/
function isOnlySelections(operations) {
return operations.every(op => op.type == 'set_selection')
}
/**
* Check whether a list of `operations` and a list of `previous` operations are
* contiguous text insertions.
*
* @param {Array} operations
* @param {Array} previous
*/
function isContiguousInserts(operations, previous) {
const edits = operations.filter(op => op.type != 'set_selection')
const prevEdits = previous.filter(op => op.type != 'set_selection')
if (!edits.length || !prevEdits.length) return false
const onlyInserts = edits.every(op => op.type == 'insert_text')
const prevOnlyInserts = prevEdits.every(op => op.type == 'insert_text')
if (!onlyInserts || !prevOnlyInserts) return false
const first = edits[0]
const last = prevEdits[prevEdits.length - 1]
if (first.key != last.key) return false
if (first.offset != last.offset + last.text.length) return false
return true
}
/**
* Check whether a list of `operations` and a list of `previous` operations are
* contiguous text removals.
*
* @param {Array} operations
* @param {Array} previous
*/
function isContiguousRemoves(operations, previous) {
const edits = operations.filter(op => op.type != 'set_selection')
const prevEdits = previous.filter(op => op.type != 'set_selection')
if (!edits.length || !prevEdits.length) return false
const onlyRemoves = edits.every(op => op.type == 'remove_text')
const prevOnlyRemoves = prevEdits.every(op => op.type == 'remove_text')
if (!onlyRemoves || !prevOnlyRemoves) return false
const first = edits[0]
const last = prevEdits[prevEdits.length - 1]
if (first.key != last.key) return false
if (first.offset + first.length != last.offset) return false
return true
}
/**
* Export.
*
* @type {Transform}
*/
export default Transform