fix redrafting for the 39482398th time #17

merged
opened by scanash.com targeting main
  • Fixed truncated links
  • Fixed multiple line breaks replaced with spaces
  • Fixed HTML tags stripped on web
Changed files
+46 -5
src
components
PostControls
lib
view
com
composer
text-input
+2 -1
src/components/PostControls/PostMenu/PostMenuItems.tsx
···
} from '#/lib/routes/types'
import {logEvent, useGate} from '#/lib/statsig/statsig'
import {richTextToString} from '#/lib/strings/rich-text-helpers'
+
import {restoreLinks} from '#/lib/strings/rich-text-manip'
import {toShareUrl} from '#/lib/strings/url-helpers'
import {logger} from '#/logger'
import {isWeb} from '#/platform/detection'
···
}
openComposer({
-
text: record.text,
+
text: restoreLinks(record.text, record.facets),
imageUris,
videoUri,
onPost: () => {
+3 -3
src/lib/api/index.ts
···
const {text: parsedText, facets: markdownFacets} =
parseMarkdownLinks(trimmedText)
-
let rt = new RichText({text: parsedText}, {cleanNewlines: true})
+
let rt = new RichText({text: parsedText})
await rt.detectFacets(agent)
if (markdownFacets.length > 0) {
···
)
const width = Math.round(
-
videoDraft.asset?.width ||
+
videoDraft.asset?.width ||
('redraftDimensions' in videoDraft ? videoDraft.redraftDimensions.width : 1000)
)
const height = Math.round(
-
videoDraft.asset?.height ||
+
videoDraft.asset?.height ||
('redraftDimensions' in videoDraft ? videoDraft.redraftDimensions.height : 1000)
)
+32
src/lib/strings/rich-text-manip.ts
···
import {toShortUrl} from './url-helpers'
+
export function restoreLinks(
+
text: string,
+
facets?: AppBskyRichtextFacet.Main[],
+
): string {
+
if (!facets?.length) {
+
return text
+
}
+
+
const rt = new UnicodeString(text)
+
const parts: string[] = []
+
let lastIndex = 0
+
+
const sortedFacets = [...facets].sort(
+
(a, b) => a.index.byteStart - b.index.byteStart,
+
)
+
+
for (const facet of sortedFacets) {
+
const isLink = facet.features.find(AppBskyRichtextFacet.isLink)
+
if (!isLink) {
+
continue
+
}
+
+
parts.push(rt.slice(lastIndex, facet.index.byteStart))
+
parts.push(isLink.uri)
+
lastIndex = facet.index.byteEnd
+
}
+
+
parts.push(rt.slice(lastIndex))
+
+
return parts.join('')
+
}
+
export function shortenLinks(rt: RichText): RichText {
if (!rt.facets?.length) {
return rt
+9 -1
src/view/com/composer/text-input/TextInput.web.tsx
···
}
},
},
-
content: generateJSON(richtext.text.toString(), extensions, {
+
content: generateJSON(textToHtml(richtext.text.toString()), extensions, {
preserveWhitespace: 'full',
}),
autofocus: 'end',
···
},
})
+
function textToHtml(text: string): string {
+
return text
+
.replace(/&/g, '&')
+
.replace(/</g, '&lt;')
+
.replace(/>/g, '&gt;')
+
.replace(/\n/g, '<br>')
+
}
+
function getImageOrVideoFromUri(
items: DataTransferItemList,
callback: (uri: string) => void,