index.tsx 11 KB

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