edemaine / svgtiler

Tool for drawing diagrams on a grid, combining grids of SVGs into a big SVG figure
MIT License
58 stars 6 forks source link

Callback when starting a new drawing #79

Closed edemaine closed 2 years ago

edemaine commented 2 years ago

It would be useful to be able to specify a callback function that gets called when starting on a new drawing file. For example:

state = null  # global variable
export onNewDrawing = -> state = []  # new proposal
export default -> ...sometimes modify state to carry data to future cells...

This could also be used as a way to (manually) implement symbol aliases (#68) within a drawing file.

Note that an alternative/workaround right now is to add data to the Context object. This is shared within each drawing but no more.

Another application: preprocessing the drawing. This is arguably a better time to modify a drawing than #69. Maybe even allow returning some global content to draw like #81? But maybe makes more sense to do such global drawing after the layout.

# Example 1: compute
export beforeDrawing = (drawing) ->
  for row, i in drawing.keys
    for char, j in row
      # do some computation / walking through the drawing
      # to detect things that will be used later
  # maybe return content to draw?

# Example 2: configuration in first row of drawing
export beforeDrawing = (drawing) ->
  firstRow = drawing.keys.shift() # remove first row from drawing!
  # set some settings based on firstRow

For symmetry with #81, could maybe be called beforeDrawing.

Could even modify keys to be non-strings (e.g. objects), as this happens after coercion to String.

edemaine commented 2 years ago

I ended up implementing this as an API call svgtiler.beforeRender, so you can add multiple callbacks if you want.