-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathPublicLab.RichTextModule.js
185 lines (117 loc) · 4.53 KB
/
PublicLab.RichTextModule.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
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Form module for rich text entry
*/
var crossvent = require('crossvent');
module.exports = PublicLab.RichTextModule = PublicLab.Module.extend({
init: function(_editor, options) {
var _module = this;
_module.key = 'body';
_module.options = options || _editor.options.richTextModule || {};
_module.options.name = "body";
_module.options.instructions = "Guide others through the steps to reproduce your work.";
// break into subclass common to all modules, perhaps:
_module.options.guides = [
{
icon: "mouse-pointer",
position: 30,
text: "Drag images into the text area to upload them."
},
{
icon: "list-ul",
position: 90,
text: "Be sure to list required materials and resources."
},
{
icon: "clock-o",
position: 90,
text: "Your work is auto-saved so you can return to it in this browser. To recover drafts, open the <code>...</code> menu below."
}
];
_module._super(_editor, _module.options);
// customize options after Module defaults set in _super()
_module.options.initialValue = _editor.options[_module.key] || _module.el.find('textarea').val();
_module.options.required = true;
// should be switchable for other editors:
_module.wysiwyg = _module.options.wysiwyg || PublicLab.Woofmark(_module.options.textarea, _editor, _module);
_module.editable = _module.wysiwyg.editable;
_module.textarea = _module.wysiwyg.textarea;
if (_module.wysiwyg.mode == "wysiwyg") _module.focusables.push($(_module.editable));
else _module.focusables.push($(_module.textarea));
_module.value = function(text) {
// woofmark automatically returns the markdown, not rich text:
if (typeof text === 'string') {
if (_module.afterParse) _module.afterParse();
return _module.wysiwyg.value(text);
} else {
return _module.wysiwyg.value();
}
}
_module.value(_module.options.initialValue);
_module.valid = function() {
return _module.value() != "";
}
_module.html = function() {
return _module.wysiwyg.editable.innerHTML;
}
_module.markdown = function() {
return _module.value();
}
// converts to markdown and back to html, or the reverse,
// to trigger @callouts and such formatting
_module.parse = function() {
_module.value(_module.value());
_module.afterParse();
}
// construct HTML additions
_module.build();
_module.afterParse = function() {
// bootstrap styling for plots2
$(_module.wysiwyg.editable).find('table').addClass('table');
}
_module.afterParse();
_module.setMode = function(mode) {
return _module.wysiwyg.setMode(mode);
}
_module.height = function() {
var height;
if (_module.wysiwyg.mode == "wysiwyg") height = $('.wk-wysiwyg').height();
else height = $('.ple-textarea').height();
return height;
}
var growTextarea = require('grow-textarea');
// Make textarea match content height
_module.resize = function() {
growTextarea(_module.options.textarea, { extra: 10 });
}
_module.resize();
crossvent.add(_module.options.textarea, 'blur', function (e) {
_editor.validate();
});
crossvent.add(_module.options.textarea, 'keydown', function (e) {
_editor.validate();
});
crossvent.add(_module.wysiwyg.editable, 'blur', function (e) {
_editor.validate();
});
crossvent.add(_module.wysiwyg.editable, 'keydown', function (e) {
_editor.validate();
});
// once woofmark's done with the textarea, this is triggered
// using woofmark's special event system, crossvent
// -- move this into the Woofmark adapter initializer
crossvent.add(_module.options.textarea, 'woofmark-mode-change', function (e) {
_module.resize();
_module.afterParse();
// ensure document is scrolled to the same place:
document.body.scrollTop = _module.scrollTop;
// might need to adjust for markdown/rich text not
// taking up same amount of space, if menu is below _editor...
//if (_editor.wysiwyg.mode == "markdown")
if (_module.wysiwyg.mode == "wysiwyg") _module.focusables[0] = $(_module.editable);
else _module.focusables[0] = $(_module.textarea);
});
$(_module.options.textarea).on('change keydown', function(e) {
_module.resize();
});
}
});