Skip to content

Commit 703c03b

Browse files
ADFA-5586 | Markdown Previewer: explain empty Project picks and render JS-driven HTML (#105)
fix(ADFA-5586): report empty Project picks and run page scripts in the previewer Project returned silently when no project was open or no file matched, and the only HTML in a fresh project (Gradle's problems report) lives under build/, which the picker skips. Show a dialog for both cases. Walk the project off the main thread and prune build/ and hidden directories instead of walking them and filtering afterwards. The problems report renders with JavaScript; with JS disabled its static "Loading..." body never changed. Enable JS, with file and content access disabled. Co-authored-by: Daniel Alome <astrocoder007@gmail.com>
1 parent 67192b7 commit 703c03b

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

‎plugins/Markdown-Previewer/src/main/kotlin/org/appdevforall/markdownpreviewer/fragments/MarkdownPreviewFragment.kt‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
3131
import com.itsaky.androidide.plugins.base.PluginFragmentHelper
3232
import com.itsaky.androidide.plugins.services.IdeFileService
3333
import com.itsaky.androidide.plugins.services.IdeProjectService
34+
import kotlinx.coroutines.Dispatchers
3435
import kotlinx.coroutines.launch
36+
import kotlinx.coroutines.withContext
3537
import java.io.File
3638

3739
class MarkdownPreviewFragment : Fragment() {
@@ -145,7 +147,9 @@ class MarkdownPreviewFragment : Fragment() {
145147

146148
private fun setupWebView() {
147149
webView.settings.apply {
148-
javaScriptEnabled = false
150+
javaScriptEnabled = true
151+
allowFileAccess = false
152+
allowContentAccess = false
149153
loadWithOverviewMode = true
150154
useWideViewPort = true
151155
builtInZoomControls = true
@@ -238,22 +242,35 @@ class MarkdownPreviewFragment : Fragment() {
238242
}
239243

240244
private fun showProjectFilePicker() {
241-
val project = projectService?.getCurrentProject() ?: return
245+
val project = projectService?.getCurrentProject()
246+
if (project == null) {
247+
showMessage(R.string.no_project)
248+
return
249+
}
250+
val rootDir = project.rootDir
242251
viewLifecycleOwner.lifecycleScope.launch {
243-
val files = findSupportedFiles(project.rootDir)
244-
if (files.isEmpty()) return@launch
245-
showFileSelectionDialog(files, project.rootDir)
252+
val files = withContext(Dispatchers.IO) { findSupportedFiles(rootDir) }
253+
if (files.isEmpty()) showMessage(R.string.no_files_found)
254+
else showFileSelectionDialog(files, rootDir)
246255
}
247256
}
248257

249258
private fun findSupportedFiles(rootDir: File): List<File> {
250259
return rootDir.walkTopDown()
260+
.onEnter { it == rootDir || (it.name != "build" && !it.name.startsWith(".")) }
251261
.filter { it.isFile && MarkdownPreviewerPlugin.isSupportedFile(it) }
252-
.filter { !it.absolutePath.contains("/build/") && !it.absolutePath.contains("/.") }
253262
.sortedBy { it.name.lowercase() }
254263
.toList()
255264
}
256265

266+
private fun showMessage(messageRes: Int) {
267+
MaterialAlertDialogBuilder(requireContext())
268+
.setTitle(R.string.select_from_project)
269+
.setMessage(messageRes)
270+
.setPositiveButton(android.R.string.ok, null)
271+
.show()
272+
}
273+
257274
private fun showFileSelectionDialog(files: List<File>, rootDir: File) {
258275
val fileNames = files.map { it.absolutePath.removePrefix(rootDir.absolutePath + "/") }.toTypedArray()
259276
MaterialAlertDialogBuilder(requireContext())

‎plugins/Markdown-Previewer/src/main/res/values/strings.xml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
<!-- Status Messages -->
2020
<string name="loading">Loading…</string>
21-
<string name="no_project">No project available</string>
22-
<string name="no_files_found">No supported files found</string>
21+
<string name="no_project">No project is open. Open a project, or use Storage to pick a file.</string>
22+
<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>
2323
<string name="file_not_found">File not found</string>
2424
<string name="cannot_read_file">Cannot read file</string>
2525

0 commit comments

Comments
 (0)