File size: 19,706 Bytes
4d70170 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 |
<script lang="ts">
/* eslint-disable vue/no-unused-refs */
import { defineComponent, toRaw } from 'vue'
import {
BridgeEvents,
copyToClipboard,
isPlainObject,
openInEditor,
sortByKey,
} from '@vue-devtools/shared-utils'
import DataFieldEdit from '@front/mixins/data-field-edit'
import { formattedValue, valueDetails, valueType } from '@front/util/format'
import { getBridge } from '../bridge'
function subFieldCount(value) {
if (Array.isArray(value)) {
return value.length
}
else if (value && typeof value === 'object') {
return Object.keys(value).length
}
else {
return 0
}
}
export default defineComponent({
name: 'DataField',
mixins: [
DataFieldEdit,
],
props: {
field: {
type: Object,
required: true,
},
depth: {
type: Number,
required: true,
},
path: {
type: String,
required: true,
},
forceCollapse: {
type: String,
default: null,
},
isStateField: {
type: Boolean,
default: false,
},
},
emits: ['editState'],
data() {
return {
contextMenuOpen: false,
limit: 20,
expanded: false,
}
},
computed: {
depthMargin(): number {
return (this.depth + 1) * 14 + 10
},
valueType(): string {
return valueType(this.field.value)
},
interpretedValueType(): string {
return valueType(this.field.value, false)
},
valueDetails(): string {
return valueDetails(this.field.value)
},
nativeValueType(): string {
return typeof this.field.value
},
isExpandableType(): boolean {
let value = this.field.value
if (this.valueType === 'custom') {
value = value._custom.value
}
const closed = this.fieldOptions.closed
const closedDefined = typeof closed !== 'undefined'
return (!closedDefined
&& (
Array.isArray(value)
|| isPlainObject(value)
))
|| (
closedDefined
&& !closed
)
},
formattedValue(): string {
const value = this.field.value
const objectType = value?._custom?.objectType || this.field.objectType
if (objectType === 'Reactive') {
return 'Reactive'
}
else if (this.fieldOptions.abstract) {
return ''
}
else {
let result = `<span class="value-formatted-ouput">${formattedValue(value)}</span>`
if (objectType) {
result += ` <span class="text-gray-500">(${objectType})</span>`
}
return result
}
},
rawValue(): any {
let value = this.field.value
// CustomValue API
const isCustom = this.valueType === 'custom'
let inherit = {}
if (isCustom) {
inherit = value._custom.fields || {}
value = value._custom.value
}
if (value && value._isArray) {
value = value.items
}
return { value, inherit }
},
formattedSubFields(): any[] {
let { value, inherit } = this.rawValue
if (Array.isArray(value)) {
return value.slice(0, this.limit).map((item, i) => ({
key: i,
value: item,
...inherit,
}))
}
else if (typeof value === 'object') {
value = Object.keys(value).map(key => ({
key,
value: value[key],
...inherit,
}))
if (this.valueType !== 'custom') {
value = sortByKey(value)
}
}
return value.slice(0, this.limit)
},
subFieldCount(): number {
const { value } = this.rawValue
return subFieldCount(value)
},
valueTooltip(): string {
const type = this.valueType
if (this.field.raw) {
return `<span class="font-mono">${this.field.raw}</span>`
}
else if (type === 'custom') {
return this.field.value._custom.tooltip
}
else if (type.indexOf('native ') === 0) {
return type.substring('native '.length)
}
else {
return null
}
},
fieldOptions(): any {
if (this.valueType === 'custom') {
return Object.assign({}, this.field, this.field.value._custom)
}
else {
return this.field
}
},
editErrorMessage(): string {
if (!this.valueValid) {
return 'Invalid value (must be valid JSON)'
}
else if (!this.keyValid) {
if (this.duplicateKey) {
return 'Duplicate key'
}
else {
return 'Invalid key'
}
}
return ''
},
valueClass(): string[] {
const cssClass = [this.valueType, `raw-${this.nativeValueType}`]
if (this.valueType === 'custom') {
const value = this.field.value
value._custom.type && cssClass.push(`type-${value._custom.type}`)
value._custom.class && cssClass.push(value._custom.class)
}
return cssClass
},
displayedKey(): string {
let key = this.field.key
if (typeof key === 'string') {
key = key.replace('__vue__', '')
}
return key
},
customActions(): { icon: string, tooltip?: string }[] {
return this.field.value?._custom?.actions ?? []
},
},
watch: {
forceCollapse: {
handler(value) {
if (value === 'expand' && this.depth < 4) {
this.expanded = true
}
else if (value === 'collapse') {
this.expanded = false
}
},
immediate: true,
},
},
created() {
const value = this.field.value && this.field.value._custom ? this.field.value._custom.value : this.field.value
this.expanded = this.depth === 0 && this.field.key !== '$route' && (subFieldCount(value) < 12)
},
methods: {
copyValue() {
copyToClipboard(this.field.value)
},
copyPath() {
copyToClipboard(this.path)
},
onClick(event) {
// Cancel if target is interactive
if (event.target.tagName === 'INPUT' || event.target.className.includes('button')) {
return
}
// CustomValue API `file`
if (this.valueType === 'custom' && this.fieldOptions.file) {
return openInEditor(this.fieldOptions.file)
}
if (this.valueType === 'custom' && this.fieldOptions.type === '$refs') {
if (this.$isChrome) {
const evl = `inspect(window.__VUE_DEVTOOLS_INSTANCE_MAP__.get("${this.fieldOptions.uid}").$refs["${this.fieldOptions.key}"])`
chrome.devtools.inspectedWindow.eval(evl)
}
else {
// eslint-disable-next-line no-alert
window.alert('DOM inspection is not supported in this shell.')
}
}
// Default action
this.toggle()
},
toggle() {
if (this.isExpandableType) {
this.expanded = !this.expanded
!this.expanded && this.cancelCurrentEdition()
}
},
hyphen: v => v.replace(/\s/g, '-'),
onContextMenuMouseEnter() {
clearTimeout(this.$_contextMenuTimer)
},
onContextMenuMouseLeave() {
clearTimeout(this.$_contextMenuTimer)
this.$_contextMenuTimer = setTimeout(() => {
this.contextMenuOpen = false
}, 4000)
},
showMoreSubfields() {
this.limit += 20
},
logToConsole(level = 'log') {
getBridge().send(BridgeEvents.TO_BACK_LOG, {
level,
value: toRaw(this.field.value),
revive: true,
})
},
executeCustomAction(index: number) {
getBridge().send(BridgeEvents.TO_BACK_CUSTOM_STATE_ACTION, {
value: toRaw(this.field.value),
actionIndex: index,
})
},
},
renderError: null,
})
</script>
<template>
<div class="data-field">
<VTooltip
:style="{ marginLeft: `${depth * 14}px` }"
:disabled="!field.meta"
:class="{
'force-toolbar z-10': contextMenuOpen || editing,
}"
class="self"
placement="left"
:distance="0"
:skidding="24"
@click="onClick"
@mouseenter="onContextMenuMouseEnter"
@mouseleave="onContextMenuMouseLeave"
>
<span
v-show="isExpandableType"
:class="{ rotated: expanded }"
class="arrow right"
/>
<span
v-if="editing && renamable"
>
<input
ref="keyInput"
v-model="editedKey"
class="edit-input key-input"
:class="{ error: !keyValid }"
@keydown.esc.capture.stop.prevent="cancelEdit()"
@keydown.enter="submitEdit()"
>
</span>
<span
v-else
v-tooltip="fieldOptions.abstract && {
content: valueTooltip,
html: true,
}"
:class="{ abstract: fieldOptions.abstract }"
class="key text-purple-700 dark:text-purple-300"
>{{ displayedKey }}</span><span
v-if="!fieldOptions.abstract"
class="colon"
>:</span>
<span
v-if="editing"
class="edit-overlay"
>
<input
ref="editInput"
v-model="editedValue"
class="edit-input value-input text-black"
:class="{ error: !valueValid }"
list="special-tokens"
:type="inputType"
@keydown.esc.capture.stop.prevent="cancelEdit()"
@keydown.enter="submitEdit()"
>
<span class="actions">
<VueIcon
v-if="!editValid"
v-tooltip="editErrorMessage"
class="small-icon warning"
icon="warning"
/>
<template v-else>
<VueButton
v-tooltip="{
content: $t('DataField.edit.cancel.tooltip'),
html: true,
}"
class="icon-button flat"
icon-left="cancel"
@click="cancelEdit()"
/>
<VueButton
v-tooltip="{
content: $t('DataField.edit.submit.tooltip'),
html: true,
}"
class="icon-button flat"
icon-left="save"
@click="submitEdit()"
/>
</template>
</span>
</span>
<template v-else>
<!-- eslint-disable vue/no-v-html -->
<span
v-tooltip="{
content: valueTooltip,
html: true,
}"
:class="valueClass"
class="value"
@dblclick="openEdit()"
v-html="formattedValue"
/>
<!-- eslint-enable vue/no-v-html -->
<span class="actions">
<VueDropdown
v-if="valueDetails"
>
<template #trigger>
<VueButton
v-tooltip="'More details'"
class="edit-value icon-button flat"
icon-left="info"
/>
</template>
<template #default>
<div class="px-4 py-2 flex flex-col max-h-48 overflow-auto relative">
<div class="flex items-center fixed top-0 right-0">
<VueButton
v-close-popper
class="edit-value icon-button flat"
icon-left="close"
/>
</div>
<div class="whitespace-pre-wrap max-w-lg break-words text-xs font-mono">{{ valueDetails }}</div>
</div>
</template>
</VueDropdown>
<VueButton
v-for="(action, index) of customActions"
:key="index"
v-tooltip="action.tooltip"
class="icon-button flat"
:icon-left="action.icon"
@click="executeCustomAction(index)"
/>
<VueButton
v-if="valueType === 'native Error'"
v-tooltip="'Log error to console'"
class=" icon-button flat"
icon-left="input"
@click="logToConsole('error')"
/>
<VueButton
v-if="isValueEditable"
v-tooltip="'Edit value'"
class="edit-value icon-button flat"
icon-left="edit"
@click="openEdit()"
/>
<template v-if="quickEdits">
<VueButton
v-for="(info, index) of quickEdits"
:key="index"
v-tooltip="{
content: info.title || 'Quick edit',
html: true,
}"
:class="info.class"
:icon-left="info.icon"
class="quick-edit icon-button flat"
@click="quickEdit(info, $event)"
/>
</template>
<VueButton
v-if="isSubfieldsEditable && !addingValue"
v-tooltip="'Add new value'"
class="add-value icon-button flat"
icon-left="add_circle"
@click="addNewValue()"
/>
<VueButton
v-if="removable"
v-tooltip="'Remove value'"
class="remove-field icon-button flat"
icon-left="delete"
@click="removeField()"
/>
<!-- Context menu -->
<VueDropdown
v-model="contextMenuOpen"
>
<template #trigger>
<VueButton
icon-left="more_vert"
class="icon-button flat"
/>
</template>
<div
class="context-menu-dropdown"
@mouseenter="onContextMenuMouseEnter"
@mouseleave="onContextMenuMouseLeave"
>
<VueDropdownButton
icon-left="flip_to_front"
@click="copyValue"
>
{{ $t('DataField.contextMenu.copyValue') }}
</VueDropdownButton>
<VueDropdownButton
icon-left="flip_to_front"
@click="copyPath"
>
{{ $t('DataField.contextMenu.copyPath') }}
</VueDropdownButton>
</div>
</VueDropdown>
</span>
</template>
<template #popper>
<div
v-if="field.meta"
class="meta"
>
<div
v-for="(val, key) in field.meta"
:key="key"
class="meta-field"
>
<span class="key">{{ key }}</span>
<span class="value">{{ val }}</span>
</div>
</div>
</template>
</VTooltip>
<div
v-if="expanded && isExpandableType"
class="children"
>
<DataField
v-for="subField in formattedSubFields"
:key="subField.key"
:field="subField"
:parent-field="field"
:depth="depth + 1"
:path="`${path}.${subField.key}`"
:editable="isEditable"
:removable="isSubfieldsEditable"
:renamable="editable && valueType === 'plain-object'"
:force-collapse="forceCollapse"
:is-state-field="isStateField"
@edit-state="(path, payload) => $emit('editState', path, payload)"
/>
<VueButton
v-if="subFieldCount > limit"
v-tooltip="'Show more'"
:style="{ marginLeft: `${depthMargin}px` }"
icon-left="more_horiz"
class="icon-button flat more"
@click="showMoreSubfields()"
/>
<DataField
v-if="isSubfieldsEditable && addingValue"
ref="newField"
:field="newField"
:depth="depth + 1"
:path="`${path}.${newField.key}`"
:renamable="valueType === 'plain-object'"
:force-collapse="forceCollapse"
editable
removable
:is-state-field="isStateField"
@cancel-edit="addingValue = false"
@submit-edit="addingValue = false"
@edit-state="(path, payload) => $emit('editState', path, payload)"
/>
</div>
</div>
</template>
<style lang="stylus" scoped>
.data-field
user-select text
font-size 12px
@apply font-mono;
cursor pointer
.self
height 20px
line-height 20px
position relative
white-space nowrap
padding-left 14px
.high-density &
height 14px
line-height 14px
span, div
display inline-block
vertical-align middle
.arrow
position absolute
top 7px
left 0px
transition transform .1s ease
&.rotated
transform rotate(90deg)
.actions
display inline-flex
align-items center
position relative
top -1px
> *
visibility hidden
&:first-child
margin-left 6px
&:not(:last-child)
margin-right 6px
&.v-popper--shown
visibility visible
.icon-button
user-select none
width 20px
height @width
.icon-button :deep(.vue-ui-icon),
.small-icon
width 16px
height @width
.warning :deep(svg)
fill $orange
&:hover,
&.force-toolbar
.actions > *
visibility visible
.colon
margin-right .5em
position relative
.type
color $background-color
padding 3px 6px
font-size 10px
line-height 10px
height 16px
border-radius 3px
margin 2px 6px
position relative
background-color #eee
&.prop
background-color #96afdd
&.computed
background-color #af90d5
&.vuex-getter
background-color #5dd5d5
&.firebase-binding
background-color #ffcc00
&.observable
background-color #ff9999
.vue-ui-dark-mode &
color: #242424
.edit-overlay
display inline-flex
align-items center
.key
&.abstract
color $blueishGrey
.vue-ui-dark-mode &
color lighten($blueishGrey, 20%)
.value
display inline-block
color #444
&.string, &.native
color $red
&.string
:deep(span)
color $black
.vue-ui-dark-mode &
color $red
&.null
color #999
&.literal
color $vividBlue
&.raw-boolean :deep(.value-formatted-ouput)
width 36px
display inline-block
&.native.Error
background $red
color $white !important
padding 0 4px
border-radius $br
&::before
content 'Error: '
opacity .75
&.custom
&.type-component
color $green
&::before,
&::after
color $darkGrey
&::before
content '<'
&::after
content '>'
&.type-function
font-style italic
:deep(span)
color $vividBlue
font-family dejavu sans mono, monospace
.platform-mac &
font-family Menlo, monospace
.platform-windows &
font-family Consolas, Lucida Console, Courier New, monospace
.vue-ui-dark-mode &
color $purple
&.type-component-definition
color $green
:deep(span)
color $darkerGrey
&.type-reference
opacity 0.5
:deep(.attr-title)
color #800080
.vue-ui-dark-mode &
color #e36eec
.vue-ui-dark-mode &
color #bdc6cf
&.string, &.native
color #e33e3a
&.null
color #999
&.literal
color $purple
.meta
font-size 12px
font-family Menlo, Consolas, monospace
min-width 150px
.key
display inline-block
width 80px
color lighten(#881391, 60%)
.vue-ui-dark-mode &
color #881391
.value
color white
.vue-ui-dark-mode &
color black
.meta-field
&:not(:last-child)
margin-bottom 4px
.edit-input
font-family Menlo, Consolas, monospace
border solid 1px $green
border-radius 3px
padding 2px
outline none
&.error
border-color $orange
.value-input
width 180px
.key-input
width 90px
color #881391
.remove-field
margin-left 10px
.context-menu-dropdown
.vue-ui-button
display block
width 100%
.more
width 20px
height @width
:deep(.vue-ui-icon)
width 16px
height @width
</style>
|