Skip to content

Commit

Permalink
[PLAY-807] Created extensions prop For advanced RTE (#2560)
Browse files Browse the repository at this point in the history
**What does this PR do?** A clear and concise description with your
runway ticket url.

This PR adds a new prop called `moreExtensions` to the advanced variant
of the RTE to allows devs to be able to add as many extensions they want
to the editor and hav e the button show up in the ellipsis dropdown.

[RUNWAY STORY](https://nitro.powerhrg.com/runway/backlog_items/PLAY-807)

**Screenshots:** Screenshots to visualize your addition/change

![Screenshot 2023-05-17 at 5 40 37
PM](https://github.com/powerhome/playbook/assets/73710701/7489abec-59ce-4415-bb39-de62b1b7a65f)


**How to test?** Steps to confirm the desired behavior:
1. Scroll down to the Advanced (More Extensions) doc example. 
2. Click the ellipsis drowpdown to see the menu.
  • Loading branch information
nidaqg authored May 18, 2023
1 parent a812590 commit d1bafe7
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 13 deletions.
2 changes: 2 additions & 0 deletions playbook-website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"@fortawesome/fontawesome-pro": "^6.2.1",
"@mapbox/mapbox-gl-draw": "^1.4.1",
"@rails/webpacker": "5.2.1",
"@tiptap/extension-highlight": "^2.0.3",
"@tiptap/extension-horizontal-rule": "^2.0.3",
"@tiptap/extension-link": "^2.0.2",
"@tiptap/pm": "^2.0.2",
"@tiptap/react": "^2.0.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import React, { useState } from 'react'

import Flex from '../../pb_flex/_flex'
import PbReactPopover from '../../pb_popover/_popover'
import Icon from '../../pb_icon/_icon'
import Nav from '../../pb_nav/_nav'
import NavItem from '../../pb_nav/_item'

const MoreExtensionsDropdown = ({extensions}: any) => {
const [showPopover, setShowPopover] = useState(false)

const handleTogglePopover = () => {
setShowPopover(true)
}

const handlePopoverClose = (shouldClosePopover: boolean) => {
setShowPopover(!shouldClosePopover)
}


const popoverReference = (
<button
className="toolbar_button"
onClick={handleTogglePopover}
>
<Flex
align="center"
className="toolbar_button_icon"
justify="center"
>
<Icon icon="ellipsis" size="lg" />
</Flex>
</button>

);

return (
<PbReactPopover
closeOnClick='outside'
padding='none'
placement="bottom"
reference={popoverReference}
shouldClosePopover={handlePopoverClose}
show={showPopover}
>
<Nav
paddingTop={extensions.length > 1 ? "xs" : "none"}
paddingBottom={extensions.length > 1 ? "xs" : "none"}
variant="subtle"
>
{extensions && extensions.map(({ icon, text, onclick, isActive}:any, index:number) => (
<NavItem
cursor="pointer"
className={`pb_tiptap_toolbar_dropdown_list_item ${isActive ? "is-active" : ""}`}
iconLeft={icon}
key={`${text}_${index}`}
margin='none'
onClick={()=> {onclick(); setShowPopover(false)}}
text={text}
paddingTop='xxs'
paddingBottom='xxs'
/>
))}
</Nav>
</PbReactPopover>
)
}

export default MoreExtensionsDropdown
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import ToolbarDropdown from "./ToolbarDropdown";
import ToolbarNodes from "./ToolbarNodes";
import { ToolbarTypes } from "./EditorTypes";
import ToolbarHistoryItems from "./ToolbarHistory";
import MoreExtensionsDropdown from "./MoreExtensionsDropdown";

const EditorToolbar = ({ editor }:any) => {
const EditorToolbar = ({ editor, extensions }:any) => {
const toolbaritems = [
{
icon: "bold",
Expand Down Expand Up @@ -51,10 +52,16 @@ const EditorToolbar = ({ editor }:any) => {
)}
<SectionSeparator orientation="vertical" />
<ToolbarNodes editor={editor} />
{
extensions && (
<>
<MoreExtensionsDropdown extensions={extensions}/>
</>
)
}
</FlexItem>
<ToolbarHistoryItems editor={editor} />
</Flex>
{/* <SectionSeparator /> */}
</Background>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,17 @@ const popoverReference = (
activeCount === 2 ? (
activeItems[1]
) : (
activeCount === 1 ? (
activeItems[0] || null
) : (
<Flex align="center" key="paragraph" gap="xs">
<Icon icon="paragraph" size="lg" />
<div>Paragraph</div>
<Flex className={showPopover ? "fa-flip-vertical" : ""} display="inline_flex">
<Icon fixedWidth icon="angle-down" margin-left="xs" />
</Flex>
</Flex>
)
)
}
</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ type RichTextEditorProps = {
data?: { [key: string]: string },
focus?: boolean,
id?: string,
inline?: boolean,
inline?: boolean,
extensions?: { [key: string]: string }[],
name?: string,
onChange: (html: string, text: string) => void,
placeholder?: string,
Expand All @@ -56,6 +57,7 @@ const RichTextEditor = (props: RichTextEditorProps) => {
data = {},
focus = false,
inline = false,
extensions,
name,
onChange = noop,
placeholder,
Expand Down Expand Up @@ -162,7 +164,7 @@ const RichTextEditor = (props: RichTextEditorProps) => {
{
advancedEditor ? (
<div className='pb_rich_text_editor_advanced_container'>
<EditorToolbar editor={advancedEditor}/>
<EditorToolbar extensions={extensions} editor={advancedEditor}/>
{ children }
</div>
) : (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React from 'react'
import { RichTextEditor } from '../..'
import { useEditor, EditorContent } from "@tiptap/react"
import StarterKit from "@tiptap/starter-kit"
import Link from '@tiptap/extension-link'

import HorizontalRule from "@tiptap/extension-horizontal-rule"
import Highlight from '@tiptap/extension-highlight'


const RichTextEditorMoreExtensions = (props) => {

const editor = useEditor({
extensions: [
StarterKit,
Link,
HorizontalRule,
Highlight.configure({ multicolor: true })
],
content:"Add your text here. You can format your text, add links, quotes, and bullets."
})
if (!editor) {
return null
}

const ExtensionsList = [
{
icon: "horizontal-rule",
isActive: editor.isActive("horizontalRule"),
text: "Horizontal Rule",
onclick: () => editor.chain().focus().setHorizontalRule().run(),
},
{
icon: "highlighter",
isActive: editor.isActive("highlight"),
text: "Highlighter",
onclick: () => editor.chain().focus().toggleHighlight().run(),
}
]


return (
<div>
<RichTextEditor
advancedEditor={editor}
extensions={ExtensionsList}
{...props}
>
<EditorContent editor={editor}/>
</RichTextEditor>
</div>
)
}

export default RichTextEditorMoreExtensions
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
This variant allows you to optionally include any of [Tiptap’s 53 extensions](https://tiptap.dev/extensions) within any advanced editor by using the `extensions` prop.

__NOTE__: In order to leverage this prop, you __must__ install the extension you need in your project, import it and pass it to the extensions array as shown in this example with the HorizontalRule and the Highlight extensions.

In order to add the extension to the editor toolbar, create an array of objects (as shown in the ExtensionsList array in the example below). Each object in this array should include:

`icon`: the icon to display within the toolbar dropdown (any Fontawesome icons can be used)
`isActive`: sets the extension to active within the dropdown, when applicable
`text`: the label within the toolbar dropdown
`onclick`: initializes the extension when it’s clicked within the dropdown (snytax varies with extension, see Tiptap's docs for more information)

This array can then be passed to the `extensions` prop and all extensions in the array will be rendered in the ellipsis dropdown.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ examples:
react:
- rich_text_editor_default: Default
- rich_text_editor_advanced_default: Advanced Default
- rich_text_editor_more_extensions: Advanced (Extra Extensions)
- rich_text_editor_simple: Simple
- rich_text_editor_attributes: Attributes
- rich_text_editor_focus: Focus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export { default as RichTextEditorTemplates } from './_rich_text_editor_template
export { default as RichTextEditorToolbarBottom } from './_rich_text_editor_toolbar_bottom.jsx'
export { default as RichTextEditorInline } from './_rich_text_editor_inline.jsx'
export { default as RichTextEditorPreview } from './_rich_text_editor_preview.jsx'
export { default as RichTextEditorAdvancedDefault } from './_rich_text_editor_advanced_default.jsx'
export { default as RichTextEditorAdvancedDefault } from './_rich_text_editor_advanced_default.jsx'
export { default as RichTextEditorMoreExtensions } from './_rich_text_editor_more_extensions.jsx'
19 changes: 11 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@
core-js-pure "^3.25.1"
regenerator-runtime "^0.13.11"

"@babel/runtime@^7.1.2":
"@babel/runtime@^7.1.2", "@babel/runtime@^7.7.6":
version "7.21.5"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.21.5.tgz#8492dddda9644ae3bda3b45eabe87382caee7200"
integrity sha512-8jI69toZqqcsnqGGqwGS4Qb1VwLOEp4hz+CXPywcvjs60u3B4Pom/U/7rm4W8tMOYEB+E9wgD0mW1l3r8qlI9Q==
Expand All @@ -1122,13 +1122,6 @@
dependencies:
regenerator-runtime "^0.13.11"

"@babel/runtime@^7.7.6":
version "7.21.5"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.21.5.tgz#8492dddda9644ae3bda3b45eabe87382caee7200"
integrity sha512-8jI69toZqqcsnqGGqwGS4Qb1VwLOEp4hz+CXPywcvjs60u3B4Pom/U/7rm4W8tMOYEB+E9wgD0mW1l3r8qlI9Q==
dependencies:
regenerator-runtime "^0.13.11"

"@babel/template@^7.18.10", "@babel/template@^7.20.7", "@babel/template@^7.3.3":
version "7.20.7"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.20.7.tgz#a15090c2839a83b02aa996c0b4994005841fd5a8"
Expand Down Expand Up @@ -2261,6 +2254,11 @@
resolved "https://registry.yarnpkg.com/@tiptap/extension-heading/-/extension-heading-2.0.2.tgz#0cc9238b97275c4f290cd579286bf5bd8e235b5d"
integrity sha512-99PL9Rx8mioo13SNuIkhZm1VW6UuUKKG0b7fAimsunaEbbLNtToXIasS68pX8BkgWnsBfDhR0HyhnpIBRg7W0w==

"@tiptap/extension-highlight@^2.0.3":
version "2.0.3"
resolved "https://registry.yarnpkg.com/@tiptap/extension-highlight/-/extension-highlight-2.0.3.tgz#4a52de6666dfe4a80b018aa43805d2d220e90219"
integrity sha512-NrtibY8cZkIjZMQuHRrKd4php+plOvAoSo8g3uVFu275I/Ixt5HqJ53R4voCXs8W8BOBRs2HS2QX8Cjh79XhtA==

"@tiptap/extension-history@^2.0.2":
version "2.0.2"
resolved "https://registry.yarnpkg.com/@tiptap/extension-history/-/extension-history-2.0.2.tgz#83f817b842ec6b6e1cd8e300afa529916fca92a0"
Expand All @@ -2271,6 +2269,11 @@
resolved "https://registry.yarnpkg.com/@tiptap/extension-horizontal-rule/-/extension-horizontal-rule-2.0.2.tgz#7e3ecfe5edb277cfba47824f36ffe7f97fb5f675"
integrity sha512-QUqNnHqRXMZRE6uFRX0UDFn07JDiiNukPRCIL4vyV/YJ6HmQiBjbsS/1hLjAwBuqaN8dtlHLdR4PjMuA6qrUYw==

"@tiptap/extension-horizontal-rule@^2.0.3":
version "2.0.3"
resolved "https://registry.yarnpkg.com/@tiptap/extension-horizontal-rule/-/extension-horizontal-rule-2.0.3.tgz#5c67db2c0bf3bc14a8aab80df584bee5aa23fbeb"
integrity sha512-SZRUSh07b/M0kJHNKnfBwBMWrZBEm/E2LrK1NbluwT3DBhE+gvwiEdBxgB32zKHNxaDEXUJwUIPNC3JSbKvPUA==

"@tiptap/extension-italic@^2.0.2":
version "2.0.2"
resolved "https://registry.yarnpkg.com/@tiptap/extension-italic/-/extension-italic-2.0.2.tgz#e7ed247c00fdcdf3d64d0b844b798fdfa41b34d4"
Expand Down

0 comments on commit d1bafe7

Please sign in to comment.