diff --git a/src/main/kotlin/com/alvincezy/tinypic2/actions/TinyPicUploadAction.kt b/src/main/kotlin/com/alvincezy/tinypic2/actions/TinyPicUploadAction.kt index b2aee0c..e7e924e 100644 --- a/src/main/kotlin/com/alvincezy/tinypic2/actions/TinyPicUploadAction.kt +++ b/src/main/kotlin/com/alvincezy/tinypic2/actions/TinyPicUploadAction.kt @@ -3,7 +3,9 @@ package com.alvincezy.tinypic2.actions import com.alvincezy.tinypic2.* import com.alvincezy.tinypic2.exts.supportTinify import com.alvincezy.tinypic2.model.VirtualFileAware +import com.alvincezy.tinypic2.tinify.PluginStat import com.alvincezy.tinypic2.tinify.TinifyBackgroundTask +import com.alvincezy.tinypic2.tinify.getTopSelection import com.alvincezy.tinypic2.tinify.prepare import com.intellij.openapi.actionSystem.AnActionEvent import com.intellij.openapi.diagnostic.Logger @@ -16,6 +18,7 @@ import com.intellij.openapi.project.Project import com.intellij.openapi.ui.Messages import com.intellij.openapi.vfs.VfsUtilCore import com.intellij.openapi.vfs.VirtualFile +import com.intellij.openapi.vfs.VirtualFileManager import com.intellij.openapi.vfs.VirtualFileVisitor import com.tinify.Tinify @@ -23,7 +26,7 @@ import com.tinify.Tinify * Created by alvince on 2017/6/28. * * @author alvince.zy@gmail.com - * @version 1.1.1, 2018/8/21 + * @version 1.1.1, 2018/8/24 * @since 1.0 */ class TinyPicUploadAction : TinifyAction() { @@ -47,11 +50,19 @@ class TinyPicUploadAction : TinifyAction() { private fun pickAndTinify(project: Project) { val descriptor = FileChooserDescriptor(true, true, false, false, false, true) - val selectedFiles = FileChooser.chooseFiles(descriptor, project, project.baseDir) + val urlSelectTo = PluginStat.urlSelectToTinify(project) + console("base directory: $urlSelectTo") + val selectedFiles = FileChooser.chooseFiles(descriptor, project, + VirtualFileManager.getInstance().findFileByUrl(urlSelectTo)) if (selectedFiles.isEmpty()) { return@pickAndTinify } + val rootSelection = getTopSelection(selectedFiles) + if (rootSelection != null) { + console("select root dir: $rootSelection") + PluginStat.pickFileDefault = rootSelection.path + } enable(false) ProgressManager.getInstance().run(object : Task.Backgroundable(project, Constants.APP_NAME, true) { override fun run(indicator: ProgressIndicator) { diff --git a/src/main/kotlin/com/alvincezy/tinypic2/tinify/PluginStat.kt b/src/main/kotlin/com/alvincezy/tinypic2/tinify/PluginStat.kt new file mode 100644 index 0000000..1234db1 --- /dev/null +++ b/src/main/kotlin/com/alvincezy/tinypic2/tinify/PluginStat.kt @@ -0,0 +1,21 @@ +package com.alvincezy.tinypic2.tinify + +import com.intellij.openapi.project.Project + +/** + * Singleton plugin stat wrapper + * + * Created by alvince on 18-8-24. + * + * @author alvince.zy@gmail.com + * @version 1.1.1, 2018/8/24 + */ +object PluginStat { + + var pickFileDefault: String = "" + + /** + * Obtain files dir-url select to + */ + fun urlSelectToTinify(project: Project): String = "file://${if (pickFileDefault.isEmpty()) project.baseDir.path else pickFileDefault}" +} diff --git a/src/main/kotlin/com/alvincezy/tinypic2/tinify/tinify.kt b/src/main/kotlin/com/alvincezy/tinypic2/tinify/tinify.kt index 1732832..108dd05 100644 --- a/src/main/kotlin/com/alvincezy/tinypic2/tinify/tinify.kt +++ b/src/main/kotlin/com/alvincezy/tinypic2/tinify/tinify.kt @@ -35,3 +35,17 @@ fun backupTinifySource(file: VirtualFile, refresh: Boolean = false) { ?.refresh(true, false) } } + +fun getTopSelection(selections: Array): VirtualFile? { + if (selections.isEmpty()) return null + + var root = selections[0] + selections.forEach { + val filePath = if (it.isDirectory) it.path else it.parent.path + + if (root.path.contains(filePath)) { + root = it + } + } + return root +}