From f42b1597e9fb1c6160b60eebaf833423395aeae3 Mon Sep 17 00:00:00 2001 From: soufiane Date: Thu, 24 Jul 2025 18:28:00 +0200 Subject: [PATCH] wip #7634 @6:00 --- .../MarkdownOperation.php | 29 ++++++++++++++++--- resources/markdowneditor/js/markdowneditor.js | 2 +- .../markdowneditor/js/markdowneditor.undo.js | 1 + .../js/markdowneditor.versions.js | 4 +-- .../markdown_editor.blade.php | 2 +- 5 files changed, 30 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/Admin/Operations/FluidbookPublication/MarkdownOperation.php b/app/Http/Controllers/Admin/Operations/FluidbookPublication/MarkdownOperation.php index 0fe0fad42..8e7c1b0b8 100644 --- a/app/Http/Controllers/Admin/Operations/FluidbookPublication/MarkdownOperation.php +++ b/app/Http/Controllers/Admin/Operations/FluidbookPublication/MarkdownOperation.php @@ -13,10 +13,12 @@ use Cubist\Util\Files\Files; trait MarkdownOperation { static $_names = []; + protected function setupMarkdownRoutes($segment, $routeName, $controller) { Route::match(['get'], $segment . '/{id}/edit/markdown', $controller . '@markdown')->name('fluidbook_markdowneditor'); Route::match(['get'], $segment . '/{id}/edit/markdown/versions', $controller . '@getMarkdownsVersions'); + Route::match(['get'], $segment . '/{id}/edit/markdown/versions/restore/{version}', $controller . '@restoreMarkdowns'); Route::match(['get'], $segment . '/{id}/markdown', $controller . '@getFilesById'); Route::match(['put'], $segment . '/{id}/save/markdown', $controller . '@saveMarkdown'); } @@ -28,20 +30,26 @@ trait MarkdownOperation } $token = Str::random(10); + $dir = self::getMarkdownsDir($id); + $file = $dir . 'latest.markdown3.gz'; + $basefile = $dir . 'latest.markdown.gz'; + $latest = json_decode(gzdecode(file_get_contents($file))) == "[]" ? gzdecode(file_get_contents($basefile)) : gzdecode(file_get_contents($file)); + //dd(gzdecode(file_get_contents($file)) === "[]"); $fluidbook=FluidbookPublication::find($id); - $contents=$fluidbook->getAccessibleContents(); + $isArray = gettype(json_decode($latest,true)); + $contents=($isArray === "array" ? json_decode($latest,true) : json_decode(json_decode($latest, true),true)) ?? $fluidbook->getAccessibleContents()['pages']; return view('fluidbook_publication.markdown_editor', ['contents' => $contents, 'version' => 'stable', 'id' => $id, 'fluidbook' => $fluidbook, 'access' => "", 'token' => $token]); } - protected function saveMarkdown($fluidbook_id) + protected function saveMarkdown($fluidbook_id, $message = '', $md = '[]') { if (!FluidbookPublication::hasPermission($fluidbook_id)) { abort(401); } - $markdowns = request('markdowns', '[]'); - $comments = request('message'); + $markdowns = request('markdowns', $md); + $comments = $message ?? request('message'); $user_id = backpack_user()->id; /** @var FluidbookPublication $fluidbook */ @@ -120,4 +128,17 @@ trait MarkdownOperation return static::$_names[$u]; } + protected function restoreMarkdowns($fluidbook_id, $version) + { + if (!FluidbookPublication::hasPermission($fluidbook_id)) { + abort(401); + } + + $dir = self::getMarkdownsDir($fluidbook_id); + $markdowns = Files::firstThatExists($dir . '/' . $version . '.markdown3.gz', $dir . '/' . $version . '.markdown.gz'); + $markdowns = gzdecode(file_get_contents($markdowns)); + $this->saveMarkdown($fluidbook_id, __('Restaurer la sauvegarde des liens :date', ['date' => date('Y-m-d H:i:s', $version)]), $markdowns); + + //return response()->json(['success' => 'ok']); + } } diff --git a/resources/markdowneditor/js/markdowneditor.js b/resources/markdowneditor/js/markdowneditor.js index 0cd065d67..10f2fecfe 100644 --- a/resources/markdowneditor/js/markdowneditor.js +++ b/resources/markdowneditor/js/markdowneditor.js @@ -162,7 +162,7 @@ MarkdownEditor.prototype = { this.editor.setMarkdown(state[lastKey]) this.editor.moveCursorToStart(true) }else { - this.contentMarkdown = MARKDOWN_DATA[this.currentPage] + this.contentMarkdown = MARKDOWN_DATA[this.currentPage] ?? '' this.editor.setMarkdown(this.contentMarkdown) this.editor.moveCursorToStart(true) } diff --git a/resources/markdowneditor/js/markdowneditor.undo.js b/resources/markdowneditor/js/markdowneditor.undo.js index 1796d9590..52d682cc8 100644 --- a/resources/markdowneditor/js/markdowneditor.undo.js +++ b/resources/markdowneditor/js/markdowneditor.undo.js @@ -79,6 +79,7 @@ MarkdowneditorUndo.prototype = { if (index > 0 && index < this.states[this.markdowneditor.getCurrentPage()].length) { this.states[this.markdowneditor.getCurrentPage()] = this.states[this.markdowneditor.getCurrentPage()].slice(0, index); } + MARKDOWN_DATA[this.markdowneditor.getCurrentPage()] = cs this.states[this.markdowneditor.getCurrentPage()].push(cs); this.scrolls[this.markdowneditor.getCurrentPage()].push($(".toastui-editor-md-preview").scrollTop()); this.indexes[this.markdowneditor.getCurrentPage()]++; diff --git a/resources/markdowneditor/js/markdowneditor.versions.js b/resources/markdowneditor/js/markdowneditor.versions.js index d23ef9720..9cc57492a 100644 --- a/resources/markdowneditor/js/markdowneditor.versions.js +++ b/resources/markdowneditor/js/markdowneditor.versions.js @@ -45,12 +45,12 @@ MarkdownVersions.prototype = { } //Restore links from 2022-12-07 13:37:15 - this.linkeditor.save.saveIfUnsavedChanges(TRANSLATIONS.before_restore_message,false,callback); + this.markdowneditor.save.saveIfUnsavedChanges(TRANSLATIONS.before_restore_message,false,callback); }, _restoreVersion(timestamp) { $.ajax({ - url: '/fluidbook-publication/' + FLUIDBOOK_DATA.id + '/edit/links/versions/restore/' + timestamp + '', + url: '/fluidbook-publication/' + FLUIDBOOK_DATA.id + '/edit/markdown/versions/restore/' + timestamp + '', success: function (data) { window.location.reload(); }, diff --git a/resources/views/fluidbook_publication/markdown_editor.blade.php b/resources/views/fluidbook_publication/markdown_editor.blade.php index 862447dcb..7c66caae0 100644 --- a/resources/views/fluidbook_publication/markdown_editor.blade.php +++ b/resources/views/fluidbook_publication/markdown_editor.blade.php @@ -130,7 +130,7 @@