client/src/components/SlateEditor/index.js
author ymh <ymh.work@gmail.com>
Fri, 30 Nov 2018 10:53:15 +0100
changeset 183 f8f3af9e5c83
parent 173 0e6703cd0968
child 191 3f71ad81a5a9
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:
171
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
     1
import { Value } from 'slate';
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
     2
import Plain from 'slate-plain-serializer';
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
     3
import { Editor } from 'slate-react';
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
     4
import React from 'react';
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
     5
import { withNamespaces } from 'react-i18next';
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
     6
import { connect } from 'react-redux';
171
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
     7
import * as R from 'ramda';
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
     8
import HtmlSerializer from './HtmlSerializer';
161
a642639dbc07 Split scss files and adapt session page design
salimr <riwad.salim@yahoo.fr>
parents: 159
diff changeset
     9
import './SlateEditor.css';
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
    10
import { now } from '../../utils';
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    11
import Toolbar from './Toolbar';
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    12
import { getAutoSubmit } from '../../selectors/authSelectors';
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    13
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
    14
5
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
 *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    17
 * @type {Component}
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
8
6f572b6b6be3 try to make tests work again
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    20
class SlateEditor extends React.Component {
5
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
  /**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    23
   * Deserialize the initial editor state.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    24
   *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    25
   * @type {Object}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    26
   */
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    27
  constructor(props) {
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    28
    super(props);
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    29
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    30
    this.state = {
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    31
      value: props.note ? Value.fromJSON(JSON.parse(props.note.raw)) : Plain.deserialize(''),
16
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
    32
      startedAt: null,
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    33
      finishedAt: null,
159
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
    34
      enterKeyValue: 0,
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    35
    };
171
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    36
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    37
    this.editorRef = React.createRef();
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    38
  }
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    39
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    40
  get editor() {
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    41
    if(this.editorRef) {
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    42
      return this.editorRef.current;
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    43
    }
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    44
    return null;
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    45
  }
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    46
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
    47
  componentDidMount = () => {
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    48
    this.focus();
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    49
  }
5
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
   /**
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    52
   * On change, save the new state.
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    53
   *
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    54
   * @param {Change} change
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    55
   */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    56
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    57
  onChange = ({value, operations}) => {
16
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
    58
171
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    59
    const oldState = R.clone(this.state);
16
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
    60
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    61
    const newState = {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    62
      value
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    63
    };
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    64
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    65
    (operations || []).some((op) => {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    66
      if(['insert_text', 'remove_text', 'add_mark', 'remove_mark', 'set_mark', 'insert_node', 'merge_node', 'move_node', 'remove_node', 'set_node', 'split_node'].indexOf(op.type)>=0) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    67
        const tsnow = now();
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    68
        if(this.state.startedAt == null) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    69
          newState.startedAt = tsnow;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    70
        }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    71
        newState.finishedAt = tsnow;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    72
        return true;
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    73
      }
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    74
      return false;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    75
    });
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    76
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    77
    this.setState(newState, () => {
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    78
      if (typeof this.props.onChange === 'function') {
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    79
        this.props.onChange(R.clone(this.state), oldState, {value, operations});
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    80
      }
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    81
    })
5
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
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    84
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    85
  asPlain = () => {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    86
    return Plain.serialize(this.state.value);
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    87
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    88
15
4a8bbd314a46 Store note JSON data.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    89
  asRaw = () => {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    90
    return JSON.stringify(this.state.value.toJSON());
15
4a8bbd314a46 Store note JSON data.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    91
  }
4a8bbd314a46 Store note JSON data.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    92
17
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 16
diff changeset
    93
  asHtml = () => {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    94
    return HtmlSerializer.serialize(this.state.value);
17
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 16
diff changeset
    95
  }
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 16
diff changeset
    96
26
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
    97
  asCategories = () => {
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    98
    return this.state.value.document.getMarksByType('category').map((mark) => mark.data.toJS()).toArray();
26
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
    99
  }
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
   100
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   101
  clear = () => {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   102
    const value = Plain.deserialize('');
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   103
    this.setState({
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   104
      value,
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   105
      enterKeyValue: 0
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   106
    })
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   107
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   108
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   109
  focus = () => {
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   110
    if(this.editor) {
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   111
      this.editor.focus();
171
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
   112
    }
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   113
  }
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   114
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   115
  submitNote = () => {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   116
    this.setState({ enterKeyValue: 0 }, () => {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   117
      if (typeof this.props.submitNote === 'function') {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   118
        this.props.submitNote();
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   119
      }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   120
    });
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   121
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   122
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   123
  /**
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   124
   * On key down, if it's a formatting command toggle a mark.
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   125
   *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   126
   * @param {Event} e
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   127
   * @param {Change} change
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   128
   * @return {Change}
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   129
   */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   130
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   131
  onKeyUp = (e, editor, next) => {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   132
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   133
    const { value } = this.state;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   134
    const noteText = value.document.text.trim();
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   135
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   136
    if(e.key === "Enter" && noteText.length !== 0) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   137
      this.setState({ enterKeyValue: this.state.enterKeyValue + 1 });
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   138
    } else if ( e.getModifierState() || (e.key !== "Control" && e.key !== "Shift" && e.key !== "Meta" && e.key !== "Alt") ) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   139
      this.setState({ enterKeyValue: 0 });
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   140
    }
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   141
    return next();
72
7634b424f426 Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 64
diff changeset
   142
  }
7634b424f426 Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 64
diff changeset
   143
8
6f572b6b6be3 try to make tests work again
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   144
  /**
159
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   145
   * On key down, if it's a formatting command toggle a mark.
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   146
   *
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   147
   * @param {Event} e
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   148
   * @param {Change} change
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   149
   * @return {Change}
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   150
   */
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   151
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   152
  onKeyDown = (e, editor, next) => {
159
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   153
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   154
    const { value, enterKeyValue } = this.state;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   155
    const { autoSubmit } = this.props;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   156
    const noteText = value.document.text.trim();
159
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   157
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   158
    // we prevent empty first lines
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   159
    if(e.key === "Enter" && noteText.length === 0) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   160
      e.preventDefault();
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   161
      return next();
159
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   162
    }
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   163
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   164
    // Enter submit the note
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   165
    if(e.key === "Enter" && ( enterKeyValue === 2 || e.ctrlKey || autoSubmit ) && noteText.length !== 0) {
159
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   166
      e.preventDefault();
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   167
      this.submitNote();
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   168
      return next();
159
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   169
    }
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   170
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   171
    if (!e.ctrlKey) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   172
      return next();
159
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   173
    }
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   174
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   175
    e.preventDefault();
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   176
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   177
    // Decide what to do based on the key code...
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   178
    switch (e.key) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   179
      default: {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   180
        break;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   181
      }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   182
      // When "B" is pressed, add a "bold" mark to the text.
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   183
      case 'b': {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   184
        editor.toggleMark('bold');
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   185
        break;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   186
      }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   187
      case 'i': {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   188
        // When "U" is pressed, add an "italic" mark to the text.
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   189
        editor.toggleMark('italic');
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   190
        break;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   191
      }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   192
      case 'u': {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   193
        // When "U" is pressed, add an "underline" mark to the text.
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   194
        editor.toggleMark('underlined');
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   195
        break;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   196
      }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   197
    }
