File size: 3,403 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { Dialog, DialogActions, DialogContent } from '@mui/material';
import Button from '@mui/material/Button';
import TextField from '@mui/material/TextField';
import DeleteIcon from '@mui/icons-material/Delete';
import DoneIcon from '@mui/icons-material/Done';
import type { JsonSchema } from '@react-page/editor';
import { makeUniformsSchema, AutoForm, AutoFields } from '@react-page/editor';
import React, { useCallback, useRef, useState } from 'react';
import type { Data } from '../../types';

import type { SlatePluginControls } from '../../types/slatePluginDefinitions';
import { useEffect } from 'react';

function Controls<T extends Data>(
  props: SlatePluginControls<T> & {
    schema?: JsonSchema<T>;
  }
) {
  const uniformsSchema = props.schema
    ? makeUniformsSchema<T>(props.schema)
    : null;

  const hasSchema = Boolean(props.schema);

  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  const formRef = useRef<any>();

  const [text, setText] = useState<string | null>(null);

  const onCancel = () => {
    props.close();
  };

  const saveAndCloseWithData = useCallback(
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    (data: any) => {
      props.close();
      if (props.shouldInsertWithText) {
        props.add({ text, data });
      } else {
        props.add({ data });
      }
    },
    [props.shouldInsertWithText, text]
  );

  const submitForm = useCallback(() => {
    if (formRef.current) {
      formRef.current.submit();
    }
  }, [formRef.current]);

  const onOkClick = useCallback(() => {
    if (uniformsSchema) {
      submitForm();
    } else {
      saveAndCloseWithData({});
    }
  }, [submitForm, saveAndCloseWithData, hasSchema]);
  const onRemove = () => {
    props.remove();
    props.close();
  };

  return (
    <Dialog
      disableEnforceFocus={true}
      PaperProps={{
        style: { minWidth: 300 },
      }}
      open={props.open}
    >
      <DialogContent>
        {!props.shouldInsertWithText ? null : (
          <div style={{ marginBottom: '1em' }}>
            <TextField
              autoFocus={true}
              placeholder={'Text'}
              onChange={(e) => setText(e.target.value)}
              value={text}
            />
          </div>
        )}

        {hasSchema && uniformsSchema ? (
          <AutoForm
            ref={formRef}
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            model={props.data as any}
            schema={uniformsSchema}
            onSubmit={saveAndCloseWithData}
          >
            <AutoFields />
          </AutoForm>
        ) : null}
      </DialogContent>

      <DialogActions>
        <Button
          variant="text"
          onClick={onCancel}
          style={{ marginRight: 'auto' }}
        >
          {props.cancelLabel || 'Cancel'}
        </Button>
        {props.isActive ? (
          <Button variant="contained" color="secondary" onClick={onRemove}>
            {props.removeLabel || 'Remove'}
            <DeleteIcon style={{ marginLeft: 10 }} />
          </Button>
        ) : null}
        {hasSchema ? (
          <Button variant="contained" color="primary" onClick={onOkClick}>
            {props.submitLabel || 'Ok'}
            <DoneIcon style={{ marginLeft: 10 }} />
          </Button>
        ) : null}
      </DialogActions>
    </Dialog>
  );
}

export default Controls;