summaryrefslogtreecommitdiff
path: root/Applications
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-06-26 22:47:29 +0200
committerAndreas Kling <kling@serenityos.org>2020-06-26 22:47:29 +0200
commit85ac7dc1b1d051e66b4f6b12f78c173d70e6273b (patch)
treef455a8aa36578b05c70523c6e1edfe1d043ff6df /Applications
parent8e6522d034783eb20016a9fe22eb28025731d5e9 (diff)
downloadserenity-85ac7dc1b1d051e66b4f6b12f78c173d70e6273b.zip
TextEditor: Add live preview for HTML documents :^)
This allows you to edit HTML and see the changes live. This feature is possibly my favorite feature in the world right now.
Diffstat (limited to 'Applications')
-rw-r--r--Applications/TextEditor/TextEditorWidget.cpp31
-rw-r--r--Applications/TextEditor/TextEditorWidget.h9
2 files changed, 39 insertions, 1 deletions
diff --git a/Applications/TextEditor/TextEditorWidget.cpp b/Applications/TextEditor/TextEditorWidget.cpp
index e0d14a3bc7..39b339de3c 100644
--- a/Applications/TextEditor/TextEditorWidget.cpp
+++ b/Applications/TextEditor/TextEditorWidget.cpp
@@ -74,6 +74,9 @@ TextEditorWidget::TextEditorWidget()
if (m_markdown_preview_enabled)
update_markdown_preview();
+ if (m_html_preview_enabled)
+ update_html_preview();
+
// Do not mark as dirty on the first change (When document is first opened.)
if (m_document_opening) {
m_document_opening = false;
@@ -390,10 +393,21 @@ TextEditorWidget::TextEditorWidget()
},
this);
+ m_html_preview_action = GUI::Action::create_checkable(
+ "HTML preview", [this](auto& action) {
+ set_html_preview_enabled(action.is_checked());
+ },
+ this);
+
+ m_preview_actions.add_action(*m_markdown_preview_action);
+ m_preview_actions.add_action(*m_html_preview_action);
+ m_preview_actions.set_exclusive(true);
+
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_action(*m_html_preview_action);
view_menu.add_separator();
auto& font_menu = view_menu.add_submenu("Font");
@@ -481,6 +495,7 @@ void TextEditorWidget::set_path(const LexicalPath& lexical_path)
}
set_markdown_preview_enabled(m_extension == "md");
+ set_html_preview_enabled(m_extension == "html");
update_title();
}
@@ -546,6 +561,17 @@ void TextEditorWidget::drop_event(GUI::DropEvent& event)
}
}
+void TextEditorWidget::set_html_preview_enabled(bool enabled)
+{
+ if (m_html_preview_enabled == enabled)
+ return;
+ m_html_preview_enabled = enabled;
+ m_html_preview_action->set_checked(enabled);
+ m_page_view->set_visible(enabled);
+ if (enabled)
+ update_html_preview();
+}
+
void TextEditorWidget::set_markdown_preview_enabled(bool enabled)
{
if (m_markdown_preview_enabled == enabled)
@@ -565,3 +591,8 @@ void TextEditorWidget::update_markdown_preview()
m_page_view->load_html(html, URL::create_with_file_protocol(m_path));
}
}
+
+void TextEditorWidget::update_html_preview()
+{
+ m_page_view->load_html(m_editor->text(), URL::create_with_file_protocol(m_path));
+}
diff --git a/Applications/TextEditor/TextEditorWidget.h b/Applications/TextEditor/TextEditorWidget.h
index cb21db1bc3..23802f531a 100644
--- a/Applications/TextEditor/TextEditorWidget.h
+++ b/Applications/TextEditor/TextEditorWidget.h
@@ -45,12 +45,14 @@ public:
GUI::TextEditor& editor() { return *m_editor; }
void set_markdown_preview_enabled(bool);
+ void set_html_preview_enabled(bool);
private:
TextEditorWidget();
void set_path(const LexicalPath& file);
void update_title();
void update_markdown_preview();
+ void update_html_preview();
virtual void drop_event(GUI::DropEvent&) override;
@@ -64,13 +66,17 @@ private:
RefPtr<GUI::Action> m_save_as_action;
RefPtr<GUI::Action> m_find_replace_action;
RefPtr<GUI::Action> m_line_wrapping_setting_action;
- RefPtr<GUI::Action> m_markdown_preview_action;
+
RefPtr<GUI::Action> m_find_next_action;
RefPtr<GUI::Action> m_find_previous_action;
RefPtr<GUI::Action> m_replace_next_action;
RefPtr<GUI::Action> m_replace_previous_action;
RefPtr<GUI::Action> m_replace_all_action;
+ GUI::ActionGroup m_preview_actions;
+ RefPtr<GUI::Action> m_markdown_preview_action;
+ RefPtr<GUI::Action> m_html_preview_action;
+
RefPtr<GUI::StatusBar> m_statusbar;
RefPtr<GUI::TextBox> m_find_textbox;
@@ -95,4 +101,5 @@ private:
bool m_document_dirty { false };
bool m_document_opening { false };
bool m_markdown_preview_enabled { false };
+ bool m_html_preview_enabled { false };
};