From 98b7599218ce870b07dfb6d3e72b26b9bbaa9c2a Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Sun, 20 Apr 2025 18:31:12 +0200 Subject: [PATCH] fix attachments messing up styling offsets --- application/gtk-text.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/application/gtk-text.c b/application/gtk-text.c index 2056180..66b1eab 100644 --- a/application/gtk-text.c +++ b/application/gtk-text.c @@ -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); } /* -- 2.43.5