通用评论

1
  1. {"version":3,"sources":["../src/helper.js"],"names":["isFunction","isUrl","arrayToObject","htmlEncode","renderContent","emojiObejct","emoji","functionToCheck","toString","call","userInput","regexp","res","match","array","keyField","reduce","obj","item","str","replace","i","charCodeAt","content","onClick","newContent","indexOf","IMAGE_SPLIT","split","pop","join","REGEXP","a","b","src","slice","prefixUrl","value","ext","title"],"mappings":";;;;;QAKgBA,U,GAAAA,U;QAMAC,K,GAAAA,K;QAYAC,a,GAAAA,a;QAYAC,U,GAAAA,U;QAYAC,a,GAAAA,a;;AA/ChB;;AACA;;;;;;AAEA,IAAMC,cAAcH,cAAcI,eAAd,EAAqB,OAArB,CAApB;;AAEO,SAASN,UAAT,CAAoBO,eAApB,EAAqC;AAC1C,SACEA,mBAAmB,GAAGC,QAAH,CAAYC,IAAZ,CAAiBF,eAAjB,MAAsC,mBAD3D;AAGD;;AAEM,SAASN,KAAT,CAAeS,SAAf,EAA0B;AAC/B,MAAMC,SAAS,kGAAf;AACA,MAAIC,MAAMF,UAAUG,KAAV,CAAgBF,MAAhB,CAAV;AACA,MAAIC,QAAQ,IAAZ,EAAkB,OAAO,KAAP,CAAlB,KACK,OAAO,IAAP;AACN;;AAED;;;;;AAKO,SAASV,aAAT,CAAuBY,KAAvB,EAA8BC,QAA9B,EAAwC;AAC7C,SAAOD,MAAME,MAAN,CAAa,UAACC,GAAD,EAAMC,IAAN,EAAe;AACjCD,QAAIC,KAAKH,QAAL,CAAJ,IAAsBG,IAAtB;AACA,WAAOD,GAAP;AACD,GAHM,EAGJ,EAHI,CAAP;AAID;;AAED;;;;;AAKO,SAASd,UAAT,CAAoBgB,GAApB,EAAyB;AAC9B,MAAI,CAACA,GAAL,EAAU,OAAO,EAAP;AACV,SAAOA,IAAIC,OAAJ,CAAY,SAAZ,EAAuB,UAASC,CAAT,EAAY;AACxC,WAAO,OAAOA,EAAEC,UAAF,CAAa,CAAb,CAAP,GAAyB,GAAhC;AACD,GAFM,CAAP;AAGD;;AAED;;;;;AAKO,SAASlB,aAAT,CAAuBmB,OAAvB,EAAgCC,OAAhC,EAAyC;AAC9C,MAAIC,aAAaF,OAAjB;AACA,MAAIE,WAAWC,OAAX,CAAmBC,qBAAnB,MAAoC,CAAC,CAAzC,EAA4C;AAC1CF,iBAAaA,WAAWG,KAAX,CAAiBD,qBAAjB,CAAb;AACAF,eAAWI,GAAX;AACAJ,iBAAaA,WAAWK,IAAX,CAAgB,EAAhB,CAAb;AACD;;AAED,SAAO3B,WAAWsB,UAAX,EACJL,OADI,CACIW,gBADJ,EACY,UAASC,CAAT,EAAYC,CAAZ,EAAe;AAC9B,QAAMC,MAAMF,EAAEG,KAAF,CAAQ,CAAR,EAAW,CAAC,CAAZ,CAAZ;;AAEA;AACA;AACA,QAAIlC,MAAMiC,GAAN,CAAJ,EAAgB;AACd,kCAAyBA,GAAzB,iBAAsCA,GAAtC;AACD;AACD;AACA,QAAM5B,QAAQD,YAAY6B,GAAZ,CAAd;AACA,QAAI5B,KAAJ,EAAW;AACT,6BAAoB8B,gBAApB,GAAgC9B,MAAM+B,KAAtC,SAA+C/B,MAAMgC,GAArD,iBACEhC,MAAMiC,KADR;AAGD;AACD,iBAAWL,GAAX;AACD,GAjBI,EAkBJd,OAlBI,CAkBI,KAlBJ,EAkBW,QAlBX,CAAP;AAmBD","file":"helper.js","sourcesContent":["import { REGEXP, IMAGE_SPLIT } from \"./constant\";\r\nimport emoji, { prefixUrl } from \"./emoji\";\r\n\r\nconst emojiObejct = arrayToObject(emoji, \"title\");\r\n\r\nexport function isFunction(functionToCheck) {\r\n return (\r\n functionToCheck && {}.toString.call(functionToCheck) === \"[object Function]\"\r\n );\r\n}\r\n\r\nexport function isUrl(userInput) {\r\n const regexp = /(http(s)?:\\/\\/.)?(www\\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/g;\r\n var res = userInput.match(regexp);\r\n if (res === null) return false;\r\n else return true;\r\n}\r\n\r\n/**\r\n * 将对象数组转换为对象\r\n * @param {array} array Array of Objects\r\n * @param {string} keyField string\r\n */\r\nexport function arrayToObject(array, keyField) {\r\n return array.reduce((obj, item) => {\r\n obj[item[keyField]] = item;\r\n return obj;\r\n }, {});\r\n}\r\n\r\n/**\r\n * HTML 编码\r\n * 将 < > 等字符串进行编码\r\n * @param {string} str 文本\r\n */\r\nexport function htmlEncode(str) {\r\n if (!str) return \"\";\r\n return str.replace(/[<>]/gim, function(i) {\r\n return \"&#\" + i.charCodeAt(0) + \";\";\r\n });\r\n}\r\n\r\n/**\r\n * 渲染编辑器\r\n * [x] => <img src=\"x\" />\r\n * @param {strig} content\r\n */\r\nexport function renderContent(content, onClick) {\r\n let newContent = content;\r\n if (newContent.indexOf(IMAGE_SPLIT) !== -1) {\r\n newContent = newContent.split(IMAGE_SPLIT);\r\n newContent.pop();\r\n newContent = newContent.join(\"\");\r\n }\r\n\r\n return htmlEncode(newContent)\r\n .replace(REGEXP, function(a, b) {\r\n const src = a.slice(1, -1);\r\n\r\n // 兼容旧的评\r\n // 因为旧的评论用 [img url] 方式存储的\r\n if (isUrl(src)) {\r\n return `<br/><img src=\"${src}\" alt=\"${src}\" style=\"max-width: 100%\" />`;\r\n }\r\n // 如果不存在对应的表情, 则返回原文\r\n const emoji = emojiObejct[src];\r\n if (emoji) {\r\n return `<img src=\"${prefixUrl}${emoji.value}.${emoji.ext}\" alt=\"${\r\n emoji.title\r\n }\" />`;\r\n }\r\n return `[${src}]`;\r\n })\r\n .replace(/\\n/g, \"<br />\");\r\n}\r\n"]}