forked from mathquill/mathquill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaseclasses.js
260 lines (227 loc) · 6.82 KB
/
baseclasses.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
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
/*************************************************
* Abstract base classes of blocks and commands.
************************************************/
/**
* MathElement is the core Math DOM tree node prototype.
* Both MathBlock's and MathCommand's descend from it.
*/
function MathElement(){}
_ = MathElement.prototype;
_.prev = 0;
_.next = 0;
_.parent = 0;
_.firstChild = 0;
_.lastChild = 0;
_.eachChild = function(fn) {
for (var child = this.firstChild; child; child = child.next)
if (fn.call(this, child) === false) break;
return this;
};
_.foldChildren = function(fold, fn) {
this.eachChild(function(child) {
fold = fn.call(this, fold, child);
});
return fold;
};
_.keydown = function(e) {
return this.parent.keydown(e);
};
_.textInput = function(ch) {
return this.parent.textInput(ch);
};
/**
* Commands and operators, like subscripts, exponents, or fractions.
* Descendant commands are organized into blocks.
* May be passed a MathFragment that's being replaced.
*/
function MathCommand(cmd, html_template, text_template, replacedFragment) {
if (!arguments.length) return;
var self = this; // minifier optimization
self.cmd = cmd;
if (html_template) self.html_template = html_template;
if (text_template) self.text_template = text_template;
self.jQ = $(self.html_template[0]).data(jQueryDataKey, {cmd: self});
self.initBlocks(replacedFragment);
}
_ = MathCommand.prototype = new MathElement;
_.initBlocks = function(replacedFragment) {
var self = this;
//single-block commands
if (self.html_template.length === 1) {
self.firstChild =
self.lastChild =
self.jQ.data(jQueryDataKey).block =
(replacedFragment && replacedFragment.blockify()) || new MathBlock;
self.firstChild.parent = self;
self.firstChild.jQ = self.jQ.append(self.firstChild.jQ);
return;
}
//otherwise, the succeeding elements of html_template should be child blocks
var newBlock, prev, num_blocks = self.html_template.length;
this.firstChild = newBlock = prev =
(replacedFragment && replacedFragment.blockify()) || new MathBlock;
newBlock.parent = self;
newBlock.jQ = $(self.html_template[1])
.data(jQueryDataKey, {block: newBlock})
.append(newBlock.jQ)
.appendTo(self.jQ);
newBlock.blur();
for (var i = 2; i < num_blocks; i += 1) {
newBlock = new MathBlock;
newBlock.parent = self;
newBlock.prev = prev;
prev.next = newBlock;
prev = newBlock;
newBlock.jQ = $(self.html_template[i])
.data(jQueryDataKey, {block: newBlock})
.appendTo(self.jQ);
newBlock.blur();
}
self.lastChild = newBlock;
};
_.latex = function() {
return this.foldChildren(this.cmd, function(latex, child) {
return latex + '{' + (child.latex() || ' ') + '}';
});
};
_.text = function() {
var i = 0;
return this.foldChildren(this.text_template[i], function(text, child) {
i += 1;
var child_text = child.text();
if (text && this.text_template[i] === '('
&& child_text[0] === '(' && child_text.slice(-1) === ')')
return text + child_text.slice(1, -1) + this.text_template[i];
return text + child.text() + (this.text_template[i] || '');
});
};
_.remove = function() {
var self = this,
prev = self.prev,
next = self.next,
parent = self.parent;
if (prev)
prev.next = next;
else
parent.firstChild = next;
if (next)
next.prev = prev;
else
parent.lastChild = prev;
self.jQ.remove();
return self;
};
_.respace = $.noop; //placeholder for context-sensitive spacing
_.placeCursor = function(cursor) {
//append the cursor to the first empty child, or if none empty, the last one
cursor.appendTo(this.foldChildren(this.firstChild, function(prev, child) {
return prev.isEmpty() ? prev : child;
}));
};
_.isEmpty = function() {
return this.foldChildren(true, function(isEmpty, child) {
return isEmpty && child.isEmpty();
});
};
/**
* Lightweight command without blocks or children.
*/
function Symbol(cmd, html, text) {
MathCommand.call(this, cmd, [ html ],
[ text || (cmd && cmd.length > 1 ? cmd.slice(1) : cmd) ]);
}
_ = Symbol.prototype = new MathCommand;
_.initBlocks = $.noop;
_.latex = function(){ return this.cmd; };
_.text = function(){ return this.text_template; };
_.placeCursor = $.noop;
_.isEmpty = function(){ return true; };
/**
* Children and parent of MathCommand's. Basically partitions all the
* symbols and operators that descend (in the Math DOM tree) from
* ancestor operators.
*/
function MathBlock(){}
_ = MathBlock.prototype = new MathElement;
_.latex = function() {
return this.foldChildren('', function(latex, child) {
return latex + child.latex();
});
};
_.text = function() {
return this.firstChild === this.lastChild ?
this.firstChild.text() :
this.foldChildren('(', function(text, child) {
return text + child.text();
}) + ')';
};
_.isEmpty = function() {
return this.firstChild === 0 && this.lastChild === 0;
};
_.focus = function() {
this.jQ.addClass('hasCursor');
if (this.isEmpty())
this.jQ.removeClass('empty');
return this;
};
_.blur = function() {
this.jQ.removeClass('hasCursor');
if (this.isEmpty())
this.jQ.addClass('empty');
return this;
};
/**
* An entity outside the Math DOM tree with one-way pointers (so it's only
* a "view" of part of the tree, not an actual node/entity in the tree)
* that delimit a list of symbols and operators.
*/
function MathFragment(parent, prev, next) {
if (!arguments.length) return;
var self = this;
self.parent = parent;
self.prev = prev || 0; //so you can do 'new MathFragment(block)' without
self.next = next || 0; //ending up with this.prev or this.next === undefined
self.jQinit(self.fold($(), function(jQ, child){ return child.jQ.add(jQ); }));
}
_ = MathFragment.prototype;
_.remove= MathCommand.prototype.remove;
_.jQinit = function(children) {
this.jQ = children;
};
_.each = function(fn) {
for (var el = this.prev.next || this.parent.firstChild; el !== this.next; el = el.next)
if (fn.call(this, el) === false) break;
return this;
};
_.fold = function(fold, fn) {
this.each(function(el) {
fold = fn.call(this, fold, el);
});
return fold;
};
_.latex = function() {
return this.fold('', function(latex, el){ return latex + el.latex(); });
};
_.blockify = function() {
var self = this,
prev = self.prev,
next = self.next,
parent = self.parent,
newBlock = new MathBlock,
newFirstChild = newBlock.firstChild = prev.next || parent.firstChild,
newLastChild = newBlock.lastChild = next.prev || parent.lastChild;
if (prev)
prev.next = next;
else
parent.firstChild = next;
if (next)
next.prev = prev;
else
parent.lastChild = prev;
newFirstChild.prev = self.prev = 0;
newLastChild.next = self.next = 0;
self.parent = newBlock;
self.each(function(el){ el.parent = newBlock; });
newBlock.jQ = self.jQ;
return newBlock;
};