Document Layers with Undo and Redo
FigmaPhone screenMedium
A design document is a set of layers. Each layer has an integer id and holds named properties such as color or width, each with a string value.
A layer exists only while it has at least one property. Reading a layer that has none returns None, whether it was never touched or all of its properties were undone.
You build the Document class behind an editor's undo history in three stages. Each example drives one Document instance through a list of [method, [args]] calls and lists every call's return value.
Part 1: Apply and undo
Implement the first three methods of Document.
apply(layer_id, name, value)sets propertynameof layerlayer_idtovalue, creating the layer if it does not exist and replacing the old value if the property already has one. ReturnsNone. Every call is one undoable step, even when the value does not change.get_layer(layer_id)returns the layer's properties as a list of[name, value]pairs sorted by name, orNonewhen the layer has no properties.undo()reverts the most recent step that has not been undone: the property goes back to its exact previous value, or the property is removed if it did not exist before. ReturnsTrue, orFalsewhen there is nothing to undo.
- Example 1
- init
[]- operations
[["apply",[1,"color","green"]],["apply",[2,"shape","triangle"]],["apply",[1,"color","blue"]],["get_layer",[1]],["undo",[]],["get_layer",[1]],["get_layer",[2]],["undo",[]],["undo",[]],["get_layer",[2]],["get_layer",[1]],["undo",[]]]- Output
[null,null,null,[["color","blue"]],true,[["color","green"]],[["shape","triangle"]],true,true,null,null,false]
Why: The third apply overwrites layer 1's color, so the first undo restores
green. The next two undos remove layer 2'sshapeand layer 1'scolor, leaving both layers without properties, so reading them returnsNone. The last undo has nothing left to revert and returnsFalse.
Constraints
1 <= len(operations) <= 10^41 <= layer_id <= 10^91 <= len(name) <= 101 <= len(value) <= 10- Each character of
nameandvalueis a lowercase letter or a digit - Each operation is one of
apply,get_layer,undo