Skip to content

Commit

Permalink
Add select file action
Browse files Browse the repository at this point in the history
  • Loading branch information
prasmussen committed Aug 5, 2024
1 parent 9cc5ee0 commit 2194e3e
Showing 1 changed file with 34 additions and 17 deletions.
51 changes: 34 additions & 17 deletions glot_core/src/page/snippet_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,20 +319,8 @@ impl Page<Model, Msg, AppEffect, Markup> for SnippetPage {

Msg::FileSelected(captured) => {
let filename = captured.value();

let maybe_index = model
.files
.to_vec()
.iter()
.enumerate()
.find(|(_, file)| file.name == filename)
.map(|(index, _)| index);

if let Some(index) = maybe_index {
model.files.select_index(index);
}

Ok(focus_editor_effect())
let effect = select_file(model, &filename);
Ok(effect)
}

Msg::EditFileClicked => Ok(open_edit_file_modal(model)),
Expand Down Expand Up @@ -435,11 +423,13 @@ impl Page<Model, Msg, AppEffect, Markup> for SnippetPage {
Msg::EditTitleClicked => Ok(open_title_modal(model)),

Msg::SearchModalMsg(child_msg) => {
let files = model.files.to_vec();

let data: search_modal::UpdateData<Msg, AppEffect, QuickAction> =
search_modal::update(
child_msg,
&mut model.search_modal_state,
quick_actions(),
quick_actions(files),
Msg::SearchModalMsg,
)?;

Expand All @@ -452,6 +442,7 @@ impl Page<Model, Msg, AppEffect, Markup> for SnippetPage {
QuickAction::AddFile => open_add_file_modal(model),
QuickAction::Share => open_sharing_modal(model),
QuickAction::Settings => open_settings_modal(model),
QuickAction::SelectFile(name) => select_file(model, &name),
QuickAction::GoToFrontPage => go_to_home(model),

QuickAction::GoToLanguage(action) => {
Expand Down Expand Up @@ -1030,6 +1021,7 @@ pub enum QuickAction {
AddFile,
Settings,
Share,
SelectFile(String),
GoToFrontPage,
GoToLanguage(LanguageQuickAction),
}
Expand All @@ -1044,6 +1036,7 @@ impl search_modal::EntryExtra for QuickAction {
QuickAction::AddFile => "Add file".into(),
QuickAction::Share => "Open sharing dialog".into(),
QuickAction::Settings => "Open settings".into(),
QuickAction::SelectFile(name) => format!("Select {}", name),
QuickAction::GoToFrontPage => "Go to front page".into(),
QuickAction::GoToLanguage(action) => action.title(),
}
Expand All @@ -1058,6 +1051,7 @@ impl search_modal::EntryExtra for QuickAction {
QuickAction::AddFile => vec!["add".into(), "file".into()],
QuickAction::Share => vec!["open".into(), "sharing".into(), "share".into()],
QuickAction::Settings => vec!["open".into(), "settings".into()],
QuickAction::SelectFile(name) => vec!["select".into(), name.clone()],
QuickAction::GoToFrontPage => vec!["home".into(), "frontpage".into()],
QuickAction::GoToLanguage(action) => action.keywords(),
}
Expand All @@ -1072,6 +1066,7 @@ impl search_modal::EntryExtra for QuickAction {
QuickAction::AddFile => heroicons_maud::document_plus_outline(),
QuickAction::Share => heroicons_maud::share_outline(),
QuickAction::Settings => heroicons_maud::cog_6_tooth_outline(),
QuickAction::SelectFile(_) => heroicons_maud::document_outline(),
QuickAction::GoToFrontPage => heroicons_maud::link_outline(),
QuickAction::GoToLanguage(action) => action.icon(),
}
Expand All @@ -1098,13 +1093,14 @@ impl fmt::Display for QuickAction {
QuickAction::AddFile => write!(f, "add-file"),
QuickAction::Share => write!(f, "share"),
QuickAction::Settings => write!(f, "settings"),
QuickAction::SelectFile(name) => write!(f, "select-file-{}", name),
QuickAction::GoToFrontPage => write!(f, "go-to-front-page"),
QuickAction::GoToLanguage(action) => action.fmt(f),
}
}
}

fn quick_actions() -> Vec<search_modal::Entry<QuickAction>> {
fn quick_actions(files: Vec<File>) -> Vec<search_modal::Entry<QuickAction>> {
let snippet_actions = vec![
QuickAction::Run,
QuickAction::EditTitle,
Expand All @@ -1116,12 +1112,17 @@ fn quick_actions() -> Vec<search_modal::Entry<QuickAction>> {
QuickAction::GoToFrontPage,
];

let file_actions = files
.iter()
.map(|file| QuickAction::SelectFile(file.name.clone()))
.collect();

let language_actions = quick_action::language_actions()
.into_iter()
.map(QuickAction::GoToLanguage)
.collect();

[snippet_actions, language_actions]
[snippet_actions, file_actions, language_actions]
.concat()
.into_iter()
.map(search_modal::Entry::new)
Expand Down Expand Up @@ -1186,6 +1187,22 @@ fn open_add_file_modal(model: &mut Model) -> Effect<Msg, AppEffect> {
)
}

fn select_file(model: &mut Model, filename: &str) -> Effect<Msg, AppEffect> {
let maybe_index = model
.files
.to_vec()
.iter()
.enumerate()
.find(|(_, file)| file.name == filename)
.map(|(index, _)| index);

if let Some(index) = maybe_index {
model.files.select_index(index);
}

focus_editor_effect()
}

fn go_to_home(model: &Model) -> Effect<Msg, AppEffect> {
let route = Route::Home;
let url = route.to_absolute_path(&model.browser_ctx.current_url);
Expand Down

0 comments on commit 2194e3e

Please sign in to comment.