From 3c8e69899bcd403ed82bee2599bdd592150e5956 Mon Sep 17 00:00:00 2001 From: cel Date: Tue, 9 Jan 2018 19:04:39 -1000 Subject: Render line-comments and diffs better --- lib/render-msg.js | 2 +- lib/serve.js | 145 +++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 106 insertions(+), 41 deletions(-) diff --git a/lib/render-msg.js b/lib/render-msg.js index 78c7d5c..1603d24 100644 --- a/lib/render-msg.js +++ b/lib/render-msg.js @@ -1670,7 +1670,7 @@ RenderMsg.prototype.lineComment = function (cb) { }, String(self.c.commitId).substr(0, 8)), ' ', h('a', { href: self.toUrl('/git/line-comment/' + - encodeURIComponent(self.msg.key)) + encodeURIComponent(self.msg.key || JSON.stringify(self.msg))) }, h('code', self.c.filePath + ':' + self.c.line)) )), self.c.text ? diff --git a/lib/serve.js b/lib/serve.js index 6c8941c..6e3b451 100644 --- a/lib/serve.js +++ b/lib/serve.js @@ -1790,7 +1790,7 @@ Serve.prototype.gitCommit = function (rev) { : file.created ? ph('a', {href: self.app.render.toUrl('/git/blob/' - + file.hash[0] + + (file.hash[1] || file.hash[0]) + '?msg=' + encodeURIComponent(obj.msg.key)) }, 'created') : file.hash ? @@ -1798,6 +1798,8 @@ Serve.prototype.gitCommit = function (rev) { self.app.render.toUrl('/git/diff/' + file.hash[0] + '..' + file.hash[1] + '?msg=' + encodeURIComponent(obj.msg.key)) + + '&commit=' + rev + + '&path=' + encodeURIComponent(file.name) }, 'changed') : file.mode ? 'mode changed' : JSON.stringify(file)) @@ -2107,7 +2109,13 @@ Serve.prototype.gitDiff = function (revs) { obj: rev2, msg: msg2.key, }, done()) - done(function (err, obj1, obj2) { + /* + self.app.git.guessCommitAndPath({ + obj: rev2, + msg: msg2.key, + }, done()) + */ + done(function (err, obj1, obj2/*, info2*/) { if (err && err.name === 'BlobNotFoundError') return cb(null, self.askWantBlobsForm(err.links)) if (err) return cb(err) @@ -2124,6 +2132,8 @@ Serve.prototype.gitDiff = function (revs) { cb(null, self.gitDiffTable(diff, lineComments, { obj: obj2, hash: rev2, + commit: self.query.commit, // info2.commit, + path: self.query.path, // info2.path, })) }) }) @@ -2136,6 +2146,7 @@ Serve.prototype.gitDiff = function (revs) { } Serve.prototype.gitDiffTable = function (diff, lineComments, lineCommentInfo) { + var updateMsg = lineCommentInfo.obj.msg var self = this return pull( ph('table', [ @@ -2146,7 +2157,7 @@ Serve.prototype.gitDiffTable = function (diff, lineComments, lineCommentInfo) { var newLine = hunk.newStart return [ ph('tr', [ - ph('td', {colspan: 2}), + ph('td', {colspan: 3}), ph('td', ph('pre', '@@ -' + oldLine + ',' + hunk.oldLines + ' ' + '+' + newLine + ',' + hunk.newLines + ' @@')) @@ -2158,27 +2169,34 @@ Serve.prototype.gitDiffTable = function (diff, lineComments, lineCommentInfo) { if (s == '\\') return var html = self.app.render.highlight(line) var lineNums = [s == '+' ? '' : oldLine++, s == '-' ? '' : newLine++] - // var id = [filename].concat(lineNums).join('-') + var hash = lineCommentInfo.hash var newLineNum = lineNums[lineNums.length-1] + var id = hash + '-' + (newLineNum || (lineNums[0] + '-')) + var idEnc = encodeURIComponent(id) + var allowComment = s !== '-' + && self.query.commit && self.query.path return [ ph('tr', { class: s == '+' ? 'diff-new' : s == '-' ? 'diff-old' : '' }, [ lineNums.map(function (num, i) { - return ph('td', String(num)) - // TODO: allow linking to comments - /* - var idEnc = encodeURIComponent(id) - return '' + - (num ? '' + - num + '' + - (updateId && i === lineNums.length-1 && s !== '-' ? - ' ' - : '') - : '') + '' - } - */ + return ph('td', [ + ph('a', { + name: i === 0 ? idEnc : undefined, + href: '#' + idEnc + }, String(num)) + ]) }), + ph('td', + allowComment ? ph('a', { + href: '?msg=' + + encodeURIComponent(self.query.msg) + + '&comment=' + idEnc + + '&commit=' + encodeURIComponent(self.query.commit) + + '&path=' + encodeURIComponent(self.query.path) + + '#' + idEnc + }, '…') : '' + ), ph('td', ph('pre', u.escapeHTML(html))) ]), (lineComments[newLineNum] ? @@ -2187,14 +2205,20 @@ Serve.prototype.gitDiffTable = function (diff, lineComments, lineCommentInfo) { self.renderLineCommentThread(lineComments[newLineNum]) ) ) - : /*commit && query.comment === id ? + : newLineNum && lineCommentInfo && self.query.comment === id ? ph('tr', ph('td', {colspan: 4}, - // self.renderLineCommentForm(req, repo, updateId, commit, filename, newLineNum) - self.renderLineCommentForm(lineCommentInfo) + self.renderLineCommentForm({ + id: id, + line: newLineNum, + updateId: updateMsg.key, + repoId: updateMsg.value.content.repo, + commitId: lineCommentInfo.commit, + filePath: lineCommentInfo.path, + }) ) ) - :*/ '') + : '') ] }) ) @@ -2213,16 +2237,42 @@ Serve.prototype.renderLineCommentThread = function (lineComment) { )) } -Serve.prototype.renderLineCommentForm = function (info) { - var obj = info.obj - var hash = info.hash - return ph('pre', JSON.stringify(info, 0, 2)) +Serve.prototype.renderLineCommentForm = function (opts) { + return [ + this.phComposer({ + placeholder: 'comment on this line', + id: opts.id, + lineComment: opts + }) + ] +} + +// return a composer, pull-hyperscript style +Serve.prototype.phComposer = function (opts) { + var self = this + return u.readNext(function (cb) { + self.composer(opts, function (err, composer) { + if (err) return cb(err) + cb(null, pull.once(composer.outerHTML)) + }) + }) } Serve.prototype.gitLineComment = function (path) { var self = this - var id = decodeURIComponent(String(path)) - self.getMsgDecryptedMaybeOoo(id, function (err, msg) { + var id + try { + id = decodeURIComponent(String(path)) + if (id[0] === '%') { + return self.getMsgDecryptedMaybeOoo(id, gotMsg) + } else { + msg = JSON.parse(id) + } + } catch(e) { + return gotMsg(e) + } + gotMsg(null, msg) + function gotMsg(err, msg) { if (err) return pull( pull.once(u.renderError(err).outerHTML), self.respondSink(400, {'Content-Type': ctype('html')}) @@ -2232,28 +2282,35 @@ Serve.prototype.gitLineComment = function (path) { pull.once('Missing message ' + id), self.respondSink(500, {'Content-Type': ctype('html')}) ) - self.app.git.getObjectAtPath({ + self.app.git.diffFile({ msg: c.updateId, - obj: c.commitId, + commit: c.commitId, path: c.filePath, - }, function (err, obj) { + }, function (err, file) { if (err && err.name === 'BlobNotFoundError') return self.askWantBlobs(err.links) if (err) return pull( pull.once(err.stack), self.respondSink(400, {'Content-Type': 'text/plain'}) ) - - self.redirect(self.app.render.toUrl(publishedMsg.key)) - - /* - pull( - ph('pre', JSON.stringify(obj, 0, 2)), - self.respondSink(200) - ) - */ + var path + if (file.created) { + path = '/git/blob/' + file.hash[1] + + '?msg=' + encodeURIComponent(c.updateId) + + '&commit=' + c.commitId + + '&path=' + encodeURIComponent(c.filePath) + + '#' + file.hash[1] + '-' + c.line + } else { + path = '/git/diff/' + file.hash[0] + '..' + file.hash[1] + + '?msg=' + encodeURIComponent(c.updateId) + + '&commit=' + c.commitId + + '&path=' + encodeURIComponent(c.filePath) + + '#' + file.hash[1] + '-' + c.line + } + var url = self.app.render.toUrl(path) + self.redirect(url) }) - }) + } } Serve.prototype.gitObjectLinks = function (headMsgId, type) { @@ -2958,6 +3015,14 @@ Serve.prototype.composer = function (opts, cb) { type: 'post', text: String(data.text).replace(/\r\n/g, '\n'), } + if (opts.lineComment) { + content.type = 'line-comment' + content.updateId = opts.lineComment.updateId + content.repo = opts.lineComment.repoId + content.commitId = opts.lineComment.commitId + content.filePath = opts.lineComment.filePath + content.line = opts.lineComment.line + } var mentions = ssbMentions(data.text, {bareFeedNames: true, emoji: true}) .filter(function (mention) { if (mention.emoji) { -- cgit v1.2.3