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