| author | ymh <ymh.work@gmail.com> |
| Wed, 05 Sep 2018 12:27:52 +0200 | |
| changeset 155 | e55ae84508bf |
| parent 25 | e04714a1d4eb |
| child 157 | 5c3af4f10e92 |
| permissions | -rw-r--r-- |
|
17
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
1 |
import React from 'react' |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
2 |
import { Html } from 'slate' |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
3 |
|
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
4 |
const BLOCK_TAGS = { |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
5 |
p: 'paragraph', |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
6 |
} |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
7 |
|
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
8 |
// Add a dictionary of mark tags. |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
9 |
const MARK_TAGS = { |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
10 |
em: 'italic', |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
11 |
strong: 'bold', |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
12 |
u: 'underline', |
|
25
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
13 |
category: 'span' |
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
14 |
} |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
15 |
|
|
17
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
16 |
const rules = [ |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
17 |
// Block rules |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
18 |
{ |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
19 |
deserialize(el, next) { |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
20 |
const type = BLOCK_TAGS[el.tagName] |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
21 |
if (!type) return |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
22 |
return { |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
23 |
kind: 'block', |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
24 |
type: type, |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
25 |
nodes: next(el.children) |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
26 |
} |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
27 |
}, |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
28 |
serialize(object, children) { |
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
29 |
if (object.kind !== 'block') return |
|
17
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
30 |
switch (object.type) { |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
31 |
case 'paragraph': |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
32 |
case 'line': return <p>{children}</p> |
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
33 |
default: return; |
|
17
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
34 |
} |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
35 |
} |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
36 |
}, |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
37 |
// Mark rules |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
38 |
{ |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
39 |
deserialize(el, next) { |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
40 |
const type = MARK_TAGS[el.tagName] |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
41 |
if (!type) return |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
42 |
return { |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
43 |
kind: 'mark', |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
44 |
type: type, |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
45 |
nodes: next(el.children) |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
46 |
} |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
47 |
}, |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
48 |
serialize(object, children) { |
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
49 |
if (object.kind !== 'mark') return |
|
17
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
50 |
switch (object.type) { |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
51 |
case 'bold': return <strong>{children}</strong> |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
52 |
case 'italic': return <em>{children}</em> |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
53 |
case 'underline': return <u>{children}</u> |
|
25
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
54 |
case 'category': return <span style={{ backgroundColor: object.data.get('color') }}>{children}</span> |
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
55 |
default: return; |
|
17
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
56 |
} |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
57 |
} |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
58 |
} |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
59 |
] |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
60 |
|
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
61 |
const serializer = new Html({ rules }) |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
62 |
|
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
63 |
export default serializer |