use markdown::Cursor;
use ui::menu::{self, Item};
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Choice {
Dismiss,
Chip,
Bookmark,
Embed,
Image,
}
impl Choice {
pub fn label(self) -> &'static str {
match self {
Self::Dismiss => "Dismiss",
Self::Chip => "Create chip",
Self::Bookmark => "Create bookmark",
Self::Embed => "Create embed",
Self::Image => "Create image",
}
}
}
pub struct Paste {
pub at: Cursor,
pub url: String,
pub rows: Vec<Choice>,
pub cursor: menu::Cursor,
}
impl Paste {
pub fn open(at: Cursor, url: String, alone: bool) -> Self {
let mut rows = vec![Choice::Dismiss, Choice::Chip];
if alone {
rows.extend([Choice::Bookmark, Choice::Embed]);
if markdown::is_image(&url) {
rows.push(Choice::Image);
}
}
let mut paste = Self {
at,
url,
rows,
cursor: menu::Cursor::default(),
};
paste.step(1);
paste
}
pub fn menu(&self) -> Vec<Item> {
self.rows
.iter()
.map(|choice| Item::action(choice.label()))
.collect()
}
pub fn step(&mut self, delta: isize) {
let menu = self.menu();
self.cursor.step(&menu, delta);
}
pub fn choice(&self, path: Option<&[usize]>) -> Option<Choice> {
let row = match path {
Some(path) => *path.first()?,
None => self.cursor.row()?,
};
self.rows.get(row).copied()
}
}