Add support for markdown syntax in contacts

pull/1/head
yflory 8 years ago
parent cba113a2b0
commit e2d031c76a

@ -1,8 +1,9 @@
define([ define([
'jquery', 'jquery',
'/bower_components/chainpad-crypto/crypto.js', '/bower_components/chainpad-crypto/crypto.js',
'/common/curve.js' '/common/curve.js',
], function ($, Crypto, Curve) { '/bower_components/marked/marked.min.js',
], function ($, Crypto, Curve, Marked) {
var Msg = {}; var Msg = {};
var Types = { var Types = {
@ -19,6 +20,10 @@ define([
var ready = []; var ready = [];
var pending = {}; var pending = {};
var parseMessage = function (content) {
return Marked(content);
};
var createData = Msg.createData = function (common, hash) { var createData = Msg.createData = function (common, hash) {
var proxy = common.getProxy(); var proxy = common.getProxy();
return { return {
@ -199,6 +204,7 @@ define([
var onChannelReady = function (common, chanId) { var onChannelReady = function (common, chanId) {
if (ready.indexOf(chanId) !== -1) { return; } if (ready.indexOf(chanId) !== -1) { return; }
ready.push(chanId); ready.push(chanId);
channels[chanId].updateStatus();
var friends = getFriendList(common); var friends = getFriendList(common);
if (ready.length === Object.keys(friends).length) { if (ready.length === Object.keys(friends).length) {
// All channels are ready // All channels are ready
@ -220,8 +226,9 @@ define([
if (!isId) { return; } if (!isId) { return; }
var decryptedMsg = channel.encryptor.decrypt(msg); var decryptedMsg = channel.encryptor.decrypt(msg);
console.log(decryptedMsg);
var parsed = JSON.parse(decryptedMsg); var parsed = JSON.parse(decryptedMsg);
if (parsed[0] !== Types.mapId && parsed[0] !== Types.mapidAck) { return; } if (parsed[0] !== Types.mapId && parsed[0] !== Types.mapIdAck) { return; }
if (parsed[2] !== sender || !parsed[1]) { return; } if (parsed[2] !== sender || !parsed[1]) { return; }
channel.mapId[sender] = parsed[1]; channel.mapId[sender] = parsed[1];
@ -300,7 +307,9 @@ define([
// Input // Input
var channel = channels[data.channel]; var channel = channels[data.channel];
var $input = $('<input>', {type: 'text'}).appendTo($inputBlock); var $input = $('<textarea>').appendTo($inputBlock);
$input.attr('placeholder', common.Messages.contacts_typeHere);
var sending = false; var sending = false;
var send = function () { var send = function () {
if (sending) { return; } if (sending) { return; }
@ -320,10 +329,34 @@ define([
console.error(err); console.error(err);
}); });
}; };
$('<button>').text(common.Messages.contacts_send).appendTo($inputBlock).click(send); $('<button>', {'class': 'btn btn-primary fa fa-paper-plane'})
$input.on('keypress', function (e) { //.text(common.Messages.contacts_send)
.appendTo($inputBlock).click(send);
/*$input.on('keypress', function (e) {
if (e.which === 13) { send(); } if (e.which === 13) { send(); }
}); });*/
var onKeyDown = function (e) {
if (e.keyCode == 13) {
if (e.ctrlKey) {
var val = this.value;
if (typeof this.selectionStart == "number" && typeof this.selectionEnd == "number") {
var start = this.selectionStart;
this.value = val.slice(0, start) + "\n" + val.slice(this.selectionEnd);
this.selectionStart = this.selectionEnd = start + 1;
} else if (document.selection && document.selection.createRange) {
this.focus();
var range = document.selection.createRange();
range.text = "\r\n";
range.collapse(false);
range.select();
}
return false;
}
send();
return false;
}
};
$input.on('keydown', onKeyDown);
// Header // Header
var $rightCol = $('<span>', {'class': 'right-col'}); var $rightCol = $('<span>', {'class': 'right-col'});
@ -386,7 +419,8 @@ define([
channel.lastSender = msg[0]; channel.lastSender = msg[0];
// content // content
$('<div>', {'class':'content'}).text(msg[2]).appendTo($msg); //$('<div>', {'class':'content'}).text(msg[2]).appendTo($msg);
$('<div>', {'class':'content'}).html(parseMessage(msg[2])).appendTo($msg);
} }
$messages.scrollTop($messages[0].scrollHeight); $messages.scrollTop($messages[0].scrollHeight);
channel.lastDisplayed = i-1; channel.lastDisplayed = i-1;
@ -540,7 +574,11 @@ define([
network.sendto(peer, cryptMsg); network.sendto(peer, cryptMsg);
channel.updateStatus(); channel.updateStatus();
}; };
chan.members.forEach(onJoining); chan.members.forEach(function (peer) {
if (peer === Msg.hk) { return; }
if (channel.userList.indexOf(peer) !== -1) { return; }
channel.userList.push(peer);
});
chan.on('join', onJoining); chan.on('join', onJoining);
chan.on('leave', function (peer) { chan.on('leave', function (peer) {
var i = channel.userList.indexOf(peer); var i = channel.userList.indexOf(peer);

Loading…
Cancel
Save