client/src/components/SlateEditor.js
author salimr <riwad.salim@yahoo.fr>
Tue, 25 Sep 2018 02:02:13 +0200
changeset 157 5c3af4f10e92
parent 143 cfcbf4bc66f1
child 159 a4705c2b4544
permissions -rw-r--r--
Update slate editor
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
     1
import { Value } from 'slate'
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
     2
import Plain from 'slate-plain-serializer'
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
     3
import { Editor } from 'slate-react'
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     4
import React from 'react'
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
     5
import Portal from 'react-portal'
26
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
     6
import Immutable from 'immutable'
17
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 16
diff changeset
     7
import HtmlSerializer from '../HtmlSerializer'
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
     8
import AnnotationPlugin from '../AnnotationPlugin'
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
     9
import CategoriesTooltip from './CategoriesTooltip'
74
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents: 72
diff changeset
    10
import { now } from '../utils';
138
a1fb2ced3049 propagate annotations categories from session protocol definition
ymh <ymh.work@gmail.com>
parents: 103
diff changeset
    11
import { defaultAnnotationsCategories } from '../constants';
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    12
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    13
const plugins = [];
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    14
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    15
/**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    16
 * Define the default node type.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    17
 */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    18
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    19
const DEFAULT_NODE = 'paragraph'
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    20
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    21
/**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    22
 * Define a schema.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    23
 *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    24
 * @type {Object}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    25
 */
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
    26
// TODO Check if we can move this to the plugin using the schema option
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
    27
