From cf658c1b3ab3df508a16731906e291cc80be8530 Mon Sep 17 00:00:00 2001 From: ansuz Date: Fri, 26 May 2017 12:41:30 +0200 Subject: [PATCH 1/2] 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; From 57017c68fa2169644ff721fb4c9af253ba9112b2 Mon Sep 17 00:00:00 2001 From: ansuz Date: Fri, 26 May 2017 12:42:12 +0200 Subject: [PATCH 2/2] hacks for cross-domain safari --- www/common/fsStore.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/common/fsStore.js b/www/common/fsStore.js index b20c38c48..08a1a8fa3 100644 --- a/www/common/fsStore.js +++ b/www/common/fsStore.js @@ -172,7 +172,7 @@ define([ if (typeof(proxy.loginToken) !== 'number') { proxy[tokenKey] = Math.floor(Math.random()*Number.MAX_SAFE_INTEGER); } - + if (sessionStorage) { sessionStorage.setItem('User_hash', localStorage.getItem('User_hash')); } var localToken = tryParsing(localStorage.getItem(tokenKey)); if (localToken === null) { // if that number hasn't been set to localStorage, do so.