I think the current behavior of automatic opening and closing previews is a bit shaky to me. If been thinking a bit on how it could be solved and have some idea now. I'm curious what others think. Please consider my proposal below.
I am using Atom, so this issue is focused on Atom. I'm not sure how well it would translate to VS Code.
Related issues that could possibly be closed by such a rewrite: shd101wyy/markdown-preview-enhanced#719, shd101wyy/vscode-markdown-preview-enhanced#1707, shd101wyy/vscode-markdown-preview-enhanced#1426
Current behavior
Nice
When I open a md file from the project tree view it opens a preview.
When I open a md file using File -> Open (ctrl+O) it opens a preview.
When I open a md file using using "Find File" (cmd/alt+P) it opens a preview.
When I close a file and have "Open Only One Preview" unchecked in package config, the preview closes.
NOTE: "Open Only One Preview" is checked by default, and so is "Automatically close preview when closing a markdown file" (which only works if "Open Only One Preview" is unchecked), which means this plugin ships with conflicting settings by default....
Not so nice
Opening a file from command line won't open a preview: atom /path/to/my/file.md
When clicking on a tab with an md file open (which doesn't have a preview tab yet), a preview won't open.
New, never-saved content (e.g. when starting a new file with ctrl+N) for which I set the grammar to markdown (in the bottom bar or via ctrl+shift+L), won't show a preview and even explicit toggling of preview won't work.
When I later save such content as an md, it won't open a preview automatically.
When I switch to an editor tab that isn't a markdown file (e.g. some cpp file that I'm editing), the preview doesn't close. I practically always don't want to see my markdown preview anymore once I start editing another file. I'm on a 13 inch screen often, so I need to use my screen real estate efficiently. This is the one that particularly annoys me. I even don't get a command in the command palette to close the current previews.
Toggle Preview (ctrl+shift+M) doesn't close previews in various cases, because it seems designed to only "toggle-close" the preview matching with the md file currently being edited (I guess they're stored in editor/preview pairs). So if I watch another file (e.g. my cpp file) there is no way to close the preview with a shortcut. Also, if I have "Open Only One Preview" enabled and I close the md file (auto closing preview doesn't work then), there is no way to toggle-close the preview with a shortcut anymore...
Proposed behavior
I'd personally argue to only support a "Open Only One Preview" mode (so don't make it an option, just enforce it). That probably simplifies the "auto preview open/close" code (no need to keep track editor/preview pairs), making it easier to maintain and less bug-prone.
In such a mode:
You check if the current active editor is a markdown file, for example based on the extension. You could check the current file like this:
atom.workspace.getActiveTextEditor().getTitle(), then check the extension for that.
Alternatively, or as a fallback for unsaved new files, you can check the grammar like this: atom.workspace.getActiveTextEditor().getGrammar().scopeName (which would be source.gfm for the "Github Markdown" grammar).
You might need to check it at a fixed frequency (e.g. 1Hz) or preferably you can implement it as a callback on some event like "active editor changed". Though for that, I don't know enough about atoms internals.
Based on the info above (when your "active editor changed" callback triggers), you can:
Open a preview whenever you change to an editor window with md file extension (or md grammar).
Close a preview whenever you change to a windows which is not an md.
In case someone wants to keep a preview, they have two options:
Open Command Palette (alt/cmd+shift+P) and run: 'Markdown Preview Enhanced: Pin Current Preview', which turns the window from an auto-preview window into a regular-preview window. You could say that a regular-preview gets 'detached' from the auto opening/closing behavior. Currently, if you hover a preview you get some buttons in the top right, you could consider adding a 'Pin Preview' button there, which is probably even more intuitive to use than the command palette. NOTE: this means that there can always only be 1 (or 0) auto-preview window in my proposed solution. The auto-preview window can become regular-preview window (by 'pinning'), but not the other way around.
Open Command Palette (alt/cmd+shift+P) and run: 'Markdown Preview Enhanced: Disable Auto Preview', (or, when disabled, 'Markdown Preview Enhanced: Enable Auto Preview'). This allows to easily switch on/off the preview behavior temporarily, which might sometimes be useful. I wouldn't be sure if I'd make it persistent though (e.g. write it to ~/.atom/config.cson).
When auto preview is disabled, all preview windows are regular-preview windows and the 'Pin Preview' hover button and alt/cmd+shift+P: 'Markdown Preview Enhanced: Pin Current Preview' should disappear.
To me, the above functionality would be absolutely awesome! Some challenges I foresee:
If you want regular-preview windows to continue to sync scroll, you'll still need to keep track of raw/preview pairs.
If you check on a fixed frequency (e.g. 1Hz) if active editor is markdown, you should avoid re-opening a manually closed preview automatically again after ~1 second. Hence, using a callback on "active editor changed" is nicer/cleaner.
If you don't want to strip the old behavior (yet), consider adding this as a new experimental feature (enabled by default š?) called something like: "Only show preview for current markdown file" with extra text "Will make sure a preview is shown for current file being edited only, and will automatically close if current active file is not a markdown file".
Another cool extra option that came to mind:
Add an option to "Auto fold project tree view when opening a preview". On 13 inch, Project tree view plus 2 main panes is results in too small main panes already... So this would be a cool option. But lets improve auto preview opening/closing first š
What are the plans for enhancing it? It seems for me that not even with "Single Preview" enabled the preview closes when I close the Md file. It keeps opened and would be nice to have this features out of the box!
I think the current behavior of automatic opening and closing previews is a bit shaky to me. If been thinking a bit on how it could be solved and have some idea now. I'm curious what others think. Please consider my proposal below.
I am using Atom, so this issue is focused on Atom. I'm not sure how well it would translate to VS Code.
Related issues that could possibly be closed by such a rewrite: shd101wyy/markdown-preview-enhanced#719, shd101wyy/vscode-markdown-preview-enhanced#1707, shd101wyy/vscode-markdown-preview-enhanced#1426
Current behavior
Nice
Not so nice
atom /path/to/my/file.md
Proposed behavior
I'd personally argue to only support a "Open Only One Preview" mode (so don't make it an option, just enforce it). That probably simplifies the "auto preview open/close" code (no need to keep track editor/preview pairs), making it easier to maintain and less bug-prone.
In such a mode:
atom.workspace.getActiveTextEditor().getTitle()
, then check the extension for that.atom.workspace.getActiveTextEditor().getGrammar().scopeName
(which would besource.gfm
for the "Github Markdown" grammar).~/.atom/config.cson
).To me, the above functionality would be absolutely awesome! Some challenges I foresee:
If you don't want to strip the old behavior (yet), consider adding this as a new experimental feature (enabled by default š?) called something like: "Only show preview for current markdown file" with extra text "Will make sure a preview is shown for current file being edited only, and will automatically close if current active file is not a markdown file".
Another cool extra option that came to mind: