client/src/components/SlateEditor/AnnotationPlugin.js
author ymh <ymh.work@gmail.com>
Tue, 13 Nov 2018 16:46:15 +0100
changeset 172 4b780ebbedc6
parent 168 ea92f4fe783d
permissions -rw-r--r--
- Upgrade libraries - Make things work again
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     1
function AnnotationPlugin(options) {
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     2
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     3
  const { onChange } = options
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     4
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     5
  return {
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
     6
    onSelect(event, editor, next) {
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     7
      event.preventDefault()
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     8
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
     9
      const { value } = editor
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 102
diff changeset
    10
      const { selection } = value
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 102
diff changeset
    11
      const { start, end} = selection
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    12
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    13
      if (selection.isCollapsed) {
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
    14
        return next();
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    15
      }
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    16
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
    17
      const nodes = [];
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    18
      let hasStarted = false;
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    19
      let hasEnded = false;
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    20
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    21
      // Keep only the relevant nodes,
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    22
      // i.e. nodes which are contained within selection
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 102
diff changeset
    23
      value.document.nodes.forEach((node) => {
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 102
diff changeset
    24
        if (start.isInNode(node)) {
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    25
          hasStarted = true;
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    26
        }
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    27
        if (hasStarted && !hasEnded) {
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    28
          nodes.push(node);
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    29
        }
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 102
diff changeset
    30
        if (end.isAtEndOfNode(node)) {
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    31
          hasEnded = true;
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    32
        }
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    33
      });
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    34
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    35
      // Concatenate the nodes text
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
    36
      const text = nodes.map((node) => {
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
    37
        let textStart = start.isInNode(node) ? start.offset : 0;
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
    38
        let textEnd = end.isAtEndOfNode(node) ? end.offset : node.text.length;
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
    39
        return node.text.substring(textStart,textEnd);
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
    40
      }).join('\n');
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    41
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    42
      if (onChange) {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 102
diff changeset
    43
        onChange(text, start.offset, end.offset);
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    44
      }
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
    45
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
    46
      return next();
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
    47
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    48
    }
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    49
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    50
  };
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    51
}
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    52
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    53
export default AnnotationPlugin;