diff --git a/src/components/Sidebar.js b/src/components/Sidebar.js index fb329c17..0b3148cf 100644 --- a/src/components/Sidebar.js +++ b/src/components/Sidebar.js @@ -3,9 +3,7 @@ * * License: MIT */ - -import React, { Component } from "react"; -import ReactDOM from "react-dom"; +import React, { useState, useEffect, useRef, useContext } from "react"; import classNames from "classnames"; import icons from "../icons"; @@ -23,65 +21,47 @@ import "setimmediate"; import PluginsModal from "./PluginsModal"; import { getSelectedBlockElement } from "../utils"; -class BlockStyles extends Component { - static contextType = ActionsContext; - - constructor(props) { - super(props); - this.state = { - isOpen: false - }; - - this.onModalOpenClick = this.onModalOpenClick.bind(this); - this.onChange = this.onChange.bind(this); - this.toggleModalVisibility = this.toggleModalVisibility.bind(this); - this.renderButton = this.renderButton.bind(this); - } - onChange(editorState) { - this.props.onChange(editorState); - } +const BlockStyles = ({ i18n, plugins, editorState, onChange, open, modalOptions, onClose }) => { + const context = useContext(ActionsContext); + const [isOpen, setIsOpen] = useState(false); - onModalOpenClick(e) { + const onModalOpenClick = (e) => { e.preventDefault(); document.body.classList.add("megadraft-modal--open"); - this.setState({ isOpen: true }); - } - - toggleModalVisibility() { - this.setState({ isOpen: !this.state.isOpen }); - } - - renderModal() { - return ( - - ); - } - - renderModalButton() { - return ( - - ); - } - - renderButton(item) { + setIsOpen(true); + }; + + const toggleModalVisibility = () => { + setIsOpen(!isOpen); + }; + + const renderModal = () => ( + + ); + + const renderModalButton = () => ( + + ); + + const renderButton = (item) => { const Button = item.buttonComponent; return ( @@ -89,7 +69,7 @@ class BlockStyles extends Component { key={item.type} className="sidemenu__item" onClick={() => { - this.context.onAction({ + context.onAction({ type: SIDEBAR_ADD_PLUGIN, pluginName: item.title }); @@ -98,212 +78,158 @@ class BlockStyles extends Component { - ); + if (hideSidebarOnBlur && !hasFocus) { + return null; } -} -export class SideMenu extends Component { - static contextType = ActionsContext; + return ( + + ); +}; - constructor(props) { - super(props); - this.state = { - open: false - }; - this.toggle = this.toggle.bind(this); - this.onChange = this.onChange.bind(this); - } - onChange(editorState) { - this.props.onChange(editorState); - } - toggle() { - this.context.onAction({ - type: this.state.open ? SIDEBAR_SHRINK : SIDEBAR_EXPAND - }); - this.setState({ - open: !this.state.open - }); - } - render() { - const className = classNames("sidemenu", { - "sidemenu--open": this.state.open +const SideMenu = ({ editorHasFocus, hideSidebarOnBlur, maxSidebarButtons, i18n, editorState, plugins, onChange }) => { + const context = useContext(ActionsContext); + const [open, setOpen] = useState(false); + + const toggle = () => { + context.onAction({ + type: open ? SIDEBAR_SHRINK : SIDEBAR_EXPAND }); - return ( -
  • - + setOpen(!open); + }; + + return ( +
  • + + +
  • + ); +}; - - - ); - } -} -export default class SideBar extends Component { - constructor(props) { - super(props); - this.state = { top: 0 }; - this.onChange = this.onChange.bind(this); - } - getValidSidebarPlugins() { - let plugins = []; - for (let plugin of this.props.plugins) { - if ( - !plugin.buttonComponent || - typeof plugin.buttonComponent !== "function" - ) { - continue; - } - plugins.push(plugin); - } - return plugins; - } - onChange(editorState) { - this.props.onChange(editorState); - } +const SideBar = ({ readOnly, i18n, editorState, onChange, maxSidebarButtons, editorHasFocus, hideSidebarOnBlur, modalOptions, plugins }) => { + const [top, setTop] = useState(0); + const containerEl = useRef(null); - componentDidUpdate() { - if (this.updatingPosition) { - clearImmediate(this.updatingPosition); - } - this.updatingPosition = null; - this.updatingPosition = setImmediate(() => { - return this.setBarPosition(); - }); - } + const getValidSidebarPlugins = () => { + return plugins.filter(plugin => plugin.buttonComponent && typeof plugin.buttonComponent === "function"); + }; - setBarPosition() { - const container = ReactDOM.findDOMNode(this.containerEl); - const editor = container ? container.parentElement : null; + const handleChange = (editorState) => { + onChange(editorState); + }; - const selection = window.getSelection(); - if (selection.rangeCount === 0) { - return null; - } + useEffect(() => { + const updatePosition = () => { + const container = containerEl.current; // Acesso ao container + const editor = container ? container.parentElement : null; - const element = getSelectedBlockElement(selection.getRangeAt(0)); + const selection = window.getSelection(); + if (selection.rangeCount === 0) { + return null; + } - if (!element || !container || !editor || !editor.contains(element)) { - return; - } + const element = getSelectedBlockElement(selection.getRangeAt(0)); - const containerTop = - container.getBoundingClientRect().top - - document.documentElement.clientTop; - let top = element.getBoundingClientRect().top - 4 - containerTop; - top = Math.max(0, Math.floor(top)); + if (!element || !container || !editor || !editor.contains(element)) { + return; + } - if (this.state.top !== top) { - this.setState({ - top: top - }); - } - } + const containerTop = container.getBoundingClientRect().top - document.documentElement.clientTop; + let newTop = element.getBoundingClientRect().top - 4 - containerTop; + newTop = Math.max(0, Math.floor(newTop)); - render() { - if (this.props.readOnly) { - return null; - } - return ( -
    { - this.containerEl = el; - }} - className="sidebar" - > -
    -
      - -
    -
    + if (top !== newTop) { + setTop(newTop); + } + }; + + const positionUpdater = setImmediate(updatePosition); + + return () => { + clearImmediate(positionUpdater); + }; + }, [top]); + + if (readOnly) { + return null; + } + + return ( +
    +
    +
      + +
    - ); - } -} +
    + ); +}; + +export default SideBar;