You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
4.1 KiB
JavaScript
140 lines
4.1 KiB
JavaScript
9 years ago
|
require.config({ paths: {
|
||
|
'json.sortify': '/bower_components/json.sortify/dist/JSON.sortify'
|
||
|
}});
|
||
|
|
||
|
define([
|
||
|
'/api/config?cb=' + Math.random().toString(16).substring(2),
|
||
9 years ago
|
'/bower_components/chainpad-netflux/chainpad-netflux.js',
|
||
|
'/bower_components/chainpad-crypto/crypto.js',
|
||
9 years ago
|
'/bower_components/textpatcher/TextPatcher.amd.js',
|
||
9 years ago
|
'json.sortify',
|
||
9 years ago
|
'/bower_components/chainpad-json-validator/json-ot.js',
|
||
9 years ago
|
'/common/cryptpad-common.js',
|
||
9 years ago
|
'/bower_components/fabric.js/dist/fabric.min.js',
|
||
9 years ago
|
'/bower_components/jquery/dist/jquery.min.js',
|
||
9 years ago
|
'/bower_components/file-saver/FileSaver.min.js',
|
||
8 years ago
|
//'/customize/pad.js'
|
||
8 years ago
|
], function (Config, Realtime, Crypto, TextPatcher, JSONSortify, JsonOT, Cryptpad) {
|
||
9 years ago
|
var saveAs = window.saveAs;
|
||
|
|
||
9 years ago
|
var module = window.APP = { };
|
||
|
var $ = module.$ = window.jQuery;
|
||
9 years ago
|
var Fabric = module.Fabric = window.fabric;
|
||
9 years ago
|
|
||
9 years ago
|
var secret = Cryptpad.getSecrets();
|
||
|
/*
|
||
9 years ago
|
var key;
|
||
|
var channel = '';
|
||
|
if (!/#/.test(window.location.href)) {
|
||
|
key = Crypto.genKey();
|
||
|
} else {
|
||
|
var hash = window.location.hash.slice(1);
|
||
|
channel = hash.slice(0, 32);
|
||
|
key = hash.slice(32);
|
||
9 years ago
|
}*/
|
||
9 years ago
|
|
||
|
/* Initialize Fabric */
|
||
|
var canvas = module.canvas = new Fabric.Canvas('canvas');
|
||
|
var $canvas = $('canvas');
|
||
|
|
||
|
var $width = $('#width');
|
||
|
var updateBrushWidth = function () {
|
||
|
canvas.freeDrawingBrush.width = Number($width.val());
|
||
|
};
|
||
|
updateBrushWidth();
|
||
|
|
||
|
$width.on('change', updateBrushWidth);
|
||
|
|
||
9 years ago
|
var palette = ['red', 'blue', 'green', 'white', 'black', 'purple',
|
||
|
'gray', 'beige', 'brown', 'cyan', 'darkcyan', 'gold', 'yellow', 'pink'];
|
||
9 years ago
|
var $colors = $('#colors');
|
||
|
$colors.html(function (i, val) {
|
||
|
return palette.map(function (c) {
|
||
|
return "<span class='palette' style='background-color:"+c+"'></span>";
|
||
|
}).join("");
|
||
|
});
|
||
|
|
||
|
$('.palette').on('click', function () {
|
||
|
var color = $(this).css('background-color');
|
||
|
canvas.freeDrawingBrush.color = color;
|
||
|
});
|
||
|
|
||
|
var setEditable = function (bool) {
|
||
|
canvas.isDrawingMode = bool;
|
||
|
$canvas.css('border-color', bool? 'black': 'red');
|
||
|
};
|
||
|
|
||
9 years ago
|
var saveImage = module.saveImage = function () {
|
||
|
$canvas[0].toBlob(function (blob) {
|
||
|
var defaultName = "pretty-picture.png";
|
||
|
saveAs(blob, window.prompt("What would you like to name your image?",
|
||
|
defaultName) || "pretty-picture.png");
|
||
|
});
|
||
|
};
|
||
|
|
||
9 years ago
|
var initializing = true;
|
||
|
|
||
|
var config = module.config = {
|
||
9 years ago
|
initialState: '{}',
|
||
9 years ago
|
// TODO initialState ?
|
||
|
websocketURL: Config.websocketURL,
|
||
9 years ago
|
//userName: Crypto.rand64(8),
|
||
9 years ago
|
channel: secret.channel,
|
||
|
//cryptKey: key,
|
||
|
crypto: Crypto.createEncryptor(secret.key),
|
||
9 years ago
|
transformFunction: JsonOT.validate,
|
||
9 years ago
|
};
|
||
|
|
||
9 years ago
|
var onInit = config.onInit = function (info) {
|
||
9 years ago
|
window.location.hash = info.channel + secret.key;
|
||
9 years ago
|
$(window).on('hashchange', function() {
|
||
|
window.location.reload();
|
||
|
});
|
||
|
};
|
||
9 years ago
|
|
||
|
var onRemote = config.onRemote = function () {
|
||
|
if (initializing) { return; }
|
||
|
var userDoc = module.realtime.getUserDoc();
|
||
|
canvas.loadFromJSON(userDoc);
|
||
|
canvas.renderAll();
|
||
|
};
|
||
|
|
||
|
var onLocal = config.onLocal = function () {
|
||
|
if (initializing) { return; }
|
||
|
var content = JSONSortify(canvas.toDatalessJSON());
|
||
|
module.patchText(content);
|
||
|
};
|
||
|
|
||
|
var onReady = config.onReady = function (info) {
|
||
|
var realtime = module.realtime = info.realtime;
|
||
|
module.patchText = TextPatcher.create({
|
||
|
realtime: realtime
|
||
|
});
|
||
|
|
||
|
setEditable(true);
|
||
|
initializing = false;
|
||
|
onRemote();
|
||
|
};
|
||
|
|
||
|
var onAbort = config.onAbort = function (info) {
|
||
|
setEditable(false);
|
||
|
window.alert("Server Connection Lost");
|
||
9 years ago
|
|
||
|
if (window.confirm("Would you like to save your image?")) {
|
||
|
saveImage();
|
||
|
}
|
||
9 years ago
|
};
|
||
|
|
||
|
var rt = Realtime.start(config);
|
||
|
|
||
|
canvas.on('mouse:up', onLocal);
|
||
|
|
||
|
$('#clear').on('click', function () {
|
||
|
canvas.clear();
|
||
|
});
|
||
|
|
||
9 years ago
|
$('#save').on('click', function () {
|
||
|
saveImage();
|
||
|
});
|
||
9 years ago
|
});
|