index.tsx 10 KB

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