Skip to content

Commit 8ac1ce1

Browse files
authored
ENG-683 Add proper index to share via (#345)
* Add proper index to share via * Refactor index comparison in ExportDialog to use localeCompare for better string handling
1 parent 278324c commit 8ac1ce1

1 file changed

Lines changed: 27 additions & 2 deletions

File tree

apps/roam/src/components/Export.tsx

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ import apiPut from "roamjs-components/util/apiPut";
4444
import { ExportGithub } from "./ExportGithub";
4545
import isLiveBlock from "roamjs-components/queries/isLiveBlock";
4646
import createPage from "roamjs-components/writes/createPage";
47-
import { createShapeId, IndexKey, TLParentId } from "tldraw";
47+
import {
48+
createShapeId,
49+
IndexKey,
50+
TLParentId,
51+
getIndexAbove,
52+
TLShape,
53+
} from "tldraw";
4854
import calcCanvasNodeSizeAndImg from "~/utils/calcCanvasNodeSizeAndImg";
4955
import { DiscourseNodeShape } from "~/components/canvas/DiscourseNodeUtil";
5056
import { MAX_WIDTH } from "~/components/canvas/Tldraw";
@@ -271,6 +277,23 @@ const ExportDialog: ExportDialogComponent = ({
271277
};
272278
const shapeBounds = extractShapesBounds(store);
273279

280+
// Get existing shapes to determine the highest index
281+
const existingShapes = Object.values(store).filter(
282+
(shape) => (shape as TLShape).typeName === "shape",
283+
);
284+
285+
// Find the highest index among existing shapes
286+
let currentIndex: IndexKey = "a1" as IndexKey;
287+
if (existingShapes.length > 0) {
288+
const highestIndex = existingShapes.reduce((highest: IndexKey, shape) => {
289+
const shapeWithIndex = shape as TLShape;
290+
return shapeWithIndex.index.localeCompare(highest) > 0
291+
? shapeWithIndex.index
292+
: highest;
293+
}, "a1" as IndexKey);
294+
currentIndex = highestIndex;
295+
}
296+
274297
type CommonBounds = {
275298
top: number;
276299
right: number;
@@ -315,8 +338,10 @@ const ExportDialog: ExportDialogComponent = ({
315338
extensionAPI,
316339
});
317340
const newShapeId = createShapeId();
341+
currentIndex = getIndexAbove(currentIndex);
342+
318343
const newShape: DiscourseNodeShape = {
319-
index: "a1" as IndexKey, // TODO does this need to be unique?
344+
index: currentIndex,
320345
rotation: 0,
321346
isLocked: false,
322347
type: nodeType,

0 commit comments

Comments
 (0)