Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: new component jsonViewer #5544

Merged
merged 3 commits into from
Feb 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/effects/common-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"qrcode": "catalog:",
"tippy.js": "catalog:",
"vue": "catalog:",
"vue-json-viewer": "catalog:",
"vue-router": "catalog:",
"vue-tippy": "catalog:"
},
Expand Down
1 change: 1 addition & 0 deletions packages/effects/common-ui/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './captcha';
export * from './col-page';
export * from './ellipsis-text';
export * from './icon-picker';
export * from './json-viewer';
export * from './page';
export * from './resize';
export * from './tippy';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { default as JsonViewer } from './index.vue';

export * from './types';
77 changes: 77 additions & 0 deletions packages/effects/common-ui/src/components/json-viewer/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<script lang="ts" setup>
import type { SetupContext } from 'vue';

import type { Recordable } from '@vben/types';

import type { JsonViewerProps } from './types';

import { computed, useAttrs } from 'vue';
// @ts-ignore
import VueJsonViewer from 'vue-json-viewer';

import { $t } from '@vben/locales';

import { isBoolean, isString } from '@vben-core/shared/utils';

defineOptions({ name: 'JsonViewer' });

const props = withDefaults(defineProps<JsonViewerProps>(), {
expandDepth: 1,
copyable: false,
sort: false,
boxed: false,
theme: 'default-json-theme',
expanded: false,
previewMode: false,
showArrayIndex: true,
showDoubleQuotes: false,
parseString: true,
});

const emit = defineEmits<{
parseError: [error: Error];
}>();

const attrs: SetupContext['attrs'] = useAttrs();

const bindProps = computed<Recordable<any>>(() => {
const copyable = {
copyText: $t('ui.jsonViewer.copy'),
copiedText: $t('ui.jsonViewer.copied'),
timeout: 2000,
...(isBoolean(props.copyable) ? {} : props.copyable),
};

return {
...props,
...attrs,
copyable: props.copyable ? copyable : false,
};
});

const modelValue = defineModel();

