index.jsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. import React,{Component} from 'react';
  2. import style from './index.less';
  3. import config from "@config/index";
  4. import {filterArr,isIE,getPageCoordinate,filterDataArr,preventDefault,setFontColorSize,moveEnd} 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. let indexNum = '';
  20. class EditableSpan extends Component{
  21. constructor(props){
  22. super(props);
  23. this.state={
  24. timer:null,
  25. clearTimer:null,
  26. oldText:props.value,
  27. labelVal:'', //存放标签原有的值--主诉字数限制用
  28. preVal:'',
  29. index:null,
  30. searchPre:'',
  31. };
  32. this.$span = React.createRef();
  33. this.handleFocus = this.handleFocus.bind(this);
  34. this.onChange = this.onChange.bind(this);
  35. this.handleBlur = this.handleBlur.bind(this);
  36. this.handleKeydown = this.handleKeydown.bind(this);
  37. this.handleKeyup = this.handleKeyup.bind(this);
  38. this.handleClick = this.handleClick.bind(this);
  39. this.selectStart = this.selectStart.bind(this);
  40. this.selectEnd=this.selectEnd.bind(this);
  41. }
  42. handleFocus(e){
  43. e.stopPropagation();
  44. const that = this;
  45. let txt = '';
  46. //黏贴时去掉html格式
  47. $(this.$span.current).on("paste",function(e){
  48. setTimeout(function(){
  49. txt = that.$span.current.innerText||that.$span.current.innerHTML;
  50. that.$span.current.innerHTML = txt;
  51. moveEnd($(that.$span.current)[0]); //光标落到最后去
  52. });
  53. })
  54. const {mainSaveText,full,setFocusIndex,i,boxMark,value}= this.props;
  55. let mainText = filterDataArr(mainSaveText);//主诉字数
  56. if(+boxMark==3||+boxMark==4){ //主诉为空,且第一次聚焦其他史查体时提示且不可输入
  57. if(!mainText&&full&&(value===''||value===undefined)){
  58. Notify.error("无法操作,请先输入主诉");
  59. e.target.blur();
  60. return ;
  61. }
  62. }
  63. let text = e.target.innerText || e.target.innerHTML;
  64. setFocusIndex&&setFocusIndex({i,boxMark,dom:this.$span});
  65. this.setState({
  66. labelVal:text,
  67. index:i,
  68. searchPre:text
  69. });
  70. }
  71. onChange(e){
  72. e.stopPropagation();
  73. const {handleChange,boxMark,i,handleSearch,noSearch,mainSaveText,mainIds,handleClear} = this.props;
  74. const {labelVal,searchPre} = this.state;
  75. const text1 = e.target.innerText || e.target.innerHTML;
  76. // e.newValue IE浏览器DOMCharacterDataModified监听
  77. // const text1 = e.target.innerText || e.target.innerHTML || e.newValue;
  78. let mainText = filterDataArr(mainSaveText);//主诉字数
  79. if(+boxMark==1){
  80. if(mainText.length >= config.limited){
  81. if(text1.length > labelVal.length){
  82. e.target.innerHTML = labelVal; //innerHTML兼容FF26
  83. e.target.blur();
  84. Notify.info(config.limitText);
  85. return
  86. }else if(text1.length == labelVal.length){
  87. this.setState({
  88. labelVal:text1
  89. });
  90. }else{
  91. handleChange&&handleChange({text1,boxMark,i});
  92. }
  93. return
  94. }
  95. }
  96. this.setState({
  97. labelVal:text1
  98. });
  99. const that = this;
  100. handleChange&&handleChange({text1,boxMark,i});
  101. if(noSearch){
  102. return;
  103. }
  104. //延迟搜索
  105. clearTimeout(this.state.timer);
  106. const timer = setTimeout(function(){
  107. let newText = e.target.innerText || e.target.innerHTML;
  108. let temp = '',isEnd=false;
  109. let search='';
  110. clearTimeout(that.state.timer);
  111. temp = newText.replace(searchPre,'');
  112. isEnd = !(newText.indexOf(searchPre)>0);
  113. search = temp.replace(config.regPreAndAft,'');
  114. if(!temp&&searchPre&&newText){
  115. search = searchPre;
  116. }
  117. if(config.punctuationReg.test(search)){ //只有标点符号时不搜索
  118. handleSearch&&handleSearch({text:search,isEnd,boxMark,mainIds});
  119. }else{//只有标点符号时要清空搜索结果
  120. handleClear && handleClear({boxMark})
  121. }
  122. //搜索后保持现在的值,继续输入时要用于对比
  123. /*that.setState({
  124. searchPre:newText
  125. });*/
  126. },config.delayTime);
  127. this.setState({
  128. timer
  129. });
  130. }
  131. handleBlur(e){//为了阻止冒泡事件
  132. e.stopPropagation();
  133. //解除绑定事件
  134. $(this.$span.current).off("paste");
  135. }
  136. handleKeydown(e){
  137. const ev = e||window.event;
  138. const {i} = this.props;
  139. const target = ev.target||ev.srcElement;
  140. let innerVal = target.innerText || target.innerHTML,ele,boxTop;
  141. //禁止回车事件
  142. if(ev.keyCode==13){return false;}
  143. //backspace事件 和delete
  144. if(ev.keyCode==8 || ev.keyCode==46){
  145. //用于对比backspace前后的值
  146. this.setState({
  147. preVal:innerVal
  148. })
  149. }
  150. let range = window.getSelection();
  151. let textIndex = range.focusOffset;
  152. let textLength = range.anchorNode&&range.anchorNode.length;
  153. let preObj = $(this.$span.current).prev();
  154. if(ev.keyCode==37){//向左
  155. let preObj = $(this.$span.current).prev();
  156. let obj = preObj[0]&&preObj[0].nodeName=="DIV"?preObj.prev():preObj;
  157. if(textIndex == 0){
  158. preventDefault(ev);
  159. if(obj){
  160. obj[0]&&this.moveEnd(obj[0]);
  161. }
  162. }
  163. }
  164. if(ev.keyCode==39){//向右
  165. let curObj = $(this.$span.current)
  166. let nextObj = $(this.$span.current).next();
  167. let obj = nextObj[0]&&nextObj[0].nodeName=="DIV"?nextObj.next():nextObj;
  168. if(textIndex == textLength || textLength==undefined || (textIndex == 0 && textLength==1)){
  169. preventDefault(ev);
  170. if(obj){
  171. obj[0]&&obj.focus();
  172. }
  173. }
  174. }
  175. }
  176. handleKeyup(e){
  177. const {boxMark,handleKeydown,removeId,handleClear,removeSpan,handleChange,select_end} = this.props;
  178. const {preVal,index} = this.state;
  179. const ev = e||window.event;
  180. const target = ev.target||ev.srcElement;
  181. let innerVal = target.innerText || target.innerHTML,ele,boxTop;
  182. // 可编辑div不支持oninput事件,用此事件替代
  183. /*if(isIE() && innerVal != preVal){
  184. this.onChange(ev);
  185. }*/
  186. if(ev.keyCode==46){//delete
  187. //判断nexObj
  188. // let nextObj = $(this.$span.current).next();
  189. // ||textIndex ==innerVal.length&&preObj[0].nodeName=="DIV"
  190. let range = window.getSelection();
  191. let textIndex = range.focusOffset;
  192. let nextObj = $(this.$span.current);
  193. let nexObj = $(this.$span.current).next();
  194. let nexObjN = nexObj.next();
  195. let preObj = $(this.$span.current).prev();
  196. let nexVal = nexObj[0]&&nexObj[0].innerText || nexObj[0]&&nexObj[0].innerHTML;
  197. if(preVal.trim().length==1&& !innerVal){
  198. removeId && removeId({boxMark,i:index,text:"",flag:'del'});
  199. handleClear && handleClear({boxMark});//删除最后一个字时清空搜索结果,避免现病史搜索框不立即消失的情况
  200. //如果后一个不是标签,则光标移到最前
  201. if(nextObj && nextObj[0].nodeName !=="DIV"){
  202. // nextObj.focus();
  203. this.moveEnd(nextObj[0],1)
  204. }else{
  205. this.moveEnd(preObj[0])
  206. }
  207. }
  208. //action里往后删除
  209. if(innerVal == preVal){
  210. let data = innerVal.trim();
  211. if(nextObj && !config.punctuationReg.test(data) || data=='<br>'){
  212. handleKeydown&&handleKeydown({boxMark,i:index,text:data,flag:'del'});
  213. if(nextObj && nextObj[0] && nextObj[0].nodeName !=="DIV"){
  214. // IE浏览器focus光标在最后,其他浏览器在最前
  215. // nextObj.focus();
  216. if(boxMark==2&&this.props.data[index+1]&&this.props.data[index+1].flag==3){
  217. this.moveEnd(nexObjN[0],1);
  218. }else{
  219. this.moveEnd(nextObj[0],1);
  220. }
  221. }
  222. /*this.setState({
  223. index: null
  224. })*/
  225. }else if(indexNum ==innerVal.length&&nexObj[0]&&nexObj[0].nodeName=="DIV"){//span最后一位后面为div标签
  226. handleKeydown&&handleKeydown({boxMark,i:index+1,text:data,flag:'del'});
  227. this.moveEnd(nextObj[0]);
  228. }else if(nexObj[0]&&nexObj[0].nodeName=="SPAN"){
  229. if(nexVal.length <= 1){//为空或者一个字delete时候
  230. removeId && removeId({boxMark,i:index+1,text:"",flag:'del'});
  231. handleClear && handleClear({boxMark});
  232. this.moveEnd(nexObj[0])
  233. }else if(nexVal.length > 1){
  234. let tmpVal = nexVal.substr(1,data.length)
  235. handleChange&&handleChange({text1:tmpVal,boxMark,i:index+1});
  236. nexObj.html(tmpVal)
  237. this.moveEnd(nexObj[0],1)
  238. }
  239. }
  240. }
  241. }
  242. if(ev.keyCode==8&&!select_end){
  243. // 主诉现病史去重:删除最后一个字的时候移除该数据(将name、id和value替换成空)并移除id
  244. // 前面是标签,内容为空时再删一次才移除标签;前面是文本,则直接移除;
  245. let preObj = $(this.$span.current).prev();
  246. let curObj = $(this.$span.current);
  247. if(index!==0&&preVal.trim().length==1&& !innerVal){
  248. removeId && removeId({boxMark,i:index,text:""});
  249. handleClear && handleClear({boxMark});//删除最后一个字时清空搜索结果,避免现病史搜索框不立即消失的情况
  250. if(preObj[0].nodeName !=="DIV"){
  251. this.moveEnd(preObj[0]);
  252. }
  253. }
  254. let range = window.getSelection();
  255. let textIndex = range.focusOffset;
  256. if(innerVal !== preVal){
  257. }else{
  258. // 中英文数字和下划线--单独删除标签
  259. /*const reg = new RegExp("([\u4E00-\uFA29]|[\uE7C7-\uE7F3]|[a-zA-Z0-9_])");
  260. if(index!==0 && reg.test(innerVal)){
  261. let obj = preObj[0].nodeName=="DIV"?preObj.prev():preObj;
  262. delSingleLable && delSingleLable({boxMark,i:index});
  263. this.moveEnd(obj[0]);
  264. this.setState({
  265. index: null
  266. })
  267. }*/
  268. let data = innerVal.trim();
  269. //判断是否为空、中英文:, 。、;,且不是第一位
  270. // let pattern = new RegExp(/^\,?$|^\,?$|^\.?$|^\。?$|^\、?$|^\;?$|^\;?$|^\:?$|^\:?$|\s/);
  271. // if(index!==0 && pattern.test(data)){
  272. // 后半段是处理IE
  273. if(index!==0 && !config.punctuationReg.test(data) || index!==0 && data=='<br>'||indexNum == 0&&preObj[0]&&preObj[0].nodeName=="DIV"){
  274. // let preObj = $(this.$span.current).prev();
  275. let obj = preObj[0].nodeName=="DIV"?preObj.prev():preObj;
  276. handleKeydown&&handleKeydown({boxMark,i:index,text:data,flag:'backsp'});
  277. this.moveEnd(obj[0]);
  278. this.setState({
  279. index: null
  280. })
  281. }else if(textIndex == 0&&preObj[0]&&preObj[0].nodeName=="SPAN"){
  282. let data = preObj[0].innerText || preObj[0].innerHTML;
  283. if(data.length == 1){//span只有一个文本,删除后移除检索
  284. removeId && removeId({boxMark,i:index-1,text:""});
  285. handleClear && handleClear({boxMark});
  286. if(preObj[0].nodeName !=="DIV"){
  287. if(boxMark==2){
  288. this.moveEnd(preObj[0],1);
  289. }else{
  290. this.moveEnd(preObj[0]);
  291. }
  292. }
  293. }else{
  294. let tmpVal = data.substr(0,data.length-1)
  295. handleChange&&handleChange({text1:tmpVal,boxMark,i:index});
  296. preObj.html(tmpVal)
  297. if(preObj[0].nodeName !=="DIV"){
  298. this.moveEnd(preObj[0]);
  299. }
  300. }
  301. }
  302. }
  303. // 主诉使用模板删除最后一个空span时移除
  304. if(boxMark==1 && index==0 && !innerVal){
  305. removeSpan();
  306. }
  307. }
  308. }
  309. componentWillReceiveProps(next){
  310. const isRead = this.props.isRead;
  311. if(next.isRead != isRead){
  312. this.$span.current.innerText?(this.$span.current.innerText = next.value||''):(this.$span.current.innerHTML = next.value||'');
  313. }
  314. }
  315. handleClick(e){
  316. $(this.$span.current).attr({"contentEditable":true}).focus()
  317. let range = window.getSelection();
  318. let textIndex = range.focusOffset;
  319. indexNum = textIndex
  320. }
  321. componentDidMount(){
  322. const {value} = this.props;
  323. if(value){
  324. this.$span.current.innerText?(this.$span.current.innerText = value||''):(this.$span.current.innerHTML = value||'');
  325. }
  326. }
  327. selectStart(){
  328. const {i,setSelectArea,boxMark}= this.props;
  329. setSelectArea({i,boxMark,dir:'start'});
  330. }
  331. selectEnd(){
  332. const {i,setSelectArea,boxMark}= this.props;
  333. setSelectArea({i,boxMark,dir:'end'});
  334. }
  335. getClass(){
  336. const {full,value,saveText,i,preIsExt,afterIsExt,mouseSelect} = this.props;
  337. const br = preIsExt&&!afterIsExt; //最后一个体征标签
  338. const preSelected = saveText[i-1];
  339. const isFull = full?' '+style['full']:''; //是否宽度设为整行宽度
  340. //有标点符号之外的字符或者前一个标签有选中值时,显示为黑色,否则查体中,有体征标记显示蓝色,否则灰显
  341. const ext = preIsExt?style['ext']:style['unselect'];
  342. const unselect = value.match(config.punctuationReg)||preSelected?'':ext;
  343. const hasBr = br?style['editable-br']:''; //最后一个体征标签换行
  344. const selectedArea = mouseSelect?style['selected-area']:'';
  345. const $span = this.$span.current;
  346. if(br&&$span&&!$span.innerText){
  347. this.$span.current.innerHTML='&nbsp;';
  348. }
  349. return classNames(style['editable-span'],isFull,unselect,hasBr,setFontColorSize(2,6),selectedArea);
  350. }
  351. render() {
  352. return <span className={this.getClass()}
  353. contentEditable='true'
  354. ref={this.$span}
  355. onInput={this.onChange}
  356. onFocus={this.handleFocus}
  357. onBlur={this.handleBlur}
  358. onmousedown={this.selectStart}
  359. onmouseup={this.selectEnd}
  360. onkeydown={this.handleKeydown}
  361. onclick={this.handleClick}
  362. onkeyup={this.handleKeyup}></span>;
  363. }
  364. }
  365. export default EditableSpan;