|
| 1 | +import React, { Fragment, useCallback } from 'react'; |
| 2 | +import { Modal, useModal } from '@faceless-ui/modal'; |
| 3 | +import { Transforms } from 'slate'; |
| 4 | +import { useSlate, ReactEditor } from 'slate-react'; |
| 5 | +import MinimalTemplate from '../../../../../../../src/admin/components/templates/Minimal'; |
| 6 | +import ElementButton from '../../../../../../../src/admin/components/forms/field-types/RichText/elements/Button'; |
| 7 | +import X from '../../../../../../../src/admin/components/icons/X'; |
| 8 | +import Button from '../../../../../../../src/admin/components/elements/Button'; |
| 9 | +import Form from '../../../../../../../src/admin/components/forms/Form'; |
| 10 | +import Submit from '../../../../../../../src/admin/components/forms/Submit'; |
| 11 | +import reduceFieldsToValues from '../../../../../../../src/admin/components/forms/Form/reduceFieldsToValues'; |
| 12 | +import Text from '../../../../../../../src/admin/components/forms/field-types/Text'; |
| 13 | +import Checkbox from '../../../../../../../src/admin/components/forms/field-types/Checkbox'; |
| 14 | +import Select from '../../../../../../../src/admin/components/forms/field-types/Select'; |
| 15 | + |
| 16 | +import './index.scss'; |
| 17 | + |
| 18 | +const baseClass = 'button-rich-text-button'; |
| 19 | + |
| 20 | +const initialFormData = { |
| 21 | + style: 'primary', |
| 22 | +}; |
| 23 | + |
| 24 | +const insertButton = (editor, { href, label, style, newTab = false }: any) => { |
| 25 | + const text = { text: ' ' }; |
| 26 | + const button = { |
| 27 | + type: 'button', |
| 28 | + href, |
| 29 | + style, |
| 30 | + newTab, |
| 31 | + label, |
| 32 | + children: [ |
| 33 | + text, |
| 34 | + ], |
| 35 | + }; |
| 36 | + |
| 37 | + const nodes = [button, { children: [{ text: '' }] }]; |
| 38 | + |
| 39 | + if (editor.blurSelection) { |
| 40 | + Transforms.select(editor, editor.blurSelection); |
| 41 | + } |
| 42 | + |
| 43 | + Transforms.insertNodes(editor, nodes); |
| 44 | + |
| 45 | + const currentPath = editor.selection.anchor.path[0]; |
| 46 | + const newSelection = { anchor: { path: [currentPath + 1, 0], offset: 0 }, focus: { path: [currentPath + 1, 0], offset: 0 } }; |
| 47 | + |
| 48 | + Transforms.select(editor, newSelection); |
| 49 | + ReactEditor.focus(editor); |
| 50 | +}; |
| 51 | + |
| 52 | +const ToolbarButton: React.FC<{path: string}> = ({ path }) => { |
| 53 | + const { open, closeAll } = useModal(); |
| 54 | + const editor = useSlate(); |
| 55 | + |
| 56 | + const handleAddButton = useCallback((fields) => { |
| 57 | + const data = reduceFieldsToValues(fields); |
| 58 | + insertButton(editor, data); |
| 59 | + closeAll(); |
| 60 | + }, [editor, closeAll]); |
| 61 | + |
| 62 | + const modalSlug = `${path}-add-button`; |
| 63 | + |
| 64 | + return ( |
| 65 | + <Fragment> |
| 66 | + <ElementButton |
| 67 | + className={baseClass} |
| 68 | + format="button" |
| 69 | + onClick={() => open(modalSlug)} |
| 70 | + > |
| 71 | + Button |
| 72 | + </ElementButton> |
| 73 | + <Modal |
| 74 | + slug={modalSlug} |
| 75 | + className={`${baseClass}__modal`} |
| 76 | + > |
| 77 | + <MinimalTemplate> |
| 78 | + <header className={`${baseClass}__header`}> |
| 79 | + <h3>Add button</h3> |
| 80 | + <Button |
| 81 | + buttonStyle="none" |
| 82 | + onClick={closeAll} |
| 83 | + > |
| 84 | + <X /> |
| 85 | + </Button> |
| 86 | + </header> |
| 87 | + <Form |
| 88 | + onSubmit={handleAddButton} |
| 89 | + initialData={initialFormData} |
| 90 | + > |
| 91 | + <Text |
| 92 | + label="Label" |
| 93 | + name="label" |
| 94 | + required |
| 95 | + /> |
| 96 | + <Text |
| 97 | + label="URL" |
| 98 | + name="href" |
| 99 | + required |
| 100 | + /> |
| 101 | + <Select |
| 102 | + label="Style" |
| 103 | + name="style" |
| 104 | + options={[ |
| 105 | + { |
| 106 | + label: 'Primary', |
| 107 | + value: 'primary', |
| 108 | + }, |
| 109 | + { |
| 110 | + label: 'Secondary', |
| 111 | + value: 'secondary', |
| 112 | + }, |
| 113 | + ]} |
| 114 | + /> |
| 115 | + <Checkbox |
| 116 | + label="Open in new tab" |
| 117 | + name="newTab" |
| 118 | + /> |
| 119 | + <Submit> |
| 120 | + Add button |
| 121 | + </Submit> |
| 122 | + </Form> |
| 123 | + </MinimalTemplate> |
| 124 | + </Modal> |
| 125 | + </Fragment> |
| 126 | + ); |
| 127 | +}; |
| 128 | + |
| 129 | +export default ToolbarButton; |
0 commit comments