index.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 { PINYIN_MAP } from './constants';
  58. import { RareWordsKeyboardProps } from './props';
  59. import { formatZDatas, loadFontFace, matchWordsRecommend } from './utils';
  60. import { ZDATAS } from './zdatas';
  61. import '../_util/assert-component2';
  62. import { useEffect, useEvent, useState } from 'functional-mini/component';
  63. import { useComponentEvent } from '../_util/hooks/useComponentEvent';
  64. import { useEvent as useStableCallback } from '../_util/hooks/useEvent';
  65. import { mountComponent } from '../_util/component';
  66. import { useInstanceBoundingClientRect } from '../_util/hooks/useInstanceBoundingClientRect';
  67. var wordsData = formatZDatas(ZDATAS.datas);
  68. var RareWordsKeyboard = function (props) {
  69. var _a = useState({
  70. showErrorPage: false,
  71. loading: false,
  72. }), fontState = _a[0], setFontState = _a[1];
  73. var _b = useState(0), maxDisplayNum = _b[0], setMaxDisplayNum = _b[1];
  74. var _c = useState({
  75. inputValue: [],
  76. displayStr: '',
  77. matchWordsList: [],
  78. showMoreWords: false, // 是否展示更多候选字
  79. }), keyboardState = _c[0], setKeyboardState = _c[1];
  80. var _d = useComponentEvent(props), triggerEvent = _d.triggerEvent, triggerEventOnly = _d.triggerEventOnly;
  81. var handleHide = useStableCallback(function () {
  82. setKeyboardState({
  83. inputValue: [],
  84. matchWordsList: [],
  85. displayStr: '',
  86. showMoreWords: false,
  87. });
  88. triggerEventOnly('close');
  89. });
  90. // 计算展示值和候选字列表
  91. function computeMatchWords(inputValue) {
  92. var displayStr = Array.isArray(inputValue)
  93. ? inputValue.join('')
  94. : inputValue;
  95. var curMatchWords = matchWordsRecommend(wordsData, displayStr);
  96. return {
  97. displayStr: displayStr,
  98. matchWordsList: curMatchWords,
  99. };
  100. }
  101. function loadFont() {
  102. setFontState(function (pre) {
  103. return __assign(__assign({}, pre), { loading: true });
  104. });
  105. loadFontFace()
  106. .then(function () {
  107. setFontState({ showErrorPage: false, loading: false });
  108. })
  109. .catch(function (err) {
  110. setFontState({ showErrorPage: true, loading: false });
  111. triggerEvent('error', err);
  112. });
  113. }
  114. var getBoundingClientRect = useInstanceBoundingClientRect().getBoundingClientRect;
  115. function computeMaxDisplayNum() {
  116. return __awaiter(this, void 0, void 0, function () {
  117. var _a, singleWords, wordsWrap, maxDisplayNumInOneLine;
  118. return __generator(this, function (_b) {
  119. switch (_b.label) {
  120. case 0: return [4 /*yield*/, Promise.all([
  121. getBoundingClientRect('.ant-rare-words-keyboard-match_words_hidden'),
  122. getBoundingClientRect('.ant-rare-words-keyboard-match_words_inner'),
  123. ])];
  124. case 1:
  125. _a = _b.sent(), singleWords = _a[0], wordsWrap = _a[1];
  126. if (!(wordsWrap === null || wordsWrap === void 0 ? void 0 : wordsWrap.width) || !(singleWords === null || singleWords === void 0 ? void 0 : singleWords.width))
  127. return [2 /*return*/];
  128. maxDisplayNumInOneLine = parseInt(((wordsWrap === null || wordsWrap === void 0 ? void 0 : wordsWrap.width) / (singleWords === null || singleWords === void 0 ? void 0 : singleWords.width)).toString(), 10);
  129. setMaxDisplayNum(maxDisplayNumInOneLine);
  130. return [2 /*return*/];
  131. }
  132. });
  133. });
  134. }
  135. useEffect(function () {
  136. loadFont();
  137. computeMaxDisplayNum();
  138. }, []);
  139. useEvent('handleRetry', function () {
  140. loadFont();
  141. });
  142. useEvent('handleWordClick', function (e) {
  143. var _a = e.currentTarget.dataset.value, value = _a === void 0 ? '' : _a;
  144. if (!value)
  145. return;
  146. triggerEvent('change', value);
  147. handleHide();
  148. });
  149. useEvent('hanleLookMore', function () {
  150. if (keyboardState.matchWordsList.length <= maxDisplayNum) {
  151. handleHide();
  152. return;
  153. }
  154. setKeyboardState(function (old) {
  155. return __assign(__assign({}, old), { showMoreWords: !old.showMoreWords });
  156. });
  157. });
  158. // 隐藏键盘,失去焦点
  159. useEvent('handleHide', function () {
  160. handleHide();
  161. });
  162. // 点击键盘key值
  163. useEvent('handleKeyClick', function (e) {
  164. if (fontState.loading) {
  165. return;
  166. }
  167. var _a = e.target.dataset.value, value = _a === void 0 ? '' : _a;
  168. if (!value) {
  169. return;
  170. }
  171. setKeyboardState(function (old) {
  172. var inputValue = __spreadArray(__spreadArray([], old.inputValue, true), [value], false);
  173. return __assign(__assign(__assign({}, old), { inputValue: inputValue }), computeMatchWords(inputValue));
  174. });
  175. });
  176. // 点击删除
  177. useEvent('handleDelete', function () {
  178. if (keyboardState.inputValue.length === 0) {
  179. return;
  180. }
  181. setKeyboardState(function (old) {
  182. var inputValue = __spreadArray([], old.inputValue, true);
  183. inputValue.pop();
  184. return __assign(__assign(__assign({}, old), { inputValue: __spreadArray([], inputValue, true) }), computeMatchWords(inputValue));
  185. });
  186. });
  187. return __assign(__assign(__assign(__assign({}, fontState), { maxDisplayNum: maxDisplayNum }), keyboardState), { pinyinMaps: PINYIN_MAP });
  188. };
  189. mountComponent(RareWordsKeyboard, RareWordsKeyboardProps);