remove utils.py file; use a setting file for delay between updates
utils.py contained two functions. One which wasn't used, and another, get_settings() which I moved to MarkdownLivePreview.py Add an entry to the command palette to edit the settings
This commit is contained in:
@ -1,3 +1,12 @@
|
|||||||
|
"""
|
||||||
|
Terminology
|
||||||
|
original_view: the view in the regular editor, without it's own window
|
||||||
|
markdown_view: the markdown view, in the special window
|
||||||
|
preview_view: the preview view, in the special window
|
||||||
|
original_window: the regular window
|
||||||
|
preview_window: the window with the markdown file and the preview
|
||||||
|
"""
|
||||||
|
|
||||||
import os.path
|
import os.path
|
||||||
import sublime
|
import sublime
|
||||||
import sublime_plugin
|
import sublime_plugin
|
||||||
@ -5,44 +14,22 @@ import sublime_plugin
|
|||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
from .markdown2html import markdown2html
|
from .markdown2html import markdown2html
|
||||||
from .utils import *
|
|
||||||
|
|
||||||
MARKDOWN_VIEW_INFOS = "markdown_view_infos"
|
MARKDOWN_VIEW_INFOS = "markdown_view_infos"
|
||||||
PREVIEW_VIEW_INFOS = "preview_view_infos"
|
PREVIEW_VIEW_INFOS = "preview_view_infos"
|
||||||
# FIXME: put this as a setting for the user to choose?
|
SETTING_DELAY_BETWEEN_UPDATES = "delay_between_updates"
|
||||||
DELAY = 100 # ms
|
|
||||||
|
|
||||||
|
|
||||||
def get_resource(resource):
|
|
||||||
path = "Packages/MarkdownLivePreview/resources/" + resource
|
|
||||||
abs_path = os.path.join(sublime.packages_path(), "..", path)
|
|
||||||
if os.path.isfile(abs_path):
|
|
||||||
with open(abs_path, "r") as fp:
|
|
||||||
return fp.read()
|
|
||||||
return sublime.load_resource(path)
|
|
||||||
|
|
||||||
|
|
||||||
resources = {}
|
resources = {}
|
||||||
|
|
||||||
|
|
||||||
def plugin_loaded():
|
def plugin_loaded():
|
||||||
|
global DELAY
|
||||||
resources["base64_404_image"] = get_resource("404.base64")
|
resources["base64_404_image"] = get_resource("404.base64")
|
||||||
resources["base64_loading_image"] = get_resource("loading.base64")
|
resources["base64_loading_image"] = get_resource("loading.base64")
|
||||||
resources["stylesheet"] = get_resource("stylesheet.css")
|
resources["stylesheet"] = get_resource("stylesheet.css")
|
||||||
|
# FIXME: how could we make this setting update without restarting sublime text
|
||||||
|
# and not loading it every update as well
|
||||||
# try to reload the resources if we save this file
|
DELAY = get_settings().get(SETTING_DELAY_BETWEEN_UPDATES)
|
||||||
try:
|
|
||||||
plugin_loaded()
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# Terminology
|
|
||||||
# original_view: the view in the regular editor, without it's own window
|
|
||||||
# markdown_view: the markdown view, in the special window
|
|
||||||
# preview_view: the preview view, in the special window
|
|
||||||
# original_window: the regular window
|
|
||||||
# preview_window: the window with the markdown file and the preview
|
|
||||||
|
|
||||||
|
|
||||||
class MdlpInsertCommand(sublime_plugin.TextCommand):
|
class MdlpInsertCommand(sublime_plugin.TextCommand):
|
||||||
@ -237,3 +224,23 @@ class MarkdownLivePreviewListener(sublime_plugin.EventListener):
|
|||||||
)
|
)
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_settings():
|
||||||
|
return sublime.load_settings("MarkdownLivePreview.sublime-settings")
|
||||||
|
|
||||||
|
|
||||||
|
def get_resource(resource):
|
||||||
|
path = "Packages/MarkdownLivePreview/resources/" + resource
|
||||||
|
abs_path = os.path.join(sublime.packages_path(), "..", path)
|
||||||
|
if os.path.isfile(abs_path):
|
||||||
|
with open(abs_path, "r") as fp:
|
||||||
|
return fp.read()
|
||||||
|
return sublime.load_resource(path)
|
||||||
|
|
||||||
|
|
||||||
|
# try to reload the resources if we save this file
|
||||||
|
try:
|
||||||
|
plugin_loaded()
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|||||||
@ -3,5 +3,13 @@
|
|||||||
{
|
{
|
||||||
"caption": "MarkdownLivePreview: Open Preview",
|
"caption": "MarkdownLivePreview: Open Preview",
|
||||||
"command": "open_markdown_preview"
|
"command": "open_markdown_preview"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"caption": "MarkdownLivePreview: Open Settings",
|
||||||
|
"command": "edit_settings", "args":
|
||||||
|
{
|
||||||
|
"base_file": "${packages}/MarkdownLivePreview/MarkdownLivePreview.sublime-settings",
|
||||||
|
"default": "{\n\t$0\n}\n"
|
||||||
|
},
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
4
MarkdownLivePreview.sublime-settings
Normal file
4
MarkdownLivePreview.sublime-settings
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
// minimum number of milliseconds to wait before updating the preview again
|
||||||
|
"delay_between_updates": 100
|
||||||
|
}
|
||||||
@ -1,9 +1 @@
|
|||||||
```
|
# hello world
|
||||||
hello world
|
|
||||||
```
|
|
||||||
|
|
||||||
```
|
|
||||||
second part
|
|
||||||
```
|
|
||||||
|
|
||||||
hello world
|
|
||||||
|
|||||||
27
utils.py
27
utils.py
@ -1,27 +0,0 @@
|
|||||||
# import sublime
|
|
||||||
import time
|
|
||||||
|
|
||||||
|
|
||||||
def get_settings():
|
|
||||||
return sublime.get_settings("MarkdownLivePreview.sublime-settings")
|
|
||||||
|
|
||||||
|
|
||||||
def min_time_between_call(timeout, on_block=lambda *args, **kwargs: None):
|
|
||||||
""" Enforces a timeout between each call to the function
|
|
||||||
timeout is in seconds
|
|
||||||
"""
|
|
||||||
last_call = 0
|
|
||||||
|
|
||||||
def outer(func):
|
|
||||||
def wrapper(*args, **kwargs):
|
|
||||||
nonlocal last_call
|
|
||||||
|
|
||||||
if time.time() - last_call < timeout:
|
|
||||||
time.sleep(timeout - (time.time() - last_call))
|
|
||||||
|
|
||||||
last_call = time.time()
|
|
||||||
return func(*args, **kwargs)
|
|
||||||
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
return outer
|
|
||||||
Reference in New Issue
Block a user