From da1263a2ce8e6987fef7619d4981cdb47793bd62 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 18:40:03 +0100 Subject: [PATCH 01/68] Create LiveEditController.php --- .../src/Http/LiveEditController.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 packages/realtime-compiler/src/Http/LiveEditController.php diff --git a/packages/realtime-compiler/src/Http/LiveEditController.php b/packages/realtime-compiler/src/Http/LiveEditController.php new file mode 100644 index 00000000000..0cd89bbddfb --- /dev/null +++ b/packages/realtime-compiler/src/Http/LiveEditController.php @@ -0,0 +1,21 @@ + Date: Tue, 7 Nov 2023 20:57:39 +0100 Subject: [PATCH 02/68] Buffer page router contents This allows us to inject code before serving but without it affecting any compiled source saved to disk --- packages/realtime-compiler/src/Routing/PageRouter.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/realtime-compiler/src/Routing/PageRouter.php b/packages/realtime-compiler/src/Routing/PageRouter.php index a7754cc19be..9981e9a6651 100644 --- a/packages/realtime-compiler/src/Routing/PageRouter.php +++ b/packages/realtime-compiler/src/Routing/PageRouter.php @@ -63,12 +63,14 @@ protected function getHtml(HydePage $page): string } if (config('hyde.server.save_preview')) { - return file_get_contents(StaticPageBuilder::handle($page)); + $contents = file_get_contents(StaticPageBuilder::handle($page)); } else { Hyde::shareViewData($page); - return $page->compile(); + $contents = $page->compile(); } + + return $contents; } public static function handle(Request $request): Response From 3cde09a228b92b571ef7cd0f2e3b3ede639b706f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 12 Nov 2023 17:49:06 +0100 Subject: [PATCH 03/68] Add configuration option for the live edit feature --- config/hyde.php | 3 +++ packages/framework/config/hyde.php | 3 +++ 2 files changed, 6 insertions(+) diff --git a/config/hyde.php b/config/hyde.php index 0f83b8031a4..6c1089dad3a 100644 --- a/config/hyde.php +++ b/config/hyde.php @@ -418,6 +418,9 @@ // Should preview pages be saved to the output directory? 'save_preview' => true, + // Should the live edit feature be enabled? + 'live_edit' => true, + // Configure the realtime compiler dashboard 'dashboard' => [ // Should the realtime compiler dashboard be enabled? diff --git a/packages/framework/config/hyde.php b/packages/framework/config/hyde.php index 0f83b8031a4..6c1089dad3a 100644 --- a/packages/framework/config/hyde.php +++ b/packages/framework/config/hyde.php @@ -418,6 +418,9 @@ // Should preview pages be saved to the output directory? 'save_preview' => true, + // Should the live edit feature be enabled? + 'live_edit' => true, + // Configure the realtime compiler dashboard 'dashboard' => [ // Should the realtime compiler dashboard be enabled? From 49bc7a5922e80fa6c7b1ce570fd310a076c818db Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 18:45:39 +0100 Subject: [PATCH 04/68] Add enabled method --- packages/realtime-compiler/src/Http/LiveEditController.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/realtime-compiler/src/Http/LiveEditController.php b/packages/realtime-compiler/src/Http/LiveEditController.php index 0cd89bbddfb..10fb78f2f75 100644 --- a/packages/realtime-compiler/src/Http/LiveEditController.php +++ b/packages/realtime-compiler/src/Http/LiveEditController.php @@ -18,4 +18,9 @@ public function handle(): Response { // TODO: Implement handle() method. } + + public static function enabled(): bool + { + return config('hyde.server.live_edit', true); + } } From 766808bb39a20349fd72f7aa21534a27bb8922e7 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 18:45:57 +0100 Subject: [PATCH 05/68] Add new live edit API route to page router --- packages/realtime-compiler/src/Routing/PageRouter.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/realtime-compiler/src/Routing/PageRouter.php b/packages/realtime-compiler/src/Routing/PageRouter.php index 9981e9a6651..1aadd3f1d38 100644 --- a/packages/realtime-compiler/src/Routing/PageRouter.php +++ b/packages/realtime-compiler/src/Routing/PageRouter.php @@ -6,6 +6,7 @@ use Desilva\Microserve\Response; use Hyde\Foundation\Facades\Routes; use Hyde\Framework\Actions\StaticPageBuilder; +use Hyde\RealtimeCompiler\Http\LiveEditController; use Hyde\Framework\Features\Documentation\DocumentationSearchPage; use Hyde\Pages\Concerns\HydePage; use Hyde\RealtimeCompiler\Concerns\InteractsWithLaravel; @@ -36,6 +37,10 @@ protected function handlePageRequest(): Response return (new DashboardController($this->request))->handle(); } + if ($this->request->path === '/_hyde/live-edit' && LiveEditController::enabled()) { + return (new LiveEditController($this->request))->handle(); + } + return new HtmlResponse(200, 'OK', [ 'body' => $this->getHtml($this->getPageFromRoute()), ]); From 3feb14c93e7abca618e74cfe4abbf30b3b39d0f3 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 18:48:52 +0100 Subject: [PATCH 06/68] Authorize POST requests --- packages/realtime-compiler/src/Http/LiveEditController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/realtime-compiler/src/Http/LiveEditController.php b/packages/realtime-compiler/src/Http/LiveEditController.php index 10fb78f2f75..764d433af40 100644 --- a/packages/realtime-compiler/src/Http/LiveEditController.php +++ b/packages/realtime-compiler/src/Http/LiveEditController.php @@ -16,7 +16,7 @@ class LiveEditController extends BaseController public function handle(): Response { - // TODO: Implement handle() method. + $this->authorizePostRequest(); } public static function enabled(): bool From 4972dae67b5c048aea090eb9e6926cb4b4683c95 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 18:51:29 +0100 Subject: [PATCH 07/68] Add expectsJson method to base controller --- packages/realtime-compiler/src/Http/BaseController.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/realtime-compiler/src/Http/BaseController.php b/packages/realtime-compiler/src/Http/BaseController.php index 502c71be683..8e0e27e7311 100644 --- a/packages/realtime-compiler/src/Http/BaseController.php +++ b/packages/realtime-compiler/src/Http/BaseController.php @@ -110,4 +110,9 @@ protected function writeToConsole(string $message, string $context = 'dashboard' $this->console->printMessage($message, $context); } } + + protected function expectsJson(): bool + { + return getallheaders()['Accept'] === 'application/json'; + } } From 2d6647e52367af8d14640df0b37df0635b03e0fe Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 18:52:27 +0100 Subject: [PATCH 08/68] Handle HttpExceptions --- .../src/Http/LiveEditController.php | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/packages/realtime-compiler/src/Http/LiveEditController.php b/packages/realtime-compiler/src/Http/LiveEditController.php index 764d433af40..3481733ef44 100644 --- a/packages/realtime-compiler/src/Http/LiveEditController.php +++ b/packages/realtime-compiler/src/Http/LiveEditController.php @@ -4,7 +4,8 @@ namespace Hyde\RealtimeCompiler\Http; -use Desilva\Microserve\Response; +use Desilva\Microserve\JsonResponse; +use Symfony\Component\HttpKernel\Exception\HttpException; /** * @internal This class is not intended to be edited outside the Hyde Realtime Compiler. @@ -14,9 +15,24 @@ class LiveEditController extends BaseController protected bool $withConsoleOutput = true; protected bool $withSession = true; - public function handle(): Response + public function handle(): JsonResponse { - $this->authorizePostRequest(); + try { + $this->authorizePostRequest(); + + return $this->handleRequest(); + } catch (HttpException $exception) { + if ($this->expectsJson()) { + return $this->sendJsonErrorResponse($exception->getStatusCode(), $exception->getMessage()); + } + + throw $exception; + } + } + + protected function handleRequest(): JsonResponse + { + // } public static function enabled(): bool From 09e36208351a100993db48b40ccff9db69010cbf Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 19:05:24 +0100 Subject: [PATCH 09/68] Collect and validate required input --- packages/realtime-compiler/src/Http/LiveEditController.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/realtime-compiler/src/Http/LiveEditController.php b/packages/realtime-compiler/src/Http/LiveEditController.php index 3481733ef44..05f413851b5 100644 --- a/packages/realtime-compiler/src/Http/LiveEditController.php +++ b/packages/realtime-compiler/src/Http/LiveEditController.php @@ -32,6 +32,9 @@ public function handle(): JsonResponse protected function handleRequest(): JsonResponse { + $pagePath = $this->request->data['pagePath'] ?? $this->abort(400, 'Must provide page path'); + $content = $this->request->data['contentInput'] ?? $this->abort(400, 'Must provide content'); + // } From b06340ae293f0b14e9438ca6925f751883095376 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 19:09:02 +0100 Subject: [PATCH 10/68] Get and validate the page --- .../realtime-compiler/src/Http/LiveEditController.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/realtime-compiler/src/Http/LiveEditController.php b/packages/realtime-compiler/src/Http/LiveEditController.php index 05f413851b5..0f94f9b7e0b 100644 --- a/packages/realtime-compiler/src/Http/LiveEditController.php +++ b/packages/realtime-compiler/src/Http/LiveEditController.php @@ -4,7 +4,9 @@ namespace Hyde\RealtimeCompiler\Http; +use Hyde\Hyde; use Desilva\Microserve\JsonResponse; +use Hyde\Pages\Concerns\BaseMarkdownPage; use Symfony\Component\HttpKernel\Exception\HttpException; /** @@ -35,6 +37,12 @@ protected function handleRequest(): JsonResponse $pagePath = $this->request->data['pagePath'] ?? $this->abort(400, 'Must provide page path'); $content = $this->request->data['contentInput'] ?? $this->abort(400, 'Must provide content'); + $page = Hyde::pages()->getPage($pagePath); + + if (! $page instanceof BaseMarkdownPage) { + $this->abort(400, 'Page is not a markdown page'); + } + // } From e3923d5115b4b529d49cb0df21dae22549af7985 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 19:13:08 +0100 Subject: [PATCH 11/68] Save the page Markdown --- packages/realtime-compiler/src/Http/LiveEditController.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/realtime-compiler/src/Http/LiveEditController.php b/packages/realtime-compiler/src/Http/LiveEditController.php index 0f94f9b7e0b..83d970cdbca 100644 --- a/packages/realtime-compiler/src/Http/LiveEditController.php +++ b/packages/realtime-compiler/src/Http/LiveEditController.php @@ -5,6 +5,7 @@ namespace Hyde\RealtimeCompiler\Http; use Hyde\Hyde; +use Hyde\Markdown\Models\Markdown; use Desilva\Microserve\JsonResponse; use Hyde\Pages\Concerns\BaseMarkdownPage; use Symfony\Component\HttpKernel\Exception\HttpException; @@ -43,6 +44,9 @@ protected function handleRequest(): JsonResponse $this->abort(400, 'Page is not a markdown page'); } + $page->markdown = new Markdown($content); + $page->save(); + // } From 7b7a1c9756e68ea3fd24ff2ad45703e23ff90d3c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 19:14:27 +0100 Subject: [PATCH 12/68] Return with success response --- packages/realtime-compiler/src/Http/LiveEditController.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/realtime-compiler/src/Http/LiveEditController.php b/packages/realtime-compiler/src/Http/LiveEditController.php index 83d970cdbca..e46f7811e78 100644 --- a/packages/realtime-compiler/src/Http/LiveEditController.php +++ b/packages/realtime-compiler/src/Http/LiveEditController.php @@ -47,7 +47,9 @@ protected function handleRequest(): JsonResponse $page->markdown = new Markdown($content); $page->save(); - // + return new JsonResponse(200, 'OK', [ + 'message' => 'Page saved successfully.', + ]); } public static function enabled(): bool From bfd3d3a6825f07e6675753fb5c91aaea87dd56e0 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 20:05:00 +0100 Subject: [PATCH 13/68] Scaffold live edit resource files --- packages/realtime-compiler/resources/live-edit.blade.php | 0 packages/realtime-compiler/resources/live-edit.css | 0 packages/realtime-compiler/resources/live-edit.js | 0 3 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 packages/realtime-compiler/resources/live-edit.blade.php create mode 100644 packages/realtime-compiler/resources/live-edit.css create mode 100644 packages/realtime-compiler/resources/live-edit.js diff --git a/packages/realtime-compiler/resources/live-edit.blade.php b/packages/realtime-compiler/resources/live-edit.blade.php new file mode 100644 index 00000000000..e69de29bb2d diff --git a/packages/realtime-compiler/resources/live-edit.css b/packages/realtime-compiler/resources/live-edit.css new file mode 100644 index 00000000000..e69de29bb2d diff --git a/packages/realtime-compiler/resources/live-edit.js b/packages/realtime-compiler/resources/live-edit.js new file mode 100644 index 00000000000..e69de29bb2d From 083dd8d84559177f6b15909ce5f1668533669790 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 20:09:47 +0100 Subject: [PATCH 14/68] Create accessor to get live edit resources --- .../realtime-compiler/src/Http/LiveEditController.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/realtime-compiler/src/Http/LiveEditController.php b/packages/realtime-compiler/src/Http/LiveEditController.php index e46f7811e78..e3dd39e9a9b 100644 --- a/packages/realtime-compiler/src/Http/LiveEditController.php +++ b/packages/realtime-compiler/src/Http/LiveEditController.php @@ -7,6 +7,7 @@ use Hyde\Hyde; use Hyde\Markdown\Models\Markdown; use Desilva\Microserve\JsonResponse; +use Illuminate\Support\Facades\Blade; use Hyde\Pages\Concerns\BaseMarkdownPage; use Symfony\Component\HttpKernel\Exception\HttpException; @@ -56,4 +57,13 @@ public static function enabled(): bool { return config('hyde.server.live_edit', true); } + + public static function injectLiveEditScript(string $html): string + { + return str_replace('', sprintf('%s', Blade::render(file_get_contents(__DIR__.'/../../resources/live-edit.blade.php'), [ + 'styles' => file_get_contents(__DIR__.'/../../resources/live-edit.css'), + 'scripts' => file_get_contents(__DIR__.'/../../resources/live-edit.js'), + 'csrfToken' => self::generateCSRFToken(), + ])), $html); + } } From 9166d4e2623ee19434111b2ab5da89579b1a32c6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 20:12:02 +0100 Subject: [PATCH 15/68] Update page router to inject live edit scripts when enabled --- packages/realtime-compiler/src/Routing/PageRouter.php | 5 +++++ packages/realtime-compiler/tests/RealtimeCompilerTest.php | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/packages/realtime-compiler/src/Routing/PageRouter.php b/packages/realtime-compiler/src/Routing/PageRouter.php index 1aadd3f1d38..1e41b531bd4 100644 --- a/packages/realtime-compiler/src/Routing/PageRouter.php +++ b/packages/realtime-compiler/src/Routing/PageRouter.php @@ -5,6 +5,7 @@ use Desilva\Microserve\Request; use Desilva\Microserve\Response; use Hyde\Foundation\Facades\Routes; +use Hyde\Pages\Concerns\BaseMarkdownPage; use Hyde\Framework\Actions\StaticPageBuilder; use Hyde\RealtimeCompiler\Http\LiveEditController; use Hyde\Framework\Features\Documentation\DocumentationSearchPage; @@ -75,6 +76,10 @@ protected function getHtml(HydePage $page): string $contents = $page->compile(); } + if ($page instanceof BaseMarkdownPage && LiveEditController::enabled()) { + $contents = LiveEditController::injectLiveEditScript($contents); + } + return $contents; } diff --git a/packages/realtime-compiler/tests/RealtimeCompilerTest.php b/packages/realtime-compiler/tests/RealtimeCompilerTest.php index 981f4b53df5..9f48e9467a9 100644 --- a/packages/realtime-compiler/tests/RealtimeCompilerTest.php +++ b/packages/realtime-compiler/tests/RealtimeCompilerTest.php @@ -17,6 +17,10 @@ ob_start(); +beforeEach(function () { + putenv('SERVER_LIVE_EDIT=false'); +}); + test('handle routes index page', function () { putenv('SERVER_DASHBOARD=false'); mockRoute(''); From 6b4230348b48e0dbd4a3b78e055af74421b61dca Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 20:52:03 +0100 Subject: [PATCH 16/68] Add environment variable for the live edit feature --- config/hyde.php | 2 +- packages/framework/config/hyde.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config/hyde.php b/config/hyde.php index 6c1089dad3a..b05f8683950 100644 --- a/config/hyde.php +++ b/config/hyde.php @@ -419,7 +419,7 @@ 'save_preview' => true, // Should the live edit feature be enabled? - 'live_edit' => true, + 'live_edit' => env('SERVER_LIVE_EDIT', true), // Configure the realtime compiler dashboard 'dashboard' => [ diff --git a/packages/framework/config/hyde.php b/packages/framework/config/hyde.php index 6c1089dad3a..b05f8683950 100644 --- a/packages/framework/config/hyde.php +++ b/packages/framework/config/hyde.php @@ -419,7 +419,7 @@ 'save_preview' => true, // Should the live edit feature be enabled? - 'live_edit' => true, + 'live_edit' => env('SERVER_LIVE_EDIT', true), // Configure the realtime compiler dashboard 'dashboard' => [ From 54b7a4f68b6ad3ac2e8cd88f0a60445054594f9e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 21:27:05 +0100 Subject: [PATCH 17/68] Create base live edit markup --- .../resources/live-edit.blade.php | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/packages/realtime-compiler/resources/live-edit.blade.php b/packages/realtime-compiler/resources/live-edit.blade.php index e69de29bb2d..efbe61210ee 100644 --- a/packages/realtime-compiler/resources/live-edit.blade.php +++ b/packages/realtime-compiler/resources/live-edit.blade.php @@ -0,0 +1,33 @@ +
+ + @php + /** @var \Hyde\Pages\Concerns\BaseMarkdownPage $page */ + $markdown = $page->markdown()->body(); + @endphp + + + + +
From 4e2bfafe29a6523dab86cbe8570eaf15e40e4337 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 21:33:21 +0100 Subject: [PATCH 18/68] Simplify function signature --- packages/realtime-compiler/resources/live-edit.blade.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/realtime-compiler/resources/live-edit.blade.php b/packages/realtime-compiler/resources/live-edit.blade.php index efbe61210ee..5995528465a 100644 --- a/packages/realtime-compiler/resources/live-edit.blade.php +++ b/packages/realtime-compiler/resources/live-edit.blade.php @@ -25,9 +25,5 @@ - + From ec4f02fe8c8ab03fea5e14eb31628a9a2e628a42 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 21:36:06 +0100 Subject: [PATCH 19/68] Render markdown into textarea --- packages/realtime-compiler/resources/live-edit.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/realtime-compiler/resources/live-edit.blade.php b/packages/realtime-compiler/resources/live-edit.blade.php index 5995528465a..7d7021f9f75 100644 --- a/packages/realtime-compiler/resources/live-edit.blade.php +++ b/packages/realtime-compiler/resources/live-edit.blade.php @@ -20,7 +20,7 @@ - + From 42ae9824593b17a4388e5e7237b1edcee6b0c4f6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 21:36:32 +0100 Subject: [PATCH 20/68] Create function to initialize live editor --- packages/realtime-compiler/resources/live-edit.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/realtime-compiler/resources/live-edit.js b/packages/realtime-compiler/resources/live-edit.js index e69de29bb2d..9e1cb91f327 100644 --- a/packages/realtime-compiler/resources/live-edit.js +++ b/packages/realtime-compiler/resources/live-edit.js @@ -0,0 +1,3 @@ +function initLiveEdit() { + // +} From dd6ed11095f9a399f622d0701dfba5540eda46f6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 21:37:05 +0100 Subject: [PATCH 21/68] Get the article element --- packages/realtime-compiler/resources/live-edit.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/realtime-compiler/resources/live-edit.js b/packages/realtime-compiler/resources/live-edit.js index 9e1cb91f327..47a04dbf593 100644 --- a/packages/realtime-compiler/resources/live-edit.js +++ b/packages/realtime-compiler/resources/live-edit.js @@ -1,3 +1,14 @@ function initLiveEdit() { - // + function getArticle() { + let article = document.querySelector('#content > article'); + + if (article === null) { + // If no article element is found the user may have a custom template, so we cannot know which element to edit. + throw new Error('No article element found, cannot live edit. If you are using a custom template, please make sure to include an article element in the #content container.'); + } + + return article; + } + + const article = getArticle(); } From 8eae7fd6f97f5dfb7fe9860e32025702682fb1df Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 21:39:55 +0100 Subject: [PATCH 22/68] Create base double click event listener function --- packages/realtime-compiler/resources/live-edit.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/realtime-compiler/resources/live-edit.js b/packages/realtime-compiler/resources/live-edit.js index 47a04dbf593..9808d55b7c5 100644 --- a/packages/realtime-compiler/resources/live-edit.js +++ b/packages/realtime-compiler/resources/live-edit.js @@ -10,5 +10,11 @@ function initLiveEdit() { return article; } + function switchToEditor() { + // TODO + } + const article = getArticle(); + + article.addEventListener('dblclick', switchToEditor); } From c569a1970e795fdc99947fe954fba87bde0a763a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 21:42:23 +0100 Subject: [PATCH 23/68] Sketch out editor switching logic --- .../realtime-compiler/resources/live-edit.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/realtime-compiler/resources/live-edit.js b/packages/realtime-compiler/resources/live-edit.js index 9808d55b7c5..5a55e93faac 100644 --- a/packages/realtime-compiler/resources/live-edit.js +++ b/packages/realtime-compiler/resources/live-edit.js @@ -11,7 +11,23 @@ function initLiveEdit() { } function switchToEditor() { - // TODO + function hasEditorBeenSetUp() { + return document.querySelector('#live-edit-container') === null; + } + + function setupEditor() { + // + } + + function showEditor() { + // + } + + if (! hasEditorBeenSetUp()) { + setupEditor(); + } else { + showEditor(); + } } const article = getArticle(); From 5fb295eb9383760963a92216cbcb2b6f374b27cd Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 21:42:30 +0100 Subject: [PATCH 24/68] Flip if-else --- packages/realtime-compiler/resources/live-edit.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/realtime-compiler/resources/live-edit.js b/packages/realtime-compiler/resources/live-edit.js index 5a55e93faac..739e2e59339 100644 --- a/packages/realtime-compiler/resources/live-edit.js +++ b/packages/realtime-compiler/resources/live-edit.js @@ -23,10 +23,10 @@ function initLiveEdit() { // } - if (! hasEditorBeenSetUp()) { - setupEditor(); - } else { + if (hasEditorBeenSetUp()) { showEditor(); + } else { + setupEditor(); } } From df568f6c49d71362afd673fd39adb6010c5d6d55 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 21:46:52 +0100 Subject: [PATCH 25/68] Extract function --- packages/realtime-compiler/resources/live-edit.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/realtime-compiler/resources/live-edit.js b/packages/realtime-compiler/resources/live-edit.js index 739e2e59339..1b6fd81361c 100644 --- a/packages/realtime-compiler/resources/live-edit.js +++ b/packages/realtime-compiler/resources/live-edit.js @@ -11,8 +11,12 @@ function initLiveEdit() { } function switchToEditor() { + function getLiveEditor() { + return document.querySelector('#live-edit-container'); + } + function hasEditorBeenSetUp() { - return document.querySelector('#live-edit-container') === null; + return getLiveEditor() === null; } function setupEditor() { From 7500b4df80f9b1fe487fdb2f792829e485a8e36d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 21:47:09 +0100 Subject: [PATCH 26/68] Invert faulty boolean logic --- packages/realtime-compiler/resources/live-edit.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/realtime-compiler/resources/live-edit.js b/packages/realtime-compiler/resources/live-edit.js index 1b6fd81361c..5a88f12edad 100644 --- a/packages/realtime-compiler/resources/live-edit.js +++ b/packages/realtime-compiler/resources/live-edit.js @@ -16,7 +16,7 @@ function initLiveEdit() { } function hasEditorBeenSetUp() { - return getLiveEditor() === null; + return getLiveEditor() !== null; } function setupEditor() { From ca5f77d5c10554c67ab5b7d5af7c669a3f258df4 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 21:49:03 +0100 Subject: [PATCH 27/68] Implement editor setup function --- packages/realtime-compiler/resources/live-edit.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/realtime-compiler/resources/live-edit.js b/packages/realtime-compiler/resources/live-edit.js index 5a88f12edad..8fcd2671e1c 100644 --- a/packages/realtime-compiler/resources/live-edit.js +++ b/packages/realtime-compiler/resources/live-edit.js @@ -20,7 +20,12 @@ function initLiveEdit() { } function setupEditor() { - // + const template = document.getElementById('live-edit-template'); + const editor = document.importNode(template.content, true); + const article = getArticle(); + + article.parentNode.insertBefore(editor, article.nextSibling); + article.style.display = 'none'; } function showEditor() { From 293b2a9a6ed21adaafd52a80fe980567fe3f09ee Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 21:51:33 +0100 Subject: [PATCH 28/68] Implement show editor function --- packages/realtime-compiler/resources/live-edit.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/realtime-compiler/resources/live-edit.js b/packages/realtime-compiler/resources/live-edit.js index 8fcd2671e1c..82a347768d5 100644 --- a/packages/realtime-compiler/resources/live-edit.js +++ b/packages/realtime-compiler/resources/live-edit.js @@ -29,7 +29,8 @@ function initLiveEdit() { } function showEditor() { - // + article.style.display = 'none'; + getLiveEditor().style.display = ''; } if (hasEditorBeenSetUp()) { From b86e7157aca641dd325d38b59ba87e5c97a86d1e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 21:51:53 +0100 Subject: [PATCH 29/68] Use helper function --- packages/realtime-compiler/resources/live-edit.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/realtime-compiler/resources/live-edit.js b/packages/realtime-compiler/resources/live-edit.js index 82a347768d5..c25f1f8fb73 100644 --- a/packages/realtime-compiler/resources/live-edit.js +++ b/packages/realtime-compiler/resources/live-edit.js @@ -25,7 +25,8 @@ function initLiveEdit() { const article = getArticle(); article.parentNode.insertBefore(editor, article.nextSibling); - article.style.display = 'none'; + + showEditor(); } function showEditor() { From 134931f2f327bd69a8d9014262efbe3b64475223 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 21:56:44 +0100 Subject: [PATCH 30/68] Create function to hide editor --- packages/realtime-compiler/resources/live-edit.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/realtime-compiler/resources/live-edit.js b/packages/realtime-compiler/resources/live-edit.js index c25f1f8fb73..894b722dcb4 100644 --- a/packages/realtime-compiler/resources/live-edit.js +++ b/packages/realtime-compiler/resources/live-edit.js @@ -34,6 +34,11 @@ function initLiveEdit() { getLiveEditor().style.display = ''; } + function hideEditor() { + article.style.display = ''; + getLiveEditor().style.display = 'none'; + } + if (hasEditorBeenSetUp()) { showEditor(); } else { From 1792f8bbd41e62f86a9689b75d31cf0fa13ffe5e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 21:58:49 +0100 Subject: [PATCH 31/68] Add event listener to hide editor on cancel button click --- packages/realtime-compiler/resources/live-edit.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/realtime-compiler/resources/live-edit.js b/packages/realtime-compiler/resources/live-edit.js index 894b722dcb4..c284c58e770 100644 --- a/packages/realtime-compiler/resources/live-edit.js +++ b/packages/realtime-compiler/resources/live-edit.js @@ -27,6 +27,10 @@ function initLiveEdit() { article.parentNode.insertBefore(editor, article.nextSibling); showEditor(); + + document.getElementById('liveEditCancel').addEventListener('click', function () { + hideEditor(); + }); } function showEditor() { From c90e82f3831836379770962748642bf391e5cb0b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 13 Nov 2023 22:00:07 +0100 Subject: [PATCH 32/68] Use function name instead of closure wrapper --- packages/realtime-compiler/resources/live-edit.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/realtime-compiler/resources/live-edit.js b/packages/realtime-compiler/resources/live-edit.js index c284c58e770..072d18202a7 100644 --- a/packages/realtime-compiler/resources/live-edit.js +++ b/packages/realtime-compiler/resources/live-edit.js @@ -28,9 +28,7 @@ function initLiveEdit() { showEditor(); - document.getElementById('liveEditCancel').addEventListener('click', function () { - hideEditor(); - }); + document.getElementById('liveEditCancel').addEventListener('click', hideEditor); } function showEditor() { From 394976ea88cb8011d46c760ca2ebe8ab39acaf02 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 14 Nov 2023 10:35:14 +0100 Subject: [PATCH 33/68] Focus on the textarea --- packages/realtime-compiler/resources/live-edit.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/realtime-compiler/resources/live-edit.js b/packages/realtime-compiler/resources/live-edit.js index 072d18202a7..23deecf185f 100644 --- a/packages/realtime-compiler/resources/live-edit.js +++ b/packages/realtime-compiler/resources/live-edit.js @@ -34,6 +34,7 @@ function initLiveEdit() { function showEditor() { article.style.display = 'none'; getLiveEditor().style.display = ''; + focusOnTextarea(); } function hideEditor() { @@ -41,6 +42,13 @@ function initLiveEdit() { getLiveEditor().style.display = 'none'; } + function focusOnTextarea() { + const textarea = getLiveEditor().querySelector('textarea'); + + textarea.selectionStart = textarea.value.length; + textarea.focus(); + } + if (hasEditorBeenSetUp()) { showEditor(); } else { From 16391d8797af4d66d80cdcb340f253566d5db0d6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 14 Nov 2023 10:40:51 +0100 Subject: [PATCH 34/68] Add form identifier --- packages/realtime-compiler/resources/live-edit.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/realtime-compiler/resources/live-edit.blade.php b/packages/realtime-compiler/resources/live-edit.blade.php index 7d7021f9f75..db3c40e53be 100644 --- a/packages/realtime-compiler/resources/live-edit.blade.php +++ b/packages/realtime-compiler/resources/live-edit.blade.php @@ -7,7 +7,7 @@