this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 79 lines 2.7 kB view raw
1import states from './states'; 2 3function handleContentLinks(opts) { 4 const { mentions = [], instance, previewMode, statusURL } = opts || {}; 5 return (e) => { 6 let { target } = e; 7 target = target.closest('a'); 8 if (!target) return; 9 10 // If cmd/ctrl/shift/alt key is pressed or middle-click, let the browser handle it 11 if (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey || e.which === 2) { 12 return; 13 } 14 15 const prevText = target.previousSibling?.textContent; 16 const textBeforeLinkIsAt = prevText?.endsWith('@'); 17 const textStartsWithAt = target.innerText.startsWith('@'); 18 if ( 19 ((target.classList.contains('u-url') || 20 target.classList.contains('mention')) && 21 textStartsWithAt) || 22 (textBeforeLinkIsAt && !textStartsWithAt) 23 ) { 24 const targetText = ( 25 target.querySelector('span') || target 26 ).innerText.trim(); 27 const username = targetText.replace(/^@/, ''); 28 const url = target.getAttribute('href'); 29 // Only fallback to acct/username check if url doesn't match 30 const mention = 31 mentions.find((mention) => mention.url === url) || 32 mentions.find( 33 (mention) => 34 mention.acct === username || mention.username === username, 35 ); 36 console.warn('MENTION', mention, url); 37 if (mention) { 38 e.preventDefault(); 39 e.stopPropagation(); 40 states.showAccount = { 41 account: mention.acct, 42 instance, 43 }; 44 } else if (!/^http/i.test(targetText)) { 45 console.log('mention not found', targetText); 46 e.preventDefault(); 47 e.stopPropagation(); 48 const href = target.getAttribute('href'); 49 states.showAccount = { 50 account: href, 51 instance, 52 }; 53 } 54 } else if (!previewMode) { 55 const textBeforeLinkIsHash = prevText?.endsWith('#'); 56 if (target.classList.contains('hashtag') || textBeforeLinkIsHash) { 57 e.preventDefault(); 58 e.stopPropagation(); 59 const tag = target.innerText.replace(/^#/, '').trim(); 60 const hashURL = instance ? `#/${instance}/t/${tag}` : `#/t/${tag}`; 61 console.log({ hashURL }); 62 location.hash = hashURL; 63 } else if ( 64 states.unfurledLinks[target.href]?.url && 65 statusURL !== target.href 66 ) { 67 // If unfurled AND not self-referential 68 e.preventDefault(); 69 e.stopPropagation(); 70 states.prevLocation = { 71 pathname: location.hash.replace(/^#/, ''), 72 }; 73 location.hash = `#${states.unfurledLinks[target.href].url}`; 74 } 75 } 76 }; 77} 78 79export default handleContentLinks;