Easily switch between source mode <-> live preview <-> preview

@StefanoCecere @AriesOdyssey you can switch between all three modes with a bit of javascript. If you put this in a Templater template, you can add it as a hotkey. Or any other plugin you prefer to launch arbitrary JS.

Source mode

const view = app.workspace.activeLeaf.getViewState()
view.state.mode = 'source'
view.state.source = true
app.workspace.activeLeaf.setViewState(view)

Live preview

const view = app.workspace.activeLeaf.getViewState()
view.state.mode = 'source'
view.state.source = false
app.workspace.activeLeaf.setViewState(view)

Reading mode

const view = app.workspace.activeLeaf.getViewState()
view.state.mode = 'preview'
app.workspace.activeLeaf.setViewState(view)

I spend most of my time in Reading mode, but I use the above to switch to editing mode automatically when inserting a template. I just put the code at the end of the template.

14 Likes