index.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. import React from 'react';
  2. import classNames from 'classnames';
  3. import JsonViewerFoundation, {
  4. JsonViewerOptions,
  5. JsonViewerAdapter,
  6. } from '@douyinfe/semi-foundation/jsonViewer/foundation';
  7. import '@douyinfe/semi-foundation/jsonViewer/jsonViewer.scss';
  8. import { cssClasses } from '@douyinfe/semi-foundation/jsonViewer/constants';
  9. import ButtonGroup from '../button/buttonGroup';
  10. import Button from '../button';
  11. import Input from '../input';
  12. import DragMove from '../dragMove';
  13. import {
  14. IconCaseSensitive,
  15. IconChevronLeft,
  16. IconChevronRight,
  17. IconClose,
  18. IconRegExp,
  19. IconSearch,
  20. IconWholeWord,
  21. } from '@douyinfe/semi-icons';
  22. import BaseComponent, { BaseProps } from '../_base/baseComponent';
  23. import { createPortal } from 'react-dom';
  24. import { isEqual } from "lodash";
  25. import LocaleConsumer from '../locale/localeConsumer';
  26. import { Locale } from '../locale/interface';
  27. const prefixCls = cssClasses.PREFIX;
  28. export type { JsonViewerOptions };
  29. export interface JsonViewerProps extends BaseProps {
  30. value: string;
  31. width: number | string;
  32. height: number | string;
  33. showSearch?: boolean;
  34. className?: string;
  35. style?: React.CSSProperties;
  36. onChange?: (value: string) => void;
  37. renderTooltip?: (value: string, el: HTMLElement) => HTMLElement;
  38. options?: JsonViewerOptions
  39. }
  40. export interface JsonViewerState {
  41. searchOptions: SearchOptions;
  42. showSearchBar: boolean;
  43. customRenderMap: Map<HTMLElement, React.ReactNode>
  44. }
  45. interface SearchOptions {
  46. caseSensitive: boolean;
  47. wholeWord: boolean;
  48. regex: boolean
  49. }
  50. class JsonViewerCom extends BaseComponent<JsonViewerProps, JsonViewerState> {
  51. static defaultProps: Partial<JsonViewerProps> = {
  52. width: 400,
  53. height: 400,
  54. value: '',
  55. options: {
  56. readOnly: false,
  57. autoWrap: true
  58. }
  59. };
  60. private editorRef: React.RefObject<HTMLDivElement>;
  61. private searchInputRef: React.RefObject<HTMLInputElement>;
  62. private replaceInputRef: React.RefObject<HTMLInputElement>;
  63. private isComposing: boolean = false;
  64. foundation: JsonViewerFoundation;
  65. constructor(props: JsonViewerProps) {
  66. super(props);
  67. this.editorRef = React.createRef();
  68. this.searchInputRef = React.createRef();
  69. this.replaceInputRef = React.createRef();
  70. this.foundation = new JsonViewerFoundation(this.adapter);
  71. this.state = {
  72. searchOptions: {
  73. caseSensitive: false,
  74. wholeWord: false,
  75. regex: false,
  76. },
  77. showSearchBar: false,
  78. customRenderMap: new Map(),
  79. };
  80. }
  81. componentDidMount() {
  82. this.foundation.init();
  83. }
  84. componentDidUpdate(prevProps: JsonViewerProps): void {
  85. if (!isEqual(prevProps.options, this.props.options) || this.props.value !== prevProps.value) {
  86. this.foundation.jsonViewer.dispose();
  87. this.foundation.init();
  88. }
  89. }
  90. get adapter(): JsonViewerAdapter<JsonViewerProps, JsonViewerState> {
  91. return {
  92. ...super.adapter,
  93. getEditorRef: () => this.editorRef.current,
  94. getSearchRef: () => this.searchInputRef.current,
  95. notifyChange: value => {
  96. this.props.onChange?.(value);
  97. },
  98. notifyHover: (value, el) => {
  99. const res = this.props.renderTooltip?.(value, el);
  100. return res;
  101. },
  102. notifyCustomRender: (customRenderMap) => {
  103. this.setState({ customRenderMap });
  104. },
  105. setSearchOptions: (key: string) => {
  106. this.setState(
  107. {
  108. searchOptions: {
  109. ...this.state.searchOptions,
  110. [key]: !this.state.searchOptions[key],
  111. },
  112. },
  113. () => {
  114. this.searchHandler();
  115. }
  116. );
  117. },
  118. showSearchBar: () => {
  119. this.setState({ showSearchBar: !this.state.showSearchBar });
  120. },
  121. };
  122. }
  123. getValue() {
  124. return this.foundation.jsonViewer.getModel().getValue();
  125. }
  126. format() {
  127. this.foundation.jsonViewer.format();
  128. }
  129. getStyle() {
  130. const { width, height } = this.props;
  131. return {
  132. width,
  133. height,
  134. };
  135. }
  136. searchHandler = () => {
  137. const value = this.searchInputRef.current?.value;
  138. this.foundation.search(value);
  139. };
  140. changeSearchOptions = (key: string) => {
  141. this.foundation.setSearchOptions(key);
  142. };
  143. renderSearchBox() {
  144. return (
  145. <div className={`${prefixCls}-search-bar-container`} style={{ position: 'absolute', top: 20, right: 20 }}>
  146. {this.renderSearchBar()}
  147. {this.renderReplaceBar()}
  148. </div>
  149. );
  150. }
  151. renderSearchOptions() {
  152. const searchOptionItems = [
  153. {
  154. key: 'caseSensitive',
  155. icon: IconCaseSensitive,
  156. },
  157. {
  158. key: 'regex',
  159. icon: IconRegExp,
  160. },
  161. {
  162. key: 'wholeWord',
  163. icon: IconWholeWord,
  164. },
  165. ];
  166. return (
  167. <ul className={`${prefixCls}-search-options`}>
  168. {searchOptionItems.map(({ key, icon: Icon }) => (
  169. <li
  170. key={key}
  171. className={classNames(`${prefixCls}-search-options-item`, {
  172. [`${prefixCls}-search-options-item-active`]: this.state.searchOptions[key],
  173. })}
  174. >
  175. <Icon onClick={() => this.changeSearchOptions(key)} />
  176. </li>
  177. ))}
  178. </ul>
  179. );
  180. }
  181. renderSearchBar() {
  182. return (
  183. <LocaleConsumer
  184. componentName="JsonViewer"
  185. >
  186. {(locale: Locale['JsonViewer'], localeCode: Locale['code']) => (
  187. <div className={`${prefixCls}-search-bar`}>
  188. <Input
  189. placeholder={locale.search}
  190. className={`${prefixCls}-search-bar-input`}
  191. onChange={(_value, e) => {
  192. e.preventDefault();
  193. if (!this.isComposing) {
  194. this.searchHandler();
  195. }
  196. this.searchInputRef.current?.focus();
  197. }}
  198. onCompositionStart={() => {
  199. this.isComposing = true;
  200. }}
  201. onCompositionEnd={() => {
  202. this.isComposing = false;
  203. this.searchHandler();
  204. this.searchInputRef.current?.focus();
  205. }}
  206. ref={this.searchInputRef}
  207. />
  208. {this.renderSearchOptions()}
  209. <ButtonGroup>
  210. <Button
  211. icon={<IconChevronLeft />}
  212. onClick={e => {
  213. e.preventDefault();
  214. this.foundation.prevSearch();
  215. }}
  216. />
  217. <Button
  218. icon={<IconChevronRight />}
  219. onClick={e => {
  220. e.preventDefault();
  221. this.foundation.nextSearch();
  222. }}
  223. />
  224. </ButtonGroup>
  225. <Button
  226. icon={<IconClose />}
  227. size="small"
  228. theme={'borderless'}
  229. type={'tertiary'}
  230. onClick={() => this.foundation.showSearchBar()}
  231. />
  232. </div>
  233. )}
  234. </LocaleConsumer>
  235. );
  236. }
  237. renderReplaceBar() {
  238. const { readOnly } = this.props.options;
  239. return (
  240. <LocaleConsumer
  241. componentName="JsonViewer"
  242. >
  243. {(locale: Locale['JsonViewer'], localeCode: Locale['code']) => (
  244. <div className={`${prefixCls}-replace-bar`}>
  245. <Input
  246. placeholder={locale.replace}
  247. className={`${prefixCls}-replace-bar-input`}
  248. onChange={(value, e) => {
  249. e.preventDefault();
  250. }}
  251. ref={this.replaceInputRef}
  252. />
  253. <Button
  254. style={{ width: 'fit-content' }}
  255. disabled={readOnly}
  256. onClick={() => {
  257. const value = this.replaceInputRef.current?.value;
  258. this.foundation.replace(value);
  259. }}
  260. >
  261. {locale.replace}
  262. </Button>
  263. <Button
  264. style={{ width: 'fit-content' }}
  265. disabled={readOnly}
  266. onClick={() => {
  267. const value = this.replaceInputRef.current?.value;
  268. this.foundation.replaceAll(value);
  269. }}
  270. >
  271. {locale.replaceAll}
  272. </Button>
  273. </div>
  274. )}
  275. </LocaleConsumer>
  276. );
  277. }
  278. render() {
  279. let isDragging = false;
  280. const { width, className, style, showSearch = true, ...rest } = this.props;
  281. return (
  282. <>
  283. <div style={{ ...this.getStyle(), position: 'relative', ...style }} className={className} {...this.getDataAttr(rest)}>
  284. <div
  285. style={{ ...this.getStyle(), padding: '12px 0' }}
  286. ref={this.editorRef}
  287. className={classNames(prefixCls, `${prefixCls}-background`)}
  288. ></div>
  289. {showSearch && (
  290. <DragMove
  291. onMouseDown={() => {
  292. isDragging = false;
  293. }}
  294. onMouseMove={() => {
  295. isDragging = true;
  296. }}
  297. >
  298. <div style={{ position: 'absolute', top: 0, left: width }}>
  299. {!this.state.showSearchBar ? (
  300. <Button
  301. className={`${prefixCls}-search-bar-trigger`}
  302. onClick={e => {
  303. e.preventDefault();
  304. if (isDragging) {
  305. e.stopPropagation();
  306. e.preventDefault();
  307. return;
  308. }
  309. this.foundation.showSearchBar();
  310. }}
  311. icon={<IconSearch />}
  312. style={{ position: 'absolute', top: 20, right: 20 }}
  313. />
  314. ) : (
  315. this.renderSearchBox()
  316. )}
  317. </div>
  318. </DragMove>
  319. )}
  320. </div>
  321. {Array.from(this.state.customRenderMap.entries()).map(([key, value]) => {
  322. // key.innerHTML = '';
  323. return createPortal(value, key);
  324. })}
  325. </>
  326. );
  327. }
  328. }
  329. export default JsonViewerCom;