1
0

index.tsx 11 KB

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