Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
alvince committed Aug 25, 2018
2 parents f352ec2 + ba10857 commit d07012c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,14 +18,15 @@ 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

/**
* Created by alvince on 2017/6/28.
*
* @author [email protected]
* @version 1.1.1, 2018/8/21
* @version 1.1.1, 2018/8/24
* @since 1.0
*/
class TinyPicUploadAction : TinifyAction() {
Expand All @@ -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) {
Expand Down
21 changes: 21 additions & 0 deletions src/main/kotlin/com/alvincezy/tinypic2/tinify/PluginStat.kt
Original file line number Diff line number Diff line change
@@ -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 [email protected]
* @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}"
}
14 changes: 14 additions & 0 deletions src/main/kotlin/com/alvincezy/tinypic2/tinify/tinify.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,17 @@ fun backupTinifySource(file: VirtualFile, refresh: Boolean = false) {
?.refresh(true, false)
}
}

fun getTopSelection(selections: Array<VirtualFile>): 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
}

0 comments on commit d07012c

Please sign in to comment.