PlaygroundController.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { useCallback } from 'react';
  2. import { useForm } from 'react-hook-form';
  3. import { GlobalCodeMirrorEditorKey } from '../../consts';
  4. import { PlaygroundAllEditorTheme, PlaygroundReactCodeMirror } from '../../services';
  5. import { useCodeMirrorEditorIsolated } from '../../stores';
  6. export const InitEditorValueRow = (): JSX.Element => {
  7. const { data } = useCodeMirrorEditorIsolated(GlobalCodeMirrorEditorKey.MAIN);
  8. const initDoc = data?.initDoc;
  9. const initEditorValue = useCallback(() => {
  10. initDoc?.('# Header\n\n- foo\n-bar\n');
  11. }, [initDoc]);
  12. return (
  13. <div className="row">
  14. <div className="col">
  15. <button
  16. type="button"
  17. className="btn btn-outline-secondary"
  18. onClick={() => initEditorValue()}
  19. >
  20. Initialize editor value
  21. </button>
  22. </div>
  23. </div>
  24. );
  25. };
  26. type SetCaretLineRowFormData = {
  27. lineNumber: number | string;
  28. };
  29. export const SetCaretLineRow = (): JSX.Element => {
  30. const { data } = useCodeMirrorEditorIsolated(GlobalCodeMirrorEditorKey.MAIN);
  31. const { register, handleSubmit } = useForm<SetCaretLineRowFormData>({
  32. defaultValues: {
  33. lineNumber: 1,
  34. },
  35. });
  36. const setCaretLine = data?.setCaretLine;
  37. const onSubmit = handleSubmit((submitData) => {
  38. const lineNumber = Number(submitData.lineNumber) || 1;
  39. setCaretLine?.(lineNumber);
  40. });
  41. return (
  42. <form className="row mt-3" onSubmit={onSubmit}>
  43. <div className="col">
  44. <div className="input-group">
  45. <input
  46. {...register('lineNumber')}
  47. type="number"
  48. className="form-control"
  49. placeholder="Input line number"
  50. aria-label="line number"
  51. aria-describedby="button-set-cursor"
  52. />
  53. <button type="submit" className="btn btn-outline-secondary" id="button-set-cursor">Set the cursor</button>
  54. </div>
  55. </div>
  56. </form>
  57. );
  58. };
  59. type SetThemeRowProps = {
  60. setEditorTheme: (value: string) => void,
  61. }
  62. const SetThemeRow = (props: SetThemeRowProps): JSX.Element => {
  63. const { setEditorTheme } = props;
  64. const createItems = (items: string[]): JSX.Element => {
  65. return (
  66. <div>
  67. { items.map((theme) => {
  68. return (
  69. <button
  70. type="button"
  71. className="btn btn-outline-secondary"
  72. onClick={() => {
  73. setEditorTheme(theme);
  74. }}
  75. >{theme}
  76. </button>
  77. );
  78. }) }
  79. </div>
  80. );
  81. };
  82. return (
  83. <>
  84. <div className="row mt-3">
  85. <h2>default</h2>
  86. <div className="col">
  87. {createItems(Object.keys(PlaygroundAllEditorTheme))}
  88. </div>
  89. </div>
  90. <div className="row mt-3">
  91. <h2>ReactCodeMirror</h2>
  92. <div className="col">
  93. {createItems(Object.keys(PlaygroundReactCodeMirror))}
  94. </div>
  95. </div>
  96. </>
  97. );
  98. };
  99. type PlaygroundControllerProps = {
  100. setEditorTheme: (value: string) => void
  101. };
  102. export const PlaygroundController = (props: PlaygroundControllerProps): JSX.Element => {
  103. const { setEditorTheme } = props;
  104. return (
  105. <div className="container mt-5">
  106. <InitEditorValueRow />
  107. <SetCaretLineRow />
  108. <SetThemeRow setEditorTheme={setEditorTheme} />
  109. </div>
  110. );
  111. };