# All files / composer/utils/draft-js-custom-plugins/short-link ShortLinkTooltip.jsx

5.88% Statements 3/51
0% Branches 0/10
0% Functions 0/22
6.52% Lines 3/46

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156

    7x

    7x

    7x

     import React from 'react';
    import PropTypes from 'prop-types';
    import { EditorState } from '@bufferapp/draft-js';
    import addUnshortenedLink from '../unshortened-link/modifiers/addUnshortenedLink';
    import Button from '../../../components/Button';

    import styles from './ShortLinkTooltip.css';

    const getEmptyTooltipState = () => ({
      entityKey: null,
      unshortenedLink: null,
      shortLink: null,
      position: null, // Data structure in getNewTooltipPosition()
    });

    const getNewTooltipPosition = ({ top, left }) => ({
      top,
      left,
    });

    const findEntityRangeForKey = (contentBlock, entityKey, callback) => {
      contentBlock.findEntityRanges((character) => (character.getEntity() === entityKey), callback);
    };

    class ShortLinkTooltip extends React.Component {
      static propTypes = {
        callbacks: PropTypes.object.isRequired,
        store: PropTypes.object.isRequired,
        onLinkUnshortened: PropTypes.func,
        classNames: PropTypes.object,
      };

      static defaultProps = {
        onLinkUnshortened: () => {},
        classNames: {},
      };

      constructor(props) {
        super(props);

        this.state = getEmptyTooltipState();
        this.hideTooltipTimeoutId = null;
        this.isClickOnTooltipInProgress = false;
      }

      componentWillMount() {
        this.props.callbacks.onShortLinkMouseOver = this.onShortLinkMouseOver;
        this.props.callbacks.onShortLinkMouseOut = this.onShortLinkMouseOut;
        this.props.callbacks.onEditorStateChange = this.onEditorStateChange;
      }

      componentWillUnmount() {
        this.props.callbacks.onShortLinkMouseOver = null;
        this.props.callbacks.onShortLinkMouseOut = null;
        this.props.callbacks.onEditorStateChange = null;
      }

      onShortLinkMouseOver = (shortLinkData) => {
        clearTimeout(this.hideTooltipTimeoutId);
        this.showTooltip(shortLinkData);
      };

      onTooltipMouseOver = () => clearTimeout(this.hideTooltipTimeoutId);
      onShortLinkMouseOut = () => (this.hideTooltipTimeoutId = setTimeout(this.hideTooltip, 20));
      onTooltipMouseOut = () => (this.hideTooltipTimeoutId = setTimeout(this.hideTooltip, 20));
      onTooltipMouseDown = () => (this.isClickOnTooltipInProgress = true);
      onTooltipMouseUp = () => (this.isClickOnTooltipInProgress = false);

      onEditorStateChange = () => {
        // Don't hide tooltip when it's being clicked (and editor receives blur event
        // as a result)
        if (!this.isClickOnTooltipInProgress) this.hideTooltip();
      };

      onUnshortenButtonClick = () => {
        const { getEditorState, setEditorState } = this.props.store;
        const { entityKey, unshortenedLink, shortLink } = this.state;

        let wasLinkUnshortened = false;
        const editorState = getEditorState();
        const contentState = editorState.getCurrentContent();
        let newEditorState;

        contentState.getBlockMap().forEach((contentBlock) => {
          findEntityRangeForKey(contentBlock, entityKey, (start, end) => {
            newEditorState = addUnshortenedLink(editorState, contentBlock, {
              shortLink,
              unshortenedLink,
              indices: [start, end],
            }, { isUserAction: true });

            // Re-focus composer
            newEditorState = EditorState.forceSelection(newEditorState, newEditorState.getSelection());

            wasLinkUnshortened = true;
          });
        });

        if (wasLinkUnshortened) {
          setEditorState(newEditorState);
          this.props.onLinkUnshortened(unshortenedLink);
        }
      };

      isTooltipVisible = () => this.state.position !== null;

      showTooltip = ({ entityKey, unshortenedLink, shortLink, relativePosition }) => {
        const { width, height } = relativePosition;
        let { top, left } = relativePosition;

        top = top + height;
        left = left + width / 2;

        this.setState({
          entityKey,
          unshortenedLink,
          shortLink,
          position: getNewTooltipPosition({ top, left }),
        });
      };

      hideTooltip = () => {
        if (!this.isTooltipVisible()) return;

        this.setState(getEmptyTooltipState());
      };

      render = () => {
        const { unshortenedLink, position } = this.state;

        const tooltipClassName = [
          this.isTooltipVisible() ? styles.visibleTooltip : styles.hiddenTooltip,
          this.isTooltipVisible() ? this.props.classNames.visible : this.props.classNames.hidden,
        ].join(' ');

        return (
          <span
            className={tooltipClassName} style={position} ref="tooltip"
            onMouseOver={this.onTooltipMouseOver} onMouseOut={this.onTooltipMouseOut}
            onMouseDown={this.onTooltipMouseDown} onMouseUp={this.onTooltipMouseUp}
            aria-hidden={!this.isTooltipVisible()}
          >
            <span className={styles.tooltipContent}>
              {unshortenedLink}

              <Button className={styles.button} onClick={this.onUnshortenButtonClick}>
                Unshorten
              </Button>
            </span>
          </span>
        );
      };
    }

    export default ShortLinkTooltip;