better handling of hellthreads with mentions at bottom

This commit is contained in:
Henry Jameson 2021-06-11 03:11:58 +03:00
parent b84aeff6bf
commit f883d2f75c
10 changed files with 138 additions and 142 deletions

View File

@ -28,11 +28,6 @@ const MentionLink = {
userScreenName: { userScreenName: {
required: false, required: false,
type: String type: String
},
firstMention: {
required: false,
type: Boolean,
default: false
} }
}, },
methods: { methods: {
@ -89,7 +84,6 @@ const MentionLink = {
{ {
'-you': this.isYou, '-you': this.isYou,
'-highlighted': this.highlight, '-highlighted': this.highlight,
'-firstMention': this.firstMention,
'-oldStyle': this.oldStyle '-oldStyle': this.oldStyle
}, },
this.highlightType this.highlightType

View File

@ -38,10 +38,6 @@
.new { .new {
margin-right: 0.25em; margin-right: 0.25em;
&.-firstMention {
display: none;
}
&.-you { &.-you {
& .shortName, & .shortName,
& .full { & .full {

View File

@ -1,5 +1,6 @@
.MentionsLine { .MentionsLine {
.showMoreLess { .showMoreLess {
white-space: normal;
&.-newStyle { &.-newStyle {
line-height: 1.5; line-height: 1.5;
font-size: inherit; font-size: inherit;

View File

@ -33,22 +33,23 @@ export default Vue.component('RichContent', {
default: false default: false
}, },
// Whether to hide last mentions (hellthreads) // Whether to hide last mentions (hellthreads)
hideLastMentions: { hideMentions: {
required: false,
type: Boolean,
default: false
},
// Whether to hide first mentions
hideFirstMentions: {
required: false, required: false,
type: Boolean, type: Boolean,
default: false default: false
} }
}, },
// NEVER EVER TOUCH DATA INSIDE RENDER
render (h) { render (h) {
// Pre-process HTML // Pre-process HTML
const html = preProcessPerLine(this.html, this.greentext, this.hideLastMentions) const { newHtml: html, lastMentions } = preProcessPerLine(this.html, this.greentext, this.hideLastMentions)
console.log(this.hideFirstMentions, this.hideLastMentions) const firstMentions = [] // Mentions that appear in the beginning of post body
const lastTags = [] // Tags that appear at the end of post body
const writtenMentions = [] // All mentions that appear in post body
const writtenTags = [] // All tags that appear in post body
// unique index for vue "tag" property
let mentionIndex = 0
let tagsIndex = 0
const renderImage = (tag) => { const renderImage = (tag) => {
return <StillImage return <StillImage
@ -57,20 +58,37 @@ export default Vue.component('RichContent', {
/> />
} }
const renderHashtag = (attrs, children, encounteredTextReverse) => {
const linkData = getLinkData(attrs, children, tagsIndex++)
writtenTags.push(linkData)
attrs.target = '_blank'
if (!encounteredTextReverse) {
lastTags.push(linkData)
attrs['data-parser-last'] = true
}
return <a {...{ attrs }}>
{ children.map(processItem) }
</a>
}
const renderMention = (attrs, children, encounteredText) => { const renderMention = (attrs, children, encounteredText) => {
return (this.hideFirstMentions && !encounteredText) const linkData = getLinkData(attrs, children, mentionIndex++)
? '' writtenMentions.push(linkData)
: <MentionLink if (!encounteredText) {
firstMentions.push(linkData)
return ''
} else {
return <MentionLink
url={attrs.href} url={attrs.href}
content={flattenDeep(children).join('')} content={flattenDeep(children).join('')}
firstMention={!encounteredText}
/> />
} }
}
// We stop treating mentions as "first" ones when we encounter // We stop treating mentions as "first" ones when we encounter
// non-whitespace text // non-whitespace text
let encounteredText = false let encounteredText = false
// Processor to use with mini_html_converter // Processor to use with html_tree_converter
const processItem = (item, index, array, what) => { const processItem = (item, index, array, what) => {
// Handle text nodes - just add emoji // Handle text nodes - just add emoji
if (typeof item === 'string') { if (typeof item === 'string') {
@ -104,12 +122,22 @@ export default Vue.component('RichContent', {
if (Array.isArray(item)) { if (Array.isArray(item)) {
const [opener, children] = item const [opener, children] = item
const Tag = getTagName(opener) const Tag = getTagName(opener)
const attrs = getAttrs(opener)
switch (Tag) { switch (Tag) {
case 'span': // replace images with StillImage
if (attrs['class'] && attrs['class'].includes('lastMentions')) {
if (firstMentions.length > 0) {
break
} else {
return ''
}
} else {
break
}
case 'img': // replace images with StillImage case 'img': // replace images with StillImage
return renderImage(opener) return renderImage(opener)
case 'a': // replace mentions with MentionLink case 'a': // replace mentions with MentionLink
if (!this.handleLinks) break if (!this.handleLinks) break
const attrs = getAttrs(opener)
if (attrs['class'] && attrs['class'].includes('mention')) { if (attrs['class'] && attrs['class'].includes('mention')) {
return renderMention(attrs, children, encounteredText) return renderMention(attrs, children, encounteredText)
} else if (attrs['class'] && attrs['class'].includes('hashtag')) { } else if (attrs['class'] && attrs['class'].includes('hashtag')) {
@ -132,17 +160,9 @@ export default Vue.component('RichContent', {
} }
} }
} }
// Processor for back direction (for finding "last" stuff, just easier this way) // Processor for back direction (for finding "last" stuff, just easier this way)
let encounteredTextReverse = false let encounteredTextReverse = false
const renderHashtag = (attrs, children, encounteredTextReverse) => {
attrs.target = '_blank'
if (!encounteredTextReverse) {
attrs['data-parser-last'] = true
}
return <a {...{ attrs }}>
{ children.map(processItem) }
</a>
}
const processItemReverse = (item, index, array, what) => { const processItemReverse = (item, index, array, what) => {
// Handle text nodes - just add emoji // Handle text nodes - just add emoji
if (typeof item === 'string') { if (typeof item === 'string') {
@ -166,14 +186,37 @@ export default Vue.component('RichContent', {
} }
return item return item
} }
return <span class="RichContent">
const event = {
firstMentions,
lastMentions,
lastTags,
writtenMentions,
writtenTags
}
const result = <span class="RichContent">
{ this.$slots.prefix } { this.$slots.prefix }
{ convertHtmlToTree(html).map(processItem).reverse().map(processItemReverse).reverse() } { convertHtmlToTree(html).map(processItem).reverse().map(processItemReverse).reverse() }
{ this.$slots.suffix } { this.$slots.suffix }
</span> </span>
// DO NOT MOVE TO UPDATE. BAD IDEA.
this.$emit('parseReady', event)
return result
} }
}) })
const getLinkData = (attrs, children, index) => {
return {
index,
url: attrs.href,
hashtag: attrs['data-tag'],
content: flattenDeep(children).join('')
}
}
/** Pre-processing HTML /** Pre-processing HTML
* *
* Currently this does two things: * Currently this does two things:
@ -183,13 +226,13 @@ export default Vue.component('RichContent', {
* *
* @param {String} html - raw HTML to process * @param {String} html - raw HTML to process
* @param {Boolean} greentext - whether to enable greentexting or not * @param {Boolean} greentext - whether to enable greentexting or not
* @param {Boolean} removeLastMentions - whether to remove last mentions
*/ */
export const preProcessPerLine = (html, greentext, removeLastMentions) => { export const preProcessPerLine = (html, greentext) => {
// Only mark first (last) encounter const lastMentions = []
let lastMentionsMarked = false
return convertHtmlToLines(html).reverse().map((item, index, array) => { const newHtml = convertHtmlToLines(html).reverse().map((item, index, array) => {
// Going over each line in reverse to detect last mentions,
// keeping non-text stuff as-is
if (!item.text) return item if (!item.text) return item
const string = item.text const string = item.text
@ -205,6 +248,7 @@ export const preProcessPerLine = (html, greentext, removeLastMentions) => {
} }
} }
// Converting that line part into tree
const tree = convertHtmlToTree(string) const tree = convertHtmlToTree(string)
// If line has loose text, i.e. text outside a mention or a tag // If line has loose text, i.e. text outside a mention or a tag
@ -215,18 +259,23 @@ export const preProcessPerLine = (html, greentext, removeLastMentions) => {
if (Array.isArray(item)) { if (Array.isArray(item)) {
const [opener, children, closer] = item const [opener, children, closer] = item
const tag = getTagName(opener) const tag = getTagName(opener)
// If we have a link we probably have mentions
if (tag === 'a') { if (tag === 'a') {
const attrs = getAttrs(opener) const attrs = getAttrs(opener)
if (attrs['class'] && attrs['class'].includes('mention')) { if (attrs['class'] && attrs['class'].includes('mention')) {
// Got mentions
hasMentions = true hasMentions = true
return [opener, children, closer] return [opener, children, closer]
} else { } else {
// Not a mention? Means we have loose text or whatever
hasLooseText = true hasLooseText = true
return [opener, children, closer] return [opener, children, closer]
} }
} else if (tag === 'span' || tag === 'p') { } else if (tag === 'span' || tag === 'p') {
return [opener, [...children].reverse().map(process).reverse(), closer] // For span and p we need to go deeper
return [opener, [...children].map(process), closer]
} else { } else {
// Everything else equals to a loose text
hasLooseText = true hasLooseText = true
return [opener, children, closer] return [opener, children, closer]
} }
@ -234,82 +283,43 @@ export const preProcessPerLine = (html, greentext, removeLastMentions) => {
if (typeof item === 'string') { if (typeof item === 'string') {
if (item.trim() !== '') { if (item.trim() !== '') {
// only meaningful strings are loose text
hasLooseText = true hasLooseText = true
} }
return item return item
} }
} }
const result = [...tree].reverse().map(process).reverse() // We now processed our tree, now we need to mark line as lastMentions
const result = [...tree].map(process)
if (removeLastMentions && hasMentions && !hasLooseText && !lastMentionsMarked) { // Only check last (first since list is reversed) line
lastMentionsMarked = true if (hasMentions && !hasLooseText && index === 0) {
return '' let mentionIndex = 0
const process = (item) => {
if (Array.isArray(item)) {
const [opener, children] = item
const tag = getTagName(opener)
if (tag === 'a') {
const attrs = getAttrs(opener)
lastMentions.push(getLinkData(attrs, children, mentionIndex++))
} else if (children) {
children.forEach(process)
}
}
}
result.forEach(process)
// we DO need mentions here so that we conditionally remove them if don't
// have first mentions
return ['<span class="lastMentions">', flattenDeep(result).join(''), '</span>'].join('')
} else { } else {
return flattenDeep(result).join('') return flattenDeep(result).join('')
} }
}).reverse().join('') }).reverse().join('')
return { newHtml, lastMentions }
} }
export const getHeadTailLinks = (html) => { export const getHeadTailLinks = (html) => {
// Exported object properties // Exported object properties
const firstMentions = [] // Mentions that appear in the beginning of post body
const lastMentions = [] // Mentions that appear at the end of post body
const lastTags = [] // Tags that appear at the end of post body
const writtenMentions = [] // All mentions that appear in post body
const writtenTags = [] // All tags that appear in post body
let encounteredText = false
let processingFirstMentions = true
let index = 0 // unique index for vue "tag" property
const getLinkData = (attrs, children, index) => {
return {
index,
url: attrs.href,
hashtag: attrs['data-tag'],
content: flattenDeep(children).join('')
}
}
// Processor to use with html_tree_converter
const processItem = (item) => {
// Handle text nodes - stop treating mentions as "first" when text encountered
if (typeof item === 'string') {
const emptyText = item.trim() === ''
if (emptyText) return
if (!encounteredText) {
encounteredText = true
processingFirstMentions = false
}
// Encountered text? That means tags we've been collectings aren't "last"!
lastTags.splice(0)
lastMentions.splice(0)
return
}
// Handle tag nodes
if (Array.isArray(item)) {
const [opener, children] = item
const Tag = getTagName(opener)
if (Tag !== 'a') return children && children.forEach(processItem)
const attrs = getAttrs(opener)
if (attrs['class']) {
const linkData = getLinkData(attrs, children, index++)
if (attrs['class'].includes('mention')) {
if (processingFirstMentions) {
firstMentions.push(linkData)
}
writtenMentions.push(linkData)
lastMentions.push(linkData)
} else if (attrs['class'].includes('hashtag')) {
lastTags.push(linkData)
writtenTags.push(linkData)
}
return // Stop processing, we don't care about link's contents
}
children && children.forEach(processItem)
}
}
convertHtmlToTree(html).forEach(processItem)
return { firstMentions, writtenMentions, writtenTags, lastTags, lastMentions }
} }

View File

@ -19,7 +19,6 @@ import generateProfileLink from 'src/services/user_profile_link_generator/user_p
import { highlightClass, highlightStyle } from '../../services/user_highlighter/user_highlighter.js' import { highlightClass, highlightStyle } from '../../services/user_highlighter/user_highlighter.js'
import { muteWordHits } from '../../services/status_parser/status_parser.js' import { muteWordHits } from '../../services/status_parser/status_parser.js'
import { unescape, uniqBy } from 'lodash' import { unescape, uniqBy } from 'lodash'
import { getHeadTailLinks } from 'src/components/rich_content/rich_content.jsx'
import { library } from '@fortawesome/fontawesome-svg-core' import { library } from '@fortawesome/fontawesome-svg-core'
import { import {
@ -101,7 +100,8 @@ const Status = {
userExpanded: false, userExpanded: false,
mediaPlaying: [], mediaPlaying: [],
suspendable: true, suspendable: true,
error: null error: null,
headTailLinks: null
} }
}, },
computed: { computed: {
@ -168,9 +168,6 @@ const Status = {
muteWordHits () { muteWordHits () {
return muteWordHits(this.status, this.muteWords) return muteWordHits(this.status, this.muteWords)
}, },
headTailLinks () {
return getHeadTailLinks(this.status.raw_html)
},
mentions () { mentions () {
return this.status.attentions.filter(attn => { return this.status.attentions.filter(attn => {
return attn.screen_name !== this.replyToName && return attn.screen_name !== this.replyToName &&
@ -182,6 +179,7 @@ const Status = {
})) }))
}, },
alsoMentions () { alsoMentions () {
if (!this.headTailLinks) return []
const set = new Set(this.headTailLinks.writtenMentions.map(m => m.url)) const set = new Set(this.headTailLinks.writtenMentions.map(m => m.url))
return this.headTailLinks.writtenMentions.filter(mention => { return this.headTailLinks.writtenMentions.filter(mention => {
return !set.has(mention.url) return !set.has(mention.url)
@ -196,9 +194,6 @@ const Status = {
hasMentionsLine () { hasMentionsLine () {
return this.mentionsLine.length > 0 return this.mentionsLine.length > 0
}, },
hideLastMentions () {
return this.headTailLinks.firstMentions.length === 0
},
muted () { muted () {
if (this.statusoid.user.id === this.currentUser.id) return false if (this.statusoid.user.id === this.currentUser.id) return false
const { status } = this const { status } = this
@ -346,6 +341,9 @@ const Status = {
}, },
removeMediaPlaying (id) { removeMediaPlaying (id) {
this.mediaPlaying = this.mediaPlaying.filter(mediaId => mediaId !== id) this.mediaPlaying = this.mediaPlaying.filter(mediaId => mediaId !== id)
},
setHeadTailLinks (headTailLinks) {
this.headTailLinks = headTailLinks
} }
}, },
watch: { watch: {
@ -356,7 +354,7 @@ const Status = {
// Post is above screen, match its top to screen top // Post is above screen, match its top to screen top
window.scrollBy(0, rect.top - 100) window.scrollBy(0, rect.top - 100)
} else if (rect.height >= (window.innerHeight - 50)) { } else if (rect.height >= (window.innerHeight - 50)) {
// Post we want to see is taller than screen so match its top to screen top // Post we wahttp://localhost:8080/users/hj/dmsnt to see is taller than screen so match its top to screen top
window.scrollBy(0, rect.top - 100) window.scrollBy(0, rect.top - 100)
} else if (rect.bottom > window.innerHeight - 50) { } else if (rect.bottom > window.innerHeight - 50) {
// Post is below screen, match its bottom to screen bottom // Post is below screen, match its bottom to screen bottom

View File

@ -305,11 +305,11 @@
:no-heading="noHeading" :no-heading="noHeading"
:highlight="highlight" :highlight="highlight"
:focused="isFocused" :focused="isFocused"
:hide-first-mentions="mentionsOwnLine && isReply" :hide-mentions="mentionsOwnLine && (isReply || true)"
:hide-last-mentions="hideLastMentions"
:head-tail-links="headTailLinks"
@mediaplay="addMediaPlaying($event)" @mediaplay="addMediaPlaying($event)"
@mediapause="removeMediaPlaying($event)" @mediapause="removeMediaPlaying($event)"
@parseReady="setHeadTailLinks"
ref="content"
/> />
<div <div

View File

@ -3,6 +3,7 @@ import RichContent, { getHeadTailLinks } from 'src/components/rich_content/rich_
import MentionsLine from 'src/components/mentions_line/mentions_line.vue' import MentionsLine from 'src/components/mentions_line/mentions_line.vue'
import { mapGetters } from 'vuex' import { mapGetters } from 'vuex'
import { library } from '@fortawesome/fontawesome-svg-core' import { library } from '@fortawesome/fontawesome-svg-core'
import { set } from 'vue'
import { import {
faFile, faFile,
faMusic, faMusic,
@ -27,11 +28,7 @@ const StatusContent = {
'noHeading', 'noHeading',
'fullContent', 'fullContent',
'singleLine', 'singleLine',
// if this was computed at upper level it can be passed here, otherwise 'hideMentions'
// it will be in this component
'headTailLinks',
'hideFirstMentions',
'hideLastMentions'
], ],
data () { data () {
return { return {
@ -39,9 +36,9 @@ const StatusContent = {
showingLongSubject: false, showingLongSubject: false,
// not as computed because it sets the initial state which will be changed later // not as computed because it sets the initial state which will be changed later
expandingSubject: !this.$store.getters.mergedConfig.collapseMessageWithSubject, expandingSubject: !this.$store.getters.mergedConfig.collapseMessageWithSubject,
headTailLinksComputed: this.headTailLinks headTailLinks: null,
? this.headTailLinks firstMentions: [],
: getHeadTailLinks(this.status.raw_html) lastMentions: []
} }
}, },
computed: { computed: {
@ -81,12 +78,6 @@ const StatusContent = {
attachmentTypes () { attachmentTypes () {
return this.status.attachments.map(file => fileType.fileType(file.mimetype)) return this.status.attachments.map(file => fileType.fileType(file.mimetype))
}, },
mentionsFirst () {
return this.headTailLinksComputed.firstMentions
},
mentionsLast () {
return this.headTailLinksComputed.lastMentions
},
...mapGetters(['mergedConfig']) ...mapGetters(['mergedConfig'])
}, },
components: { components: {
@ -107,6 +98,11 @@ const StatusContent = {
this.expandingSubject = !this.expandingSubject this.expandingSubject = !this.expandingSubject
} }
}, },
setHeadTailLinks (headTailLinks) {
set(this, 'headTailLinks', headTailLinks)
set(this, 'firstMentions', headTailLinks.firstMentions)
set(this, 'lastMentions', headTailLinks.lastMentions)
},
generateTagLink (tag) { generateTagLink (tag) {
return `/tag/${tag}` return `/tag/${tag}`
} }

View File

@ -48,20 +48,21 @@
:html="status.raw_html" :html="status.raw_html"
:emoji="status.emojis" :emoji="status.emojis"
:handle-links="true" :handle-links="true"
:hide-mentions="hideMentions"
:greentext="mergedConfig.greentext" :greentext="mergedConfig.greentext"
:hide-first-mentions="hideFirstMentions" @parseReady="setHeadTailLinks"
:hide-last-mentions="hideLastMentions" ref="text"
> >
<template v-slot:prefix> <template v-slot:prefix>
<MentionsLine <MentionsLine
v-if="!hideFirstMentions && mentionsFirst" v-if="!hideMentions && firstMentions && firstMentions.length > 0"
:mentions="mentionsFirst" :mentions="firstMentions"
/> />
</template> </template>
<template v-slot:suffix> <template v-slot:suffix>
<MentionsLine <MentionsLine
v-if="!hideFirstMentions && mentionsLast" v-if="!hideMentions && lastMentions.length > 0 && firstMentions.length === 0"
:mentions="mentionsLast" :mentions="lastMentions"
/> />
</template> </template>
</RichContent> </RichContent>

View File

@ -32,9 +32,7 @@ const StatusContent = {
'noHeading', 'noHeading',
'fullContent', 'fullContent',
'singleLine', 'singleLine',
'hideFirstMentions', 'hideMentions'
'hideLastMentions',
'headTailLinks'
], ],
computed: { computed: {
hideAttachments () { hideAttachments () {
@ -94,6 +92,9 @@ const StatusContent = {
StatusBody StatusBody
}, },
methods: { methods: {
setHeadTailLinks (headTailLinks) {
this.$emit('parseReady', headTailLinks)
},
setMedia () { setMedia () {
const attachments = this.attachmentSize === 'hide' ? this.status.attachments : this.galleryAttachments const attachments = this.attachmentSize === 'hide' ? this.status.attachments : this.galleryAttachments
return () => this.$store.dispatch('setMedia', attachments) return () => this.$store.dispatch('setMedia', attachments)

View File

@ -4,9 +4,8 @@
<StatusBody <StatusBody
:status="status" :status="status"
:single-line="singleLine" :single-line="singleLine"
:hide-first-mentions="hideFirstMentions" :hide-mentions="hideMentions"
:hide-last-mentions="hideLastMentions" @parseReady="setHeadTailLinks"
:head-tail-links="headTailLinks"
> >
<div v-if="status.poll && status.poll.options"> <div v-if="status.poll && status.poll.options">
<poll :base-poll="status.poll" /> <poll :base-poll="status.poll" />