From 0bd4caeb7e83e7399affe107c07ba2f259eabb99 Mon Sep 17 00:00:00 2001 From: yflory Date: Thu, 18 May 2017 14:57:04 +0200 Subject: [PATCH] Add the task list code to the markdown renderer --- www/code/inner.html | 5 +++++ www/code/main.js | 48 ++++++++++++++++++++++++---------------- www/common/diffMarked.js | 20 ++++++++++++++++- www/slide/slide.js | 17 -------------- 4 files changed, 53 insertions(+), 37 deletions(-) diff --git a/www/code/inner.html b/www/code/inner.html index 8dd02548a..36d5911ee 100644 --- a/www/code/inner.html +++ b/www/code/inner.html @@ -55,6 +55,11 @@ max-width: 80%; resize: horizontal; } + .CodeMirror.fullPage { + min-width: 100%; + max-width: 100%; + resize: none; + } .CodeMirror-focused .cm-matchhighlight { background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAFklEQVQI12NgYGBgkKzc8x9CMDAwAAAmhwSbidEoSQAAAABJRU5ErkJggg==); background-position: bottom; diff --git a/www/code/main.js b/www/code/main.js index 04e20141a..a1261b8ce 100644 --- a/www/code/main.js +++ b/www/code/main.js @@ -13,14 +13,14 @@ define([ Cryptget, DiffMd) { var Messages = Cryptpad.Messages; - var module = window.APP = { + var APP = window.APP = { Cryptpad: Cryptpad, }; $(function () { Cryptpad.addLoadingScreen(); - var ifrw = module.ifrw = $('#pad-iframe')[0].contentWindow; + var ifrw = APP.ifrw = $('#pad-iframe')[0].contentWindow; var stringify = function (obj) { return JSONSortify(obj); }; @@ -28,6 +28,7 @@ define([ var toolbar; var editor; var $iframe = $('#pad-iframe').contents(); + var $previewContainer = $iframe.find('#previewContainer'); var $preview = $iframe.find('#preview'); $preview.click(function (e) { if (!e.target) { return; } @@ -58,7 +59,7 @@ define([ var isHistoryMode = false; - var setEditable = module.setEditable = function (bool) { + var setEditable = APP.setEditable = function (bool) { if (readOnly && bool) { return; } editor.setOption('readOnly', !bool); }; @@ -121,21 +122,24 @@ define([ var textValue = canonicalize(CodeMirror.$textarea.val()); var shjson = stringifyInner(textValue); - module.patchText(shjson); + APP.patchText(shjson); - if (module.realtime.getUserDoc() !== shjson) { + if (APP.realtime.getUserDoc() !== shjson) { console.error("realtime.getUserDoc() !== shjson"); } }; var onModeChanged = function (mode) { - if (mode === "markdown") { + var $codeMirror = $iframe.find('.CodeMirror'); + if (mode === "markdown") { APP.$previewButton.show(); - $preview.show(); + $previewContainer.show(); + $codeMirror.removeClass('fullPage'); return; } APP.$previewButton.hide(); - $preview.hide(); + $previewContainer.hide(); + $codeMirror.addClass('fullPage'); }; config.onInit = function (info) { @@ -161,7 +165,7 @@ define([ network: info.network, $container: $bar }; - toolbar = module.toolbar = Toolbar.create(configTb); + toolbar = APP.toolbar = Toolbar.create(configTb); Title.setToolbar(toolbar); CodeMirror.init(config.onLocal, Title, toolbar); @@ -221,10 +225,16 @@ define([ $previewButton.removeClass('fa-question').addClass('fa-eye'); $previewButton.attr('title', 'TODO Preview'); //TODO $previewButton.click(function () { + var $codeMirror = $iframe.find('.CodeMirror'); if (CodeMirror.highlightMode !== 'markdown') { - return void $preview.hide(); + $previewContainer.show(); + } + $previewContainer.toggle(); + if ($previewContainer.is(':visible')) { + $codeMirror.removeClass('fullPage'); + } else { + $codeMirror.addClass('fullPage'); } - $preview.toggle(); }); $rightside.append($previewButton); @@ -240,15 +250,15 @@ define([ }; config.onReady = function (info) { - if (module.realtime !== info.realtime) { - var realtime = module.realtime = info.realtime; - module.patchText = TextPatcher.create({ + if (APP.realtime !== info.realtime) { + var realtime = APP.realtime = info.realtime; + APP.patchText = TextPatcher.create({ realtime: realtime, //logging: true }); } - var userDoc = module.realtime.getUserDoc(); + var userDoc = APP.realtime.getUserDoc(); var isNew = false; if (userDoc === "" || userDoc === "{}") { isNew = true; } @@ -301,7 +311,7 @@ define([ if (isHistoryMode) { return; } var oldDoc = canonicalize(CodeMirror.$textarea.val()); - var shjson = module.realtime.getUserDoc(); + var shjson = APP.realtime.getUserDoc(); // Update the user list (metadata) from the hyperjson Metadata.update(shjson); @@ -312,7 +322,7 @@ define([ DiffMd.apply(DiffMd.render(remoteDoc), $preview); var highlightMode = hjson.highlightMode; - if (highlightMode && highlightMode !== module.highlightMode) { + if (highlightMode && highlightMode !== APP.highlightMode) { CodeMirror.setMode(highlightMode, onModeChanged); } @@ -324,7 +334,7 @@ define([ if (shjson2 !== shjson) { console.error("shjson2 !== shjson"); TextPatcher.log(shjson, TextPatcher.diff(shjson, shjson2)); - module.patchText(shjson2); + APP.patchText(shjson2); } } if (oldDoc !== remoteDoc) { Cryptpad.notify(); } @@ -351,7 +361,7 @@ define([ config.onError = onConnectError; - module.realtime = Realtime.start(config); + APP.realtime = Realtime.start(config); editor.on('change', onLocal); diff --git a/www/common/diffMarked.js b/www/common/diffMarked.js index b3cc5fe1e..c73dfd072 100644 --- a/www/common/diffMarked.js +++ b/www/common/diffMarked.js @@ -6,7 +6,7 @@ define([ var DiffMd = {} var DiffDOM = window.diffDOM; - var renderer = DiffMd.renderer = new Marked.Renderer(); + var renderer = new Marked.Renderer(); Marked.setOptions({ renderer: renderer @@ -16,6 +16,24 @@ define([ return Marked(md); }; + // Tasks list + var checkedTaskItemPtn = /^\s*\[x\]\s*/; + var uncheckedTaskItemPtn = /^\s*\[ \]\s*/; + renderer.listitem = function (text) { + var isCheckedTaskItem = checkedTaskItemPtn.test(text); + var isUncheckedTaskItem = uncheckedTaskItemPtn.test(text); + if (isCheckedTaskItem) { + text = text.replace(checkedTaskItemPtn, + ' ') + '\n'; + } + if (isUncheckedTaskItem) { + text = text.replace(uncheckedTaskItemPtn, + ' ') + '\n'; + } + var cls = (isCheckedTaskItem || isUncheckedTaskItem) ? ' class="todo-list-item"' : ''; + return '' + text + '\n'; + }; + var forbiddenTags = [ 'SCRIPT', 'IFRAME', diff --git a/www/slide/slide.js b/www/slide/slide.js index 108a8848a..9f919ecfa 100644 --- a/www/slide/slide.js +++ b/www/slide/slide.js @@ -2,23 +2,6 @@ define([ 'jquery', '/common/diffMarked.js', ],function ($, DiffMd) { - // Tasks list - var checkedTaskItemPtn = /^\s*\[x\]\s*/; - var uncheckedTaskItemPtn = /^\s*\[ \]\s*/; - DiffMd.renderer.listitem = function (text) { - var isCheckedTaskItem = checkedTaskItemPtn.test(text); - var isUncheckedTaskItem = uncheckedTaskItemPtn.test(text); - if (isCheckedTaskItem) { - text = text.replace(checkedTaskItemPtn, - ' ') + '\n'; - } - if (isUncheckedTaskItem) { - text = text.replace(uncheckedTaskItemPtn, - ' ') + '\n'; - } - var cls = (isCheckedTaskItem || isUncheckedTaskItem) ? ' class="todo-list-item"' : ''; - return '' + text + '\n'; - }; var Slide = { index: 0,