Skip to content

Commit

Permalink
fix: ignore file link to mmd topic when insert image from custom snip…
Browse files Browse the repository at this point in the history
…pet;
  • Loading branch information
mindolph committed Dec 21, 2024
1 parent 4192c86 commit efcfd3b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package com.mindolph.mindmap.extension.attributes;

import com.igormaznitsa.mindmap.model.MMapURI;
import com.mindolph.mfx.dialog.DialogFactory;
import com.mindolph.mfx.util.FxImageUtils;
import com.mindolph.mindmap.I18n;
import com.mindolph.mindmap.dialog.ImagePreviewDialog;
import com.mindolph.mindmap.extension.api.ExtensionContext;
import com.mindolph.mindmap.model.TopicNode;
import javafx.scene.image.Image;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -79,4 +87,33 @@ public static boolean setIconAttribute(String iconName, Set<TopicNode> topics) {
}
return changed;
}

/**
* @param context
* @param selected file to load to topics.
* @param withFileLink ank user for attach file link to the image attribute if true.
*/
public static void loadImageFileToSelectedTopics(ExtensionContext context, File selected, boolean withFileLink) {
Image image;
try {
image = new Image(new FileInputStream(selected));
ImagePreviewDialog scaleImageDialog = new ImagePreviewDialog("Resize&Preview", image);
Image scaledImage = scaleImageDialog.showAndWait();
if (scaledImage != null) {
String rescaledImageAsBase64 = FxImageUtils.imageToBase64(scaledImage);
String fileName = FilenameUtils.getBaseName(selected.getName());
String filePath;
if (withFileLink && DialogFactory.yesNoConfirmDialog(I18n.getIns().getString("Images.Extension.Question.AddFilePath.Title"), I18n.getIns().getString("Images.Extension.Question.AddFilePath"))) {
filePath = MMapURI.makeFromFilePath(context.getWorkspaceDir(), selected.getAbsolutePath(), null).toString();
}
else {
filePath = null;
}
setImageAttribute(context.getSelectedTopics(), rescaledImageAsBase64, filePath, fileName);
context.doNotifyModelChanged(true);
}
} catch (Exception ex) {
DialogFactory.errDialog(I18n.getIns().getString("Images.Extension.Error"));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.mindolph.mindmap.extension.attributes.images;

import com.igormaznitsa.mindmap.model.MMapURI;
import com.mindolph.base.FontIconManager;
import com.mindolph.base.constant.IconKey;
import com.mindolph.base.dialog.DialogFileFilters;
import com.mindolph.mfx.dialog.ConfirmDialogBuilder;
import com.mindolph.mfx.dialog.DialogFactory;
import com.mindolph.mfx.dialog.FileDialogBuilder;
import com.mindolph.mfx.util.AwtImageUtils;
import com.mindolph.mfx.util.FxImageUtils;
import com.mindolph.mindmap.I18n;
import com.mindolph.mindmap.dialog.AddImageChooseDialog;
import com.mindolph.mindmap.dialog.ImagePreviewDialog;
Expand All @@ -21,13 +19,11 @@
import javafx.scene.control.MenuItem;
import javafx.scene.image.Image;
import javafx.scene.input.Clipboard;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.SystemUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;

public class ImagePopUpMenuExtension extends BasePopupMenuItemExtension {
Expand Down Expand Up @@ -96,39 +92,14 @@ public MenuItem makeMenuItem(ExtensionContext context, TopicNode activeTopic) {
.extensionFilters(DialogFileFilters.IMAGE_EXTENSION_FILTER).buildAndShow();
// TODO remember the selected for next time popup;
if (selected != null) {
loadImageFileToSelectedTopics(context, selected);
AttributeUtils.loadImageFileToSelectedTopics(context, selected, true);
}
}
});
}
return result;
}

