Skip to content

Commit

Permalink
fix: onCreateMenu and onCreateContextMenu possibly applying mutated c…
Browse files Browse the repository at this point in the history
…hanges without returning
  • Loading branch information
josdejong committed Feb 27, 2024
1 parent 6108ba7 commit da8fd60
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
9 changes: 7 additions & 2 deletions src/lib/components/modes/JSONEditorRoot.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import TreeMode from './treemode/TreeMode.svelte'
import type { JSONPatchDocument, JSONPath } from 'immutable-json-patch'
import { isMenuSpace } from '$lib/typeguards.js'
import { cloneDeep } from 'lodash-es'
export let content: Content
export let selection: JSONEditorSelection | null
Expand Down Expand Up @@ -112,12 +113,16 @@
? modeMenuItems.concat(items) // menu is empty, readOnly mode
: modeMenuItems.concat(separatorMenuItem, items)
return onRenderMenu(updatedItems, { mode, modal: insideModal }) || updatedItems
const updatedItemsOriginal = cloneDeep(updatedItems) // the user may change updatedItems in the callback
return onRenderMenu(updatedItems, { mode, modal: insideModal }) || updatedItemsOriginal
}
let handleRenderContextMenu: OnRenderContextMenuInternal
$: handleRenderContextMenu = (items: ContextMenuItem[]) => {
return onRenderContextMenu(items, { mode, modal: insideModal, selection }) || items
const itemsOriginal = cloneDeep(items) // the user may change items in the callback
return onRenderContextMenu(items, { mode, modal: insideModal, selection }) || itemsOriginal
}
export function patch(operations: JSONPatchDocument): JSONPatchResult {
Expand Down
9 changes: 3 additions & 6 deletions src/routes/examples/custom_menu_buttons/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,10 @@
className: 'custom-copy-button'
}
const space: MenuSpace = {
type: 'space'
}
const itemsWithoutSpace = items.slice(0, items.length - 2)
const head = items.slice(0, items.length - 1)
const tail = items.slice(items.length - 1) // the tail contains space
return itemsWithoutSpace.concat([separator, customCopyButton, space])
return head.concat(separator, customCopyButton, tail)
}
</script>

Expand Down

0 comments on commit da8fd60

Please sign in to comment.