Layer Document Undo Redo
MediumDesign ยท Figma
Implement LayerDocument, which stores string properties on named layers with undo and redo.
set(layer, prop, value)sets a property. Each call is one edit, even if the value is unchanged.get(layer, prop)returns the current value, or""if there is none.begin_batch()andend_batch()group thesetcalls between them into one edit. A batch with nosetcalls is not an edit.undo()reverses the latest applied edit, restoring each property it changed to its value before that edit, and returns whether there was one.redo()reapplies the most recently undone edit and returns whether there was one.- A new edit discards all undone edits.
Example 1
- Input:
- LayerDocument(), set("a", "x", "1"), set("a", "x", "2"), undo(), get("a", "x"), undo(), get("a", "x"), redo(), get("a", "x")
- Output:
- [null, null, null, true, "1", true, "", true, "1"]
- Explanation
- The second undo removes the first edit, so
xhas no value untilredo()restores"1".
Example 2
- Input:
- LayerDocument(), begin_batch(), set("a", "x", "1"), set("a", "y", "2"), set("a", "x", "3"), end_batch(), get("a", "x"), undo(), get("a", "x"), get("a", "y"), redo(), get("a", "x")
- Output:
- [null, null, null, null, null, null, "3", true, "", "", true, "3"]
- Explanation
- The whole batch is one edit, so one
undo()removes both properties.
Example 3
- Input:
- LayerDocument(), set("a", "x", "1"), undo(), set("a", "x", "2"), redo(), get("a", "x")
- Output:
- [null, null, true, null, false, "2"]
Constraints
- At most
10^5calls are made layer,propandvalueare non-empty strings of at most 20 lowercase letters and digitsbegin_batch()andend_batch()alternate, starting withbegin_batch()undo()andredo()are never called while a batch is open