]> _ Git - fluidbook-toolbox.git/commitdiff
wip #7634 @6:00
authorsoufiane <soufiane@cubedesigners.com>
Thu, 24 Jul 2025 16:28:00 +0000 (18:28 +0200)
committersoufiane <soufiane@cubedesigners.com>
Thu, 24 Jul 2025 16:28:00 +0000 (18:28 +0200)
app/Http/Controllers/Admin/Operations/FluidbookPublication/MarkdownOperation.php
resources/markdowneditor/js/markdowneditor.js
resources/markdowneditor/js/markdowneditor.undo.js
resources/markdowneditor/js/markdowneditor.versions.js
resources/views/fluidbook_publication/markdown_editor.blade.php

index 0fe0fad42fb44aa548b6a62747ff3664d7b6d0e9..8e7c1b0b8f0ecb2c54cba47bcee2edb2c77b08bc 100644 (file)
@@ -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']);
+    }
 }
index 0cd065d6701364fb46b7d6b43b05c255159bb8bd..10f2fecfe143b08fb4e6b5c2856cdbcb325997b2 100644 (file)
@@ -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)
         }
index 1796d9590a197d8c4e0285a72d26b29985c5b8f7..52d682cc8fe3b7d5eaffdcbc6261d0ef64ebce9c 100644 (file)
@@ -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()]++;
index d23ef972020f6311b493df349e52eed806f472c5..9cc57492a138557aa0324dffa6702dd5132b0bff 100644 (file)
@@ -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();
             },
index 862447dcb23ad6c3ea25d541b34a761a73b863f5..7c66caae0aadaa1925b7f5202493c79786890a1e 100644 (file)
     <script>
         var TRANSLATIONS = @json($translations);
         var FLUIDBOOK_DATA = @json($fbdata);
-        var MARKDOWN_DATA = @json($contents['pages']);
+        var MARKDOWN_DATA = @json($contents);
         console.log(MARKDOWN_DATA)
     </script>
     <script