159
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   198
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   199
    return next();
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   200
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   201
  }
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   202
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   203
  // Add a `renderMark` method to render marks.
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   204
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   205
  renderMark = (props, editor, next) => {
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   206
    const { children, mark, attributes } = props
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   207
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   208
    switch (mark.type) {
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   209
      case 'bold':
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   210
        return <strong {...attributes}>{children}</strong>
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   211
      case 'code':
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   212
        return <code {...attributes}>{children}</code>
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   213
      case 'italic':
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   214
        return <em {...attributes}>{children}</em>
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   215
      case 'underlined':
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   216
        return <ins {...attributes}>{children}</ins>
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   217
      case 'category':
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   218
        let spanStyle = {
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   219
          backgroundColor: mark.data.get('color')
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   220
        };
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   221
        return <span {...attributes} style={ spanStyle } >{children}</span>
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   222
      default:
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   223
        return next();
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   224
    }
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   225
  }
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   226
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   227
  renderNode = (props, editor, next) => {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   228
    const { attributes, children, node } = props
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
    switch (node.type) {
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   231
      case 'block-quote':
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   232
        return <blockquote {...attributes}>{children}</blockquote>
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   233
      case 'bulleted-list':
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   234
        return <ul {...attributes}>{children}</ul>
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   235
      case 'heading-one':
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   236
        return <h1 {...attributes}>{children}</h1>
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   237
      case 'heading-two':
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   238
        return <h2 {...attributes}>{children}</h2>
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   239
      case 'list-item':
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   240
        return <li {...attributes}>{children}</li>
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   241
      case 'numbered-list':
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   242
        return <ol {...attributes}>{children}</ol>
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   243
      default:
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   244
        return next();
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   245
    }
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   246
  }
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   247
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   248
    /**
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   249
   * Render.
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   250
   *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   251
   * @return {Element}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   252
   */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   253
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   254
  render = () => (
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   255
    <div className="bg-secondary mb-5">
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   256
      <div className="sticky-top">
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   257
        <Toolbar
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   258
          value={this.state.value}
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   259
          editor={this.editor}
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   260
          note={this.props.note}
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   261
          annotationCategories={this.props.annotationCategories}
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   262
          isButtonDisabled={this.props.isButtonDisabled}
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   263
          submitNote={this.submitNote}
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   264
        />
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   265
      </div>
159
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   266
      <div className="editor-slatejs p-2">
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   267
        <Editor
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   268
          ref={this.editorRef}
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   269
          spellCheck
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   270
          placeholder={this.props.t('slate_editor.placeholder')}
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   271
          value={this.state.value}
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   272
          onChange={this.onChange}
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   273
          onKeyDown={this.onKeyDown}
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   274
          onKeyUp={this.onKeyUp}
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   275
          renderMark={this.renderMark}
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   276
          renderNode = {this.renderNode}
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   277
        />
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   278
      </div>
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   279
    </div>
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   280
  );
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   281
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   282
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   283
}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   284
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   285
/**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   286
 * Export.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   287
 */
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   288
function mapStateToProps(state, props) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   289
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   290
  const autoSubmit = getAutoSubmit(state);
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   291
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   292
  return {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   293
    autoSubmit,
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   294
  };
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   295
}
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   296
171
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
   297
export default withNamespaces("", {
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
   298
  innerRef: (ref) => {
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   299
    if(!ref) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   300
      return;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   301
    }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   302
    const wrappedRef = ref.getWrappedInstance();
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   303
    const editorRef = (wrappedRef && wrappedRef.props) ? wrappedRef.props.editorRef : null;
171
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
   304
    if(editorRef && editorRef.hasOwnProperty('current')) {
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   305
      editorRef.current = wrappedRef;
171
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
   306
    }
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
   307
  }
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   308
})(connect(mapStateToProps, null, null, { withRef: true })(SlateEditor));