-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds rich text editor upload element
- Loading branch information
Showing
24 changed files
with
633 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,9 @@ | |
text-overflow: ellipsis; | ||
overflow: hidden; | ||
} | ||
|
||
&--selected { | ||
box-shadow: $focus-box-shadow; | ||
outline: none; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/admin/components/forms/field-types/RichText/elements/upload/Button/Fields/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import React, { Fragment, useState, useEffect } from 'react'; | ||
import { useConfig, useAuth } from '@payloadcms/config-provider'; | ||
import { useWatchForm } from '../../../../../../Form/context'; | ||
import Relationship from '../../../../../Relationship'; | ||
import Select from '../../../../../Select'; | ||
|
||
const createOptions = (collections, permissions) => collections.reduce((options, collection) => { | ||
if (permissions?.collections?.[collection.slug]?.read?.permission && collection?.admin?.enableRichTextRelationship && collection.upload) { | ||
return [ | ||
...options, | ||
{ | ||
label: collection.labels.plural, | ||
value: collection.slug, | ||
}, | ||
]; | ||
} | ||
|
||
return options; | ||
}, []); | ||
|
||
const UploadFields = () => { | ||
const { collections } = useConfig(); | ||
const { permissions } = useAuth(); | ||
|
||
const [options, setOptions] = useState(() => createOptions(collections, permissions)); | ||
|
||
const { getData } = useWatchForm(); | ||
const { relationTo } = getData(); | ||
|
||
useEffect(() => { | ||
setOptions(createOptions(collections, permissions)); | ||
}, [collections, permissions]); | ||
|
||
return ( | ||
<Fragment> | ||
<Select | ||
required | ||
label="Relation To" | ||
name="relationTo" | ||
options={options} | ||
/> | ||
{relationTo && ( | ||
<Relationship | ||
label="Upload" | ||
name="value" | ||
relationTo={relationTo} | ||
required | ||
/> | ||
)} | ||
</Fragment> | ||
); | ||
}; | ||
|
||
export default UploadFields; |
33 changes: 33 additions & 0 deletions
33
src/admin/components/forms/field-types/RichText/elements/upload/Button/index.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
@import '../../../../../../../scss/styles.scss'; | ||
|
||
.upload-rich-text-button { | ||
.btn { | ||
margin-right: base(1); | ||
} | ||
|
||
&__modal { | ||
display: flex; | ||
align-items: center; | ||
height: 100%; | ||
|
||
&.payload__modal-item--enterDone { | ||
@include blur-bg; | ||
} | ||
} | ||
|
||
&__header { | ||
width: 100%; | ||
margin-bottom: $baseline; | ||
display: flex; | ||
justify-content: space-between; | ||
|
||
h3 { | ||
margin: 0; | ||
} | ||
|
||
svg { | ||
width: base(1.5); | ||
height: base(1.5); | ||
} | ||
} | ||
} |
122 changes: 122 additions & 0 deletions
122
src/admin/components/forms/field-types/RichText/elements/upload/Button/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import React, { Fragment, useCallback, useEffect, useState } from 'react'; | ||
import { Modal, useModal } from '@faceless-ui/modal'; | ||
import { Transforms } from 'slate'; | ||
import { ReactEditor, useSlate } from 'slate-react'; | ||
import { useConfig } from '@payloadcms/config-provider'; | ||
import ElementButton from '../../Button'; | ||
import UploadIcon from '../../../../../../icons/Upload'; | ||
import Form from '../../../../../Form'; | ||
import MinimalTemplate from '../../../../../../templates/Minimal'; | ||
import Button from '../../../../../../elements/Button'; | ||
import Submit from '../../../../../Submit'; | ||
import X from '../../../../../../icons/X'; | ||
import Fields from './Fields'; | ||
import { requests } from '../../../../../../../api'; | ||
|
||
import './index.scss'; | ||
|
||
const initialFormData = {}; | ||
|
||
const baseClass = 'upload-rich-text-button'; | ||
|
||
const insertUpload = (editor, { value, relationTo }) => { | ||
const text = { text: ' ' }; | ||
|
||
const upload = { | ||
type: 'upload', | ||
value, | ||
relationTo, | ||
children: [ | ||
text, | ||
], | ||
}; | ||
|
||
const nodes = [upload, { children: [{ text: '' }] }]; | ||
|
||
if (editor.blurSelection) { | ||
Transforms.select(editor, editor.blurSelection); | ||
} | ||
|
||
Transforms.insertNodes(editor, nodes); | ||
|
||
const currentPath = editor.selection.anchor.path[0]; | ||
const newSelection = { anchor: { path: [currentPath + 1, 0], offset: 0 }, focus: { path: [currentPath + 1, 0], offset: 0 } }; | ||
|
||
Transforms.select(editor, newSelection); | ||
ReactEditor.focus(editor); | ||
}; | ||
|
||
const UploadButton: React.FC<{path: string}> = ({ path }) => { | ||
const { open, closeAll } = useModal(); | ||
const editor = useSlate(); | ||
const { serverURL, routes: { api }, collections } = useConfig(); | ||
const [renderModal, setRenderModal] = useState(false); | ||
const [loading, setLoading] = useState(false); | ||
const [hasEnabledCollections] = useState(() => collections.find(({ upload, admin: { enableRichTextRelationship } }) => upload && enableRichTextRelationship)); | ||
const modalSlug = `${path}-add-upload`; | ||
|
||
const handleAddUpload = useCallback(async (_, { relationTo, value }) => { | ||
setLoading(true); | ||
|
||
const res = await requests.get(`${serverURL}${api}/${relationTo}/${value}?depth=0`); | ||
const json = await res.json(); | ||
|
||
insertUpload(editor, { value: { id: json.id }, relationTo }); | ||
closeAll(); | ||
setRenderModal(false); | ||
setLoading(false); | ||
}, [editor, closeAll, api, serverURL]); | ||
|
||
useEffect(() => { | ||
if (renderModal) { | ||
open(modalSlug); | ||
} | ||
}, [renderModal, open, modalSlug]); | ||
|
||
if (!hasEnabledCollections) return null; | ||
|
||
return ( | ||
<Fragment> | ||
<ElementButton | ||
className={baseClass} | ||
format="upload" | ||
onClick={() => setRenderModal(true)} | ||
> | ||
<UploadIcon /> | ||
</ElementButton> | ||
{renderModal && ( | ||
<Modal | ||
slug={modalSlug} | ||
className={`${baseClass}__modal`} | ||
> | ||
<MinimalTemplate> | ||
<header className={`${baseClass}__header`}> | ||
<h3>Add Upload</h3> | ||
<Button | ||
buttonStyle="none" | ||
onClick={() => { | ||
closeAll(); | ||
setRenderModal(false); | ||
}} | ||
> | ||
<X /> | ||
</Button> | ||
</header> | ||
<Form | ||
onSubmit={handleAddUpload} | ||
initialData={initialFormData} | ||
disabled={loading} | ||
> | ||
<Fields /> | ||
<Submit> | ||
Add upload | ||
</Submit> | ||
</Form> | ||
</MinimalTemplate> | ||
</Modal> | ||
)} | ||
</Fragment> | ||
); | ||
}; | ||
|
||
export default UploadButton; |
Oops, something went wrong.