joypixels / emojione

[Archived] The world's largest independent emoji font. Maintained at https://github.com/joypixels/emoji-toolkit.
https://www.joypixels.com
Other
4.46k stars 532 forks source link

Changing images back to unicode/shortName #47

Open aedanexplosive opened 9 years ago

aedanexplosive commented 9 years ago

Is it possible to change the converted images back to unicode or shortName. I am directly converting the text in an editable div to submit to a database. The problem is I would prefer to store the small shortName or unicode and still allow the user to manipulate and copy/paste the images already existing. Is there a function to convert the images back?

aedanexplosive commented 9 years ago

I wrote something quickly along these lines. Can be improved but works. My snippets don't contain any other images other than emoji so could be improved to handle images that aren't emojis by checking for emojione in src urls:

ns.ImageToShortname = function(str) {
        var finalString = str,
        url,
        urlRegex =  /<img.*?src="([^">]*\/([^">]*?))".*?>/g,
        imgRegex = /<img.*?.*?>/;

        while ( url = urlRegex.exec( str ) ) {
            var urlString = url[1],
            currentShortname = "";
                urlString = urlString.substr(urlString.lastIndexOf('/') + 1).split('.')[0];
                for (var shortname in ns.emojioneList) {
                    if (ns.emojioneList[shortname].indexOf(urlString.toLowerCase()) != -1) {
                        currentShortname = shortname;
                    }
                }
                finalString = finalString.replace(imgRegex, currentShortname);
        }
        return finalString;
    }
winadv commented 6 years ago

I am trying to do the same, I use version 4.0 and I want to save in my database only the shortname as I do. I've tried the above code and nothing.