-
Notifications
You must be signed in to change notification settings - Fork 716
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Json viewer #2561
base: release
Are you sure you want to change the base?
Json viewer #2561
Changes from 12 commits
706c457
1a88810
b0cbc3f
20bf9a7
cda569d
37ca223
3b30c1a
72afc1b
9103898
189b629
7408e51
9211573
96bbe42
8ffdbb6
0d2c835
306e763
bf4630c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
--- | ||
localeCode: zh-CN | ||
order: 86 | ||
category: 展示类 | ||
title: Json-Viewer JSON编辑器 | ||
icon: doc-list | ||
dir: column | ||
noInline: true | ||
brief: 用于展示和编辑 JSON 数据 | ||
--- | ||
|
||
## 代码演示 | ||
|
||
### 如何引入 | ||
|
||
```jsx import | ||
import { JsonViewer } from '@douyinfe/semi-ui'; | ||
``` | ||
|
||
### 基本用法 | ||
|
||
JsonViewer 的基本用法。传入 value height 和 width 参数,设置组件的高度和宽度和初始值。 | ||
|
||
```jsx live=true dir="column" noInline=true | ||
import React from 'react'; | ||
import { JsonViewer } from '@douyinfe/semi-ui'; | ||
|
||
class SimpleList extends React.Component { | ||
render() { | ||
const data = `{ | ||
"name": "Semi", | ||
"version": "0.0.0" | ||
}`; | ||
|
||
return ( | ||
<div> | ||
<div style={{ marginRight: 16 }}> | ||
<h3 style={{ marginBottom: 16 }}>Default Size</h3> | ||
<JsonViewer height={400} width={700} value={data} /> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
render(SimpleList); | ||
``` | ||
|
||
### 设置行高 | ||
|
||
配置 options 的 lineHeight 参数,设置固定行高(单位:px, 默认 20)。 | ||
|
||
```jsx live=true dir="column" noInline=true | ||
import React from 'react'; | ||
import { JsonViewer } from '@douyinfe/semi-ui'; | ||
|
||
class SimpleList extends React.Component { | ||
render() { | ||
const data = `{ | ||
"name": "Semi", | ||
"version": "0.0.0" | ||
}`; | ||
|
||
return ( | ||
<div> | ||
<div style={{ marginRight: 16 }}> | ||
<h3 style={{ marginBottom: 16 }}>Default Size</h3> | ||
<JsonViewer height={400} width={700} value={data} options={{ lineHeight: 25 }} /> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
render(SimpleList); | ||
``` | ||
|
||
### 自动换行 | ||
|
||
配置 options 的 autoWrap 参数,设置组件是否自动换行。 | ||
|
||
```jsx live=true dir="column" noInline=true | ||
import React from 'react'; | ||
import { JsonViewer } from '@douyinfe/semi-ui'; | ||
|
||
class SimpleList extends React.Component { | ||
render() { | ||
const data = `{ | ||
"name": "Semi", | ||
"version": "0.0.0", | ||
"description": "lorem ipsum dolor sit amet, consectetur adipiscing elit.lorem ipsum dolor sit amet, consectetur adipiscing elit.lorem ipsum dolor sit amet, consectetur adipiscing elit.lorem ipsum dolor sit amet, consectetur adipiscing elit." | ||
}`; | ||
|
||
return ( | ||
<div> | ||
<div style={{ marginRight: 16 }}> | ||
<h3 style={{ marginBottom: 16 }}>Default Size</h3> | ||
<JsonViewer height={400} width={700} value={data} options={{ autoWrap: true }} /> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
render(SimpleList); | ||
``` | ||
|
||
### 格式化配置 | ||
|
||
配置 options 的 formatOptions 参数,设置组件的格式化配置。 | ||
|
||
- tabSize: number, 设置缩进大小,4 表示每级缩进 4 个空格 | ||
- insertSpaces: boolean, true 表示使用空格进行缩进,false 表示使用制表符(Tab) | ||
- eol: string, 设置换行符,可以是\n,\r\n, | ||
|
||
```jsx live=true dir="column" noInline=true | ||
import React from 'react'; | ||
import { JsonViewer } from '@douyinfe/semi-ui'; | ||
|
||
class SimpleList extends React.Component { | ||
render() { | ||
const data = `{ | ||
"name": "Semi", | ||
"version": "0.0.0", | ||
"description": "lorem ipsum dolor sit amet, consectetur adipiscing elit.lorem ipsum dolor sit amet, consectetur adipiscing elit.lorem ipsum dolor sit amet, consectetur adipiscing elit.lorem ipsum dolor sit amet, consectetur adipiscing elit." | ||
}`; | ||
|
||
return ( | ||
<div> | ||
<div style={{ marginRight: 16 }}> | ||
<h3 style={{ marginBottom: 16 }}>Default Size</h3> | ||
<JsonViewer | ||
height={400} | ||
width={700} | ||
value={data} | ||
options={{ formatOptions: { tabSize: 2, insertSpaces: true, eol: '\n' } }} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
render(SimpleList); | ||
pointhalo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
## API 参考 | ||
|
||
### JsonViewer | ||
|
||
| 属性 | 说明 | 类型 | 默认值 | | ||
|-------------------|------------------------------------------------|---------------------------------|--------------| | ||
| value | 设置 value 属性 | string | - | | ||
| height | 设置高度 属性 | number | - | | ||
| width | 设置宽度 属性 | number | - | | ||
| options | 设置格式化配置 属性 | JsonViewerOptions | - | | ||
| onChange | 设置 value 变化回调 属性 | (value: string) => void | - | | ||
| onValueHover | 设置 value 悬浮回调 属性 | ({value: string, target: HTMLElement}) => HTMLElement | - | | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个是你 storybook 里写的 hoverHandler 吗? |
||
|
||
### JsonViewerOptions | ||
|
||
| 属性 | 说明 | 类型 | 默认值 | | ||
|-------------------|------------------------------------------------|---------------------------------|-----------| | ||
| lineHeight | 设置行高 属性 | number | 20 | | ||
| autoWrap | 设置是否自动换行 属性 | boolean | true | | ||
| formatOptions | 设置格式化配置 属性 | FormattingOptions | | | ||
|
||
### FormattingOptions | ||
|
||
| 属性 | 说明 | 类型 | 默认值 | | ||
|-------------------|------------------------------------------------|---------------------------------|-----------| | ||
| tabSize | 设置缩进大小 属性 | number | 4 | | ||
| insertSpaces | 设置是否使用空格进行缩进 属性 | boolean | true | | ||
| eol | 设置换行符 属性 | string | '\n' | | ||
|
||
|
||
|
||
|
||
## 使用场景 | ||
|
||
JsonViewer 和 Monaco Editor 各有其适用场景: | ||
|
||
- **JsonViewer 适用于**: | ||
- 仅需 JSON 查看和基础编辑的场景 | ||
- 对加载速度和性能要求较高的场景 | ||
- 需要更轻量级组件的场景 | ||
|
||
- **Monaco Editor 适用于**: | ||
- 需要完整 IDE 功能的场景(如语法高亮、代码补全、错误提示等) | ||
- 需要多语言支持的场景 | ||
- 需要更复杂编辑功能的场景 | ||
|
||
### 技术实现 | ||
|
||
JsonViewer 底层使用 jsonc-parser 进行 JSON 词法解析和格式化,并在此基础上实现了 JSON 的 AST 解析,代码提示等功能。 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,10 +12,10 @@ | |
"bootstrap": "lerna bootstrap -- --legacy-peer-deps", | ||
"docsite": "npm run develop", | ||
"pre-develop": "npm run scripts:changelog && node ./scripts/designToken.js ./static/designToken.json", | ||
"develop": "npm run pre-develop && gatsby clean && lerna run build:lib --scope @douyinfe/semi-webpack-plugin --scope eslint-plugin-semi-design && gatsby develop -H 0.0.0.0 --port=3666 --verbose", | ||
"develop": "npm run pre-develop && gatsby clean && lerna run build:lib --scope @douyinfe/semi-json-viewer-core --scope @douyinfe/semi-webpack-plugin --scope eslint-plugin-semi-design && gatsby develop -H 0.0.0.0 --port=3666 --verbose", | ||
"scripts:changelog": "node scripts/changelog.js", | ||
"start": "npm run story", | ||
"pre-story": "lerna exec --scope=@douyinfe/semi-ui --scope=@douyinfe/semi-foundation -- rimraf ./lib && lerna run build:lib --scope @douyinfe/semi-webpack-plugin --scope eslint-plugin-semi-design", | ||
"pre-story": "lerna exec --scope=@douyinfe/semi-ui --scope=@douyinfe/semi-foundation -- rimraf ./lib && lerna run build:lib --scope @douyinfe/semi-json-viewer-core --scope @douyinfe/semi-webpack-plugin --scope eslint-plugin-semi-design", | ||
"story": "npm run pre-story && sb dev -c ./.storybook/js/ -p 6006", | ||
"story:ts": "npm run pre-story && sb dev -c ./.storybook/ts/ -p 6007", | ||
"story:ani": "npm run pre-story && sb dev -c ./.storybook/animation/react -p 6008", | ||
|
@@ -35,7 +35,7 @@ | |
"build:css": "lerna run build:css", | ||
"build-storybook": "sb build -c ./.storybook/js/ -o ./storybook && cp -r storybook storybook-static", | ||
"build-storybook-static": "sb build -c ./.storybook/js/", | ||
"build:gatsbydoc": "lerna run build:lib --scope @douyinfe/semi-webpack-plugin --scope eslint-plugin-semi-design && cross-env NODE_ENV=production node --max_old_space_size=16384 ./node_modules/gatsby/cli.js build --prefix-paths --verbose && rimraf build && mv public build", | ||
"build:gatsbydoc": "lerna run build:lib --scope --scope @douyinfe/semi-json-viewer-core --scope @douyinfe/semi-webpack-plugin --scope eslint-plugin-semi-design && cross-env NODE_ENV=production node --max_old_space_size=16384 ./node_modules/gatsby/cli.js build --prefix-paths --verbose && rimraf build && mv public build", | ||
"build:icon": "lerna run build:icon --scope='@douyinfe/semi-{icons,illustrations}'", | ||
"cypress:coverage": "npx wait-on http://127.0.0.1:6006 && ./node_modules/.bin/cypress run", | ||
"postcypress:coverage": "yarn coverage:merge", | ||
|
@@ -217,7 +217,8 @@ | |
"webpack": "^5.77.0", | ||
"webpack-cli": "^5.1.4", | ||
"webpack-dev-server": "^3.11.2", | ||
"webpackbar": "^5.0.0-3" | ||
"webpackbar": "^5.0.0-3", | ||
"worker-loader": "^3.0.8" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
|
@@ -242,5 +243,6 @@ | |
"stylelint" | ||
] | ||
}, | ||
"license": "MIT" | ||
"license": "MIT", | ||
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个pacakgeMannger 这行不要带上去 |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { BASE_CLASS_PREFIX } from "../base/constants"; | ||
|
||
const cssClasses = { | ||
PREFIX: `${BASE_CLASS_PREFIX}-json-viewer`, | ||
} as const; | ||
|
||
export { cssClasses }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from '@douyinfe/semi-json-viewer-core'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
|
||
import BaseFoundation from '../base/foundation'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. index.ts 可以直接跟 foudation 合成一个文件。就叫 foundation就行。 |
||
|
||
export interface JsonViewerAdapter { | ||
|
||
} | ||
|
||
|
||
class JsonViewerFoundation extends BaseFoundation<JsonViewerAdapter> {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
后续加一个 【使用场景】
描述一下与直接使用 Monaco的区别
什么场景建议使用 Mocaco
什么场景建议使用 JsonViewer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
以及 我们底层依赖什么包来做什么事情,例如使用 jsonc-parser来干什么,而在什么地方做了额外的改动处理。(2-3句话简短解释就行)