@@ -1106,19 +1106,32 @@ class AnnotoriousAnnotator extends HTMLElement {
11061106 saveButton . textContent = "ERROR"
11071107 throw err
11081108 } )
1109- page . items = page . items . map ( i => ( {
1110- ...i ,
1111- ...( mod . items ?. find ( a => a . target === i . target ) ?? { } )
1112- } ) )
1109+ page . items = page . items . map ( i => {
1110+ const selectorValue = i . target ?. selector ?. value ?? i . target
1111+ // Prefer matching by ID for previously-saved annotations, fall back to selector for new ones
1112+ const match = mod . items ?. find ( a => a . id === i . id )
1113+ ?? mod . items ?. find ( a => {
1114+ const aSelector = a . target ?. selector ?. value ?? a . target
1115+ return aSelector === selectorValue
1116+ } )
1117+ return match ? { ...i , ...match } : i
1118+ } )
11131119 this . #modifiedAnnotationPage = page
1120+ this . #resolvedAnnotationPage = JSON . parse ( JSON . stringify ( page ) )
1121+ // Sync server-assigned IDs back to Annotorious so subsequent saves use the correct IDs
1122+ let syncAnnotations = JSON . parse ( JSON . stringify ( page . items ) )
1123+ syncAnnotations = this . formatAnnotations ( syncAnnotations )
1124+ syncAnnotations = this . convertSelectors ( syncAnnotations , true )
1125+ this . #annotoriousInstance. clearAnnotations ( )
1126+ this . #annotoriousInstance. setAnnotations ( syncAnnotations , false )
1127+ this . #resolvedAnnotationPage. $isDirty = false
11141128 TPEN . eventDispatcher . dispatch ( "tpen-page-committed" , this . #modifiedAnnotationPage)
11151129 TPEN . eventDispatcher . dispatch ( "tpen-toast" , {
11161130 message : "Annotations Saved" ,
11171131 status : "success"
11181132 } )
11191133 saveButton . removeAttribute ( "disabled" )
11201134 saveButton . textContent = "Save Annotations"
1121- this . #resolvedAnnotationPage. $isDirty = false
11221135 return this . #modifiedAnnotationPage
11231136 }
11241137
@@ -1127,17 +1140,23 @@ class AnnotoriousAnnotator extends HTMLElement {
11271140 * https://annotorious.dev/api-reference/openseadragon-annotator/#clearannotations
11281141 */
11291142 async deleteAllAnnotations ( ) {
1143+ const deleteAllBtn = this . shadowRoot . getElementById ( "deleteAllBtn" )
1144+ deleteAllBtn . setAttribute ( "disabled" , "true" )
1145+ deleteAllBtn . textContent = "deleting. please wait..."
11301146 this . #annotoriousInstance. clearAnnotations ( )
11311147 this . #resolvedAnnotationPage. $isDirty = true
1132- await this . saveAnnotations ( )
11331148 try {
1149+ await this . saveAnnotations ( )
11341150 await this . clearColumnsServerSide ( )
11351151 } catch ( err ) {
1136- console . error ( "Could not clear columns server side ." , err )
1152+ console . error ( "Could not delete all annotations ." , err )
11371153 TPEN . eventDispatcher . dispatch ( "tpen-toast" , {
1138- message : "Could not clear columns. Some column data may remain ." ,
1154+ message : "Could not delete all annotations ." ,
11391155 status : "error"
11401156 } )
1157+ } finally {
1158+ deleteAllBtn . removeAttribute ( "disabled" )
1159+ deleteAllBtn . textContent = "Delete All Annotations"
11411160 }
11421161 }
11431162
0 commit comments