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

Add images to comments editor #12193

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/main/java/org/jabref/gui/ClipBoardManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;

import javafx.application.Platform;
import javafx.scene.control.TextInputControl;
Expand Down Expand Up @@ -94,10 +96,22 @@ public static String getHtmlContents() {
return result;
}

public static List<File> getFiles() {
return clipboard.getFiles();
}

public static boolean hasHtml() {
return clipboard.hasHtml();
}

public static boolean hasFiles() {
return clipboard.hasFiles();
}

public static Set<DataFormat> getTypes() {
return clipboard.getContentTypes();
}

/**
* Get the String residing on the primary clipboard (if it exists).
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static FieldEditorFX getForField(final Field field,
} else if (field == InternalField.KEY_FIELD) {
return new CitationKeyEditor(field, suggestionProvider, fieldCheckers, databaseContext, undoAction, redoAction);
} else if (fieldProperties.contains(FieldProperty.MARKDOWN)) {
return new MarkdownEditor(field, suggestionProvider, fieldCheckers, preferences, undoManager, undoAction, redoAction);
return new MarkdownEditor(field, suggestionProvider, fieldCheckers, preferences, undoManager, undoAction, redoAction, databaseContext);
} else {
// There was no specific editor found

Expand Down
52 changes: 51 additions & 1 deletion src/main/java/org/jabref/gui/fieldeditors/MarkdownEditor.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.jabref.gui.fieldeditors;

import java.io.File;
import java.util.List;

import javax.swing.undo.UndoManager;

import javafx.scene.control.TextInputControl;
Expand All @@ -10,15 +13,56 @@
import org.jabref.gui.undo.RedoAction;
import org.jabref.gui.undo.UndoAction;
import org.jabref.logic.integrity.FieldCheckers;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.field.Field;

import com.vladsch.flexmark.html2md.converter.FlexmarkHtmlConverter;

public class MarkdownEditor extends SimpleEditor {
private final FlexmarkHtmlConverter flexmarkHtmlConverter = FlexmarkHtmlConverter.builder().build();

public MarkdownEditor(Field field, SuggestionProvider<?> suggestionProvider, FieldCheckers fieldCheckers, GuiPreferences preferences, UndoManager undoManager, UndoAction undoAction, RedoAction redoAction) {
private final BibDatabaseContext databaseContext;

public MarkdownEditor(Field field,
SuggestionProvider<?> suggestionProvider,
FieldCheckers fieldCheckers,
GuiPreferences preferences,
UndoManager undoManager,
UndoAction undoAction,
RedoAction redoAction,
BibDatabaseContext databaseContext) {
super(field, suggestionProvider, fieldCheckers, preferences, true, undoManager, undoAction, redoAction);

this.databaseContext = databaseContext;

this.setOnDragDropped(event -> {
boolean success = false;
if (event.getDragboard().hasFiles()) {
String mdText = imageToMdText(event.getDragboard().getFiles());

this.insertTextFromDragInput(event.getX(), event.getY(), mdText);

success = true;
}
event.setDropCompleted(success);
event.consume();
});
}

private String imageToMdText(List<File> files) {
String mdImageTemplate = "![%s](file://%s)\n";
StringBuilder allImagesText = new StringBuilder();

// With this you can get the path to the default directory
if (databaseContext.getMetaData().getDefaultFileDirectory().isPresent()) {
databaseContext.getMetaData().getDefaultFileDirectory().get();
}

for (File file: files) {
allImagesText.append(mdImageTemplate.formatted(file.getName(), file.getAbsolutePath()));
}

return allImagesText.toString();
}

@Override
Expand All @@ -29,6 +73,12 @@ public void paste() {
if (ClipBoardManager.hasHtml()) {
String htmlText = ClipBoardManager.getHtmlContents();
String mdText = flexmarkHtmlConverter.convert(htmlText);
super.replaceSelection(mdText);
} else if (ClipBoardManager.hasFiles()) {
List<File> files = ClipBoardManager.getFiles();

String mdText = imageToMdText(files);

super.replaceSelection(mdText);
} else {
super.paste();
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/jabref/gui/fieldeditors/SimpleEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

import javafx.scene.Parent;
import javafx.scene.control.TextInputControl;
import javafx.scene.control.skin.TextAreaSkin;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.text.HitInfo;

import org.jabref.gui.autocompleter.AutoCompletionTextInputBinding;
import org.jabref.gui.autocompleter.ContentSelectorSuggestionProvider;
Expand Down Expand Up @@ -73,6 +75,24 @@ public void requestFocus() {
textInput.requestFocus();
}

public void insertTextFromDragInput(double x, double y, String text) {
TextAreaSkin skin = (TextAreaSkin) textInput.getSkin();

HitInfo hit = skin.getIndex(x, y);

skin.positionCaret(hit, false);

int insertionPoint = hit.getInsertionIndex();

textInput.getLength();

if (insertionPoint > textInput.getLength()) {
textInput.appendText("\n\n" + text);
} else {
textInput.insertText(insertionPoint, text);
}
}

protected TextInputControl getTextInput() {
return textInput;
}
Expand Down
Loading