diff --git a/lua/dbee/api/ui.lua b/lua/dbee/api/ui.lua index 3099a36..542d89f 100644 --- a/lua/dbee/api/ui.lua +++ b/lua/dbee/api/ui.lua @@ -72,6 +72,14 @@ function ui.editor_namespace_create_note(id, name) return state.editor():namespace_create_note(id, name) end +--- Opens a file as a temporary note in the global namespace +---@param id namespace_id +---@param file string +---@return note_id +function ui.editor_open_file_as_note(file) + return state.editor():open_file_as_note(file) +end + --- Get notes of a specified namespace. ---@param id namespace_id ---@return note_details[] diff --git a/lua/dbee/ui/drawer/init.lua b/lua/dbee/ui/drawer/init.lua index c7e0615..e32a6a0 100644 --- a/lua/dbee/ui/drawer/init.lua +++ b/lua/dbee/ui/drawer/init.lua @@ -105,6 +105,11 @@ function DrawerUI:new(handler, editor, result, opts) o:on_current_note_changed(data) end) + editor:register_event_listener("note_created", + function (data) + o:on_note_created(data) + end) + return o end @@ -130,6 +135,12 @@ function DrawerUI:on_current_note_changed(data) self:refresh() end +-- event listener for new note created +---@private +function DrawerUI:on_note_created(_) + self:refresh() +end + ---@private ---@param bufnr integer ---@return NuiTree tree diff --git a/lua/dbee/ui/editor/init.lua b/lua/dbee/ui/editor/init.lua index 151afed..f5da98b 100644 --- a/lua/dbee/ui/editor/init.lua +++ b/lua/dbee/ui/editor/init.lua @@ -292,6 +292,28 @@ function EditorUI:namespace_create_note(id, name) return note_id end +-- Creates a new note in namespace. +-- Errors if id or name is nil or there is a note with the same +-- name in namespace already. +---@param file string +---@return note_id +function EditorUI:open_file_as_note(file) + ---@type note_details + local s = { + id = file, + name = file, + file = file, + } + + -- a bit shitty + self.notes["global"] = self.notes["global"] or {} + self.notes["global"][file] = s + + self:trigger_event("note_created", { note = s }) + + return file +end + ---@param id namespace_id ---@return note_details[] function EditorUI:namespace_get_notes(id)