123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- import { REGEXP } from "./constant";
- import emoji, { prefixUrl, ext } from "./emoji";
-
- export function isFunction(functionToCheck) {
- return (
- functionToCheck && {}.toString.call(functionToCheck) === "[object Function]"
- );
- }
-
- export function isUrl(userInput) {
- const regexp = /(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/g;
- var res = userInput.match(regexp);
- if (res === null) return false;
- else return true;
- }
-
- /**
- * 将对象数组转换为对象
- * @param {array} array Array of Objects
- * @param {string} keyField string
- */
- export function arrayToObject(array, keyField) {
- return array.reduce((obj, item) => {
- obj[item[keyField]] = item;
- return obj;
- }, {});
- }
-
- /**
- * 渲染编辑器
- * [x] => <img src="x" />
- * @param {strig} content
- */
- export function renderContent(content, onClick) {
- return content.replace(REGEXP, function(a, b) {
- const src = a.slice(1, -1);
- if (isUrl(src)) {
- return `<img src="${src}" alt="${src}" style="max-width: 300px" />`;
- }
- const emojiObejct = arrayToObject(emoji, "title");
- const value = emojiObejct[src].value;
- return `<img src="${prefixUrl}${value}.${ext}" alt="${value}" />`;
- });
- }
|