diff options
author | Thanos Apollo <[email protected]> | 2024-12-18 03:41:21 +0200 |
---|---|---|
committer | Thanos Apollo <[email protected]> | 2024-12-18 04:31:03 +0200 |
commit | d21ffc3d600353fa4ac17d825beb219be8c7bb6c (patch) | |
tree | 79d42c43e67404be780b48ac67e3697d1f243def | |
parent | dbaaf5b2b0390add21aef5b75d8e021545836bc5 (diff) |
Remove org-roam specific data & adjust for org-gnosis
-rw-r--r-- | org-roam-ui.el | 184 |
1 files changed, 119 insertions, 65 deletions
diff --git a/org-roam-ui.el b/org-roam-ui.el index 198cef6..97e1da2 100644 --- a/org-roam-ui.el +++ b/org-roam-ui.el @@ -6,7 +6,7 @@ ;; URL: https://github.com/org-roam/org-roam-ui ;; Keywords: files outlines ;; Version: 0.1 -;; Package-Requires: ((emacs "27.1") (org-roam "2.0.0") (simple-httpd "20191103.1446") (websocket "1.13")) +;; Package-Requires: ((emacs "27.1") (simple-httpd "20191103.1446") (websocket "1.13")) ;; This file is NOT part of GNU Emacs. @@ -38,9 +38,8 @@ ;;;; Dependencies (require 'json) (require 'simple-httpd) -(require 'org-roam) +(require 'org-gnosis) (require 'websocket) -;; (require 'org-roam-dailies) (defgroup org-roam-ui nil "UI in Org-roam." @@ -53,7 +52,7 @@ :group 'org-roam-ui :type 'directory) -(defcustom org-roam-ui-port 35901 +(defcustom org-roam-ui-port 35902 "Roam UI Port." :group 'org-roam-ui :type 'number) @@ -133,7 +132,7 @@ Defaults to #'browse-url." :group 'org-roam-ui :type 'function) -(defcustom org-roam-ui-tags-function #'org-roam-ui-get-tags +(defcustom org-roam-ui-tags-function #'(lambda () (org-gnosis-select '* 'tags '1=1 t)) "Function that returns all tags as a list." :group 'org-roam-ui :type 'function) @@ -143,7 +142,7 @@ Defaults to #'browse-url." :group 'org-roam-ui :type 'function) -(defcustom org-roam-ui-links-function #'org-roam-ui-get-links +(defcustom org-roam-ui-links-function #'(lambda () (org-gnosis-select '* 'links)) "Function that returns links." :group 'org-roam-ui :type 'function) @@ -227,17 +226,25 @@ This serves the web-build and API over HTTP." (remove-hook 'after-save-hook #'org-roam-ui--on-save) (org-roam-ui-follow-mode -1))))) +(defun org-roam-ui--log-data (message data) + "Log MESSAGE along with DATA for debugging purposes." + (message "%s: %s" message (if (stringp data) data (prin1-to-string data)))) + (defun org-roam-ui--ws-on-open (ws) "Open the websocket WS to org-roam-ui and send initial data." - (progn + (when (websocket-openp ws) (setq org-roam-ui-ws-socket ws) - (org-roam-ui--send-variables org-roam-ui-ws-socket) - (org-roam-ui--send-graphdata) + (org-roam-ui--log-data "WebSocket opened." nil) + (condition-case err + (progn + (org-roam-ui--send-variables ws) + (org-roam-ui--send-graphdata)) + (error (message "Error while sending data on open: %S" err))) (when org-roam-ui-update-on-save (add-hook 'after-save-hook #'org-roam-ui--on-save)) - (message "Connection established with org-roam-ui") (when org-roam-ui-follow - (org-roam-ui-follow-mode 1)))) + (org-roam-ui-follow-mode 1)) + (message "Connection established with org-roam-ui."))) (defun org-roam-ui--ws-on-message (_ws frame) "Functions to run when the org-roam-ui server receives a message. @@ -342,7 +349,7 @@ TODO: Be able to delete individual nodes." "Send graphdata on saving an org-roam buffer. TODO: Make this only send the changes to the graph data, not the complete graph." - (when (org-roam-buffer-p) + (when (bound-and-true-p org-gnosis-mode) (org-roam-ui--send-variables org-roam-ui-ws-socket) (org-roam-ui--send-graphdata))) @@ -492,23 +499,59 @@ If not provided, data is retrieved from the org-roam database." (lambda (link) (string-match-p "cite" (nth 2 link))) links)) +;;;; Databse query for org-roam +;; (defun org-roam-ui-query () +;; "Database query." +;; (org-roam-db-query +;; [:select [id +;; file +;; title +;; level +;; (funcall group-concat tag +;; (emacsql-escape-raw \, ))] +;; :as tags +;; :from nodes +;; :left-join tags +;; :on (= id node_id) +;; :group :by id])) + +;; Query for gnosis +(defun org-roam-ui-query () + "Query to get nodes from org-gnosis db." + (emacsql org-gnosis-db + [:select [id file title level + (funcall group-concat tag + (emacsql-escape-raw \, ))] + :from nodes + :as tags + :left-join tags + :on (= id id) + :group :by id])) (defun org-roam-ui--get-nodes () - "Nodes to display in org-roam-ui." - (org-roam-db-query [:select [id - file - title - level - pos - olp - properties - (funcall group-concat tag - (emacsql-escape-raw \, ))] - :as tags - :from nodes - :left-join tags - :on (= id node_id) - :group :by id])) + "Fetch nodes to display in org-roam-ui with properly structured output." + (mapcar (lambda (node) + (let ((id (nth 0 node)) + (file (nth 1 node)) + (title (nth 2 node)) + (level (nth 3 node)) + (tags (nth 4 node))) + ;; Construct the node structure with correct ordering + (list id + file + title + level + 1 ;; Assume priority or similar static position value + nil ;; This corresponds to the `position` from your desired output + ;; Properties + (list (cons "CATEGORY" (file-name-base file)) + (cons "ID" id) + (cons "BLOCKED" "") + (cons "ALLTAGS" (propertize (concat ":" tags ":") 'face 'org-tag)) ;; Assuming tags need to be formatted this way + (cons "FILE" file) + (cons "PRIORITY" "B")) + tags))) ;; Put tags at the last position as per your requirement + (org-roam-ui-query))) (defun org-roam-ui--get-links (&optional old) "Get the cites and links tables as rows from the org-roam db. @@ -571,13 +614,19 @@ from all other links." (when (and (websocket-openp org-roam-ui-ws-socket) (org-roam-buffer-p) (buffer-file-name (buffer-base-buffer))) - (let* ((node (org-roam-id-at-point))) - (unless (string= org-roam-ui--ws-current-node node) - (setq org-roam-ui--ws-current-node node) - (websocket-send-text org-roam-ui-ws-socket - (json-encode `((type . "command") - (data . ((commandName . "follow") - (id . ,node)))))))))) + (let* ((node (org-gnosis-get-id))) + (setq org-roam-ui--ws-current-node node) + (websocket-send-text org-roam-ui-ws-socket + (json-encode `((type . "command") + (data . ((commandName . "follow") + (id . ,node)))))) + ;; (unless (string= org-roam-ui--ws-current-node node) + ;; (setq org-roam-ui--ws-current-node node) + ;; (websocket-send-text org-roam-ui-ws-socket + ;; (json-encode `((type . "command") + ;; (data . ((commandName . "follow") + ;; (id . ,node))))))) + ))) (defun org-roam-ui--update-theme () @@ -603,43 +652,48 @@ from all other links." (defun org-roam-ui--send-variables (ws) "Send miscellaneous org-roam variables through the websocket WS." - (let ((daily-dir org-roam-ui-dailies-directory) - (attach-dir (if (boundp 'org-attach-id-dir) - org-attach-id-dir + (let ((daily-dir (or org-roam-ui-dailies-directory "")) + (attach-dir (or (and (boundp 'org-attach-id-dir) org-attach-id-dir) (expand-file-name ".attach/" org-directory))) - (use-inheritance (if (boundp 'org-attach-use-inheritance) - org-attach-use-inheritance - nil)) - (sub-dirs (org-roam-ui-find-subdirectories))) - (websocket-send-text org-roam-ui-ws-socket - (json-encode - `((type . "variables") - (data . - (("subDirs". - ,sub-dirs) - ("dailyDir" . - ,daily-dir) - ("attachDir" . - ,attach-dir) - ("useInheritance" . - ,use-inheritance) - ("roamDir" . ,org-roam-ui-directory) - ("katexMacros" . ,org-roam-ui-latex-macros)))))))) + (use-inheritance (or (and (boundp 'org-attach-use-inheritance) + org-attach-use-inheritance) + nil)) + (sub-dirs (or (org-roam-ui-find-subdirectories) (list "")))) + (org-roam-ui--log-data "Sending variables" + `(("subDirs" . ,sub-dirs) + ("dailyDir" . ,daily-dir) + ("attachDir" . ,attach-dir) + ("useInheritance" . ,use-inheritance) + ("roamDir" . ,(or org-roam-ui-directory "")) + ("katexMacros" . ,(or org-roam-ui-latex-macros '())))) + (websocket-send-text ws + (json-encode + `((type . "variables") + (data . + (("subDirs" . ,sub-dirs) + ("dailyDir" . ,daily-dir) + ("attachDir" . ,attach-dir) + ("useInheritance" . ,use-inheritance) + ("roamDir" . ,(or org-roam-ui-directory "")) + ("katexMacros" . ,(or org-roam-ui-latex-macros '()))))))))) (defun org-roam-ui-sql-to-alist (column-names rows) "Convert sql result to alist for json encoding. -ROWS is the sql result, while COLUMN-NAMES is the columns to use." +ROWS is the sql result, while COLUMN-names is the columns to use." (let (res) (while rows - ;; I don't know how to get the tags as a simple list, so we post process it - (if (not (string= (car column-names) "tags")) - (push (cons (pop column-names) (pop rows)) res) + (cond + ((not (string= (car column-names) "tags")) + (push (cons (pop column-names) (pop rows)) res)) + ((string= (car column-names) "properties") + nil) + (t (push (cons (pop column-names) (seq-remove (lambda (elt) (string= elt ",")) rows)) res) - (setq rows nil))) + (setq rows nil)))) res)) (defun org-roam-ui-get-theme () @@ -689,7 +743,7 @@ The ID of the node you want to travel to. The SPEED in ms it takes to make the transition. The PADDING around the nodes in the viewport." (interactive) - (if-let ((node (or id (org-roam-id-at-point)))) + (if-let ((node (or id (org-gnosis-get-id)))) (websocket-send-text org-roam-ui-ws-socket (json-encode `((type . "command") (data . ((commandName . "zoom") @@ -704,7 +758,7 @@ The PADDING around the nodes in the viewport." "Open the local graph view of the current node. Optionally with ID (string), SPEED (number, ms) and PADDING (number, px)." (interactive) - (if-let ((node (or id (org-roam-id-at-point)))) + (if-let ((node (or id (org-gnosis-get-id)))) (websocket-send-text org-roam-ui-ws-socket (json-encode `((type . "command") (data . ((commandName . "local") @@ -717,7 +771,7 @@ Optionally with ID (string), SPEED (number, ms) and PADDING (number, px)." (defun org-roam-ui-change-local-graph (&optional id manipulation) "Add or remove current node to the local graph. If not in local mode, open local-graph for this node." (interactive) - (if-let ((node (or id (org-roam-id-at-point)))) + (if-let ((node (or id (org-gnosis-get-id)))) (websocket-send-text org-roam-ui-ws-socket (json-encode `((type . "command") (data . ((commandName . "change-local-graph") @@ -760,9 +814,9 @@ Optionally with ID (string), SPEED (number, ms) and PADDING (number, px)." :init-value nil (if org-roam-ui-follow-mode (progn - (add-hook 'post-command-hook #'org-roam-ui--update-current-node) + (add-hook 'org-gnosis-mode-hook #'org-roam-ui-node-zoom) (message "org-roam-ui will now follow you around.")) - (remove-hook 'post-command-hook #'org-roam-ui--update-current-node) + (remove-hook 'org-gnosis-mode-hook #'org-roam-ui-node-zoom) (message "org-roam-ui will now leave you alone."))) (provide 'org-roam-ui) |