public static void loadImageFileToSelectedTopics(ExtensionContext context, File selected) {
Image image;
try {
image = new Image(new FileInputStream(selected));
ImagePreviewDialog scaleImageDialog = new ImagePreviewDialog("Resize&Preview", image);
Image scaledImage = scaleImageDialog.showAndWait();
if (scaledImage != null) {
String rescaledImageAsBase64 = FxImageUtils.imageToBase64(scaledImage);
String fileName = FilenameUtils.getBaseName(selected.getName());
String filePath;
if (DialogFactory.yesNoConfirmDialog(I18n.getIns().getString("Images.Extension.Question.AddFilePath.Title"), I18n.getIns().getString("Images.Extension.Question.AddFilePath"))) {
filePath = MMapURI.makeFromFilePath(context.getWorkspaceDir(), selected.getAbsolutePath(), null).toString();
}
else {
filePath = null;
}
AttributeUtils.setImageAttribute(context.getSelectedTopics(), rescaledImageAsBase64, filePath, fileName);
context.doNotifyModelChanged(true);
}
} catch (Exception ex) {
DialogFactory.errDialog(I18n.getIns().getString("Images.Extension.Error"));
log.error("Unexpected error during loading image file : " + selected, ex);
}
}

@Override
public ContextMenuSection getSection() {
return ContextMenuSection.EXTRAS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.mindolph.mfx.dialog.DialogFactory;
import com.mindolph.mindmap.event.MindmapEvents;
import com.mindolph.mindmap.extension.api.ExtensionContext;
import com.mindolph.mindmap.extension.attributes.images.ImagePopUpMenuExtension;
import com.mindolph.mindmap.extension.attributes.AttributeUtils;
import com.mindolph.mindmap.icon.IconID;
import com.mindolph.mindmap.model.BaseElement;
import com.mindolph.mindmap.model.ModelManager;
Expand Down Expand Up @@ -375,7 +375,7 @@ public void onSnippet(Snippet snippet) {
// since the ImageSnippet only works for JFX, it cant be imported to core module, so the custom
// image snippet needs to be handled separately.
if (StringUtils.isNotBlank(snippet.getFilePath())) {
ImagePopUpMenuExtension.loadImageFileToSelectedTopics((ExtensionContext) mindMapView, new File(snippet.getFilePath()));
AttributeUtils.loadImageFileToSelectedTopics((ExtensionContext) mindMapView, new File(snippet.getFilePath()), false);
}
else {
mindMapView.appendTextAsTopicTree(snippet.getCode(), "");
Expand Down
14 changes: 7 additions & 7 deletions code/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,29 @@
<maven.compiler.target>21</maven.compiler.target>
<javafx.version>23.0.1</javafx.version>
<commons.lang.version>3.17.0</commons.lang.version>
<commons.io.version>2.17.0</commons.io.version>
<commons.text.version>1.12.0</commons.text.version>
<commons.io.version>2.18.0</commons.io.version>
<commons.text.version>1.13.0</commons.text.version>
<commons.collections.version>4.4</commons.collections.version>
<commons.codec.version>1.17.1</commons.codec.version>
<commons.validator.version>1.9.0</commons.validator.version>
<commons.csv.version>1.12.0</commons.csv.version>
<swstate.version>2.0.1</swstate.version>
<plantuml.version>1.2024.7</plantuml.version>
<plantuml.version>1.2024.8</plantuml.version>
<swiftboot.version>2.4.8</swiftboot.version>
<slf4j.version>2.0.16</slf4j.version>
<log4j2.version>2.24.1</log4j2.version>
<junit.version>5.11.0</junit.version>
<junit.version>5.11.4</junit.version>
<mfx.version>2.0</mfx.version>
<gson.version>2.11.0</gson.version>
<controlsfx.version>11.2.1</controlsfx.version>
<richtextfx.version>0.11.3</richtextfx.version>
<richtextfx.version>0.11.4</richtextfx.version>
<reactfx.version>2.0-M5</reactfx.version>
<!-- <reactor.version>3.5.3</reactor.version>-->
<jsoup.version>1.18.1</jsoup.version>
<jsoup.version>1.18.3</jsoup.version>
<json.version>20240303</json.version>
<flexmark.version>0.64.8</flexmark.version>
<langchain4j.version>0.35.0</langchain4j.version>
<dashscope.version>2.16.9</dashscope.version>
<dashscope.version>2.16.10</dashscope.version>
</properties>

<dependencies>
Expand Down

0 comments on commit efcfd3b

Please sign in to comment.