-
-
Notifications
You must be signed in to change notification settings - Fork 829
/
TextEditor.js
173 lines (147 loc) · 3.73 KB
/
TextEditor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import app from '../../common/app';
import Component from '../Component';
import ItemList from '../utils/ItemList';
import listItems from '../helpers/listItems';
import Button from './Button';
import BasicEditorDriver from '../utils/BasicEditorDriver';
import Tooltip from './Tooltip';
import LoadingIndicator from './LoadingIndicator';
/**
* The `TextEditor` component displays a textarea with controls, including a
* submit button.
*
* ### Attrs
*
* - `composer`
* - `submitLabel`
* - `value`
* - `placeholder`
* - `disabled`
* - `preview`
*/
export default class TextEditor extends Component {
oninit(vnode) {
super.oninit(vnode);
/**
* The value of the editor.
*
* @type {String}
*/
this.value = this.attrs.value || '';
/**
* Whether the editor is disabled.
*/
this.disabled = !!this.attrs.disabled;
/**
* Whether the editor is loading.
*/
this.loading = true;
/**
* Async operations to complete before the editor is ready.
*/
this._loaders = [];
}
view() {
return (
<div className="TextEditor">
{this.loading ? (
<LoadingIndicator />
) : (
<>
<div className="TextEditor-editorContainer"></div>
<ul className="TextEditor-controls Composer-footer">
{listItems(this.controlItems().toArray())}
<li className="TextEditor-toolbar">{this.toolbarItems().toArray()}</li>
</ul>
</>
)}
</div>
);
}
oncreate(vnode) {
super.oncreate(vnode);
this._load().then(() => {
setTimeout(this.onbuild.bind(this), 50);
});
}
onbuild() {
this.attrs.composer.editor = this.buildEditor(this.$('.TextEditor-editorContainer')[0]);
}
onupdate(vnode) {
super.onupdate(vnode);
const newDisabled = !!this.attrs.disabled;
if (this.disabled !== newDisabled) {
this.disabled = newDisabled;
this.attrs.composer.editor.disabled(newDisabled);
}
}
_load() {
return Promise.all(this._loaders.map((loader) => loader())).then(() => {
this.loading = false;
m.redraw();
});
}
buildEditorParams() {
return {
classNames: ['FormControl', 'Composer-flexible', 'TextEditor-editor'],
disabled: this.disabled,
placeholder: this.attrs.placeholder || '',
value: this.value,
oninput: this.oninput.bind(this),
inputListeners: [],
onsubmit: () => {
this.onsubmit();
m.redraw();
},
};
}
buildEditor(dom) {
return new BasicEditorDriver(dom, this.buildEditorParams());
}
/**
* Build an item list for the text editor controls.
*
* @return {ItemList<import('mithril').Children>}
*/
controlItems() {
const items = new ItemList();
items.add(
'submit',
<Button icon="fas fa-paper-plane" className="Button Button--primary" itemClassName="App-primaryControl" onclick={this.onsubmit.bind(this)}>
{this.attrs.submitLabel}
</Button>
);
if (this.attrs.preview) {
items.add(
'preview',
<Tooltip text={app.translator.trans('core.forum.composer.preview_tooltip')}>
<Button icon="far fa-eye" className="Button Button--icon" onclick={this.attrs.preview} />
</Tooltip>
);
}
return items;
}
/**
* Build an item list for the toolbar controls.
*
* @return {ItemList<import('mithril').Children>}
*/
toolbarItems() {
return new ItemList();
}
/**
* Handle input into the textarea.
*
* @param {string} value
*/
oninput(value) {
this.value = value;
this.attrs.onchange(this.value);
}
/**
* Handle the submit button being clicked.
*/
onsubmit() {
this.attrs.onsubmit(this.value);
}
}