Skip to content

Commit b702a92

Browse files
feat(applet): improve long-string display in components panel (#419)
1 parent 064a411 commit b702a92

File tree

4 files changed

+43
-13
lines changed

4 files changed

+43
-13
lines changed

packages/applet/src/components/state/StateFieldViewer.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ const props = defineProps<{
2323
const STATE_FIELDS_LIMIT_SIZE = 30
2424
const limit = ref(STATE_FIELDS_LIMIT_SIZE)
2525
// display value
26-
const displayedValue = computed(() => formatInspectorStateValue(props.data.value))
26+
const displayedValue = computed(() => formatInspectorStateValue(props.data.value, false, {
27+
customClass: {
28+
string: 'max-w-120 truncate',
29+
},
30+
}))
2731
const type = computed(() => getInspectorStateValueType(props.data.value))
2832
const raw = computed(() => getRaw(props.data.value))
2933
const { expanded, toggleExpanded } = useToggleExpanded()

packages/devtools-kit/__tests__/component/format.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,16 @@ describe('format: toSubmit', () => {
124124
expect(serialized).toStrictEqual(value.target)
125125
})
126126
})
127+
128+
describe('format: customClass', () => {
129+
it.each([
130+
{ value: 'string-value', target: '<span class="custom-class">string-value</span>' },
131+
])('value: $value should be serialized to target with custom class', (value) => {
132+
const serialized = format.formatInspectorStateValue(value.value, false, {
133+
customClass: {
134+
string: 'custom-class',
135+
},
136+
})
137+
expect(serialized).toStrictEqual(value.target)
138+
})
139+
})

packages/devtools-kit/src/core/component/state/format.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ export function getInspectorStateValueType(value, raw = true) {
4646
}
4747
}
4848

49-
export function formatInspectorStateValue(value, quotes = false) {
49+
export function formatInspectorStateValue(value, quotes = false, options?: {
50+
// Support more types if needed
51+
customClass?: Partial<Record<'string', string>>
52+
}) {
53+
const { customClass } = options ?? {}
5054
let result
5155
const type = getInspectorStateValueType(value, false)
5256
if (type !== 'custom' && value?._custom)
@@ -58,7 +62,7 @@ export function formatInspectorStateValue(value, quotes = false) {
5862
}
5963
else if (type === 'custom') {
6064
// For digging out nested custom name.
61-
const nestedName = value._custom.value?._custom && formatInspectorStateValue(value._custom.value)
65+
const nestedName = value._custom.value?._custom && formatInspectorStateValue(value._custom.value, quotes, options)
6266
return nestedName || value._custom.displayText || value._custom.display
6367
}
6468
else if (type === 'array') {
@@ -72,21 +76,30 @@ export function formatInspectorStateValue(value, quotes = false) {
7276
}
7377
else if (typeof value === 'string') {
7478
const typeMatch = value.match(rawTypeRE)
75-
if (typeMatch)
76-
value = escape(typeMatch[1])
77-
78-
else if (quotes)
79-
value = `<span>"</span>${escape(value)}<span>"</span>`
79+
if (typeMatch) {
80+
value = escapeString(typeMatch[1])
81+
}
8082

81-
else
82-
value = escape(value)
83+
else if (quotes) {
84+
value = `<span>"</span>${
85+
customClass?.string ? `<span class=${customClass.string}>${escapeString(value)}</span>` : escapeString(value)
86+
}<span>"</span>`
87+
}
8388

84-
value = value.replace(/ /g, '&nbsp;')
85-
.replace(/\n/g, '<span>\\n</span>')
89+
else {
90+
value = customClass?.string
91+
? `<span class="${customClass?.string ?? ''}">${escapeString(value)}</span>`
92+
: escapeString(value)
93+
}
8694
}
8795
return value
8896
}
8997

98+
function escapeString(value: string) {
99+
return escape(value).replace(/ /g, '&nbsp;')
100+
.replace(/\n/g, '<span>\\n</span>')
101+
}
102+
90103
export function getRaw(value: InspectorState['value']): {
91104
value: object | string | number | boolean | null
92105
inherit: {} | { abstract: true }

packages/playground/applet/src/stores/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const useAppStore = defineStore('app', () => {
1515

1616
export const useCounterStore = defineStore('counter', () => {
1717
const count = ref(10)
18-
const name = ref('webfansplz!!!')
18+
const name = ref('webfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplzwebfansplz!!!')
1919
function increment() {
2020
count.value++
2121
}

0 commit comments

Comments
 (0)