From 5eaa7ff4068dd92ecca51305827bf627f7b7f005 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 28 Apr 2020 21:42:45 +0200 Subject: TextEditor: Add live preview when editing markdown :^) This works by hooking into the change notifications from the TextEditor widget and parsing the document content as markdown, and generating an HTML document from it which is displayed using LibWeb. This will make it a bit easier to write man pages! :^) --- Applications/TextEditor/Makefile | 2 +- Applications/TextEditor/TextEditorWidget.cpp | 53 +++++++++++++++++++++++++--- Applications/TextEditor/TextEditorWidget.h | 9 ++++- 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/Applications/TextEditor/Makefile b/Applications/TextEditor/Makefile index 051788ffa6..6d917e407d 100755 --- a/Applications/TextEditor/Makefile +++ b/Applications/TextEditor/Makefile @@ -4,6 +4,6 @@ OBJS = \ PROGRAM = TextEditor -LIB_DEPS = GUI Gfx VT IPC Thread Pthread Core JS +LIB_DEPS = Web Markdown GUI Gfx VT Protocol IPC Thread Pthread Core JS include ../../Makefile.common diff --git a/Applications/TextEditor/TextEditorWidget.cpp b/Applications/TextEditor/TextEditorWidget.cpp index afb27ea441..c0944743ef 100644 --- a/Applications/TextEditor/TextEditorWidget.cpp +++ b/Applications/TextEditor/TextEditorWidget.cpp @@ -42,12 +42,16 @@ #include #include #include +#include #include #include #include #include #include #include +#include +#include +#include #include TextEditorWidget::TextEditorWidget() @@ -58,12 +62,18 @@ TextEditorWidget::TextEditorWidget() auto& toolbar_container = add(); auto& toolbar = toolbar_container.add(); - m_editor = add(); + + auto& splitter = add(); + + m_editor = splitter.add(); m_editor->set_ruler_visible(true); m_editor->set_automatic_indentation_enabled(true); m_editor->set_line_wrapping_enabled(true); m_editor->on_change = [this] { + if (m_markdown_preview_enabled) + update_markdown_preview(); + // Do not mark as diry on the first change (When document is first opened.) if (m_document_opening) { m_document_opening = false; @@ -76,6 +86,9 @@ TextEditorWidget::TextEditorWidget() update_title(); }; + m_html_view = splitter.add(); + m_html_view->set_visible(false); + m_find_replace_widget = add(); m_find_replace_widget->set_fill_with_background_color(true); m_find_replace_widget->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed); @@ -405,9 +418,17 @@ TextEditorWidget::TextEditorWidget() syntax_actions.add_action(*m_js_highlight); syntax_menu.add_action(*m_js_highlight); + m_markdown_preview_action = GUI::Action::create_checkable( + "Markdown preview", [this](auto& action) { + set_markdown_preview_enabled(action.is_checked()); + }, + this); + auto& view_menu = menubar->add_menu("View"); view_menu.add_action(*m_line_wrapping_setting_action); view_menu.add_separator(); + view_menu.add_action(*m_markdown_preview_action); + view_menu.add_separator(); view_menu.add_submenu(move(font_menu)); view_menu.add_submenu(move(syntax_menu)); @@ -445,12 +466,15 @@ void TextEditorWidget::set_path(const FileSystemPath& file) m_name = file.title(); m_extension = file.extension(); - if (m_extension == "cpp" || m_extension == "h") + if (m_extension == "cpp" || m_extension == "h") { m_cpp_highlight->activate(); - else if (m_extension == "js") + } else if (m_extension == "js") { m_js_highlight->activate(); - else + } else { m_plain_text_highlight->activate(); + } + + set_markdown_preview_enabled(m_extension == "md"); update_title(); } @@ -515,3 +539,24 @@ void TextEditorWidget::drop_event(GUI::DropEvent& event) open_sesame(urls.first().path()); } } + +void TextEditorWidget::set_markdown_preview_enabled(bool enabled) +{ + if (m_markdown_preview_enabled == enabled) + return; + m_markdown_preview_enabled = enabled; + m_markdown_preview_action->set_checked(enabled); + m_html_view->set_visible(enabled); + if (enabled) + update_markdown_preview(); +} + +void TextEditorWidget::update_markdown_preview() +{ + Markdown::Document document; + if (document.parse(m_editor->text())) { + auto html = document.render_to_html(); + auto html_document = Web::parse_html_document(html); + m_html_view->set_document(html_document); + } +} diff --git a/Applications/TextEditor/TextEditorWidget.h b/Applications/TextEditor/TextEditorWidget.h index 89e22f48bd..a2c57a1c37 100644 --- a/Applications/TextEditor/TextEditorWidget.h +++ b/Applications/TextEditor/TextEditorWidget.h @@ -33,7 +33,7 @@ #include #include #include - +#include class TextEditorWidget final : public GUI::Widget { C_OBJECT(TextEditorWidget) public: @@ -43,10 +43,13 @@ public: GUI::TextEditor& editor() { return *m_editor; } + void set_markdown_preview_enabled(bool); + private: TextEditorWidget(); void set_path(const FileSystemPath& file); void update_title(); + void update_markdown_preview(); virtual void drop_event(GUI::DropEvent&) override; @@ -60,6 +63,7 @@ private: RefPtr m_save_as_action; RefPtr m_find_replace_action; RefPtr m_line_wrapping_setting_action; + RefPtr m_markdown_preview_action; RefPtr m_find_next_action; RefPtr m_find_previous_action; RefPtr m_replace_next_action; @@ -84,6 +88,9 @@ private: RefPtr m_cpp_highlight; RefPtr m_js_highlight; + RefPtr m_html_view; + bool m_document_dirty { false }; bool m_document_opening { false }; + bool m_markdown_preview_enabled { false }; }; -- cgit v1.2.3