| author | salimr <riwad.salim@yahoo.fr> |
| Mon, 08 Oct 2018 03:24:47 +0200 | |
| changeset 159 | a4705c2b4544 |
| parent 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' |
| 157 | 2 |
import Html from 'slate-html-serializer' |
|
17
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', |
| 157 | 6 |
ul: 'bulleted-list', |
7 |
ol: 'numbered-list', |
|
8 |
li: 'list-item', |
|
|
17
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
9 |
} |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
10 |
|
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
11 |
// Add a dictionary of mark tags. |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
12 |
const MARK_TAGS = { |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
13 |
em: 'italic', |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
14 |
strong: 'bold', |
| 157 | 15 |
u: 'underlined', |
|
25
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
16 |
category: 'span' |
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
17 |
} |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
18 |
|
|
17
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
19 |
const rules = [ |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
20 |
// Block rules |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
21 |
{ |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
22 |
deserialize(el, next) { |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
23 |
const type = BLOCK_TAGS[el.tagName] |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
24 |
if (!type) return |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
25 |
return { |
| 157 | 26 |
object: 'block', |
|
17
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
27 |
type: type, |
| 157 | 28 |
nodes: next(el.childNodes) |
|
17
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
29 |
} |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
30 |
}, |
| 157 | 31 |
serialize(obj, children) { |
32 |
if (obj.object !== 'block') return |
|
33 |
switch (obj.type) { |
|
34 |
case 'numbered-list': |
|
35 |
return <ol>{children}</ol>; |
|
36 |
case 'bulleted-list': |
|
37 |
return <ul>{children}</ul>; |
|
|
159
a4705c2b4544
Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents:
157
diff
changeset
|
38 |
case 'list-item': |
| 157 | 39 |
return <li>{children}</li>; |
|
17
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
40 |
case 'paragraph': |
| 157 | 41 |
case 'line': |
42 |
return <p>{children}</p> |
|
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
43 |
default: return; |
|
17
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
44 |
} |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
45 |
} |
|
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 |
// Mark rules |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
48 |
{ |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
49 |
deserialize(el, next) { |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
50 |
const type = MARK_TAGS[el.tagName] |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
51 |
if (!type) return |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
52 |
return { |
| 157 | 53 |
object: 'mark', |
|
17
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
54 |
type: type, |
| 157 | 55 |
nodes: next(el.childNodes) |
|
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 |
}, |
| 157 | 58 |
serialize(obj, children) { |
59 |
if (obj.object !== 'mark') return |
|
60 |
switch (obj.type) { |
|
61 |
case 'bold': |
|
62 |
return <strong>{children}</strong> |
|
63 |
case 'italic': |
|
64 |
return <em>{children}</em> |
|
65 |
case 'underlined': |
|
66 |
return <ins>{children}</ins> |
|
67 |
case 'category': |
|
|
159
a4705c2b4544
Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents:
157
diff
changeset
|
68 |
return <span style={{ backgroundColor: obj.get('color') }}>{children}</span> |
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
69 |
default: return; |
|
17
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
70 |
} |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
71 |
} |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
72 |
} |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
73 |
] |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
74 |
|
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
75 |
const serializer = new Html({ rules }) |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
76 |
|
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
77 |
export default serializer |