Skip to content

Commit f387ddf

Browse files
author
Zaydek Michels-Gualtieri
committed
OK this may be working. Turns out comparing prevElement against nextElement works so long as we blur the values of strings.
1 parent 76d3aac commit f387ddf

File tree

1 file changed

+52
-23
lines changed

1 file changed

+52
-23
lines changed

src/Editor/useEditor.js

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -361,39 +361,68 @@ const methods = state => ({
361361
console.log("parseElements", Date.now() - t)
362362

363363
if (renderOpts.rerenderIfNeeded) {
364-
365364
// Compares whether two children are equal. Character
366365
// data is not compared in order to preserve
367366
// spellcheck highlighting.
368-
//
369-
// Code based on toReact.
367+
368+
// const isUndefinedOrNull = value => {
369+
// return value === undefined || value === null
370+
// }
371+
372+
const isString = value => {
373+
return typeof value === "string"
374+
}
375+
376+
const isObject = value => {
377+
const ok = (
378+
typeof value === "object" &&
379+
!Array.isArray(value)
380+
)
381+
return ok
382+
}
383+
384+
const isArray = value => {
385+
const ok = (
386+
typeof value === "object" &&
387+
Array.isArray(value)
388+
)
389+
return ok
390+
}
391+
392+
// const areEqualChildrenObjects = (objectA, objectB) => {
393+
// const ok = (
394+
// objectA.type === objectB.type &&
395+
// JSON.stringify(objectA.syntax) === JSON.stringify(objectB.syntax) &&
396+
// areEqualChildren(objectA.children, objectB.children)
397+
// )
398+
// return ok
399+
// }
400+
370401
const areEqualChildren = (childrenA, childrenB) => {
371-
if (typeof childrenA === "string" && childrenB === "string") {
372-
return true
373-
}
374-
// Non-objects:
375-
if (typeof childrenA !== "object" || typeof childrenB !== "object") {
402+
if (childrenA === null || childrenB === null) {
376403
return childrenA === childrenB
377404
}
378-
// Objects (non-arrays):
379-
if (
380-
(typeof chidrenA === "object" && !Array.isArray(childrenA)) ||
381-
(typeof chidrenB === "object" && !Array.isArray(childrenB))
382-
) {
405+
406+
if (isString(childrenA) && isString(childrenB)) {
407+
return true
408+
} else if (isObject(childrenA) && isObject(childrenB)) {
383409
const ok = (
384410
childrenA.type === childrenB.type &&
385-
childrenA.id === childrenB.id &&
386411
JSON.stringify(childrenA.syntax) === JSON.stringify(childrenB.syntax) &&
387412
areEqualChildren(childrenA.children, childrenB.children)
388413
)
389414
return ok
390415
}
391-
const ok = (
392-
(childrenA.length && childrenB.length) &&
393-
childrenA.length === childrenB.length &&
394-
childrenA.every((_, x) => areEqualChildren(childrenA[x], childrenB[x]))
395-
)
396-
return ok
416+
417+
if (!isArray(childrenA) || !isArray(childrenB) || childrenA.length !== childrenB.length) {
418+
return false
419+
}
420+
for (let x = 0; x < childrenA.length; x++) {
421+
if (!areEqualChildren(childrenA[x], childrenB[x])) {
422+
return false
423+
}
424+
}
425+
return true
397426
}
398427

399428
// Compares whether two elements are equal. Elements
@@ -408,7 +437,6 @@ const methods = state => ({
408437
)
409438
return ok
410439
}
411-
412440
let id = ""
413441
const selection = document.getSelection()
414442
if (selection.rangeCount) {
@@ -420,13 +448,14 @@ const methods = state => ({
420448
// if (nextElement) {
421449
// nextElement.reactKey = uuidv4().slice(0, 8)
422450
// }
423-
const unsafe = (
451+
const mustRerender = (
424452
state.pos1.x - 1 >= 0 &&
425453
ascii.isPunctuation(state.nodes[state.pos1.y].data[state.pos1.x - 1])
426454
)
427455
const prevElement = state.elements.find(each => each.id === id)
428456
const nextElement = nextElements.find(each => each.id === id)
429-
if (prevElement && nextElement && (!areEqualElements(prevElement, nextElement) || unsafe)) {
457+
if (prevElement && nextElement && (mustRerender || !areEqualElements(prevElement, nextElement))) {
458+
console.log("forced a rerender key")
430459
nextElement.reactKey = uuidv4().slice(0, 8)
431460
}
432461
}

0 commit comments

Comments
 (0)