summaryrefslogtreecommitdiff
path: root/public/vendor/ot/codemirror-adapter.js
blob: beff98c673cf76626fe044fc32934c775e05f717 (plain)
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
/*global ot */

ot.CodeMirrorAdapter = (function (global) {
    'use strict';

    var TextOperation = ot.TextOperation;
    var Selection = ot.Selection;

    function CodeMirrorAdapter(cm) {
        this.cm = cm;
        this.ignoreNextChange = false;
        this.changeInProgress = false;
        this.selectionChanged = false;

        bind(this, 'onChanges');
        bind(this, 'onChange');
        bind(this, 'onCursorActivity');
        bind(this, 'onFocus');
        bind(this, 'onBlur');

        cm.on('changes', this.onChanges);
        cm.on('change', this.onChange);
        cm.on('cursorActivity', this.onCursorActivity);
        cm.on('focus', this.onFocus);
        cm.on('blur', this.onBlur);
    }

    // Removes all event listeners from the CodeMirror instance.
    CodeMirrorAdapter.prototype.detach = function () {
        this.cm.off('changes', this.onChanges);
        this.cm.off('change', this.onChange);
        this.cm.off('cursorActivity', this.onCursorActivity);
        this.cm.off('focus', this.onFocus);
        this.cm.off('blur', this.onBlur);
    };

    function cmpPos(a, b) {
        if (a.line < b.line) {
            return -1;
        }
        if (a.line > b.line) {
            return 1;
        }
        if (a.ch < b.ch) {
            return -1;
        }
        if (a.ch > b.ch) {
            return 1;
        }
        return 0;
    }

    function posEq(a, b) {
        return cmpPos(a, b) === 0;
    }

    function posLe(a, b) {
        return cmpPos(a, b) <= 0;
    }

    function minPos(a, b) {
        return posLe(a, b) ? a : b;
    }

    function maxPos(a, b) {
        return posLe(a, b) ? b : a;
    }

    function codemirrorDocLength(doc) {
        return doc.indexFromPos({
                line: doc.lastLine(),
                ch: 0
            }) +
            doc.getLine(doc.lastLine()).length;
    }

    // Converts a CodeMirror change array (as obtained from the 'changes' event
    // in CodeMirror v4) or single change or linked list of changes (as returned
    // by the 'change' event in CodeMirror prior to version 4) into a
    // TextOperation and its inverse and returns them as a two-element array.
    CodeMirrorAdapter.operationFromCodeMirrorChanges = function (changes, doc) {
        // Approach: Replay the changes, beginning with the most recent one, and
        // construct the operation and its inverse. We have to convert the position
        // in the pre-change coordinate system to an index. We have a method to
        // convert a position in the coordinate system after all changes to an index,
        // namely CodeMirror's `indexFromPos` method. We can use the information of
        // a single change object to convert a post-change coordinate system to a
        // pre-change coordinate system. We can now proceed inductively to get a
        // pre-change coordinate system for all changes in the linked list.
        // A disadvantage of this approach is its complexity `O(n^2)` in the length
        // of the linked list of changes.

        var docEndLength = codemirrorDocLength(doc);
        var operation = new TextOperation().retain(docEndLength);
        var inverse = new TextOperation().retain(docEndLength);

        var indexFromPos = function (pos) {
            return doc.indexFromPos(pos);
        };

        function last(arr) {
            return arr[arr.length - 1];
        }

        function sumLengths(strArr) {
            if (strArr.length === 0) {
                return 0;
            }
            var sum = 0;
            for (var i = 0; i < strArr.length; i++) {
                sum += strArr[i].length;
            }
            return sum + strArr.length - 1;
        }

        function updateIndexFromPos(indexFromPos, change) {
            return function (pos) {
                if (posLe(pos, change.from)) {
                    return indexFromPos(pos);
                }
                if (posLe(change.to, pos)) {
                    return indexFromPos({
                        line: pos.line + change.text.length - 1 - (change.to.line - change.from.line),
                        ch: (change.to.line < pos.line) ?
                            pos.ch : (change.text.length <= 1) ?
                            pos.ch - (change.to.ch - change.from.ch) + sumLengths(change.text) : pos.ch - change.to.ch + last(change.text).length
                    }) + sumLengths(change.removed) - sumLengths(change.text);
                }
                if (change.from.line === pos.line) {
                    return indexFromPos(change.from) + pos.ch - change.from.ch;
                }
                return indexFromPos(change.from) +
                    sumLengths(change.removed.slice(0, pos.line - change.from.line)) +
                    1 + pos.ch;
            };
        }

        for (var i = changes.length - 1; i >= 0; i--) {
            var change = changes[i];
            indexFromPos = updateIndexFromPos(indexFromPos, change);

            var fromIndex = indexFromPos(change.from);
            var restLength = docEndLength - fromIndex - sumLengths(change.text);

            operation = new TextOperation()
                .retain(fromIndex)['delete'](sumLengths(change.removed))
                .insert(change.text.join('\n'))
                .retain(restLength)
                .compose(operation);

            inverse = inverse.compose(new TextOperation()
                .retain(fromIndex)['delete'](sumLengths(change.text))
                .insert(change.removed.join('\n'))
                .retain(restLength)
            );

            docEndLength += sumLengths(change.removed) - sumLengths(change.text);
        }

        return [operation, inverse];
    };

    // Singular form for backwards compatibility.
    CodeMirrorAdapter.operationFromCodeMirrorChange =
        CodeMirrorAdapter.operationFromCodeMirrorChanges;

    // Apply an operation to a CodeMirror instance.
    CodeMirrorAdapter.applyOperationToCodeMirror = function (operation, cm) {
        cm.operation(function () {
            var ops = operation.ops;
            var index = 0; // holds the current index into CodeMirror's content
            for (var i = 0, l = ops.length; i < l; i++) {
                var op = ops[i];
                if (TextOperation.isRetain(op)) {
                    index += op;
                } else if (TextOperation.isInsert(op)) {
                    cm.replaceRange(op, cm.posFromIndex(index), null, 'ignoreHistory');
                    index += op.length;
                } else if (TextOperation.isDelete(op)) {
                    var from = cm.posFromIndex(index);
                    var to = cm.posFromIndex(index - op);
                    cm.replaceRange('', from, to, 'ignoreHistory');
                }
            }
        });
    };

    CodeMirrorAdapter.prototype.registerCallbacks = function (cb) {
        this.callbacks = cb;
    };

    CodeMirrorAdapter.prototype.onChange = function () {
        // By default, CodeMirror's event order is the following:
        // 1. 'change', 2. 'cursorActivity', 3. 'changes'.
        // We want to fire the 'selectionChange' event after the 'change' event,
        // but need the information from the 'changes' event. Therefore, we detect
        // when a change is in progress by listening to the change event, setting
        // a flag that makes this adapter defer all 'cursorActivity' events.
        this.changeInProgress = true;
    };

    CodeMirrorAdapter.prototype.onChanges = function (_, changes) {
        if (!this.ignoreNextChange) {
            var pair = CodeMirrorAdapter.operationFromCodeMirrorChanges(changes, this.cm);
            this.trigger('change', pair[0], pair[1]);
        }
        if (this.selectionChanged) {
            this.trigger('selectionChange');
        }
        this.changeInProgress = false;
        this.ignoreNextChange = false;
    };

    CodeMirrorAdapter.prototype.onCursorActivity =
        CodeMirrorAdapter.prototype.onFocus = function () {
            if (this.changeInProgress) {
                this.selectionChanged = true;
            } else {
                this.trigger('selectionChange');
            }
        };

    CodeMirrorAdapter.prototype.onBlur = function () {
        if (!this.cm.somethingSelected()) {
            this.trigger('blur');
        }
    };

    CodeMirrorAdapter.prototype.getValue = function () {
        return this.cm.getValue();
    };

    CodeMirrorAdapter.prototype.getSelection = function () {
        var cm = this.cm;

        var selectionList = cm.listSelections();
        var ranges = [];
        for (var i = 0; i < selectionList.length; i++) {
            ranges[i] = new Selection.Range(
                cm.indexFromPos(selectionList[i].anchor),
                cm.indexFromPos(selectionList[i].head)
            );
        }

        return new Selection(ranges);
    };

    CodeMirrorAdapter.prototype.setSelection = function (selection) {
        var ranges = [];
        for (var i = 0; selection && i < selection.ranges.length; i++) {
            var range = selection.ranges[i];
            ranges[i] = {
                anchor: this.cm.posFromIndex(range.anchor),
                head: this.cm.posFromIndex(range.head)
            };
        }
        this.cm.setSelections(ranges);
    };

    var addStyleRule = (function () {
        var added = {};
        var styleElement = document.createElement('style');
        document.documentElement.getElementsByTagName('head')[0].appendChild(styleElement);
        var styleSheet = styleElement.sheet;

        return function (css) {
            if (added[css]) {
                return;
            }
            added[css] = true;
            styleSheet.insertRule(css, (styleSheet.cssRules || styleSheet.rules).length);
        };
    }());

    CodeMirrorAdapter.prototype.setOtherCursor = function (position, color, clientId) {
        var cursorPos = this.cm.posFromIndex(position);
        var cursorCoords = this.cm.cursorCoords(cursorPos);
        var cursorEl = document.createElement('span');
        cursorEl.className = 'other-client';
        cursorEl.style.display = 'none';
        /*
        cursorEl.style.padding = '0';
        cursorEl.style.marginLeft = cursorEl.style.marginRight = '-1px';
        cursorEl.style.borderLeftWidth = '2px';
        cursorEl.style.borderLeftStyle = 'solid';
        cursorEl.style.borderLeftColor = color;
        cursorEl.style.height = (cursorCoords.bottom - cursorCoords.top) * 0.9 + 'px';
        cursorEl.style.zIndex = 0;
        */
        cursorEl.setAttribute('data-clientid', clientId);
        return this.cm.setBookmark(cursorPos, {
            widget: cursorEl,
            insertLeft: true
        });
    };

    CodeMirrorAdapter.prototype.setOtherSelectionRange = function (range, color, clientId) {
        var match = /^#([0-9a-fA-F]{6})$/.exec(color);
        if (!match) {
            throw new Error("only six-digit hex colors are allowed.");
        }
        var selectionClassName = 'selection-' + match[1];
        var rgbcolor = hex2rgb(color);
        var rule = '.' + selectionClassName + ' { background: rgba(' + rgbcolor.red + ',' + rgbcolor.green + ',' + rgbcolor.blue + ',0.2); }';
        addStyleRule(rule);

        var anchorPos = this.cm.posFromIndex(range.anchor);
        var headPos = this.cm.posFromIndex(range.head);

        return this.cm.markText(
            minPos(anchorPos, headPos),
            maxPos(anchorPos, headPos), {
                className: selectionClassName
            }
        );
    };

    CodeMirrorAdapter.prototype.setOtherSelection = function (selection, color, clientId) {
        var selectionObjects = [];
        for (var i = 0; i < selection.ranges.length; i++) {
            var range = selection.ranges[i];
            if (range.isEmpty()) {
                //selectionObjects[i] = this.setOtherCursor(range.head, color, clientId);
            } else {
                selectionObjects[i] = this.setOtherSelectionRange(range, color, clientId);
            }
        }
        return {
            clear: function () {
                for (var i = 0; i < selectionObjects.length; i++) {
                    selectionObjects[i].clear();
                }
            }
        };
    };

    CodeMirrorAdapter.prototype.trigger = function (event) {
        var args = Array.prototype.slice.call(arguments, 1);
        var action = this.callbacks && this.callbacks[event];
        if (action) {
            action.apply(this, args);
        }
    };

    CodeMirrorAdapter.prototype.applyOperation = function (operation) {
        this.ignoreNextChange = true;
        CodeMirrorAdapter.applyOperationToCodeMirror(operation, this.cm);
    };

    CodeMirrorAdapter.prototype.registerUndo = function (undoFn) {
        this.cm.undo = undoFn;
    };

    CodeMirrorAdapter.prototype.registerRedo = function (redoFn) {
        this.cm.redo = redoFn;
    };

    // Throws an error if the first argument is falsy. Useful for debugging.
    function assert(b, msg) {
        if (!b) {
            throw new Error(msg || "assertion error");
        }
    }

    // Bind a method to an object, so it doesn't matter whether you call
    // object.method() directly or pass object.method as a reference to another
    // function.
    function bind(obj, method) {
        var fn = obj[method];
        obj[method] = function () {
            fn.apply(obj, arguments);
        };
    }

    return CodeMirrorAdapter;

}(this));

function hex2rgb(hex) {
    if (hex[0] == "#") hex = hex.substr(1);
    if (hex.length == 3) {
        var temp = hex;
        hex = '';
        temp = /^([a-f0-9])([a-f0-9])([a-f0-9])$/i.exec(temp).slice(1);
        for (var i = 0; i < 3; i++) hex += temp[i] + temp[i];
    }
    var triplets = /^([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i.exec(hex).slice(1);
    return {
        red: parseInt(triplets[0], 16),
        green: parseInt(triplets[1], 16),
        blue: parseInt(triplets[2], 16)
    }
}