Skip to content

Commit

Permalink
#4672 Add right click menu to copyable items
Browse files Browse the repository at this point in the history
  • Loading branch information
stroomdev66 committed Jan 28, 2025
1 parent 4ac0895 commit 8731dd1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import stroom.svg.shared.SvgImage;
import stroom.util.client.ClipboardUtil;
import stroom.util.shared.GwtNullSafe;
import stroom.widget.menu.client.presenter.IconMenuItem;
import stroom.widget.menu.client.presenter.Item;
import stroom.widget.menu.client.presenter.ShowMenuEvent;
import stroom.widget.popup.client.presenter.PopupPosition;
import stroom.widget.util.client.ElementUtil;
import stroom.widget.util.client.MouseUtil;
import stroom.widget.util.client.SvgImageUtil;
Expand All @@ -11,11 +15,15 @@
import com.google.gwt.dom.client.BrowserEvents;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.shared.HasHandlers;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;

import java.util.ArrayList;
import java.util.List;

public class CopyTextUtil {

public static final String ICON_NAME = "svgIcon";
Expand Down Expand Up @@ -79,17 +87,33 @@ public static void render(final String value,
}
}

public static void onClick(final NativeEvent e) {
if (BrowserEvents.MOUSEDOWN.equals(e.getType()) && MouseUtil.isPrimary(e)) {
public static void onClick(final NativeEvent e,
final HasHandlers hasHandlers) {
if (BrowserEvents.MOUSEDOWN.equals(e.getType())) {
final Element element = e.getEventTarget().cast();
final Element copy =
ElementUtil.findParent(element, CopyTextUtil.COPY_CLASS_NAME, 5);
if (copy != null) {
final Element container = ElementUtil.findParent(
element, "docRefLinkContainer", 5);

GwtNullSafe.consumeNonBlankString(container, Element::getInnerText, text ->
ClipboardUtil.copy(text.trim()));
final Element container = ElementUtil.findParent(
element, "docRefLinkContainer", 5);
final String text = GwtNullSafe.get(container, Element::getInnerText);
if (GwtNullSafe.isNonBlankString(text)) {
if (MouseUtil.isPrimary(e)) {
final Element copy = ElementUtil.findParent(element, CopyTextUtil.COPY_CLASS_NAME, 5);
if (copy != null) {
ClipboardUtil.copy(text.trim());
}
} else {
final List<Item> menuItems = new ArrayList<>();
menuItems.add(new IconMenuItem.Builder()
.priority(1)
.icon(SvgImage.COPY)
.text("Copy")
.command(() -> ClipboardUtil.copy(text.trim()))
.build());
ShowMenuEvent
.builder()
.items(menuItems)
.popupPosition(new PopupPosition(e.getClientX(), e.getClientY()))
.fire(hasHandlers);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ protected void onBind() {
onNewTabSelected(event.getSelectedItem())));
registerHandler(dataView.getTabBar().addShowMenuHandler(e -> getEventBus().fireEvent(e)));
registerHandler(htmlPresenter.getWidget().addDomHandler(e ->
CopyTextUtil.onClick(e.getNativeEvent()), MouseDownEvent.getType()));
CopyTextUtil.onClick(e.getNativeEvent(), this), MouseDownEvent.getType()));
}

private void onNewTabSelected(final TabData tab) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public InfoDocumentPresenter(final EventBus eventBus,
@Override
protected void onBind() {
registerHandler(getView().asWidget().addDomHandler(e ->
CopyTextUtil.onClick(e.getNativeEvent()), MouseDownEvent.getType()));
CopyTextUtil.onClick(e.getNativeEvent(), this), MouseDownEvent.getType()));
}

@Override
Expand Down
24 changes: 24 additions & 0 deletions unreleased_changes/20250128_162441_331__4672.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
* Issue **#4672** : Add right click menu to copyable items.


```sh
# ********************************************************************************
# Issue title: Add right click context menu to table cells, especially doc ref cells.
# Issue link: https://github.com/gchq/stroom/issues/4672
# ********************************************************************************

# ONLY the top line will be included as a change entry in the CHANGELOG.
# The entry should be in GitHub flavour markdown and should be written on a SINGLE
# line with no hard breaks. You can have multiple change files for a single GitHub issue.
# The entry should be written in the imperative mood, i.e. 'Fix nasty bug' rather than
# 'Fixed nasty bug'.
#
# Examples of acceptable entries are:
#
#
# * Issue **123** : Fix bug with an associated GitHub issue in this repository
#
# * Issue **namespace/other-repo#456** : Fix bug with an associated GitHub issue in another repository
#
# * Fix bug with no associated GitHub issue.
```

0 comments on commit 8731dd1

Please sign in to comment.