From 95de8a82e02e523c2d612540d6e5fb082c8ccec0 Mon Sep 17 00:00:00 2001 From: Fan Pei Date: Fri, 2 Feb 2024 17:52:56 +0900 Subject: [PATCH 1/4] feat: Extend getRaw to return customType --- .../component/state/__tests__/format.spec.ts | 19 +++++++------ .../src/core/component/state/format.ts | 28 ++++++++++++++----- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/packages/devtools-kit/src/core/component/state/__tests__/format.spec.ts b/packages/devtools-kit/src/core/component/state/__tests__/format.spec.ts index a63eb35f..af7d820b 100644 --- a/packages/devtools-kit/src/core/component/state/__tests__/format.spec.ts +++ b/packages/devtools-kit/src/core/component/state/__tests__/format.spec.ts @@ -1,7 +1,8 @@ import * as format from '../format' +import { customTypeEnums } from '../../types' import { INFINITY, NAN, NEGATIVE_INFINITY, UNDEFINED } from '../constants' -describe('format: displayText and rawValue can be calculated by formatInspectorStateValue, getRawValue', () => { +describe('format: displayText and rawValue can be calculated by formatInspectorStateValue, getRaw', () => { describe('type: literals', () => { // eslint-disable-next-line test/consistent-test-it test.each([ @@ -16,7 +17,7 @@ describe('format: displayText and rawValue can be calculated by formatInspectorS { literal: UNDEFINED, displayText: 'undefined' }, ])('type: %s', (value) => { const displayText = format.formatInspectorStateValue(value.literal) - const rawValue = format.getRawValue(value.literal).value + const rawValue = format.getRaw(value.literal).value expect(displayText).toBe(value.displayText) expect(rawValue).toBe(value.literal) @@ -26,7 +27,7 @@ describe('format: displayText and rawValue can be calculated by formatInspectorS it('type: plain object', () => { const value = { foo: 'bar' } const displayText = format.formatInspectorStateValue(value) - const rawValue = format.getRawValue(value).value + const rawValue = format.getRaw(value).value expect(displayText).toBe('Object') expect(rawValue).toEqual(value) @@ -35,7 +36,7 @@ describe('format: displayText and rawValue can be calculated by formatInspectorS it('type: array', () => { const value = ['foo', { bar: 'baz' }] const displayText = format.formatInspectorStateValue(value) - const rawValue = format.getRawValue(value).value + const rawValue = format.getRaw(value).value expect(displayText).toBe('Array[2]') expect(rawValue).toEqual(value) @@ -45,7 +46,7 @@ describe('format: displayText and rawValue can be calculated by formatInspectorS it('type: common custom', () => { const value = { _custom: { displayText: 'custom-display', value: Symbol(123) } } const displayText = format.formatInspectorStateValue(value) - const rawValue = format.getRawValue(value).value + const rawValue = format.getRaw(value).value expect(displayText).toBe(value._custom.displayText) expect(rawValue).toEqual(value._custom.value) @@ -62,7 +63,7 @@ describe('format: displayText and rawValue can be calculated by formatInspectorS } const displayText = format.formatInspectorStateValue(value) - const rawValue = format.getRawValue(value).value + const rawValue = format.getRaw(value).value expect(displayText).toBe(value._custom.value._custom.displayText) expect(rawValue).toEqual(value._custom.value._custom.value) @@ -87,8 +88,9 @@ describe('format: toEdit', () => { { value: { foo: NAN }, target: '{"foo":NaN}' }, { value: { foo: NEGATIVE_INFINITY }, target: '{"foo":-Infinity}' }, { value: { foo: UNDEFINED }, target: '{"foo":undefined}' }, + { value: '123', customType: 'bigint' as customTypeEnums, target: '123' }, ])('value: $value will be deserialized to target', (value) => { - const deserialized = format.toEdit(value.value) + const deserialized = format.toEdit(value.value, value.customType) expect(deserialized).toBe(value.target) }) }) @@ -113,8 +115,9 @@ describe('format: toSubmit', () => { { value: '{"foo":undefined}', target: {} }, // Regex test: The token in key field kept untouched. { value: '{"undefined": NaN }', target: { undefined: Number.NaN } }, + { value: '123', customType: 'bigint' as customTypeEnums, target: BigInt(123) }, ])('value: $value will be serialized to target', (value) => { - const serialized = format.toSubmit(value.value) + const serialized = format.toSubmit(value.value, value.customType) expect(serialized).toStrictEqual(value.target) }) }) diff --git a/packages/devtools-kit/src/core/component/state/format.ts b/packages/devtools-kit/src/core/component/state/format.ts index df4cf0c1..7a686176 100644 --- a/packages/devtools-kit/src/core/component/state/format.ts +++ b/packages/devtools-kit/src/core/component/state/format.ts @@ -1,4 +1,4 @@ -import { InspectorCustomState, InspectorState } from '../types' +import { InspectorCustomState, InspectorState, customTypeEnums } from '../types' import { INFINITY, NAN, NEGATIVE_INFINITY, UNDEFINED, rawTypeRE, specialTypeRE } from './constants' import { isPlainObject } from './is' import { escape, internalStateTokenToString, replaceStringToToken, replaceTokenToString } from './util' @@ -87,30 +87,44 @@ export function formatInspectorStateValue(value, quotes = false) { return value } -export function getRawValue(value: InspectorState['value']) { +export function getRaw(value: InspectorState['value']): { + value: object | string | number | boolean | null + inherit: {} | { abstract: true } + customType?: customTypeEnums +} { + let customType: customTypeEnums const isCustom = getInspectorStateValueType(value) === 'custom' let inherit = {} if (isCustom) { const data = value as InspectorCustomState const customValue = data._custom?.value + const currentCustomType = data._custom?.type const nestedCustom = typeof customValue === 'object' && customValue !== null && '_custom' in customValue - ? getRawValue(customValue) - : { inherit: undefined, value: undefined } + ? getRaw(customValue) + : { inherit: undefined, value: undefined, customType: undefined } inherit = nestedCustom.inherit || data._custom?.fields || {} value = nestedCustom.value || customValue as string + customType = nestedCustom.customType || currentCustomType as customTypeEnums } // @ts-expect-error @TODO: type if (value && value._isArray) // @ts-expect-error @TODO: type value = value.items - return { value, inherit } + // @ts-expect-error customType map be assigned as undefined. + return { value, inherit, customType } } -export function toEdit(value: unknown) { +export function toEdit(value: unknown, customType?: customTypeEnums) { + if (customType === 'bigint') + return value as string + return replaceTokenToString(JSON.stringify(value)) } -export function toSubmit(value: string) { +export function toSubmit(value: string, customType?: customTypeEnums) { + if (customType === 'bigint') + return BigInt(value) + return JSON.parse(replaceStringToToken(value), reviver) } From 24762a73cb2a5f9bb67b1a087e856ddacb4c267e Mon Sep 17 00:00:00 2001 From: Fan Pei Date: Fri, 2 Feb 2024 17:54:55 +0900 Subject: [PATCH 2/4] feat: Add type customTypeEnums --- .../src/core/component/state/custom.ts | 16 ++++++++-------- .../src/core/component/types/custom.ts | 1 + .../src/core/component/types/index.ts | 1 + 3 files changed, 10 insertions(+), 8 deletions(-) create mode 100644 packages/devtools-kit/src/core/component/types/custom.ts diff --git a/packages/devtools-kit/src/core/component/state/custom.ts b/packages/devtools-kit/src/core/component/state/custom.ts index 21cb1603..b7b7cb8c 100644 --- a/packages/devtools-kit/src/core/component/state/custom.ts +++ b/packages/devtools-kit/src/core/component/state/custom.ts @@ -1,4 +1,4 @@ -import type { InspectorState } from '../types' +import type { InspectorState, customTypeEnums } from '../types' import { getComponentName, getInstanceName } from '../general/util' import { processInstanceState } from './process' import { escape, getSetupStateType, toRaw } from './util' @@ -21,7 +21,7 @@ export function getFunctionDetails(func: Function) { const name = typeof func.name === 'string' ? func.name : '' return { _custom: { - type: 'function', + type: 'function' satisfies customTypeEnums, displayText: `function ${escape(name)}${args}`, tooltipText: string.trim() ? `
${string}
` : null, }, @@ -64,7 +64,7 @@ export function getSetDetails(val: Set) { const list = Array.from(val) return { _custom: { - type: 'set', + type: 'set' satisfies customTypeEnums, displayText: `Set[${list.length}]`, value: list, readOnly: true, @@ -120,7 +120,7 @@ function namedNodeMapToObject(map: NamedNodeMap) { export function getStoreDetails(store) { return { _custom: { - type: 'store', + type: 'store' satisfies customTypeEnums, displayText: 'Store', value: { state: store.state, @@ -179,7 +179,7 @@ export function getComponentDefinitionDetails(definition) { } return { _custom: { - type: 'component-definition', + type: 'component-definition' satisfies customTypeEnums, displayText: display, tooltipText: 'Component definition', ...definition.__file @@ -195,7 +195,7 @@ export function getHTMLElementDetails(value: HTMLElement) { try { return { _custom: { - type: 'HTMLElement', + type: 'HTMLElement' satisfies customTypeEnums, displayText: `<${value.tagName.toLowerCase()}>`, value: namedNodeMapToObject(value.attributes), }, @@ -204,7 +204,7 @@ export function getHTMLElementDetails(value: HTMLElement) { catch (e) { return { _custom: { - type: 'HTMLElement', + type: 'HTMLElement' satisfies customTypeEnums, displayText: `${String(value)}`, }, } @@ -232,7 +232,7 @@ export function getObjectDetails(object: Record) { if (typeof object.__asyncLoader === 'function') { return { _custom: { - type: 'component-definition', + type: 'component-definition' satisfies customTypeEnums, display: 'Async component definition', }, } diff --git a/packages/devtools-kit/src/core/component/types/custom.ts b/packages/devtools-kit/src/core/component/types/custom.ts new file mode 100644 index 00000000..f4269f14 --- /dev/null +++ b/packages/devtools-kit/src/core/component/types/custom.ts @@ -0,0 +1 @@ +export type customTypeEnums = 'function' | 'bigint' | 'map' | 'set' | 'store' | 'router' | 'component' | 'component-definition' | 'HTMLElement' | 'component-definition' diff --git a/packages/devtools-kit/src/core/component/types/index.ts b/packages/devtools-kit/src/core/component/types/index.ts index a0dcea7a..3c7d5038 100644 --- a/packages/devtools-kit/src/core/component/types/index.ts +++ b/packages/devtools-kit/src/core/component/types/index.ts @@ -2,3 +2,4 @@ export * from './state' export * from './tree' export * from './editor' export * from './bounding-rect' +export * from './custom' From 37c0dcb342b5ca3e6f1a6e008fa39306dc03dba3 Mon Sep 17 00:00:00 2001 From: Fan Pei Date: Fri, 2 Feb 2024 17:57:19 +0900 Subject: [PATCH 3/4] feat: Enable edit on bigint --- .../inspector/InspectorDataField/Actions.vue | 23 +++++++++++++------ .../InspectorDataField/EditInput.vue | 12 +++++----- .../inspector/InspectorStateField.vue | 20 ++++++++-------- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/packages/client/src/components/inspector/InspectorDataField/Actions.vue b/packages/client/src/components/inspector/InspectorDataField/Actions.vue index e57e465f..f6f3e73c 100644 --- a/packages/client/src/components/inspector/InspectorDataField/Actions.vue +++ b/packages/client/src/components/inspector/InspectorDataField/Actions.vue @@ -1,7 +1,7 @@ - -