Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion crates/engine/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use notecraft_store::page::Version;
use notecraft_store::recycle::Recycled;
use notecraft_store::search::Hit;
use notecraft_store::tags::Tagged;
use notecraft_store::{StoreError, assets, check, ops, page, recycle, search, tags, tree};
use notecraft_store::{StoreError, assets, check, ops, page, page_templates, recycle, search, tags, tree};
use std::collections::BTreeMap;
use std::path::PathBuf;

Expand Down Expand Up @@ -100,6 +100,29 @@ fn node_copy(s: &mut Session, p: &Params) -> Result<Output, EngineError> {
Ok(Output::Path(ops::copy_node(&s.resolve(req(p, "path")?)?, &s.resolve(req(p, "to")?)?)?))
}

fn page_templates_list(s: &mut Session, _p: &Params) -> Result<Output, EngineError> {
Ok(Output::Text(page_templates::describe(&page_templates::list(s.root()))))
}

fn page_from_template(s: &mut Session, p: &Params) -> Result<Output, EngineError> {
let parent = s.resolve(req(p, "parent")?)?;
let id = req(p, "template")?;
let template =
page_templates::find(s.root(), id).ok_or_else(|| EngineError::BadParam(format!("there is no template {id}; page.templates lists them")))?;
Ok(Output::Path(page_templates::new_page(&template, &parent, &s.settings)?))
}

fn page_save_template(s: &mut Session, p: &Params) -> Result<Output, EngineError> {
let page = s.resolve(req(p, "path")?)?;
Ok(Output::Path(page_templates::save(s.root(), &page, req(p, "name")?)?))
}

fn page_remove_template(s: &mut Session, p: &Params) -> Result<Output, EngineError> {
let id = req(p, "template")?;
page_templates::remove(s.root(), id)?;
Ok(Output::Text(format!("removed {id}")))
}

fn node_move_up(s: &mut Session, p: &Params) -> Result<Output, EngineError> {
Ok(Output::Path(ops::move_page(&s.resolve(req(p, "path")?)?, true)?))
}
Expand Down Expand Up @@ -423,6 +446,36 @@ to (required): where the copy goes; it is put last there, and every node in it g
changes_tree: true,
run: node_copy,
},
CommandSpec {
id: "page.templates",
label: "List the page templates, built in and saved: one line each, with the id, the category and the name, separated by tabs",
params: "",
changes_tree: false,
run: page_templates_list,
},
CommandSpec {
id: "page.from_template",
label: "A new page started from a template",
params: "parent (required): the section (or page, for a subpage) the new page goes in, last
template (required): the template's id, as page.templates lists it (builtin:meeting-notes, saved:lab-report)",
changes_tree: true,
run: page_from_template,
},
CommandSpec {
id: "page.save_template",
label: "Keep a page as a template",
params: "path (required): the page; its words, boxes, ink, files, paper and colour are kept, not its subpages
name (required): the template's name",
changes_tree: false,
run: page_save_template,
},
CommandSpec {
id: "page.remove_template",
label: "Take away a page saved as a template",
params: "template (required): the saved template's id (saved:...). Built-in templates cannot be removed",
changes_tree: false,
run: page_remove_template,
},
CommandSpec {
id: "node.move_up",
label: "Move a page up among its siblings",
Expand Down
1 change: 1 addition & 0 deletions crates/store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod error;
pub mod links;
pub mod ops;
pub mod page;
pub mod page_templates;
pub mod recycle;
pub mod search;
pub mod settings;
Expand Down
2 changes: 1 addition & 1 deletion crates/store/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn clean_title(title: &str) -> Result<String> {

/// A folder path under `parent` that does not exist yet, ignoring case, so two
/// pages titled "Notes" and "notes" never collide on Windows or macOS.
fn unique_child(parent: &Path, base: &str) -> PathBuf {
pub(crate) fn unique_child(parent: &Path, base: &str) -> PathBuf {
let taken: HashSet<String> = match fs::read_dir(parent) {
Ok(rd) => rd.filter_map(|e| e.ok()).map(|e| e.file_name().to_string_lossy().to_lowercase()).collect(),
Err(_) => HashSet::new(),
Expand Down
Loading
Loading