Skip to content

Commit

Permalink
chore: fix TypeScript issues
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Oct 27, 2023
1 parent 7d1eb02 commit 9a106b6
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 79 deletions.
2 changes: 1 addition & 1 deletion src/lib/assets/jump.js/src/jump.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export const createJump = () => {

// API

function jump(target: Element, options: JumpOptions = {}) {
function jump(target: Element | number | string, options: JumpOptions = {}) {
// resolve options, or use defaults
duration = 1000
offset = options.offset || 0
Expand Down
7 changes: 6 additions & 1 deletion src/lib/components/JSONEditor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import memoizeOne from 'memoize-one'
import type { Callbacks, Component } from 'svelte-simple-modal/types/Modal.svelte'
import ModalRef from '../components/modals/ModalRef.svelte'
import type { Open } from 'svelte-simple-modal/types/Modal.svelte.js'
// TODO: document how to enable debugging in the readme: localStorage.debug="jsoneditor:*", then reload
const debug = createDebug('jsoneditor:JSONEditor')
Expand Down Expand Up @@ -103,7 +104,7 @@
let instanceId = uniqueId()
let hasFocus = false
let refJSONEditorRoot: JSONEditorRoot
let open // svelte-simple-modal context open(...)
let open: Open // svelte-simple-modal context open(...)
let jsoneditorModalState: {
component: Component
callbacks: Partial<Callbacks>
Expand Down Expand Up @@ -260,12 +261,16 @@
}
export async function updateProps(props: JSONEditorPropsOptional): Promise<void> {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
this.$set(props)
await tick() // await rerender
}
export async function destroy() {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
this.$destroy()
await tick() // await destroying
Expand Down
27 changes: 9 additions & 18 deletions src/lib/components/modals/SortModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { getNestedPaths } from '$lib/utils/arrayUtils.js'
import { pathToOption, stringifyJSONPath } from '../../utils/pathUtils.js'
import { sortJson } from '$lib/logic/sort.js'
import { sortModalState } from './sortModalState.js'
import { sortModalStates } from './sortModalStates'
import type { JSONPath, JSONValue } from 'immutable-json-patch'
import { compileJSONPointer, getIn } from 'immutable-json-patch'
import { createDebug } from '$lib/utils/debug.js'
Expand Down Expand Up @@ -41,42 +41,33 @@
}
const directions = [asc, desc]
let selectedProperty =
(sortModalState[stateId] && sortModalState[stateId].selectedProperty) || undefined
let selectedDirection =
(sortModalState[stateId] && sortModalState[stateId].selectedDirection) || asc
let selectedProperty = sortModalStates[stateId]?.selectedProperty
let selectedDirection = sortModalStates[stateId]?.selectedDirection || asc
let sortError: string | undefined = undefined
$: {
// if there is only one option, select it and do not render the select box
if (selectedProperty === undefined && properties && properties.length === 1) {
selectedProperty = properties[0]
}
}
$: {
// remember the selected values for the next time we open the SortModal
// just in memory, not persisted
sortModalState[stateId] = {
sortModalStates[stateId] = {
selectedProperty,
selectedDirection
}
debug('store state in memory', stateId, sortModalState[stateId])
debug('store state in memory', stateId, sortModalStates[stateId])
}
function handleSort() {
try {
sortError = undefined
const itemPath: JSONPath | undefined = selectedProperty?.value
const itemPath: JSONPath = selectedProperty?.value || properties?.[0]?.value || []
const direction = selectedDirection?.value
const operations = sortJson(json, rootPath, itemPath, direction)
onSort({ operations, rootPath, itemPath, direction })
close()
} catch (err) {
sortError = err.toString()
sortError = String(err)
}
}
Expand Down Expand Up @@ -107,7 +98,7 @@
/>
</td>
</tr>
{#if jsonIsArray && (properties.length > 1 || selectedProperty === undefined)}
{#if jsonIsArray && ((properties && properties?.length > 1) || selectedProperty === undefined)}
<tr>
<th>Property</th>
<td>
Expand Down Expand Up @@ -143,7 +134,7 @@
class="jse-primary"
on:click={handleSort}
use:focus
disabled={jsonIsArray ? !selectedProperty : false}
disabled={jsonIsArray && (properties && properties?.length > 1) ? !selectedProperty : false}
>
Sort
</button>
Expand Down
52 changes: 32 additions & 20 deletions src/lib/components/modals/TransformModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import type { JSONPath, JSONValue } from 'immutable-json-patch'
import { compileJSONPointer, getIn } from 'immutable-json-patch'
import { stringifyJSONPath } from '$lib/utils/pathUtils.js'
import { transformModalState } from './transformModalState.js'
import { transformModalStates, transformModalStateShared } from './transformModalStates.js'
import TransformWizard from './TransformWizard.svelte'
import TransformModalHeader from './TransformModalHeader.svelte'
import AbsolutePopup from './popup/AbsolutePopup.svelte'
Expand Down Expand Up @@ -53,18 +53,20 @@
export let onTransform: OnPatch
let selectedJson: JSONValue | undefined
$: selectedJson = getIn(json, rootPath)
$: selectedContent = { json: selectedJson }
let selectedContent: Content
$: selectedContent = selectedJson ? { json: selectedJson } : { text: '' }
const { close } = getContext<Context>('simple-modal')
const stateId = `${id}:${compileJSONPointer(rootPath)}`
const state = transformModalState[stateId] || {}
const state = transformModalStates[stateId] || {}
// showWizard is not stored inside a stateId
let showWizard = transformModalState.showWizard !== false
let showOriginal = transformModalState.showOriginal !== false
let showWizard = transformModalStateShared.showWizard !== false
let showOriginal = transformModalStateShared.showOriginal !== false
let queryOptions = state.queryOptions || {}
let query =
Expand All @@ -88,17 +90,24 @@
debug('updateQueryByWizard', { queryOptions, query, isManual })
}
function handleChangeQuery(event) {
query = event.target.value
function handleChangeQuery(event: Event) {
query = (event.target as HTMLTextAreaElement).value
isManual = true
debug('handleChangeQuery', { query, isManual })
}
function previewTransform(json: JSONValue, query: string) {
function previewTransform(json: JSONValue | undefined, query: string) {
if (json === undefined) {
previewContent = { text: '' }
previewError = 'Error: No JSON'
return
}
try {
debug('previewTransform', {
query
})
const jsonTransformed = getSelectedQueryLanguage(queryLanguageId).executeQuery(
json,
query,
Expand All @@ -122,17 +131,23 @@
$: {
// remember the selected values for the next time we open the SortModal
// just in memory, not persisted
transformModalState[stateId] = {
transformModalStates[stateId] = {
queryOptions,
query,
queryLanguageId,
isManual
}
debug('store state in memory', stateId, transformModalState[stateId])
debug('store state in memory', stateId, transformModalStates[stateId])
}
function handleTransform() {
if (selectedJson === undefined) {
previewContent = { text: '' }
previewError = 'Error: No JSON'
return
}
try {
debug('handleTransform', { query })
const jsonTransformed = getSelectedQueryLanguage(queryLanguageId).executeQuery(
Expand Down Expand Up @@ -163,14 +178,14 @@
showWizard = !showWizard
// not stored inside a stateId
transformModalState.showWizard = showWizard
transformModalStateShared.showWizard = showWizard
}
function toggleShowOriginal() {
showOriginal = !showOriginal
// not stored inside a stateId
transformModalState.showOriginal = showOriginal
transformModalStateShared.showOriginal = showOriginal
}
function focus(element: HTMLElement) {
Expand Down Expand Up @@ -236,12 +251,9 @@
<div class="jse-label">
<div class="jse-label-inner">Query</div>
</div>
<textarea
class="jse-query"
spellcheck="false"
value={query}
on:input={handleChangeQuery}
/>
<textarea class="jse-query" spellcheck="false" on:input={handleChangeQuery}
>{query}</textarea
>
</div>
<div class="jse-data-contents" class:jse-hide-original-data={!showOriginal}>
<div class="jse-original-data" class:jse-hide={!showOriginal}>
Expand All @@ -266,7 +278,7 @@
{parser}
{parseMemoizeOne}
{onRenderValue}
onRenderMenu={noop}
onRenderMenu={() => undefined}
onError={console.error}
onChange={noop}
onChangeMode={noop}
Expand Down Expand Up @@ -300,7 +312,7 @@
{parser}
{parseMemoizeOne}
{onRenderValue}
onRenderMenu={noop}
onRenderMenu={() => undefined}
onError={console.error}
onChange={noop}
onChangeMode={noop}
Expand Down
1 change: 0 additions & 1 deletion src/lib/components/modals/sortModalState.js

This file was deleted.

8 changes: 8 additions & 0 deletions src/lib/components/modals/sortModalStates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { NumberOption, PathOption } from '$lib/types.js'

export interface SortModalState {
selectedProperty: PathOption
selectedDirection: NumberOption
}

export const sortModalStates: Record<string, SortModalState> = {}
10 changes: 0 additions & 10 deletions src/lib/components/modals/transformModalState.ts

This file was deleted.

20 changes: 20 additions & 0 deletions src/lib/components/modals/transformModalStates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { QueryLanguageOptions } from '$lib/types.js'

export interface TransformModalState {
queryLanguageId: string
queryOptions: QueryLanguageOptions
query: string
isManual: boolean
}

export const transformModalStates: Record<string, TransformModalState> = {}

export interface TransformModalStateShared {
showWizard: boolean
showOriginal: boolean
}

export const transformModalStateShared: TransformModalStateShared = {
showWizard: true,
showOriginal: true
}
4 changes: 3 additions & 1 deletion src/lib/components/modes/JSONEditorRoot.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
// check for 'code' mode is here for backward compatibility (deprecated since v0.4.0)
className:
'jse-group-button jse-first' +
(mode === Mode.text || mode === 'code' ? ' jse-selected' : ''),
(mode === Mode.text || (mode as string) === 'code' ? ' jse-selected' : ''),
onClick: () => onChangeMode(Mode.text)
},
{
Expand Down Expand Up @@ -127,6 +127,8 @@
if (refTextMode) {
return refTextMode.patch(operations)
}
throw new Error(`Method patch is not available in mode "${mode}"`)
}
export function expand(callback?: OnExpand): void {
Expand Down
9 changes: 7 additions & 2 deletions src/lib/components/modes/tablemode/TableMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@
return false
}
function handleContextMenuFromTableMenu(event) {
function handleContextMenuFromTableMenu(event: Event) {
if (readOnly) {
return
}
Expand Down Expand Up @@ -1769,7 +1769,7 @@
{#if showRefreshButton}
<th class="jse-table-cell jse-table-cell-header">
<RefreshColumnHeader
count={json.length}
count={Array.isArray(json) ? json.length : 0}
{maxSampleCount}
onRefresh={() => (maxSampleCount = Infinity)}
/>
Expand Down Expand Up @@ -1867,6 +1867,7 @@
{
icon: faWrench,
text: 'Paste as JSON instead',
title: 'Paste the text as JSON instead of a single value',
// We use mousedown here instead of click: this message pops up
// whilst the user is editing a value. When clicking this button,
// the actual value is applied and the event is not propagated
Expand All @@ -1875,6 +1876,7 @@
},
{
text: 'Leave as is',
title: 'Keep the pasted content as a single value',
onClick: handleClearPastedJson
}
]}
Expand All @@ -1894,11 +1896,13 @@
{
icon: faCheck,
text: 'Ok',
title: 'Accept the repaired document',
onClick: acceptAutoRepair
},
{
icon: faCode,
text: 'Repair manually instead',
title: 'Leave the document unchanged and repair it manually instead',
onClick: handleRequestRepair
}
]
Expand All @@ -1917,6 +1921,7 @@
{
icon: faCode,
text: 'Repair manually',
title: 'Open the document in "code" mode and repair it manually',
onClick: handleRequestRepair
}
]
Expand Down
Loading

0 comments on commit 9a106b6

Please sign in to comment.