Thanks to the great BBT plugin author @Emiliano, I borrowed the monkey patches from your plugin.
Mentioning the patchy way of adding a column, some more details might be helpful:
The fixes I mentioned above are to refresh itemsView outside the Zotero loading stage. I found it is not possible to directly call `refreshAndMaintainSelection`, there are extra steps you need to do:
1. ZoteroPane.itemsView._columnsId = null;
Reset the _columnsId to force update _cloumns, otherwise the new columns are not updated in the itemTree._getColumns()
2. document
.querySelector(`.${ZoteroPane.itemsView.tree._columns._styleKey}`)
?.remove();
Remove the styles so they can be re-initialized later.
3. await ZoteroPane.itemsView.refreshAndMaintainSelection();
4. ZoteroPane.itemsView.tree._columns =
new ZoteroPane.itemsView.tree._columns.__proto__.constructor(
ZoteroPane.itemsView.tree
);
Reload virtualized-table to force update new columns in the itemView.tree._columns._columns.
I think this also explains some loading failures you mentioned in related issues and group discussions that extra columns disappear mysteriously. Overlay plugins do this at the loading stage where ZoteroPane is not yet fully initialized and when the patch is delayed, users suffer this missing columns bug.
This is just a theory of my own though. What proved to be true is these steps work for me and without them, it doesn't work for a bootstrapped plugin.
To be honest, I don't like/even hate the patchy way and am always trying to minimize it in plugin development. However, I have to use it to add a column. One thing we can do is to share a global patch, rather than patch it in every plugin: that's what the toolkit does.
@Emiliano are you interested in this toolkit? BBT is very influential in the community, and influences and encourages many young plugin developers. We could together make the code segmentations into a public lib and make plugin development more easily, which is also a benefit for us when maintaining plugins.
Some friends and I are trying to bring a better experience to plugin development, as we all suffer from the lacking of documentation (now we have one, step-by-step guidance doc, in Chinese); the lacking type hints, which forces us to dive very deep into the Zotero source code (now we have a zotero-types definition lib, almost covers all APIs that developers concerns); the lacking public tools and plugins APIs (now we are going to have one, which is this one Zotero Plugin Toolkit).