Skip to content

Commit

Permalink
add selection copy to preview window (#7421)
Browse files Browse the repository at this point in the history
* add selection copy to preview window

* add key in en property file for copy selection

* save selected html
  • Loading branch information
tmrd993 authored Feb 5, 2021
1 parent 242a494 commit 00894a3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We added some symbols and keybindings to the context menu in the entry editor. [#7268](https://github.com/JabRef/jabref/pull/7268)
- We added keybindings for setting and clearing the read status. [#7264](https://github.com/JabRef/jabref/issues/7264)
- We added two new fields to track the creation and most recent modification date and time for each entry. [koppor#130](https://github.com/koppor/jabref/issues/130)
- We added a feature that allows the user to copy highlighted text in the preview window. [#6962](https://github.com/JabRef/jabref/issues/6962)

### Changed

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/jabref/gui/preview/PreviewPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ private ContextMenu createPopupMenu() {
MenuItem copyPreview = new MenuItem(Localization.lang("Copy preview"), IconTheme.JabRefIcons.COPY.getGraphicNode());
keyBindingRepository.getKeyCombination(KeyBinding.COPY_PREVIEW).ifPresent(copyPreview::setAccelerator);
copyPreview.setOnAction(event -> previewView.copyPreviewToClipBoard());
MenuItem copySelection = new MenuItem(Localization.lang("Copy selection"));
copySelection.setOnAction(event -> previewView.copySelectionToClipBoard());
MenuItem printEntryPreview = new MenuItem(Localization.lang("Print entry preview"), IconTheme.JabRefIcons.PRINTED.getGraphicNode());
printEntryPreview.setOnAction(event -> previewView.print());
MenuItem previousPreviewLayout = new MenuItem(Localization.lang("Previous preview layout"));
Expand All @@ -156,6 +158,7 @@ private ContextMenu createPopupMenu() {

ContextMenu menu = new ContextMenu();
menu.getItems().add(copyPreview);
menu.getItems().add(copySelection);
menu.getItems().add(printEntryPreview);
menu.getItems().add(new SeparatorMenuItem());
menu.getItems().add(nextPreviewLayout);
Expand Down
34 changes: 33 additions & 1 deletion src/main/java/org/jabref/gui/preview/PreviewViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ public class PreviewViewer extends ScrollPane implements InvalidationListener {

private static final Logger LOGGER = LoggerFactory.getLogger(PreviewViewer.class);

// https://stackoverflow.com/questions/5669448/get-selected-texts-html-in-div/5670825#5670825
private static final String JS_GET_SELECTION_HTML_SCRIPT = "function getSelectionHtml() {" +
" var html = \"\";" +
" if (typeof window.getSelection != \"undefined\") {" +
" var sel = window.getSelection();" +
" if (sel.rangeCount) {" +
" var container = document.createElement(\"div\");" +
" for (var i = 0, len = sel.rangeCount; i < len; ++i) {" +
" container.appendChild(sel.getRangeAt(i).cloneContents());" +
" }" +
" html = container.innerHTML;" +
" }" +
" } else if (typeof document.selection != \"undefined\") {" +
" if (document.selection.type == \"Text\") {" +
" html = document.selection.createRange().htmlText;" +
" }" +
" }" +
" return html;" +
"};" +
"getSelectionHtml();";
private static final String JS_HIGHLIGHT_FUNCTION =
"<head>" +
" <meta charset=\"UTF-8\">" +
Expand Down Expand Up @@ -224,12 +244,24 @@ public void copyPreviewToClipBoard() {
clipBoardManager.setContent(content);
}

public void copySelectionToClipBoard() {
ClipboardContent content = new ClipboardContent();
content.putString(getSelectionTextContent());
content.putHtml(getSelectionHtmlContent());

clipBoardManager.setContent(content);
}

@Override
public void invalidated(Observable observable) {
update();
}

public String getSelectionHtmlContent() {
public String getSelectionTextContent() {
return (String) previewView.getEngine().executeScript("window.getSelection().toString()");
}

public String getSelectionHtmlContent() {
return (String) previewView.getEngine().executeScript(JS_GET_SELECTION_HTML_SCRIPT);
}
}
1 change: 1 addition & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1325,6 +1325,7 @@ Synchronize\ files=Synchronize files
Unabbreviate=Unabbreviate
should\ contain\ a\ protocol=should contain a protocol
Copy\ preview=Copy preview
Copy\ selection=Copy selection
Automatically\ setting\ file\ links=Automatically setting file links
Regenerating\ citation\ keys\ according\ to\ metadata=Regenerating citation keys according to metadata
Regenerate\ all\ keys\ for\ the\ entries\ in\ a\ BibTeX\ file=Regenerate all keys for the entries in a BibTeX file
Expand Down

0 comments on commit 00894a3

Please sign in to comment.