Fixes for Bug #320, Users tagging does not work if @ is in the middle of already typed text

git-svn-id: https://svn.research-infrastructures.eu/d4science/gcube/trunk/portlets/user/share-updates@117260 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Massimiliano Assante 2015-07-15 16:37:33 +00:00
parent 30a59d7599
commit 3e72d59d1c
2 changed files with 42 additions and 31 deletions

View File

@ -7,6 +7,9 @@
<dependent-module archiveName="gcube-widgets-1.9.1-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/gcube-widgets/gcube-widgets">
<dependency-type>uses</dependency-type>
</dependent-module>
<dependent-module archiveName="pickitem-widget-1.1.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/pickitem-widget/pickitem-widget">
<dependency-type>uses</dependency-type>
</dependent-module>
<property name="java-output-path" value="/${module}/target/www/WEB-INF/classes"/>
<property name="context-root" value="share-updates"/>
</wb-module>

View File

@ -4,6 +4,7 @@
package org.gcube.portlets.user.shareupdates.client.view;
import java.util.ArrayList;
import java.util.HashSet;
import org.gcube.portlets.user.shareupdates.client.ShareUpdateService;
import org.gcube.portlets.user.shareupdates.client.ShareUpdateServiceAsync;
@ -36,7 +37,7 @@ public class SuperPosedTextArea extends TextArea {
public final static int ARROW_UP = 38;
public final static int ARROW_DOWN = 40;
private ArrayList<String> mentionedUsers = new ArrayList<String>();
private HashSet<String> mentionedUsers = new HashSet<String>();
private ArrayList<String> hashtags = new ArrayList<String>();
/**
@ -44,9 +45,10 @@ public class SuperPosedTextArea extends TextArea {
*/
public SuperPosedTextArea() {
sinkEvents(Event.ONPASTE);
sinkEvents(Event.ONKEYUP);
sinkEvents(Event.ONKEYPRESS);
sinkEvents(Event.ONCONTEXTMENU);
sinkEvents(Event.ONKEYDOWN);
sinkEvents(Event.ONKEYUP);
//get the users
shareupdateService.getPortalUsers(new AsyncCallback<ArrayList<ItemBean>>() {
@Override
@ -69,8 +71,8 @@ public class SuperPosedTextArea extends TextArea {
public void onFailure(Throwable caught) {
}
});
DOM.setElementAttribute(getElement(), "id", "postTextArea");
getElement().setAttribute("id", "postTextArea");
bind();
}
@ -100,10 +102,15 @@ public class SuperPosedTextArea extends TextArea {
t.schedule(100);
break;
}
case Event.ONKEYPRESS: {
pickUserDlg.onKeyPress(this.getCursorPos(), event.getKeyCode(), this.getAbsoluteLeft(), this.getAbsoluteTop()+this.getOffsetHeight(), getText());
pickHashtagDlg.onKeyPress(this.getCursorPos(), event.getKeyCode(), this.getAbsoluteLeft(), this.getAbsoluteTop()+this.getOffsetHeight(), getText());
break;
}
case Event.ONKEYUP: {
injectInDiv(getText());
pickUserDlg.onKeyUp(event.getKeyCode(), this.getAbsoluteLeft(), this.getAbsoluteTop()+this.getOffsetHeight(), getText());
pickHashtagDlg.onKeyUp(event.getKeyCode(), this.getAbsoluteLeft(), this.getAbsoluteTop()+this.getOffsetHeight(), getText());
pickUserDlg.onKeyUp(event.getKeyCode());
pickHashtagDlg.onKeyUp(event.getKeyCode());
break;
}
case Event.ONCONTEXTMENU: {
@ -115,7 +122,7 @@ public class SuperPosedTextArea extends TextArea {
//avoid the arrow up to move the cursor at the beginning of the textbox and the TAB to move around inputs and enter to go newline
if (event.getKeyCode() == ARROW_UP || event.getKeyCode() == KeyCodes.KEY_TAB || event.getKeyCode() == KeyCodes.KEY_ENTER) {
DOM.eventCancelBubble(event, true);
DOM.eventPreventDefault(event);
event.preventDefault();
return;
}
}
@ -123,7 +130,7 @@ public class SuperPosedTextArea extends TextArea {
//avoid the arrow up to move the cursor at the beginning of the textbox and the TAB to move around inputs and enter to go newline
if (event.getKeyCode() == ARROW_UP || event.getKeyCode() == KeyCodes.KEY_TAB || event.getKeyCode() == KeyCodes.KEY_ENTER) {
DOM.eventCancelBubble(event, true);
DOM.eventPreventDefault(event);
event.preventDefault();
return;
}
}
@ -142,20 +149,21 @@ public class SuperPosedTextArea extends TextArea {
DOM.getElementById("highlighter").setInnerHTML("");
}
private void injectInDiv(String textAreaText) {
GWT.log("# injectInDiv: "+textAreaText);
String text;
// parse the text:
// replace all the line braks by <br/>, and all the double spaces by the html version &nbsp;
// replace all the line breaks by <br/>, and all the double spaces by the html version &nbsp;
text = textAreaText.replaceAll("(\r\n|\n)","<br />");
text = text.replaceAll("\\s\\s","&nbsp;&nbsp;");
for (String mentionedUser : mentionedUsers) {
text = text.replaceAll(mentionedUser,"<span class=\"highlightedUser\">"+mentionedUser+"</span>");
}
// for (String hashtag : hashtags) {
// text = text.replaceAll(hashtag,"<span>"+hashtag+"</span>");
// }
for (String hashtag : hashtags) {
text = text.replaceAll(hashtag,"<span>"+hashtag+"</span>");
}
// re-inject the processed text into the div
DOM.getElementById("highlighter").setInnerHTML(text);
}
@ -167,28 +175,28 @@ public class SuperPosedTextArea extends TextArea {
eventBus.addHandler(PickedItemEvent.TYPE, new PickedItemEventHandler() {
@Override
public void onSelectedItem(PickedItemEvent event) {
String triggerChar = event.getTriggerChar();
char triggerChar = event.getTriggerChar();
String toAdd = event.getSelectedItem().getAlternativeName();
if (triggerChar.compareTo("#") == 0) { //has to be treated differently becase the # char remain present in the text unlike the @
if (triggerChar == '#') { //has to be treated differently becase the # char remain present in the text unlike the @
hashtags.add(toAdd);
int tPos = getText().lastIndexOf(triggerChar);
setText(getText().substring(0, tPos) + toAdd);
Element highDiv = DOM.getElementById("highlighter");
tPos = highDiv.getInnerHTML().lastIndexOf(triggerChar);
highDiv.setInnerHTML(highDiv.getInnerHTML().substring(0, tPos) + toAdd);
String preceedingPart = getText().substring(0, event.getItemCursorIndexStart());
String afterPart = getText().substring(event.getItemCursorIndexEnd()+1);
//GWT.log("# BEFORE: "+preceedingPart);
setText(preceedingPart + toAdd + " " + afterPart);
injectInDiv(getText());
}
if (event.getTriggerChar().compareTo("@") == 0) {
if (triggerChar == '@') {
GWT.log("GOT @ EVENT! "+ event.getSelectedItem().getAlternativeName());
mentionedUsers.add(toAdd);
String[] toSplit = getText().split(triggerChar); //get the preceeding part
setText(toSplit[0]+toAdd);
Element highDiv = DOM.getElementById("highlighter");
String preceedingPart = getText().substring(0, event.getItemCursorIndexStart());
String afterPart = getText().substring(event.getItemCursorIndexEnd()+1);
setText(preceedingPart + toAdd + " " + afterPart);
injectInDiv(getText());
String[] htmlToSplit = highDiv.getInnerHTML().split(triggerChar); //get the preceeding part
String highLightedUser = "<span class=\"highlightedUser\">"+toAdd+"</span>";
highDiv.setInnerHTML(htmlToSplit[0]+highLightedUser);
}
}
});
@ -203,7 +211,7 @@ public class SuperPosedTextArea extends TextArea {
GWT.log(toReturn.toString());
return toReturn;
}
public ArrayList<String> getHashtags() {
ArrayList<String> toReturn = new ArrayList<String>();
for (String hashtag : hashtags) {