fix relationships between md_view & preview

ST isn't crashing anymore
This commit is contained in:
math2001
2016-11-26 10:58:24 +11:00
parent 100b712ef1
commit 57f6989720
4 changed files with 57 additions and 18 deletions

View File

@ -38,6 +38,8 @@ This plugin is not finished, there's still some things to fix (custom css, focus
![demo](demo.gif)
### Custom css
### How to open the [README](http://github.com/math2001/MarkdownLivePreview/README.md)
Some of the package add a command in the menus, others in the command palette, or other nowhere. None of those options are really good, especially the last one on ST3 because the packages are compressed. But, fortunately, there is plugin that exists and **will solve this problem** for us (and he has a really cute name, don't you think?): [ReadmePlease](https://packagecontrol.io/packages/ReadmePlease).

View File

@ -17,6 +17,11 @@ def sm(*t, **kwargs):
def em(*t, **kwargs):
sublime.error_message(kwargs.get('sep', ' ').join([str(el) for el in t]))
def mini(val, min):
if val < min:
return min
return val
STYLE_FILE = os.path.join(sublime.packages_path(), 'User', 'MarkdownLivePreview.css')
def get_style():
"""Of course, this is temporal, there will be an option to customize the CSS"""
@ -28,16 +33,37 @@ def get_style():
body {
padding:10px;
font-family: "Open Sans", sans-serif;
background-color: #fff;
background-color: var(--background);
font-size: 15px;
}
blockquote {
font-style: italic;
display: block;
margin-left: 30px;
border: 1px solid red;
}
"""
code {
padding-left: 0.2rem;
padding-right: 0.2rem;
background-color: rgb(244.6,244.6,244.6);
margin: 0;
border-radius: 3px;
margin: 5px;
}
pre {
display: block;
margin-top: 20px;
line-height: 2;
background-color: rgb(244.6,244.6,244.6);
padding-left: 10px;
}
pre code {
padding-left: 0;
}
""".replace('\n', '').replace('\t', '')
def pre_with_br(html):
"""Because the phantoms of sublime text does not support <pre> blocks
@ -65,7 +91,6 @@ def create_preview(window, md_view):
preview_settings = preview.settings()
preview_settings.set('gutter', False)
preview_settings.set('scroll_past_end', False)
preview_settings.set('is_markdown_preview', True)
preview_settings.set('markdown_view_id', md_view.id())
@ -73,10 +98,12 @@ def create_preview(window, md_view):
window.focus_group(focus_group)
window.focus_view(md_view)
return preview, preview_settings
return preview
def show_html(md_view, preview):
html = '<style>{}</style>'.format(get_style()) + pre_with_br(markdown2.markdown(get_view_content(md_view), extras=['fenced-code-blocks']))
html = ('<style>{}</style>'.format(get_style()) +
pre_with_br(markdown2.markdown(get_view_content(md_view),
extras=['fenced-code-blocks'])))
html = HTMLParser().unescape(html)
preview.erase_phantoms('markdown_preview')
preview.add_phantom('markdown_preview',
@ -84,8 +111,11 @@ def show_html(md_view, preview):
html,
sublime.LAYOUT_INLINE,
lambda href: sublime.run_command('open_url', {'url': href}))
y = md_view.layout_extent()[1] / md_view.text_to_layout(md_view.sel()[0].begin())[1]
preview.set_viewport_position((0, y * preview.layout_extent()[1]), False)
# 0 < y < 1
y = md_view.text_to_layout(md_view.sel()[0].begin())[1] / md_view.layout_extent()[1]
# remove half of the viewport_extent.y to center it on the screen (verticaly)
vector = 0, y * preview.layout_extent()[1] - preview.viewport_extent()[1] / 2
preview.set_viewport_position(vector, animate=False)
def get_view_content(view):
return view.substr(sublime.Region(0, view.size()))
@ -115,18 +145,19 @@ class MarkdownInPopupCommand(sublime_plugin.EventListener):
if not markdown_preview_enabled:
if preview_id is not None:
close_preview(md_view_settings, get_view_from_id(window, preview_id))
preview = get_view_from_id(window, preview_id)
if preview:
close_preview(md_view_settings, preview)
return
if preview_id is None:
preview, preview_settings = create_preview(window, md_view)
preview = create_preview(window, md_view)
else:
preview = get_view_from_id(window, preview_id)
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)
@ -135,4 +166,12 @@ class MarkdownInPopupCommand(sublime_plugin.EventListener):
if settings.get('markdown_preview_enabled') is True:
preview = get_view_from_id(view.window(), settings.get('markdown_preview_id'))
if preview:
sublime.set_timeout_async(lambda: preview.close(), 100)
sublime.set_timeout_async(lambda: preview.close(), 250)
elif settings.get('is_markdown_preview') is True:
md_view = get_view_from_id(view.window(), settings.get('markdown_view_id'))
if md_view:
def callback():
md_view_settings = md_view.settings()
md_view_settings.erase('markdown_preview_enabled')
md_view_settings.erase('markdown_preview_id')
sublime.set_timeout_async(callback, 250)

View File

@ -1,9 +1,8 @@
# Hello world!
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.
#### The Zen of Python, by Tim Peters
### The Zen of Python, by Tim Peters c
> Beautiful is better than ugly.
> Explicit is better than implicit.
@ -25,8 +24,7 @@ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
> 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 c
> Code tells you how, comments tells you why
print('hello world')
print('hi')
@ -36,7 +34,7 @@ print('This is some pretty')
print('cool stuff')
```
This is some `code`
This is some `code` ccc
- a
- list

View File

@ -1,7 +1,7 @@
# todo
- add message in status bar
- add **custom css** feature
- add **custom css** feature @done
- sync scroll @done @improve: scroll preview to center when possible
- regive focus to the right markdown view @done
- set the title of the preview @done