1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:27:45 +00:00

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.
This commit is contained in:
Andreas Kling 2020-06-26 22:47:29 +02:00
parent 8e6522d034
commit 85ac7dc1b1
2 changed files with 39 additions and 1 deletions

View file

@ -74,6 +74,9 @@ TextEditorWidget::TextEditorWidget()
if (m_markdown_preview_enabled) if (m_markdown_preview_enabled)
update_markdown_preview(); 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.) // Do not mark as dirty on the first change (When document is first opened.)
if (m_document_opening) { if (m_document_opening) {
m_document_opening = false; m_document_opening = false;
@ -390,10 +393,21 @@ TextEditorWidget::TextEditorWidget()
}, },
this); 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"); auto& view_menu = menubar->add_menu("View");
view_menu.add_action(*m_line_wrapping_setting_action); view_menu.add_action(*m_line_wrapping_setting_action);
view_menu.add_separator(); view_menu.add_separator();
view_menu.add_action(*m_markdown_preview_action); view_menu.add_action(*m_markdown_preview_action);
view_menu.add_action(*m_html_preview_action);
view_menu.add_separator(); view_menu.add_separator();
auto& font_menu = view_menu.add_submenu("Font"); 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_markdown_preview_enabled(m_extension == "md");
set_html_preview_enabled(m_extension == "html");
update_title(); 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) void TextEditorWidget::set_markdown_preview_enabled(bool enabled)
{ {
if (m_markdown_preview_enabled == 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)); 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));
}

View file

@ -45,12 +45,14 @@ public:
GUI::TextEditor& editor() { return *m_editor; } GUI::TextEditor& editor() { return *m_editor; }
void set_markdown_preview_enabled(bool); void set_markdown_preview_enabled(bool);
void set_html_preview_enabled(bool);
private: private:
TextEditorWidget(); TextEditorWidget();
void set_path(const LexicalPath& file); void set_path(const LexicalPath& file);
void update_title(); void update_title();
void update_markdown_preview(); void update_markdown_preview();
void update_html_preview();
virtual void drop_event(GUI::DropEvent&) override; virtual void drop_event(GUI::DropEvent&) override;
@ -64,13 +66,17 @@ private:
RefPtr<GUI::Action> m_save_as_action; RefPtr<GUI::Action> m_save_as_action;
RefPtr<GUI::Action> m_find_replace_action; RefPtr<GUI::Action> m_find_replace_action;
RefPtr<GUI::Action> m_line_wrapping_setting_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_next_action;
RefPtr<GUI::Action> m_find_previous_action; RefPtr<GUI::Action> m_find_previous_action;
RefPtr<GUI::Action> m_replace_next_action; RefPtr<GUI::Action> m_replace_next_action;
RefPtr<GUI::Action> m_replace_previous_action; RefPtr<GUI::Action> m_replace_previous_action;
RefPtr<GUI::Action> m_replace_all_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::StatusBar> m_statusbar;
RefPtr<GUI::TextBox> m_find_textbox; RefPtr<GUI::TextBox> m_find_textbox;
@ -95,4 +101,5 @@ private:
bool m_document_dirty { false }; bool m_document_dirty { false };
bool m_document_opening { false }; bool m_document_opening { false };
bool m_markdown_preview_enabled { false }; bool m_markdown_preview_enabled { false };
bool m_html_preview_enabled { false };
}; };