diff --git a/packages/sn-editor-react/src/components/controls/accordion-control.tsx b/packages/sn-editor-react/src/components/controls/accordion-control.tsx new file mode 100644 index 000000000..319de7a1c --- /dev/null +++ b/packages/sn-editor-react/src/components/controls/accordion-control.tsx @@ -0,0 +1,269 @@ +import { + Button, + createStyles, + Dialog, + DialogActions, + DialogContent, + DialogTitle, + IconButton, + IconButtonProps, + makeStyles, + TextField, + Theme, + Tooltip, + useTheme, +} from '@material-ui/core' +import { ArrowDropDown, Close, DragHandle } from '@material-ui/icons' + +import { Editor } from '@tiptap/react' +import React, { FC, useCallback, useRef, useState } from 'react' +import { renderToString } from 'react-dom/server' +import { useLocalization } from '../../hooks' + +const useStyles = makeStyles((theme: Theme) => { + return createStyles({ + ListIcon: { + paddingLeft: '0px', + height: '32px', + '& .MuiIconButton-label': { + flexDirection: 'column', + height: 'inherit', + justifyContent: 'center', + '& .icon-container': { + '&:first-of-type': { + marginBottom: '-14px', + }, + height: '23px', + position: 'relative', + '& .down-arrow': { + top: '0', + right: '0', + position: 'absolute', + marginTop: '0px', + marginRight: '-13px', + }, + }, + }, + }, + accordion: { + display: 'flex', + flexDirection: 'column', + rowGap: '15px', + border: '2px solid', + borderColor: theme.palette.primary.main, + position: 'relative', + padding: '10px', + borderRadius: '10px', + '& .panel-close-button': { + position: 'absolute', + right: '0', + top: '0', + }, + }, + accordionContainer: { + display: 'flex', + flexDirection: 'column', + rowGap: '20px', + }, + actionPanel: { + display: 'flex', + flex: 1, + justifyContent: 'space-between', + }, + dialog: { + '& .MuiDialogContent-root': { + padding: '8px 15px', + }, + }, + }) +}) + +interface AccordionControlProps { + editor: Editor + buttonProps?: Partial +} + +type TAccordions = { + title: string + body: string +} + +type PanelPros = { + title: string + body: string +} + +const Panel = ({ title, body }: PanelPros) => { + return ( +
+
+

+ + + {title} + +

+
+
+
{body}
+
+
+ ) +} + +const initialAccordion = { title: '', body: '' } + +export const AccordionControl: FC = ({ buttonProps, editor }) => { + const [open, setOpen] = useState(false) + + const [accordions, setAccordions] = useState([initialAccordion]) + const form = useRef(null) + + const theme = useTheme() + + const classes = useStyles(theme) + const localization = useLocalization() + + const handleClickOpen = () => { + setOpen(true) + } + + const handleClose = useCallback(() => { + setAccordions([initialAccordion]) + + setOpen(false) + }, []) + + const handleSubmit = () => { + if (accordions.length === 0) { + handleClose() + return + } + + if (!form.current?.reportValidity()) { + return + } + + const panelGroup = renderToString( + <> +
+ {accordions.map((item, index) => { + return + })} +
+ {/* eslint-disable-next-line react/self-closing-comp*/} +

+ , + ) + + editor.chain().focus().insertContent(panelGroup).run() + + handleClose() + } + + const handleClosePanel = (index: number) => { + setAccordions((prev) => { + return prev.filter((_, i) => i !== index) + }) + } + + return ( + <> + + +
+ + +
+
+ + +
+
+
+ + {localization.accordionControl.title} + +
+ {accordions.map((accordion, index) => { + return ( +
+ + handleClosePanel(index)} {...buttonProps}> + + + + + { + const newAccordions = [...accordions] + + newAccordions[index] = { ...accordions[index], title: e.target.value } + + setAccordions(newAccordions) + }} + /> + + { + const newAccordions = [...accordions] + + newAccordions[index] = { ...accordions[index], body: e.target.value } + + setAccordions(newAccordions) + }} + /> +
+ ) + })} +
+
+ +
+ + +
+ + +
+
+
+
+ + ) +} diff --git a/packages/sn-editor-react/src/components/controls/html-editor-control.tsx b/packages/sn-editor-react/src/components/controls/html-editor-control.tsx index a410f737a..be14c8d11 100644 --- a/packages/sn-editor-react/src/components/controls/html-editor-control.tsx +++ b/packages/sn-editor-react/src/components/controls/html-editor-control.tsx @@ -28,6 +28,11 @@ export const HTMLEditorControl: FC = ({ editor, buttonPr setOpen(true) } + const saveHTMLContent = () => { + editor.chain().focus().setContent(html, true).run() + setOpen(false) + } + const handleClose = () => { if (editor.getHTML() === html) { setOpen(false) @@ -38,9 +43,7 @@ export const HTMLEditorControl: FC = ({ editor, buttonPr if (!confirmResult) { return } - editor.commands.setContent(html) - - setOpen(false) + saveHTMLContent() } return ( @@ -60,7 +63,7 @@ export const HTMLEditorControl: FC = ({ editor, buttonPr aria-labelledby="html-editor-control-title" fullWidth maxWidth="lg" - onExited={() => { }}> + onExited={() => {}}> {localization.HTMLEditorControl.title} @@ -74,8 +77,7 @@ export const HTMLEditorControl: FC = ({ editor, buttonPr