From c14c28b56bfef3b8b4310cf1d50302563a3ea69f Mon Sep 17 00:00:00 2001 From: Mathieu PATUREL Date: Fri, 15 Nov 2019 16:10:50 +1100 Subject: [PATCH] fix the flickering by delaying and throwing Explanation coming on my blog, math2001.github.io, something like "lessons learned from re writing MarkdownLivePreview" --- MarkdownLivePreview.py | 18 +++++++++++++----- live-testing/images.md | 2 ++ live-testing/test.md | 6 ++---- resources/stylesheet.css | 3 ++- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/MarkdownLivePreview.py b/MarkdownLivePreview.py index ab7fa10..320a327 100644 --- a/MarkdownLivePreview.py +++ b/MarkdownLivePreview.py @@ -10,7 +10,7 @@ from .utils import * MARKDOWN_VIEW_INFOS = "markdown_view_infos" PREVIEW_VIEW_INFOS = "preview_view_infos" # FIXME: put this as a setting for the user to choose? -DELAY = 500 # ms +DELAY = 100 # ms def get_resource(resource): path = 'Packages/MarkdownLivePreview/resources/' + resource @@ -102,6 +102,7 @@ class OpenMarkdownPreviewCommand(sublime_plugin.TextCommand): # FIXME: is this the best way there is to check if the current syntax is markdown? # should we only support default markdown? # what about "md"? + # FIXME: what about other languages, where markdown preview roughly works? return 'markdown' in self.view.settings().get('syntax').lower() class MarkdownLivePreviewListener(sublime_plugin.EventListener): @@ -110,6 +111,10 @@ class MarkdownLivePreviewListener(sublime_plugin.EventListener): # markdown_view.id(): phantom set } + # we schedule an update for every key stroke, with a delay of DELAY + # then, we update only if now() - last_update > DELAY + last_update = 0 + # FIXME: maybe we shouldn't restore the file in the original window... def on_pre_close(self, markdown_view): @@ -124,7 +129,6 @@ class MarkdownLivePreviewListener(sublime_plugin.EventListener): self.file_name = markdown_view.file_name() if self.file_name is None: - # FIXME: this is duplicated code. How should it be generalized? total_region = sublime.Region(0, markdown_view.size()) self.content = markdown_view.substr(total_region) markdown_view.erase(edit, total_region) @@ -180,22 +184,26 @@ class MarkdownLivePreviewListener(sublime_plugin.EventListener): # @min_time_between_call(.5) def on_modified_async(self, markdown_view): - # FIXME: it keeps on flickering, it's really annoying - infos = markdown_view.settings().get(MARKDOWN_VIEW_INFOS) if not infos: return - self._update_preview(markdown_view) + # we schedule an update, which won't run if an + sublime.set_timeout(partial(self._update_preview, markdown_view), DELAY) def _update_preview(self, markdown_view): # if the buffer id is 0, that means that the markdown_view has been closed # This check is needed since a this function is used as a callback for when images # are loaded from the internet (ie. it could finish loading *after* the user # closes the markdown_view) + if time.time() - self.last_update < DELAY / 1000: + return + if markdown_view.buffer_id() == 0: return + self.last_update = time.time() + total_region = sublime.Region(0, markdown_view.size()) markdown = markdown_view.substr(total_region) diff --git a/live-testing/images.md b/live-testing/images.md index 734f94e..05b7958 100644 --- a/live-testing/images.md +++ b/live-testing/images.md @@ -1,5 +1,7 @@ I'm not sure that it **actually** going to work, but it seems nicer than the [previous version][prev] +this is a test, hello world + This is the first image from the local file system (absolute path, sorry, it's not going to work on your system unless your username is math2001): diff --git a/live-testing/test.md b/live-testing/test.md index 609f165..773c03f 100644 --- a/live-testing/test.md +++ b/live-testing/test.md @@ -1,4 +1,4 @@ -# hello world +# hello world This is a *test*. Some inline `[2]code()`. @@ -13,6 +13,4 @@ if input("answer yes") != 'yes': this flickering is really annoying... -this - -It looks like it's gone... Oh wait nah, it's still here... \ No newline at end of file +It looks like it's gone... Oh wait nah, it's still here... diff --git a/resources/stylesheet.css b/resources/stylesheet.css index f8c119d..fb94801 100644 --- a/resources/stylesheet.css +++ b/resources/stylesheet.css @@ -4,7 +4,8 @@ html { } body { - font-family: "Ubuntu", "DejaVu Sans", "Open Sans", sans-serif; + font-family: "Ubuntu", "DejaVu Sans", "Open Sans", sans-serif; + font-size: 15px; } blockquote {