working, needs few improvement
This commit is contained in:
@ -1,10 +1,7 @@
|
|||||||
[
|
[
|
||||||
{
|
{
|
||||||
"keys": ["alt+m"],
|
"keys": ["alt+m"],
|
||||||
"command": "toggle_setting",
|
"command": "new_markdown_live_preview",
|
||||||
"args": {
|
|
||||||
"setting": "markdown_live_preview_enabled"
|
|
||||||
},
|
|
||||||
"context": [
|
"context": [
|
||||||
{
|
{
|
||||||
"key": "selector",
|
"key": "selector",
|
||||||
|
|||||||
@ -1,9 +1,6 @@
|
|||||||
[
|
[
|
||||||
{
|
{
|
||||||
"caption": "MarkdownLivePreview: Toggle",
|
"caption": "MarkdownLivePreview: Edit Current File",
|
||||||
"command": "toggle_setting",
|
"command": "new_markdown_live_preview"
|
||||||
"args": {
|
|
||||||
"setting": "markdown_live_preview_enabled"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
69
MLPApi.py
69
MLPApi.py
@ -23,81 +23,20 @@ def plugin_loaded():
|
|||||||
'default.css')
|
'default.css')
|
||||||
|
|
||||||
def get_preview_name(md_view):
|
def get_preview_name(md_view):
|
||||||
|
file_name = md_view.file_name()
|
||||||
name = md_view.name() \
|
name = md_view.name() \
|
||||||
or os.path.basename(md_view.file_name()) \
|
or os.path.basename(file_name) if file_name else None \
|
||||||
or 'Untitled'
|
or 'Untitled'
|
||||||
return name + ' - Preview'
|
return name + ' - Preview'
|
||||||
|
|
||||||
def find_preview(window):
|
def create_preview(window, file_name):
|
||||||
for view in window.views():
|
|
||||||
vsettings = view.settings()
|
|
||||||
if vsettings.get(IS_PREVIEW):
|
|
||||||
yield view, vsettings
|
|
||||||
|
|
||||||
def create_preview(md_view):
|
|
||||||
window = md_view.window()
|
|
||||||
md_view_settings = md_view.settings()
|
|
||||||
md_view_settings.set(JUST_CREATED, True)
|
|
||||||
|
|
||||||
preview = window.new_file()
|
preview = window.new_file()
|
||||||
|
|
||||||
psettings = preview.settings()
|
preview.set_name(get_preview_name(file_name))
|
||||||
psettings.set(IS_PREVIEW, True)
|
|
||||||
psettings.set(MD_VIEW_ID, md_view.id())
|
|
||||||
preview.set_name(get_preview_name(md_view))
|
|
||||||
preview.set_scratch(True)
|
preview.set_scratch(True)
|
||||||
md_view_settings.set(PREVIEW_ID, preview.id())
|
|
||||||
def move_and_focus_md_view():
|
|
||||||
window.run_command('new_pane')
|
|
||||||
sublime.set_timeout_async(lambda: window.focus_view(md_view), 250)
|
|
||||||
sublime.set_timeout_async(move_and_focus_md_view, 250)
|
|
||||||
|
|
||||||
return preview
|
return preview
|
||||||
|
|
||||||
def hide_preview(view):
|
|
||||||
window = view.window()
|
|
||||||
vsettings = view.settings()
|
|
||||||
if vsettings.get(IS_PREVIEW):
|
|
||||||
preview = view
|
|
||||||
psettings = vsettings
|
|
||||||
md_view_id = vsettings.get(MD_VIEW_ID)
|
|
||||||
md_view = get_view_from_id(window, md_view_id)
|
|
||||||
if md_view is None:
|
|
||||||
raise ValueError('Tried to get md_view from id {} but got None'.format(md_view_id))
|
|
||||||
mdvsettings = md_view.settings()
|
|
||||||
|
|
||||||
elif vsettings.get(PREVIEW_ENABLED):
|
|
||||||
md_view = view
|
|
||||||
preview_id = vsettings.get(PREVIEW_ID)
|
|
||||||
preview = get_view_from_id(window, preview_id)
|
|
||||||
mdvsettings = vsettings
|
|
||||||
if preview is None:
|
|
||||||
raise ValueError('Tried to get preview from id {} but got None'.format(preview_id))
|
|
||||||
psettings = preview.settings()
|
|
||||||
else:
|
|
||||||
raise ValueError('Call hide_preview with a view which is not the preview or the md_view')
|
|
||||||
psettings.set(IS_HIDDEN, True)
|
|
||||||
mdvsettings.erase(PREVIEW_ID)
|
|
||||||
sublime.set_timeout(lambda: preview.close(), 250)
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
window = md_view.window()
|
|
||||||
if window is None:
|
|
||||||
return
|
|
||||||
mdvsettings = md_view.settings()
|
|
||||||
preview_id = mdvsettings.get(PREVIEW_ID)
|
|
||||||
if not preview_id:
|
|
||||||
return
|
|
||||||
mdvsettings.erase(PREVIEW_ID)
|
|
||||||
preview = get_view_from_id(window, preview_id)
|
|
||||||
if preview is None:
|
|
||||||
raise ValueError('Tried to get view from id {} but got None'.format(preview_id))
|
|
||||||
psettings = preview.settings()
|
|
||||||
psettings.set(IS_HIDDEN, True)
|
|
||||||
sublime.set_timeout(preview.close(), 250)
|
|
||||||
|
|
||||||
def get_style():
|
def get_style():
|
||||||
content = ''.join([line.strip() for line in DEFAULT_STYLE_FILE.splitlines()])
|
content = ''.join([line.strip() for line in DEFAULT_STYLE_FILE.splitlines()])
|
||||||
return content + "pre code .space {color: var(--light-bg)}"
|
return content + "pre code .space {color: var(--light-bg)}"
|
||||||
|
|||||||
@ -9,45 +9,59 @@ from .MLPApi import *
|
|||||||
from .setting_names import *
|
from .setting_names import *
|
||||||
from .functions import *
|
from .functions import *
|
||||||
|
|
||||||
|
class NewMarkdownLivePreviewCommand(sublime_plugin.ApplicationCommand):
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
|
||||||
|
"""Inspired by the edit_settings command"""
|
||||||
|
|
||||||
|
current_view = sublime.active_window().active_view()
|
||||||
|
file_name = current_view.file_name()
|
||||||
|
current_view.close()
|
||||||
|
if file_name is None:
|
||||||
|
return sublime.error_message('Not supporting unsaved file for now')
|
||||||
|
|
||||||
|
sublime.run_command('new_window')
|
||||||
|
self.window = sublime.active_window()
|
||||||
|
self.window.settings().set(PREVIEW_WINDOW, True)
|
||||||
|
self.window.run_command('set_layout', {
|
||||||
|
'cols': [0.0, 0.5, 1.0],
|
||||||
|
'rows': [0.0, 1.0],
|
||||||
|
'cells': [[0, 0, 1, 1], [1, 0, 2, 1]]
|
||||||
|
})
|
||||||
|
self.window.focus_group(1)
|
||||||
|
preview = create_preview(self.window, current_view)
|
||||||
|
|
||||||
|
self.window.focus_group(0)
|
||||||
|
md_view = self.window.open_file(file_name)
|
||||||
|
mdsettings = md_view.settings()
|
||||||
|
|
||||||
|
|
||||||
|
mdsettings.set(PREVIEW_ENABLED, True)
|
||||||
|
mdsettings.set(PREVIEW_ID, preview.id())
|
||||||
|
|
||||||
|
def is_enabled(self):
|
||||||
|
return is_markdown_view(sublime.active_window().active_view())
|
||||||
|
|
||||||
class MarkdownLivePreviewListener(sublime_plugin.EventListener):
|
class MarkdownLivePreviewListener(sublime_plugin.EventListener):
|
||||||
|
|
||||||
def on_modified(self, view):
|
def on_modified(self, view):
|
||||||
if not is_markdown_view(view):
|
if not is_markdown_view(view): # faster than getting the settings
|
||||||
return
|
return
|
||||||
|
|
||||||
vsettings = view.settings()
|
vsettings = view.settings()
|
||||||
if vsettings.get(PREVIEW_ENABLED):
|
if not vsettings.get(PREVIEW_ENABLED):
|
||||||
id = vsettings.get(PREVIEW_ID)
|
|
||||||
preview = get_view_from_id(view.window(), id)
|
|
||||||
if id is None or preview is None:
|
|
||||||
preview = create_preview(view)
|
|
||||||
sublime.set_timeout_async(lambda: show_html(view, preview), 1000)
|
|
||||||
else:
|
|
||||||
show_html(view, preview)
|
|
||||||
|
|
||||||
return
|
return
|
||||||
|
id = vsettings.get(PREVIEW_ID)
|
||||||
|
if id is None:
|
||||||
|
raise ValueError('The preview id is None')
|
||||||
|
preview = get_view_from_id(view.window(), id)
|
||||||
|
if preview is None:
|
||||||
|
raise ValueError('The preview is None (id: {})'.format(id))
|
||||||
|
|
||||||
def on_activated(self, view):
|
show_html(view, preview)
|
||||||
# if view is md_view and has no preview
|
|
||||||
# -> create preview
|
|
||||||
window = view.window()
|
|
||||||
vsettings = view.settings()
|
|
||||||
if vsettings.get(PREVIEW_ENABLED):
|
|
||||||
id = vsettings.get(PREVIEW_ID)
|
|
||||||
preview = get_view_from_id(window, id)
|
|
||||||
if id is None or preview is None:
|
|
||||||
preview = create_preview(view)
|
|
||||||
sublime.set_timeout(lambda: show_html(view, preview), 1000)
|
|
||||||
else:
|
|
||||||
show_html(view, preview)
|
|
||||||
return
|
|
||||||
|
|
||||||
# if view is preview
|
def on_window_command(self, window, command, args):
|
||||||
# -> do nothing
|
if command == 'close' and window.settings().get(PREVIEW_WINDOW):
|
||||||
if vsettings.get(IS_PREVIEW):
|
return 'close_window', {}
|
||||||
return
|
|
||||||
# if view is not the md_view or the preview
|
def on_load_async(self, view):
|
||||||
# remove preview if any
|
self.on_modified(view)
|
||||||
for view, settings in find_preview(window):
|
|
||||||
settings.set(IS_HIDDEN, True)
|
|
||||||
view.close()
|
|
||||||
@ -1,10 +1,12 @@
|
|||||||
# DuckDuckGo - The Search engine you'll fall in love with this is a test.
|
# DuckDuckGo - The Search engine you'll fall in love
|
||||||
|
|
||||||
|
This is cool
|
||||||
|
|
||||||
This is a test, and this is pretty cool!
|
This is a test, and this is pretty cool!
|
||||||
|
|
||||||
Hope you'll enjoy using MarkdownLivePreview!
|
Hope you'll enjoy using MarkdownLivePreview!
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
[GitHub](https://octodex.github.com/images/jetpacktocat.png)
|
[GitHub](https://octodex.github.com/images/jetpacktocat.png)
|
||||||
|
|
||||||
|
|||||||
@ -6,3 +6,4 @@ IS_PREVIEW = 'is_markdown_live_preview'
|
|||||||
IS_HIDDEN = 'is_hidden_markdown_live_preview'
|
IS_HIDDEN = 'is_hidden_markdown_live_preview'
|
||||||
MD_VIEW_ID = 'markdown_live_preview_md_id'
|
MD_VIEW_ID = 'markdown_live_preview_md_id'
|
||||||
JUST_CREATED = 'markdown_live_preview_just_created'
|
JUST_CREATED = 'markdown_live_preview_just_created'
|
||||||
|
PREVIEW_WINDOW = 'markdown_live_preview_window'
|
||||||
|
|||||||
Reference in New Issue
Block a user