git ssb

16+

cel / patchfoo



Commit 4ee97dfaa67cc8ab77bbac574f0d05c6d60f4e37

Render descriptions as diffs in gathering threads

cel committed on 3/28/2020, 5:55:36 PM
Parent: 646e70a38f1daae390271c3ad65c4b6c0d87de5f

Files changed

lib/app.jschanged
lib/render-msg.jschanged
lib/render.jschanged
lib/serve.jschanged
lib/app.jsView
@@ -29,8 +29,12 @@
2929 var zeros = new Buffer(24); zeros.fill(0)
3030
3131 module.exports = App
3232
33 +function getKey(msg) {
34 + return msg && msg.key
35 +}
36 +
3337 function App(sbot, config) {
3438 this.sbot = sbot
3539 this.config = config
3640
lib/render-msg.jsView
@@ -18,15 +18,19 @@
1818 this.c = content || {}
1919 this.isMissing = !content
2020 this.hasFullLink =
2121 this.c.type === 'chess_move' ||
22- this.c.type === 'ssb_chess_move'
22 + this.c.type === 'ssb_chess_move' ||
23 + (this.c.type === 'about' && this.c.about == this.value.author
24 + && typeof this.c.description === 'string')
2325
2426 if (typeof opts === 'boolean') opts = {raw: opts}
2527 this.opts = opts || {}
2628 this.shouldWrap = this.opts.wrap !== false
2729 var ts = this.value.timestamp
2830 this.date = ts ? new Date(ts) : null
31 + this.thread = opts.links || []
32 + this.single = opts.single
2933 }
3034
3135 RenderMsg.prototype.getMsg = function (id, cb) {
3236 if (!id) return cb()
@@ -644,8 +648,9 @@
644648 'talenet-version': true,
645649 }
646650
647651 RenderMsg.prototype.about = function (cb) {
652 + var self = this
648653 var keys = Object.keys(this.c).filter(function (k) {
649654 return k !== 'about' && k !== 'type' && k !== 'recps'
650655 }).sort().join()
651656 var isSelf = this.c.about === this.msg.value.author
@@ -701,8 +706,17 @@
701706 var showComputedName = !isSelf && !this.c.name
702707 var target = this.c.about
703708 var pwh = this.c.publicWebHosting
704709
710 + function descriptionOrDiff(cb) {
711 + // show diff for self-description and gathering thread.
712 + // otherwise it might require a more expensive query
713 + var prevMsg = !self.opts.full && self.getPreviousAboutInThreadSync()
714 + cb()
715 + if (prevMsg) return self.render.textEditDiffTable(prevMsg, self.msg)
716 + return h('div', {innerHTML: self.render.markdown(self.c.description)})
717 + }
718 +
705719 this.wrap([
706720 this.c.root && this.c.root !== target ? h('div',
707721 h('small', '→ ', this.link1(this.c.root, done()))
708722 ) : '',
@@ -730,10 +744,9 @@
730744 ] : ''
731745 ]) : '',
732746 this.c.genre ? h('div', ['genre: ', h('u', u.toString(this.c.genre))]) : '',
733747 this.c.shelve ? h('div', ['shelf: ', h('u', u.toString(this.c.shelve))]) : '',
734- this.c.description ? h('div',
735- {innerHTML: this.render.markdown(this.c.description)}) : '',
748 + this.c.description ? descriptionOrDiff(done()) : '',
736749 this.c.review ? h('blockquote',
737750 {innerHTML: this.render.markdown(this.c.review)}) : '',
738751 this.c.attendee ? h('div',
739752 this.link1(this.c.attendee.link, done()),
@@ -762,8 +775,29 @@
762775 ], elCb)
763776 done(cb)
764777 }
765778
779 +RenderMsg.prototype.getPreviousAboutInThreadSync = function () {
780 + var self = this
781 + var root = this.thread[0]
782 + if (!root) return
783 + if (this.c.about !== root.key) {
784 + // probably won't work
785 + return
786 + }
787 + var i = this.thread.indexOf(this.msg)
788 + var target = this.c.about
789 + if (i === -1) return
790 + for (var j = i-1; j >= 0; j--) {
791 + var msg = this.thread[j]
792 + var c = msg && msg.value && msg.value.content
793 + if (c && c.type === 'about' && c.about === target
794 + && typeof c.description === 'string') {
795 + return msg
796 + }
797 + }
798 +}
799 +
766800 RenderMsg.prototype.aboutRating = function (cb) {
767801 var rating = Number(this.c.rating)
768802 var max = Number(this.c.ratingMax)
769803 var type = this.c.ratingType
lib/render.jsView
@@ -12,8 +12,9 @@
1212 var multicb = require('multicb')
1313 var RenderMsg = require('./render-msg')
1414 var Highlight = require('highlight.js')
1515 var md = require('ssb-markdown')
16 +var Diff = require('diff')
1617
1718 module.exports = Render
1819
1920 function MdRenderer(render) {
@@ -718,4 +719,47 @@
718719 }
719720 }
720721 )
721722 }
723 +
724 +Render.prototype.textEditDiffTable = function (oldMsg, newMsg) {
725 + var oldC = oldMsg && oldMsg.value.content || {}
726 + var newC = newMsg && newMsg.value.content || {}
727 + var oldText = String(oldC.text || oldC.description || '')
728 + var newText = String(newC.text || newC.description || '')
729 + var diff = Diff.structuredPatch('', '', oldText, newText)
730 + var self = this
731 + return h('table', [
732 + diff.hunks.map(function (hunk) {
733 + var oldLine = hunk.oldStart
734 + var newLine = hunk.newStart
735 + return [
736 + h('tr', [
737 + h('td', {colspan: 2}),
738 + h('td', h('pre',
739 + '@@ -' + oldLine + ',' + hunk.oldLines + ' ' +
740 + '+' + newLine + ',' + hunk.newLines + ' @@'))
741 + ]),
742 + hunk.lines.map(function (line) {
743 + var s = line[0]
744 + if (s == '\\') return
745 + var lineNums = [s == '+' ? '' : oldLine++, s == '-' ? '' : newLine++]
746 + return [
747 + h('tr', {
748 + class: s == '+' ? 'diff-new' : s == '-' ? 'diff-old' : ''
749 + }, [
750 + lineNums.map(function (num, i) {
751 + return h('td', String(num))
752 + }),
753 + h('td', {innerHTML:
754 + h('code', s).outerHTML +
755 + u.unwrapP(self.markdown(line.substr(1),
756 + s == '-' ? oldC.mentions : newC.mentions))
757 + })
758 + ])
759 + ]
760 + })
761 + ]
762 + })
763 + ])
764 +}
765 +
lib/serve.jsView
The diff is too large to show. Use a local git client to view these changes.
Old file size: 164411 bytes
New file size: 164546 bytes

Built with git-ssb-web