simple-input-method.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /**
  2. * 简单的JS版输入法,拿来玩玩还而已,没有多大实际使用意义
  3. * simple-input-method.js
  4. */
  5. var SimpleInputMethod =
  6. {
  7. hanzi: '', // 候选汉字
  8. pinyin: '', // 候选拼音
  9. result: [], // 当前匹配到的汉字集合
  10. pageCurrent: 1, // 当前页
  11. pageSize: 5, // 每页大小
  12. pageCount: 0, // 总页数
  13. /**
  14. * 初始化字典配置
  15. */
  16. initDict: function()
  17. {
  18. var dict = pinyinUtil.dict;
  19. if(!dict.py2hz) throw '未找到合适的字典文件!';
  20. // 这一步仅仅是给字母a-z扩充,例如根据b找不到相关汉字,就把bi的结果赋值给b
  21. // 当然这种方式只是很简单的实现,真正的拼音输入法肯定不能这么简单处理
  22. dict.py2hz2 = {};
  23. dict.py2hz2['i'] = 'i'; // i比较特殊,没有符合的汉字,所以特殊处理
  24. for(var i=97; i<=123; i++)
  25. {
  26. var ch = String.fromCharCode(i);
  27. if(!dict.py2hz[ch])
  28. {
  29. for(var j in dict.py2hz)
  30. {
  31. if(j.indexOf(ch) == 0)
  32. {
  33. dict.py2hz2[ch] = dict.py2hz[j];
  34. break;
  35. }
  36. }
  37. }
  38. }
  39. },
  40. /**
  41. * 初始化DOM结构
  42. */
  43. initDom: function()
  44. {
  45. var temp = `<div class="pinyin"></div>
  46. <div class="result">
  47. <ol></ol>
  48. <div class="page-up-down"><span class="page-up">▲</span><span class="page-down">▼</span></div>
  49. </div>`;
  50. var dom = document.createElement('div');
  51. dom.id = 'simle_input_method';
  52. dom.className = 'simple-input-method';
  53. dom.innerHTML = temp;
  54. var that = this;
  55. // 初始化汉字选择和翻页键的点击事件
  56. dom.addEventListener('click', function(e)
  57. {
  58. var target = e.target;
  59. if(target.nodeName == 'LI') that.selectHanzi(parseInt(target.dataset.idx));
  60. else if(target.nodeName == 'SPAN')
  61. {
  62. if(target.className == 'page-up' && that.pageCurrent > 1)
  63. {
  64. that.pageCurrent--;
  65. that.refreshPage();
  66. }
  67. else if(target.className == 'page-down' && that.pageCurrent < that.pageCount)
  68. {
  69. that.pageCurrent++;
  70. that.refreshPage();
  71. }
  72. }
  73. })
  74. document.body.appendChild(dom);
  75. },
  76. /**
  77. * 初始化
  78. */
  79. init: function(selector)
  80. {
  81. this.initDict();
  82. this.initDom();
  83. obj = document.querySelectorAll(selector);
  84. this._target = document.querySelector('#simle_input_method');
  85. this._pinyinTarget = document.querySelector('#simle_input_method .pinyin');
  86. this._resultTarget = document.querySelector('#simle_input_method .result ol');
  87. var that = this;
  88. for(var i=0; i<obj.length; i++)
  89. {
  90. obj[i].addEventListener('keydown', function(e)
  91. {
  92. var keyCode = e.keyCode;
  93. var preventDefault = false;
  94. if(keyCode >= 65 && keyCode <= 90) // A-Z
  95. {
  96. that.addChar(String.fromCharCode(keyCode+32), this);
  97. preventDefault = true;
  98. }
  99. else if(keyCode == 8 && that.pinyin) // 删除键
  100. {
  101. that.delChar();
  102. preventDefault = true;
  103. }
  104. else if(keyCode >= 48 && keyCode <= 57 && !e.shiftKey && that.pinyin) // 1-9
  105. {
  106. that.selectHanzi(keyCode-48);
  107. preventDefault = true;
  108. }
  109. else if(keyCode == 32 && that.pinyin) // 空格
  110. {
  111. that.selectHanzi(1);
  112. preventDefault = true;
  113. }
  114. else if(keyCode == 33 && that.pageCount > 0 && that.pageCurrent > 1) // 上翻页
  115. {
  116. that.pageCurrent--;
  117. that.refreshPage();
  118. preventDefault = true;
  119. }
  120. else if(keyCode == 34 && that.pageCount > 0 && that.pageCurrent < that.pageCount) // 下翻页
  121. {
  122. that.pageCurrent++;
  123. that.refreshPage();
  124. preventDefault = true;
  125. }
  126. if(preventDefault) e.preventDefault();
  127. });
  128. obj[i].addEventListener('focus', function()
  129. {
  130. // 如果选中的不是当前文本框,隐藏输入法
  131. if(that._input !== this) that.hide();
  132. });
  133. }
  134. },
  135. /**
  136. * 单个拼音转单个汉字,例如输入 "a" 返回 "阿啊呵腌嗄吖锕"
  137. */
  138. getSingleHanzi: function(pinyin)
  139. {
  140. return pinyinUtil.dict.py2hz2[pinyin] || pinyinUtil.dict.py2hz[pinyin] || '';
  141. },
  142. /**
  143. * 拼音转汉字
  144. * @param pinyin 需要转换的拼音,如 zhongguo
  145. * @return 返回一个数组,格式类似:[["中","重","种","众","终","钟","忠"], "zhong'guo"]
  146. */
  147. getHanzi: function(pinyin)
  148. {
  149. var result = this.getSingleHanzi(pinyin);
  150. if(result) return [result.split(''), pinyin];
  151. var temp = '';
  152. for(var i=0, len = pinyin.length; i<len; i++)
  153. {
  154. temp += pinyin[i];
  155. result = this.getSingleHanzi(temp);
  156. if(!result) continue;
  157. // flag表示如果当前能匹配到结果、并且往后5个字母不能匹配结果,因为最长可能是5个字母,如 zhuang
  158. var flag = false;
  159. if((i+1) < pinyin.length)
  160. {
  161. for(var j=1, len = pinyin.length; j<=5 && (i+j)<len; j++)
  162. {
  163. if(this.getSingleHanzi(pinyin.substr(0, i+j+1)))
  164. {
  165. flag = true;
  166. break;
  167. }
  168. }
  169. }
  170. if(!flag) return [result.split(''), pinyin.substr(0, i+1) + "'" + pinyin.substr(i+1)];
  171. }
  172. return [[], '']; // 理论上一般不会出现这种情况
  173. },
  174. /**
  175. * 选择某个汉字,i有效值为1-5
  176. */
  177. selectHanzi: function(i)
  178. {
  179. var hz = this.result[(this.pageCurrent - 1) * this.pageSize + i - 1];
  180. if(!hz) return;
  181. this.hanzi += hz;
  182. var idx = this.pinyin.indexOf("'");
  183. if(idx > 0)
  184. {
  185. this.pinyin = this.pinyin.substr(idx+1);
  186. this.refresh();
  187. }
  188. else // 如果没有单引号,表示已经没有候选词了
  189. {
  190. this._input.value += this.hanzi;
  191. this.hide();
  192. }
  193. },
  194. /**
  195. * 将拼音转换成汉字候选词,并显示在界面上
  196. */
  197. refresh: function()
  198. {
  199. var temp = this.getHanzi(this.pinyin.replace(/'/g, ''));
  200. this.result = temp[0];
  201. this.pinyin = temp[1];
  202. var count = this.result.length;
  203. this.pageCurrent = 1;
  204. this.pageCount = Math.ceil(count / this.pageSize);
  205. this._pinyinTarget.innerHTML = this.hanzi + this.pinyin;
  206. this.refreshPage();
  207. },
  208. refreshPage: function()
  209. {
  210. var temp = this.result.slice((this.pageCurrent-1)*this.pageSize, this.pageCurrent*this.pageSize);
  211. var html = '';
  212. var i = 0;
  213. temp.forEach(function(val)
  214. {
  215. html += '<li data-idx="'+(++i)+'">' + val + '</li>';
  216. });
  217. this._target.querySelector('.page-up').style.opacity = this.pageCurrent > 1 ? '1' : '.3';
  218. this._target.querySelector('.page-down').style.opacity = this.pageCurrent < this.pageCount ? '1' : '.3';
  219. this._resultTarget.innerHTML = html;
  220. },
  221. addChar: function(ch, obj)
  222. {
  223. if(this.pinyin.length == 0) // 长度为1,显示输入法
  224. {
  225. this.show(obj);
  226. }
  227. this.pinyin += ch;
  228. this.refresh();
  229. },
  230. delChar: function()
  231. {
  232. if(this.pinyin.length <= 1)
  233. {
  234. this.hide();
  235. return;
  236. }
  237. this.pinyin = this.pinyin.substr(0, this.pinyin.length-1);
  238. this.refresh();
  239. },
  240. show: function(obj)
  241. {
  242. var pos = obj.getBoundingClientRect();
  243. this._target.style.left = pos.left + 'px';
  244. this._target.style.top = pos.top + pos.height + document.body.scrollTop + 'px';
  245. this._input = obj;
  246. this._target.style.display = 'block';
  247. },
  248. hide: function()
  249. {
  250. this.reset();
  251. this._target.style.display = 'none';
  252. },
  253. reset: function()
  254. {
  255. this.hanzi = '';
  256. this.pinyin = '';
  257. this.result = [];
  258. this.pageCurrent = 1;
  259. this.pageCount = 0;
  260. this._pinyinTarget.innerHTML = '';
  261. }
  262. };