Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.itsaky.androidide.plugins.base.PluginFragmentHelper
import com.itsaky.androidide.plugins.services.IdeFileService
import com.itsaky.androidide.plugins.services.IdeProjectService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File

class MarkdownPreviewFragment : Fragment() {
Expand Down Expand Up @@ -145,7 +147,9 @@ class MarkdownPreviewFragment : Fragment() {

private fun setupWebView() {
webView.settings.apply {
javaScriptEnabled = false
javaScriptEnabled = true
allowFileAccess = false
allowContentAccess = false
loadWithOverviewMode = true
useWideViewPort = true
builtInZoomControls = true
Expand Down Expand Up @@ -238,22 +242,35 @@ class MarkdownPreviewFragment : Fragment() {
}

private fun showProjectFilePicker() {
val project = projectService?.getCurrentProject() ?: return
val project = projectService?.getCurrentProject()
if (project == null) {
showMessage(R.string.no_project)
return
}
val rootDir = project.rootDir
viewLifecycleOwner.lifecycleScope.launch {
val files = findSupportedFiles(project.rootDir)
if (files.isEmpty()) return@launch
showFileSelectionDialog(files, project.rootDir)
val files = withContext(Dispatchers.IO) { findSupportedFiles(rootDir) }
if (files.isEmpty()) showMessage(R.string.no_files_found)
else showFileSelectionDialog(files, rootDir)
}
}

private fun findSupportedFiles(rootDir: File): List<File> {
return rootDir.walkTopDown()
.onEnter { it == rootDir || (it.name != "build" && !it.name.startsWith(".")) }
.filter { it.isFile && MarkdownPreviewerPlugin.isSupportedFile(it) }
.filter { !it.absolutePath.contains("/build/") && !it.absolutePath.contains("/.") }
.sortedBy { it.name.lowercase() }
.toList()
}

private fun showMessage(messageRes: Int) {
MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.select_from_project)
.setMessage(messageRes)
.setPositiveButton(android.R.string.ok, null)
.show()
}

private fun showFileSelectionDialog(files: List<File>, rootDir: File) {
val fileNames = files.map { it.absolutePath.removePrefix(rootDir.absolutePath + "/") }.toTypedArray()
MaterialAlertDialogBuilder(requireContext())
Expand Down
4 changes: 2 additions & 2 deletions plugins/Markdown-Previewer/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

<!-- Status Messages -->
<string name="loading">Loading…</string>
<string name="no_project">No project available</string>
<string name="no_files_found">No supported files found</string>
<string name="no_project">No project is open. Open a project, or use Storage to pick a file.</string>
<string name="no_files_found">This project has no Markdown or HTML files. Files under build/ and hidden folders are skipped; use Storage to open one of those.</string>
<string name="file_not_found">File not found</string>
<string name="cannot_read_file">Cannot read file</string>

Expand Down
Loading