index.jsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. import React,{Component} from 'react';
  2. import style from './index.less';
  3. import config from "@config/index";
  4. import {filterArr,isIE,getPageCoordinate,filterDataArr} from '@utils/tools.js';
  5. import Notify from '../Notify/index.js';
  6. import classNames from 'classnames';
  7. import $ from 'jquery';
  8. /*****
  9. * author:zn@2018-12-10
  10. * 自由文本输入组件
  11. * 接收参数:
  12. * value:默认显示文字
  13. * handleChange:输入变化时触发函数,接收以下参数:
  14. * * boxMark:病例项标识
  15. * * i:标签index
  16. * * text:标签内文字
  17. *
  18. * ****/
  19. class EditableSpan extends Component{
  20. constructor(props){
  21. super(props);
  22. this.state={
  23. timer:null,
  24. clearTimer:null,
  25. oldText:props.value,
  26. labelVal:'', //存放标签原有的值--主诉字数限制用
  27. preVal:'',
  28. index:null,
  29. searchPre:'',
  30. };
  31. this.$span = React.createRef();
  32. this.handleFocus = this.handleFocus.bind(this);
  33. this.onChange = this.onChange.bind(this);
  34. this.handleBlur = this.handleBlur.bind(this);
  35. this.handleKeydown = this.handleKeydown.bind(this);
  36. this.handleKeyup = this.handleKeyup.bind(this);
  37. this.moveEnd = this.moveEnd.bind(this);
  38. this.handleClick = this.handleClick.bind(this);
  39. }
  40. handleFocus(e){
  41. e.stopPropagation();
  42. const {mainSaveText,full,setFocusIndex,i,boxMark,value}= this.props;
  43. let mainText = filterDataArr(mainSaveText);//主诉字数
  44. if(+boxMark==3||+boxMark==4){ //主诉为空,且第一次聚焦其他史查体时提示且不可输入
  45. if(!mainText&&full&&(value===''||value===undefined)){
  46. Notify.error("无法操作,请先输入主诉");
  47. e.target.blur();
  48. return ;
  49. }
  50. }
  51. let text = e.target.innerText || e.target.innerHTML;
  52. setFocusIndex&&setFocusIndex({i,boxMark,dom:this.$span});
  53. this.setState({
  54. labelVal:text,
  55. index:i,
  56. searchPre:text
  57. });
  58. }
  59. onChange(e){
  60. e.stopPropagation();
  61. const {handleChange,boxMark,i,handleSearch,value,mainSaveText,mainIds,handleClear} = this.props;
  62. const {labelVal,searchPre} = this.state;
  63. const text1 = e.target.innerText || e.target.innerHTML;
  64. // e.newValue IE浏览器DOMCharacterDataModified监听
  65. // const text1 = e.target.innerText || e.target.innerHTML || e.newValue;
  66. let mainText = filterDataArr(mainSaveText);//主诉字数
  67. if(+boxMark==1){
  68. if(mainText.length >= config.limited){
  69. if(text1.length > labelVal.length){
  70. e.target.innerText = labelVal;
  71. Notify.info(config.limitText);
  72. return
  73. }else if(text1.length == labelVal.length){
  74. this.setState({
  75. labelVal:text1
  76. });
  77. }else{
  78. handleChange&&handleChange({text1,boxMark,i});
  79. }
  80. return
  81. }
  82. }
  83. this.setState({
  84. labelVal:text1
  85. });
  86. const that = this;
  87. handleChange&&handleChange({text1,boxMark,i});
  88. //延迟搜索
  89. clearTimeout(this.state.timer);
  90. const timer = setTimeout(function(){
  91. let newText = e.target.innerText || e.target.innerHTML;
  92. let temp = '',isEnd=false;
  93. let search='';
  94. clearTimeout(that.state.timer);
  95. temp = newText.replace(searchPre,'');
  96. isEnd = !(newText.indexOf(searchPre)>0);
  97. search = temp.replace(/[(^\s*)|(\s*$)|(^\,*)|(\,*$)]/g,'');
  98. // if(!search&&searchPre){
  99. if(!temp&&searchPre&&newText){
  100. search = searchPre;
  101. }
  102. //console.log(labelVal,'旧:',searchPre,'新:',newText,'搜索:',search);
  103. if(config.punctuationReg.test(search)){ //只有标点符号时不搜索
  104. handleSearch&&handleSearch({text:search,isEnd,boxMark,mainIds});
  105. }else{//只有标点符号时要清空搜索结果
  106. handleClear && handleClear({boxMark})
  107. }
  108. //搜索后保持现在的值,继续输入时要用于对比
  109. /*that.setState({
  110. searchPre:newText
  111. });*/
  112. },config.delayTime);
  113. this.setState({
  114. timer
  115. });
  116. }
  117. handleBlur(e){//为了阻止冒泡事件
  118. const {boxMark,handleClear,handleChange,i} = this.props;
  119. e.stopPropagation();
  120. // 延时清空搜索结果,不延时会影响选中
  121. /*clearTimeout(this.state.clearTimer);
  122. const clearTimer = setTimeout(function(){
  123. handleClear && handleClear({boxMark})
  124. },config.delayTime);
  125. this.setState({
  126. clearTimer
  127. });*/
  128. }
  129. moveEnd(obj) {
  130. if(window.getSelection){//ie11 10 9 ff safari
  131. obj.focus(); //解决ff不获取焦点无法定位问题
  132. var range = window.getSelection();//创建range
  133. range.selectAllChildren(obj);//range 选择obj下所有子内容
  134. range.collapseToEnd();//光标移至最后
  135. }
  136. else if (document.selection) {//ie10 9 8 7 6 5
  137. var range = document.selection.createRange();//创建选择对象
  138. range.moveToElementText(obj);//range定位到obj
  139. range.collapse(false);//光标移至最后
  140. range.select();
  141. }
  142. }
  143. handleKeydown(e){
  144. const ev = e||window.event;
  145. const {i} = this.props;
  146. const target = ev.target||ev.srcElement;
  147. let innerVal = target.innerText || target.innerHTML,ele,boxTop;
  148. //禁止回车事件
  149. if(ev.keyCode==13){return false;}
  150. //backspace事件 和delete
  151. if(ev.keyCode==8 || ev.keyCode==46){
  152. //用于对比backspace前后的值
  153. this.setState({
  154. preVal:innerVal
  155. })
  156. }
  157. let range = window.getSelection();
  158. let textIndex = range.focusOffset;
  159. let textLength = range.anchorNode.length;
  160. if(ev.keyCode==37&& i!=0){//向左
  161. let preObj = $(this.$span.current).prev();
  162. let obj = preObj[0]&&preObj[0].nodeName=="DIV"?preObj.prev():preObj;
  163. if(textIndex == 0){
  164. if(ev.preventDefault){//阻止默认事件
  165. ev.preventDefault();
  166. }else{
  167. ev.returnValue=false;
  168. }
  169. if(obj){
  170. this.moveEnd(obj[0]);
  171. }
  172. }
  173. }
  174. if(ev.keyCode==39){//向右
  175. let nextObj = $(this.$span.current).next();
  176. let obj = nextObj[0]&&nextObj[0].nodeName=="DIV"?nextObj.next():nextObj;
  177. if(textIndex == textLength || textLength==undefined || (textIndex == 0 && textLength==1)){
  178. if(ev.preventDefault){//阻止默认事件
  179. ev.preventDefault();
  180. }else{
  181. ev.returnValue=false;
  182. }
  183. if(obj){
  184. obj.focus();
  185. }
  186. }
  187. }
  188. }
  189. handleKeyup(e){
  190. const {boxMark,handleKeydown,removeId,handleClear,removeSpan} = this.props;
  191. const {preVal,index} = this.state;
  192. const ev = e||window.event;
  193. const target = ev.target||ev.srcElement;
  194. let innerVal = target.innerText || target.innerHTML,ele,boxTop;
  195. // 可编辑div不支持oninput事件,用此事件替代
  196. /*if(isIE() && innerVal != preVal){
  197. this.onChange(ev);
  198. }*/
  199. if(ev.keyCode==46){//delete
  200. //判断nexObj
  201. // let nextObj = $(this.$span.current).next();
  202. let nextObj = $(this.$span.current);
  203. if(preVal.trim().length==1&& !innerVal){
  204. removeId && removeId({boxMark,i:index,text:"",flag:'del'});
  205. handleClear && handleClear({boxMark});//删除最后一个字时清空搜索结果,避免现病史搜索框不立即消失的情况
  206. //如果后一个不是标签,则光标移到最前
  207. if(nextObj && nextObj[0].nodeName !=="DIV"){
  208. nextObj.focus();
  209. }
  210. }
  211. //action里往后删除
  212. if(innerVal == preVal){
  213. let data = innerVal.trim();
  214. if(nextObj && !config.punctuationReg.test(data) || data=='<br>'){
  215. handleKeydown&&handleKeydown({boxMark,i:index,text:data,flag:'del'});
  216. // nextObj.focus();
  217. if(nextObj && nextObj[0] && nextObj[0].nodeName !=="DIV"){
  218. // IE浏览器focus光标在最后,其他浏览器在最前
  219. nextObj.focus();
  220. }
  221. /*this.setState({
  222. index: null
  223. })*/
  224. }
  225. }
  226. }
  227. if(ev.keyCode==8){
  228. // 主诉现病史去重:删除最后一个字的时候移除该数据(将name、id和value替换成空)并移除id
  229. // 前面是标签,内容为空时再删一次才移除标签;前面是文本,则直接移除;
  230. let preObj = $(this.$span.current).prev();
  231. if(index!==0&&preVal.trim().length==1&& !innerVal){
  232. removeId && removeId({boxMark,i:index,text:""});
  233. handleClear && handleClear({boxMark});//删除最后一个字时清空搜索结果,避免现病史搜索框不立即消失的情况
  234. if(preObj[0].nodeName !=="DIV"){
  235. this.moveEnd(preObj[0]);
  236. }
  237. }
  238. if(innerVal !== preVal){
  239. }
  240. else{
  241. // 中英文数字和下划线--单独删除标签
  242. /*const reg = new RegExp("([\u4E00-\uFA29]|[\uE7C7-\uE7F3]|[a-zA-Z0-9_])");
  243. if(index!==0 && reg.test(innerVal)){
  244. let obj = preObj[0].nodeName=="DIV"?preObj.prev():preObj;
  245. delSingleLable && delSingleLable({boxMark,i:index});
  246. this.moveEnd(obj[0]);
  247. this.setState({
  248. index: null
  249. })
  250. }*/
  251. let data = innerVal.trim();
  252. //判断是否为空、中英文:, 。、;,且不是第一位
  253. // let pattern = new RegExp(/^\,?$|^\,?$|^\.?$|^\。?$|^\、?$|^\;?$|^\;?$|^\:?$|^\:?$|\s/);
  254. // if(index!==0 && pattern.test(data)){
  255. // 后半段是处理IE
  256. if(index!==0 && !config.punctuationReg.test(data) || index!==0 && data=='<br>'){
  257. // let preObj = $(this.$span.current).prev();
  258. let obj = preObj[0].nodeName=="DIV"?preObj.prev():preObj;
  259. handleKeydown&&handleKeydown({boxMark,i:index,text:data,flag:'backsp'});
  260. this.moveEnd(obj[0]);
  261. this.setState({
  262. index: null
  263. })
  264. }
  265. }
  266. // 主诉使用模板删除最后一个空span时移除
  267. if(boxMark==1 && index==0 && !innerVal){
  268. removeSpan();
  269. }
  270. }
  271. }
  272. componentWillReceiveProps(next){
  273. const isRead = this.props.isRead;
  274. if(next.isRead != isRead){
  275. this.$span.current.innerText?(this.$span.current.innerText = next.value||''):(this.$span.current.innerHTML = next.value||'');
  276. }
  277. }
  278. handleClick(e){
  279. $(this.$span.current).attr({"contentEditable":true}).focus()
  280. }
  281. componentDidMount(){
  282. const {value} = this.props;
  283. const that = this;
  284. if(value){
  285. this.$span.current.innerText?(this.$span.current.innerText = value||''):(this.$span.current.innerHTML = value||'');
  286. }
  287. if(isIE()){
  288. // 左右移动没问题,但是输入过程中会失焦,并且有时光标在但是无法输入
  289. // $(this.$span.current)[0].addEventListener('DOMCharacterDataModified', function(e){that.onChange(e);}, false);
  290. // 此方法会影响左右切换时光标消失
  291. /*$(this.$span.current).onIe8Input(function(e){
  292. this.onChange(e)
  293. },this);*/
  294. }
  295. }
  296. /*cancelSelect(e){//双击不选中
  297. if(document.selection&&document.selection.empty){
  298. document.selection.empty();
  299. }else if (window.getSelection) {
  300. var sel=window.getSelection();
  301. sel.removeAllRanges();
  302. }
  303. }*/
  304. getClass(){
  305. const {full,value,saveText,i} = this.props;
  306. const preSelected = saveText[i-1];
  307. const isFull = full?' '+style['full']:''; //是否宽度设为整行宽度
  308. //有标点符号之外的字符或者前一个标签有选中值时,显示为黑色,否则灰显
  309. const unselect = value.match(config.punctuationReg)||preSelected?'':style['unselect'];
  310. return classNames(style['editable-span'],isFull,unselect);
  311. }
  312. render() {
  313. return <span className={this.getClass()}
  314. contentEditable='true'
  315. ref={this.$span}
  316. onInput={this.onChange}
  317. onFocus={this.handleFocus}
  318. onBlur={this.handleBlur}
  319. onkeydown={this.handleKeydown}
  320. onclick={this.handleClick}
  321. onkeyup={this.handleKeyup}></span>;
  322. }
  323. }
  324. export default EditableSpan;