Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix various copy to clipboard issues #5340

Merged
merged 14 commits into from
Oct 6, 2019
Merged
30 changes: 24 additions & 6 deletions src/main/java/org/jabref/gui/exporter/ExportToClipboardAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.jabref.gui.util.BackgroundTask;
import org.jabref.logic.exporter.Exporter;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.FileType;
import org.jabref.logic.util.OS;
import org.jabref.model.entry.BibEntry;
import org.jabref.preferences.JabRefPreferences;
Expand Down Expand Up @@ -73,10 +74,9 @@ public void execute() {
selectedExporter.ifPresent(exporter -> BackgroundTask.wrap(() -> exportToClipboard(exporter))
.onSuccess(this::setContentToClipboard)
.executeWith(Globals.TASK_EXECUTOR));

}

private String exportToClipboard(Exporter exporter) {
private ExportResult exportToClipboard(Exporter exporter) throws Exception {
// Set the global variable for this database's file directory before exporting,
// so formatters can resolve linked files correctly.
// (This is an ugly hack!)
Expand All @@ -102,7 +102,7 @@ private String exportToClipboard(Exporter exporter) {
entries);
// Read the file and put the contents on the clipboard:

return readFileToString(tmp);
return new ExportResult(readFileToString(tmp), exporter.getFileType());
} catch (Exception e) {
LOGGER.error("Error exporting to clipboard", e);
} finally {
Expand All @@ -115,12 +115,20 @@ private String exportToClipboard(Exporter exporter) {
}
}
}
return "";
throw new Exception("export failed.");
matthiasgeiger marked this conversation as resolved.
Show resolved Hide resolved
}

private void setContentToClipboard(String content) {
private void setContentToClipboard(ExportResult result) {
ClipboardContent clipboardContent = new ClipboardContent();
clipboardContent.putRtf(content);
List<String> extensions = result.fileType.getExtensions();
if (extensions.contains("html")) {
clipboardContent.putHtml(result.content);
} else if (extensions.contains("rtf")) {
clipboardContent.putRtf(result.content);
} else if (extensions.contains("rdf")) {
clipboardContent.putRtf(result.content);
}
clipboardContent.putString(result.content);
Globals.clipboardManager.setContent(clipboardContent);

dialogService.notify(Localization.lang("Entries exported to clipboard") + ": " + entries.size());
Expand All @@ -135,4 +143,14 @@ private String readFileToString(Path tmp) throws IOException {
return reader.lines().collect(Collectors.joining(OS.NEWLINE));
}
}

private class ExportResult {
final String content;
matthiasgeiger marked this conversation as resolved.
Show resolved Hide resolved
final FileType fileType;

ExportResult(String content, FileType fileType) {
this.content = content;
this.fileType = fileType;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@ private List<String> generateCitations() throws IOException {
* Generates a plain text string out of the preview and copies it additionally to the html to the clipboard
* (WYSIWYG Editors use the HTML, plain text editors the text)
*/
protected static String processPreview(List<String> citations) {
return String.join(CitationStyleOutputFormat.HTML.getLineSeparator(), citations);
protected static ClipboardContent processPreview(List<String> citations) {
ClipboardContent content = new ClipboardContent();
content.putHtml(String.join(CitationStyleOutputFormat.HTML.getLineSeparator(), citations));
content.putString(String.join(CitationStyleOutputFormat.HTML.getLineSeparator(), citations));
return content;
}

/**
Expand All @@ -108,6 +111,7 @@ protected static ClipboardContent processRtf(List<String> citations) {
String.join(CitationStyleOutputFormat.RTF.getLineSeparator(), citations) +
"}";
ClipboardContent content = new ClipboardContent();
content.putString(result);
content.putRtf(result);
return content;
}
Expand All @@ -134,6 +138,7 @@ protected static ClipboardContent processXslFo(List<String> citations) {
"</fo:root>" + OS.NEWLINE;

ClipboardContent content = new ClipboardContent();
content.putString(result);
content.put(ClipBoardManager.XML, result);
return content;
}
Expand All @@ -155,14 +160,15 @@ protected static ClipboardContent processHtml(List<String> citations) {
"</html>" + OS.NEWLINE;

ClipboardContent content = new ClipboardContent();
content.putString(result);
content.putHtml(result);
return content;
}

private void setClipBoardContent(List<String> citations) {
// if it's not a citation style take care of the preview
if (!(style instanceof CitationStylePreviewLayout)) {
clipBoardManager.setHtmlContent(processPreview(citations));
clipBoardManager.setContent(processPreview(citations));
} else {
// if it's generated by a citation style take care of each output format
ClipboardContent content;
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/jabref/gui/preview/PreviewViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,11 @@ public void copyPreviewToClipBoard() {
previewStringContent.append(element.getTextContent());
}

// remove JavaScript function for highlighting from plain text copy
String previewStringContentString = previewStringContent.toString().replaceAll("function highlight[\\w\\W]*\\}", "");
matthiasgeiger marked this conversation as resolved.
Show resolved Hide resolved

ClipboardContent content = new ClipboardContent();
content.putString(previewStringContent.toString());
content.putString(previewStringContentString);
content.putHtml((String) previewView.getEngine().executeScript("document.documentElement.outerHTML"));

clipBoardManager.setContent(content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ void processPreviewText() throws Exception {
OS.NEWLINE +
"Abstract: This entry describes a test scenario which may be useful in JabRef. By providing a test entry it is possible to see how certain things will look in this graphical BIB-file mananger. ";

String actual = CitationStyleToClipboardWorker.processPreview(Arrays.asList(citation, citation));
ClipboardContent clipboardContent = CitationStyleToClipboardWorker.processPreview(Arrays.asList(citation, citation));
String actual = clipboardContent.getString();

assertEquals(expected, actual);
}
Expand Down Expand Up @@ -91,8 +92,8 @@ void processPreviewHtml() throws Exception {
"</dd>" + OS.NEWLINE +
"<p></p></font>";

String actual = CitationStyleToClipboardWorker.processPreview(Arrays.asList(citation, citation));

ClipboardContent clipboardContent = CitationStyleToClipboardWorker.processPreview(Arrays.asList(citation, citation));
String actual = clipboardContent.getString();
assertEquals(expected, actual);
}

Expand Down