Skip to content

Commit

Permalink
fix: typescript issues
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Feb 2, 2024
1 parent 73cfc50 commit e757845
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/lib/components/modes/treemode/JSONValue.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

<script lang="ts">
import type { JSONEditorContext, JSONSelection, SearchResultItem } from '$lib/types.js'
import { isSvelteActionRenderer } from '$lib/typeguards.js'
import type { JSONPath } from 'immutable-json-patch'
import { isEditingSelection, isValueSelection } from '$lib/logic/selection.js'
import { isSvelteActionRenderer } from '$lib/typeguards.js'
export let path: JSONPath
export let value: unknown
Expand Down
2 changes: 1 addition & 1 deletion src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ export interface SvelteComponentRenderer {
}

export interface SvelteActionRenderer {
action: Action
action: Action<HTMLElement, Record<string, unknown>>
props: Record<string, unknown>
}

Expand Down
43 changes: 29 additions & 14 deletions src/routes/components/EvaluatorAction.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { createValueSelection, type OnSelect } from 'svelte-jsoneditor'
import { createValueSelection, type OnJSONSelect } from 'svelte-jsoneditor'
import type { Action } from 'svelte/action'
import type { JSONPath } from 'immutable-json-patch'
import { type JSONPath } from 'immutable-json-patch'

export interface EvaluatorProps {
export interface EvaluatorActionProps {
value: unknown
path: JSONPath
readOnly: boolean
onSelect: OnSelect
onSelect: OnJSONSelect
}

export const EvaluatorAction: Action<HTMLDivElement, EvaluatorProps> = (node, props) => {
function evaluate(expr: string) {
const result = expr
.split('+')
.map((value) => parseFloat(value.trim()))
.reduce((a, b) => a + b)
export const EvaluatorAction: Action<HTMLDivElement, Record<string, unknown>> = (
node,
initialProps
) => {
let props = toEvaluatorProps(initialProps)

return `The result of "${expr}" is "${result}" (double-click to edit)`
function updateResult() {
node.innerText = evaluate(String(props.value))
}

function handleValueDoubleClick(event: MouseEvent) {
Expand All @@ -30,14 +30,29 @@ export const EvaluatorAction: Action<HTMLDivElement, EvaluatorProps> = (node, pr
}

node.addEventListener('dblclick', handleValueDoubleClick)
node.innerText = evaluate(String(props.value))
updateResult()

return {
update: (props) => {
node.innerText = evaluate(String(props.value))
update: (updatedProps) => {
props = toEvaluatorProps(updatedProps)
updateResult()
},
destroy: () => {
node.removeEventListener('dblclick', handleValueDoubleClick)
}
}
}

function evaluate(expr: string) {
const result = expr
.split('+')
.map((value) => parseFloat(value.trim()))
.reduce((a, b) => a + b)

return `The result of "${expr}" is "${result}" (double-click to edit)`
}

function toEvaluatorProps(props: Record<string, unknown>): EvaluatorActionProps {
// you can add validations and typeguards here if needed
return props as unknown as EvaluatorActionProps
}
14 changes: 10 additions & 4 deletions src/routes/examples/custom_value_renderer/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<script>
import { EnumValue, JSONEditor, renderValue } from 'svelte-jsoneditor'
<script lang="ts">
import {
EnumValue,
JSONEditor,
renderValue,
type RenderValueComponentDescription,
type RenderValueProps
} from 'svelte-jsoneditor'
import ReadonlyPassword from '../../components/ReadonlyPassword.svelte'
import { EvaluatorAction } from '../../components/EvaluatorAction.ts'
import { EvaluatorAction } from '../../components/EvaluatorAction'
let content = {
text: undefined, // can be used to pass a stringified JSON document instead
Expand All @@ -20,7 +26,7 @@
{ value: 'other', text: 'Other' }
]
function onRenderValue(props) {
function onRenderValue(props: RenderValueProps): RenderValueComponentDescription[] {
const { path, value, readOnly, parser, isEditing, selection, onSelect, onPatch } = props
const key = props.path[props.path.length - 1]
Expand Down

0 comments on commit e757845

Please sign in to comment.