const jsonToShow = computed(() => {
if (props.parseString && isString(modelValue.value)) {
try {
return JSON.parse(modelValue.value);
} catch (error) {
emit('parseError', error as Error);
console.error('Error parsing JSON:', error);
return modelValue.value;
}
} else {
return modelValue.value;
}
});
</script>
<template>
<VueJsonViewer :value="jsonToShow" v-bind="bindProps">
<template #copy="slotProps">
<slot name="copy" v-bind="slotProps"></slot>
</template>
</VueJsonViewer>
</template>
<style lang="scss">
@use './style.scss';
</style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
.default-json-theme {
font-family: Consolas, Menlo, Courier, monospace;
font-size: 14px;
color: hsl(var(--foreground));
white-space: nowrap;
background: hsl(var(--background));

&.jv-container.boxed {
border: 1px solid hsl(var(--border));
}

.jv-ellipsis {
display: inline-block;
padding: 0 4px 2px;
font-size: 0.9em;
line-height: 0.9;
color: hsl(var(--secondary-foreground));
vertical-align: 2px;
cursor: pointer;
user-select: none;
background-color: hsl(var(--secondary));
border-radius: 3px;
}

.jv-button {
color: hsl(var(--primary));
}

.jv-key {
color: hsl(var(--heavy-foreground));
}

.jv-item {
&.jv-array {
color: hsl(var(--heavy-foreground));
}

&.jv-boolean {
color: hsl(var(--red-400));
}

&.jv-function {
color: hsl(var(--destructive-foreground));
}

&.jv-number {
color: hsl(var(--info-foreground));
}

&.jv-number-float {
color: hsl(var(--info-foreground));
}

&.jv-number-integer {
color: hsl(var(--info-foreground));
}

&.jv-object {
color: hsl(var(--accent-darker));
}

&.jv-undefined {
color: hsl(var(--secondary-foreground));
}

&.jv-string {
color: hsl(var(--primary));
word-break: break-word;
white-space: normal;
}
}

&.jv-container .jv-code {
padding: 10px;

&.boxed:not(.open) {
padding-bottom: 20px;
margin-bottom: 10px;
}

&.open {
padding-bottom: 10px;
}

.jv-toggle {
&::before {
padding: 0 2px;
border-radius: 2px;
}

&:hover {
&::before {
background: hsl(var(--accent-foreground));
}
}
}
}
}
24 changes: 24 additions & 0 deletions packages/effects/common-ui/src/components/json-viewer/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export interface JsonViewerProps {
/** 展开深度 */
expandDepth?: number;
/** 是否可复制 */
copyable?: boolean;
/** 是否排序 */
sort?: boolean;
/** 显示边框 */
boxed?: boolean;
/** 主题 */
theme?: string;
/** 是否展开 */
expanded?: boolean;
/** 时间格式化函数 */
timeformat?: (time: Date | number | string) => string;
/** 预览模式 */
previewMode?: boolean;
/** 显示数组索引 */
showArrayIndex?: boolean;
/** 显示双引号 */
showDoubleQuotes?: boolean;
/** 解析字符串 */
parseString?: boolean;
}
4 changes: 4 additions & 0 deletions packages/locales/src/langs/en-US/ui.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
"placeholder": "Select an icon",
"search": "Search icon..."
},
"jsonViewer": {
"copy": "Copy",
"copied": "Copied"
},
"fallback": {
"pageNotFound": "Oops! Page Not Found",
"pageNotFoundDesc": "Sorry, we couldn't find the page you were looking for.",
Expand Down
4 changes: 4 additions & 0 deletions packages/locales/src/langs/zh-CN/ui.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
"placeholder": "选择一个图标",
"search": "搜索图标..."
},
"jsonViewer": {
"copy": "复制",
"copied": "已复制"
},
"fallback": {
"pageNotFound": "哎呀!未找到页面",
"pageNotFoundDesc": "抱歉,我们无法找到您要找的页面。",
Expand Down
9 changes: 9 additions & 0 deletions playground/src/router/routes/modules/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,15 @@ const routes: RouteRecordRaw[] = [
title: 'Tippy',
},
},
{
name: 'JsonViewer',
path: '/examples/json-viewer',
component: () => import('#/views/examples/json-viewer/index.vue'),
meta: {
icon: 'tabler:json',
title: 'JsonViewer',
},
},
],
},
];
Expand Down
51 changes: 51 additions & 0 deletions playground/src/views/examples/json-viewer/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
export const json1 = {
additionalInfo: {
author: 'Your Name',
debug: true,
version: '1.3.10',
versionCode: 132,
},
additionalNotes: 'This JSON is used for demonstration purposes',
tools: [
{
description: 'Description of Tool 1',
name: 'Tool 1',
},
{
description: 'Description of Tool 2',
name: 'Tool 2',
},
{
description: 'Description of Tool 3',
name: 'Tool 3',
},
{
description: 'Description of Tool 4',
name: 'Tool 4',
},
],
};

export const json2 = `
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"model": "gpt-3.5-turbo-0613",
"system_fingerprint": "fp_44709d6fcb",
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello there, how may I assist you today?"
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21,
"debug_mode": true
}
}
`;
26 changes: 26 additions & 0 deletions playground/src/views/examples/json-viewer/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<script lang="ts" setup>
import { JsonViewer, Page } from '@vben/common-ui';

import { Card } from 'ant-design-vue';

import { json1, json2 } from './data';
</script>
<template>
<Page
title="Json Viewer"
description="一个渲染 JSON 结构数据的组件,支持复制、展开等,简单易用"
>
<Card title="默认配置">
<JsonViewer v-model="json1" />
</Card>
<Card title="可复制、默认展开3层、显示边框" class="mt-4">
<JsonViewer
v-model="json2"
:expand-depth="3"
copyable
:sort="false"
boxed
/>
</Card>
</Page>
</template>
Loading