Skip to content

Commit

Permalink
Enable saving formula by hitting the enter key (#31)
Browse files Browse the repository at this point in the history
* Split tests into multiple suites

* Enable saving formula by hitting the enter key
  • Loading branch information
c-w authored Jan 21, 2020
1 parent e89ee8b commit 82e2bd8
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 12 deletions.
13 changes: 11 additions & 2 deletions mathquill4quill.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ window.mathquill4quill = function(dependencies) {
return quill.container.getElementsByClassName("ql-tooltip")[0];
}

function getSaveButton() {
const tooltip = getTooltip();
return tooltip.getElementsByClassName("ql-action")[0];
}

function getLatexInput() {
const tooltip = getTooltip();
return tooltip.getElementsByTagName("input")[0];
Expand Down Expand Up @@ -74,11 +79,14 @@ window.mathquill4quill = function(dependencies) {
);
}

function syncMathquillToQuill(latexInput) {
function syncMathquillToQuill(latexInput, saveButton) {
const mqField = MathQuill.getInterface(2).MathField(mqInput, {
handlers: {
edit() {
latexInput.value = mqField.latex();
},
enter() {
saveButton.click();
}
}
});
Expand All @@ -101,14 +109,15 @@ window.mathquill4quill = function(dependencies) {
}

const latexInput = getLatexInput();
const saveButton = getSaveButton();

mqInput = document.createElement("span");
applyMathquillInputStyles(mqInput);

latexInputStyle = latexInput.style.all;
applyLatexInputStyles(latexInput);

mqField = syncMathquillToQuill(latexInput);
mqField = syncMathquillToQuill(latexInput, saveButton);
autofocusFormulaField(mqField);

insertAfter(mqInput, latexInput);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"build.babel": "babel mathquill4quill.js > build/mathquill4quill.js",
"build.uglify": "uglifyjs --compress --mangle -- build/mathquill4quill.js > build/mathquill4quill.min.js",
"build": "run-s build.babel build.uglify",
"test": "concurrently --kill-others --success first \"serve -n -l 8000\" \"nightwatch -e chrome tests.js\""
"test": "concurrently --kill-others --success first \"serve -n -l 8000\" \"nightwatch -e chrome tests\""
},
"devDependencies": {
"@babel/cli": "^7.5.5",
Expand Down
21 changes: 21 additions & 0 deletions tests/editor.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* eslint-env node */

module.exports = {
"Is the formula editor visible": function(browser) {
browser
.useXpath()
.url("http://localhost:8000")
.waitForElementVisible('//*[@id="editor"]')
.click('//button[@class="ql-formula"]')
.waitForElementVisible('//div[@data-mode="formula"]');
},
"Is the formula editor using mathquill": function(browser) {
browser
.useXpath()
.assert.attributeContains(
'//div[@data-mode="formula"]/span',
"class",
"mq-editable-field mq-math-mode"
);
}
};
24 changes: 24 additions & 0 deletions tests/enter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* eslint-env node */

module.exports = {
"Is the formula editor visible": function(browser) {
browser
.useXpath()
.url("http://localhost:8000")
.waitForElementVisible('//*[@id="editor"]')
.click('//button[@class="ql-formula"]')
.waitForElementVisible('//div[@data-mode="formula"]');
},
"Can an equation be inserted": function(browser) {
browser
.useXpath()
.keys(["x", "^", "3", browser.Keys.RETURN])
.waitForElementVisible('//span[@class="ql-formula"]')
.assert.attributeContains(
'//span[@class="ql-formula"]',
"data-value",
"x^3"
)
.end();
}
};
9 changes: 0 additions & 9 deletions tests.js → tests/math.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ module.exports = {
.click('//button[@class="ql-formula"]')
.waitForElementVisible('//div[@data-mode="formula"]');
},
"Is the formula editor using mathquill": function(browser) {
browser
.useXpath()
.assert.attributeContains(
'//div[@data-mode="formula"]/span',
"class",
"mq-editable-field mq-math-mode"
);
},
"Can an equation be inserted": function(browser) {
browser
.useXpath()
Expand Down

0 comments on commit 82e2bd8

Please sign in to comment.