// https://docs.slatejs.org/reference/plugins/plugin.html#schema
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    28
const schema = {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    29
  nodes: {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    30
    'bulleted-list': props => <ul {...props.attributes}>{props.children}</ul>,
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    31
    'list-item': props => <li {...props.attributes}>{props.children}</li>,
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    32
    'numbered-list': props => <ol {...props.attributes}>{props.children}</ol>,
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    33
  },
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    34
  marks: {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    35
    bold: {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    36
      fontWeight: 'bold'
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    37
    },
25
e04714a1d4eb Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 21
diff changeset
    38
    category: props => {
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
    39
      const data = props.mark.data;
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
    40
      return <span style={{ backgroundColor: data.get('color') }}>{props.children}</span>
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    41
    },
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    42
    italic: {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    43
      fontStyle: 'italic'
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    44
    },
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    45
    underlined: {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    46
      textDecoration: 'underlined'
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    47
    }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    48
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    49
}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    50
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    51
const initialValue = Value.fromJSON({
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    52
  document: {
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    53
    nodes: [
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    54
      {
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    55
        object: 'block',
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    56
        type: 'paragraph',
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    57
        nodes: [
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    58
          {
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    59
            object: 'text',
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    60
            leaves: [
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    61
              {
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    62
                text: '',
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    63
              },
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    64
            ],
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    65
          },
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    66
        ],
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    67
      },
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    68
    ],
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    69
  },
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    70
})
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    71
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    72
/**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    73
 * The rich text example.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    74
 *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    75
 * @type {Component}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    76
 */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    77
8
6f572b6b6be3 try to make tests work again
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    78
class SlateEditor extends React.Component {
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    79
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    80
  /**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    81
   * Deserialize the initial editor state.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    82
   *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    83
   * @type {Object}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    84
   */
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    85
  constructor(props) {
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    86
    super(props);
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    87
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    88
    const annotationPlugin = AnnotationPlugin({
102
b0e36664f1f2 Add TextPositionSelector.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    89
      onChange: (text, start, end) => {
b0e36664f1f2 Add TextPositionSelector.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    90
        this.setState({
b0e36664f1f2 Add TextPositionSelector.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    91
          currentSelectionText: text,
b0e36664f1f2 Add TextPositionSelector.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    92
          currentSelectionStart: start,
b0e36664f1f2 Add TextPositionSelector.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    93
          currentSelectionEnd: end
b0e36664f1f2 Add TextPositionSelector.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    94
        });
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    95
      }
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    96
    });
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    97
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    98
    plugins.push(annotationPlugin);
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    99
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   100
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   101
    this.state = {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   102
      value: props.note ? initialValue : Plain.deserialize(''),
16
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   103
      startedAt: null,
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
   104
      finishedAt: null,
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   105
      currentSelectionText: '',
102
b0e36664f1f2 Add TextPositionSelector.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
   106
      currentSelectionStart: 0,
b0e36664f1f2 Add TextPositionSelector.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
   107
      currentSelectionEnd: 0,
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   108
      hoveringMenu: null,
26
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   109
      isPortalOpen: false,
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   110
      categories: Immutable.List([]),
72
7634b424f426 Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 64
diff changeset
   111
      isCheckboxChecked: false,
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   112
    };
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   113
  }
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   114
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   115
  componentDidMount = () => {
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   116
    this.updateMenu();
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   117
    this.focus();
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   118
  }
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   119
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   120
  componentDidUpdate = () => {
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   121
    this.updateMenu();
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   122
  }
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   123
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   124
   /**
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   125
   * On change, save the new state.
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   126
   *
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   127
   * @param {Change} change
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   128
   */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   129
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   130
  onChange = ({value}) => {
16
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   131
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   132
    let newState = {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   133
      value: value,
16
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   134
      startedAt: this.state.startedAt
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   135
    };
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   136
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   137
    const isEmpty = value.document.length === 0;
16
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   138
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   139
    // Reset timers when the text is empty
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   140
    if (isEmpty) {
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   141
      Object.assign(newState, {
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   142
        startedAt: null,
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   143
        finishedAt: null
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   144
      });
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   145
    } else {
74
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents: 72
diff changeset
   146
      Object.assign(newState, { finishedAt: now() });
16
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   147
    }
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   148
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   149
    // Store start time once when the first character is typed
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   150
    if (!isEmpty && this.state.startedAt === null) {
74
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents: 72
diff changeset
   151
      Object.assign(newState, { startedAt: now() });
16
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   152
    }
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   153
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   154
    this.setState(newState)
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   155
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   156
    if (typeof this.props.onChange === 'function') {
16
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   157
      this.props.onChange(newState);
8
6f572b6b6be3 try to make tests work again
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   158
    }
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   159
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   160
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   161
  /**
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   162
   * Check if the current selection has a mark with `type` in it.
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   163
   *
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   164
   * @param {String} type
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   165
   * @return {Boolean}
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   166
   */
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   167
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   168
  hasMark = type => {
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   169
    const { value } = this.state
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   170
    return value.activeMarks.some(mark => mark.type === type)
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   171
}
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   172
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   173
  /**
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   174
   * Check if the any of the currently selected blocks are of `type`.
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   175
   *
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   176
   * @param {String} type
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   177
   * @return {Boolean}
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   178
   */
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   179
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   180
  hasBlock = type => {
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   181
    const { value } = this.state
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   182
    return value.blocks.some(node => node.type === type)
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   183
}
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   184
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   185
  asPlain = () => {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   186
    return Plain.serialize(this.state.value);
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   187
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   188
15
4a8bbd314a46 Store note JSON data.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
   189
  asRaw = () => {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   190
    return JSON.stringify(this.state.value.toJSON());
15
4a8bbd314a46 Store note JSON data.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
   191
  }
4a8bbd314a46 Store note JSON data.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
   192
17
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 16
diff changeset
   193
  asHtml = () => {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   194
    return HtmlSerializer.serialize(this.state.value);
17
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 16
diff changeset
   195
  }
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 16
diff changeset
   196
26
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   197
  asCategories = () => {
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   198
    return this.state.categories
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   199
  }
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   200
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   201
  removeCategory = (categories, key, text) => {
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   202
    const categoryIndex = categories.findIndex(category => category.key === key && category.text === text)
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   203
    return categories.delete(categoryIndex)
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   204
  }
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   205
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   206
  clear = () => {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   207
    const value = Plain.deserialize('');
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   208
    this.onChange({value});
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   209
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   210
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   211
  focus = () => {
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   212
    this.refs.editor.focus();
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   213
  }
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   214
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   215
  /**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   216
   * On key down, if it's a formatting command toggle a mark.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   217
   *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   218
   * @param {Event} e
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   219
   * @param {Object} data
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   220
   * @param {Change} change
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   221
   * @return {Change}
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   222
   */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   223
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   224
  onKeyDown = (e, change) => {
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   225
    //   if (data.key === 'enter' && this.props.isChecked && typeof this.props.onEnterKeyDown === 'function') {
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   226
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   227
    //   e.preventDefault();
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   228
    //   this.props.onEnterKeyDown();
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   229
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   230
    //   return change;
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   231
    // }
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   232
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   233
    // if (!data.isMod) return
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   234
    if (!e.ctrlKey) return
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   235
        // Decide what to do based on the key code...
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   236
        switch (e.key) {
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   237
          // When "B" is pressed, add a "bold" mark to the text.
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   238
          case 'b': {
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   239
            e.preventDefault()
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   240
            change.toggleMark('bold')
64
aecde527900a Introduce adding note on "enter" key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 62
diff changeset
   241
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   242
            return true
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   243
          }
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   244
          case 'i': {
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   245
            // When "U" is pressed, add an "italic" mark to the text.
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   246
            e.preventDefault()
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   247
            change.toggleMark('italic')
72
7634b424f426 Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 64
diff changeset
   248
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   249
            return true
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   250
          }
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   251
          case 'u': {
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   252
            // When "U" is pressed, add an "underline" mark to the text.
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   253
            e.preventDefault()
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   254
            change.toggleMark('underlined')
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   255
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   256
            return true
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   257
          }
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   258
        }
64
aecde527900a Introduce adding note on "enter" key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 62
diff changeset
   259
    }
aecde527900a Introduce adding note on "enter" key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 62
diff changeset
   260
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   261
      /**
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   262
   * When a mark button is clicked, toggle the current mark.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   263
   *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   264
   * @param {Event} e
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   265
   * @param {String} type
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   266
   */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   267
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   268
  onClickMark = (e, type) => {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   269
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   270
      e.preventDefault()
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   271
    const { value } = this.state
26
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   272
    let { categories } = this.state
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   273
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   274
    let isPortalOpen = false;
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   275
25
e04714a1d4eb Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 21
diff changeset
   276
    if (type === 'category') {
e04714a1d4eb Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 21
diff changeset
   277
      // Can't use toggleMark here, because it expects the same object
e04714a1d4eb Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 21
diff changeset
   278
      // @see https://github.com/ianstormtaylor/slate/issues/873
e04714a1d4eb Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 21
diff changeset
   279
      if (this.hasMark('category')) {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   280
        const categoryMarks = value.activeMarks.filter(mark => mark.type === 'category')
26
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   281
        categoryMarks.forEach(mark => {
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   282
          const key = mark.data.get('key');
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   283
          const text = mark.data.get('text');
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   284
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   285
          categories = this.removeCategory(categories, key, text)
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   286
          const change = value.change().removeMark(mark)
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   287
          this.onChange(change)
26
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   288
        })
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   289
25
e04714a1d4eb Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 21
diff changeset
   290
      } else {
e04714a1d4eb Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 21
diff changeset
   291
        isPortalOpen = !this.state.isPortalOpen;
e04714a1d4eb Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 21
diff changeset
   292
      }
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
   293
    } else {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   294
      const change = value.change().toggleMark(type)
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   295
      this.onChange(change)
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
   296
    }
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
   297
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   298
    this.setState({
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   299
      state: value.change,
26
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   300
      isPortalOpen: isPortalOpen,
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   301
      categories: categories
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   302
    })
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   303
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   304
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   305
  /**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   306
   * When a block button is clicked, toggle the block type.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   307
   *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   308
   * @param {Event} e
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   309
   * @param {String} type
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   310
   */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   311
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   312
  onClickBlock = (e, type) => {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   313
    e.preventDefault()
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   314
    const { value } = this.state
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   315
    const change = value.change()
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   316
    const { document } = value
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   317
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   318
    // Handle everything but list buttons.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   319
    if (type !== 'bulleted-list' && type !== 'numbered-list') {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   320
      const isActive = this.hasBlock(type)
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   321
      const isList = this.hasBlock('list-item')
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   322
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   323
      if (isList) {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   324
        change
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   325
          .setBlocks(isActive ? DEFAULT_NODE : type)
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   326
          .unwrapBlock('bulleted-list')
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   327
          .unwrapBlock('numbered-list')
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   328
      }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   329
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   330
      else {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   331
       change
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   332
          .setBlocks(isActive ? DEFAULT_NODE : type)
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   333
      }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   334
    }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   335
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   336
    // Handle the extra wrapping required for list buttons.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   337
    else {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   338
      const isList = this.hasBlock('list-item')
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   339
      const isType = value.blocks.some((block) => {
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   340
        return !!document.getClosest(block.key, parent => parent.type === type)
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   341
      })
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   342
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   343
      if (isList && isType) {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   344
       change
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   345
          .setBlocks(DEFAULT_NODE)
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   346
          .unwrapBlock('bulleted-list')
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   347
          .unwrapBlock('numbered-list')
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   348
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   349
      } else if (isList) {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   350
        change
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   351
          .unwrapBlock(type === 'bulleted-list' ? 'numbered-list' : 'bulleted-list')
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   352
          .wrapBlock(type)
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   353
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   354
      } else {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   355
        change
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   356
          .setBlocks('list-item')
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   357
          .wrapBlock(type)
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   358
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   359
      }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   360
    }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   361
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   362
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   363
    this.onChange(change)
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   364
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   365
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   366
  onPortalOpen = (portal) => {
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   367
    // When the portal opens, cache the menu element.
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   368
    this.setState({ hoveringMenu: portal.firstChild })
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   369
  }
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   370
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   371
  onPortalClose = (portal) => {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   372
    let { value } = this.state
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   373
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   374
    this.setState({
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   375
      value: value.change,
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   376
      isPortalOpen: false
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   377
    })
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   378
  }
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   379
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   380
  onCategoryClick = (category) => {
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   381
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   382
    const { value, currentSelectionText, currentSelectionStart, currentSelectionEnd } = this.state;
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   383
    const change = value.change()
26
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   384
    let { categories } = this.state;
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   385
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   386
    const categoryMarks = value.activeMarks.filter(mark => mark.type === 'category')
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   387
    categoryMarks.forEach(mark => change.removeMark(mark));
26
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   388
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   389
    change.addMark({
25
e04714a1d4eb Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 21
diff changeset
   390
      type: 'category',
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   391
      data: {
26
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   392
        text: currentSelectionText,
102
b0e36664f1f2 Add TextPositionSelector.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
   393
        selection: {
b0e36664f1f2 Add TextPositionSelector.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
   394
          start: currentSelectionStart,
b0e36664f1f2 Add TextPositionSelector.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
   395
          end: currentSelectionEnd,
b0e36664f1f2 Add TextPositionSelector.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
   396
        },
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   397
        color: category.color,
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   398
        key: category.key
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   399
      }
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   400
    })
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   401
    this.onChange(change)
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   402
26
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   403
    Object.assign(category, {
103
dc906a578616 Fix error.
Alexandre Segura <mex.zktk@gmail.com>
parents: 102
diff changeset
   404
      text: currentSelectionText,
dc906a578616 Fix error.
Alexandre Segura <mex.zktk@gmail.com>
parents: 102
diff changeset
   405
      selection: {
dc906a578616 Fix error.
Alexandre Segura <mex.zktk@gmail.com>
parents: 102
diff changeset
   406
        start: currentSelectionStart,
dc906a578616 Fix error.
Alexandre Segura <mex.zktk@gmail.com>
parents: 102
diff changeset
   407
        end: currentSelectionEnd,
dc906a578616 Fix error.
Alexandre Segura <mex.zktk@gmail.com>
parents: 102
diff changeset
   408
      },
26
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   409
    });
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   410
    categories = categories.push(category);
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   411
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   412
    this.setState({
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   413
      value: value,
26
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   414
      isPortalOpen: false,
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   415
      categories: categories
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   416
    });
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   417
  }
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   418
72
7634b424f426 Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 64
diff changeset
   419
  onButtonClick = () => {
7634b424f426 Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 64
diff changeset
   420
    if (typeof this.props.onButtonClick === 'function') {
7634b424f426 Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 64
diff changeset
   421
      this.props.onButtonClick();
7634b424f426 Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 64
diff changeset
   422
    }
7634b424f426 Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 64
diff changeset
   423
  }
7634b424f426 Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 64
diff changeset
   424
7634b424f426 Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 64
diff changeset
   425
  onCheckboxChange = (e) => {
7634b424f426 Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 64
diff changeset
   426
    if (typeof this.props.onCheckboxChange === 'function') {
7634b424f426 Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 64
diff changeset
   427
      this.props.onCheckboxChange(e);
7634b424f426 Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 64
diff changeset
   428
    }
7634b424f426 Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 64
diff changeset
   429
  }
7634b424f426 Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 64
diff changeset
   430
8
6f572b6b6be3 try to make tests work again
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   431
  /**
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   432
   * Render.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   433
   *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   434
   * @return {Element}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   435
   */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   436
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   437
  render = () => {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   438
    return (
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   439
      <div className="bg-secondary mb-5">
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   440
        <div className="sticky-top">
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   441
        {this.renderToolbar()}
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   442
        </div>
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   443
        {this.renderEditor()}
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   444
    </div>
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   445
    )
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   446
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   447
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   448
  /**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   449
   * Render the toolbar.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   450
   *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   451
   * @return {Element}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   452
   */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   453
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   454
  renderToolbar = () => {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   455
    return (
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   456
      <div className="menu toolbar-menu d-flex bg-secondary">
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   457
          {this.renderMarkButton('bold', 'format_bold')}
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   458
          {this.renderMarkButton('italic', 'format_italic')}
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   459
          {this.renderMarkButton('underlined', 'format_underlined')}
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   460
          {this.renderMarkButton('category', 'label')}
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
   461
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 74
diff changeset
   462
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   463
          {this.renderBlockButton('numbered-list', 'format_list_numbered')}
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   464
          {this.renderBlockButton('bulleted-list', 'format_list_bulleted')}
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   465
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   466
          {this.renderToolbarButtons()}
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   467
      </div>
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   468
    )
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   469
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   470
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
   471
  renderToolbarCheckbox = () => {
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
   472
    return (
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   473
      <div className="checkbox float-right">
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   474
        <label className="mr-2">
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   475
          <input type="checkbox" checked={this.props.isChecked} onChange={this.onCheckboxChange} /><small className="text-muted ml-1"> Appuyer sur <kbd className="bg-danger text-muted ml-1">Entrée</kbd> pour ajouter une note</small>
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
   476
        </label>
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
   477
      </div>
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
   478
    )
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
   479
  }
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
   480
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 74
diff changeset
   481
  renderToolbarButtons = () => {
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 74
diff changeset
   482
    return (
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 74
diff changeset
   483
      <div>
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   484
        <button type="button" className="btn btn-primary btn-sm text-secondary float-right mr-5" disabled={this.props.isButtonDisabled} onClick={this.onButtonClick}>
143
cfcbf4bc66f1 Remove react-bootstrap from components except Modal, Collapse and Dropdown
salimr <riwad.salim@yahoo.fr>
parents: 138
diff changeset
   485
          { this.props.note ? 'Save note' : 'Ajouter' }
cfcbf4bc66f1 Remove react-bootstrap from components except Modal, Collapse and Dropdown
salimr <riwad.salim@yahoo.fr>
parents: 138
diff changeset
   486
        </button>
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   487
        { !this.props.note && this.renderToolbarCheckbox() }
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 74
diff changeset
   488
      </div>
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 74
diff changeset
   489
    );
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 74
diff changeset
   490
  }
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 74
diff changeset
   491
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   492
  /**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   493
   * Render a mark-toggling toolbar button.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   494
   *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   495
   * @param {String} type
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   496
   * @param {String} icon
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   497
   * @return {Element}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   498
   */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   499
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   500
  renderMarkButton = (type, icon) => {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   501
    const isActive = this.hasMark(type)
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   502
    const onMouseDown = e => this.onClickMark(e, type)
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   503
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   504
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   505
    return (
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   506
      <span className="button text-primary" onMouseDown={onMouseDown} data-active={isActive}>
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   507
        <span className="material-icons">{icon}</span>
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   508
      </span>
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   509
    )
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   510
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   511
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   512
    // Add a `renderMark` method to render marks.
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   513
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   514
    renderMark = props => {
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   515
      const { children, mark, attributes } = props
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   516
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   517
      switch (mark.type) {
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   518
        case 'bold':
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   519
          return <strong {...attributes}>{children}</strong>
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   520
        case 'code':
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   521
          return <code {...attributes}>{children}</code>
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   522
        case 'italic':
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   523
          return <em {...attributes}>{children}</em>
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   524
        case 'underlined':
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   525
          return <ins {...attributes}>{children}</ins>
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   526
      }
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   527
  }
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   528
  /**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   529
   * Render a block-toggling toolbar button.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   530
   *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   531
   * @param {String} type
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   532
   * @param {String} icon
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   533
   * @return {Element}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   534
   */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   535
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   536
  renderBlockButton = (type, icon) => {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   537
    let isActive = this.hasBlock(type)
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   538
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   539
    if (['numbered-list', 'bulleted-list'].includes(type)) {
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   540
      const { value } = this.state
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   541
      const parent = value.document.getParent(value.blocks.first().key)
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   542
      isActive = this.hasBlock('list-item') && parent && parent.type === type
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   543
    }
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   544
    const onMouseDown = e => this.onClickBlock(e, type)
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   545
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   546
    return (
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   547
      <span className="button text-primary" onMouseDown={onMouseDown} data-active={isActive}>
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   548
        <span className="material-icons">{icon}</span>
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   549
      </span>
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   550
    )
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   551
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   552
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   553
  renderNode = props => {
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   554
    const { attributes, children, node } = props
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   555
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   556
    switch (node.type) {
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   557
      case 'block-quote':
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   558
        return <blockquote {...attributes}>{children}</blockquote>
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   559
      case 'bulleted-list':
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   560
        return <ul {...attributes}>{children}</ul>
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   561
      case 'heading-one':
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   562
        return <h1 {...attributes}>{children}</h1>
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   563
      case 'heading-two':
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   564
        return <h2 {...attributes}>{children}</h2>
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   565
      case 'list-item':
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   566
        return <li {...attributes}>{children}</li>
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   567
      case 'numbered-list':
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   568
        return <ol {...attributes}>{children}</ol>
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   569
    }
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   570
}
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   571
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   572
  /**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   573
   * Render the Slate editor.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   574
   *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   575
   * @return {Element}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   576
   */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   577
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   578
  renderEditor = () => {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   579
    return (
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   580
      <div className="editor-wrapper sticky-bottom p-2">
35
97106bacb24e Improve session page design.
Alexandre Segura <mex.zktk@gmail.com>
parents: 26
diff changeset
   581
      <div className="editor-slatejs">
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   582
        {this.renderHoveringMenu()}
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   583
        <Editor
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   584
          ref="editor"
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   585
          spellCheck
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   586
          autoFocus
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   587
          placeholder={'Votre espace de prise de note...'}
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   588
          schema={schema}
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
   589
          plugins={plugins}
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   590
          value={this.state.value}
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   591
          onChange={this.onChange}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   592
          onKeyDown={this.onKeyDown}
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   593
          renderMark={this.renderMark}
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   594
          renderNode = {this.renderNode}
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   595
        />
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   596
      </div>
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   597
      </div>
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   598
    )
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   599
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   600
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   601
  renderHoveringMenu = () => {
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   602
    return (
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   603
      <Portal ref="portal"
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   604
        isOpened={this.state.isPortalOpen} isOpen={this.state.isPortalOpen}
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   605
        onOpen={this.onPortalOpen}
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   606
        onClose={this.onPortalClose}
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   607
        closeOnOutsideClick={false} closeOnEsc={true}>
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   608
        <div className="hovering-menu">
138
a1fb2ced3049 propagate annotations categories from session protocol definition
ymh <ymh.work@gmail.com>
parents: 103
diff changeset
   609
          <CategoriesTooltip categories={this.props.annotationCategories || defaultAnnotationsCategories} onCategoryClick={this.onCategoryClick} />
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   610
        </div>
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   611
      </Portal>
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   612
    )
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   613
  }
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   614
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   615
  updateMenu = () => {
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   616
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 35
diff changeset
   617
    const { hoveringMenu } = this.state
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   618
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   619
    if (!hoveringMenu) return
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   620
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   621
    // if (state.isBlurred || state.isCollapsed) {
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   622
    //   hoveringMenu.removeAttribute('style')
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   623
    //   return
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   624
    // }
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   625
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   626
    const selection = window.getSelection()
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   627
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   628
    if (selection.isCollapsed) {
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   629
      return
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   630
    }
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   631
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   632
    const range = selection.getRangeAt(0)
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   633
    const rect = range.getBoundingClientRect()
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   634
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   635
    hoveringMenu.style.opacity = 1
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   636
    hoveringMenu.style.top = `${rect.top + window.scrollY + hoveringMenu.offsetHeight}px`
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   637
    hoveringMenu.style.left = `${rect.left + window.scrollX - hoveringMenu.offsetWidth / 2 + rect.width / 2}px`
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   638
  }
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   639
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   640
}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   641
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   642
/**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   643
 * Export.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   644
 */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   645
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   646
export default SlateEditor