Figma questions

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 property name of layer layer_id to value, creating the layer if it does not exist and replacing the old value if the property already has one. Returns None. 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, or None when 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. Returns True, or False when there is nothing to undo.
  1. 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's shape and layer 1's color, leaving both layers without properties, so reading them returns None. The last undo has nothing left to revert and returns False.

Constraints

  • 1 <= len(operations) <= 10^4
  • 1 <= layer_id <= 10^9
  • 1 <= len(name) <= 10
  • 1 <= len(value) <= 10
  • Each character of name and value is a lowercase letter or a digit
  • Each operation is one of apply, get_layer, undo