-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
100 lines (90 loc) · 2.89 KB
/
index.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
module.exports = CM;
function CM() {}
CM.prototype.view = __dirname;
if (require && require.resolve) {
var path = require('path');
var entry = require.resolve('codemirror');
CM.root = path.resolve(entry, '../../');
}
CM.prototype.init = function() {
var model = this.model;
model.setNull("options", {});
model.setNull("debounce", 150)
};
CM.prototype.create = function() {
var model = this.model;
var that = this;
var options = this.getAttribute("options");
var cm = this.cm = CodeMirror.fromTextArea(this.textarea, options);
// Dynamically load necessary files for the CodeMirror mode set through the mode option
// Mode can either be a mime-type `text/html` or a CodeMirror mode-name `htmlmixed`
// cf. http://codemirror.net/demo/loadmode.html
if (options.mode) {
var mode = options.mode;
if (/\//.test(options.mode)) {
var info = CodeMirror.findModeByMIME(options.mode);
if (info) {
mode = info.mode;
}
}
CodeMirror.autoLoadMode(cm, mode);
}
// changes in values inside the array
model.on("change", "text", function(newVal, oldVal, passed) {
//we don't want to change the CM instance if we did the change
if(passed.editing) return;
var stringInsert = passed.$stringInsert;
var stringRemove = passed.$stringRemove;
if(stringInsert && stringInsert.text) {
that.supress = true;
cm.replaceRange(stringInsert.text, cm.posFromIndex(stringInsert.index));
that.supress = false;
that.check();
} else if(stringRemove && stringRemove.howMany) {
that.supress = true;
var from = cm.posFromIndex(stringRemove.index);
var to = cm.posFromIndex(stringRemove.howMany);
cm.replaceRange('', from, to);
that.supress = false;
} else {
// using model.setDiff doesnt provide stringInsert and stringRemove, just changes the text
var cursor = that.cm.getCursor();
that.supress = true;
that.cm.setValue(newVal);
that.supress = false;
that.cm.setCursor(cursor)
}
});
var debounce;
cm.on("change", function(cm, change) {
if(that.supress) return;
if(debounce) {
clearTimeout(debounce);
debounce = null;
}
debounce = setTimeout(function(){
var value = cm.getValue();
that.model.pass({editing: true}).setDiff("text", value)
}, model.get("debounce"))
});
};
CM.prototype.check = function() {
var that = this;
setTimeout(function () {
var cmText = that.cm.getValue();
var otText = that.model.get("text") || '';
var cursor = that.cm.getCursor();
if (cmText != otText) {
/*
console.error("Text does not match!");
console.error("cm: " + cmText);
console.error("ot: " + otText);
*/
// Replace the editor text with the current model value.
that.supress = true;
that.cm.setValue(otText);
that.supress = false;
that.cm.setCursor(cursor)
}
}, 0);
};