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
|
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* Insert MathML dialog */
/* import-globals-from ../../composer/content/editorUtilities.js */
/* import-globals-from EdDialogCommon.js */
document.addEventListener("dialogaccept", onAccept);
document.addEventListener("dialogcancel", onCancel);
function Startup() {
var editor = GetCurrentEditor();
if (!editor) {
window.close();
return;
}
// Create dialog object for easy access
gDialog.accept = document.documentElement.getButton("accept");
gDialog.mode = document.getElementById("optionMode");
gDialog.direction = document.getElementById("optionDirection");
gDialog.input = document.getElementById("input");
gDialog.output = document.getElementById("output");
gDialog.tabbox = document.getElementById("tabboxInsertLaTeXCommand");
// Set initial focus
gDialog.input.focus();
// Load TeXZilla
// TeXZilla.js contains non-ASCII characters and explicitly sets
// window.TeXZilla, so we have to specify the charset parameter but don't
// need to worry about the targetObj parameter.
/* globals TeXZilla */
Services.scriptloader.loadSubScript(
"chrome://editor/content/TeXZilla.js",
{},
"UTF-8"
);
// Verify if the selection is on a <math> and initialize the dialog.
gDialog.oldMath = editor.getElementOrParentByTagName("math", null);
if (gDialog.oldMath) {
// When these attributes are absent or invalid, they default to "inline" and "ltr" respectively.
gDialog.mode.selectedIndex =
gDialog.oldMath.getAttribute("display") == "block" ? 1 : 0;
gDialog.direction.selectedIndex =
gDialog.oldMath.getAttribute("dir") == "rtl" ? 1 : 0;
gDialog.input.value = TeXZilla.getTeXSource(gDialog.oldMath);
}
// Create the tabbox with LaTeX commands.
createCommandPanel({
"√⅗²": [
"{⋯}^{⋯}",
"{⋯}_{⋯}",
"{⋯}_{⋯}^{⋯}",
"\\underset{⋯}{⋯}",
"\\overset{⋯}{⋯}",
"\\underoverset{⋯}{⋯}{⋯}",
"\\left(⋯\\right)",
"\\left[⋯\\right]",
"\\frac{⋯}{⋯}",
"\\binom{⋯}{⋯}",
"\\sqrt{⋯}",
"\\sqrt[⋯]{⋯}",
"\\cos\\left({⋯}\\right)",
"\\sin\\left({⋯}\\right)",
"\\tan\\left({⋯}\\right)",
"\\exp\\left({⋯}\\right)",
"\\ln\\left({⋯}\\right)",
"\\underbrace{⋯}",
"\\underline{⋯}",
"\\overbrace{⋯}",
"\\widevec{⋯}",
"\\widetilde{⋯}",
"\\widehat{⋯}",
"\\widecheck{⋯}",
"\\widebar{⋯}",
"\\dot{⋯}",
"\\ddot{⋯}",
"\\boxed{⋯}",
"\\slash{⋯}",
],
"(▦)": [
"\\begin{matrix} ⋯ & ⋯ \\\\ ⋯ & ⋯ \\end{matrix}",
"\\begin{pmatrix} ⋯ & ⋯ \\\\ ⋯ & ⋯ \\end{pmatrix}",
"\\begin{bmatrix} ⋯ & ⋯ \\\\ ⋯ & ⋯ \\end{bmatrix}",
"\\begin{Bmatrix} ⋯ & ⋯ \\\\ ⋯ & ⋯ \\end{Bmatrix}",
"\\begin{vmatrix} ⋯ & ⋯ \\\\ ⋯ & ⋯ \\end{vmatrix}",
"\\begin{Vmatrix} ⋯ & ⋯ \\\\ ⋯ & ⋯ \\end{Vmatrix}",
"\\begin{cases} ⋯ \\\\ ⋯ \\end{cases}",
"\\begin{aligned} ⋯ &= ⋯ \\\\ ⋯ &= ⋯ \\end{aligned}",
],
});
createSymbolPanels([
"∏∐∑∫∬∭⨌∮⊎⊕⊖⊗⊘⊙⋀⋁⋂⋃⌈⌉⌊⌋⎰⎱⟨⟩⟪⟫∥⫼⨀⨁⨂⨄⨅⨆ðıȷℏℑℓ℘ℜℵℶ",
"∀∃∄∅∉∊∋∌⊂⊃⊄⊅⊆⊇⊈⊈⊉⊊⊊⊋⊋⊏⊐⊑⊒⊓⊔⊥⋐⋑⋔⫅⫆⫋⫋⫌⫌…⋮⋯⋰⋱♭♮♯∂∇",
"±×÷†‡•∓∔∗∘∝∠∡∢∧∨∴∵∼∽≁≃≅≇≈≈≊≍≎≏≐≑≒≓≖≗≜≡≢≬⊚⊛⊞⊡⊢⊣⊤⊥",
"⊨⊩⊪⊫⊬⊭⊯⊲⊲⊳⊴⊵⊸⊻⋄⋅⋇⋈⋉⋊⋋⋌⋍⋎⋏⋒⋓⌅⌆⌣△▴▵▸▹▽▾▿◂◃◊○★♠♡♢♣⧫",
"≦≧≨≩≩≪≫≮≯≰≱≲≳≶≷≺≻≼≽≾≿⊀⊁⋖⋗⋘⋙⋚⋛⋞⋟⋦⋧⋨⋩⩽⩾⪅⪆⪇⪈⪉⪊⪋⪌⪕⪯⪰⪷⪸⪹⪺",
"←↑→↓↔↕↖↗↘↙↜↝↞↠↢↣↦↩↪↫↬↭↭↰↱↼↽↾↿⇀⇁⇂⇃⇄⇆⇇⇈⇉⇊⇋⇌⇐⇑⇒⇓⇕⇖⇗⇘⇙⟺",
"αβγδϵ϶εζηθϑικϰλμνξℴπϖρϱσςτυϕφχψωΓΔΘΛΞΠΣϒΦΨΩϝ℧",
"𝕒𝕓𝕔𝕕𝕖𝕗𝕘𝕙𝕚𝕛𝕜𝕝𝕞𝕟𝕠𝕡𝕢𝕣𝕤𝕥𝕦𝕧𝕨𝕩𝕪𝕫𝔸𝔹ℂ𝔻𝔼𝔽𝔾ℍ𝕀𝕁𝕂𝕃𝕄ℕ𝕆ℙℚℝ𝕊𝕋𝕌𝕍𝕎𝕏𝕐ℤ",
"𝒶𝒷𝒸𝒹ℯ𝒻ℊ𝒽𝒾𝒿𝓀𝓁𝓂𝓃ℴ𝓅𝓆𝓇𝓈𝓉𝓊𝓋𝓌𝓍𝓎𝓏𝒜ℬ𝒞𝒟ℰℱ𝒢ℋℐ𝒥𝒦ℒℳ𝒩𝒪𝒫𝒬ℛ𝒮𝒯𝒰𝒱𝒲𝒳𝒴𝒵",
"𝔞𝔟𝔠𝔡𝔢𝔣𝔤𝔥𝔦𝔧𝔨𝔩𝔪𝔫𝔬𝔭𝔮𝔯𝔰𝔱𝔲𝔳𝔴𝔵𝔶𝔷𝔄𝔅ℭ𝔇𝔈𝔉𝔊ℌℑ𝔍𝔎𝔏𝔐𝔑𝔒𝔓𝔔ℜ𝔖𝔗𝔘𝔙𝔚𝔛𝔜ℨ",
]);
gDialog.tabbox.selectedIndex = 0;
updateMath();
SetWindowLocation();
}
function insertLaTeXCommand(aButton) {
gDialog.input.focus();
// For a single math symbol, just use the insertText command.
if (aButton.label) {
gDialog.input.editor.insertText(aButton.label);
return;
}
// Otherwise, it's a LaTeX command with at least one argument...
var latex = TeXZilla.getTeXSource(aButton.firstChild);
var selectionStart = gDialog.input.selectionStart;
var selectionEnd = gDialog.input.selectionEnd;
// If the selection is not empty, we replace the first argument of the LaTeX
// command with the current selection.
var selection = gDialog.input.value.substring(selectionStart, selectionEnd);
if (selection != "") {
latex = latex.replace("⋯", selection);
}
// Try and move to the next position.
var latexNewStart = latex.indexOf("⋯"),
latexNewEnd;
if (latexNewStart == -1) {
// This is a unary function and the selection was used as an argument above.
// We select the expression again so that one can choose to apply further
// command to it or just move the caret after that text.
latexNewStart = 0;
latexNewEnd = latex.length;
} else {
// Otherwise, select the dots representing the next argument.
latexNewEnd = latexNewStart + 1;
}
// Update the input text and selection.
gDialog.input.editor.insertText(latex);
gDialog.input.setSelectionRange(
selectionStart + latexNewStart,
selectionStart + latexNewEnd
);
updateMath();
}
function createCommandPanel(aCommandPanelList) {
const columnCount = 10;
for (var label in aCommandPanelList) {
var commands = aCommandPanelList[label];
// Create a <rows> element with some LaTeX commands.
var rows = document.createXULElement("rows");
var i = 0,
row;
for (var command of commands) {
if (i % columnCount == 0) {
// Create a new row.
row = document.createXULElement("row");
rows.appendChild(row);
}
// Create a new button to insert the symbol.
var button = document.createXULElement("toolbarbutton");
button.setAttribute("class", "tabbable");
button.appendChild(TeXZilla.toMathML(command));
row.appendChild(button);
i++;
}
// Create a <columns> element with the desired number of columns.
var columns = document.createXULElement("columns");
for (i = 0; i < columnCount; i++) {
var column = document.createXULElement("column");
column.setAttribute("flex", "1");
columns.appendChild(column);
}
// Create the <grid> element with the <rows> and <columns> children.
var grid = document.createXULElement("grid");
grid.appendChild(columns);
grid.appendChild(rows);
// Create a new <tab> element.
var tab = document.createXULElement("tab");
tab.setAttribute("label", label);
gDialog.tabbox.tabs.appendChild(tab);
// Append the new tab panel.
gDialog.tabbox.tabpanels.appendChild(grid);
}
}
function createSymbolPanels(aSymbolPanelList) {
const columnCount = 13,
tabLabelLength = 3;
for (var symbols of aSymbolPanelList) {
// Create a <rows> element with the symbols of the i-th panel.
var rows = document.createXULElement("rows");
var i = 0,
tabLabel = "",
row;
for (var symbol of symbols) {
if (i % columnCount == 0) {
// Create a new row.
row = document.createXULElement("row");
rows.appendChild(row);
}
// Build the tab label from the first symbols of this tab.
if (i < tabLabelLength) {
tabLabel += symbol;
}
// Create a new button to insert the symbol.
var button = document.createXULElement("toolbarbutton");
button.setAttribute("label", symbol);
button.setAttribute("class", "tabbable");
row.appendChild(button);
i++;
}
// Create a <columns> element with the desired number of columns.
var columns = document.createXULElement("columns");
for (i = 0; i < columnCount; i++) {
var column = document.createXULElement("column");
column.setAttribute("flex", "1");
columns.appendChild(column);
}
// Create the <grid> element with the <rows> and <columns> children.
var grid = document.createXULElement("grid");
grid.appendChild(columns);
grid.appendChild(rows);
// Create a new <tab> element with the label determined above.
var tab = document.createXULElement("tab");
tab.setAttribute("label", tabLabel);
gDialog.tabbox.tabs.appendChild(tab);
// Append the new tab panel.
gDialog.tabbox.tabpanels.appendChild(grid);
}
}
function onAccept(event) {
if (gDialog.output.firstChild) {
var editor = GetCurrentEditor();
editor.beginTransaction();
try {
var newMath = editor.document.importNode(gDialog.output.firstChild, true);
if (gDialog.oldMath) {
// Replace the old <math> element with the new one.
editor.selectElement(gDialog.oldMath);
editor.insertElementAtSelection(newMath, true);
} else {
// Insert the new <math> element.
editor.insertElementAtSelection(newMath, false);
}
} catch (e) {}
editor.endTransaction();
} else {
dump("Null value -- not inserting in MathML Source dialog\n");
event.preventDefault();
}
SaveWindowLocation();
}
function updateMath() {
// Remove the preview, if any.
if (gDialog.output.firstChild) {
gDialog.output.firstChild.remove();
}
// Try to convert the LaTeX source into MathML using TeXZilla.
// We use the placeholder text if no input is provided.
try {
var input = gDialog.input.value || gDialog.input.placeholder;
var newMath = TeXZilla.toMathML(
input,
gDialog.mode.selectedIndex,
gDialog.direction.selectedIndex,
true
);
gDialog.output.appendChild(document.importNode(newMath, true));
gDialog.output.style.opacity = gDialog.input.value ? 1 : 0.5;
} catch (e) {}
// Disable the accept button if parsing fails or when the placeholder is used.
gDialog.accept.disabled = !gDialog.input.value || !gDialog.output.firstChild;
}
function updateMode() {
if (gDialog.output.firstChild) {
gDialog.output.firstChild.setAttribute(
"display",
gDialog.mode.selectedIndex ? "block" : "inline"
);
}
}
function updateDirection() {
if (gDialog.output.firstChild) {
gDialog.output.firstChild.setAttribute(
"dir",
gDialog.direction.selectedIndex ? "rtl" : "ltr"
);
}
}
|