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

Use Markdown in AI Summary tab implemented #12266

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 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
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.text.TextFlow?>
<fx:root alignment="TOP_CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" spacing="10.0" type="VBox" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.jabref.gui.ai.components.summary.SummaryShowingComponent">
<children>
<TextArea fx:id="summaryTextArea" editable="false" wrapText="true" VBox.vgrow="ALWAYS" />
<Button mnemonicParsing="false" onAction="#onRegenerateButtonClick" text="%Regenerate" />
<TextFlow textAlignment="CENTER">
<children>
<Text fx:id="summaryInfoText" strokeType="OUTSIDE" strokeWidth="0.0" text="%Generated at %0 by %1" />
</children>
</TextFlow>
</children>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
<?import javafx.scene.text.Text?>

<fx:root alignment="TOP_CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="400.0" minWidth="-Infinity" spacing="8.0" type="VBox" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.jabref.gui.ai.components.summary.SummaryShowingComponent">
<children>
<BorderPane minHeight="30.0">
<left>
<CheckBox fx:id="markdownCheckbox" mnemonicParsing="false" onAction="#onMarkdownToggle" text="Markdown" />
</left>
<center>
<Button mnemonicParsing="false" onAction="#onRegenerateButtonClick" text="%Regenerate" translateX="-40" />
</center>
</BorderPane>
<TextFlow textAlignment="CENTER">
mulla028 marked this conversation as resolved.
Show resolved Hide resolved
<children>
<Text fx:id="summaryInfoText" strokeType="OUTSIDE" strokeWidth="0.0" text="%Generated at %0 by %1" />
</children>
</TextFlow>
</children>
<padding>
<Insets bottom="8.0" left="8.0" right="8.0" top="8.0" />
</padding>
</fx:root>
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,26 @@
import java.util.Locale;

import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.scene.web.WebView;

import org.jabref.logic.ai.summarization.Summary;
import org.jabref.logic.layout.format.MarkdownFormatter;
import org.jabref.logic.util.WebViewStore;

import com.airhacks.afterburner.views.ViewLoader;

public class SummaryShowingComponent extends VBox {
@FXML private TextArea summaryTextArea;
@FXML private Text summaryInfoText;
@FXML private CheckBox markdownCheckbox;

private WebView contentWebView;
private final Summary summary;
private final Runnable regenerateCallback;
private final MarkdownFormatter markdownFormatter = new MarkdownFormatter();
mulla028 marked this conversation as resolved.
Show resolved Hide resolved

public SummaryShowingComponent(Summary summary, Runnable regenerateCallback) {
this.summary = summary;
Expand All @@ -32,20 +38,49 @@ public SummaryShowingComponent(Summary summary, Runnable regenerateCallback) {

@FXML
private void initialize() {
summaryTextArea.setText(summary.content());
initializeWebView();
updateContent(false); // Start in plain text mode
updateInfoText();
}

private void initializeWebView() {
contentWebView = WebViewStore.get();
contentWebView.setMinHeight(100);
mulla028 marked this conversation as resolved.
Show resolved Hide resolved
contentWebView.setPrefHeight(300);

// Apply styles directly to the WebView
contentWebView.setStyle("-fx-font-family: Sans;");
mulla028 marked this conversation as resolved.
Show resolved Hide resolved

VBox.setVgrow(contentWebView, Priority.ALWAYS);
getChildren().addFirst(contentWebView);
}

private void updateContent(boolean isMarkdown) {
String content = summary.content();
if (isMarkdown) {
contentWebView.getEngine().loadContent(markdownFormatter.format(content));
} else {
contentWebView.getEngine().loadContent("<pre>" + content + "</pre>");
}
}

private void updateInfoText() {
String newInfo = summaryInfoText
.getText()
.replaceAll("%0", formatTimestamp(summary.timestamp()))
.replaceAll("%1", summary.aiProvider().getLabel() + " " + summary.model());

summaryInfoText.setText(newInfo);
}

private static String formatTimestamp(LocalDateTime timestamp) {
return timestamp.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).withLocale(Locale.getDefault()));
}

@FXML
private void onMarkdownToggle() {
updateContent(markdownCheckbox.isSelected());
}

@FXML
private void onRegenerateButtonClick() {
regenerateCallback.run();
Expand Down
Loading