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.
151 lines
4.4 KiB
JavaScript
151 lines
4.4 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',
|
||
8 years ago
|
'fabric.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";
|
||
8 years ago
|
// TODO make this translatable
|
||
9 years ago
|
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: '{}',
|
||
8 years ago
|
websocketURL: Cryptpad.getWebsocketURL(),
|
||
|
validateKey: secret.keys.validateKey,
|
||
8 years ago
|
readOnly: false, // TODO, support read-only
|
||
9 years ago
|
channel: secret.channel,
|
||
8 years ago
|
crypto: Crypto.createEncryptor(secret.keys),
|
||
8 years ago
|
transformFunction: JsonOT.transform,
|
||
9 years ago
|
};
|
||
|
|
||
8 years ago
|
var editHash;
|
||
9 years ago
|
var onInit = config.onInit = function (info) {
|
||
8 years ago
|
editHash = Cryptpad.getEditHashFromKeys(info.channel, secret.keys);
|
||
|
Cryptpad.replaceHash(editHash);
|
||
|
};
|
||
|
|
||
8 years ago
|
// used for debugging, feel free to remove
|
||
8 years ago
|
var Catch = function (f) {
|
||
|
return function () {
|
||
|
try {
|
||
|
f();
|
||
|
} catch (e) {
|
||
|
console.error(e);
|
||
|
}
|
||
|
};
|
||
9 years ago
|
};
|
||
9 years ago
|
|
||
8 years ago
|
var onRemote = config.onRemote = Catch(function () {
|
||
9 years ago
|
if (initializing) { return; }
|
||
|
var userDoc = module.realtime.getUserDoc();
|
||
8 years ago
|
|
||
9 years ago
|
canvas.loadFromJSON(userDoc);
|
||
|
canvas.renderAll();
|
||
8 years ago
|
});
|
||
9 years ago
|
|
||
8 years ago
|
var onLocal = config.onLocal = Catch(function () {
|
||
9 years ago
|
if (initializing) { return; }
|
||
|
var content = JSONSortify(canvas.toDatalessJSON());
|
||
|
module.patchText(content);
|
||
8 years ago
|
});
|
||
9 years ago
|
|
||
|
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
|
});
|