index.jsx 14 KB

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