1. Migrated /slide/ to use framework

2. Fixed CSS not being applied on /slide/
3. Minor changes to /code/ to reuse code which is also used on /slide/
pull/1/head
Caleb James DeLisle 7 years ago
parent 1e5bc5da14
commit fa6938baa8

@ -166,6 +166,7 @@ define(function () {
out.slideOptionsText = "Options"; out.slideOptionsText = "Options";
out.slideOptionsTitle = "Customize your slides"; out.slideOptionsTitle = "Customize your slides";
out.slideOptionsButton = "Save (enter)"; out.slideOptionsButton = "Save (enter)";
out.slide_invalidLess = "Invalid custom style";
out.languageButton = "Language"; out.languageButton = "Language";
out.languageButtonTitle = "Select the language to use for the syntax highlighting"; out.languageButtonTitle = "Select the language to use for the syntax highlighting";

@ -51,8 +51,6 @@ define([
window.CodeMirror = CMeditor; window.CodeMirror = CMeditor;
var Messages = Cryptpad.Messages; var Messages = Cryptpad.Messages;
var canonicalize = function (t) { return t.replace(/\r\n/g, '\n'); };
var MEDIA_TAG_MODES = Object.freeze([ var MEDIA_TAG_MODES = Object.freeze([
'markdown', 'markdown',
'html', 'html',
@ -142,7 +140,6 @@ define([
framework.onReady(function () { framework.onReady(function () {
// add the splitter // add the splitter
if ($('.cp-splitter').length) { return; }
var splitter = $('<div>', { var splitter = $('<div>', {
'class': 'cp-splitter' 'class': 'cp-splitter'
}).appendTo($previewContainer); }).appendTo($previewContainer);
@ -266,23 +263,19 @@ define([
//// ////
framework.onContentUpdate(function (newContent) { framework.onContentUpdate(function (newContent) {
var oldDoc = canonicalize(CodeMirror.$textarea.val()); CodeMirror.contentUpdate(newContent);
var remoteDoc = newContent.content;
var highlightMode = newContent.highlightMode; var highlightMode = newContent.highlightMode;
if (highlightMode && highlightMode !== CodeMirror.highlightMode) { if (highlightMode && highlightMode !== CodeMirror.highlightMode) {
CodeMirror.setMode(highlightMode, evModeChange.fire); CodeMirror.setMode(highlightMode, evModeChange.fire);
} }
CodeMirror.setValueAndCursor(oldDoc, remoteDoc, TextPatcher);
previewPane.draw(); previewPane.draw();
}); });
framework.setContentGetter(function () { framework.setContentGetter(function () {
editor.save(); var content = CodeMirror.getContent();
content.highlightMode = CodeMirror.highlightMode;
previewPane.draw(); previewPane.draw();
return { return content;
content: canonicalize(CodeMirror.$textarea.val()),
highlightMode: CodeMirror.highlightMode
};
}); });
framework.onEditableChange(function () { framework.onEditableChange(function () {

@ -376,7 +376,9 @@ define([
}).nThen(function () { }).nThen(function () {
title = common.createTitle({ getHeadingText: titleRecommender }, onLocal); title = common.createTitle({
getHeadingText: function () { return titleRecommender(); }
}, onLocal);
var configTb = { var configTb = {
displayed: ['userlist', 'title', 'useradmin', 'spinner', 'newpad', 'share', 'limit'], displayed: ['userlist', 'title', 'useradmin', 'spinner', 'newpad', 'share', 'limit'],
title: title.getTitleConfig(), title: title.getTitleConfig(),

@ -3,9 +3,10 @@ define([
'/common/modes.js', '/common/modes.js',
'/common/themes.js', '/common/themes.js',
'/common/cryptpad-common.js', '/common/cryptpad-common.js',
'/bower_components/textpatcher/TextPatcher.js',
'/bower_components/file-saver/FileSaver.min.js' '/bower_components/file-saver/FileSaver.min.js'
], function ($, Modes, Themes, Cryptpad) { ], function ($, Modes, Themes, Cryptpad, TextPatcher) {
var saveAs = window.saveAs; var saveAs = window.saveAs;
var module = {}; var module = {};
@ -305,6 +306,42 @@ define([
editor.scrollTo(scroll.left, scroll.top); editor.scrollTo(scroll.left, scroll.top);
}; };
/////
var canonicalize = function (t) { return t.replace(/\r\n/g, '\n'); };
exp.contentUpdate = function (newContent) {
var oldDoc = canonicalize($textarea.val());
var remoteDoc = newContent.content;
exp.setValueAndCursor(oldDoc, remoteDoc, TextPatcher);
};
exp.getContent = function () {
editor.save();
return { content: canonicalize($textarea.val()) };
};
exp.mkFileManager = function (framework) {
var fmConfig = {
dropArea: $('.CodeMirror'),
body: $('body'),
onUploaded: function (ev, data) {
//var cursor = editor.getCursor();
//var cleanName = data.name.replace(/[\[\]]/g, '');
//var text = '!['+cleanName+']('+data.url+')';
var parsed = Cryptpad.parsePadUrl(data.url);
var hexFileName = Cryptpad.base64ToHex(parsed.hashData.channel);
var src = '/blob/' + hexFileName.slice(0,2) + '/' + hexFileName;
var mt = '<media-tag src="' + src + '" data-crypto-key="cryptpad:' +
parsed.hashData.key + '"></media-tag>';
editor.replaceSelection(mt);
}
};
framework._.sfCommon.createFileManager(fmConfig);
};
return exp; return exp;
}; };

@ -324,3 +324,6 @@ p {
min-width:0; min-width:0;
} }
pre.cp-slide-css-error {
color: white;
}

@ -5,7 +5,7 @@
<meta content="text/html; charset=utf-8" http-equiv="content-type"/> <meta content="text/html; charset=utf-8" http-equiv="content-type"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="referrer" content="no-referrer" /> <meta name="referrer" content="no-referrer" />
<script async data-bootload="main.js" data-main="/common/boot.js?ver=1.0" src="/bower_components/requirejs/require.js?ver=2.3.5"></script> <script async data-bootload="/common/sframe-app-outer.js" data-main="/common/boot.js?ver=1.0" src="/bower_components/requirejs/require.js?ver=2.3.5"></script>
<style> <style>
html, body { html, body {
margin: 0px; margin: 0px;

@ -1,19 +1,12 @@
define([ define([
'jquery', 'jquery',
'/bower_components/chainpad-crypto/crypto.js',
'/bower_components/textpatcher/TextPatcher.js',
'/common/toolbar3.js',
'json.sortify', 'json.sortify',
'/bower_components/chainpad-json-validator/json-ot.js',
'/common/cryptpad-common.js', '/common/cryptpad-common.js',
'/common/cryptget.js',
'/common/diffMarked.js',
'/bower_components/nthen/index.js', '/bower_components/nthen/index.js',
'/common/sframe-common.js', '/common/sframe-common.js',
'/api/config',
'/common/common-realtime.js',
'/slide/slide.js', '/slide/slide.js',
'/common/sframe-app-framework.js',
'/common/common-util.js',
'cm/lib/codemirror', 'cm/lib/codemirror',
'css!/bower_components/bootstrap/dist/css/bootstrap.min.css', 'css!/bower_components/bootstrap/dist/css/bootstrap.min.css',
@ -48,150 +41,84 @@ define([
], function ( ], function (
$, $,
Crypto,
TextPatcher,
Toolbar,
JSONSortify, JSONSortify,
JsonOT,
Cryptpad, Cryptpad,
Cryptget,
DiffMd,
nThen, nThen,
SFCommon, SFCommon,
ApiConfig,
CommonRealtime,
Slide, Slide,
Framework,
Util,
CMeditor) CMeditor)
{ {
window.CodeMirror = CMeditor; window.CodeMirror = CMeditor;
var Messages = Cryptpad.Messages; var Messages = Cryptpad.Messages;
var APP = window.APP = {
Cryptpad: Cryptpad,
};
var SLIDE_BACKCOLOR_ID = "cp-app-slide-toolbar-backcolor"; var SLIDE_BACKCOLOR_ID = "cp-app-slide-toolbar-backcolor";
var SLIDE_COLOR_ID = "cp-app-slide-toolbar-color"; var SLIDE_COLOR_ID = "cp-app-slide-toolbar-color";
var stringify = function (obj) { var mkLess = function (less) {
return JSONSortify(obj); return (
}; '#cp-app-slide-print .cp-app-slide-frame, ' +
'#cp-app-slide-modal #cp-app-slide-modal-content .cp-app-slide-frame {\r\n' +
var toolbar; less +
var isPresentMode; '\r\n}'
);
var onConnectError = function () {
Cryptpad.errorLoadingScreen(Messages.websocketError);
}; };
var andThen = function (editor, CodeMirror, common) { var mkSlidePreviewPane = function (framework, $contentContainer) {
var readOnly = false; var $previewButton = framework._.sfCommon.createButton(null, true);
var cpNfInner; $previewButton.removeClass('fa-question').addClass('fa-eye');
var metadataMgr; $previewButton.attr('title', Messages.previewButtonTitle);
var $bar = $('#cme_toolbox'); $previewButton.click(function () {
var $c = $contentContainer;
var isHistoryMode = false; if ($c.hasClass('cp-app-slide-preview')) {
framework._.sfCommon.setPadAttribute('previewMode', false, function (e) {
var setEditable = APP.setEditable = function (bool) { if (e) { return console.log(e); }
if (readOnly && bool) { return; }
editor.setOption('readOnly', !bool);
};
var Title;
var config = {
readOnly: readOnly,
transformFunction: JsonOT.validate,
// cryptpad debug logging (default is 1)
// logLevel: 0,
validateContent: function (content) {
try {
JSON.parse(content);
return true;
} catch (e) {
console.log("Failed to parse, rejecting patch");
return false;
}
}
};
var canonicalize = function (t) { return t.replace(/\r\n/g, '\n'); };
var setHistory = function (bool, update) {
isHistoryMode = bool;
setEditable(!bool);
if (!bool && update) {
config.onRemote();
}
};
var $contentContainer = $('#cp-app-slide-editor');
var $modal = $('#cp-app-slide-modal');
var $content = $('#cp-app-slide-modal-content');
var $print = $('#cp-app-slide-print');
var slideOptions = {};
var initialState = Messages.slideInitialState;
var textColor;
var backColor;
$content.click(function (e) {
if (!e.target) { return; }
var $t = $(e.target);
if ($t.is('a') || $t.parents('a').length) {
e.preventDefault();
var $a = $t.is('a') ? $t : $t.parents('a').first();
var href = $a.attr('href');
window.open(href);
}
}); });
$previewButton.removeClass('cp-toolbar-button-active');
Slide.setModal(common, $modal, $content, slideOptions, initialState); return void $c.removeClass('cp-app-slide-preview');
var enterPresentationMode = function (shouldLog) {
Slide.show(true, editor.getValue());
if (shouldLog) {
Cryptpad.log(Messages.presentSuccess);
} }
}; framework._.sfCommon.setPadAttribute('previewMode', true, function (e) {
if (e) { return console.log(e); }
});
$c.addClass('cp-app-slide-preview');
$previewButton.addClass('cp-toolbar-button-active');
Slide.updateFontSize();
});
framework._.toolbar.$rightside.append($previewButton);
if (isPresentMode) { framework._.sfCommon.getPadAttribute('previewMode', function (e, data) {
enterPresentationMode(true); if (e) { return void console.error(e); }
if (data !== false && $previewButton) {
$previewButton.click();
} }
});
CommonRealtime.onInfiniteSpinner(function () { setEditable(false); });
setEditable(false);
var initializing = true;
var stringifyInner = function (textValue) {
var obj = {
content: textValue,
metadata: metadataMgr.getMetadataLazy()
};
// stringify the json and send it into chainpad
return stringify(obj);
}; };
var onLocal = config.onLocal = function () { var mkPrintButton = function (framework, editor, $content, $print, $toolbarDrawer) {
if (initializing) { return; } var $printButton = $('<button>', {
if (isHistoryMode) { return; } title: Messages.printButtonTitle,
if (readOnly) { return; } 'class': 'cp-toolbar-rightside-button fa fa-print',
style: 'font-size: 17px'
editor.save(); }).click(function () {
Slide.update(editor.getValue(), true);
var textValue = canonicalize(CodeMirror.$textarea.val()); $print.html($content.html());
var shjson = stringifyInner(textValue); // TODO use translation key
Cryptpad.confirm("Are you sure you want to print?", function (yes) {
APP.patchText(shjson); if (yes) {
Slide.update(textValue); window.focus();
window.print();
if (APP.realtime.getUserDoc() !== shjson) {
console.error("realtime.getUserDoc() !== shjson");
} }
}, {ok: Messages.printButton});
framework.feedback('PRINT_SLIDES');
}).append($('<span>', {'class': 'cp-toolbar-drawer-element'}).text(Messages.printText));
$toolbarDrawer.append($printButton);
}; };
var mkSlideOptionsButton = function (framework, slideOptions, $toolbarDrawer) {
var metadataMgr = framework._.cpNfInner.metadataMgr;
var updateSlideOptions = function (newOpt) { var updateSlideOptions = function (newOpt) {
if (stringify(newOpt) !== stringify(slideOptions)) { if (JSONSortify(newOpt) !== JSONSortify(slideOptions)) {
$.extend(slideOptions, newOpt); $.extend(slideOptions, newOpt);
// TODO: manage realtime + cursor in the "options" modal ?? // TODO: manage realtime + cursor in the "options" modal ??
Slide.updateOptions(); Slide.updateOptions();
@ -202,38 +129,16 @@ define([
var metadata = JSON.parse(JSON.stringify(metadataMgr.getMetadata())); var metadata = JSON.parse(JSON.stringify(metadataMgr.getMetadata()));
metadata.slideOptions = slideOptions; metadata.slideOptions = slideOptions;
metadataMgr.updateMetadata(metadata); metadataMgr.updateMetadata(metadata);
onLocal(); framework.localChange();
}; };
var updateColors = function (text, back) { var createPrintDialog = function (invalidStyle) {
if (text) {
textColor = text;
$modal.css('color', text);
$modal.css('border-color', text);
$('#' + SLIDE_COLOR_ID).css('color', text);
}
if (back) {
backColor = back;
$modal.css('background-color', back);
$('#' + SLIDE_BACKCOLOR_ID).css('color', back);
}
};
var updateLocalColors = function (text, back) {
updateColors(text, back);
var metadata = JSON.parse(JSON.stringify(metadataMgr.getMetadata()));
if (backColor) { metadata.backColor = backColor; }
if (textColor) { metadata.color = textColor; }
metadataMgr.updateMetadata(metadata);
onLocal();
console.log(metadataMgr.getMetadata());
};
var createPrintDialog = function () {
var slideOptionsTmp = { var slideOptionsTmp = {
title: false, title: false,
slide: false, slide: false,
date: false, date: false,
transition: true, transition: true,
style: '' style: '',
styleLess: ''
}; };
$.extend(slideOptionsTmp, slideOptions); $.extend(slideOptionsTmp, slideOptions);
@ -299,14 +204,45 @@ define([
.on('keydown keyup', function (e) { .on('keydown keyup', function (e) {
e.stopPropagation(); e.stopPropagation();
}); });
$textarea.val(slideOptionsTmp.style); $textarea.val(invalidStyle || slideOptionsTmp.styleLess || slideOptionsTmp.style);
window.setTimeout(function () { $textarea.focus(); }, 0); window.setTimeout(function () { $textarea.focus(); }, 0);
var h; require(['/bower_components/less/dist/less.min.js'], function () { });
var parseLess = function (less, cb) {
require(['/bower_components/less/dist/less.min.js'], function (Less) {
Less.render(less, {}, function(err, css) {
if (err) { return void cb(err); }
cb(undefined, css.css);
}, window.less);
});
};
var h;
var todo = function () { var todo = function () {
slideOptionsTmp.style = $textarea.val(); if ($textarea.val() !== slideOptionsTmp.styleLess) {
var less = slideOptionsTmp.styleLess = $textarea.val();
slideOptionsTmp.style = '';
parseLess(mkLess(less), function (err, css) {
if (err) {
console.log(err);
Cryptpad.alert(
'<strong>' + Messages.slide_invalidLess + '</strong>' +
'<br>' +
'<pre class="cp-slide-css-error">' + Util.fixHTML(
'Line: ' + (err.line - 1) + '\n' +
err.extract[err.line - 1] + '\n' +
new Array(err.column+1).join(' ') +
'^--- ' + err.message
) + '</pre>'
/*Messages.slide_badLess*/, function () {
$('body').append(createPrintDialog(less));
}, true);
} else {
slideOptionsTmp.style = css;
updateLocalOptions(slideOptionsTmp); updateLocalOptions(slideOptionsTmp);
}
});
}
$container.remove(); $container.remove();
Cryptpad.stopListening(h); Cryptpad.stopListening(h);
}; };
@ -324,114 +260,6 @@ define([
return $container; return $container;
}; };
config.onInit = function (info) {
readOnly = metadataMgr.getPrivateData().readOnly;
var titleCfg = { getHeadingText: CodeMirror.getHeadingText };
Title = common.createTitle(titleCfg, config.onLocal);
Slide.setTitle(Title);
var configTb = {
displayed: ['title', 'useradmin', 'spinner', 'share', 'userlist', 'newpad', 'limit'],
title: Title.getTitleConfig(),
metadataMgr: metadataMgr,
readOnly: readOnly,
ifrw: window,
realtime: info.realtime,
common: Cryptpad,
sfCommon: common,
$container: $bar,
$contentContainer: $contentContainer
};
toolbar = APP.toolbar = Toolbar.create(configTb);
Title.setToolbar(toolbar);
CodeMirror.init(config.onLocal, Title, toolbar);
var $rightside = toolbar.$rightside;
var $drawer = toolbar.$drawer;
/* add a history button */
var histConfig = {
onLocal: config.onLocal,
onRemote: config.onRemote,
setHistory: setHistory,
applyVal: function (val) {
var remoteDoc = JSON.parse(val || '{}').content;
editor.setValue(remoteDoc || '');
editor.save();
},
$toolbar: $bar
};
var $hist = common.createButton('history', true, {histConfig: histConfig});
$drawer.append($hist);
/* save as template */
if (!metadataMgr.getPrivateData().isTemplate) {
var templateObj = {
rt: info.realtime,
getTitle: function () { return metadataMgr.getMetadata().title; }
};
var $templateButton = common.createButton('template', true, templateObj);
$rightside.append($templateButton);
}
/* add an export button */
var $export = common.createButton('export', true, {}, CodeMirror.exportText);
$drawer.append($export);
if (!readOnly) {
/* add an import button */
var $import = common.createButton('import', true, {}, CodeMirror.importText);
$drawer.append($import);
}
/* add a forget button */
var forgetCb = function (err) {
if (err) { return; }
setEditable(false);
};
var $forgetPad = common.createButton('forget', true, {}, forgetCb);
$rightside.append($forgetPad);
var $previewButton = APP.$previewButton = common.createButton(null, true);
$previewButton.removeClass('fa-question').addClass('fa-eye');
$previewButton.attr('title', Messages.previewButtonTitle);
$previewButton.click(function () {
var $c = $contentContainer;
if ($c.hasClass('cp-app-slide-preview')) {
common.setPadAttribute('previewMode', false, function (e) {
if (e) { return console.log(e); }
});
$previewButton.removeClass('cp-toolbar-button-active');
return void $c.removeClass('cp-app-slide-preview');
}
common.setPadAttribute('previewMode', true, function (e) {
if (e) { return console.log(e); }
});
$c.addClass('cp-app-slide-preview');
$previewButton.addClass('cp-toolbar-button-active');
Slide.updateFontSize();
});
$rightside.append($previewButton);
var $printButton = $('<button>', {
title: Messages.printButtonTitle,
'class': 'cp-toolbar-rightside-button fa fa-print',
style: 'font-size: 17px'
}).click(function () {
Slide.update(editor.getValue(), true);
$print.html($content.html());
// TODO use translation key
Cryptpad.confirm("Are you sure you want to print?", function (yes) {
if (yes) {
window.focus();
window.print();
}
}, {ok: Messages.printButton});
common.feedback('PRINT_SLIDES');
}).append($('<span>', {'class': 'cp-toolbar-drawer-element'}).text(Messages.printText));
$drawer.append($printButton);
var $slideOptions = $('<button>', { var $slideOptions = $('<button>', {
title: Messages.slideOptionsTitle, title: Messages.slideOptionsTitle,
'class': 'cp-toolbar-rightside-button fa fa-cog', 'class': 'cp-toolbar-rightside-button fa fa-cog',
@ -439,15 +267,44 @@ define([
}).click(function () { }).click(function () {
$('body').append(createPrintDialog()); $('body').append(createPrintDialog());
}).append($('<span>', {'class': 'cp-toolbar-drawer-element'}).text(Messages.slideOptionsText)); }).append($('<span>', {'class': 'cp-toolbar-drawer-element'}).text(Messages.slideOptionsText));
$drawer.append($slideOptions); $toolbarDrawer.append($slideOptions);
var $present = common.createButton('present', true) metadataMgr.onChange(function () {
.click(function () { var md = metadataMgr.getMetadata();
enterPresentationMode(true); if (md.slideOptions) {
updateLocalOptions(md.slideOptions);
}
}); });
$rightside.append($present); };
var mkColorConfiguration = function (framework, $modal) {
var textColor;
var backColor;
var metadataMgr = framework._.cpNfInner.metadataMgr;
var updateColors = function (text, back) {
if (text) {
textColor = text;
$modal.css('color', text);
$modal.css('border-color', text);
$('#' + SLIDE_COLOR_ID).css('color', text);
}
if (back) {
backColor = back;
$modal.css('background-color', back);
$('#' + SLIDE_BACKCOLOR_ID).css('color', back);
}
};
var updateLocalColors = function (text, back) {
updateColors(text, back);
var metadata = JSON.parse(JSON.stringify(metadataMgr.getMetadata()));
if (backColor) { metadata.backColor = backColor; }
if (textColor) { metadata.color = textColor; }
metadataMgr.updateMetadata(metadata);
framework.localChange();
console.log(metadataMgr.getMetadata());
};
var configureColors = function () {
var $back = $('<button>', { var $back = $('<button>', {
id: SLIDE_BACKCOLOR_ID, id: SLIDE_BACKCOLOR_ID,
'class': 'fa fa-square cp-toolbar-rightside-button', 'class': 'fa fa-square cp-toolbar-rightside-button',
@ -463,38 +320,36 @@ define([
var $testColor = $('<input>', { type: 'color', value: '!' }); var $testColor = $('<input>', { type: 'color', value: '!' });
var $check = $("#cp-app-slide-colorpicker"); var $check = $("#cp-app-slide-colorpicker");
if ($testColor.attr('type') !== "color" || $testColor.val() === '!') { return; } if ($testColor.attr('type') !== "color" || $testColor.val() === '!') { return; }
$back.on('click', function() {
var $picker = $('<input>', { type: 'color', value: backColor }) var $backgroundPicker = $('<input>', { type: 'color', value: backColor })
.css({ display: 'none', }) .css({ display: 'none', })
.on('change', function() { .on('change', function() { updateLocalColors(undefined, this.value); });
updateLocalColors(undefined, this.value); $check.append($backgroundPicker);
$check.html(''); $back.on('click', function() {
}); $backgroundPicker.val(backColor);
$check.append($picker); $backgroundPicker.click();
setTimeout(function() {
$picker.click();
}, 0);
}); });
$text.on('click', function() {
var $picker = $('<input>', { type: 'color', value: textColor }) var $foregroundPicker = $('<input>', { type: 'color', value: textColor })
.css({ display: 'none', }) .css({ display: 'none', })
.on('change', function() { .on('change', function() { updateLocalColors(this.value, undefined); });
updateLocalColors(this.value, undefined); $check.append($foregroundPicker);
$check.html(''); $text.on('click', function() {
}); $foregroundPicker.val(textColor);
$check.append($picker); $foregroundPicker.click();
setTimeout(function() {
$picker.click();
}, 0);
}); });
$rightside.append($back).append($text); framework._.toolbar.$rightside.append($back).append($text);
};
configureColors();
CodeMirror.configureTheme(); metadataMgr.onChange(function () {
var md = metadataMgr.getMetadata();
if (md.color || md.backColor) {
updateLocalColors(md.color, md.backColor);
}
});
};
if (!readOnly) { var mkFilePicker = function (framework, editor) {
var fileDialogCfg = { var fileDialogCfg = {
onSelect: function (data) { onSelect: function (data) {
if (data.type === 'file') { if (data.type === 'file') {
@ -504,8 +359,8 @@ define([
} }
} }
}; };
common.initFilePicker(fileDialogCfg); framework._.sfCommon.initFilePicker(fileDialogCfg);
APP.$mediaTagButton = $('<button>', { $('<button>', {
title: Messages.filePickerButton, title: Messages.filePickerButton,
'class': 'cp-toolbar-rightside-button fa fa-picture-o', 'class': 'cp-toolbar-rightside-button fa fa-picture-o',
style: 'font-size: 17px' style: 'font-size: 17px'
@ -514,80 +369,81 @@ define([
types: ['file'], types: ['file'],
where: ['root'] where: ['root']
}; };
common.openFilePicker(pickerCfg); framework._.sfCommon.openFilePicker(pickerCfg);
}).appendTo($rightside); }).appendTo(framework._.toolbar.$rightside);
var $tags = common.createButton('hashtag', true); var $tags = framework._.sfCommon.createButton('hashtag', true);
$rightside.append($tags); framework._.toolbar.$rightside.append($tags);
}
metadataMgr.onChange(function () {
var md = metadataMgr.getMetadata();
if (md.color || md.backColor) {
updateLocalColors(md.color, md.backColor);
}
if (md.slideOptions) {
updateLocalOptions(md.slideOptions);
}
});
}; };
config.onReady = function (info) { /////////////////////////////////////////////////////////////////////////////////////////////////////////
console.log('onready'); /////////////////////////////////////////////////////////////////////////////////////////////////////////
if (APP.realtime !== info.realtime) { /////////////////////////////////////////////////////////////////////////////////////////////////////////
var realtime = APP.realtime = info.realtime; /////////////////////////////////////////////////////////////////////////////////////////////////////////
APP.patchText = TextPatcher.create({
realtime: realtime,
//logging: true
});
}
var userDoc = APP.realtime.getUserDoc();
var isNew = false; var andThen2 = function (editor, CodeMirror, framework, isPresentMode) {
if (userDoc === "" || userDoc === "{}") { isNew = true; }
var newDoc = ""; var $contentContainer = $('#cp-app-slide-editor');
if (userDoc !== "") { var $modal = $('#cp-app-slide-modal');
var hjson = JSON.parse(userDoc); var $content = $('#cp-app-slide-modal-content');
var $print = $('#cp-app-slide-print');
var slideOptions = {};
if (hjson && hjson.metadata) { var $toolbarDrawer = framework._.toolbar.$drawer;
metadataMgr.updateMetadata(hjson.metadata);
} $content.click(function (e) {
if (typeof (hjson) !== 'object' || Array.isArray(hjson) || if (!e.target) { return; }
(hjson.metadata && typeof(hjson.metadata.type) !== 'undefined' && var $t = $(e.target);
hjson.metadata.type !== 'slide')) { if ($t.is('a') || $t.parents('a').length) {
var errorText = Messages.typeError; e.preventDefault();
Cryptpad.errorLoadingScreen(errorText); var $a = $t.is('a') ? $t : $t.parents('a').first();
throw new Error(errorText); var href = $a.attr('href');
window.open(href);
} }
});
newDoc = hjson.content; Slide.setModal(framework._.sfCommon, $modal, $content, slideOptions, Messages.slideInitialState);
mkPrintButton(framework, editor, $content, $print, $toolbarDrawer);
mkSlideOptionsButton(framework, slideOptions, $toolbarDrawer);
mkColorConfiguration(framework, $modal);
mkFilePicker(framework, editor);
mkSlidePreviewPane(framework, $contentContainer);
if (hjson.highlightMode) { CodeMirror.configureTheme();
CodeMirror.setMode(hjson.highlightMode);
}
} else {
Title.updateTitle(Cryptpad.initialName || Title.defaultTitle);
}
if (!CodeMirror.highlightMode) { var enterPresentationMode = function (shouldLog) {
CodeMirror.setMode('markdown'); Slide.show(true, editor.getValue());
if (shouldLog) {
Cryptpad.log(Messages.presentSuccess);
} }
};
editor.setValue(newDoc || initialState); if (isPresentMode) { enterPresentationMode(true); }
if (Cryptpad.initialName && Title.isDefaultTitle()) { framework.onContentUpdate(function (newContent) {
Title.updateTitle(Cryptpad.initialName); CodeMirror.contentUpdate(newContent);
} Slide.update(newContent.content);
});
common.getPadAttribute('previewMode', function (e, data) { framework.setContentGetter(function () {
if (e) { return void console.error(e); } console.log(framework._.title.title);
if (data !== false && APP.$previewButton) { var content = CodeMirror.getContent();
APP.$previewButton.click(); Slide.update(content.content);
} return content;
}); });
framework.onEditableChange(function () {
editor.setOption('readOnly', framework.isLocked() || framework.isReadOnly());
});
framework.setTitleRecommender(function () {
console.log('titleRecommender');
return CodeMirror.getHeadingText();
});
framework.onReady(function (/*newPad*/) {
CodeMirror.setMode('markdown', function () { });
Slide.onChange(function (o, n, l) { Slide.onChange(function (o, n, l) {
var slideNumber = ''; var slideNumber = '';
if (n !== null) { if (n !== null) {
@ -595,153 +451,58 @@ define([
slideNumber = ' (' + (++n) + '/' + l + ')'; slideNumber = ' (' + (++n) + '/' + l + ')';
} }
} }
common.setTabTitle('{title}' + slideNumber); framework._.sfCommon.setTabTitle('{title}' + slideNumber);
}); });
setEditable(!readOnly);
initializing = false;
onLocal(); // push local state to avoid parse errors later.
Slide.update(editor.getValue()); Slide.update(editor.getValue());
Cryptpad.removeLoadingScreen(); });
if (readOnly) {
config.onRemote();
return;
}
if (isNew) {
common.openTemplatePicker();
}
var fmConfig = {
dropArea: $('.CodeMirror'),
body: $('body'),
onUploaded: function (ev, data) {
var parsed = Cryptpad.parsePadUrl(data.url);
var hexFileName = Cryptpad.base64ToHex(parsed.hashData.channel);
var src = '/blob/' + hexFileName.slice(0,2) + '/' + hexFileName;
var mt = '<media-tag src="' + src + '" data-crypto-key="cryptpad:' + parsed.hashData.key + '"></media-tag>';
editor.replaceSelection(mt);
}
};
APP.FM = common.createFileManager(fmConfig);
};
config.onRemote = function () {
if (initializing) { return; }
if (isHistoryMode) { return; }
var oldDoc = canonicalize(CodeMirror.$textarea.val());
var shjson = APP.realtime.getUserDoc();
var hjson = JSON.parse(shjson);
var remoteDoc = hjson.content;
if (hjson.metadata) {
metadataMgr.updateMetadata(hjson.metadata);
}
var highlightMode = hjson.highlightMode;
if (highlightMode && highlightMode !== APP.highlightMode) {
CodeMirror.setMode(highlightMode);
}
CodeMirror.setValueAndCursor(oldDoc, remoteDoc, TextPatcher);
if (!readOnly) {
var textValue = canonicalize(CodeMirror.$textarea.val());
var shjson2 = stringifyInner(textValue);
if (shjson2 !== shjson) {
console.error("shjson2 !== shjson");
TextPatcher.log(shjson, TextPatcher.diff(shjson, shjson2));
APP.patchText(shjson2);
}
}
Slide.update(remoteDoc);
if (oldDoc !== remoteDoc) { common.notify(); }
};
config.onAbort = function () {
// inform of network disconnect
setEditable(false);
toolbar.failed();
Cryptpad.alert(Messages.common_connectionLost, undefined, true);
};
config.onConnectionChange = function (info) {
setEditable(info.state);
//toolbar.failed();
if (info.state) {
initializing = true;
//toolbar.reconnecting(info.myId);
Cryptpad.findOKButton().click();
} else {
Cryptpad.alert(Messages.common_connectionLost, undefined, true);
}
};
config.onError = onConnectError; framework.onDefaultContentNeeded(function () {
CodeMirror.contentUpdate({ content: Messages.slideInitialState });
});
cpNfInner = common.startRealtime(config); Slide.setTitle(framework._.title);
metadataMgr = cpNfInner.metadataMgr;
cpNfInner.onInfiniteSpinner(function () { framework._.toolbar.$rightside.append(
setEditable(false); framework._.sfCommon.createButton('present', true).click(function () {
Cryptpad.confirm(Messages.realtime_unrecoverableError, function (yes) { enterPresentationMode(true);
if (!yes) { return; } })
common.gotoURL(); );
});
});
editor.on('change', onLocal); editor.on('change', framework.localChange);
Cryptpad.onLogout(function () { setEditable(false); }); framework.start();
}; };
var CMEDITOR_CHECK_INTERVAL = 100;
var cmEditorAvailable = function (cb) {
var intr;
var check = function () {
if (window.CodeMirror) {
clearTimeout(intr);
cb(window.CodeMirror);
}
};
intr = setInterval(function () {
console.log("CodeMirror was not defined. Trying again in %sms", CMEDITOR_CHECK_INTERVAL);
check();
}, CMEDITOR_CHECK_INTERVAL);
check();
};
var main = function () { var main = function () {
var CM;
var CodeMirror; var CodeMirror;
var editor; var editor;
var common; var common;
var framework;
nThen(function (waitFor) { nThen(function (waitFor) {
cmEditorAvailable(waitFor(function (cm) {
CM = cm; Framework.create({
})); toolbarContainer: '#cme_toolbox',
$(waitFor(function () { contentContainer: '#cp-app-slide-editor'
Cryptpad.addLoadingScreen(); }, waitFor(function (fw) { framework = fw; }));
}));
SFCommon.create(waitFor(function (c) { APP.common = common = c; })); nThen(function (waitFor) {
}).nThen(function (/*waitFor*/) { $(waitFor());
CodeMirror = common.initCodeMirrorApp(null, CM); // TODO(cjd): This is crap but we cannot bring up codemirror until after
// the CryptPad Common is up and we can't bring up framework
// without codemirror.
SFCommon.create(waitFor(function (c) { common = c; }));
}).nThen(function () {
CodeMirror = common.initCodeMirrorApp(null, CMeditor);
$('.CodeMirror').addClass('fullPage'); $('.CodeMirror').addClass('fullPage');
editor = CodeMirror.editor; editor = CodeMirror.editor;
Cryptpad.onError(function (info) { }).nThen(waitFor());
if (info && info.type === "store") {
onConnectError(); }).nThen(function (waitFor) {
} common.getSframeChannel().onReady(waitFor());
}); }).nThen(function (/*waitFor*/) {
common.getSframeChannel().onReady(function () {
common.isPresentUrl(function (err, val) { common.isPresentUrl(function (err, val) {
isPresentMode = val; andThen2(editor, CodeMirror, framework, val);
andThen(editor, CodeMirror, common);
});
}); });
}); });
}; };

@ -1,41 +0,0 @@
// Load #1, load as little as possible because we are in a race to get the loading screen up.
define([
'/bower_components/nthen/index.js',
'/api/config',
'jquery',
'/common/requireconfig.js',
'/common/sframe-common-outer.js'
], function (nThen, ApiConfig, $, RequireConfig, SFCommonO) {
var requireConfig = RequireConfig();
// Loaded in load #2
nThen(function (waitFor) {
$(waitFor());
}).nThen(function (waitFor) {
var req = {
cfg: requireConfig,
req: [ '/common/loading.js' ],
pfx: window.location.origin
};
window.rc = requireConfig;
window.apiconf = ApiConfig;
$('#sbox-iframe').attr('src',
ApiConfig.httpSafeOrigin + '/slide/inner.html?' + requireConfig.urlArgs +
'#' + encodeURIComponent(JSON.stringify(req)));
// This is a cheap trick to avoid loading sframe-channel in parallel with the
// loading screen setup.
var done = waitFor();
var onMsg = function (msg) {
var data = JSON.parse(msg.data);
if (data.q !== 'READY') { return; }
window.removeEventListener('message', onMsg);
var _done = done;
done = function () { };
_done();
};
window.addEventListener('message', onMsg);
}).nThen(function (/*waitFor*/) {
SFCommonO.start();
});
});

@ -58,12 +58,6 @@ define([
// $print.css('font-size', (20*9/16)+'vw'); // $print.css('font-size', (20*9/16)+'vw');
}; };
var fixCSS = function (css) {
var append = '.cp #cp-app-slide-print .cp-app-slide-frame ';
var append2 = '.cp div#cp-app-slide-modal #cp-app-slide-modal-content .cp-app-slide-frame ';
return css.replace(/(\n*)([^\n}]+)\s*\{/g, '$1' + append + '$2,' + append2 + '$2 {');
};
var goTo = Slide.goTo = function (i) { var goTo = Slide.goTo = function (i) {
i = i || 0; i = i || 0;
Slide.index = i; Slide.index = i;
@ -87,8 +81,8 @@ define([
var length = getNumberOfSlides(); var length = getNumberOfSlides();
$modal.find('style.cp-app-slide-style').remove(); $modal.find('style.cp-app-slide-style').remove();
if (options.style && Slide.shown) { if (options.style) {
$modal.prepend($('<style>', {'class': 'cp-app-slide-style'}).text(fixCSS(options.style))); $modal.prepend($('<style>', {'class': 'cp-app-slide-style'}).text(options.style));
} }
$content.find('.cp-app-slide-frame').each(function (i, el) { $content.find('.cp-app-slide-frame').each(function (i, el) {
if (options.slide) { if (options.slide) {

Loading…
Cancel
Save