use super::*;
impl Workspace {
pub fn new_article(&mut self, cx: &mut Context<Self>) -> Option<usize> {
let project = self.active?;
let article = article::create(&self.projects[project].path)?;
self.projects[project].articles.insert(0, article);
self.open_article(project, 0, cx);
Some(0)
}
pub fn archive_article(&mut self, path: &Path, archived: bool, cx: &mut Context<Self>) {
let Some(article) = self.article_at_mut(path) else {
return;
};
article.archive(archived);
self.prune_archived(cx);
cx.notify();
}
pub fn article_at_mut(&mut self, path: &Path) -> Option<&mut Article> {
self.projects
.iter_mut()
.flat_map(|open| open.articles.iter_mut())
.find(|article| article.path == path)
}
pub fn open_article(&mut self, project: usize, ix: usize, cx: &mut Context<Self>) {
let text_size = self.article_font_size();
let Some(article) = self
.projects
.get_mut(project)
.and_then(|open| open.articles.get_mut(ix))
else {
return;
};
article.open(text_size, cx);
self.projects[project].article = Some(ix);
let id = self.projects[project].articles[ix]
.path
.to_string_lossy()
.into_owned();
self.active = Some(project);
self.remember(project, state::Kind::Article, id, cx);
cx.notify();
}
pub fn delete_article(&mut self, project: usize, ix: usize, cx: &mut Context<Self>) {
let Some(project) = self.projects.get_mut(project) else {
return;
};
if ix >= project.articles.len() {
return;
}
project.articles.remove(ix).remove();
project.article = project
.article
.filter(|open| *open != ix)
.map(|open| if open > ix { open - 1 } else { open });
cx.notify();
}
pub fn revert_article(&mut self, path: &Path, cx: &mut Context<Self>) {
let Some(article) = self.article_at_mut(path) else {
return;
};
article.revert(cx);
cx.notify();
}
pub fn keep_article(&mut self, path: &Path, cx: &mut Context<Self>) {
let Some(article) = self.article_at_mut(path) else {
return;
};
article.keep(cx);
cx.notify();
}
pub fn active_article(&self) -> Option<&Article> {
let project = self.active_project()?;
project.articles.get(project.article?)
}
pub fn set_cover(&mut self, source: Option<&Path>, cx: &mut Context<Self>) {
if let Some(article) = self.article_mut() {
article.set_cover(source);
cx.notify();
}
}
pub fn set_full_width(&mut self, wide: Option<bool>, cx: &mut Context<Self>) {
if let Some(article) = self.article_mut() {
article.set_full_width(wide);
cx.notify();
}
}
pub fn set_article_mode(&mut self, mode: Mode, cx: &mut Context<Self>) {
if let Some(article) = self.article_mut() {
article.set_mode(mode, cx);
cx.notify();
}
}
pub fn shuffle_cover(&mut self, cx: &mut Context<Self>) {
if let Some(article) = self.article_mut() {
article.shuffle_cover();
cx.notify();
}
}
fn article_mut(&mut self) -> Option<&mut Article> {
let project = self.projects.get_mut(self.active?)?;
project.articles.get_mut(project.article?)
}
pub fn write_article(&mut self, changed: EntityId, cx: &mut Context<Self>) {
let found = self.projects.iter_mut().find_map(|project| {
project.articles.iter_mut().find(|article| {
article
.field
.as_ref()
.is_some_and(|field| field.entity_id() == changed)
|| article
.editor
.as_ref()
.is_some_and(|editor| editor.entity_id() == changed)
})
});
if found.is_some_and(|article| article.write(cx)) {
cx.notify();
}
}
}