From cf658c1b3ab3df508a16731906e291cc80be8530 Mon Sep 17 00:00:00 2001 From: ansuz Date: Fri, 26 May 2017 12:41:30 +0200 Subject: [PATCH] backport live changes --- www/code/main.js | 17 ++++++++++++++--- www/common/common-util.js | 9 +++++++++ www/common/cryptpad-common.js | 1 + 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/www/code/main.js b/www/code/main.js index fa6ecf12b..d82d73273 100644 --- a/www/code/main.js +++ b/www/code/main.js @@ -110,6 +110,18 @@ define([ return stringify(obj); }; + var forceDrawPreview = function () { + try { + DiffMd.apply(DiffMd.render(editor.getValue()), $preview); + } catch (e) { console.error(e); } + }; + + var drawPreview = Cryptpad.throttle(function () { + if (CodeMirror.highlightMode !== 'markdown') { return; } + if (!$previewContainer.is(':visible')) { return; } + forceDrawPreview(); + }, 150); + var onLocal = config.onLocal = function () { if (initializing) { return; } if (isHistoryMode) { return; } @@ -117,7 +129,7 @@ define([ editor.save(); - DiffMd.apply(DiffMd.render(editor.getValue()), $preview); + drawPreview(); var textValue = canonicalize(CodeMirror.$textarea.val()); var shjson = stringifyInner(textValue); @@ -324,14 +336,13 @@ define([ var hjson = JSON.parse(shjson); var remoteDoc = hjson.content; - DiffMd.apply(DiffMd.render(remoteDoc), $preview); - var highlightMode = hjson.highlightMode; if (highlightMode && highlightMode !== APP.highlightMode) { CodeMirror.setMode(highlightMode, onModeChanged); } CodeMirror.setValueAndCursor(oldDoc, remoteDoc, TextPatcher); + drawPreview(); if (!readOnly) { var textValue = canonicalize(CodeMirror.$textarea.val()); diff --git a/www/common/common-util.js b/www/common/common-util.js index 6fb2ad7bb..debbd8e2d 100644 --- a/www/common/common-util.js +++ b/www/common/common-util.js @@ -122,5 +122,14 @@ define([], function () { xhr.send(null); }; + Util.throttle = function (f, ms) { + var to; + var g = function () { + window.clearTimeout(to); + to = window.setTimeout(f, ms); + }; + return g; + }; + return Util; }); diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index 730a7984f..20927dc04 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -71,6 +71,7 @@ define([ common.bytesToMegabytes = Util.bytesToMegabytes; common.bytesToKilobytes = Util.bytesToKilobytes; common.fetch = Util.fetch; + common.throttle = Util.throttle; // import hash utilities for export var createRandomHash = common.createRandomHash = Hash.createRandomHash;