From 4f3e385f783df39a0a0e7fa51f5a43944db13dd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E6=80=9D?= <2897217417@qq.com> Date: Thu, 21 Oct 2021 10:51:40 +0800 Subject: [PATCH] =?UTF-8?q?at=E8=B0=81=E5=88=97=E8=A1=A8=EF=BC=88=E7=BC=BA?= =?UTF-8?q?=E5=BC=B9=E6=A1=86=E7=9A=84=E4=B8=8A=E4=B8=8B=EF=BC=8Center?= =?UTF-8?q?=E9=94=AE=E7=9B=91=E5=90=AC=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tpm/challengesnew/tpm-md-editor.js | 109 +++++++++++------- 1 file changed, 69 insertions(+), 40 deletions(-) diff --git a/src/modules/tpm/challengesnew/tpm-md-editor.js b/src/modules/tpm/challengesnew/tpm-md-editor.js index f6c4d66df..5579f33b4 100644 --- a/src/modules/tpm/challengesnew/tpm-md-editor.js +++ b/src/modules/tpm/challengesnew/tpm-md-editor.js @@ -118,7 +118,6 @@ export default ({ mdID, onChange, onCMBeforeChange, onCMBlur, error = false, cla //替换最后的内容 cm.replaceRange(username+" ",{line,ch},{line,ch}); //将此user的login存储到atWhoLoginList集合中 - atWhoLoginListState const list = new Set(atWhoLoginList.current); users.map((item)=>{ item.username === username && list.add(item.login); @@ -261,56 +260,30 @@ export default ({ mdID, onChange, onCMBeforeChange, onCMBlur, error = false, cla } else { setAtWhoVisible(false); } - if(atWhoLoginList.current.length != 0){ - console.log('initValue',initValue); + //处理本来@了某人 -> 删掉 -> 撤回 的情况 + if(e.code === "KeyZ" && users.length != 0){ const codemirror = editorInstance.cm; - const value = codemirror.getValue(); + let value = codemirror.getValue(); //处理初始内容就自带@谁的情况 if(initValue){ + const del = []; users.map(item=>{ if(initValue.indexOf(item.username)!=-1 && initValue.charAt(initValue.indexOf(item.username)-1) === "@" && initValue.indexOf(`@${item.username}`)===value.indexOf(`@${item.username}`)){ //初始内容中有符合@+名字的格式并且当前内容未删除初始内容 - value.replace(`@${username}`,""); + del[del.length] = `@${item.username}`; } }) - Array.from(atWhoMap.keys()).map(username=>{ - if(initValue.indexOf(username)!=-1 && value.charAt(value.indexOf(username)-1) === "@"){ - - } + del.length!=0 && del.map(str=>{ + value = value.replace(str,""); }) - console.log('剔除初始内容中的@who',value); } - //以username为主键,login为value的map集合 - let atWhoMap = new Map(); - Array.from(atWhoLoginList.current).map(item=>{ - users.map(i=>{ - if(i.login === item){ - atWhoMap.set(i.username,i.login); - } - }) - }); - if(value.indexOf("@") === -1){ - //已经有要@的列表,但是没有@符号 -> 清空@集合 - atWhoLoginList.current = []; - setAtWhoLoginListState([]); - return; - } - const cursor = codemirror.doc.getCursor(); - const line = cursor.line; - const ch = cursor.ch; - const lineContent = codemirror.getLine(line); - if(lineContent && lineContent.indexOf("@") != -1){//此行有@谁 - Array.from(atWhoMap.keys()).map(username=>{ - //判断lineContent是不是以列表中的某个username结尾 - if(lineContent.endsWith(username)){ - codemirror.setSelection({line,ch:ch-username.length-1},{line,ch}); - return; - } - //处理有名字但是无@符号,有@但是名字对不上的情况 - if(value.indexOf(username)===-1 || value.charAt(value.indexOf(username)-1) !="@"){ - //符合任意一种情况->踢掉这个人 不给他发消息 + //判断value是否包含@符号 + if(value.indexOf("@") != -1){ + users.map(item =>{ + if(value.indexOf(item.username)!=-1 && value.charAt(value.indexOf(item.username)-1) ==="@"){ + //将此user的login存储到atWhoLoginList集合中 const list = new Set(atWhoLoginList.current); - list.delete(atWhoMap.get(username)); + list.add(item.login); atWhoLoginList.current = Array.from(list); setAtWhoLoginListState(Array.from(list)); } @@ -324,6 +297,62 @@ export default ({ mdID, onChange, onCMBeforeChange, onCMBlur, error = false, cla }); editorInstance.cm.on("change", (cm) => { onChange && onChange(cm.getValue()); + if(atWhoLoginList.current.length != 0){ + const codemirror = editorInstance.cm; + let value = codemirror.getValue(); + //处理初始内容就自带@谁的情况 + if(initValue){ + const del = []; + users.map(item=>{ + if(initValue.indexOf(item.username)!=-1 && initValue.charAt(initValue.indexOf(item.username)-1) === "@" && initValue.indexOf(`@${item.username}`)===value.indexOf(`@${item.username}`)){ + //初始内容中有符合@+名字的格式并且当前内容未删除初始内容 + del[del.length] = `@${item.username}`; + } + }) + del.length!=0 && del.map(str=>{ + value = value.replace(str,""); + }) + } + //以username为主键,login为value的map集合 + let atWhoMap = new Map(); + Array.from(atWhoLoginList.current).map(item=>{ + users.map(i=>{ + if(i.login === item){ + atWhoMap.set(i.username,i.login); + } + }) + }); + if(value.indexOf("@") === -1){ + //已经有要@的列表,但是没有@符号 -> 清空@集合 + atWhoLoginList.current = []; + setAtWhoLoginListState([]); + return; + } + const cursor = codemirror.doc.getCursor(); + const line = cursor.line; + const ch = cursor.ch; + const lineContent = codemirror.getLine(line); + if(lineContent && lineContent.indexOf("@") != -1){//此行有@谁 + Array.from(atWhoMap.keys()).map(username=>{ + //判断lineContent是不是以列表中的某个username结尾 + if(lineContent.endsWith(username)){ + codemirror.setSelection({line,ch:ch-username.length-1},{line,ch}); + return; + } + //处理有名字但是无@符号,有@但是名字对不上的情况 + const a = value.indexOf(username)===-1; + const b = value.charAt(value.indexOf(username)-1) !="@"; + if(value.indexOf(username)===-1 || value.charAt(value.indexOf(username)-1) !="@"){ + //符合任意一种情况->踢掉这个人 不给他发消息 + const list = new Set(atWhoLoginList.current); + list.delete(atWhoMap.get(username)); + atWhoLoginList.current = Array.from(list); + setAtWhoLoginListState(Array.from(list)); + } + }) + } + } + }); ro = onLayout() return () => {