Skip to content

Commit

Permalink
remember last directory pattern per file
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerbrandl committed Jun 25, 2016
1 parent 1b097d4 commit a3a98e1
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 18 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@ Use the plugin browser in Intellij to download the plugin.
1) Copy an image to your clipboard. E.g by right-click + copy in your favorite browser or by doing `Command-Control-Shift-4` on a mac or `PrtScn` on [windows10](http://www.howtogeek.com/226280/how-to-take-screenshots-in-windows-10/)

2) Press `shift-ctrl-V` (or change the keybinding under `Preferences->Keymap->"Paste Image into Markdown")
![](docs/images/paste_image_settings.png)
![](docs/images/paste_image_settings.jpg)

* optionally adjust file name (which defaults to a random value)

* optionally adjust file name (which defaults to a random value)
3) The plugin will depsosit the file relative to your source-directory and insert the markdown image element at the current cursor position:
![](docs/images/paste_result.jpg)


Created images will be added automatically to the VCS (if used). The plugin will remember the last directory pattern on a per file basis.


## Support

Feel welcome to suggest new features or to report issues.
Binary file added docs/images/paste_image_settings.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/images/paste_image_settings.png
Binary file not shown.
21 changes: 19 additions & 2 deletions src/img2md/ImageInsertSettingsPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

package img2md;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.border.TitledBorder;
import java.awt.*;

/**
* @author unknown
Expand All @@ -28,12 +28,18 @@ public JCheckBox getRoundCheckbox() {
return roundCheckbox;
}

public JTextField getDirectoryField() {
return directoryField;
}

private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
// Generated using JFormDesigner non-commercial license
panel1 = new JPanel();
label1 = new JLabel();
nameInput = new JTextField();
label3 = new JLabel();
directoryField = new JTextField();
whiteCheckbox = new JCheckBox();
roundCheckbox = new JCheckBox();
panel2 = new JPanel();
Expand Down Expand Up @@ -64,6 +70,15 @@ private void initComponents() {
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 0, 0));

//---- label3 ----
label3.setText("Directory Name");
panel1.add(label3, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 0, 0));
panel1.add(directoryField, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 0, 0));

//---- whiteCheckbox ----
whiteCheckbox.setText("Convert white to transparent");
whiteCheckbox.setSelected(true);
Expand Down Expand Up @@ -104,6 +119,8 @@ private void initComponents() {
private JPanel panel1;
private JLabel label1;
private JTextField nameInput;
private JLabel label3;
private JTextField directoryField;
private JCheckBox whiteCheckbox;
private JCheckBox roundCheckbox;
private JPanel panel2;
Expand Down
15 changes: 15 additions & 0 deletions src/img2md/ImageInsertSettingsPanel.jfd
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ new FormModel {
}, new FormLayoutConstraints( class com.jformdesigner.runtime.GridBagConstraintsEx ) {
"gridx": 1
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label3"
"text": "Directory Name"
}, new FormLayoutConstraints( class com.jformdesigner.runtime.GridBagConstraintsEx ) {
"gridy": 1
} )
add( new FormComponent( "javax.swing.JTextField" ) {
name: "directoryField"
auxiliary() {
"JavaCodeGenerator.variableGetter": true
}
}, new FormLayoutConstraints( class com.jformdesigner.runtime.GridBagConstraintsEx ) {
"gridx": 1
"gridy": 1
} )
add( new FormComponent( "javax.swing.JCheckBox" ) {
name: "whiteCheckbox"
"text": "Convert white to transparent"
Expand Down
53 changes: 39 additions & 14 deletions src/img2md/PasteImageFromClipboard.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package img2md;

import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
Expand All @@ -21,7 +22,6 @@
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.UUID;

Expand Down Expand Up @@ -56,9 +56,15 @@ public void actionPerformed(AnActionEvent e) {
}


// from http://stackoverflow.com/questions/17915688/intellij-plugin-get-code-from-current-open-file
Document currentDoc = FileEditorManager.getInstance(ed.getProject()).getSelectedTextEditor().getDocument();
VirtualFile currentFile = FileDocumentManager.getInstance().getFile(currentDoc);
File curDocument = new File(currentFile.getPath());


// add option to rescale image on the fly

ImageInsertSettingsPanel insertSettingsPanel = showDialog();
ImageInsertSettingsPanel insertSettingsPanel = showDialog(curDocument);
if (insertSettingsPanel == null) {
return;
}
Expand All @@ -78,18 +84,21 @@ public void actionPerformed(AnActionEvent e) {
}


// from http://stackoverflow.com/questions/17915688/intellij-plugin-get-code-from-current-open-file
Document currentDoc = FileEditorManager.getInstance(ed.getProject()).getSelectedTextEditor().getDocument();
VirtualFile currentFile = FileDocumentManager.getInstance().getFile(currentDoc);
File curDocument = new File(currentFile.getPath());

// make selectable
// File imageDir = new File(curDocument.getParent(), ".images");
File imageDir = new File(curDocument.getParent(), "."+ curDocument.getName().replace(".md", "").replace(".Rmd", "")+"_images");
String mdBaseName = curDocument.getName().replace(".md", "").replace(".Rmd", "");

// File imageDir = new File(curDocument.getParent(), "."+ mdBaseName +"_images");
String dirPattern = insertSettingsPanel.getDirectoryField().getText();

if(!imageDir.exists() || !imageDir.isDirectory()) imageDir.mkdir();

File imageFile = new File(imageDir, imageName + ".png");
File imageDir = new File(curDocument.getParent(), dirPattern.replace("{md_base_name}", mdBaseName));


if (!imageDir.exists() || !imageDir.isDirectory()) imageDir.mkdir();


File imageFile = new File(imageDir, imageName + ".png");

// todo should we silently override the image if it is already present?
save(bufferedImage, imageFile, "png");
Expand Down Expand Up @@ -119,26 +128,42 @@ public void actionPerformed(AnActionEvent e) {
assert fileByPath != null;

AbstractVcs usedVcs = ProjectLevelVcsManager.getInstance(ed.getProject()).getVcsFor(fileByPath);
if(usedVcs!=null && usedVcs.getCheckinEnvironment()!=null) {
if (usedVcs != null && usedVcs.getCheckinEnvironment() != null) {
usedVcs.getCheckinEnvironment().scheduleUnversionedFilesForAddition(Collections.singletonList(fileByPath));
}


// update directory pattern preferences for file and globally
PropertiesComponent.getInstance().setValue("PI__LAST_DIR_PATTERN", dirPattern);
PropertiesComponent.getInstance().setValue("PI__DIR_PATTERN_FOR_" + currentFile.getPath(), dirPattern);
}


private void insertImageElement(final @NotNull Editor editor, File imageFile) {
Runnable r = ()-> EditorModificationUtil.insertStringAtCaret(editor, "![]("+imageFile.toString()+")");
Runnable r = () -> EditorModificationUtil.insertStringAtCaret(editor, "![](" + imageFile.toString() + ")");

WriteCommandAction.runWriteCommandAction(editor.getProject(), r);
}


// for more examples see
// http://www.programcreek.com/java-api-examples/index.php?api=com.intellij.openapi.ui.DialogWrapper
private static ImageInsertSettingsPanel showDialog() {
private static ImageInsertSettingsPanel showDialog(File curDocument) {
DialogBuilder builder = new DialogBuilder();
ImageInsertSettingsPanel contentPanel = new ImageInsertSettingsPanel();
contentPanel.getNameInput().setText(UUID.randomUUID().toString().substring(0,8));

// restore directory pattern preferences for file and globally

PropertiesComponent propComp = PropertiesComponent.getInstance();
String dirPattern = propComp.getValue("PI__DIR_PATTERN_FOR_" + curDocument.getPath());
if (dirPattern == null) dirPattern = propComp.getValue("PI__LAST_DIR_PATTERN");
if (dirPattern == null) dirPattern = ".{md_base_name}_images";


contentPanel.getDirectoryField().setText(dirPattern);


contentPanel.getNameInput().setText(UUID.randomUUID().toString().substring(0, 8));
builder.setCenterPanel(contentPanel);
builder.setDimensionServiceKey("GrepConsoleSound");
builder.setTitle("Paste Image Settings");
Expand Down

0 comments on commit a3a98e1

Please sign in to comment.