@@ -44,7 +44,13 @@ import apiPut from "roamjs-components/util/apiPut";
4444import { ExportGithub } from "./ExportGithub" ;
4545import isLiveBlock from "roamjs-components/queries/isLiveBlock" ;
4646import 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" ;
4854import calcCanvasNodeSizeAndImg from "~/utils/calcCanvasNodeSizeAndImg" ;
4955import { DiscourseNodeShape } from "~/components/canvas/DiscourseNodeUtil" ;
5056import { 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