alexandru-dinu / obsidian-sortable

Table sorting plugin for https://obsidian.md
MIT License
233 stars 7 forks source link

Remove table state on pane close (or similar) #16

Closed alexandru-dinu closed 2 years ago

alexandru-dinu commented 3 years ago

Currently, tableState is removed from the map when order becomes the default one.

However, if the table is in an "ascending" or "descending" state, and the pane is closed, then the reference to it is lost. The (key, value) pair remains in the map.

I see two possible solutions:

  1. Register an onPaneClosing event (or similar) that removes all active references of the tables in the pane about to be closed.
  2. Persist table's sortable-id=<uuid> attribute, since it links the HTML entry to the plugin's internal tableStates.
alexandru-dinu commented 2 years ago

Following suggestions from https://github.com/obsidianmd/obsidian-releases/pull/727#issuecomment-1019552433 -- the table state is now stored in a WeakMap<HTMLTableElement, TableState> which is properly garbage collected. If a reference to a table element is lost (e.g. when a file is closed), then the (key, value) is removed.

This also has the advantage of not storing a unique id in DOM (to identify the table).