Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to "collect" nodes #163

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
39 changes: 31 additions & 8 deletions org-roam-ui.el
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
(require 'org-roam)
(require 'org-roam-dailies)
(require 'websocket)
(require 'cl-lib) ;; for implicit cl-block around dolist

(defgroup org-roam-ui nil
"UI in Org-roam."
Expand Down Expand Up @@ -131,6 +132,15 @@ Defaults to #'browse-url."
:group 'org-roam-ui
:type 'function)

(defcustom org-roam-ui-collect-function '((delve . delve-insert-nodes-by-id))
"Alist defining functions which collects nodes by passing their ids.
The key is the name of the package which contains that function.
The first package which is installed will be used. The
associated function must accept two arguments: A collection name
and a list of node ids."
:group 'org-roam-ui
:type 'alist)

;;Hooks

(defcustom org-roam-ui-before-open-node-functions nil
Expand Down Expand Up @@ -210,16 +220,29 @@ Takes _WS and FRAME as arguments."
(websocket-frame-text frame) :object-type 'alist))
(command (alist-get 'command msg))
(data (alist-get 'data msg)))
(cond ((string= command "open")
(org-roam-ui--on-msg-open-node data))
((string= command "delete")
(org-roam-ui--on-msg-delete-node data))
((string= command "create")
(org-roam-ui--on-msg-create-node data))
(t
(message
(pcase command
("open" (org-roam-ui--on-msg-open-node data))
("delete" (org-roam-ui--on-msg-delete-node data))
("create" (org-roam-ui--on-msg-create-node data))
("collect" (org-roam-ui--on-msg-collect-node))
(_ (message
"Something went wrong when receiving a message from org-roam-ui")))))

(defun org-roam-ui--on-msg-collect-node (data)
"Add a node or nodes specified in DATA to a collection.
Assumes DATA to be an alist with the following keys: `id',
`collection'. ID is a node ID or a list of IDs; COLLECTION is
the name of the collection (a string). Pass both arguments to
the first available function found in
`org-roam-ui-collect-function'."
(let* ((collection (alist-get 'collection data))
(ids (alist-get 'id data)))
(or (pcase-dolist (`(,package-name . ,fn) org-roam-ui-collect-function)
(when (featurep package-name)
(funcall fn collection (if (listp ids) ids (list ids)))
(cl-return t)))
(error "No function defined for collecting nodes, see `org-roam-ui-collect-function'"))))

(defun org-roam-ui--on-msg-open-node (data)
"Open a node when receiving DATA from the websocket."
(let* ((node (org-roam-node-from-id (alist-get'id data)))
Expand Down