jitter when restoring as well

This commit is contained in:
Ryan Di 2023-12-06 23:39:34 +08:00
parent bf53d90c68
commit d6a6c40051

View File

@ -1,7 +1,7 @@
import { mutateElement } from "./element/mutateElement"; import { mutateElement } from "./element/mutateElement";
import { ExcalidrawElement } from "./element/types"; import { ExcalidrawElement } from "./element/types";
import { import {
generateKeyBetween, generateJitteredKeyBetween,
generateNJitteredKeysBetween, generateNJitteredKeysBetween,
} from "fractional-indexing-jittered"; } from "fractional-indexing-jittered";
@ -66,19 +66,6 @@ const getContiguousMovedIndices = (
return result; return result;
}; };
export const generateFractionalIndexBetween = (
predecessor: FractionalIndex,
successor: FractionalIndex,
) => {
if (predecessor && successor) {
if (predecessor < successor) {
return generateKeyBetween(predecessor, successor);
}
return null;
}
return generateKeyBetween(predecessor, successor);
};
export const fixFractionalIndices = ( export const fixFractionalIndices = (
elements: readonly ExcalidrawElement[], elements: readonly ExcalidrawElement[],
movedElementsMap: Map<string, ExcalidrawElement>, movedElementsMap: Map<string, ExcalidrawElement>,
@ -153,29 +140,31 @@ const restoreFractionalIndex = (
if (successor && !predecessor) { if (successor && !predecessor) {
// first element in the array // first element in the array
// insert before successor // insert before successor
return generateKeyBetween(null, successor); return generateJitteredKeyBetween(null, successor);
} }
if (predecessor && !successor) { if (predecessor && !successor) {
// last element in the array // last element in the array
// insert after predecessor // insert after predecessor
return generateKeyBetween(predecessor, null); return generateJitteredKeyBetween(predecessor, null);
} }
// both predecessor and successor exist // both predecessor and successor exist
// insert after predecessor // insert after predecessor
return generateKeyBetween(predecessor, null); return generateJitteredKeyBetween(predecessor, null);
} }
return generateKeyBetween(null, null); return generateJitteredKeyBetween(null, null);
}; };
/** /**
* normalize the fractional indicies of the elements in the given array such that * restore the fractional indicies of the elements in the given array such that
* every element in the array has a fractional index smaller than its successor's * every element in the array has a fractional index smaller than its successor's
* *
* note that this function is not pure, it mutates elements whose fractional indicies * neighboring indices might be updated as well
* need updating *
* only use this function when restoring or as a fallback to guarantee fractional
* indices consistency
*/ */
export const restoreFractionalIndicies = ( export const restoreFractionalIndicies = (
allElements: readonly ExcalidrawElement[], allElements: readonly ExcalidrawElement[],