working, needs few improvement

This commit is contained in:
Mathieu PATUREL
2017-01-08 12:30:20 +11:00
parent 3dcaed0ede
commit 75a8cf53f9
6 changed files with 61 additions and 111 deletions

View File

@ -1,10 +1,7 @@
[
{
"keys": ["alt+m"],
"command": "toggle_setting",
"args": {
"setting": "markdown_live_preview_enabled"
},
"command": "new_markdown_live_preview",
"context": [
{
"key": "selector",

View File

@ -1,9 +1,6 @@
[
{
"caption": "MarkdownLivePreview: Toggle",
"command": "toggle_setting",
"args": {
"setting": "markdown_live_preview_enabled"
}
"caption": "MarkdownLivePreview: Edit Current File",
"command": "new_markdown_live_preview"
}
]

View File

@ -23,81 +23,20 @@ def plugin_loaded():
'default.css')
def get_preview_name(md_view):
file_name = md_view.file_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'
return name + ' - Preview'
def find_preview(window):
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)
def create_preview(window, file_name):
preview = window.new_file()
psettings = preview.settings()
psettings.set(IS_PREVIEW, True)
psettings.set(MD_VIEW_ID, md_view.id())
preview.set_name(get_preview_name(md_view))
preview.set_name(get_preview_name(file_name))
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
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():
content = ''.join([line.strip() for line in DEFAULT_STYLE_FILE.splitlines()])
return content + "pre code .space {color: var(--light-bg)}"

View File

@ -9,45 +9,59 @@ from .MLPApi import *
from .setting_names 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):
def on_modified(self, view):
if not is_markdown_view(view):
if not is_markdown_view(view): # faster than getting the settings
return
vsettings = view.settings()
if vsettings.get(PREVIEW_ENABLED):
if not vsettings.get(PREVIEW_ENABLED):
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 id is None or preview is None:
preview = create_preview(view)
sublime.set_timeout_async(lambda: show_html(view, preview), 1000)
else:
if preview is None:
raise ValueError('The preview is None (id: {})'.format(id))
show_html(view, preview)
return
def on_window_command(self, window, command, args):
if command == 'close' and window.settings().get(PREVIEW_WINDOW):
return 'close_window', {}
def on_activated(self, view):
# 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
# -> do nothing
if vsettings.get(IS_PREVIEW):
return
# if view is not the md_view or the preview
# remove preview if any
for view, settings in find_preview(window):
settings.set(IS_HIDDEN, True)
view.close()
def on_load_async(self, view):
self.on_modified(view)

View File

@ -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!
Hope you'll enjoy using MarkdownLivePreview!
![image](https://forum.sublimetext.com/uploads/st-forum-wide.png)
![Sublime Text Forum](https://forum.sublimetext.com/uploads/st-forum-wide.png)
[GitHub](https://octodex.github.com/images/jetpacktocat.png)

View File

@ -6,3 +6,4 @@ IS_PREVIEW = 'is_markdown_live_preview'
IS_HIDDEN = 'is_hidden_markdown_live_preview'
MD_VIEW_ID = 'markdown_live_preview_md_id'
JUST_CREATED = 'markdown_live_preview_just_created'
PREVIEW_WINDOW = 'markdown_live_preview_window'