Skip to content

Commit

Permalink
feat: hide context menu when onRenderContextMenu return false
Browse files Browse the repository at this point in the history
  • Loading branch information
Xremn committed Jan 30, 2024
1 parent b8be1a5 commit 93676eb
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 9 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,11 @@ const editor = new JSONEditor({
}
```

- `onRenderContextMenu(items: ContextMenuItem[], context: { mode: 'tree' | 'text' | 'table', modal: boolean, selection: JSONEditorSelection | null }) : ContextMenuItem[] | undefined`.
- `onRenderContextMenu(items: ContextMenuItem[], context: { mode: 'tree' | 'text' | 'table', modal: boolean, selection: JSONEditorSelection | null }) : ContextMenuItem[] | false | undefined`.
Callback which can be used to make changes to the context menu items. New items can
be added, or existing items can be removed or reorganized. When the function
returns `undefined`, the original `items` will be applied. Using the context values `mode`, `modal` and `selection`, different actions can be taken depending on the mode of the editor, whether the editor is rendered inside a modal or not and the path of selection.
returns `undefined`, the original `items` will be applied and the context menu will be displayed when `readOnly` is `false`. When the function
returns `false`, the context menu will never be displayed. Using the context values `mode`, `modal` and `selection`, different actions can be taken depending on the mode of the editor, whether the editor is rendered inside a modal or not and the path of selection.

A menu item `ContextMenuItem` can be one of the following types:

Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/modes/JSONEditorRoot.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
let handleRenderContextMenu: OnRenderContextMenuInternal
$: handleRenderContextMenu = (items: ContextMenuItem[]) => {
return onRenderContextMenu(items, { mode, modal: insideModal, selection }) || items
return onRenderContextMenu(items, { mode, modal: insideModal, selection }) ?? (readOnly ? false: items)
}
export function patch(operations: JSONPatchDocument): JSONPatchResult {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/components/modes/tablemode/TableMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@
}
function handleContextMenu(event: Event) {
if (readOnly || isEditingSelection(documentState.selection)) {
if (!onRenderContextMenu([]) || isEditingSelection(documentState.selection)) {
return
}
Expand Down Expand Up @@ -1028,7 +1028,7 @@
}
function handleContextMenuFromTableMenu(event: MouseEvent) {
if (readOnly) {
if (!onRenderContextMenu([]) ) {
return
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/components/modes/treemode/TreeMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -1879,7 +1879,7 @@
}
function handleContextMenu(event?: Event) {
if (readOnly || isEditingSelection(documentState.selection)) {
if (!onRenderContextMenu([]) || isEditingSelection(documentState.selection)) {
return
}
Expand Down Expand Up @@ -1928,7 +1928,7 @@
}
function handleContextMenuFromTreeMenu(event: MouseEvent) {
if (readOnly) {
if (!onRenderContextMenu([])) {
return
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@ export type RenderContextMenuContext = RenderMenuContext & { selection: JSONEdit
export type OnRenderContextMenu = (
items: ContextMenuItem[],
context: RenderContextMenuContext
) => ContextMenuItem[] | undefined
export type OnRenderContextMenuInternal = (items: ContextMenuItem[]) => ContextMenuItem[]
) => ContextMenuItem[] | false | undefined
export type OnRenderContextMenuInternal = (items: ContextMenuItem[]) => ContextMenuItem[] | false
export type OnError = (error: Error) => void
export type OnFocus = () => void
export type OnBlur = () => void
Expand Down

0 comments on commit 93676eb

Please sign in to comment.