mathquill / mathquill

Easily type math in your webapp
http://mathquill.com
Mozilla Public License 2.0
2.61k stars 687 forks source link

Cloning element containing Mathquill fields #1044

Open Ephraim-Bryski opened 4 months ago

Ephraim-Bryski commented 4 months ago

I have an application with editable Mathquill fields that can be added and removed by the user. I would like to implement undo and redo by cloning the state of the parent containing these fields on keystrokes, and replacing the parent with these clones on undo. I'm currently implementing this as follows:

const clones = []

function track_changes(){
  const current_state = $("#parent").clone(true, true)
  clones.push(current_state)
}

function undo(){
  const past_state = clones.pop()
  $("#parent").replaceWith(past_state)
}

While this works, it becomes impossible to edit Mathquill fields after calling undo (you cannot focus back in on the field).

My specific requirements:

Desmos implements exactly what I'm looking for.

Is there a straight-forward solution? Does it require something more complex?

Thanks!