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
··· 37 37 } from '#/lib/routes/types' 38 38 import {logEvent, useGate} from '#/lib/statsig/statsig' 39 39 import {richTextToString} from '#/lib/strings/rich-text-helpers' 40 + import {restoreLinks} from '#/lib/strings/rich-text-manip' 40 41 import {toShareUrl} from '#/lib/strings/url-helpers' 41 42 import {logger} from '#/logger' 42 43 import {isWeb} from '#/platform/detection' ··· 350 351 } 351 352 352 353 openComposer({ 353 - text: record.text, 354 + text: restoreLinks(record.text, record.facets), 354 355 imageUris, 355 356 videoUri, 356 357 onPost: () => {
+3 -3
src/lib/api/index.ts
··· 203 203 const {text: parsedText, facets: markdownFacets} = 204 204 parseMarkdownLinks(trimmedText) 205 205 206 - let rt = new RichText({text: parsedText}, {cleanNewlines: true}) 206 + let rt = new RichText({text: parsedText}) 207 207 await rt.detectFacets(agent) 208 208 209 209 if (markdownFacets.length > 0) { ··· 368 368 ) 369 369 370 370 const width = Math.round( 371 - videoDraft.asset?.width || 371 + videoDraft.asset?.width || 372 372 ('redraftDimensions' in videoDraft ? videoDraft.redraftDimensions.width : 1000) 373 373 ) 374 374 const height = Math.round( 375 - videoDraft.asset?.height || 375 + videoDraft.asset?.height || 376 376 ('redraftDimensions' in videoDraft ? videoDraft.redraftDimensions.height : 1000) 377 377 ) 378 378
+32
src/lib/strings/rich-text-manip.ts
··· 2 2 3 3 import {toShortUrl} from './url-helpers' 4 4 5 + export function restoreLinks( 6 + text: string, 7 + facets?: AppBskyRichtextFacet.Main[], 8 + ): string { 9 + if (!facets?.length) { 10 + return text 11 + } 12 + 13 + const rt = new UnicodeString(text) 14 + const parts: string[] = [] 15 + let lastIndex = 0 16 + 17 + const sortedFacets = [...facets].sort( 18 + (a, b) => a.index.byteStart - b.index.byteStart, 19 + ) 20 + 21 + for (const facet of sortedFacets) { 22 + const isLink = facet.features.find(AppBskyRichtextFacet.isLink) 23 + if (!isLink) { 24 + continue 25 + } 26 + 27 + parts.push(rt.slice(lastIndex, facet.index.byteStart)) 28 + parts.push(isLink.uri) 29 + lastIndex = facet.index.byteEnd 30 + } 31 + 32 + parts.push(rt.slice(lastIndex)) 33 + 34 + return parts.join('') 35 + } 36 + 5 37 export function shortenLinks(rt: RichText): RichText { 6 38 if (!rt.facets?.length) { 7 39 return rt
+9 -1
src/view/com/composer/text-input/TextInput.web.tsx
··· 241 241 } 242 242 }, 243 243 }, 244 - content: generateJSON(richtext.text.toString(), extensions, { 244 + content: generateJSON(textToHtml(richtext.text.toString()), extensions, { 245 245 preserveWhitespace: 'full', 246 246 }), 247 247 autofocus: 'end', ··· 503 503 }, 504 504 }) 505 505 506 + function textToHtml(text: string): string { 507 + return text 508 + .replace(/&/g, '&amp;') 509 + .replace(/</g, '&lt;') 510 + .replace(/>/g, '&gt;') 511 + .replace(/\n/g, '<br>') 512 + } 513 + 506 514 function getImageOrVideoFromUri( 507 515 items: DataTransferItemList, 508 516 callback: (uri: string) => void,