2023-01-22 15:29:48 +03:00
|
|
|
import states from './states';
|
|
|
|
|
2023-01-31 14:31:25 +03:00
|
|
|
function handleContentLinks(opts) {
|
2023-02-05 19:17:19 +03:00
|
|
|
const { mentions = [], instance } = opts || {};
|
2023-01-22 15:29:48 +03:00
|
|
|
return (e) => {
|
|
|
|
let { target } = e;
|
|
|
|
if (target.parentNode.tagName.toLowerCase() === 'a') {
|
|
|
|
target = target.parentNode;
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
target.tagName.toLowerCase() === 'a' &&
|
|
|
|
target.classList.contains('u-url')
|
|
|
|
) {
|
|
|
|
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-01-31 14:31:25 +03:00
|
|
|
} else if (
|
|
|
|
target.tagName.toLowerCase() === 'a' &&
|
|
|
|
target.classList.contains('hashtag')
|
|
|
|
) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
const tag = target.innerText.replace(/^#/, '').trim();
|
2023-02-09 12:41:42 +03:00
|
|
|
const hashURL = instance ? `#/${instance}/t/${tag}` : `#/t/${tag}`;
|
2023-02-05 19:17:19 +03:00
|
|
|
console.log({ hashURL });
|
|
|
|
location.hash = hashURL;
|
2023-01-22 15:29:48 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-01-31 14:31:25 +03:00
|
|
|
export default handleContentLinks;
|