Skip to content

Commit

Permalink
Feat: JsonViewer New Feature & BugFix (#2658)
Browse files Browse the repository at this point in the history
* feat: support readonly mode

* feat: support hidden searchbar

* feat: correct cursor position

* fix: correct cursor position with click

* fix: resolve missing content issue when copy

* fix: resolve cursor positioning issue after indentation

* fix: ensure error messages render correctly after rerender

* refactor: restructure FoldingModel underlying data structure

* docs: update jsonViewer api doc

* docs: update jsonViewer storybook instance

* v2.72.2-alpha.0

* fix: resolve scroll rendering issue after folding

* v2.72.2-alpha.1

* chore: remove console log statements

* fix: resolve rendering issue after deletion in folded state

* v2.72.2-alpha.2

* fix: resolve search box blur issue in read-only mode

* fix:  disable replace functionality in read-only mode

* fix: correct logic for automatic line wrapping

---------

Co-authored-by: 田丰 <[email protected]>
  • Loading branch information
anjiazhuyouxing and 田丰 authored Jan 7, 2025
1 parent 7c205ed commit de38239
Show file tree
Hide file tree
Showing 23 changed files with 982 additions and 251 deletions.
2 changes: 2 additions & 0 deletions content/plus/jsonviewer/index-en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ render(FormatJsonComponent);
| width | Width of wrapper DOM | number | - |
| className | className of wrapper DOM | string | - |
| style | InlineStyle of wrapper DOM | object | - |
| showSearch | Whether to show search icon | boolean | true |
| options | Formatting configuration | JsonViewerOptions | - |
| onChange | Callback for content change | (value: string) => void | - |

Expand All @@ -170,6 +171,7 @@ render(FormatJsonComponent);
| ------------- | --------------------------------------- | ----------------- | ------- |
| lineHeight | Height of each line of content, unit:px | number | 20 |
| autoWrap | Whether to wrap lines automatically. | boolean | true |
| readOnly | Whether to be read-only. | boolean | false |
| formatOptions | Content format setting | FormattingOptions | - |

### FormattingOptions
Expand Down
2 changes: 2 additions & 0 deletions content/plus/jsonviewer/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ render(FormatJsonComponent);
| width | 宽度 | number | - |
| className | 类名 | string | - |
| style | 内联样式 | object | - |
| showSearch | 是否显示搜索Icon | boolean | true |
| options | 格式化配置 | JsonViewerOptions | - |
| onChange | 内容变化回调 | (value: string) => void | - |

Expand All @@ -167,6 +168,7 @@ render(FormatJsonComponent);
|-------------------|------------------------------------------------|---------------------------------|-----------|
| lineHeight | 行高 | number | 20 |
| autoWrap | 是否自动换行 | boolean | true |
| readOnly | 是否只读 | boolean | false |
| formatOptions | 格式化配置 | FormattingOptions | - |

### FormattingOptions
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"useWorkspaces": true,
"npmClient": "yarn",
"version": "2.72.0"
"version": "2.72.2-alpha.2"
}
6 changes: 6 additions & 0 deletions packages/semi-foundation/jsonViewer/foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,16 @@ class JsonViewerFoundation extends BaseFoundation<JsonViewerAdapter> {
}

replace(replaceText: string) {
if (this.getProps().options.readOnly) {
return;
}
this.jsonViewer?.getSearchWidget().replace(replaceText);
}

replaceAll(replaceText: string) {
if (this.getProps().options.readOnly) {
return;
}
this.jsonViewer?.getSearchWidget().replaceAll(replaceText);
}

Expand Down
5 changes: 3 additions & 2 deletions packages/semi-foundation/jsonViewer/jsonViewer.scss
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ $module: #{$prefix}-json-viewer;
font-variation-settings: normal;
letter-spacing: 0px;
color: #237893;
word-wrap: break-word;
white-space: pre-wrap;
word-break: break-all !important;
white-space: pre-wrap !important;
}

&-line-number {
Expand All @@ -82,6 +82,7 @@ $module: #{$prefix}-json-viewer;
color: $color-json-viewer-line-number;
text-align: center;
width: 50px;
user-select: none;
}

