Skip to content

Commit

Permalink
Merge pull request #344 from juzaweb/feature/fix-theme-editor-show-index
Browse files Browse the repository at this point in the history
feature/fix-theme-editor-show-index
  • Loading branch information
juzaweb committed Aug 5, 2023
2 parents 734de2f + 857f6b3 commit b475718
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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',
Expand All @@ -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);
}
Expand All @@ -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}");
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@

<select id="change-theme" class="form-control mt-2">
@foreach($themes as $name => $info)
<option value="{{ $name }}" @if($name == $theme) selected @endif>{{ $info->get('title') }}</option>
<option value="{{ $name }}" @if($name == $theme) selected @endif>
{{ $info->get('title') }}
</option>
@endforeach
</select>

Expand Down

0 comments on commit b475718

Please sign in to comment.