client/src/components/SlateEditor/Toolbar.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 ToolbarButtons from './ToolbarButtons';
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
import MarkButton from './MarkButton';
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
import CategoryButton from './CategoryButton';
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
import BlockButton from './BlockButton';
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
 * Define the default node type.
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
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
const DEFAULT_NODE = 'paragraph'
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
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
/**
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
 * Render the toolbar.
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
 *
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
 * @return {Element}
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
export default class Toolbar extends React.Component {
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
   * Deserialize the initial editor state.
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
   *
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
   * @type {Object}
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
  constructor(props) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
    super(props);
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
    this.editorRef = React.createRef();
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
  }
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
  /**
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
   * Check if the current selection has a mark with `type` in it.
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
   *
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
   * @param {String} type
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
   * @return {Boolean}
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
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
  hasMark = type => {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
    const { value } = this.props;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
    return value.activeMarks.some(mark => mark.type === type);
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
  }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
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
   * Check if the any of the currently selected blocks are of `type`.
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
   *
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
   * @param {String} type
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
   * @return {Boolean}
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
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
  hasBlock = type => {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
    const { value } = this.props;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
    return value.blocks.some(node => node.type === type)
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
  }
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
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
   * When a mark button is clicked, toggle the current mark.
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
   *
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
   * @param {Event} e
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
   * @param {String} type
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
   */
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
  onClickMark = (e, type) => {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
    this.props.editor.toggleMark(type)
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
  }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
  isBlockActive = (type) => {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
    let isActive = this.hasBlock(type)
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
    if (['numbered-list', 'bulleted-list'].includes(type)) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
      const { value } = this.props;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
      const firstBlock = value.blocks.first();
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
      if(firstBlock) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
        const parent = value.document.getParent(firstBlock.key);
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
        isActive = this.hasBlock('list-item') && parent && parent.type === type;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
      }
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
    return isActive;
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
  onClickCategoryButton = (openPortal, closePortal, isOpen, e) => {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
    e.preventDefault();
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
    const { value, editor } = this.props;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
    // Can't use toggleMark here, because it expects the same object
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
    // @see https://github.com/ianstormtaylor/slate/issues/873
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
    if (this.hasMark('category')) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
      value.activeMarks.filter(mark => mark.type === 'category')
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
        .forEach(mark => editor.removeMark(mark));
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
      closePortal();
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
    } else {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
      openPortal();
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
    }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
  }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
  getSelectionParams = (value) => {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
    const { selection } = value
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
    const { start, end} = selection
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
    if (selection.isCollapsed) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
      return {};
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
    }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
    const nodes = [];
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
    let hasStarted = false;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
    let hasEnded = false;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
    // Keep only the relevant nodes,
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
    // i.e. nodes which are contained within selection
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
    value.document.nodes.forEach((node) => {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
      if (start.isInNode(node)) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
        hasStarted = true;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
      }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
      if (hasStarted && !hasEnded) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
        nodes.push(node);
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
      }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
      if (end.isAtEndOfNode(node)) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
        hasEnded = true;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
      }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
    });
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
    // Concatenate the nodes text
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
    const text = nodes.map((node) => {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
      let textStart = start.isInNode(node) ? start.offset : 0;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
      let textEnd = end.isInNode(node) ? end.offset : node.text.length;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
      return node.text.substring(textStart,textEnd);
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
    }).join('\n');
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
    return {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
      currentSelectionText: text,
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
      currentSelectionStart: start.offset,
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
      currentSelectionEnd: end.offset
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
    };
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
  }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
  onCategoryClick = (closePortal, category) => {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
    const { value, editor } = this.props;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
    const { currentSelectionText, currentSelectionStart, currentSelectionEnd } = this.getSelectionParams(value);
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
    if(!currentSelectionText) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
      closePortal();
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
      return;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
    }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
    const categoryMarks = value.activeMarks.filter(mark => mark.type === 'category')
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
    categoryMarks.forEach(mark => this.editor.removeMark(mark));
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
    editor.addMark({
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
      type: 'category',
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
      data: {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
        text: currentSelectionText,
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
        selection: {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
          start: currentSelectionStart,
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
          end: currentSelectionEnd,
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
        },
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
        color: category.color,
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
        key: category.key,
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
        name: category.name,
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
        comment: category.comment
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
      }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
    })
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
    closePortal();
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
  }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
    /**
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
   * When a block button is clicked, toggle the block type.
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
   *
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
   * @param {Event} e
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
   * @param {String} type
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
   */
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
  onClickBlock = (e, type) => {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
    e.preventDefault();
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
    const { editor, value } = this.props;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
    // Handle everything but list buttons.
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
    if (type !== 'bulleted-list' && type !== 'numbered-list') {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
      const isActive = this.hasBlock(type)
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
      const isList = this.hasBlock('list-item')
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
      if (isList) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
        editor
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
          .setBlocks(isActive ? DEFAULT_NODE : type)
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
          .unwrapBlock('bulleted-list')
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
          .unwrapBlock('numbered-list')
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
      }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
      else {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
       editor
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
          .setBlocks(isActive ? DEFAULT_NODE : type)
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
      }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
    }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
    // Handle the extra wrapping required for list buttons.
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
    else {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
      const isList = this.hasBlock('list-item')
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
      const isType = value.blocks.some((block) => {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
        return !!document.getClosest(block.key, parent => parent.type === type)
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
      })
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
      if (isList && isType) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
       editor
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
          .setBlocks(DEFAULT_NODE)
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
          .unwrapBlock('bulleted-list')
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
          .unwrapBlock('numbered-list')
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
      } else if (isList) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
        editor
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
          .unwrapBlock(type === 'bulleted-list' ? 'numbered-list' : 'bulleted-list')
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
          .wrapBlock(type)
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
      } else {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
        editor
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
          .setBlocks('list-item')
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
          .wrapBlock(type)
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
      }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
    }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
  }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
  render = () => {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
    return (
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
      <div className="menu toolbar-menu d-flex sticky-top bg-secondary">
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
        <MarkButton icon='format_bold' isActive={this.hasMark('bold')} onMouseDown={(e) => this.onClickMark(e, 'bold')} />
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
        <MarkButton icon='format_italic' isActive={this.hasMark('italic')} onMouseDown={(e) => this.onClickMark(e, 'italic')} />
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
        <MarkButton icon='format_underlined' isActive={this.hasMark('underlined')} onMouseDown={(e) => this.onClickMark(e, 'underlined')} />
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
        <CategoryButton
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
          isActive={this.hasMark('category')}
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
          onClickCategoryButton={this.onClickCategoryButton}
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
          onCategoryClick={this.onCategoryClick}
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
          annotationCategories={this.props.annotationCategories}
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
        />
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
        <BlockButton icon='format_list_numbered' isActive={this.isBlockActive('numbered-list')} onMouseDown={e => this.onClickBlock(e, 'numbered-list')} />
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
        <BlockButton icon='format_list_bulleted' isActive={this.isBlockActive('bulleted-list')} onMouseDown={e => this.onClickBlock(e, 'bulleted-list')} />
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
        <ToolbarButtons
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
          hasNote={!!this.props.note}
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
          isButtonDisabled={this.props.isButtonDisabled}
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
          submitNote={this.props.submitNote}
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
        />
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
      </div>
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
    )
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
  }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
}