|
1 |
| -<svelte:options immutable={true} /> |
| 1 | +import type { ContextMenuItem, DocumentState, JSONParser } from 'svelte-jsoneditor' |
| 2 | +import { |
| 3 | + faCheckSquare, |
| 4 | + faClone, |
| 5 | + faCopy, |
| 6 | + faCut, |
| 7 | + faPaste, |
| 8 | + faPen, |
| 9 | + faPlus, |
| 10 | + faSquare, |
| 11 | + faTrashCan |
| 12 | +} from '@fortawesome/free-solid-svg-icons' |
| 13 | +import { isKeySelection, isMultiSelection, isValueSelection } from 'svelte-jsoneditor' |
| 14 | +import { compileJSONPointer, getIn } from 'immutable-json-patch' |
| 15 | +import { getFocusPath, singleItemSelected } from '$lib/logic/selection' |
| 16 | +import { isObjectOrArray } from '$lib/utils/typeUtils' |
| 17 | +import { getEnforceString } from '$lib/logic/documentState' |
2 | 18 |
|
3 |
| -<script lang="ts"> |
4 |
| - import { |
5 |
| - faClone, |
6 |
| - faCopy, |
7 |
| - faCut, |
8 |
| - faPaste, |
9 |
| - faPen, |
10 |
| - faPlus, |
11 |
| - faTrashCan |
12 |
| - } from '@fortawesome/free-solid-svg-icons' |
13 |
| - import { compileJSONPointer, getIn } from 'immutable-json-patch' |
14 |
| - import { |
15 |
| - getFocusPath, |
16 |
| - isKeySelection, |
17 |
| - isMultiSelection, |
18 |
| - isValueSelection, |
19 |
| - singleItemSelected |
20 |
| - } from '$lib/logic/selection.js' |
21 |
| - import { isObjectOrArray } from '$lib/utils/typeUtils.js' |
22 |
| - import { faCheckSquare, faSquare } from '@fortawesome/free-regular-svg-icons' |
23 |
| - import type { |
24 |
| - ContextMenuItem, |
25 |
| - DocumentState, |
26 |
| - JSONParser, |
27 |
| - OnRenderContextMenuInternal |
28 |
| - } from '$lib/types' |
29 |
| - import { getEnforceString } from '$lib/logic/documentState.js' |
30 |
| - import ContextMenu from '../../../../components/controls/contextmenu/ContextMenu.svelte' |
| 19 | +export default function ({ |
| 20 | + json, |
| 21 | + documentState, |
| 22 | + parser, |
| 23 | + onEditValue, |
| 24 | + onEditRow, |
| 25 | + onToggleEnforceString, |
| 26 | + onCut, |
| 27 | + onCopy, |
| 28 | + onPaste, |
| 29 | + onRemove, |
| 30 | + onDuplicateRow, |
| 31 | + onInsertBeforeRow, |
| 32 | + onInsertAfterRow, |
| 33 | + onRemoveRow |
| 34 | +}: { |
| 35 | + json: unknown | undefined |
| 36 | + documentState: DocumentState |
| 37 | + parser: JSONParser |
| 38 | + onEditValue: () => void |
| 39 | + onEditRow: () => void |
| 40 | + onToggleEnforceString: () => void |
| 41 | + onCut: (indent: boolean) => void |
| 42 | + onCopy: (indent: boolean) => void |
| 43 | + onPaste: () => void |
| 44 | + onRemove: () => void |
| 45 | + onDuplicateRow: () => void |
| 46 | + onInsertBeforeRow: () => void |
| 47 | + onInsertAfterRow: () => void |
| 48 | + onRemoveRow: () => void |
| 49 | +}): ContextMenuItem[] { |
| 50 | + const selection = documentState.selection |
31 | 51 |
|
32 |
| - export let json: unknown | undefined |
33 |
| - export let documentState: DocumentState |
34 |
| - export let parser: JSONParser |
| 52 | + const hasJson = json !== undefined |
| 53 | + const hasSelection = !!selection |
| 54 | + const focusValue = |
| 55 | + json !== undefined && selection ? getIn(json, getFocusPath(selection)) : undefined |
35 | 56 |
|
36 |
| - export let showTip: boolean |
37 |
| -
|
38 |
| - export let onCloseContextMenu: () => void |
39 |
| - export let onRenderContextMenu: OnRenderContextMenuInternal |
40 |
| - export let onEditValue: () => void |
41 |
| - export let onEditRow: () => void |
42 |
| - export let onToggleEnforceString: () => void |
43 |
| - export let onCut: (indent: boolean) => void |
44 |
| - export let onCopy: (indent: boolean) => void |
45 |
| - export let onPaste: () => void |
46 |
| - export let onRemove: () => void |
47 |
| - export let onDuplicateRow: () => void |
48 |
| - export let onInsertBeforeRow: () => void |
49 |
| - export let onInsertAfterRow: () => void |
50 |
| - export let onRemoveRow: () => void |
51 |
| -
|
52 |
| - $: selection = documentState.selection |
53 |
| -
|
54 |
| - $: hasJson = json !== undefined |
55 |
| - $: hasSelection = !!selection |
56 |
| - $: focusValue = json !== undefined && selection ? getIn(json, getFocusPath(selection)) : undefined |
57 |
| -
|
58 |
| - $: hasSelectionContents = |
| 57 | + const hasSelectionContents = |
59 | 58 | hasJson &&
|
60 | 59 | (isMultiSelection(selection) || isKeySelection(selection) || isValueSelection(selection))
|
61 | 60 |
|
62 |
| - $: canEditValue = hasJson && selection != null && singleItemSelected(selection) |
63 |
| - $: canEnforceString = canEditValue && !isObjectOrArray(focusValue) |
| 61 | + const canEditValue = hasJson && selection != null && singleItemSelected(selection) |
| 62 | + const canEnforceString = canEditValue && !isObjectOrArray(focusValue) |
64 | 63 |
|
65 |
| - $: enforceString = |
| 64 | + const enforceString = |
66 | 65 | selection != null && focusValue !== undefined
|
67 | 66 | ? getEnforceString(
|
68 | 67 | focusValue,
|
|
72 | 71 | )
|
73 | 72 | : false
|
74 | 73 |
|
75 |
| - let defaultItems: ContextMenuItem[] |
76 |
| - $: defaultItems = [ |
| 74 | + return [ |
77 | 75 | { type: 'separator' },
|
78 | 76 | {
|
79 | 77 | type: 'row',
|
|
239 | 237 | ]
|
240 | 238 | }
|
241 | 239 | ]
|
242 |
| -
|
243 |
| - $: items = onRenderContextMenu(defaultItems) |
244 |
| -</script> |
245 |
| - |
246 |
| -<ContextMenu |
247 |
| - {items} |
248 |
| - {onCloseContextMenu} |
249 |
| - tip={showTip ? 'Tip: you can open this context menu via right-click or with Ctrl+Q' : undefined} |
250 |
| -/> |
| 240 | +} |
0 commit comments