lbry-desktop/ui/util/remark-lbry.js
jessopb 0b41fc041a
Bringing in emotes, stickers, and refactors from ody (#7435)
* [New Feature] Comment Emotes (#125)

* Refactor form-field

* Create new Emote Menu

* Add Emotes

* Add Emote Selector and Emote Comment creation ability

* Fix and Split CSS

* [New Feature] Stickers (#131)

* Refactor filePrice

* Refactor Wallet Tip Components

* Add backend sticker support for comments

* Add stickers

* Refactor commentCreate

* Add Sticker Selector and sticker comment creation

* Add stickers display to comments and hyperchats

* Fix wrong checks for total Super Chats

* Stickers/emojis fall out / improvements (#220)

* Fix error logs

* Improve LBC sticker flow/clarity

* Show inline error if custom sticker amount below min

* Sort emojis alphabetically

* Improve loading of Images

* Improve quality and display of emojis and fix CSS

* Display both USD and LBC prices

* Default to LBC tip if creator can't receive USD

* Don't clear text-field after sticker is sent

* Refactor notification component

* Handle notifications

* Don't show profile pic on sticker livestream comments

* Change Sticker icon

* Fix wording and number rounding

* Fix blurring emojis

* Disable non functional emote buttons

* new Stickers! (#248)

* Add new stickers (#347)

* Fix cancel sending sticker (#447)

* Refactor scrollbar CSS for portal components outside of main

Refactor channelMention suggestions into new textareaSuggestions component

Install @mui/material packages

Move channel mentioning to use @mui/Autocomplete combobox without search functionality

Add support for suggesting Emotes while typing ':'

Improve label to display matching term

Add back and improved support for searching while mentioning

Add support for suggesting emojis

Fix non concatenated strings

Add key to groups and options

Fix dispatch props

Fix Popper positioning to be consistent

Fix and Improve searching

Add back support for Winning Uri

Filter default emojis with the same name as emotes

Remove unused topSuggestion component

Fix text color on darkmode

Fix livestream updating state from both websocket and reducer and causing double of the same comments to appear

Fix blur and focus commentCreate events

Fix no name after @ error

* desktop tweaks

Co-authored-by: saltrafael <76502841+saltrafael@users.noreply.github.com>
Co-authored-by: Thomas Zarebczan <tzarebczan@users.noreply.github.com>
Co-authored-by: Rafael <rafael.saes@odysee.com>
2022-01-24 11:07:09 -05:00

167 lines
4.4 KiB
JavaScript

import { parseURI } from 'util/lbryURI';
import visit from 'unist-util-visit';
const protocol = 'lbry://';
const uriRegex = /(lbry:\/\/)[^\s"]*[^)]/g;
export const punctuationMarks = [',', '.', '!', '?', ':', ';', '-', ']', ')', '}'];
const mentionToken = '@';
// const mentionTokenCode = 64; // @
const invalidRegex = /[-_.+=?!@#$%^&*:;,{}<>\w/\\]/;
const mentionRegex = /@[^\s()"-_.+=?!@$%^&*;,{}<>/\\]*/gm;
function handlePunctuation(value) {
const modifierIndex =
(value.indexOf(':') >= 0 && value.indexOf(':')) || (value.indexOf('#') >= 0 && value.indexOf('#'));
let punctuationIndex;
punctuationMarks.some((p) => {
if (modifierIndex) {
punctuationIndex = value.indexOf(p, modifierIndex + 1) >= 0 && value.indexOf(p, modifierIndex + 1);
}
return punctuationIndex;
});
return punctuationIndex ? value.substring(0, punctuationIndex) : value;
}
// Find channel mention
function locateMention(value, fromIndex) {
const index = value.indexOf(mentionToken, fromIndex);
// Skip invalid mention
if (index > 0 && invalidRegex.test(value.charAt(index - 1))) {
return locateMention(value, index + 1);
}
return index;
}
// Find claim url
function locateURI(value, fromIndex) {
var index = value.indexOf(protocol, fromIndex);
// Skip invalid uri
if (index > 0 && invalidRegex.test(value.charAt(index - 1))) {
return locateMention(value, index + 1);
}
return index;
}
// Generate a valid markdown link
const createURI = (text, uri, embed = false) => ({
type: 'link',
url: (uri.startsWith(protocol) ? '' : protocol) + uri,
data: {
// Custom attribute
hProperties: { embed },
},
children: [{ type: 'text', value: text }],
});
const validateURI = (match, eat) => {
if (match) {
try {
const text = match[0];
const newText = handlePunctuation(text);
const uri = parseURI(newText);
const isValid = uri && uri.claimName;
const isChannel = uri.isChannel && uri.path === uri.claimName;
if (isValid) {
// Create channel link
if (isChannel) {
return eat(newText)(createURI(uri.claimName, newText, false));
}
// Create claim link
return eat(newText)(createURI(newText, newText, true));
}
} catch (err) {
// Silent errors: console.error(err)
}
}
};
// Generate a markdown link from channel name
function tokenizeMention(eat, value, silent) {
if (silent) {
return true;
}
const match = value.match(mentionRegex);
return validateURI(match, eat, self);
}
// Generate a markdown link from lbry url
function tokenizeURI(eat, value, silent) {
if (silent) {
return true;
}
const match = value.match(uriRegex);
return validateURI(match, eat);
}
// Configure tokenizer for lbry urls
tokenizeURI.locator = locateURI;
tokenizeURI.notInList = true;
tokenizeURI.notInLink = true;
tokenizeURI.notInBlock = true;
// Configure tokenizer for lbry channels
tokenizeMention.locator = locateMention;
tokenizeMention.notInList = true;
tokenizeMention.notInLink = true;
tokenizeMention.notInBlock = true;
const visitor = (node, index, parent) => {
if (node.type === 'link' && parent && parent.type === 'paragraph') {
try {
const uri = parseURI(node.url);
const isValid = uri && uri.claimName;
const isChannel = uri.isChannel && uri.path === uri.claimName;
if (isValid && !isChannel) {
if (!node.data || !node.data.hProperties) {
// Create new node data
node.data = {
hProperties: { embed: true },
};
} else if (node.data.hProperties) {
// Don't overwrite current attributes
node.data.hProperties = {
embed: true,
...node.data.hProperties,
};
}
}
} catch (err) {
// Silent errors: console.error(err)
}
}
};
// transform
const transform = (tree) => {
visit(tree, ['link'], visitor);
};
export const formattedLinks = () => transform;
// Main module
export function inlineLinks() {
const Parser = this.Parser;
const tokenizers = Parser.prototype.inlineTokenizers;
const methods = Parser.prototype.inlineMethods;
// Add an inline tokenizer (defined in the following example).
tokenizers.uri = tokenizeURI;
tokenizers.mention = tokenizeMention;
// Run it just before `text`.
methods.splice(methods.indexOf('text'), 0, 'uri');
methods.splice(methods.indexOf('text'), 0, 'mention');
}