Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve formula content when tooltip is closed #34

Merged
merged 1 commit into from
Jan 31, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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