utils.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. var __assign = (this && this.__assign) || function () {
  2. __assign = Object.assign || function(t) {
  3. for (var s, i = 1, n = arguments.length; i < n; i++) {
  4. s = arguments[i];
  5. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  6. t[p] = s[p];
  7. }
  8. return t;
  9. };
  10. return __assign.apply(this, arguments);
  11. };
  12. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  13. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  14. return new (P || (P = Promise))(function (resolve, reject) {
  15. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  16. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  17. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  18. step((generator = generator.apply(thisArg, _arguments || [])).next());
  19. });
  20. };
  21. var __generator = (this && this.__generator) || function (thisArg, body) {
  22. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  23. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  24. function verb(n) { return function (v) { return step([n, v]); }; }
  25. function step(op) {
  26. if (f) throw new TypeError("Generator is already executing.");
  27. while (g && (g = 0, op[0] && (_ = 0)), _) try {
  28. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  29. if (y = 0, t) op = [op[0] & 2, t.value];
  30. switch (op[0]) {
  31. case 0: case 1: t = op; break;
  32. case 4: _.label++; return { value: op[1], done: false };
  33. case 5: _.label++; y = op[1]; op = [0]; continue;
  34. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  35. default:
  36. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  37. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  38. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  39. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  40. if (t[2]) _.ops.pop();
  41. _.trys.pop(); continue;
  42. }
  43. op = body.call(thisArg, _);
  44. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  45. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  46. }
  47. };
  48. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  49. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  50. if (ar || !(i in from)) {
  51. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  52. ar[i] = from[i];
  53. }
  54. }
  55. return to.concat(ar || Array.prototype.slice.call(from));
  56. };
  57. import { ZDATAS } from './zdatas';
  58. import { loadFontFace as loadFontFaceJSAPI } from '../_util/jsapi/load-font-face';
  59. /**
  60. * json转字符串
  61. * @param {string} data 需要转json的字符串
  62. * @return {object} json 字符串
  63. */
  64. export function safeJSONparse(data) {
  65. var result;
  66. try {
  67. result = JSON.parse(data);
  68. }
  69. catch (_a) {
  70. result = {};
  71. }
  72. return result || {};
  73. }
  74. /**
  75. * 判断数组是否为空
  76. */
  77. export function isWordsDataEmpty(arr) {
  78. var _a;
  79. if (!arr)
  80. return true;
  81. if (!Array.isArray(arr))
  82. return true;
  83. if (arr.length === 0)
  84. return true;
  85. // 数据合法性校验
  86. if (!((_a = arr === null || arr === void 0 ? void 0 : arr[0]) === null || _a === void 0 ? void 0 : _a.charId))
  87. return true;
  88. return false;
  89. }
  90. /**
  91. * 清除字符串里的数字
  92. */
  93. export function clearNumberInStr(str) {
  94. return str.replace(/[0-9]/gi, '');
  95. }
  96. /**
  97. * 格式化字库数据
  98. * @param datas ZDatas 数据
  99. * @return {IWordsData} 字库
  100. */
  101. export function formatZDatas(datas) {
  102. if (datas === void 0) { datas = []; }
  103. return datas.map(function (item) {
  104. return __assign(__assign({}, item), { pinYinChars: item.pinYinChars.map(function (i) { return i.char; }), splitChars: item.splitChars.map(function (i) { return i.char; }) });
  105. });
  106. }
  107. /**
  108. * 候选字推荐序函数
  109. * 考虑两个维度,一个是输入值和生僻字的匹配程度,比如你输入YA 雅是完全匹配,炎是模糊匹配,排列的时候肯定”雅“在前面,
  110. * 如果除了”雅“还有一个”亚“,两个都是完全匹配,这个时候就看哪个字占比高,哪个就排在前面
  111. * @param {IWordsData} wordsData 字库数据
  112. * @param {string} inputValue 当前输入的值
  113. * @param {string} filterKey 过滤依据的key值
  114. * @return {IWordsData} 返回符合要求并且排序好的候选项列表
  115. */
  116. export function matchWordsRecommend(wordsData, inputValue, filterKey) {
  117. if (wordsData === void 0) { wordsData = []; }
  118. if (inputValue === void 0) { inputValue = ''; }
  119. if (filterKey === void 0) { filterKey = 'all'; }
  120. return wordsSorter(wordsFilter(wordsData, inputValue, filterKey), inputValue, filterKey);
  121. }
  122. /**
  123. * 字库过滤,只挑选符合要求的候选字
  124. * @param {IWordsData} wordsData 字库数据
  125. * @param {string} inputValue 当前输入的值
  126. * @param {string} filterKey 过滤依据的key值
  127. * @return {IWordsData} 返回符合要求并且排序好的候选项列表
  128. */
  129. export function wordsFilter(wordsData, inputValue, filterKey) {
  130. if (wordsData === void 0) { wordsData = []; }
  131. if (inputValue === void 0) { inputValue = ''; }
  132. if (filterKey === void 0) { filterKey = 'all'; }
  133. // 字库数据为空降级为使用本地数据
  134. if (!wordsData || isWordsDataEmpty(wordsData))
  135. wordsData = formatZDatas(ZDATAS.datas);
  136. if (!inputValue)
  137. return [];
  138. switch (filterKey) {
  139. case 'all':
  140. /* eslint-disable-next-line no-case-declarations */
  141. var matchPinyinArr = filterByPinyin(wordsData, inputValue);
  142. /* eslint-disable-next-line no-case-declarations */
  143. var matchSplitArr = filterBySplitWord(wordsData, inputValue);
  144. return mergeMatchWordsArr(matchPinyinArr, matchSplitArr);
  145. case 'pinyin':
  146. return filterByPinyin(wordsData, inputValue);
  147. case 'split':
  148. return filterBySplitWord(wordsData, inputValue);
  149. default:
  150. return [];
  151. break;
  152. }
  153. }
  154. /**
  155. * 根据拼音过滤候选项
  156. * @param {IWordsData} wordsData 字库数据
  157. * @param {string} inputValue 当前输入的值
  158. * @return {IWordsData} 返回符合要求并候选项列表
  159. */
  160. function filterByPinyin(wordsData, inputValue) {
  161. if (wordsData === void 0) { wordsData = []; }
  162. if (inputValue === void 0) { inputValue = ''; }
  163. var keyTranslate = inputValue.toUpperCase();
  164. return wordsData.filter(function (item) {
  165. var pinYinChars = (item === null || item === void 0 ? void 0 : item.pinYinChars) || [];
  166. if (pinYinChars.length === 0)
  167. return false;
  168. return (pinYinChars.filter(function (pinyinItem) {
  169. return pinyinItem.indexOf(keyTranslate) > -1;
  170. }).length > 0);
  171. });
  172. }
  173. /**
  174. * 根据拆字过滤候选项
  175. * @param {IWordsData} wordsData 字库数据
  176. * @param {string} inputValue 当前输入的值
  177. * @return {IWordsData} 返回符合要求并候选项列表
  178. */
  179. function filterBySplitWord(wordsData, inputValue) {
  180. if (wordsData === void 0) { wordsData = []; }
  181. if (inputValue === void 0) { inputValue = ''; }
  182. return wordsData.filter(function (item) {
  183. var splitChars = item.splitChars || [];
  184. if (splitChars.length === 0) {
  185. return false;
  186. }
  187. return (splitChars.filter(function (splitItem) {
  188. return splitItem.indexOf(inputValue) > -1;
  189. }).length > 0);
  190. });
  191. }
  192. /**
  193. * 合并多个候选项数组
  194. * @param {IWordsData} pinyinMatchArr 拼音匹配的候选项
  195. * @param {IWordsData} splitMatchArr 拼音匹配的候选项
  196. * @return {IWordsData} 返回合并后的候选项列表
  197. */
  198. function mergeMatchWordsArr(pinyinMatchArr, splitMatchArr) {
  199. var unDuplicate = __spreadArray(__spreadArray([], pinyinMatchArr, true), splitMatchArr, true);
  200. if (unDuplicate.length === 0)
  201. return unDuplicate;
  202. var results = [];
  203. unDuplicate.forEach(function (item) {
  204. var findDuplicateWords = results.filter(function (item2) {
  205. return item.unicodeCodePoint === item2.unicodeCodePoint;
  206. });
  207. if (findDuplicateWords.length === 0)
  208. results.push(item);
  209. });
  210. return results;
  211. }
  212. /**
  213. * 候选项排序,用户选择可能性高的候选项排在前面
  214. * @param {IWordsData} wordsData 字库数据
  215. * @param {string} inputValue 当前输入的值
  216. * @param {string} filterKey 过滤依据的key值
  217. * @return {IWordsData} 返回符合要求并且排序好的候选项列表
  218. */
  219. export function wordsSorter(wordsData, inputValue, filterKey) {
  220. if (filterKey === void 0) { filterKey = 'all'; }
  221. switch (filterKey) {
  222. case 'all':
  223. // 当输入值以字母开头使用拼音排序
  224. if (/^[a-zA-Z0-9]+$/.test(inputValue)) {
  225. return sortByPinyin(wordsData, inputValue);
  226. }
  227. return sortBySplitWord(wordsData, inputValue);
  228. case 'pinyin':
  229. return sortByPinyin(wordsData, inputValue);
  230. case 'split':
  231. return sortBySplitWord(wordsData, inputValue);
  232. default:
  233. return [];
  234. break;
  235. }
  236. }
  237. /**
  238. * 根据拼音给候选项排序
  239. * @param {IWordsData} wordsData 字库数据
  240. * @param {string} inputValue 当前输入的值
  241. * @return {IWordsData} 返回符合要求并候选项列表
  242. */
  243. function sortByPinyin(wordsData, inputValue) {
  244. if (wordsData === void 0) { wordsData = []; }
  245. if (inputValue === void 0) { inputValue = ''; }
  246. var arr = wordsData.slice();
  247. // 清除输入值中的数字
  248. var keyTranslate = clearNumberInStr(inputValue.toUpperCase());
  249. arr.forEach(function (item) {
  250. var sort = 0;
  251. var pinYinChars = (item.pinYinChars || []).map(function (pinyin) {
  252. return clearNumberInStr(pinyin.toUpperCase());
  253. });
  254. // 拼音完全匹配 + 10000
  255. if (pinYinChars.indexOf(keyTranslate) > -1)
  256. sort += 10000;
  257. // 拼音模糊匹配 + 5000
  258. if (pinYinChars.filter(function (splitKey) { return splitKey.indexOf(keyTranslate) === 0; })
  259. .length > 0) {
  260. sort += 5000;
  261. }
  262. // 加上当前字的权重
  263. sort += item.weight || 0;
  264. /* eslint-disable no-param-reassign */
  265. item.sort = sort;
  266. });
  267. // 根据最终排序值排序
  268. arr.sort(function (item1, item2) { return (item2.sort || 0) - (item1.sort || 0); });
  269. return arr;
  270. }
  271. /**
  272. * 根据拆字给候选项排序
  273. * @param {IWordsData} wordsData 字库数据
  274. * @param {string} inputValue 当前输入的值
  275. * @return {IWordsData} 返回符合要求并候选项列表
  276. */
  277. function sortBySplitWord(wordsData, inputValue) {
  278. if (wordsData === void 0) { wordsData = []; }
  279. if (inputValue === void 0) { inputValue = ''; }
  280. var arr = wordsData.slice();
  281. arr.forEach(function (item) {
  282. var sort = 0;
  283. var p = item.splitChars || [];
  284. // 拆字完全匹配 + 10000
  285. if (p.indexOf(inputValue) > -1)
  286. sort += 10000;
  287. // 拆字模糊匹配 + 5000
  288. if (p.filter(function (splitKey) { return splitKey.indexOf(inputValue) === 0; }).length > 0) {
  289. sort += 5000;
  290. }
  291. // 加上当前字的权重
  292. sort += item.weight || 0;
  293. /* eslint-disable no-param-reassign */
  294. item.sort = sort;
  295. });
  296. // 根据最终排序值排序
  297. arr.sort(function (item1, item2) { return (item2.sort || 0) - (item1.sort || 0); });
  298. return arr;
  299. }
  300. /**
  301. * 加载远程字体
  302. */
  303. export function loadFontFace() {
  304. return __awaiter(this, void 0, void 0, function () {
  305. var fontName;
  306. return __generator(this, function (_a) {
  307. fontName = "url(\"".concat(ZDATAS.fontUrl, "\")");
  308. return [2 /*return*/, loadFontFaceJSAPI({
  309. family: 'rare-words-font',
  310. source: fontName,
  311. })];
  312. });
  313. });
  314. }