This repository was archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 881
Expand file tree
/
Copy pathfirepad.js
More file actions
557 lines (457 loc) · 19.4 KB
/
firepad.js
File metadata and controls
557 lines (457 loc) · 19.4 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
var firepad = firepad || { };
firepad.Firepad = (function(global) {
if (!firepad.RichTextCodeMirrorAdapter) {
throw new Error("Oops! It looks like you're trying to include lib/firepad.js directly. This is actually one of many source files that make up firepad. You want dist/firepad.js instead.");
}
var RichTextCodeMirrorAdapter = firepad.RichTextCodeMirrorAdapter;
var RichTextCodeMirror = firepad.RichTextCodeMirror;
var RichTextToolbar = firepad.RichTextToolbar;
var ACEAdapter = firepad.ACEAdapter;
var FirebaseAdapter = firepad.FirebaseAdapter;
var EditorClient = firepad.EditorClient;
var EntityManager = firepad.EntityManager;
var ATTR = firepad.AttributeConstants;
var utils = firepad.utils;
var LIST_TYPE = firepad.LineFormatting.LIST_TYPE;
var CodeMirror = global.CodeMirror;
var ace = global.ace;
function Firepad(ref, place, options) {
if (!(this instanceof Firepad)) { return new Firepad(ref, place, options); }
if (!CodeMirror && !ace) {
throw new Error('Couldn\'t find CodeMirror or ACE. Did you forget to include codemirror.js or ace.js?');
}
this.zombie_ = false;
if (CodeMirror && place instanceof CodeMirror) {
this.codeMirror_ = this.editor_ = place;
var curValue = this.codeMirror_.getValue();
if (curValue !== '') {
throw new Error("Can't initialize Firepad with a CodeMirror instance that already contains text.");
}
} else if (ace && place && place.session instanceof ace.EditSession) {
this.ace_ = this.editor_ = place;
curValue = this.ace_.getValue();
if (curValue !== '') {
throw new Error("Can't initialize Firepad with an ACE instance that already contains text.");
}
} else {
this.codeMirror_ = this.editor_ = new CodeMirror(place);
}
var editorWrapper = this.codeMirror_ ? this.codeMirror_.getWrapperElement() : this.ace_.container;
this.firepadWrapper_ = utils.elt("div", null, { 'class': 'firepad' });
editorWrapper.parentNode.replaceChild(this.firepadWrapper_, editorWrapper);
this.firepadWrapper_.appendChild(editorWrapper);
// Don't allow drag/drop because it causes issues. See https://github.com/firebase/firepad/issues/36
utils.on(editorWrapper, 'dragstart', utils.stopEvent);
// Provide an easy way to get the firepad instance associated with this CodeMirror instance.
this.editor_.firepad = this;
this.options_ = options || { };
if (this.getOption('richTextShortcuts', false)) {
if (!CodeMirror.keyMap['richtext']) {
this.initializeKeyMap_();
}
this.codeMirror_.setOption('keyMap', 'richtext');
this.firepadWrapper_.className += ' firepad-richtext';
}
this.imageInsertionUI = this.getOption('imageInsertionUI', true);
if (this.getOption('richTextToolbar', false)) {
this.addToolbar_();
this.firepadWrapper_.className += ' firepad-richtext firepad-with-toolbar';
}
this.addPoweredByLogo_();
// Now that we've mucked with CodeMirror, refresh it.
if (this.codeMirror_)
this.codeMirror_.refresh();
var userId = this.getOption('userId', ref.push().key());
var userColor = this.getOption('userColor', colorFromUserId(userId));
this.entityManager_ = new EntityManager();
this.firebaseAdapter_ = new FirebaseAdapter(ref, userId, userColor);
if (this.codeMirror_) {
this.richTextCodeMirror_ = new RichTextCodeMirror(this.codeMirror_, this.entityManager_, { cssPrefix: 'firepad-' });
this.editorAdapter_ = new RichTextCodeMirrorAdapter(this.richTextCodeMirror_);
} else {
this.editorAdapter_ = new ACEAdapter(this.ace_);
}
this.client_ = new EditorClient(this.firebaseAdapter_, this.editorAdapter_);
this.firebaseAdapter_.on('cursor', function() {
self.trigger.apply(self, ['cursor'].concat([].slice.call(arguments)));
});
if (this.codeMirror_) {
this.richTextCodeMirror_.on('newLine', function() {
self.trigger.apply(self, ['newLine'].concat([].slice.call(arguments)));
});
}
this.firebaseAdapter_.on('ready', function() {
self.ready_ = true;
if (this.ace_) {
this.editorAdapter_.grabDocumentState();
}
var defaultText = self.getOption('defaultText', null);
if (defaultText && self.isHistoryEmpty()) {
self.setText(defaultText);
}
self.trigger('ready');
});
this.client_.on('synced', function(isSynced) { self.trigger('synced', isSynced)} );
// Hack for IE8 to make font icons work more reliably.
// http://stackoverflow.com/questions/9809351/ie8-css-font-face-fonts-only-working-for-before-content-on-over-and-sometimes
if (navigator.appName == 'Microsoft Internet Explorer' && navigator.userAgent.match(/MSIE 8\./)) {
window.onload = function() {
var head = document.getElementsByTagName('head')[0],
style = document.createElement('style');
style.type = 'text/css';
style.styleSheet.cssText = ':before,:after{content:none !important;}';
head.appendChild(style);
setTimeout(function() {
head.removeChild(style);
}, 0);
};
}
}
utils.makeEventEmitter(Firepad);
// For readability, these are the primary "constructors", even though right now they're just aliases for Firepad.
Firepad.fromCodeMirror = Firepad;
Firepad.fromACE = Firepad;
Firepad.prototype.dispose = function() {
this.zombie_ = true; // We've been disposed. No longer valid to do anything.
// Unwrap the editor.
var editorWrapper = this.codeMirror_ ? this.codeMirror_.getWrapperElement() : this.ace_.container;
this.firepadWrapper_.removeChild(editorWrapper);
this.firepadWrapper_.parentNode.replaceChild(editorWrapper, this.firepadWrapper_);
this.editor_.firepad = null;
if (this.codeMirror_ && this.codeMirror_.getOption('keyMap') === 'richtext') {
this.codeMirror_.setOption('keyMap', 'default');
}
this.firebaseAdapter_.dispose();
this.editorAdapter_.detach();
if (this.richTextCodeMirror_)
this.richTextCodeMirror_.detach();
};
Firepad.prototype.setUserId = function(userId) {
this.firebaseAdapter_.setUserId(userId);
};
Firepad.prototype.setUserColor = function(color) {
this.firebaseAdapter_.setColor(color);
};
Firepad.prototype.getText = function() {
this.assertReady_('getText');
if (this.codeMirror_)
return this.richTextCodeMirror_.getText();
else
return this.ace_.getSession().getDocument().getValue();
};
Firepad.prototype.setText = function(textPieces) {
this.assertReady_('setText');
if (this.ace_) {
return this.ace_.getSession().getDocument().setValue(textPieces);
} else {
// HACK: Hide CodeMirror during setText to prevent lots of extra renders.
this.codeMirror_.getWrapperElement().setAttribute('style', 'display: none');
this.codeMirror_.setValue("");
this.insertText(0, textPieces);
this.codeMirror_.getWrapperElement().setAttribute('style', '');
this.codeMirror_.refresh();
}
this.editorAdapter_.setCursor({position: 0, selectionEnd: 0});
};
Firepad.prototype.insertTextAtCursor = function(textPieces) {
this.insertText(this.codeMirror_.indexFromPos(this.codeMirror_.getCursor()), textPieces);
};
Firepad.prototype.insertText = function(index, textPieces) {
utils.assert(!this.ace_, "Not supported for ace yet.");
this.assertReady_('insertText');
// Wrap it in an array if it's not already.
if(Object.prototype.toString.call(textPieces) !== '[object Array]') {
textPieces = [textPieces];
}
var self = this;
self.codeMirror_.operation(function() {
// HACK: We should check if we're actually at the beginning of a line; but checking for index == 0 is sufficient
// for the setText() case.
var atNewLine = index === 0;
var inserts = firepad.textPiecesToInserts(atNewLine, textPieces);
for (var i = 0; i < inserts.length; i++) {
var string = inserts[i].string;
var attributes = inserts[i].attributes;
self.richTextCodeMirror_.insertText(index, string, attributes);
index += string.length;
}
});
};
Firepad.prototype.getOperationForSpan = function(start, end) {
var text = this.richTextCodeMirror_.getRange(start, end);
var spans = this.richTextCodeMirror_.getAttributeSpans(start, end);
var pos = 0;
var op = new firepad.TextOperation();
for(var i = 0; i < spans.length; i++) {
op.insert(text.substr(pos, spans[i].length), spans[i].attributes);
pos += spans[i].length;
}
return op;
};
Firepad.prototype.getHtml = function() {
return this.getHtmlFromRange(null, null);
};
Firepad.prototype.selectionHasAttributes = function() {
var startPos = this.codeMirror_.getCursor('start'), endPos = this.codeMirror_.getCursor('end');
var startIndex = this.codeMirror_.indexFromPos(startPos), endIndex = this.codeMirror_.indexFromPos(endPos);
return this.rangeHasAttributes(startIndex, endIndex);
};
Firepad.prototype.rangeHasAttributes = function(start, end) {
this.assertReady_('rangeHasAttributes');
var doc = (start != null && end != null) ?
this.getOperationForSpan(start, end) :
this.getOperationForSpan(0, this.codeMirror_.getValue().length);
var op;
for (var i = 0; i < doc.ops.length; i++) {
op = doc.ops[i];
for(var prop in op.attributes) if (op.attributes.hasOwnProperty(prop) && prop!=ATTR.LINE_SENTINEL) return true; // found an attribute
}
return false;
};
Firepad.prototype.getHtmlFromSelection = function() {
var startPos = this.codeMirror_.getCursor('start'), endPos = this.codeMirror_.getCursor('end');
var startIndex = this.codeMirror_.indexFromPos(startPos), endIndex = this.codeMirror_.indexFromPos(endPos);
return this.getHtmlFromRange(startIndex, endIndex);
};
Firepad.prototype.getHtmlFromRange = function(start, end) {
this.assertReady_('getHtmlFromRange');
var doc = (start != null && end != null) ?
this.getOperationForSpan(start, end) :
this.getOperationForSpan(0, this.codeMirror_.getValue().length);
return firepad.SerializeHtml(doc, this.entityManager_);
};
Firepad.prototype.insertHtml = function (index, html) {
var lines = firepad.ParseHtml(html, this.entityManager_, this.codeMirror_);
this.insertText(index, lines);
};
Firepad.prototype.insertHtmlAtCursor = function (html) {
this.insertHtml(this.codeMirror_.indexFromPos(this.codeMirror_.getCursor()), html);
};
Firepad.prototype.setHtml = function (html) {
var lines = firepad.ParseHtml(html, this.entityManager_, this.codeMirror_);
this.setText(lines);
};
Firepad.prototype.isHistoryEmpty = function() {
this.assertReady_('isHistoryEmpty');
return this.firebaseAdapter_.isHistoryEmpty();
};
Firepad.prototype.bold = function() {
this.richTextCodeMirror_.toggleAttribute(ATTR.BOLD);
this.codeMirror_.focus();
};
Firepad.prototype.italic = function() {
this.richTextCodeMirror_.toggleAttribute(ATTR.ITALIC);
this.codeMirror_.focus();
};
Firepad.prototype.underline = function() {
this.richTextCodeMirror_.toggleAttribute(ATTR.UNDERLINE);
this.codeMirror_.focus();
};
Firepad.prototype.strike = function() {
this.richTextCodeMirror_.toggleAttribute(ATTR.STRIKE);
this.codeMirror_.focus();
};
Firepad.prototype.fontSize = function(size) {
this.richTextCodeMirror_.setAttribute(ATTR.FONT_SIZE, size);
this.codeMirror_.focus();
};
Firepad.prototype.font = function(font) {
this.richTextCodeMirror_.setAttribute(ATTR.FONT, font);
this.codeMirror_.focus();
};
Firepad.prototype.color = function(color) {
this.richTextCodeMirror_.setAttribute(ATTR.COLOR, color);
this.codeMirror_.focus();
};
Firepad.prototype.highlight = function() {
this.richTextCodeMirror_.toggleAttribute(ATTR.BACKGROUND_COLOR, 'rgba(255,255,0,.65)');
this.codeMirror_.focus();
};
Firepad.prototype.align = function(alignment) {
if (alignment !== 'left' && alignment !== 'center' && alignment !== 'right') {
throw new Error('align() must be passed "left", "center", or "right".');
}
this.richTextCodeMirror_.setLineAttribute(ATTR.LINE_ALIGN, alignment);
this.codeMirror_.focus();
};
Firepad.prototype.orderedList = function() {
this.richTextCodeMirror_.toggleLineAttribute(ATTR.LIST_TYPE, 'o');
this.codeMirror_.focus();
};
Firepad.prototype.unorderedList = function() {
this.richTextCodeMirror_.toggleLineAttribute(ATTR.LIST_TYPE, 'u');
this.codeMirror_.focus();
};
Firepad.prototype.todo = function() {
this.richTextCodeMirror_.toggleTodo();
this.codeMirror_.focus();
};
Firepad.prototype.newline = function() {
this.richTextCodeMirror_.newline();
};
Firepad.prototype.deleteLeft = function() {
this.richTextCodeMirror_.deleteLeft();
};
Firepad.prototype.deleteRight = function() {
this.richTextCodeMirror_.deleteRight();
};
Firepad.prototype.indent = function() {
this.richTextCodeMirror_.indent();
this.codeMirror_.focus();
};
Firepad.prototype.unindent = function() {
this.richTextCodeMirror_.unindent();
this.codeMirror_.focus();
};
Firepad.prototype.undo = function() {
this.codeMirror_.undo();
};
Firepad.prototype.redo = function() {
this.codeMirror_.redo();
};
Firepad.prototype.insertEntity = function(type, info, origin) {
this.richTextCodeMirror_.insertEntityAtCursor(type, info, origin);
};
Firepad.prototype.insertEntityAt = function(index, type, info, origin) {
this.richTextCodeMirror_.insertEntityAt(index, type, info, origin);
};
Firepad.prototype.registerEntity = function(type, options) {
this.entityManager_.register(type, options);
};
Firepad.prototype.getOption = function(option, def) {
return (option in this.options_) ? this.options_[option] : def;
};
Firepad.prototype.assertReady_ = function(funcName) {
if (!this.ready_) {
throw new Error('You must wait for the "ready" event before calling ' + funcName + '.');
}
if (this.zombie_) {
throw new Error('You can\'t use a Firepad after calling dispose()! [called ' + funcName + ']');
}
};
Firepad.prototype.makeImageDialog_ = function() {
this.makeDialog_('img', 'Insert image url');
};
Firepad.prototype.makeDialog_ = function(id, placeholder) {
var self = this;
var hideDialog = function() {
var dialog = document.getElementById('overlay');
dialog.style.visibility = "hidden";
self.firepadWrapper_.removeChild(dialog);
};
var cb = function() {
var dialog = document.getElementById('overlay');
dialog.style.visibility = "hidden";
var src = document.getElementById(id).value;
if (src !== null)
self.insertEntity(id, { 'src': src });
self.firepadWrapper_.removeChild(dialog);
};
var input = utils.elt('input', null, { 'class':'firepad-dialog-input', 'id':id, 'type':'text', 'placeholder':placeholder, 'autofocus':'autofocus' });
var submit = utils.elt('a', 'Submit', { 'class': 'firepad-btn', 'id':'submitbtn' });
utils.on(submit, 'click', utils.stopEventAnd(cb));
var cancel = utils.elt('a', 'Cancel', { 'class': 'firepad-btn' });
utils.on(cancel, 'click', utils.stopEventAnd(hideDialog));
var buttonsdiv = utils.elt('div', [submit, cancel], { 'class':'firepad-btn-group' });
var div = utils.elt('div', [input, buttonsdiv], { 'class':'firepad-dialog-div' });
var dialog = utils.elt('div', [div], { 'class': 'firepad-dialog', id:'overlay' });
this.firepadWrapper_.appendChild(dialog);
};
Firepad.prototype.addToolbar_ = function() {
this.toolbar = new RichTextToolbar(this.imageInsertionUI);
this.toolbar.on('undo', this.undo, this);
this.toolbar.on('redo', this.redo, this);
this.toolbar.on('bold', this.bold, this);
this.toolbar.on('italic', this.italic, this);
this.toolbar.on('underline', this.underline, this);
this.toolbar.on('strike', this.strike, this);
this.toolbar.on('font-size', this.fontSize, this);
this.toolbar.on('font', this.font, this);
this.toolbar.on('color', this.color, this);
this.toolbar.on('left', function() { this.align('left')}, this);
this.toolbar.on('center', function() { this.align('center')}, this);
this.toolbar.on('right', function() { this.align('right')}, this);
this.toolbar.on('ordered-list', this.orderedList, this);
this.toolbar.on('unordered-list', this.unorderedList, this);
this.toolbar.on('todo-list', this.todo, this);
this.toolbar.on('indent-increase', this.indent, this);
this.toolbar.on('indent-decrease', this.unindent, this);
this.toolbar.on('insert-image', this.makeImageDialog_, this);
this.firepadWrapper_.insertBefore(this.toolbar.element(), this.firepadWrapper_.firstChild);
};
Firepad.prototype.addPoweredByLogo_ = function() {
var poweredBy = utils.elt('a', null, { 'class': 'powered-by-firepad'} );
poweredBy.setAttribute('href', 'http://www.firepad.io/');
poweredBy.setAttribute('target', '_blank');
this.firepadWrapper_.appendChild(poweredBy)
};
Firepad.prototype.initializeKeyMap_ = function() {
function binder(fn) {
return function(cm) {
// HACK: CodeMirror will often call our key handlers within a cm.operation(), and that
// can mess us up (we rely on events being triggered synchronously when we make CodeMirror
// edits). So to escape any cm.operation(), we do a setTimeout.
setTimeout(function() {
fn.call(cm.firepad);
}, 0);
}
}
CodeMirror.keyMap["richtext"] = {
"Ctrl-B": binder(this.bold),
"Cmd-B": binder(this.bold),
"Ctrl-I": binder(this.italic),
"Cmd-I": binder(this.italic),
"Ctrl-U": binder(this.underline),
"Cmd-U": binder(this.underline),
"Ctrl-H": binder(this.highlight),
"Cmd-H": binder(this.highlight),
"Enter": binder(this.newline),
"Delete": binder(this.deleteRight),
"Backspace": binder(this.deleteLeft),
"Tab": binder(this.indent),
"Shift-Tab": binder(this.unindent),
fallthrough: ['default']
};
};
function colorFromUserId (userId) {
var a = 1;
for (var i = 0; i < userId.length; i++) {
a = 17 * (a+userId.charCodeAt(i)) % 360;
}
var hue = a/360;
return hsl2hex(hue, 1, 0.75);
}
function rgb2hex (r, g, b) {
function digits (n) {
var m = Math.round(255*n).toString(16);
return m.length === 1 ? '0'+m : m;
}
return '#' + digits(r) + digits(g) + digits(b);
}
function hsl2hex (h, s, l) {
if (s === 0) { return rgb2hex(l, l, l); }
var var2 = l < 0.5 ? l * (1+s) : (l+s) - (s*l);
var var1 = 2 * l - var2;
var hue2rgb = function (hue) {
if (hue < 0) { hue += 1; }
if (hue > 1) { hue -= 1; }
if (6*hue < 1) { return var1 + (var2-var1)*6*hue; }
if (2*hue < 1) { return var2; }
if (3*hue < 2) { return var1 + (var2-var1)*6*(2/3 - hue); }
return var1;
};
return rgb2hex(hue2rgb(h+1/3), hue2rgb(h), hue2rgb(h-1/3));
}
return Firepad;
})(this);
// Export Text classes
firepad.Firepad.Formatting = firepad.Formatting;
firepad.Firepad.Text = firepad.Text;
firepad.Firepad.Entity = firepad.Entity;
firepad.Firepad.LineFormatting = firepad.LineFormatting;
firepad.Firepad.Line = firepad.Line;
firepad.Firepad.TextOperation = firepad.TextOperation;
firepad.Firepad.Headless = firepad.Headless;
// Export adapters
firepad.Firepad.RichTextCodeMirrorAdapter = firepad.RichTextCodeMirrorAdapter;
firepad.Firepad.ACEAdapter = firepad.ACEAdapter;