clean code, working, add auto close/open preview

This commit is contained in:
math2001
2016-11-25 17:57:59 +11:00
parent 35e53cb683
commit af3ea839c3
3 changed files with 116 additions and 81 deletions

View File

@ -1,7 +1,7 @@
import sublime import sublime
import sublime_plugin import sublime_plugin
import html
from . import markdown2 from . import markdown2
import os.path
# Main sublime tools function # Main sublime tools function
@ -14,25 +14,9 @@ def sm(*t, **kwargs):
def em(*t, **kwargs): def em(*t, **kwargs):
sublime.error_message(kwargs.get('sep', ' ').join([str(el) for el in t])) sublime.error_message(kwargs.get('sep', ' ').join([str(el) for el in t]))
class MarkdownInPopupCommand(sublime_plugin.EventListener): def get_style():
def run(self, view): """Of course, this is temporal, there will be an option to customize the CSS"""
view.show_popup('<h1> hello </h1>', sublime.HIDE_ON_MOUSE_MOVE_AWAY, 1) return """<style>
return
ph = sublime.Phantom(sublime.Region(0), "<h1> hello </h1>", sublime.LAYOUT_INLINE, None)
ph_set = sublime.PhantomSet(sublime.active_window().active_view(), 'tests')
ph_set.update([ph])
def get_view_content(self, view):
return view.substr(sublime.Region(0, view.size()))
def on_modified(self, current_view):
current_view_settings = current_view.settings()
if 'markdown' not in current_view_settings.get('syntax').lower():
return
if current_view_settings.get('markdown_preview_enabled', False) is not True:
return
w = current_view.window()
html = """<style>
body { body {
padding:10px; padding:10px;
font-family: "Open Sans", sans-serif; font-family: "Open Sans", sans-serif;
@ -46,57 +30,84 @@ class MarkdownInPopupCommand(sublime_plugin.EventListener):
border: 1px solid red; border: 1px solid red;
} }
</style>""" </style>"""
html += markdown2.markdown(self.get_view_content(current_view))
view_id = current_view_settings.get('markdown_preview_id', None)
def create_preview_panel():
focus_group, focus_view = w.get_view_index(current_view)
preview = w.new_file()
w.run_command('new_pane')
view_id = preview.id()
current_view_settings.set('markdown_preview_id', view_id)
w.focus_group(focus_group)
w.focus_view(current_view)
return preview
def show_html(view, html): def close_preview(md_view_settings, preview):
view.settings().set('gutter', False) preview.close()
view.erase_phantoms('markdown_preview') md_view_settings.erase('markdown_preview_id')
self.phantom_id = view.add_phantom('markdown_preview', md_view_settings.erase('markdown_preview_enabled')
def create_preview(window, md_view):
focus_group, focus_view = window.get_view_index(md_view)
preview = window.new_file()
window.run_command('new_pane') # move the preview to a new group
preview.set_name(os.path.basename(md_view.file_name()) + ' - Preview')
preview_settings = preview.settings()
preview_settings.set('gutter', False)
preview_settings.set('is_markdown_preview', True)
preview_settings.set('markdown_view_id', md_view.id())
md_view.settings().set('markdown_preview_id', preview.id())
window.focus_group(focus_group)
window.focus_view(md_view)
return preview, preview_settings
def show_html(md_view, preview):
html = get_style() + markdown2.markdown(get_view_content(md_view))
preview.erase_phantoms('markdown_preview')
preview.add_phantom('markdown_preview',
sublime.Region(0), sublime.Region(0),
html, html,
sublime.LAYOUT_INLINE, sublime.LAYOUT_INLINE,
lambda href: sublime.run_command('open_url', {'url': href})) lambda href: sublime.run_command('open_url', {'url': href}))
for view in w.views(): def get_view_content(view):
if view.id() == view_id: return view.substr(sublime.Region(0, view.size()))
show_html(view, html)
break def get_view_from_id(window, id):
for view in window.views():
if view.id() == id:
return view
class MarkdownInPopupCommand(sublime_plugin.EventListener):
def on_load(self, view):
settings = view.settings()
if not 'markdown' in settings.get('syntax').lower():
return
settings.add_on_change('markdown_preview_enabled', lambda: self.on_modified(view))
def on_modified(self, md_view):
window = md_view.window()
md_view_settings = md_view.settings()
if not 'markdown' in md_view_settings.get('syntax').lower():
return
markdown_preview_enabled = md_view_settings.get('markdown_preview_enabled') is True
preview_id = md_view_settings.get('markdown_preview_id', None)
if not markdown_preview_enabled:
if preview_id is not None:
close_preview(md_view_settings, get_view_from_id(window, preview_id))
return
if preview_id is None:
preview, preview_settings = create_preview(window, md_view)
else: else:
preview = create_preview_panel() preview = get_view_from_id(window, preview_id)
show_html(preview, html) if not preview:
md_view_settings.erase('markdown_preview_id')
md_view_settings.erase('markdown_preview_enabled')
return
preview_settings = preview.settings()
show_html(md_view, preview)
def on_pre_close(self, view):
# class MarkdownInPopupCommand(sublime_plugin.EventListener): settings = view.settings()
class MarkdownInPopupCommandc: if settings.get('markdown_preview_enabled') is True:
preview = get_view_from_id(view.window(), settings.get('markdown_preview_id'))
def update_phantom(self, content, preview_view): if preview:
ph = sublime.Phantom(sublime.Region(0), content, sublime.LAYOUT_BLOCK) sublime.set_timeout_async(lambda: preview.close(), 100)
self.phantom_set.update([ph])
def on_modified(self, view):
content = """
<style>
body {
margin:10px;
}
blockquote {
font-style: italic;
display: block;
margin: 10px;
}
</style>
"""
content += markdown2.markdown(view.substr(sublime.Region(0, view.size())))
# self.update_phantom(content)

View File

@ -2,6 +2,27 @@
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
> Explicit is better than implicit #### The Zen of Python, by Tim Peters
> Beautiful is better than ugly.
> Explicit is better than implicit.
> Simple is better than complex.
> Complex is better than complicated.
> Flat is better than nested.
> Sparse is better than dense.
> Readability counts.
> Special cases aren't special enough to break the rules.
> Although practicality beats purity.
> Errors should never pass silently.
> Unless explicitly silenced.
> In the face of ambiguity, refuse the temptation to guess.
> There should be one-- and preferably only one --obvious way to do it.
> Although that way may not be obvious at first unless you're Dutch.
> Now is better than never.
> Although never is often better than *right* now.
> If the implementation is hard to explain, it's a bad idea.
> If the implementation is easy to explain, it may be a good idea.
> Namespaces are one honking great idea -- let's do more of those!
> Code tells you how, comments tells you why
Hi!

View File

@ -1,6 +1,9 @@
# todo # todo
- add **custom css** feature - add **custom css** feature
- check when setting is activated and create panel and stuff
- sync scroll - sync scroll
- regive focus to the right markdown view @done - regive focus to the right markdown view @done
- set the title of the preview
- add message in status bar
- disable previewing when the preview is closed
- check when setting is activated and create panel and stuff