From 6031edb7581190574cb3c82931c1e7c7bb877ed8 Mon Sep 17 00:00:00 2001 From: Bojan Mojsilovic Date: Wed, 7 Feb 2024 18:09:06 +0100 Subject: [PATCH] Remove empty space at the end of a note --- src/components/EmbeddedNote/EmbeddedNote.tsx | 6 +- src/components/LinkPreview/LinkPreview.tsx | 4 +- src/components/ParsedNote/ParsedNote.tsx | 100 ++++++++++++++----- src/constants.ts | 1 + src/index.scss | 10 ++ src/lib/notes.tsx | 4 +- 6 files changed, 95 insertions(+), 30 deletions(-) diff --git a/src/components/EmbeddedNote/EmbeddedNote.tsx b/src/components/EmbeddedNote/EmbeddedNote.tsx index 299fbf6..c849d30 100644 --- a/src/components/EmbeddedNote/EmbeddedNote.tsx +++ b/src/components/EmbeddedNote/EmbeddedNote.tsx @@ -15,7 +15,7 @@ import VerificationCheck from '../VerificationCheck/VerificationCheck'; import styles from './EmbeddedNote.module.scss'; -const EmbeddedNote: Component<{ note: PrimalNote, mentionedUsers?: Record, includeEmbeds?: boolean}> = (props) => { +const EmbeddedNote: Component<{ note: PrimalNote, mentionedUsers?: Record, includeEmbeds?: boolean, isLast?: boolean}> = (props) => { const threadContext = useThreadContext(); const intl = useIntl(); @@ -36,7 +36,7 @@ const EmbeddedNote: Component<{ note: PrimalNote, mentionedUsers?: Record @@ -48,7 +48,7 @@ const EmbeddedNote: Component<{ note: PrimalNote, mentionedUsers?: Record navToThread()} data-event={props.note.post.id} data-event-bech32={noteId()} diff --git a/src/components/LinkPreview/LinkPreview.tsx b/src/components/LinkPreview/LinkPreview.tsx index 63218d5..9a6fa48 100644 --- a/src/components/LinkPreview/LinkPreview.tsx +++ b/src/components/LinkPreview/LinkPreview.tsx @@ -4,7 +4,7 @@ import { hookForDev } from '../../lib/devTools'; import styles from './LinkPreview.module.scss'; -const LinkPreview: Component<{ preview: any, id?: string, bordered?: boolean }> = (props) => { +const LinkPreview: Component<{ preview: any, id?: string, bordered?: boolean, isLast?: boolean }> = (props) => { const media = useMediaContext(); @@ -44,6 +44,8 @@ const LinkPreview: Component<{ preview: any, id?: string, bordered?: boolean }> k += " embeddedContent"; + k += props.isLast ? ' noBottomMargin' : ''; + return k; }; diff --git a/src/components/ParsedNote/ParsedNote.tsx b/src/components/ParsedNote/ParsedNote.tsx index c8cd308..1a24388 100644 --- a/src/components/ParsedNote/ParsedNote.tsx +++ b/src/components/ParsedNote/ParsedNote.tsx @@ -399,6 +399,17 @@ const ParsedNote: Component<{ parseToken(token); } + + // Check if the last media is the last meaningfull content in the note + // And if so, make it the actual last content + // @ts-ignore + const lastMediaIndex = content.findLastIndex(c => c.type !== 'text'); + + const lastContent = content[content.length - 1]; + + if (lastMediaIndex === content.length - 2 && lastContent.type === 'text' && lastContent.tokens.every(t => [' ', ''].includes(t))) { + setContent((cont) => cont.slice(0, cont.length - 1)); + } }; const renderLinebreak = (item: NoteContent) => { @@ -430,11 +441,15 @@ const ParsedNote: Component<{ return <>{text}; }; - const renderImage = (item: NoteContent) => { + const renderImage = (item: NoteContent, index?: number) => { const groupCount = item.tokens.length; const imageGroup = generatePrivateKey(); + // Remove bottom margin if media is the last thing in the note + const lastClass = index === content.length-1 ? + 'noBottomMargin' : ''; + if (groupCount === 1) { if (isNoteTooLong()) return; @@ -446,7 +461,7 @@ const ParsedNote: Component<{ setWordsDisplayed(w => w + 100); return } - const renderVideo = (item: NoteContent) => { + const renderVideo = (item: NoteContent, index?: number) => { + // Remove bottom margin if media is the last thing in the note + const lastClass = index === content.length-1 ? + 'noBottomMargin' : ''; + return { (token) => { if (isNoteTooLong()) return; @@ -512,6 +531,7 @@ const ParsedNote: Component<{ } klass += ' embeddedContent'; + klass += ` ${lastClass}`; setWordsDisplayed(w => w + shortMentionInWords); @@ -533,7 +553,10 @@ const ParsedNote: Component<{ }; } - const renderYouTube = (item: NoteContent) => { + const renderYouTube = (item: NoteContent, index?: number) => { + // Remove bottom margin if media is the last thing in the note + const lastClass = index === content.length-1 ? + 'noBottomMargin' : ''; return {(token) => { @@ -544,7 +567,7 @@ const ParsedNote: Component<{ const youtubeId = isYouTube(token) && RegExp.$1; return