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

feat: onRenderContextMenu context selection #376

Merged
merged 5 commits into from
Jan 10, 2024
Merged
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,10 @@ const editor = new JSONEditor({
}
```

- `onRenderContextMenu(items: ContextMenuItem[], context: { mode: 'tree' | 'text' | 'table', modal: boolean }) : ContextMenuItem[] | undefined`.
- `onRenderContextMenu(items: ContextMenuItem[], context: { mode: 'tree' | 'text' | 'table', modal: boolean, selection: JSONEditorSelection | null }) : ContextMenuItem[] | 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` and `modal`, different actions can be taken depending on the mode of the editor and whether the editor is rendered inside a modal or not.
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.

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

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"@codemirror/view": "^6.22.1",
"@fortawesome/free-regular-svg-icons": "^6.5.1",
"@fortawesome/free-solid-svg-icons": "^6.5.1",
"@lezer/highlight": "^1.2.0",
"@replit/codemirror-indentation-markers": "^6.5.0",
"ajv": "^8.12.0",
"codemirror-wrapped-line-indent": "^1.0.0",
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 }) || items
return onRenderContextMenu(items, { mode, modal: insideModal, selection }) || items
}

export function patch(operations: JSONPatchDocument): JSONPatchResult {
Expand Down
1 change: 1 addition & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ export type OnContextMenu = (contextMenuProps: AbsolutePopupOptions) => void
export type RenderMenuContext = {
mode: 'tree' | 'text' | 'table'
modal: boolean
selection: JSONEditorSelection | null
}
export type OnRenderMenu = (items: MenuItem[], context: RenderMenuContext) => MenuItem[] | undefined
export type OnRenderMenuInternal = (items: MenuItem[]) => MenuItem[]
Expand Down
6 changes: 6 additions & 0 deletions src/routes/development/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import {
type Content,
type ContextMenuItem,
createAjvValidator,
createValueSelection,
EditableValue,
Expand Down Expand Up @@ -377,6 +378,10 @@
console.log('onChangeQueryLanguage', newQueryLanguageId)
queryLanguageId = newQueryLanguageId
}
function onRenderContextMenu(item: ContextMenuItem[], content: RenderMenuContext) {
console.log('onRenderContextMenu', item, content)
return item
}

function openInWindow() {
const popupWindow = window.open(
Expand Down Expand Up @@ -773,6 +778,7 @@
onChange={onChangeTree}
onSelect={onSelectTree}
onRenderValue={$useCustomValueRenderer ? customRenderValue : renderValue}
{onRenderContextMenu}
{onChangeMode}
onFocus={() => console.log('onFocus tree')}
onBlur={() => console.log('onBlur tree', { content: refTreeEditor?.get() })}
Expand Down