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
8 changes: 8 additions & 0 deletions krillnotes-core/src/core/scripting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,14 @@ impl ScriptRegistry {
///
/// Returns [`KrillnotesError::Scripting`] if the hook throws a Rhai error
/// or returns a malformed map.
pub fn set_query_context(&self, context: QueryContext) {
*self.query_context.lock().unwrap() = Some(context);
}

pub fn clear_query_context(&self) {
*self.query_context.lock().unwrap() = None;
}

pub fn run_on_save_hook(
&self,
schema_name: &str,
Expand Down
2 changes: 1 addition & 1 deletion krillnotes-core/src/core/workspace/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use super::*;

impl Workspace {
fn build_query_context(&self) -> Result<QueryContext> {
pub(crate) fn build_query_context(&self) -> Result<QueryContext> {
let all_notes = self.list_all_notes()?;
let mut notes_by_id: HashMap<String, Dynamic> = HashMap::new();
let mut children_by_id: HashMap<String, Vec<Dynamic>> = HashMap::new();
Expand Down
8 changes: 6 additions & 2 deletions krillnotes-core/src/core/workspace/notes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1837,13 +1837,17 @@ impl Workspace {
// - hook called commit() → apply effective_title / effective_fields
// - hook called reject(…) → return ValidationFailed error
// - hook returned Map (old API) → hard Scripting error with migration message
let (title, fields) = match self.script_registry.run_on_save_hook(
let context = self.build_query_context()?;
self.script_registry.set_query_context(context);
let hook_result = self.script_registry.run_on_save_hook(
&note_schema,
note_id,
&note_schema,
&title,
&fields,
)? {
);
self.script_registry.clear_query_context();
let (title, fields) = match hook_result? {
None => (title, fields),
Some(tx) if tx.committed => {
let pn = tx.pending_notes.get(note_id).ok_or_else(|| {
Expand Down
Loading