-
Notifications
You must be signed in to change notification settings - Fork 0
/
visual-math-input.js
158 lines (132 loc) · 5.85 KB
/
visual-math-input.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
define(['jquery', './mathquill'], function ($, MathQuill) {
// When a control button is clicked, the input blurs.
// This lets the control button know which input to act on.
let lastFocusedInput = null;
class Input {
constructor(input, parent) {
this.$input = $(input);
this.$parent = $(parent);
let wrapper = document.createElement('div');
this.wrapper = $(wrapper).addClass('visual-math-input-field');
let MQ = MathQuill.getInterface(2);
this.field = MQ.MathField(wrapper, {
spaceBehavesLikeTab: true,
handlers: {
edit: field => {
this.onEdit($(input), field);
}
}
});
this.$parent.append(wrapper);
this.onEdit = ($input, field) => $input.val('\\[ ' + field.latex() + ' \\]');
this.$textarea.on('blur', () => lastFocusedInput = this);
}
get $textarea() {
return $(this.field.el()).find('textarea');
}
enable() {
this.$textarea.prop('disabled', false);
}
disable() {
this.$textarea.prop('disabled', true);
}
}
class Control {
constructor(name, text, onClick) {
this.name = name;
this.text = text;
this.onClick = onClick;
this.$element = null;
}
enable() {
if (this.$element !== null) {
return;
}
let element = document.createElement('button');
this.$element = $(element);
this.$element.html(this.text);
this.$element.addClass('visual-math-input-control btn btn-primary');
this.$element.on('click', event => {
event.preventDefault();
if (lastFocusedInput !== null) {
this.onClick(lastFocusedInput.field);
lastFocusedInput.field.focus();
}
});
}
}
class ControlList {
constructor(wrapper) {
this.controls = [];
this.$wrapper = $(wrapper);
this.$wrapper.addClass('visual-math-input-wrapper');
this.defineDefault();
}
define(name, text, onClick) {
this.controls[name] = new Control(name, text, onClick);
}
enable(names) {
for (let name of names) {
let control = this.controls[name];
control.enable();
this.$wrapper.append(control.$element);
}
}
enableAll() {
for (let name in this.controls) {
let control = this.controls[name];
control.enable();
this.$wrapper.append(control.$element);
}
}
defineDefault() {
// It is also possible to render \\[ \\binom{n}{k} \\] with MathJax.
// Using MathQuill's HTML output is slightly less clean, but we avoid using YUI and MathJax.
let sqrt = '<span class="mq-root-block">√</span>';
let int = '<span class="mq-root-block">∫</span>';
let sum = '<span class="mq-root-block"><span class="mq-large-operator mq-non-leaf">∑</span></span>';
let lim = '<span class="mq-root-block">lim</span>';
let nchoosek = '<div class="mq-math-mode" style="cursor:pointer;font-size:100%;">';
nchoosek += '<span class="mq-root-block">';
nchoosek += '<span class="mq-non-leaf">';
nchoosek += '<span class="mq-paren mq-scaled" style="transform: scale(0.8, 1.5);">(</span>';
nchoosek += '<span class="mq-non-leaf" style="margin-top:0;">';
nchoosek += '<span class="mq-array mq-non-leaf">';
nchoosek += '<span style="font-size: 14px;"><var>n</var></span>';
nchoosek += '<span style="font-size: 14px;"><var>k</var></span>';
nchoosek += '</span></span>';
nchoosek += '<span class="mq-paren mq-scaled" style="transform: scale(0.8, 1.5);">)</span></span>';
nchoosek += '</span></div>';
let divide = '<span class="mq-root-block">/</span>';
let plusminus = '<span class="mq-root-block">±</span>';
let theta = '<span class="mq-root-block">θ</span>';
let pi = '<span class="mq-root-block">π</span>';
let infinity = '<span class="mq-root-block">∞</span>';
let caret = '<div class="mq-math-mode" style="cursor:pointer;font-size:100%;">';
caret += '<span class="mq-root-block">';
caret += '<var>x</var>';
caret += '<span class="mq-supsub mq-non-leaf mq-sup-only">';
caret += '<span class="mq-sup">';
caret += '<var>y</var>';
caret += '</span></span></span></div>';
this.define('sqrt', sqrt, field => field.cmd('\\sqrt'));
this.define('int', int, field => field.cmd('\\int'));
this.define('sum', sum, field => field.cmd('\\sum'));
this.define('lim', lim, field => {
field.cmd('\\lim').typedText('_').write('x').cmd('\\to').write('0').moveToRightEnd();
});
this.define('nchoosek', nchoosek, field => field.cmd('\\choose'));
this.define('divide', divide, field => field.cmd('\\frac'));
this.define('plusminus', plusminus, field => field.cmd('\\pm'));
this.define('theta', theta, field => field.cmd('\\theta'));
this.define('pi', pi, field => field.cmd('\\pi'));
this.define('infinity', infinity, field => field.cmd('\\infinity'));
this.define('caret', caret, field => field.cmd('^'));
}
}
return {
Input: Input,
Control: Control,
ControlList: ControlList
};
});