jiaqingjiayi-xiaochengxu/甲情_甲意/node_modules/antd-mini/less/RareWordsKeyboard/utils.js

260 lines
9.3 KiB
JavaScript
Raw Normal View History

2024-11-15 03:51:28 +00:00
import { __assign, __awaiter, __generator, __spreadArray } from "tslib";
import { ZDATAS } from './zdatas';
import { loadFontFace as loadFontFaceJSAPI } from '../_util/jsapi/load-font-face';
/**
* json转字符串
* @param {string} data 需要转json的字符串
* @return {object} json 字符串
*/
export function safeJSONparse(data) {
var result;
try {
result = JSON.parse(data);
}
catch (_a) {
result = {};
}
return result || {};
}
/**
* 判断数组是否为空
*/
export function isWordsDataEmpty(arr) {
var _a;
if (!arr)
return true;
if (!Array.isArray(arr))
return true;
if (arr.length === 0)
return true;
// 数据合法性校验
if (!((_a = arr === null || arr === void 0 ? void 0 : arr[0]) === null || _a === void 0 ? void 0 : _a.charId))
return true;
return false;
}
/**
* 清除字符串里的数字
*/
export function clearNumberInStr(str) {
return str.replace(/[0-9]/gi, '');
}
/**
* 格式化字库数据
* @param datas ZDatas 数据
* @return {IWordsData} 字库
*/
export function formatZDatas(datas) {
if (datas === void 0) { datas = []; }
return datas.map(function (item) {
return __assign(__assign({}, item), { pinYinChars: item.pinYinChars.map(function (i) { return i.char; }), splitChars: item.splitChars.map(function (i) { return i.char; }) });
});
}
/**
* 候选字推荐序函数
* 考虑两个维度一个是输入值和生僻字的匹配程度比如你输入YA 雅是完全匹配炎是模糊匹配排列的时候肯定在前面
* 如果除了还有一个两个都是完全匹配这个时候就看哪个字占比高哪个就排在前面
* @param {IWordsData} wordsData 字库数据
* @param {string} inputValue 当前输入的值
* @param {string} filterKey 过滤依据的key值
* @return {IWordsData} 返回符合要求并且排序好的候选项列表
*/
export function matchWordsRecommend(wordsData, inputValue, filterKey) {
if (wordsData === void 0) { wordsData = []; }
if (inputValue === void 0) { inputValue = ''; }
if (filterKey === void 0) { filterKey = 'all'; }
return wordsSorter(wordsFilter(wordsData, inputValue, filterKey), inputValue, filterKey);
}
/**
* 字库过滤只挑选符合要求的候选字
* @param {IWordsData} wordsData 字库数据
* @param {string} inputValue 当前输入的值
* @param {string} filterKey 过滤依据的key值
* @return {IWordsData} 返回符合要求并且排序好的候选项列表
*/
export function wordsFilter(wordsData, inputValue, filterKey) {
if (wordsData === void 0) { wordsData = []; }
if (inputValue === void 0) { inputValue = ''; }
if (filterKey === void 0) { filterKey = 'all'; }
// 字库数据为空降级为使用本地数据
if (!wordsData || isWordsDataEmpty(wordsData))
wordsData = formatZDatas(ZDATAS.datas);
if (!inputValue)
return [];
switch (filterKey) {
case 'all':
/* eslint-disable-next-line no-case-declarations */
var matchPinyinArr = filterByPinyin(wordsData, inputValue);
/* eslint-disable-next-line no-case-declarations */
var matchSplitArr = filterBySplitWord(wordsData, inputValue);
return mergeMatchWordsArr(matchPinyinArr, matchSplitArr);
case 'pinyin':
return filterByPinyin(wordsData, inputValue);
case 'split':
return filterBySplitWord(wordsData, inputValue);
default:
return [];
break;
}
}
/**
* 根据拼音过滤候选项
* @param {IWordsData} wordsData 字库数据
* @param {string} inputValue 当前输入的值
* @return {IWordsData} 返回符合要求并候选项列表
*/
function filterByPinyin(wordsData, inputValue) {
if (wordsData === void 0) { wordsData = []; }
if (inputValue === void 0) { inputValue = ''; }
var keyTranslate = inputValue.toUpperCase();
return wordsData.filter(function (item) {
var pinYinChars = (item === null || item === void 0 ? void 0 : item.pinYinChars) || [];
if (pinYinChars.length === 0)
return false;
return (pinYinChars.filter(function (pinyinItem) {
return pinyinItem.indexOf(keyTranslate) > -1;
}).length > 0);
});
}
/**
* 根据拆字过滤候选项
* @param {IWordsData} wordsData 字库数据
* @param {string} inputValue 当前输入的值
* @return {IWordsData} 返回符合要求并候选项列表
*/
function filterBySplitWord(wordsData, inputValue) {
if (wordsData === void 0) { wordsData = []; }
if (inputValue === void 0) { inputValue = ''; }
return wordsData.filter(function (item) {
var splitChars = item.splitChars || [];
if (splitChars.length === 0) {
return false;
}
return (splitChars.filter(function (splitItem) {
return splitItem.indexOf(inputValue) > -1;
}).length > 0);
});
}
/**
* 合并多个候选项数组
* @param {IWordsData} pinyinMatchArr 拼音匹配的候选项
* @param {IWordsData} splitMatchArr 拼音匹配的候选项
* @return {IWordsData} 返回合并后的候选项列表
*/
function mergeMatchWordsArr(pinyinMatchArr, splitMatchArr) {
var unDuplicate = __spreadArray(__spreadArray([], pinyinMatchArr, true), splitMatchArr, true);
if (unDuplicate.length === 0)
return unDuplicate;
var results = [];
unDuplicate.forEach(function (item) {
var findDuplicateWords = results.filter(function (item2) {
return item.unicodeCodePoint === item2.unicodeCodePoint;
});
if (findDuplicateWords.length === 0)
results.push(item);
});
return results;
}
/**
* 候选项排序用户选择可能性高的候选项排在前面
* @param {IWordsData} wordsData 字库数据
* @param {string} inputValue 当前输入的值
* @param {string} filterKey 过滤依据的key值
* @return {IWordsData} 返回符合要求并且排序好的候选项列表
*/
export function wordsSorter(wordsData, inputValue, filterKey) {
if (filterKey === void 0) { filterKey = 'all'; }
switch (filterKey) {
case 'all':
// 当输入值以字母开头使用拼音排序
if (/^[a-zA-Z0-9]+$/.test(inputValue)) {
return sortByPinyin(wordsData, inputValue);
}
return sortBySplitWord(wordsData, inputValue);
case 'pinyin':
return sortByPinyin(wordsData, inputValue);
case 'split':
return sortBySplitWord(wordsData, inputValue);
default:
return [];
break;
}
}
/**
* 根据拼音给候选项排序
* @param {IWordsData} wordsData 字库数据
* @param {string} inputValue 当前输入的值
* @return {IWordsData} 返回符合要求并候选项列表
*/
function sortByPinyin(wordsData, inputValue) {
if (wordsData === void 0) { wordsData = []; }
if (inputValue === void 0) { inputValue = ''; }
var arr = wordsData.slice();
// 清除输入值中的数字
var keyTranslate = clearNumberInStr(inputValue.toUpperCase());
arr.forEach(function (item) {
var sort = 0;
var pinYinChars = (item.pinYinChars || []).map(function (pinyin) {
return clearNumberInStr(pinyin.toUpperCase());
});
// 拼音完全匹配 + 10000
if (pinYinChars.indexOf(keyTranslate) > -1)
sort += 10000;
// 拼音模糊匹配 + 5000
if (pinYinChars.filter(function (splitKey) { return splitKey.indexOf(keyTranslate) === 0; })
.length > 0) {
sort += 5000;
}
// 加上当前字的权重
sort += item.weight || 0;
/* eslint-disable no-param-reassign */
item.sort = sort;
});
// 根据最终排序值排序
arr.sort(function (item1, item2) { return (item2.sort || 0) - (item1.sort || 0); });
return arr;
}
/**
* 根据拆字给候选项排序
* @param {IWordsData} wordsData 字库数据
* @param {string} inputValue 当前输入的值
* @return {IWordsData} 返回符合要求并候选项列表
*/
function sortBySplitWord(wordsData, inputValue) {
if (wordsData === void 0) { wordsData = []; }
if (inputValue === void 0) { inputValue = ''; }
var arr = wordsData.slice();
arr.forEach(function (item) {
var sort = 0;
var p = item.splitChars || [];
// 拆字完全匹配 + 10000
if (p.indexOf(inputValue) > -1)
sort += 10000;
// 拆字模糊匹配 + 5000
if (p.filter(function (splitKey) { return splitKey.indexOf(inputValue) === 0; }).length > 0) {
sort += 5000;
}
// 加上当前字的权重
sort += item.weight || 0;
/* eslint-disable no-param-reassign */
item.sort = sort;
});
// 根据最终排序值排序
arr.sort(function (item1, item2) { return (item2.sort || 0) - (item1.sort || 0); });
return arr;
}
/**
* 加载远程字体
*/
export function loadFontFace() {
return __awaiter(this, void 0, void 0, function () {
var fontName;
return __generator(this, function (_a) {
fontName = "url(\"".concat(ZDATAS.fontUrl, "\")");
return [2 /*return*/, loadFontFaceJSAPI({
family: 'rare-words-font',
source: fontName,
})];
});
});
}