client/src/components/SlateEditor/CategoryButton.js
author ymh <ymh.work@gmail.com>
Fri, 30 Nov 2018 10:53:15 +0100
changeset 183 f8f3af9e5c83
parent 173 0e6703cd0968
permissions -rw-r--r--
Change the settings to avoid using Session authentication for rest framework as it raise exceptions in case client and backend are on the same domain On the filter, adapt to take into account new version of django_filters
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
import React from 'react';
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
import * as R from 'ramda';
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
import { PortalWithState } from 'react-portal';
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
import CategoriesTooltip from './CategoriesTooltip';
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
import { defaultAnnotationsCategories } from '../../constants';
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
/**
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 * Render a category toolbar button.
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 *
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
 * @param {String} type
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
 * @param {String} icon
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
 * @return {Element}
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
 */
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
export default class CategoryButton extends React.Component {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
  constructor(props) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
    super(props);
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
    this.hoveringMenuRef = React.createRef();
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
  }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
  get hoveringMenu() {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
    if(this.hoveringMenuRef) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
      return this.hoveringMenuRef.current;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
    }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
    return null;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
  }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
  updateMenu = () => {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
    const hoveringMenu = this.hoveringMenu;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
    if (!hoveringMenu) return
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
    const selection = window.getSelection()
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
    if (selection.isCollapsed) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
      return
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
    }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
    const range = selection.getRangeAt(0)
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
    const rect = range.getBoundingClientRect()
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
    hoveringMenu.style.opacity = 1
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
    hoveringMenu.style.top = `${rect.top + rect.height + window.scrollY + hoveringMenu.offsetHeight}px`
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
    hoveringMenu.style.left = `${rect.left + window.scrollX - hoveringMenu.offsetWidth / 2 + rect.width / 2}px`
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
  }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
  render = () => {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
    const isActive = this.props.isActive;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
    const onClickCategoryButton = this.props.onClickCategoryButton;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
    const onCategoryClick = this.props.onCategoryClick;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
    const annotationCategories = this.props.annotationCategories;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
    const markActivation = "button sticky-top" + ((!isActive)?" text-primary":" text-dark");
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
    return (
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
      <PortalWithState
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
        // closeOnOutsideClick
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
        closeOnEsc
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
        onOpen={this.updateMenu}
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
      >
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
        {({ openPortal, closePortal, isOpen, portal }) => {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
          const onMouseDown = R.partial(onClickCategoryButton, [openPortal, closePortal, isOpen]);
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
          const onCategoryClickHandler = R.partial(onCategoryClick, [closePortal,]);
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
          return (
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
            <React.Fragment>
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
              <span className={markActivation} onMouseDown={onMouseDown} data-active={isActive}>
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
                <span className="material-icons">label</span>
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
              </span>
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
              {portal(
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
                <div className="hovering-menu" ref={this.hoveringMenuRef}>
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
                  <CategoriesTooltip categories={annotationCategories || defaultAnnotationsCategories} onCategoryClick={onCategoryClickHandler} />
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
                </div>
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
              )}
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
            </React.Fragment>
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
          )}
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
        }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
      </PortalWithState>
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
    )
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
  }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
}