From de322564666db0b460fef20ccce0b01db27d54ac Mon Sep 17 00:00:00 2001 From: Ryan Di Date: Wed, 29 Nov 2023 18:13:51 +0800 Subject: [PATCH] simplify --- src/zindex.ts | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/zindex.ts b/src/zindex.ts index a6a785aa2..214dba47d 100644 --- a/src/zindex.ts +++ b/src/zindex.ts @@ -519,13 +519,13 @@ const randomNumInBetween = (start: number, end: number) => { return Math.random() * (end - start) + start; }; -export const generateFractionalIndex = ({ - start = FRACTIONAL_INDEX_FLOOR, - end = FRACTIONAL_INDEX_CEILING, -}: { - start?: number; - end?: number; -}) => { +export const generateFractionalIndex = ( + predecessorElement: ExcalidrawElement | undefined, + successorElement: ExcalidrawElement | undefined, +) => { + const start = getFractionalIndex(predecessorElement, FRACTIONAL_INDEX_FLOOR); + const end = getFractionalIndex(successorElement, FRACTIONAL_INDEX_CEILING); + const nextTemp = randomNumInBetween(start, end); return ( (randomNumInBetween(nextTemp, end) + randomNumInBetween(start, nextTemp)) / @@ -533,12 +533,24 @@ export const generateFractionalIndex = ({ ); }; +/** + * + */ +export const getNextFractionalIndexAt = ( + index: number, + allElements: ExcalidrawElement[], +) => { + const predecessor = allElements[index - 1]; + const successor = allElements[index + 1]; + + return generateFractionalIndex(predecessor, successor); +}; + /** * normalize the fractional indicies of the elements in the given array such that * a. all elements have a fraction index between floor and ceiling as defined above * b. for every element, its fractional index is greater than its predecessor's and smaller than its successor's */ - export const normalizeFractionalIndexing = ( allElements: readonly ExcalidrawElement[], ) => { @@ -558,10 +570,10 @@ export const normalizeFractionalIndexing = ( successorElement, ) ) { - const nextFractionalIndex = generateFractionalIndex({ - start: getFractionalIndex(predecessorElement, FRACTIONAL_INDEX_FLOOR), - end: getFractionalIndex(successorElement, FRACTIONAL_INDEX_CEILING), - }); + const nextFractionalIndex = generateFractionalIndex( + predecessorElement, + successorElement, + ); normalizedElements.push({ ...element,