1
|
|
|
/*global window, document, wysiwyg */ |
2
|
|
|
|
3
|
|
|
export default class RichTexarea { |
4
|
|
|
|
5
|
|
|
constructor(wrapper, color, link, image, smiley) { |
6
|
|
|
this.color = color |
7
|
|
|
this.link = link |
8
|
|
|
this.image = image |
9
|
|
|
this.smiley = smiley |
10
|
|
|
this.wrapper = wrapper |
11
|
|
|
this.textarea = wrapper.querySelector('.form-rich') |
12
|
|
|
this.btns = this.wrapper.querySelectorAll('.wysiwyg-toolbar-icon') |
13
|
|
|
|
14
|
|
|
if (this.textarea.getAttribute('disabled') !== 'disabled') { |
15
|
|
|
this.textEditor = wysiwyg({ |
16
|
|
|
element: this.textarea, |
17
|
|
|
onKeyUp: () => { |
18
|
|
|
this.setHTML() |
19
|
|
|
}, |
20
|
|
|
hijackContextmenu: false |
21
|
|
|
}) |
22
|
|
|
|
23
|
|
|
this._action = this.action.bind(this) |
24
|
|
|
Array.prototype.forEach.call(this.btns, (btn) => { |
25
|
|
|
btn.addEventListener('click', this._action) |
26
|
|
|
}) |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
setHTML() { |
31
|
|
|
this.textarea.innerHTML = this.textEditor.getHTML() |
32
|
|
|
var evt = document.createEvent('KeyboardEvent') |
33
|
|
|
evt.initKeyboardEvent('keyup', true, true, window, 0, 0, 0, 0, 0, 'e'.charCodeAt(0)) |
34
|
|
|
this.textarea.dispatchEvent(evt) |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
_replaceSelectionWithHtml(html) { |
38
|
|
|
if(document.activeElement.getAttribute('contenteditable') != null) { |
39
|
|
|
var range |
40
|
|
|
if (window.getSelection && window.getSelection().getRangeAt) { |
41
|
|
|
range = window.getSelection().getRangeAt(0) |
42
|
|
|
range.deleteContents() |
43
|
|
|
var div = document.createElement('div') |
44
|
|
|
div.innerHTML = html |
45
|
|
|
var frag = document.createDocumentFragment(), child |
46
|
|
|
while ( (child = div.firstChild) ) { |
47
|
|
|
frag.appendChild(child) |
48
|
|
|
} |
49
|
|
|
range.insertNode(frag) |
50
|
|
|
} else if (document.selection && document.selection.createRange) { |
51
|
|
|
range = document.selection.createRange() |
52
|
|
|
html = (node.nodeType == 3) ? node.data : node.outerHTML |
|
|
|
|
53
|
|
|
range.pasteHTML(html) |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
else { |
57
|
|
|
this.wrapper.querySelector('[contenteditable]').focus() |
58
|
|
|
this._replaceSelectionWithHtml(html) |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
action(e) { |
63
|
|
|
this.el = e.target |
64
|
|
|
if(this.el.tagName.toLowerCase() === 'span') this.el = this.el.parentNode |
|
|
|
|
65
|
|
|
|
66
|
|
|
this.action = this.el.getAttribute('data-action') |
67
|
|
|
this.popup = this.el.getAttribute('data-popup') |
68
|
|
|
this.param = this.el.getAttribute('data-param') |
69
|
|
|
if(typeof this.popup !== 'undefined' && this.popup !== null){ |
70
|
|
|
var off |
71
|
|
|
switch(this.popup){ |
|
|
|
|
72
|
|
|
case 'color': |
73
|
|
|
off = this.color.onColor((color) => { |
74
|
|
|
if(color !== null) { |
75
|
|
|
this.textEditor[this.action](color) |
76
|
|
|
this.setHTML() |
77
|
|
|
} |
78
|
|
|
off() |
79
|
|
|
}) |
80
|
|
|
this.color.show(this.el) |
81
|
|
|
break |
82
|
|
|
case 'link': |
83
|
|
|
var html = this.textEditor.getHTML() |
84
|
|
|
this._replaceSelectionWithHtml(`<a href="[LINK]" target="[TARGET]" rel="[REL]">${window.getSelection().toString()}</a>`) |
85
|
|
|
off = this.link.onLink((obj) => { |
86
|
|
|
if(obj.link !== null) { |
87
|
|
|
html = this.textEditor.getHTML().replace('[LINK]', obj.link) |
88
|
|
|
if(obj.target) html = html.replace(/\[TARGET\]/, '_blank') |
|
|
|
|
89
|
|
|
else html = html.replace(/target=\"\[TARGET\]\"/, '') |
90
|
|
|
if(obj.noFollow) html = html.replace(/\[REL\]/, 'nofollow') |
|
|
|
|
91
|
|
|
else html = html.replace(/rel=\"\[REL\]\"/, '') |
92
|
|
|
this.textEditor.setHTML(html) |
93
|
|
|
} |
94
|
|
|
else this.textEditor.setHTML(html) |
|
|
|
|
95
|
|
|
this.setHTML() |
96
|
|
|
off() |
97
|
|
|
}) |
98
|
|
|
this.link.show(this.el) |
99
|
|
|
break |
100
|
|
|
case 'image': |
101
|
|
|
var html = this.textEditor.getHTML() |
|
|
|
|
102
|
|
|
this._replaceSelectionWithHtml(`${window.getSelection().toString()}[MEDIA]`) |
103
|
|
|
off = this.image.onImg((obj) => { |
104
|
|
|
if(obj.image.indexOf('.mp4') > 0){ |
105
|
|
|
html = this.textEditor.getHTML().replace('[MEDIA]', `<video controls><source src="${obj.image}" type="video/mp4"></source></video>`) |
106
|
|
|
} |
107
|
|
|
else{ |
108
|
|
|
this.textEditor[this.action](obj.image) |
109
|
|
|
html = this.textEditor.getHTML().replace('[MEDIA]', '') |
110
|
|
|
} |
111
|
|
|
this.textEditor.setHTML(html) |
112
|
|
|
this.setHTML() |
113
|
|
|
off() |
114
|
|
|
}) |
115
|
|
|
this.image.show(this.el) |
116
|
|
|
break |
117
|
|
|
case 'smiley': |
118
|
|
|
var html = this.textEditor.getHTML() |
|
|
|
|
119
|
|
|
off = this.smiley.onSmiley((obj) => { |
120
|
|
|
this._replaceSelectionWithHtml(obj) |
121
|
|
|
this.textEditor.setHTML(this.textEditor.getHTML()) |
122
|
|
|
this.setHTML() |
123
|
|
|
off() |
124
|
|
|
}) |
125
|
|
|
this.smiley.show(this.el) |
126
|
|
|
break |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
else if(this.action === 'code'){ |
130
|
|
|
this._replaceSelectionWithHtml(`<pre><code>${window.getSelection().toString()}</code></pre>`) |
131
|
|
|
this.textEditor.setHTML(this.textEditor.getHTML()) |
132
|
|
|
this.setHTML() |
133
|
|
|
} |
134
|
|
|
else{ |
135
|
|
|
this.textEditor[this.action](this.param) |
136
|
|
|
this.setHTML() |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.