I am currently developing a new plug-in and would like to be able to easily detect transitions between the three main modes listed in the title. Ideally there should be an event to hook into but I cannot find anything in the API documentation. Have I overlooked something as there is very little in the way of useful info and examples in that document?
At present I have a system that works but is somewhat dirty. After spending a few hours puzzling over this I determined that using the getState method on the active MarkdownView gives me access to two variables that represent 3 states:
-
mode=’preview’, source=true => Reading Mode (1)
-
mode=’source’, source=true => Source Mode
-
mode=’source’ source=false => Live Preview
-
mode=’preview’ source = false => Reading Mode (2)
The difficulty I have with this is twofold:
-
How can I detect when one of these values changes?
-
The two variants for reading mode do not behave in the same way. The plug-in is based on codeblock syntax and adds a set of grouped radio inputs into the DOM. From what I can ascertain this actually results in two additions of the grouped inputs into the DOM, one for the Reading mode and another for Live Preview, consequently I have to give them different group names so that they do not interact. Despite this the radio buttons work as expected for Reading Mode (1) in all circumstances but does not respond to clicks when in the Reading Mode (2) combination. The only difference is the setting of ‘source’ variable. I have proven this by using a short timer to force Reading Mode (2) into Reading Mode(1) by setting source=true using setState(). This reactivates the radio inputs in Reading Mode (1). Also by inspection it appears that the problem is caused by the radio button clicks are still being routed to the ‘Live Preview’ group rather than the ‘Reading View’ group. By inference it looks as though forcing source=true deactivates the Live Preview part of the DOM and reactivates the Reading View group.
Having to force source=true is cludgy and also has the downside of always going into LivePreview when I go back into editing mode unless I implement a ‘remember and restore’ original state when switching back into edit mode.
As already said above, I haven’t been able to determine how to check when a view state changes other than by the implementation of a short interval timer. This is far from an ideal solution.
I feel I may have missed something obvious so if anyone can enlighten me as to a better way it would be greatly appreciated.
Hoping this makes sense to anyone who cares!!!
TIA
Bob