&-content-container {
Expand Down
4 changes: 2 additions & 2 deletions packages/semi-foundation/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "@douyinfe/semi-foundation",
"version": "2.72.0",
"version": "2.72.2-alpha.2",
"description": "",
"scripts": {
"build:lib": "node ./scripts/compileLib.js",
"prepublishOnly": "npm run build:lib"
},
"dependencies": {
"@douyinfe/semi-animation": "2.72.0",
"@douyinfe/semi-json-viewer-core": "2.72.0",
"@douyinfe/semi-json-viewer-core": "2.72.2-alpha.2",
"@mdx-js/mdx": "^3.0.1",
"async-validator": "^3.5.0",
"classnames": "^2.2.6",
Expand Down
108 changes: 54 additions & 54 deletions packages/semi-json-viewer-core/package.json
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
{
"name": "@douyinfe/semi-json-viewer-core",
"version": "2.72.0",
"description": "",
"main": "lib/index.js",
"module": "lib/index.js",
"typings": "src/index.ts",
"scripts": {
"build:lib": "node ./script/compileLib.js",
"prepublishOnly": "npm run build:lib"
},
"files": [
"dist/*",
"lib/*"
],
"dependencies": {
"jsonc-parser": "^3.3.1"
},
"devDependencies": {
"esbuild": "^0.24.0"
},
"sideEffects": [
"*.scss",
"*.css",
"lib/es/index.js",
"./index.ts"
],
"keywords": [
"bytedance douyin design system",
"semi design to any design",
"a11y react component library",
"design to code",
"code to design",
"3000+ design token",
"dark mode",
"semi design",
"design ops",
"modern design system",
"figma ui kit"
],
"homepage": "https://semi.design",
"bugs": {
"url": "https://github.com/DouyinFE/semi-design/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/DouyinFE/semi-design"
},
"_unpkg": true,
"unpkgFiles": [
"dist/css",
"dist/umd/*.js"
],
"author": "",
"license": "MIT"
"name": "@douyinfe/semi-json-viewer-core",
"version": "2.72.2-alpha.2",
"description": "",
"main": "lib/index.js",
"module": "lib/index.js",
"typings": "src/index.ts",
"scripts": {
"build:lib": "node ./script/compileLib.js",
"prepublishOnly": "npm run build:lib"
},
"files": [
"dist/*",
"lib/*"
],
"dependencies": {
"jsonc-parser": "^3.3.1"
},
"devDependencies": {
"esbuild": "^0.24.0"
},
"sideEffects": [
"*.scss",
"*.css",
"lib/es/index.js",
"./index.ts"
],
"keywords": [
"bytedance douyin design system",
"semi design to any design",
"a11y react component library",
"design to code",
"code to design",
"3000+ design token",
"dark mode",
"semi design",
"design ops",
"modern design system",
"figma ui kit"
],
"homepage": "https://semi.design",
"bugs": {
"url": "https://github.com/DouyinFE/semi-design/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/DouyinFE/semi-design"
},
"_unpkg": true,
"unpkgFiles": [
"dist/css",
"dist/umd/*.js"
],
"author": "",
"license": "MIT"
}
3 changes: 2 additions & 1 deletion packages/semi-json-viewer-core/src/common/emitterEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export interface GlobalEvents {
contentChanged: IModelContentChangeEvent | IModelContentChangeEvent[];
problemsChanged: IProblemsChangedEvent;
hoverNode: IHoverNodeEvent;
renderHoverNode: IRenderHoverNodeEvent
renderHoverNode: IRenderHoverNodeEvent;
forceRender: undefined
}

interface IRange {
Expand Down
45 changes: 45 additions & 0 deletions packages/semi-json-viewer-core/src/common/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,48 @@ export function firstNonWhitespaceIndex(str: string): number {
}
return -1;
}

export const enum StringEOL {
Unknown = 0,
Invalid = 3,
LF = 1,
CRLF = 2,
}

export function countEOL(text: string): [number, number, number, StringEOL] {
let eolCount = 0;
let firstLineLength = 0;
let lastLineStart = 0;
let eol: StringEOL = StringEOL.Unknown;
for (let i = 0, len = text.length; i < len; i++) {
const chr = text.charCodeAt(i);

if (chr === CharCode.CarriageReturn) {
if (eolCount === 0) {
firstLineLength = i;
}
eolCount++;
if (i + 1 < len && text.charCodeAt(i + 1) === CharCode.LineFeed) {
// \r\n... case
eol |= StringEOL.CRLF;
i++; // skip \n
} else {
// \r... case
eol |= StringEOL.Invalid;
}
lastLineStart = i + 1;
} else if (chr === CharCode.LineFeed) {
// \n... case
eol |= StringEOL.LF;
if (eolCount === 0) {
firstLineLength = i;
}
eolCount++;
lastLineStart = i + 1;
}
}
if (eolCount === 0) {
firstLineLength = text.length;
}
return [eolCount, firstLineLength, text.length - lastLineStart, eol];
}
19 changes: 19 additions & 0 deletions packages/semi-json-viewer-core/src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,23 @@ export function isObject(val: any): val is Record<string, any> {

export function isNumber(val: any): val is number {
return typeof val === 'number';
}

export function findFirstIdxMonotonousOrArrLen<T>(
array: readonly T[],
predicate: (item: T) => boolean,
startIdx = 0,
endIdxEx = array.length
): number {
let i = startIdx;
let j = endIdxEx;
while (i < j) {
const k = Math.floor((i + j) / 2);
if (predicate(array[k])) {
j = k;
} else {
i = k + 1;
}
}
return i;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { setCurrentNameSpaceId } from '../common/nameSpace';
export interface JsonViewerOptions {
lineHeight?: number;
autoWrap?: boolean;
readOnly?: boolean;
formatOptions?: FormattingOptions;
completionOptions?: CompletionOptions
}
Expand Down
Loading

0 comments on commit de38239

Please sign in to comment.