2023-01-22 15:29:48 +03:00
|
|
|
import states from './states';
|
|
|
|
|
2023-01-31 14:31:25 +03:00
|
|
|
function handleContentLinks(opts) {
|
2023-03-16 08:02:46 +03:00
|
|
|
const { mentions = [], instance, previewMode } = opts || {};
|
2023-01-22 15:29:48 +03:00
|
|
|
return (e) => {
|
|
|
|
let { target } = e;
|
2023-02-23 11:45:53 +03:00
|
|
|
target = target.closest('a');
|
|
|
|
if (!target) return;
|
|
|
|
if (target.classList.contains('u-url')) {
|
2023-01-22 15:29:48 +03:00
|
|
|
const targetText = (
|
|
|
|
target.querySelector('span') || target
|
|
|
|
).innerText.trim();
|
|
|
|
const username = targetText.replace(/^@/, '');
|
|
|
|
const url = target.getAttribute('href');
|
|
|
|
const mention = mentions.find(
|
|
|
|
(mention) =>
|
|
|
|
mention.username === username ||
|
|
|
|
mention.acct === username ||
|
|
|
|
mention.url === url,
|
|
|
|
);
|
|
|
|
if (mention) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2023-02-05 19:17:19 +03:00
|
|
|
states.showAccount = {
|
|
|
|
account: mention.acct,
|
|
|
|
instance,
|
|
|
|
};
|
2023-01-22 15:29:48 +03:00
|
|
|
} else if (!/^http/i.test(targetText)) {
|
|
|
|
console.log('mention not found', targetText);
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
const href = target.getAttribute('href');
|
2023-02-05 19:17:19 +03:00
|
|
|
states.showAccount = {
|
|
|
|
account: href,
|
|
|
|
instance,
|
|
|
|
};
|
2023-01-22 15:29:48 +03:00
|
|
|
}
|
2023-03-16 08:02:46 +03:00
|
|
|
} else if (!previewMode) {
|
|
|
|
if (target.classList.contains('hashtag')) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
const tag = target.innerText.replace(/^#/, '').trim();
|
|
|
|
const hashURL = instance ? `#/${instance}/t/${tag}` : `#/t/${tag}`;
|
|
|
|
console.log({ hashURL });
|
|
|
|
location.hash = hashURL;
|
|
|
|
} else if (states.unfurledLinks[target.href]?.url) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
states.prevLocation = {
|
|
|
|
pathname: location.hash.replace(/^#/, ''),
|
|
|
|
};
|
|
|
|
location.hash = `#${states.unfurledLinks[target.href].url}`;
|
|
|
|
}
|
2023-01-22 15:29:48 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-01-31 14:31:25 +03:00
|
|
|
export default handleContentLinks;
|