1
0

index.tsx 9.6 KB

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