index.tsx 10 KB

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