diff --git a/modules/Backend/Http/Controllers/Backend/Appearance/EditorController.php b/modules/Backend/Http/Controllers/Backend/Appearance/EditorController.php index ec525ee09..7122582dd 100644 --- a/modules/Backend/Http/Controllers/Backend/Appearance/EditorController.php +++ b/modules/Backend/Http/Controllers/Backend/Appearance/EditorController.php @@ -24,7 +24,7 @@ class EditorController extends BackendController { - protected array $editSupportExtensions = ['twig']; + protected array $editSupportExtensions = ['twig', 'blade.php', 'tsx']; public function __construct(protected LocalThemeRepositoryContract $themeRepository) { @@ -34,15 +34,16 @@ public function index(Request $request, string $theme = null): View { $title = trans('cms::app.theme_editor'); $theme = $theme ?: jw_current_theme(); - $themePath = $this->themeRepository->find($theme)->getPath(); + $themePath = $this->themeRepository->find($theme)?->getPath(); $themes = $this->themeRepository->all(); $directories = $this->getThemeTree("{$themePath}/views", convert_linux_path($themePath)); - $file = $this->getCurrentFile($request); - $path = Crypt::decryptString($file); - if (!file_exists("{$themePath}/{$path}")) { - return abort(404); - } + $file = $this->getCurrentFile($request, $themePath); + //$path = Crypt::decryptString($file); + + /*if (!file_exists("{$themePath}/{$path}")) { + abort(404, "Cannot find file {$path}"); + }*/ return view( 'cms::backend.appearance.editor.index', @@ -69,6 +70,8 @@ public function getFileContent(Request $request, string $theme): JsonResponse $file = str_replace('..', '', $file); $repository = $this->themeRepository->find($theme); + throw_if($repository === null, new \RuntimeException('Theme not found')); + if (!$repository->fileExists($file)) { return abort(404); } @@ -84,9 +87,11 @@ public function getFileContent(Request $request, string $theme): JsonResponse public function save(EditorRequest $request, string $theme): JsonResponse|RedirectResponse { $file = Crypt::decryptString($request->input('file')); + $extension = pathinfo($file, PATHINFO_EXTENSION); $contents = $request->input('content'); $repository = $this->themeRepository->find($theme); - $extension = pathinfo($file, PATHINFO_EXTENSION); + + throw_if($repository === null, new \RuntimeException('Theme not found')); if (!in_array($extension, $this->editSupportExtensions)) { return $this->error("Unable to edit file {$extension}"); @@ -131,9 +136,7 @@ protected function getViewName($file): ?string return null; } - $view = str_replace('views/', '', $file); - $view = str_replace('.twig', '', $view); - $view = str_replace('/', '.', $view); + $view = str_replace(array('views/', '.twig', '/'), array('', '', '.'), $file); return 'theme::'.$view; } @@ -178,11 +181,27 @@ protected function getLanguageFile(string $file): string }; } - protected function getCurrentFile(Request $request): string + protected function getCurrentFile(Request $request, string $themePath): string { return $request->get( 'file', - Crypt::encryptString('views/index.twig') + Crypt::encryptString($this->findIndexFile($themePath)) ); } + + protected function findIndexFile(string $themePath): ?string + { + // Twig theme + if (File::exists("{$themePath}/views/index.twig")) { + return 'views/index.twig'; + } + + // Blade theme + if (File::exists("{$themePath}/views/index.blade.php")) { + return 'views/index.blade.php'; + } + + // Inertia theme + return 'views/index.tsx'; + } } diff --git a/modules/Backend/resources/views/backend/appearance/editor/index.blade.php b/modules/Backend/resources/views/backend/appearance/editor/index.blade.php index 7ee2da764..73110df78 100644 --- a/modules/Backend/resources/views/backend/appearance/editor/index.blade.php +++ b/modules/Backend/resources/views/backend/appearance/editor/index.blade.php @@ -26,7 +26,9 @@