git ssb

30+

cel / git-ssb-web



Commit 5c44d260494ea080f8dc3bccd2392b5cf87db845

Render images in diffs

Charles Lehner committed on 4/12/2016, 6:22:06 PM
Parent: 5cf09d72665845ad57b6ae7fb0fcfed9b9c180d6

Files changed

index.jschanged
static/styles.csschanged
index.jsView
@@ -1463,9 +1463,9 @@
14631463 return cat([
14641464 pull(
14651465 Repo.diffTrees(repos, treeIds, true),
14661466 pull.map(function (item) {
1467- var filename = item.filename = escapeHTML(item.path.join('/'))
1467+ var filename = escapeHTML(item.filename = item.path.join('/'))
14681468 var oldId = item.id && item.id[0]
14691469 var newId = item.id && item.id[lastI]
14701470 var oldMode = item.mode && item.mode[0]
14711471 var newMode = item.mode && item.mode[lastI]
@@ -1477,34 +1477,53 @@
14771477 ' to ' + newMode.toString(8) :
14781478 'changed'
14791479 if (item.id)
14801480 changedFiles.push(item)
1481+ var commitId = item.id[lastI] ? id : treeIds.filter(Boolean)[0]
1482+ var blobsPath = treeIds[1]
1483+ ? [repos[1].id, 'blob', treeIds[1]]
1484+ : [repos[0].id, 'blob', treeIds[0]]
1485+ var rawsPath = treeIds[1]
1486+ ? [repos[1].id, 'raw', treeIds[1]]
1487+ : [repos[0].id, 'raw', treeIds[0]]
1488+ item.blobPath = blobsPath.concat(item.path)
1489+ item.rawPath = rawsPath.concat(item.path)
14811490 var fileHref = item.id ?
14821491 '#' + encodeURIComponent(item.path.join('/')) :
1483- encodeLink([repos[0].id, 'blob', id].concat(item.path))
1492+ encodeLink(item.blobPath)
14841493 return ['<a href="' + fileHref + '">' + filename + '</a>', action]
14851494 }),
14861495 table()
14871496 ),
14881497 pull(
14891498 pull.values(changedFiles),
14901499 paramap(function (item, cb) {
1500+ var extension = getExtension(item.filename)
1501+ if (extension in imgMimes) {
1502+ var filename = escapeHTML(item.filename)
1503+ return cb(null,
1504+ '<pre><table class="code">' +
1505+ '<tr><th id="' + escapeHTML(item.filename) + '">' +
1506+ filename + '</th></tr>' +
1507+ '<tr><td><img src="' + encodeLink(item.rawPath) + '"' +
1508+ ' alt="' + filename + '"/></td></tr>' +
1509+ '</table></pre>')
1510+ }
14911511 var done = multicb({ pluck: 1, spread: true })
14921512 getRepoObjectString(repos[0], item.id[0], done())
14931513 getRepoObjectString(repos[1], item.id[lastI], done())
14941514 done(function (err, strOld, strNew) {
14951515 if (err) return cb(err)
1496- var commitId = item.id[lastI] ? id : treeIds.filter(Boolean)[0]
14971516 cb(null, htmlLineDiff(item.filename, item.filename,
14981517 strOld, strNew,
1499- encodeLink([repos[0].id, 'blob', commitId].concat(item.path))))
1518+ encodeLink(item.blobPath)))
15001519 })
15011520 }, 4)
15021521 )
15031522 ])
15041523 }
15051524
1506- function htmlLineDiff(filename, anchor, oldStr, newStr, blobHref, rawHref) {
1525+ function htmlLineDiff(filename, anchor, oldStr, newStr, blobHref) {
15071526 var diff = JsDiff.structuredPatch('', '', oldStr, newStr)
15081527 var groups = diff.hunks.map(function (hunk) {
15091528 var oldLine = hunk.oldStart
15101529 var newLine = hunk.newStart
@@ -1528,9 +1547,9 @@
15281547 '<td class="code-text">' + html + '</td></tr>'
15291548 }))
15301549 })
15311550 return '<pre><table class="code">' +
1532- '<tr><th colspan=3 id="' + anchor + '">' + filename +
1551+ '<tr><th colspan=3 id="' + escapeHTML(anchor) + '">' + filename +
15331552 '<span class="right-bar">' +
15341553 '<a href="' + blobHref + '">View</a> ' +
15351554 '</span></th></tr>' +
15361555 [].concat.apply([], groups).join('') +
static/styles.cssView
@@ -374,8 +374,12 @@
374374
375375 .diff-old { background-color: #ffe2dd; }
376376 .diff-new { background-color: #d1ffd6; }
377377
378+.code img {
379+ max-width: 100%;
380+}
381+
378382 /* Pull requests */
379383
380384 #commits:not(:target) ~ #commits-tab,
381385 #commits:target ~ #files-tab {

Built with git-ssb-web