Skip to content

Commit

Permalink
feature: add clone snippet feature to custom snippet group
Browse files Browse the repository at this point in the history
  • Loading branch information
mindolph committed Dec 25, 2024
1 parent 5b1cbe2 commit 7d1bdc0
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import com.mindolph.core.AppManager;
import com.mindolph.core.model.Snippet;
import com.mindolph.mfx.dialog.DialogFactory;
import com.mindolph.mfx.dialog.impl.TextDialogBuilder;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Dialog;
import javafx.scene.control.ListView;
import javafx.scene.control.MenuItem;
import javafx.scene.input.MouseButton;
Expand All @@ -23,6 +25,7 @@
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.Optional;
import java.util.function.Consumer;

/**
Expand All @@ -36,12 +39,14 @@ public class ListSnippetView extends AnchorPane implements SnippetViewable<Snipp

private final AppManager appManager = AppManager.getInstance();

// Editable view is used for custom snippet group.
private final BooleanProperty editableProperty = new SimpleBooleanProperty(false);

private final ListView<Snippet> listView;

private final MenuItem miNew = new MenuItem("New Snippet", FontIconManager.getIns().getIcon(IconKey.PLUS));
private final MenuItem miEdit = new MenuItem("Edit Snippet", FontIconManager.getIns().getIcon(IconKey.EDIT_TEXT));
private final MenuItem miClone = new MenuItem("Clone Snippet", FontIconManager.getIns().getIcon(IconKey.CLONE));
private final MenuItem miRemove = new MenuItem("Remove Snippet", FontIconManager.getIns().getIcon(IconKey.DELETE));

// event to SnippetView after snippet changes
Expand Down Expand Up @@ -78,10 +83,12 @@ private ContextMenu createContextMenu() {
ContextMenu contextMenu = new ContextMenu();
miNew.setOnAction(this);
miEdit.setOnAction(this);
miClone.setOnAction(this);
miRemove.setOnAction(this);
miEdit.setDisable(listView.getSelectionModel().getSelectedItem() == null);
miClone.setDisable(listView.getSelectionModel().getSelectedItem() == null);
miRemove.setDisable(listView.getSelectionModel().getSelectedItem() == null);
contextMenu.getItems().addAll(miNew, miEdit, miRemove);
contextMenu.getItems().addAll(miNew, miEdit, miClone, miRemove);
return contextMenu;
}

Expand All @@ -106,6 +113,26 @@ else if (actionEvent.getSource() == this.miEdit) {
snippetChanged.push(null);
}
}
else if (actionEvent.getSource() == this.miClone) {
Snippet<?> selectedItem = listView.getSelectionModel().getSelectedItem();
Dialog<String> dialog = new TextDialogBuilder()
.owner(DialogFactory.DEFAULT_WINDOW)
.title("Clone %s".formatted(selectedItem.getTitle()))
.content("Input a snippet name")
.text(selectedItem.getTitle())
.width(400)
.build();
Optional<String> optNewSnippetName = dialog.showAndWait();
if (optNewSnippetName.isPresent()) {
String newSnippetName = optNewSnippetName.get();
Snippet<?> clonedSnippet = selectedItem.deepClone();
clonedSnippet.title(newSnippetName);
log.debug(clonedSnippet.getTitle());
log.info("Save cloned snippet '%s' with type '%s'".formatted(clonedSnippet.getTitle(), clonedSnippet.getType()));
appManager.saveSnippet(fileType, clonedSnippet.getType(), Collections.singletonList(clonedSnippet), false);
snippetChanged.push(null);
}
}
else if (actionEvent.getSource() == this.miRemove) {
Snippet<?> selectedItem = listView.getSelectionModel().getSelectedItem();
if (selectedItem != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class SnippetView extends BaseView {
public SnippetView() {
super("/control/snippet_view.fxml", false);
tfKeyword = TextFields.createClearableTextField();
tfKeyword.setPromptText("filter snippets");
tfKeyword.textProperty().addListener((observableValue, s, newKeyword) -> {
this.filter(newKeyword);
});
Expand Down Expand Up @@ -153,7 +154,6 @@ public void reload(List<BaseSnippetGroup> snippetGroups, String fileType) {
if (view instanceof ListSnippetView lsv) {
// specific handling for custom snippet group.
lsv.subscribeSnippetChanged(snippet -> {
log.info("## snippets changed");
if (snippetGroup instanceof CustomSnippetGroup csg) {
csg.reloadSnippets(fileType);
filter(tfKeyword.getText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ else if (t1 == rdTypeImage) {
File file = DialogFactory.openFileDialog(DialogFactory.DEFAULT_WINDOW, defaultDir,
new FileChooser.ExtensionFilter("Image", "*.png", "*.jpg"));
if (file != null && file.exists()) {
log.debug(file.getAbsolutePath());
if (log.isTraceEnabled()) log.trace(file.getAbsolutePath());
updateSnippet(this.loadImage(file));
}
});
Expand Down Expand Up @@ -152,13 +152,14 @@ private void initUI(String fileType) {
try {
this.fileTypeSnippetMap = JsonUtils.jsonTo(FILE_TYPE_SNIPPET_MAP_IN_JSON, new TypeReference<Map<String, List<String>>>() {
});
log.trace(StringUtils.join(this.fileTypeSnippetMap.get(SupportFileTypes.TYPE_MARKDOWN), ","));
if (log.isTraceEnabled())
log.trace(StringUtils.join(this.fileTypeSnippetMap.get(SupportFileTypes.TYPE_MARKDOWN), ","));
} catch (IOException e) {
log.warn("failed to parse file type and snippet type mapping", e);
this.fileTypeSnippetMap = new HashMap<>();
}
List<String> supportedTypes = this.fileTypeSnippetMap.get(fileType);
log.debug(StringUtils.join(supportedTypes, ","));
if (log.isTraceEnabled()) log.trace(StringUtils.join(supportedTypes, ","));
if (supportedTypes != null) {
if (!supportedTypes.contains(Snippet.TYPE_IMAGE)) {
hide(itemImage, rdTypeImage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,9 @@ public void deleteSnippets(String fileType, List<Snippet<?>> snippets) {
}

private SnippetsRecord loadSnippetsRecord(String fileType) {

try {
SnippetsRecord snippetsRecord = new Gson().fromJson(new FileReader(snippetsFile(fileType)), SnippetsRecord.class);
if (snippetsRecord != null) log.debug(String.valueOf(snippetsRecord.version()));
// if (snippetsRecord != null) log.debug(String.valueOf(snippetsRecord.version()));
return snippetsRecord;
} catch (FileNotFoundException e) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public Snippet(SnippetRecord sr) {
.filePath(sr.filePath());
}

public Snippet<?> deepClone() {
return new Snippet<>(this.title).code(this.code).type(this.type).filePath(this.filePath).description(this.description);
}

public String getTitle() {
return title;
Expand Down

0 comments on commit 7d1bdc0

Please sign in to comment.