Skip to content

Commit e5c000d

Browse files
author
lucasbbsdk
authored
Add delete button for history entries (#90)
1 parent f201fce commit e5c000d

File tree

5 files changed

+128
-5
lines changed

5 files changed

+128
-5
lines changed

index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@
4949
data-name="displayHistory"
5050
data-value="true">
5151
</label>
52+
<label>
53+
Display delete button on formula history:
54+
<input
55+
type="checkbox"
56+
class="option"
57+
data-name="displayDeleteButtonOnHistory"
58+
data-value="true">
59+
</label>
60+
5261
</main>
5362

5463
<footer>

mathquill4quill.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,25 @@
5252
overflow: auto;
5353
}
5454

55+
.mathquill4quill-history-container-with-delete-button {
56+
width: 318px;
57+
}
58+
5559
.mathquill4quill-latex-input {
5660
visibility: hidden !important;
5761
padding: 0 !important;
5862
border: 0 !important;
5963
width: 0 !important;
6064
}
65+
66+
.mathquill4quill-history-delete-button::after {
67+
display: inline-block;
68+
content: "\00d7";
69+
padding: 7px;
70+
cursor: pointer;
71+
}
72+
73+
.mathquill4quill-history-inner-container {
74+
display: flex;
75+
justify-content: space-between;
76+
}

mathquill4quill.js

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ window.mathquill4quill = function(dependencies) {
9797
}
9898
}
9999

100+
function removeItemFromHistoryList(array, index) {
101+
array.splice(index, 1);
102+
try {
103+
localStorage.setItem(historyCacheKey, JSON.stringify(array));
104+
} catch (e) {
105+
// eslint-disable-line no-empty
106+
}
107+
}
108+
100109
function getTooltip() {
101110
return quill.container.getElementsByClassName("ql-tooltip")[0];
102111
}
@@ -277,8 +286,25 @@ window.mathquill4quill = function(dependencies) {
277286
button.setAttribute("class", "mathquill4quill-history-button");
278287
}
279288

280-
function applyHistoryContainerStyles(container) {
281-
container.setAttribute("class", "mathquill4quill-history-container");
289+
function applyHistoryInnerContainerStyles(container) {
290+
container.setAttribute(
291+
"class",
292+
"mathquill4quill-history-inner-container"
293+
);
294+
}
295+
296+
function applyHistoryDeleteButtonStyles(button) {
297+
button.setAttribute("class", "mathquill4quill-history-delete-button");
298+
button.setAttribute("title", "Delete");
299+
button.setAttribute("role", "button");
300+
}
301+
302+
function applyHistoryContainerStyles(container, withDeleteButton) {
303+
let className = "mathquill4quill-history-container";
304+
if (withDeleteButton) {
305+
className += " mathquill4quill-history-container-with-delete-button";
306+
}
307+
container.setAttribute("class", className);
282308
}
283309

284310
function createHistoryButton(latex, mqField) {
@@ -308,15 +334,30 @@ window.mathquill4quill = function(dependencies) {
308334

309335
historyDiv = document.createElement("div");
310336
let container = document.createElement("div");
311-
applyHistoryContainerStyles(container);
337+
applyHistoryContainerStyles(container, displayDeleteButtonOnHistory);
312338
let header = document.createElement("p");
313339
header.innerHTML = "Past formulas (max " + historySize + ")";
314340
historyDiv.appendChild(header);
315341

316342
history.forEach(element => {
317343
const button = createHistoryButton(element, mqField);
318344
applyHistoryButtonStyles(button);
319-
container.appendChild(button);
345+
if (displayDeleteButtonOnHistory) {
346+
const innerContainer = document.createElement("div");
347+
applyHistoryInnerContainerStyles(innerContainer);
348+
const deleteButton = document.createElement("div");
349+
applyHistoryDeleteButtonStyles(deleteButton);
350+
deleteButton.addEventListener("click", () => {
351+
innerContainer.remove();
352+
const index = history.indexOf(element);
353+
removeItemFromHistoryList(history, index);
354+
});
355+
innerContainer.appendChild(button);
356+
innerContainer.appendChild(deleteButton);
357+
container.appendChild(innerContainer);
358+
} else {
359+
container.appendChild(button);
360+
}
320361
});
321362
historyDiv.appendChild(container);
322363
tooltip.appendChild(historyDiv);
@@ -357,6 +398,8 @@ window.mathquill4quill = function(dependencies) {
357398
let historyList = fetchHistoryList(historyCacheKey);
358399
const historySize = options.historySize || 10;
359400
const displayHistory = options.displayHistory || false;
401+
const displayDeleteButtonOnHistory =
402+
options.displayDeleteButtonOnHistory || false;
360403

361404
const mqInput = newMathquillInput();
362405
const operatorButtons = newOperatorButtons();

reactjs/src/index.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,24 @@ const CUSTOM_OPERATORS = [
1919
function Index() {
2020
const [operators, setOperators] = useState([]);
2121
const [displayHistory, setDisplayHistory] = useState(false);
22+
const [
23+
displayDeleteButtonOnHistory,
24+
setDisplayDeleteButtonOnHistory
25+
] = useState(false);
2226

2327
const toggleDisplayHistory = event => {
2428
setDisplayHistory(event.target.checked);
2529
};
2630

31+
const toggleDisplayDeleteOnHistory = event => {
32+
setDisplayDeleteButtonOnHistory(event.target.checked);
33+
};
34+
2735
const toggleOperators = event => {
2836
setOperators(event.target.checked ? CUSTOM_OPERATORS : []);
2937
};
3038

31-
const options = { displayHistory, operators };
39+
const options = { displayHistory, operators, displayDeleteButtonOnHistory };
3240

3341
return (
3442
<main className="demo-container">
@@ -47,6 +55,14 @@ function Index() {
4755
onChange={toggleDisplayHistory}
4856
/>
4957
</label>
58+
<label>
59+
Display delete button on formula history:
60+
<input
61+
type="checkbox"
62+
className="option"
63+
onChange={toggleDisplayDeleteOnHistory}
64+
/>
65+
</label>
5066

5167
<footer>
5268
<a href="https://github.com/c-w/mathquill4quill">

tests/deleteButton.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* eslint-disable no-undef */
2+
/* eslint-env node */
3+
4+
module.exports = {
5+
"Is the formula editor visible": function(browser) {
6+
browser
7+
.useXpath()
8+
.url(
9+
"http://localhost:8000/?&operators=true&displayHistory=true&displayDeleteButtonOnHistory=true"
10+
)
11+
.waitForElementVisible('//*[@id="editor"]')
12+
.click('//button[@class="ql-formula"]')
13+
.waitForElementVisible('//div[@data-mode="formula"]');
14+
},
15+
"Is delete button working": function(browser) {
16+
browser
17+
.useXpath()
18+
.click('//button[@data-value="\\sqrt"]')
19+
.waitForElementVisible('//span[@class="mq-scaled mq-sqrt-prefix"]')
20+
.click('//a[@class="ql-action"]')
21+
.waitForElementVisible('//span[@class="ql-formula"]')
22+
.click('//button[@class="ql-formula"]')
23+
.useCss()
24+
.waitForElementVisible(".mathquill4quill-history-container")
25+
.execute(
26+
function() {
27+
document
28+
.querySelector(".mathquill4quill-history-delete-button")
29+
.click();
30+
return document.querySelector(".mathquill4quill-history-button");
31+
},
32+
[],
33+
function(result) {
34+
this.assert.equal(result.value, null);
35+
}
36+
)
37+
.end();
38+
}
39+
};

0 commit comments

Comments
 (0)