]> uap-core.de Git - note.git/commitdiff
fix attachments messing up styling offsets
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Sun, 20 Apr 2025 16:31:12 +0000 (18:31 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Sun, 20 Apr 2025 16:31:12 +0000 (18:31 +0200)
application/gtk-text.c

index 2056180e782d3d7b5539caae055a0e1fd30099b0..66b1eab8838eb6c4efb513a9ec3c115c36390bab 100644 (file)
@@ -734,17 +734,16 @@ void editor_apply_styles(Resource *note, UIWIDGET textview, UiText *text, CxList
     GtkTextTagTable *tagtable = gtk_text_buffer_get_tag_table(buffer);
     NoteEditor *editor = g_object_get_data(G_OBJECT(textview), "editor");
     
+    // anything that needs to be inserted into the textbuffer must be inserted
+    // after all other styling operations
+    CxList *insert_attachments = cxLinkedListCreateSimple(CX_STORE_POINTERS);
+    
     CxIterator i = cxListIterator(styles);
     cx_foreach(MDDocStyleSection*, sec, i) {
         if(sec->length == MDDocStyleSection_IMAGE) {
-            Attachment *attachment = note_get_attachment(note, sec->link);
-            if(attachment && attachment->ui->img->value) {  
-                GtkTextIter iter;
-                gtk_text_buffer_get_iter_at_offset(buffer, &iter, sec->pos);
-                editor_insert_image(editor, attachment, &iter);
-            } else {
-                // TODO: what do we do in this case?
-            }
+            // don't insert attachments now, because it would mess up the indices
+            // insert in reverse order
+            cxListInsert(insert_attachments, 0, sec);
             continue;
         }
         
@@ -765,6 +764,20 @@ void editor_apply_styles(Resource *note, UIWIDGET textview, UiText *text, CxList
             gtk_text_buffer_apply_tag(buffer, linktag, &begin, &end);
         }
     }
+    
+    i = cxListIterator(insert_attachments);
+    cx_foreach(MDDocStyleSection *, sec, i) {
+        Attachment *attachment = note_get_attachment(note, sec->link);
+        if(attachment && attachment->ui->img->value) {  
+            GtkTextIter iter;
+            gtk_text_buffer_get_iter_at_offset(buffer, &iter, sec->pos);
+            editor_insert_image(editor, attachment, &iter);
+        } else {
+            // TODO: what do we do in this case?
+        }
+    }
+    
+    cxListFree(insert_attachments);
 }
 
 /*