it should be possible to undo pasting an image into the project pane (IJPL-160554)

GitOrigin-RevId: 364544b6623b3f09e748a313202d7392d460de22
This commit is contained in:
Bas Leijdekkers
2024-08-19 20:28:36 +02:00
committed by intellij-monorepo-bot
parent f78ebb663a
commit 2a0ccb7079
2 changed files with 35 additions and 29 deletions

View File

@@ -76,6 +76,7 @@ action.Images.ShowBorder.description=Show border around the image
action.Images.ChangeBackground.text=Change Background action.Images.ChangeBackground.text=Change Background
action.Images.ChangeBackground.description=Change editor background action.Images.ChangeBackground.description=Change editor background
paste.image.command.name=Paste Image File ''{0}''
group.ImagesRootGroup.text=Images group.ImagesRootGroup.text=Images
black.cell.color.descriptor='Black' cell black.cell.color.descriptor='Black' cell
white.cell.color.descriptor='White' cell white.cell.color.descriptor='White' cell

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. // Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.intellij.images.ide package org.intellij.images.ide
import com.intellij.ide.PasteProvider import com.intellij.ide.PasteProvider
@@ -6,10 +6,13 @@ import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.CommonDataKeys import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.actionSystem.DataContext import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.application.runWriteAction import com.intellij.openapi.application.runWriteAction
import com.intellij.openapi.command.CommandProcessor
import com.intellij.openapi.command.UndoConfirmationPolicy
import com.intellij.openapi.diagnostic.logger import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.ide.CopyPasteManager import com.intellij.openapi.ide.CopyPasteManager
import com.intellij.openapi.vfs.VfsUtil import com.intellij.openapi.vfs.VfsUtil
import com.intellij.openapi.vfs.VirtualFile import com.intellij.openapi.vfs.VirtualFile
import org.intellij.images.ImagesBundle
import java.awt.Image import java.awt.Image
import java.awt.datatransfer.DataFlavor.imageFlavor import java.awt.datatransfer.DataFlavor.imageFlavor
import java.awt.image.BufferedImage import java.awt.image.BufferedImage
@@ -17,7 +20,6 @@ import java.awt.image.MultiResolutionImage
import java.io.IOException import java.io.IOException
import javax.imageio.ImageIO import javax.imageio.ImageIO
/** /**
* Represents a basic paste provider that allows to paste screenshots (from clipboard) as PNG files. * Represents a basic paste provider that allows to paste screenshots (from clipboard) as PNG files.
* *
@@ -63,41 +65,44 @@ open class ImagePasteProvider : PasteProvider {
return return
} }
runWriteAction { val fileName = VfsUtil.getNextAvailableName(newFileParent, "img", "png")
val nextAvailableName = VfsUtil.getNextAvailableName(newFileParent, "img", "png") val project = CommonDataKeys.PROJECT.getData(dataContext)
CommandProcessor.getInstance().executeCommand(project, {
runWriteAction {
// Step 2: Create file // Step 2: Create file
val imageFile = try { val imageFile = try {
newFileParent.createChildData(this, nextAvailableName) newFileParent.createChildData(this, fileName)
} }
catch (ioException: IOException) { catch (ioException: IOException) {
logger.error("Failed to create a pasted image file due to I/O error. Aborting operation.", ioException) logger.error("Failed to create a pasted image file due to I/O error. Aborting operation.", ioException)
null null
} ?: return@runWriteAction } ?: return@runWriteAction
// Step 3: Save image data to the created file // Step 3: Save image data to the created file
try {
imageFile.getOutputStream(this)
.use {
ImageIO.write(imageToPaste, "png", it)
}
}
catch (ioException: IOException) {
logger.error("Failed to save a pasted image to a file due to I/O error. Aborting operation", ioException)
// cleaning empty file
try { try {
imageFile.delete(this) imageFile.getOutputStream(this)
.use {
ImageIO.write(imageToPaste, "png", it)
}
} }
catch (ioException: IOException) { catch (ioException: IOException) {
// just skip it logger.error("Failed to save a pasted image to a file due to I/O error. Aborting operation", ioException)
// cleaning empty file
try {
imageFile.delete(this)
}
catch (_: IOException) {
// just skip it
}
return@runWriteAction
} }
return@runWriteAction imageFilePasted(dataContext, imageFile)
} }
}, ImagesBundle.message("paste.image.command.name", fileName), null, UndoConfirmationPolicy.REQUEST_CONFIRMATION)
imageFilePasted(dataContext, imageFile)
}
} }
open fun imageFilePasted(dataContext: DataContext, imageFile: VirtualFile) = Unit open fun imageFilePasted(dataContext: DataContext, imageFile: VirtualFile) = Unit