Skip to content

Commit

Permalink
chore: fix TypeScript issues (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Dec 8, 2023
1 parent 2168f05 commit a37f462
Show file tree
Hide file tree
Showing 17 changed files with 134 additions and 107 deletions.
2 changes: 2 additions & 0 deletions src/lib/components/JSONEditor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,8 @@
$: {
debug('mode changed to', mode)
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
if (mode === 'code') {
// check for 'code' is here for backward compatibility (deprecated since v0.4.0)
console.warn(
Expand Down
9 changes: 5 additions & 4 deletions src/lib/components/JSONEditor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { beforeEach, afterEach, test, describe, expect, vi } from 'vitest'
import '@testing-library/jest-dom'
import { render, screen } from '@testing-library/svelte'
import JSONEditor from './JSONEditor.svelte'
import { type Content, Mode } from '$lib/types.js'

describe('JSONEditor', () => {
const content = {
const content: Content = {
json: [{ id: 1 }, { id: 2, name: 'Joe' }, { id: 3 }]
}

Expand All @@ -27,7 +28,7 @@ describe('JSONEditor', () => {
test('render tree mode', () => {
const { container } = render(JSONEditor, {
props: {
mode: 'tree',
mode: Mode.tree,
content
}
})
Expand All @@ -41,7 +42,7 @@ describe('JSONEditor', () => {
test('render text mode', () => {
const { container } = render(JSONEditor, {
props: {
mode: 'text',
mode: Mode.text,
content
}
})
Expand All @@ -55,7 +56,7 @@ describe('JSONEditor', () => {
test('render table mode', () => {
const { container } = render(JSONEditor, {
props: {
mode: 'table',
mode: Mode.table,
content
}
})
Expand Down
10 changes: 5 additions & 5 deletions src/lib/components/controls/contextmenu/ContextMenu.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@
ArrowRight: 'Right'
}
function handleKeyDown(event) {
function handleKeyDown(event: KeyboardEvent & { currentTarget: EventTarget & HTMLDivElement }) {
const combo = keyComboFromEvent(event)
const direction: 'Up' | 'Down' | 'Left' | 'Right' | undefined = directionByCombo[combo]
if (direction && event.target) {
if (direction && event.currentTarget) {
event.preventDefault()
const buttons: HTMLButtonElement[] = Array.from(
refContextMenu.querySelectorAll('button:not([disabled])')
)
const nearest = findNearestElement({
const nearest = findNearestElement<HTMLButtonElement>({
allElements: buttons,
currentElement: event.target,
currentElement: event.currentTarget as unknown as HTMLButtonElement,
direction,
hasPrio: (element: HTMLButtonElement) => {
return element.getAttribute('data-type') !== 'jse-open-dropdown'
Expand All @@ -75,7 +75,7 @@
tabindex="-1"
class="jse-contextmenu"
bind:this={refContextMenu}
on:keydown={handleKeyDown}
on:keydown={(event) => handleKeyDown(event)}
>
{#each items as item}
{#if isMenuButton(item)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
export let selected: boolean
export let onContextMenu: OnContextMenu
function handleClick(event) {
let buttonElem = event.target
function handleClick(event: MouseEvent & { currentTarget: EventTarget & HTMLButtonElement }) {
let buttonElem: Element | null = event.currentTarget
while (buttonElem && buttonElem.nodeName !== 'BUTTON') {
buttonElem = buttonElem.parentNode
buttonElem = buttonElem.parentNode as Element
}
if (buttonElem) {
Expand All @@ -39,7 +39,7 @@
class="jse-context-menu-pointer"
class:jse-selected={selected}
title={CONTEXT_MENU_EXPLANATION}
on:click={handleClick}
on:click={(event) => handleClick(event)}
>
<Icon data={faCaretDown} />
</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
} catch (error) {
return {
path: undefined,
error
error: error as Error
}
}
}
Expand All @@ -64,7 +64,7 @@
}
}
function handleInput(event) {
function handleInput(event: Event & { currentTarget: EventTarget & HTMLInputElement }) {
inputPath = event.currentTarget.value
}
Expand All @@ -81,15 +81,15 @@
if (result.path !== undefined) {
onChange(result.path)
} else {
onError(result.error)
onError(result.error as Error)
}
}
}
function handleCopy() {
copyToClipBoard(inputPath)
copied = true
copiedTimer = setTimeout(() => (copied = false), copiedDelay)
copiedTimer = window.setTimeout(() => (copied = false), copiedDelay)
focus()
}
</script>
Expand All @@ -108,7 +108,7 @@
type="button"
class="jse-navigation-bar-validation-error"
use:tooltip={{
text: inputValidationError,
text: String(inputValidationError || ''),
...absolutePopupContext
}}
>
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/modes/tablemode/ColumnHeader.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import type { JSONPath } from 'immutable-json-patch'
import type { SortedColumn } from '$lib/types.js'
import { SortDirection } from '$lib/types.js'
import { stringifyJSONPath } from '../../../utils/pathUtils.js'
import { stringifyJSONPath } from '$lib/utils/pathUtils.js'
import Icon from 'svelte-awesome'
import { faCaretDown, faCaretUp } from '@fortawesome/free-solid-svg-icons'
import { isEmpty, isEqual } from 'lodash-es'
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/modes/tablemode/TableMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@
}
function findNextInside(path: JSONPath): JSONSelection {
const index = parseInt(path[0])
const index = parseInt(path[0], 10)
const nextPath = [String(index + 1), ...path.slice(1)]
return existsIn(json, nextPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
$: canEnforceString = canEditValue && !isObjectOrArray(focusValue)
$: enforceString =
selection != null
selection != null && focusValue !== undefined
? getEnforceString(
focusValue,
documentState.enforceStringMap,
Expand Down
17 changes: 10 additions & 7 deletions src/lib/components/modes/textmode/TextMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@
return true
} catch (err) {
onError(err)
onError(err as Error)
}
return false
Expand All @@ -305,7 +305,7 @@
return true
} catch (err) {
onError(err)
onError(err as Error)
}
return false
Expand All @@ -325,7 +325,7 @@
jsonStatus = JSON_STATUS_VALID
jsonParseError = null
} catch (err) {
onError(err)
onError(err as Error)
}
}
Expand Down Expand Up @@ -353,7 +353,7 @@
}
})
} catch (err) {
onError(err)
onError(err as Error)
}
}
Expand Down Expand Up @@ -396,7 +396,7 @@
}
})
} catch (err) {
onError(err)
onError(err as Error)
}
}
Expand Down Expand Up @@ -460,12 +460,15 @@
function handleSelectValidationError(validationError: ValidationError) {
debug('select validation error', validationError)
const richValidationError = toRichValidationError(validationError)
const { from, to } = toRichValidationError(validationError)
if (from === null || to === null) {
return
}
// we take "to" as head, not as anchor, because the scrollIntoView will
// move to the head, and when a large whole object is selected as a whole,
// we want to scroll to the start of the object and not the end
setSelection(richValidationError.from, richValidationError.to)
setSelection(from, to)
focus()
}
Expand Down
26 changes: 13 additions & 13 deletions src/lib/components/modes/textmode/menu/TextMenu.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@
import type { MenuItem, OnRenderMenuWithoutContext } from '$lib/types'
export let readOnly = false
export let onFormat
export let onCompact
export let onSort
export let onTransform
export let onToggleSearch
export let onUndo
export let onRedo
export let canUndo
export let canRedo
export let canFormat
export let canCompact
export let canSort
export let canTransform
export let onFormat: () => boolean
export let onCompact: () => boolean
export let onSort: () => void
export let onTransform: () => void
export let onToggleSearch: () => void
export let onUndo: () => void
export let onRedo: () => void
export let canUndo: boolean
export let canRedo: boolean
export let canFormat: boolean
export let canCompact: boolean
export let canSort: boolean
export let canTransform: boolean
export let onRenderMenu: OnRenderMenuWithoutContext
const searchItem: MenuItem = {
Expand Down
Loading

0 comments on commit a37f462

Please sign in to comment.