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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | 1× 1× 546× 546× 546× 546× 546× 546× 546× 383× 373× 224× 224× 224× 149× 149× 149× 711× 251× 38× 163× 163× 383× 332× 1× 1× 1× 1× 1× 1× 1× 1× 1× 129× 1× 87× 1× 1× 1× 167× | import isString from '../../util/isString' import isNumber from '../../util/isNumber' import Conflict from './Conflict' const INSERT = "insert" const DELETE = "delete" class TextOperation { constructor(data) { Iif (!data || data.type === undefined || data.pos === undefined || data.str === undefined) { throw new Error("Illegal argument: insufficient data.") } // 'insert' or 'delete' this.type = data.type // the position where to apply the operation this.pos = data.pos // the string to delete or insert this.str = data.str // sanity checks Iif(!this.isInsert() && !this.isDelete()) { throw new Error("Illegal type.") } Iif (!isString(this.str)) { throw new Error("Illegal argument: expecting string.") } Iif (!isNumber(this.pos) || this.pos < 0) { throw new Error("Illegal argument: expecting positive number as pos.") } } apply(str) { if (this.isEmpty()) return str if (this.type === INSERT) { Iif (str.length < this.pos) { throw new Error("Provided string is too short.") } Iif (str.splice) { return str.splice(this.pos, 0, this.str) } else { return str.slice(0, this.pos).concat(this.str).concat(str.slice(this.pos)) } } else /* if (this.type === DELETE) */ { Iif (str.length < this.pos + this.str.length) { throw new Error("Provided string is too short.") } Iif (str.splice) { return str.splice(this.pos, this.str.length) } else { return str.slice(0, this.pos).concat(str.slice(this.pos + this.str.length)) } } } clone() { return new TextOperation(this) } isNOP() { return this.type === "NOP" || this.str.length === 0 } isInsert() { return this.type === INSERT } isDelete() { return this.type === DELETE } getLength() { return this.str.length } invert() { var data = { type: this.isInsert() ? DELETE : INSERT, pos: this.pos, str: this.str } return new TextOperation(data) } hasConflict(other) { return hasConflict(this, other) } isEmpty() { return this.str.length === 0 } toJSON() { return { type: this.type, pos: this.pos, str: this.str } } toString() { return ["(", (this.isInsert() ? INSERT : DELETE), ",", this.pos, ",'", this.str, "')"].join('') } } TextOperation.prototype._isOperation = true TextOperation.prototype._isTextOperation = true function hasConflict(a, b) { // Insert vs Insert: // // Insertions are conflicting iff their insert position is the same. if (a.type === INSERT && b.type === INSERT) return (a.pos === b.pos) // Delete vs Delete: // // Deletions are conflicting if their ranges overlap. if (a.type === DELETE && b.type === DELETE) { // to have no conflict, either `a` should be after `b` or `b` after `a`, otherwise. return !(a.pos >= b.pos + b.str.length || b.pos >= a.pos + a.str.length) } // Delete vs Insert: // // A deletion and an insertion are conflicting if the insert position is within the deleted range. var del, ins if (a.type === DELETE) { del = a; ins = b } else { del = b; ins = a } return (ins.pos >= del.pos && ins.pos < del.pos + del.str.length) } // Transforms two Insertions // -------- function transform_insert_insert(a, b) { if (a.pos === b.pos) { b.pos += a.str.length } else if (a.pos < b.pos) { b.pos += a.str.length } else { a.pos += b.str.length } } // Transform two Deletions // -------- // function transform_delete_delete(a, b, first) { // reduce to a normalized case if (a.pos > b.pos) { return transform_delete_delete(b, a, !first) } if (a.pos === b.pos && a.str.length > b.str.length) { return transform_delete_delete(b, a, !first) } // take out overlapping parts if (b.pos < a.pos + a.str.length) { var s = b.pos - a.pos var s1 = a.str.length - s var s2 = s + b.str.length a.str = a.str.slice(0, s) + a.str.slice(s2) b.str = b.str.slice(s1) b.pos -= s } else { b.pos -= a.str.length } } // Transform Insert and Deletion // -------- // function transform_insert_delete(a, b) { if (a.type === DELETE) { return transform_insert_delete(b, a) } // we can assume, that a is an insertion and b is a deletion // a is before b if (a.pos <= b.pos) { b.pos += a.str.length } // a is after b else if (a.pos >= b.pos + b.str.length) { a.pos -= b.str.length } // Note: this is a conflict case the user should be noticed about // If applied still, the deletion takes precedence // a.pos > b.pos && <= b.pos + b.length else { var s = a.pos - b.pos b.str = b.str.slice(0, s) + a.str + b.str.slice(s) a.str = "" } } function transform(a, b, options) { options = options || {} if (options["no-conflict"] && hasConflict(a, b)) { throw new Conflict(a, b) } if (!options.inplace) { a = a.clone() b = b.clone() } if (a.type === INSERT && b.type === INSERT) { transform_insert_insert(a, b) } else if (a.type === DELETE && b.type === DELETE) { transform_delete_delete(a, b, true) } else { transform_insert_delete(a,b) } return [a, b] } TextOperation.transform = function() { return transform.apply(null, arguments) } /* Factories */ TextOperation.Insert = function(pos, str) { return new TextOperation({ type: INSERT, pos: pos, str: str }) } TextOperation.Delete = function(pos, str) { return new TextOperation({ type: DELETE, pos: pos, str: str }) } TextOperation.INSERT = INSERT TextOperation.DELETE = DELETE TextOperation.fromJSON = function(data) { return new TextOperation(data) } export default TextOperation |