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

ENH: #273 Adds readonly mode custom var to control deleting and creating nodes via org-roam-ui #276

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 4 additions & 2 deletions components/contextmenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export default interface ContextMenuProps {
target: OrgRoamNode | string | null
nodeType?: string
coordinates: { [direction: string]: number | undefined }
readonlyMode: boolean
handleLocal: (node: OrgRoamNode, add: string) => void
menuClose: () => void
scope: { nodeIds: string[] }
Expand All @@ -71,6 +72,7 @@ export const ContextMenu = (props: ContextMenuProps) => {
target,
nodeType,
coordinates,
readonlyMode,
handleLocal,
menuClose,
scope,
Expand Down Expand Up @@ -132,7 +134,7 @@ export const ContextMenu = (props: ContextMenuProps) => {
>
Open in Emacs
</MenuItem>
) : (
) : !readonlyMode && (
<MenuItem icon={<AddIcon />} onClick={() => createNodeInEmacs(target, webSocket)}>
Create node
</MenuItem>
Expand Down Expand Up @@ -183,7 +185,7 @@ export const ContextMenu = (props: ContextMenuProps) => {
>
Preview
</MenuItem>
{target?.level === 0 && (
{!readonlyMode && target?.level === 0 && (
<MenuItem
closeOnSelect={false}
icon={<DeleteIcon color="red.500" />}
Expand Down
29 changes: 19 additions & 10 deletions org-roam-ui.el
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ Defaults to #'browse-url."
:group 'org-roam-ui
:type 'function)

(defcustom org-roam-ui-readonly-mode nil
"If true, 'org-roam-ui' will open in readonly mode."
:group 'org-roam-ui
:type 'boolean)

;;Hooks

(defcustom org-roam-ui-before-open-node-functions nil
Expand Down Expand Up @@ -255,20 +260,22 @@ Takes _WS and FRAME as arguments."
"Delete a node when receiving DATA from the websocket.

TODO: Be able to delete individual nodes."
(progn
(message "Deleted %s" (alist-get 'file data))
(delete-file (alist-get 'file data))
(org-roam-db-sync)
(org-roam-ui--send-graphdata)))
(if (not org-roam-ui-readonly-mode)
(progn
(message "Deleted %s" (alist-get 'file data))
(delete-file (alist-get 'file data))
(org-roam-db-sync)
(org-roam-ui--send-graphdata))))

(defun org-roam-ui--on-msg-create-node (data)
"Create a node when receiving DATA from the websocket."
(progn
(if (and (fboundp #'orb-edit-note) (alist-get 'ROAM_REFS data))
(if (not org-roam-ui-readonly-mode)
(progn
(if (and (fboundp #'orb-edit-note) (alist-get 'ROAM_REFS data))
(orb-edit-note (alist-get 'id data)))
(org-roam-capture-
:node (org-roam-node-create :title (alist-get 'title data))
:props '(:finalize find-file))))
(org-roam-capture-
:node (org-roam-node-create :title (alist-get 'title data))
:props '(:finalize find-file)))))

(defun org-roam-ui--ws-on-close (_websocket)
"What to do when _WEBSOCKET to org-roam-ui is closed."
Expand Down Expand Up @@ -601,6 +608,8 @@ from all other links."
("useInheritance" .
,use-inheritance)
("roamDir" . ,org-roam-directory)
("readonlyMode" .
,org-roam-ui-readonly-mode)
("katexMacros" . ,org-roam-ui-latex-macros))))))))

(defun org-roam-ui-sql-to-alist (column-names rows)
Expand Down
2 changes: 2 additions & 0 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export interface EmacsVariables {
katexMacros?: { [key: string]: string }
attachDir?: string
useInheritance?: boolean
readonlyMode?: boolean
subDirs: string[]
}
export type Tags = string[]
Expand Down Expand Up @@ -710,6 +711,7 @@ export function GraphPage() {
target={contextMenuTarget}
background={false}
coordinates={contextPos}
readonlyMode={emacsVariables.readonlyMode || false}
handleLocal={handleLocal}
menuClose={contextMenu.onClose.bind(contextMenu)}
webSocket={WebSocketRef.current}
Expand Down