Skip to content

Commit

Permalink
Preserve formula content until tooltip is closed (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
c-w authored Jan 31, 2020
1 parent 905907c commit 5eaf9b6
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion mathquill4quill.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,31 @@ window.mathquill4quill = function(dependencies) {
const Quill = dependencies.Quill || window.Quill;
const MathQuill = dependencies.MathQuill || window.MathQuill;
const katex = dependencies.katex || window.katex;
const localStorage = dependencies.localStorage || window.localStorage;

function setCacheItem(key, value) {
try {
localStorage.setItem(key, value);
} catch (e) {
// eslint-disable-line no-empty
}
}

function getCacheItem(key) {
try {
return localStorage.getItem(key);
} catch (e) {
return "";
}
}

function removeCacheItem(key) {
try {
localStorage.removeItem(key);
} catch (e) {
// eslint-disable-line no-empty
}
}

function insertAfter(newNode, referenceNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
Expand Down Expand Up @@ -59,6 +84,7 @@ window.mathquill4quill = function(dependencies) {

function newMathquillInput() {
const autofocus = options.autofocus == null ? true : options.autofocus;
const cacheKey = options.cacheKey || "__mathquill4quill_cache__";
let mqInput = null;
let mqField = null;
let latexInputStyle = null;
Expand All @@ -83,14 +109,23 @@ window.mathquill4quill = function(dependencies) {
const mqField = MathQuill.getInterface(2).MathField(mqInput, {
handlers: {
edit() {
latexInput.value = mqField.latex();
const latex = mqField.latex();
latexInput.value = latex;
setCacheItem(cacheKey, latex);
},
enter() {
saveButton.click();
}
}
});

const cachedLatex = getCacheItem(cacheKey);
if (cachedLatex) {
mqField.latex(cachedLatex);
}

saveButton.addEventListener("click", () => removeCacheItem(cacheKey));

return mqField;
}

Expand Down

0 comments on commit 5eaf9b6

Please sign in to comment.