Prechádzať zdrojové kódy

Compile markdown on each keystroke

Matthias Vogelgesang 7 rokov pred
rodič
commit
7dda2db66e
1 zmenil súbory, kde vykonal 33 pridanie a 10 odobranie
  1. 33 10
      src/iridium-window.c

+ 33 - 10
src/iridium-window.c

@@ -81,15 +81,35 @@ on_markdown_compiled (GObject *object,
   g_free (html);
 }
 
+static void
+compile_markdown (IridiumWindow *self,
+                  GtkTextBuffer *buffer)
+{
+  GtkTextIter start;
+  GtkTextIter end;
+  gchar *text;
+
+  gtk_text_buffer_get_start_iter (buffer, &start);
+  gtk_text_buffer_get_end_iter (buffer, &end);
+  text = gtk_text_buffer_get_slice (buffer, &start, &end, FALSE);
+  iridium_markdown_compile_async (self->markdown, text, G_PRIORITY_DEFAULT, NULL,
+      on_markdown_compiled, self);
+  g_free (text);
+}
+
+static void
+on_text_changed (GtkTextBuffer *buffer,
+                 IridiumWindow *self)
+{
+  compile_markdown (self, buffer);
+}
+
 static void
 on_note_selected (IridiumWindow *self, IridiumNoteRow *row, gpointer user_data)
 {
   IridiumNote *note;
   GtkTextBuffer *buffer;
   GBindingFlags flags;
-  GtkTextIter start;
-  GtkTextIter end;
-  gchar *text;
 
   if (self->title_binding != NULL)
     g_clear_object (&self->title_binding);
@@ -105,13 +125,6 @@ on_note_selected (IridiumWindow *self, IridiumNoteRow *row, gpointer user_data)
   buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->source_view));
   self->title_binding = g_object_bind_property (note, "title", self->title_entry, "text", flags);
   self->content_binding = g_object_bind_property (note, "content", buffer, "text", flags);
-
-  gtk_text_buffer_get_start_iter (buffer, &start);
-  gtk_text_buffer_get_end_iter (buffer, &end);
-  text = gtk_text_buffer_get_slice (buffer, &start, &end, FALSE);
-  iridium_markdown_compile_async (self->markdown, text, G_PRIORITY_DEFAULT, NULL,
-      on_markdown_compiled, self);
-  g_free (text);
 }
 
 static void
@@ -145,6 +158,16 @@ preview_toggled (GSimpleAction *action,
   visible = !g_variant_get_boolean (state);
   g_simple_action_set_state (action, g_variant_new_boolean (visible));
   gtk_widget_set_visible (GTK_WIDGET (self->html_view), visible);
+
+  if (visible) {
+    GtkTextBuffer *buffer;
+
+    buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->source_view));
+    compile_markdown (self, buffer);
+    g_signal_connect (buffer, "changed", G_CALLBACK (on_text_changed), self);
+    buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->source_view));
+  }
+
   g_variant_unref (state);
 }