term_input.html 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <!--键入[[显示术语下拉菜单-->
  2. <html>
  3. <body>
  4. <style>
  5. .text_input>.textarea{
  6. padding: 5px;
  7. font-family: inherit;
  8. width: 100%;
  9. height: 100px;
  10. resize: vertical;
  11. font-size: 14px;
  12. line-height: 1;
  13. border: 1px solid #ddd;
  14. white-space: pre-wrap;
  15. word-break: break-all;
  16. z-index: 1;
  17. resize: vertical;
  18. }
  19. .text_input>.text_shadow{
  20. position: absolute;
  21. width: 100%;
  22. visibility: hidden;
  23. }
  24. .text_input .cursor{
  25. position: absolute;
  26. border-left: 1px solid #000;
  27. }
  28. .text_input>.menu{
  29. width: 200px;
  30. height:300px;
  31. background-color: aqua;
  32. box-shadow: #000;
  33. position:absolute;
  34. display: none;
  35. z-index: 100;
  36. }
  37. .text_input>.menu ul{
  38. list-style-type: none;
  39. margin: 0;
  40. padding: 0;
  41. }
  42. .text_input>.menu ul li{
  43. cursor: pointer;
  44. }
  45. .text_input>.menu ul li:hover{
  46. background-color: wheat;
  47. }
  48. .text_input>.menu .focus{
  49. color:red;
  50. }
  51. .term_at_menu_input{
  52. height:1em;
  53. border: 1px solid gray;
  54. }
  55. .text_input{
  56. width:100%;
  57. position: relative;
  58. }
  59. </style>
  60. <h2>术语输入测试</h2>
  61. <div id="kk" style="width:50%;"></div>
  62. <h2>术语输入测试</h2>
  63. <div id="mm" style="width:60%;"></div>
  64. <script>
  65. var menuFocusIndex=0;
  66. var term_data=["amanussa","anadhiṭṭhita","anantarāya","anissaṭṭha","aniyata","antaravāsaka"];
  67. var term_input_text ;
  68. var term_input="";
  69. document.querySelector("#kk").innerHTML = term_render_text_input("test1 http://aa.org/?book=1&para=1");
  70. document.querySelector("#mm").innerHTML = term_render_text_input("test1 http://aa.org/?book=1&para=1");
  71. function term_render_text_input(text){
  72. let html ="";
  73. html += '<div class="text_input" >';
  74. html += '<div class="menu"></div>';
  75. html += '<div class="textarea text_shadow"></div>';
  76. html += '<textarea class="textarea tran_sent_textarea" onfocus="text_input_textarea_focuse(this)">';
  77. html += text;
  78. html += '</textarea> ';
  79. html += '</div>';
  80. return html;
  81. }
  82. function term_set_word_list_data(el){
  83. term_data=["amanussa","anadhiṭṭhita","anantarāya","anissaṭṭha","aniyata","antaravāsaka"];
  84. }
  85. function text_input_textarea_focuse(el){
  86. term_set_word_list_data(el);
  87. term_input_text = el;
  88. term_input_text.onresize = function(){
  89. term_input_text.parentElement.querySelector(".text_shadow").style.height=term_input_text.style.height;
  90. }
  91. term_input_text.onkeydown = function (e) {
  92. term_input_text.style.height = term_input_text.parentElement.querySelector(".text_shadow").scrollHeight
  93. let menu = term_input_text.parentElement.querySelector('.menu');
  94. switch (e.key) {
  95. case "ArrowDown"://down arrow
  96. if(menu.style.display=="block"){
  97. menuFocusIndex++;
  98. if(menuFocusIndex>5){
  99. menuFocusIndex=5;
  100. }
  101. menu.innerHTML=TermAtRenderMenu({focus:menuFocusIndex});
  102. return false;
  103. }
  104. break;
  105. case "ArrowUp"://up arrow
  106. if(menu.style.display=="block"){
  107. menuFocusIndex--;
  108. if(menuFocusIndex<0){
  109. menuFocusIndex=0;
  110. }
  111. menu.innerHTML=TermAtRenderMenu({focus:menuFocusIndex});
  112. return false;
  113. }
  114. break;
  115. case "Enter":
  116. if(menu.style.display=="block"){
  117. term_insert(term_data[menuFocusIndex]);
  118. return false;
  119. }
  120. /*
  121. else{
  122. if(!e.shiftKey){
  123. return false;
  124. }
  125. }*/
  126. break;
  127. case "Escape":
  128. break;
  129. case "ArrowLeft":
  130. if(menu.style.display=="block"){
  131. return false;
  132. }
  133. break;
  134. case "ArrowRight":
  135. if(menu.style.display=="block"){
  136. return false;
  137. }
  138. break;
  139. case "Backspace":
  140. if(menu.style.display=="block"){
  141. if(term_input.length>0){
  142. term_input = term_input.slice(0,-1);
  143. menu.innerHTML=TermAtRenderMenu({focus:menuFocusIndex});
  144. }
  145. return false;
  146. }
  147. break;
  148. default:
  149. if(menu.style.display=="block"){
  150. term_input += e.key;
  151. menu.innerHTML=TermAtRenderMenu({focus:menuFocusIndex});
  152. return false;
  153. }
  154. break;
  155. }
  156. }
  157. term_input_text.onkeyup = function (e) {
  158. let value = term_input_text.value
  159. let selectionStart = term_input_text.selectionStart
  160. let str1 = value.slice(0, selectionStart)
  161. let str2 = value.slice(selectionStart)
  162. let textNode1 = document.createTextNode(str1)
  163. let textNode2 = document.createTextNode(str2)
  164. let cursor = document.createElement('span')
  165. cursor.innerHTML = '&nbsp;'
  166. cursor.setAttribute('class','cursor')
  167. let mirror = term_input_text.parentElement.querySelector('.text_shadow')
  168. mirror.innerHTML = ''
  169. mirror.appendChild(textNode1)
  170. mirror.appendChild(cursor)
  171. mirror.appendChild(textNode2)
  172. let menu = term_input_text.parentElement.querySelector('.menu');
  173. if(str1.slice(-2)=="[[" && menu.style.display!="block"){
  174. menuFocusIndex=0;
  175. menu.innerHTML=TermAtRenderMenu({focus:0});
  176. menu.style.display="block";
  177. menu.style.top=cursor.offsetTop+14;
  178. menu.style.left=cursor.offsetLeft;
  179. }
  180. if(e.key=="Escape"){
  181. term_at_menu_hide();
  182. }
  183. console.log(e);
  184. console.log(cursor.offsetLeft,cursor.offsetTop)
  185. }
  186. }
  187. function term_at_menu_hide(){
  188. let menu = term_input_text.parentElement.querySelector('.menu');
  189. menu.style.display="none";
  190. term_input="";
  191. }
  192. function term_insert(strTerm){
  193. let value = term_input_text.value;
  194. let selectionStart = term_input_text.selectionStart;
  195. let str1 = value.slice(0, selectionStart)
  196. let str2 = value.slice(selectionStart);
  197. //TODO 光标会跑到最下面
  198. term_input_text.value = str1+strTerm+"]]"+str2;
  199. term_at_menu_hide();
  200. }
  201. function TermAtRenderMenu(params) {
  202. let html="";
  203. html +="<div class='term_at_menu_input'>"+term_input+"|</div>";
  204. html +="<ul class='term_at_menu_ul'>";
  205. let index=0;
  206. let focusIndex = params.focus%term_data.length;
  207. for (const it of term_data) {
  208. if(term_input=="" || it.indexOf(term_input)!=-1){
  209. html +="<li ";
  210. if(focusIndex==index){
  211. html +="class='focus' "
  212. }
  213. html += "onclick=\"term_insert('"+it+"')\" ";
  214. index++;
  215. html +=">"+index+ ". "+it+"<li>";
  216. }
  217. }
  218. return html;
  219. }
  220. /*
  221. // element: t.refs.textarea
  222. function getCaretPosition(element) {
  223. let left;
  224. let top;
  225. if (document.selection) {
  226. element.focus();
  227. const range = document.selection.createRange();
  228. left = range.boundingLeft + element.scrollLeft;
  229. top = range.boundingTop + element.scrollTop;
  230. } else {
  231. const SHADOWEDITOR = '__shadow_editor__';
  232. const SHADOWEDITORTEXT = '__shadow_editor_text__';
  233. const SHADOWEDITORCARET = '__shadow_editor_caret__';
  234. const shadowEditor = element[SHADOWEDITOR] || document.createElement('div');
  235. const shadowEditorCaret = element[SHADOWEDITORCARET] || document.createElement('span');
  236. const shadowEditorText = element[SHADOWEDITORTEXT] || document.createElement('span');
  237. let focusOffset = { left: 0, top: 0 };
  238. if (!element[SHADOWEDITOR]) {
  239. // add shadpw element to element's cache
  240. element[SHADOWEDITOR] = shadowEditor;
  241. element[SHADOWEDITORCARET] = shadowEditorCaret;
  242. element[SHADOWEDITORTEXT] = shadowEditorText;
  243. // append shadow to document body
  244. shadowEditor.appendChild(shadowEditorText);
  245. shadowEditor.appendChild(shadowEditorCaret);
  246. document.body.appendChild(shadowEditor);
  247. // set shadow element's style
  248. const style = shadowEditor.style;
  249. const computed = window.getComputedStyle ? getComputedStyle(element) : element.currentStyle; // currentStyle for IE < 9
  250. if (element.nodeName != 'INPUT') {
  251. // only for textarea
  252. style.whiteSpace = 'pre-wrap';
  253. style.wordWrap = 'break-word';
  254. } else {
  255. style.whiteSpace = 'nowrap';
  256. }
  257. style.position = 'absolute';
  258. style.overflow = 'hidden';
  259. style.visibility = 'hidden';
  260. properties.forEach((prop) => {
  261. style[prop] = computed[prop];
  262. });
  263. shadowEditorCaret.textContent = '|';
  264. shadowEditorCaret.style.cssText = 'display:inline-block;width:0;overflow:hidden;word-wrap:break-word;word-break:break-all;';
  265. }
  266. const offset = getElementOffset(element);
  267. shadowEditor.style.top = `${offset.top}px`;
  268. shadowEditor.style.left = `${offset.left}px`;
  269. const index = element.selectionEnd;
  270. const SHADOWEDITORCONTENT = element.value.substring(0, index);
  271. shadowEditorText.textContent = SHADOWEDITORCONTENT;
  272. shadowEditorCaret.style.display = 'inline-block';
  273. try { focusOffset = getElementOffset(shadowEditorCaret); } catch (e) { }
  274. shadowEditorCaret.style.display = 'none';
  275. left = focusOffset.left - element.scrollLeft;
  276. top = focusOffset.top - element.scrollTop;
  277. const winOffset = getScrollOffset();
  278. left -= winOffset.x;
  279. top -= winOffset.y;
  280. }
  281. return {
  282. left,
  283. top,
  284. };
  285. }
  286. */
  287. /*
  288. 如果你只是想获得输入框中光标的 offset 的话, 你可以用从 At.js 剥离出来的 jQuery 插件: Caret.js
  289. 可以是用AT-JS jquery插件来实现你需要的效果:
  290. 下载地址:
  291. http://pan.baidu.com/share/link?shareid=495943&uk=386708086
  292. 使用方法:
  293. $("input[name=message]").atWho("@",{
  294. tpl: "<li id='${uid}' data-value='${name}'>${name} <small>${spacenote}</small></li>",
  295. 'data':friend_list
  296. });
  297. 评论
  298. 但是如果你想做,你题目描述的东西的话,还是建议用插件。 这里有一个不错的jquery插件: http://ichord.github.io/At.js/
  299. 评论
  300. */
  301. </script>
  302. </body>
  303. </html>