PlaygroundController.tsx 3.1 KB

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