File size: 9,653 Bytes
1e92f2d |
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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 |
import {
Editor,
Node,
Operation,
Path,
Point,
Range,
Text,
Element,
} from 'slate'
import { EDITOR_TO_PENDING_DIFFS } from './weak-maps'
export type StringDiff = {
start: number
end: number
text: string
}
export type TextDiff = {
id: number
path: Path
diff: StringDiff
}
/**
* Check whether a text diff was applied in a way we can perform the pending action on /
* recover the pending selection.
*/
export function verifyDiffState(editor: Editor, textDiff: TextDiff): boolean {
const { path, diff } = textDiff
if (!Editor.hasPath(editor, path)) {
return false
}
const node = Node.get(editor, path)
if (!Text.isText(node)) {
return false
}
if (diff.start !== node.text.length || diff.text.length === 0) {
return (
node.text.slice(diff.start, diff.start + diff.text.length) === diff.text
)
}
const nextPath = Path.next(path)
if (!Editor.hasPath(editor, nextPath)) {
return false
}
const nextNode = Node.get(editor, nextPath)
return Text.isText(nextNode) && nextNode.text.startsWith(diff.text)
}
export function applyStringDiff(text: string, ...diffs: StringDiff[]) {
return diffs.reduce(
(text, diff) =>
text.slice(0, diff.start) + diff.text + text.slice(diff.end),
text
)
}
function longestCommonPrefixLength(str: string, another: string) {
const length = Math.min(str.length, another.length)
for (let i = 0; i < length; i++) {
if (str.charAt(i) !== another.charAt(i)) {
return i
}
}
return length
}
function longestCommonSuffixLength(
str: string,
another: string,
max: number
): number {
const length = Math.min(str.length, another.length, max)
for (let i = 0; i < length; i++) {
if (
str.charAt(str.length - i - 1) !== another.charAt(another.length - i - 1)
) {
return i
}
}
return length
}
/**
* Remove redundant changes from the diff so that it spans the minimal possible range
*/
export function normalizeStringDiff(targetText: string, diff: StringDiff) {
const { start, end, text } = diff
const removedText = targetText.slice(start, end)
const prefixLength = longestCommonPrefixLength(removedText, text)
const max = Math.min(
removedText.length - prefixLength,
text.length - prefixLength
)
const suffixLength = longestCommonSuffixLength(removedText, text, max)
const normalized: StringDiff = {
start: start + prefixLength,
end: end - suffixLength,
text: text.slice(prefixLength, text.length - suffixLength),
}
if (normalized.start === normalized.end && normalized.text.length === 0) {
return null
}
return normalized
}
/**
* Return a string diff that is equivalent to applying b after a spanning the range of
* both changes
*/
export function mergeStringDiffs(
targetText: string,
a: StringDiff,
b: StringDiff
): StringDiff | null {
const start = Math.min(a.start, b.start)
const overlap = Math.max(
0,
Math.min(a.start + a.text.length, b.end) - b.start
)
const applied = applyStringDiff(targetText, a, b)
const sliceEnd = Math.max(
b.start + b.text.length,
a.start +
a.text.length +
(a.start + a.text.length > b.start ? b.text.length : 0) -
overlap
)
const text = applied.slice(start, sliceEnd)
const end = Math.max(a.end, b.end - a.text.length + (a.end - a.start))
return normalizeStringDiff(targetText, { start, end, text })
}
/**
* Get the slate range the text diff spans.
*/
export function targetRange(textDiff: TextDiff): Range {
const { path, diff } = textDiff
return {
anchor: { path, offset: diff.start },
focus: { path, offset: diff.end },
}
}
/**
* Normalize a 'pending point' a.k.a a point based on the dom state before applying
* the pending diffs. Since the pending diffs might have been inserted with different
* marks we have to 'walk' the offset from the starting position to ensure we still
* have a valid point inside the document
*/
export function normalizePoint(editor: Editor, point: Point): Point | null {
let { path, offset } = point
if (!Editor.hasPath(editor, path)) {
return null
}
let leaf = Node.get(editor, path)
if (!Text.isText(leaf)) {
return null
}
const parentBlock = Editor.above(editor, {
match: n => Element.isElement(n) && Editor.isBlock(editor, n),
at: path,
})
if (!parentBlock) {
return null
}
while (offset > leaf.text.length) {
const entry = Editor.next(editor, { at: path, match: Text.isText })
if (!entry || !Path.isDescendant(entry[1], parentBlock[1])) {
return null
}
offset -= leaf.text.length
leaf = entry[0]
path = entry[1]
}
return { path, offset }
}
/**
* Normalize a 'pending selection' to ensure it's valid in the current document state.
*/
export function normalizeRange(editor: Editor, range: Range): Range | null {
const anchor = normalizePoint(editor, range.anchor)
if (!anchor) {
return null
}
if (Range.isCollapsed(range)) {
return { anchor, focus: anchor }
}
const focus = normalizePoint(editor, range.focus)
if (!focus) {
return null
}
return { anchor, focus }
}
export function transformPendingPoint(
editor: Editor,
point: Point,
op: Operation
): Point | null {
const pendingDiffs = EDITOR_TO_PENDING_DIFFS.get(editor)
const textDiff = pendingDiffs?.find(({ path }) =>
Path.equals(path, point.path)
)
if (!textDiff || point.offset <= textDiff.diff.start) {
return Point.transform(point, op, { affinity: 'backward' })
}
const { diff } = textDiff
// Point references location inside the diff => transform the point based on the location
// the diff will be applied to and add the offset inside the diff.
if (point.offset <= diff.start + diff.text.length) {
const anchor = { path: point.path, offset: diff.start }
const transformed = Point.transform(anchor, op, {
affinity: 'backward',
})
if (!transformed) {
return null
}
return {
path: transformed.path,
offset: transformed.offset + point.offset - diff.start,
}
}
// Point references location after the diff
const anchor = {
path: point.path,
offset: point.offset - diff.text.length + diff.end - diff.start,
}
const transformed = Point.transform(anchor, op, {
affinity: 'backward',
})
if (!transformed) {
return null
}
if (
op.type === 'split_node' &&
Path.equals(op.path, point.path) &&
anchor.offset < op.position &&
diff.start < op.position
) {
return transformed
}
return {
path: transformed.path,
offset: transformed.offset + diff.text.length - diff.end + diff.start,
}
}
export function transformPendingRange(
editor: Editor,
range: Range,
op: Operation
): Range | null {
const anchor = transformPendingPoint(editor, range.anchor, op)
if (!anchor) {
return null
}
if (Range.isCollapsed(range)) {
return { anchor, focus: anchor }
}
const focus = transformPendingPoint(editor, range.focus, op)
if (!focus) {
return null
}
return { anchor, focus }
}
export function transformTextDiff(
textDiff: TextDiff,
op: Operation
): TextDiff | null {
const { path, diff, id } = textDiff
switch (op.type) {
case 'insert_text': {
if (!Path.equals(op.path, path) || op.offset >= diff.end) {
return textDiff
}
if (op.offset <= diff.start) {
return {
diff: {
start: op.text.length + diff.start,
end: op.text.length + diff.end,
text: diff.text,
},
id,
path,
}
}
return {
diff: {
start: diff.start,
end: diff.end + op.text.length,
text: diff.text,
},
id,
path,
}
}
case 'remove_text': {
if (!Path.equals(op.path, path) || op.offset >= diff.end) {
return textDiff
}
if (op.offset + op.text.length <= diff.start) {
return {
diff: {
start: diff.start - op.text.length,
end: diff.end - op.text.length,
text: diff.text,
},
id,
path,
}
}
return {
diff: {
start: diff.start,
end: diff.end - op.text.length,
text: diff.text,
},
id,
path,
}
}
case 'split_node': {
if (!Path.equals(op.path, path) || op.position >= diff.end) {
return {
diff,
id,
path: Path.transform(path, op, { affinity: 'backward' })!,
}
}
if (op.position > diff.start) {
return {
diff: {
start: diff.start,
end: Math.min(op.position, diff.end),
text: diff.text,
},
id,
path,
}
}
return {
diff: {
start: diff.start - op.position,
end: diff.end - op.position,
text: diff.text,
},
id,
path: Path.transform(path, op, { affinity: 'forward' })!,
}
}
case 'merge_node': {
if (!Path.equals(op.path, path)) {
return {
diff,
id,
path: Path.transform(path, op)!,
}
}
return {
diff: {
start: diff.start + op.position,
end: diff.end + op.position,
text: diff.text,
},
id,
path: Path.transform(path, op)!,
}
}
}
const newPath = Path.transform(path, op)
if (!newPath) {
return null
}
return {
diff,
path: newPath,
id,
}
}
|