Browse Source

Fix emoji

master
morethanwords 4 years ago
parent
commit
95353248f5
  1. 36
      src/emoji/index.ts
  2. 4
      src/emoji/regex.ts
  3. 374
      src/lib/richtextprocessor.ts
  4. 51
      src/lib/utils.ts
  5. 2
      src/scripts/format_jsons.js

36
src/emoji/index.ts

@ -0,0 +1,36 @@
// https://github.com/twitter/twemoji-parser/blob/master/src/lib/regex.js
export function encodeEmoji(emojiText: string) {
const codepoints = toCodePoints(removeVS16s(emojiText)).join('-');
return codepoints;
}
const vs16RegExp = /\uFE0F/g;
// avoid using a string literal like '\u200D' here because minifiers expand it inline
const zeroWidthJoiner = String.fromCharCode(0x200d);
const removeVS16s = (rawEmoji: string) => (rawEmoji.indexOf(zeroWidthJoiner) < 0 ? rawEmoji.replace(vs16RegExp, '') : rawEmoji);
export function toCodePoints(unicodeSurrogates: string): Array<string> {
const points = [];
let char = 0;
let previous = 0;
let i = 0;
while(i < unicodeSurrogates.length) {
char = unicodeSurrogates.charCodeAt(i++);
if(previous) {
points.push((0x10000 + ((previous - 0xd800) << 10) + (char - 0xdc00)).toString(16));
previous = 0;
} else if (char > 0xd800 && char <= 0xdbff) {
previous = char;
} else {
points.push(char.toString(16));
}
}
if(points.length && points[0].length == 2) {
points[0] = '00' + points[0];
}
return points;
}

4
src/emoji/regex.ts

File diff suppressed because one or more lines are too long

374
src/lib/richtextprocessor.js → src/lib/richtextprocessor.ts

File diff suppressed because one or more lines are too long

51
src/lib/utils.ts

@ -482,57 +482,6 @@ export function calcImageInBox (imageW: number, imageH: number, boxW: number, bo
return {w: boxedImageW, h: boxedImageH}; return {w: boxedImageW, h: boxedImageH};
} }
/**
* emojiUnicode
* Get the unicode code of an emoji in base 16.
*
* @name emojiUnicode
* @function
* @param {String} input The emoji character.
* @returns {String} The base 16 unicode code.
*/
export function emojiUnicode(input: string) {
let pairs = emojiUnicode.raw(input).split(' ').map(val => parseInt(val).toString(16))/* .filter(p => p != 'fe0f') */;
if(pairs.length && pairs[0].length == 2) pairs[0] = '00' + pairs[0];
return pairs.join('-');
}
/**
* emojiunicode.raw
* Get the unicode code points of an emoji in base 16.
*
* @name emojiunicode.raw
* @function
* @param {String} input The emoji character.
* @returns {String} The unicode code points.
*/
emojiUnicode.raw = function(input: string) {
if(input.length === 1) {
return input.charCodeAt(0).toString();
} else if(input.length > 1) {
const pairs = [];
for(var i = 0; i < input.length; i++) {
// high surrogate
if(input.charCodeAt(i) >= 0xd800 && input.charCodeAt(i) <= 0xdbff) {
if(input.charCodeAt(i + 1) >= 0xdc00 && input.charCodeAt(i + 1) <= 0xdfff) {
// low surrogate
pairs.push(
(input.charCodeAt(i) - 0xd800) * 0x400
+ (input.charCodeAt(i + 1) - 0xdc00) + 0x10000
);
}
} else if(input.charCodeAt(i) < 0xd800 || input.charCodeAt(i) > 0xdfff) {
// modifiers and joiners
pairs.push(input.charCodeAt(i))
}
}
return pairs.join(' ');
}
return '';
};
export function getEmojiToneIndex(input: string) { export function getEmojiToneIndex(input: string) {
let match = input.match(/[\uDFFB-\uDFFF]/); let match = input.match(/[\uDFFB-\uDFFF]/);
return match ? 5 - (57343 - match[0].charCodeAt(0)) : 0; return match ? 5 - (57343 - match[0].charCodeAt(0)) : 0;

2
src/scripts/format_jsons.js

@ -93,6 +93,8 @@ if(false) {
let emoji = unified/* .replace(/-FE0F/gi, '') */.split('-') let emoji = unified/* .replace(/-FE0F/gi, '') */.split('-')
.reduce((prev, curr) => prev + String.fromCodePoint(parseInt(curr, 16)), ''); .reduce((prev, curr) => prev + String.fromCodePoint(parseInt(curr, 16)), '');
//emoji = emoji.replace(/\ufe0f/g, '');
let c = categories[category] === undefined ? 9 : categories[category]; let c = categories[category] === undefined ? 9 : categories[category];
//obj[emoji] = '' + c + sort_order; //obj[emoji] = '' + c + sort_order;
//obj[emoji] = +('' + (c * 1000 + sort_order)).replace(/0+/g, '0').replace(/^(\d)0(\d)/g, '$1$2'); //obj[emoji] = +('' + (c * 1000 + sort_order)).replace(/0+/g, '0').replace(/^(\d)0(\d)/g, '$1$2');

Loading…
Cancel
Save