Skip to content
Open
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
4 changes: 3 additions & 1 deletion content/plus/jsonviewer/index-en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ render(CustomRenderJsonComponent);
| showSearch | Whether to show search icon | boolean | true |
| options | Formatting configuration | JsonViewerOptions | - |
| onChange | Callback for content change | (value: string) => void | - |
| onBlur | Callback when input box loses focus | (e:event) => void | - |
| onFocus | The callback when the input box is focused | (e:event) => void | - |

### JsonViewerOptions

Expand All @@ -247,6 +249,7 @@ render(CustomRenderJsonComponent);
| readOnly | Whether to be read-only. | boolean | false | - |
| customRenderRule | Custom render rules | CustomRenderRule[] | - | 2.74.0 |
| formatOptions | Content format setting | FormattingOptions | - | - |
| autoFocus| Whether to auto focus | boolean | false | - |

### FormattingOptions

Expand All @@ -255,7 +258,6 @@ render(CustomRenderJsonComponent);
| tabSize | Indent size. Unit: px | number | 4 |
| insertSpaces | Whether to use spaces for indentation | boolean | true |
| eol | Line break character | string | '\n' |

### CustomRenderRule

| Attribute | Description | Type | Default |
Expand Down
3 changes: 3 additions & 0 deletions content/plus/jsonviewer/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ render(CustomRenderJsonComponent);
| showSearch | 是否显示搜索Icon | boolean | true |
| options | 编辑器配置 | JsonViewerOptions | - |
| onChange | 内容变化回调 | (value: string) => void | - |
| onBlur | 输入框失去焦点时的回调 | (e:event) => void | - |
| onFocus | 输入框 focus 时的回调 | (e:event) => void | - |

### JsonViewerOptions

Expand All @@ -244,6 +246,7 @@ render(CustomRenderJsonComponent);
| readOnly | 是否只读 | boolean | false | - |
| customRenderRule | 自定义渲染规则 | CustomRenderRule[] | - | 2.74.0 |
| formatOptions | 格式化配置 | FormattingOptions | - | - |
| autoFocus| 是否自动对焦 | boolean | false | - |

### CustomRenderRule
| 属性 | 说明 | 类型 | 默认值 |
Expand Down
11 changes: 9 additions & 2 deletions packages/semi-foundation/jsonViewer/foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export interface JsonViewerAdapter<P = Record<string, any>, S = Record<string, a
notifyHover: (value: string, el: HTMLElement) => HTMLElement | undefined;
setSearchOptions: (key: string) => void;
showSearchBar: () => void;
notifyCustomRender: (customRenderMap: Map<HTMLElement, any>) => void
notifyCustomRender: (customRenderMap: Map<HTMLElement, any>) => void;
notifyFocus: (e: Event) => void;
notifyBlur: (e: Event) => void
}

class JsonViewerFoundation extends BaseFoundation<JsonViewerAdapter> {
Expand All @@ -37,7 +39,12 @@ class JsonViewerFoundation extends BaseFoundation<JsonViewerAdapter> {
this.search(this._adapter.getSearchRef().value);
}
});

this.jsonViewer.emitter.on('focus', (e) => {
this._adapter.notifyFocus(e);
});
this.jsonViewer.emitter.on('blur', (e) => {
this._adapter.notifyBlur(e);
});
}

search(searchText: string, caseSensitive?: boolean, wholeWord?: boolean, regex?: boolean) {
Expand Down
2 changes: 2 additions & 0 deletions packages/semi-json-viewer-core/src/common/emitterEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export interface GlobalEvents {
hoverNode: IHoverNodeEvent;
renderHoverNode: IRenderHoverNodeEvent;
forceRender: undefined;
focus: Event;
blur: Event;
customRender: ICustomRenderEvent
}

Expand Down
7 changes: 6 additions & 1 deletion packages/semi-json-viewer-core/src/json-viewer/jsonViewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export interface JsonViewerOptions {
formatOptions?: FormattingOptions;
completionOptions?: CompletionOptions;
customRenderRule?: CustomRenderRule[];
prefixCls?: string
prefixCls?: string;
autoFocus?: boolean
}

export interface CompletionOptions {
Expand Down Expand Up @@ -54,6 +55,10 @@ export class JsonViewer {

layout() {
this._view.layout();
const { autoFocus, readOnly } = this._view.options || {};
if (autoFocus && !readOnly) {
this._view.contentDom.focus();
}
}

getModel() {
Expand Down
8 changes: 8 additions & 0 deletions packages/semi-json-viewer-core/src/view/edit/editWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ export class EditWidget {
if (e.button !== 0) return; // 只处理左键
this._selectionModel.updateFromSelectingEnd();
});
this._view.contentDom.addEventListener('focus', e => {
if (this._view.options?.readOnly) return;
this.emitter.emit('focus', e);
});
this._view.contentDom.addEventListener('blur', e => {
if (this._view.options?.readOnly) return;
this.emitter.emit('blur', e);
});
}

private buildBaseOperation(type: IModelContentChangeEvent['type'] = 'insert') {
Expand Down
10 changes: 9 additions & 1 deletion packages/semi-ui/jsonViewer/_story/jsonViewer.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ export const DefaultJsonViewer = () => {
console.log(value, 'value');
};

const onFocusHandler = (e) => {
console.log('focus', e);
};
const onBlurHandler = (e) => {
console.log('blur', e);
};
const [autoWrap, setAutoWrap] = useState(true);
const [lineHeight, setLineHeight] = useState(20);
const jsonviewerRef = useRef(null);
Expand All @@ -94,8 +100,10 @@ export const DefaultJsonViewer = () => {
value={baseStr}
width={700}
height={400}
options={{ lineHeight: lineHeight, autoWrap: autoWrap, formatOptions: { tabSize: 4 } }}
options={{ lineHeight: lineHeight, autoWrap: autoWrap, formatOptions: { tabSize: 4 } ,autoFocus: true}}
onChange={onChangeHandler}
onFocus={onFocusHandler}
onBlur={onBlurHandler}
ref={jsonviewerRef}
/>
</>
Expand Down
13 changes: 11 additions & 2 deletions packages/semi-ui/jsonViewer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export interface JsonViewerProps extends BaseProps {
style?: React.CSSProperties;
onChange?: (value: string) => void;
renderTooltip?: (value: string, el: HTMLElement) => HTMLElement;
options?: JsonViewerOptions
options?: JsonViewerOptions;
onFocus?: (e: Event) => void;
onBlur?: (e: Event) => void
}

export interface JsonViewerState {
Expand All @@ -59,7 +61,8 @@ class JsonViewerCom extends BaseComponent<JsonViewerProps, JsonViewerState> {
value: '',
options: {
readOnly: false,
autoWrap: true
autoWrap: true,
autoFocus: false
}
};

Expand Down Expand Up @@ -126,6 +129,12 @@ class JsonViewerCom extends BaseComponent<JsonViewerProps, JsonViewerState> {
}
);
},
notifyFocus: (e) => {
this.props.onFocus?.(e);
},
notifyBlur: (e) => {
this.props.onBlur?.(e);
},
showSearchBar: () => {
this.setState({ showSearchBar: !this.state.showSearchBar });
this.setState({ searchOptions: {
Expand Down
Loading