mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:27:45 +00:00
TextEditor: Allow turning off the preview mode
This patch adds a PreviewMode enum with the following values: - None - Markdown - HTML This makes it a bit more logical to implement exclusive behavior.
This commit is contained in:
parent
a378500b45
commit
efc335c457
2 changed files with 54 additions and 28 deletions
|
@ -71,11 +71,7 @@ TextEditorWidget::TextEditorWidget()
|
||||||
m_editor->set_line_wrapping_enabled(true);
|
m_editor->set_line_wrapping_enabled(true);
|
||||||
|
|
||||||
m_editor->on_change = [this] {
|
m_editor->on_change = [this] {
|
||||||
if (m_markdown_preview_enabled)
|
update_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) {
|
||||||
|
@ -387,18 +383,24 @@ TextEditorWidget::TextEditorWidget()
|
||||||
edit_menu.add_action(*m_replace_previous_action);
|
edit_menu.add_action(*m_replace_previous_action);
|
||||||
edit_menu.add_action(*m_replace_all_action);
|
edit_menu.add_action(*m_replace_all_action);
|
||||||
|
|
||||||
|
m_no_preview_action = GUI::Action::create_checkable(
|
||||||
|
"No preview", [this](auto&) {
|
||||||
|
set_preview_mode(PreviewMode::None);
|
||||||
|
});
|
||||||
|
|
||||||
m_markdown_preview_action = GUI::Action::create_checkable(
|
m_markdown_preview_action = GUI::Action::create_checkable(
|
||||||
"Markdown preview", [this](auto& action) {
|
"Markdown preview", [this](auto&) {
|
||||||
set_markdown_preview_enabled(action.is_checked());
|
set_preview_mode(PreviewMode::Markdown);
|
||||||
},
|
},
|
||||||
this);
|
this);
|
||||||
|
|
||||||
m_html_preview_action = GUI::Action::create_checkable(
|
m_html_preview_action = GUI::Action::create_checkable(
|
||||||
"HTML preview", [this](auto& action) {
|
"HTML preview", [this](auto&) {
|
||||||
set_html_preview_enabled(action.is_checked());
|
set_preview_mode(PreviewMode::HTML);
|
||||||
},
|
},
|
||||||
this);
|
this);
|
||||||
|
|
||||||
|
m_preview_actions.add_action(*m_no_preview_action);
|
||||||
m_preview_actions.add_action(*m_markdown_preview_action);
|
m_preview_actions.add_action(*m_markdown_preview_action);
|
||||||
m_preview_actions.add_action(*m_html_preview_action);
|
m_preview_actions.add_action(*m_html_preview_action);
|
||||||
m_preview_actions.set_exclusive(true);
|
m_preview_actions.set_exclusive(true);
|
||||||
|
@ -406,6 +408,7 @@ TextEditorWidget::TextEditorWidget()
|
||||||
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_no_preview_action);
|
||||||
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_action(*m_html_preview_action);
|
||||||
view_menu.add_separator();
|
view_menu.add_separator();
|
||||||
|
@ -494,8 +497,12 @@ void TextEditorWidget::set_path(const LexicalPath& lexical_path)
|
||||||
m_plain_text_highlight->activate();
|
m_plain_text_highlight->activate();
|
||||||
}
|
}
|
||||||
|
|
||||||
set_markdown_preview_enabled(m_extension == "md");
|
if (m_extension == "md")
|
||||||
set_html_preview_enabled(m_extension == "html");
|
set_preview_mode(PreviewMode::Markdown);
|
||||||
|
else if (m_extension == "html")
|
||||||
|
set_preview_mode(PreviewMode::HTML);
|
||||||
|
else
|
||||||
|
set_preview_mode(PreviewMode::None);
|
||||||
|
|
||||||
update_title();
|
update_title();
|
||||||
}
|
}
|
||||||
|
@ -561,26 +568,38 @@ void TextEditorWidget::drop_event(GUI::DropEvent& event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextEditorWidget::set_html_preview_enabled(bool enabled)
|
void TextEditorWidget::set_preview_mode(PreviewMode mode)
|
||||||
{
|
{
|
||||||
if (m_html_preview_enabled == enabled)
|
if (m_preview_mode == mode)
|
||||||
return;
|
return;
|
||||||
m_html_preview_enabled = enabled;
|
m_preview_mode = mode;
|
||||||
m_html_preview_action->set_checked(enabled);
|
|
||||||
m_page_view->set_visible(enabled);
|
if (m_preview_mode == PreviewMode::HTML) {
|
||||||
if (enabled)
|
m_html_preview_action->set_checked(true);
|
||||||
|
m_page_view->set_visible(true);
|
||||||
update_html_preview();
|
update_html_preview();
|
||||||
|
} else if (m_preview_mode == PreviewMode::Markdown) {
|
||||||
|
m_markdown_preview_action->set_checked(true);
|
||||||
|
m_page_view->set_visible(true);
|
||||||
|
update_markdown_preview();
|
||||||
|
} else {
|
||||||
|
m_no_preview_action->set_checked(true);
|
||||||
|
m_page_view->set_visible(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextEditorWidget::set_markdown_preview_enabled(bool enabled)
|
void TextEditorWidget::update_preview()
|
||||||
{
|
{
|
||||||
if (m_markdown_preview_enabled == enabled)
|
switch (m_preview_mode) {
|
||||||
return;
|
case PreviewMode::Markdown:
|
||||||
m_markdown_preview_enabled = enabled;
|
|
||||||
m_markdown_preview_action->set_checked(enabled);
|
|
||||||
m_page_view->set_visible(enabled);
|
|
||||||
if (enabled)
|
|
||||||
update_markdown_preview();
|
update_markdown_preview();
|
||||||
|
break;
|
||||||
|
case PreviewMode::HTML:
|
||||||
|
update_html_preview();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextEditorWidget::update_markdown_preview()
|
void TextEditorWidget::update_markdown_preview()
|
||||||
|
|
|
@ -44,13 +44,19 @@ public:
|
||||||
|
|
||||||
GUI::TextEditor& editor() { return *m_editor; }
|
GUI::TextEditor& editor() { return *m_editor; }
|
||||||
|
|
||||||
void set_markdown_preview_enabled(bool);
|
enum class PreviewMode {
|
||||||
void set_html_preview_enabled(bool);
|
None,
|
||||||
|
Markdown,
|
||||||
|
HTML,
|
||||||
|
};
|
||||||
|
|
||||||
|
void set_preview_mode(PreviewMode);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TextEditorWidget();
|
TextEditorWidget();
|
||||||
void set_path(const LexicalPath& file);
|
void set_path(const LexicalPath& file);
|
||||||
void update_title();
|
void update_title();
|
||||||
|
void update_preview();
|
||||||
void update_markdown_preview();
|
void update_markdown_preview();
|
||||||
void update_html_preview();
|
void update_html_preview();
|
||||||
|
|
||||||
|
@ -74,6 +80,7 @@ private:
|
||||||
RefPtr<GUI::Action> m_replace_all_action;
|
RefPtr<GUI::Action> m_replace_all_action;
|
||||||
|
|
||||||
GUI::ActionGroup m_preview_actions;
|
GUI::ActionGroup m_preview_actions;
|
||||||
|
RefPtr<GUI::Action> m_no_preview_action;
|
||||||
RefPtr<GUI::Action> m_markdown_preview_action;
|
RefPtr<GUI::Action> m_markdown_preview_action;
|
||||||
RefPtr<GUI::Action> m_html_preview_action;
|
RefPtr<GUI::Action> m_html_preview_action;
|
||||||
|
|
||||||
|
@ -100,6 +107,6 @@ 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_html_preview_enabled { false };
|
PreviewMode m_preview_mode { PreviewMode::None };
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue