From: Vincent Vanwaelscappel Date: Thu, 8 Dec 2022 15:30:52 +0000 (+0100) Subject: wip #5631 @2 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=bc91d68c9fd02ef45f44c89c8078f7a7ab35c48c;p=fluidbook-toolbox.git wip #5631 @2 --- diff --git a/app/Http/Controllers/Admin/Operations/FluidbookPublication/EditOperation.php b/app/Http/Controllers/Admin/Operations/FluidbookPublication/EditOperation.php index 6c489671d..609de1634 100644 --- a/app/Http/Controllers/Admin/Operations/FluidbookPublication/EditOperation.php +++ b/app/Http/Controllers/Admin/Operations/FluidbookPublication/EditOperation.php @@ -6,7 +6,9 @@ use App\Models\FluidbookDocument; use App\Models\FluidbookPublication; use App\Util\FluidbookLinks; use Cubist\Backpack\Http\Controllers\Base\XSendFileController; +use Cubist\Util\Files\Files; use Illuminate\Support\Facades\Route; +use PhpOffice\PhpSpreadsheet\Writer\Xlsx; trait EditOperation { @@ -15,6 +17,8 @@ trait EditOperation { Route::match(['get'], $segment . '/{id}/edit/links', $controller . '@links'); Route::match(['get'], $segment . '/{id}/edit/links/versions', $controller . '@getLinkVersions'); + Route::match(['get'], $segment . '/{id}/edit/links/versions/export/{version}', $controller . '@exportLinks'); + Route::match(['get'], $segment . '/{id}/edit/links/versions/restore/{version}', $controller . '@restoreLinks'); Route::put($segment . '/{id}/save/links', $controller . '@saveLinks'); Route::get($segment . '/{id}/edit/{type}_{page}.{format}', $controller . '@getLinkPage') // ->whereIn('type', ['raster', 'images', 'texts', 'vector']) @@ -71,6 +75,31 @@ trait EditOperation return response()->json($links); } + protected function exportLinks($fluidbook_id, $version) + { + if (!FluidbookPublication::hasPermission($fluidbook_id)) { + abort(401); + } + FluidbookLinks::getLinksAndRulers($fluidbook_id, $links, $rulers, $version); + $xlsx = FluidbookLinks::linksToExcel($links, $rulers); + $tmpfile = Files::tempnam() . '.xlsx'; + $writer = new Xlsx($xlsx); + $writer->save($tmpfile); + return response()->file($tmpfile)->deleteFileAfterSend(); + } + + protected function restoreLinks($fluidbook_id, $version) + { + if (!FluidbookPublication::hasPermission($fluidbook_id)) { + abort(401); + } + $comments = 'Restore links from ' . date('Y-m-d H:i:s', $version); + + // New way + FluidbookLinks::getLinksAndRulers($fluidbook_id, $links, $rulers, $version); + FluidbookLinks::saveLinksInFile($fluidbook_id, backpack_user()->id, $comments, $links, $rulers, [], []); + } + // protected function getThumb($doc_id, $doc_page) // { // diff --git a/app/Util/FluidbookLinks.php b/app/Util/FluidbookLinks.php index b9b4c6ff7..01ad6a19a 100644 --- a/app/Util/FluidbookLinks.php +++ b/app/Util/FluidbookLinks.php @@ -52,7 +52,7 @@ class FluidbookLinks $s->setTitle('Links'); // Labels - $i = 0; + $i = 1; foreach ($cols as $id => $label) { $s->setCellValueByColumnAndRow($i, 1, $id); $s->getColumnDimensionByColumn($i)->setAutoSize(true); @@ -64,7 +64,7 @@ class FluidbookLinks self::_correctImageSpecialLinks($links); $j = 2; foreach ($links as $l) { - $i = 0; + $i = 1; foreach ($cols as $id => $label) { if (($id == 'document_id' || $id == 'document_page')) { if (!is_null($pages)) { @@ -103,7 +103,7 @@ class FluidbookLinks $s->setTitle('Rulers'); $rcols = array('page', 'type', 'pos'); - $i = 0; + $i = 1; // Labels foreach ($rcols as $id) { $s->setCellValueByColumnAndRow($i, 1, $id); @@ -114,7 +114,7 @@ class FluidbookLinks // Contents $j = 2; foreach ($rulers as $r) { - $i = 0; + $i = 1; foreach ($rcols as $id) { if (!is_null($pages) && ($id == 'document_id' || $id == 'document_page')) { $infos = $pages[$r['page']]; @@ -328,7 +328,7 @@ class FluidbookLinks continue; } $link->$attr = trim($link->$attr); - if (strpos($link->$attr, '///') === 0 || $link->$attr == '') { + if (str_starts_with($link->$attr, '///') || $link->$attr == '') { continue; } $link->$attr = '///' . Crypt::safeEncrypt($link->uid . '|||' . $link->$attr, self::_getLinkKey()); @@ -348,7 +348,7 @@ class FluidbookLinks } foreach ($link as $attr => $item) { - if (strpos($item, '///') !== 0) { + if (!str_starts_with($item, '///')) { continue; } $v = Crypt::safeDecrypt(substr($item, 3), self::_getLinkKey()); @@ -417,7 +417,11 @@ class FluidbookLinks $res = []; foreach ($updates as $timestamp => $u) { - $u['name'] = User::find($u['user'])->name; + try { + $u['name'] = User::find($u['user'])->name; + } catch (\Exception $e) { + $u['name'] = '-'; + } $u['date'] = date('Y-m-d H:i:s', $timestamp); $u['timestamp'] = $timestamp; $res[] = $u; diff --git a/resources/linkeditor/js/linkeditor.js b/resources/linkeditor/js/linkeditor.js index 74b1d9d57..d28d242c6 100644 --- a/resources/linkeditor/js/linkeditor.js +++ b/resources/linkeditor/js/linkeditor.js @@ -95,6 +95,13 @@ LinkEditor.prototype = { }); }, + initTooltips: function () { + $('[data-tooltip]:not(.init-tooltip)').each(function () { + tippy($(this).get(0), {content: $(this).data('tooltip')}); + }); + $(this).addClass('init-tooltip'); + }, + initEvents: function () { var $this = this; $(window).on('hashchange', function () { @@ -181,7 +188,10 @@ LinkEditor.prototype = { this.changePage(); }, - runAction: function (act) { + runAction: function (act, args) { + if (arguments === undefined) { + args = []; + } var a = act.split('.'); var o = this; let po = this; @@ -189,7 +199,12 @@ LinkEditor.prototype = { po = o; o = o[a[i]]; } - return o.call(po); + try { + return o.apply(po, args); + } catch (e) { + console.log(e); + console.error('Error while calling ' + act, args); + } }, firstPage: function () { diff --git a/resources/linkeditor/js/linkeditor.save.js b/resources/linkeditor/js/linkeditor.save.js index 072301e20..0f0353b99 100644 --- a/resources/linkeditor/js/linkeditor.save.js +++ b/resources/linkeditor/js/linkeditor.save.js @@ -30,25 +30,38 @@ LinkeditorSave.prototype = { } }, - save: function (message) { + save: function (message, notify, callback) { + if (notify === undefined) { + notify = true; + } + if (callback === undefined) { + callback = function () { + + }; + } var $this = this; if (message === undefined) { message = TRANSLATIONS.manual_save_message; } - var notificationTimeout=5000; + var notificationTimeout = 5000; $.ajax({ url: '/fluidbook-publication/' + FLUIDBOOK_DATA.id + '/save/links', method: 'post', data: { _method: 'put', 'message': message, rulers: window.RULERS, links: window.LINKS, }, success: function (data) { - new Noty({ - type: "success", - text: TRANSLATIONS.success_save, - timeout: notificationTimeout, - }).show(); + if (notify) { + new Noty({ + type: "success", + text: TRANSLATIONS.success_save, + timeout: notificationTimeout, + }).show(); + } clearTimeout($this.automaticSaveTimeout); $this.unsavedChanges = false; $this.runningAutomaticSaveTimeout = false; + + $this.linkeditor.versions.refresh(); + callback(); }, error: function (jqXHR, status, error) { $this.linkeditor.hasChanged(); diff --git a/resources/linkeditor/js/linkeditor.toolbar.js b/resources/linkeditor/js/linkeditor.toolbar.js index 3c7dac2de..b85291a51 100644 --- a/resources/linkeditor/js/linkeditor.toolbar.js +++ b/resources/linkeditor/js/linkeditor.toolbar.js @@ -5,7 +5,7 @@ function LinkeditorToolbar(linkeditor) { LinkeditorToolbar.prototype = { init: function () { - var $this=this; + var $this = this; $("#linkeditor-page-field input").on('change', function () { $this.linkeditor.changePage($(this).val()); $(this).blur(); @@ -24,14 +24,12 @@ LinkeditorToolbar.prototype = { }); }); - $('[data-action]').click(function () { - $this.linkeditor.runAction($(this).data('action')); + $(document).on('click', '[data-action]', function () { + $this.linkeditor.runAction($(this).data('action'), $(this).is('[data-action-args]') ? $(this).data('action-args') : []); return false; }); - $('[data-tooltip]').each(function () { - tippy($(this).get(0), {content: $(this).data('tooltip')}); - }); + this.linkeditor.initTooltips(); }, }; module.exports = LinkeditorToolbar; diff --git a/resources/linkeditor/js/linkeditor.versions.js b/resources/linkeditor/js/linkeditor.versions.js index 4cf8d1af3..9804e060d 100644 --- a/resources/linkeditor/js/linkeditor.versions.js +++ b/resources/linkeditor/js/linkeditor.versions.js @@ -21,7 +21,8 @@ LinkeditorVersions.prototype = { setVersions: function (data) { var list = $("#linkeditor-panel-versions-list"); list.html(''); - $.each(data, function (timestamp, version) { + $.each(data, function (k, version) { + let actionArgs = JSON.stringify([version.timestamp]); var item = '
'; item += '
'; item += '
' + version.date + '
'; @@ -33,25 +34,57 @@ LinkeditorVersions.prototype = { item += '
' + version.rulers + ' rulers
'; item += '
' item += '
'; - item += '
'; + item += '
' + + '
'; item += '
' item += '
' list.append(item); }); this.linkeditor.initIcons(); + this.linkeditor.initTooltips(); this.resize(); }, + restoreVersion: function (timestamp) { + var $this = this; + var callback = function () { + $this._restoreVersion(timestamp); + } + + //Restore links from 2022-12-07 13:37:15 + if (this.linkeditor.save.unsavedChanges) { + console.log('save save save'); + this.linkeditor.save.save('Save before restoring links', false, function () { + setTimeout(function () { + callback(); + }, 1000); + }); + } else { + callback(); + } + }, + + _restoreVersion(timestamp) { + $.ajax({ + url: '/fluidbook-publication/' + FLUIDBOOK_DATA.id + '/edit/links/versions/restore/' + timestamp + '', + success: function (data) { + window.location.reload(); + }, + }); + }, + resize: function () { - var w=$("#linkeditor-panel-versions-list").outerWidth(); - if(w<=0){ + var w = $("#linkeditor-panel-versions-list").outerWidth(); + if (w <= 0) { return; } - if(w<300){ + if (w < 300) { $("#linkeditor-panel-versions-list").addClass('small'); - }else{ + } else { $("#linkeditor-panel-versions-list").removeClass('small'); } }, + + }; module.exports = LinkeditorVersions; diff --git a/resources/linkeditor/style/inc/_versions.sass b/resources/linkeditor/style/inc/_versions.sass index 03ec50f10..8ca744134 100644 --- a/resources/linkeditor/style/inc/_versions.sass +++ b/resources/linkeditor/style/inc/_versions.sass @@ -1,5 +1,6 @@ #linkeditor-panel-versions padding: 5px 10px + user-select: none #linkeditor-panel-versions-list font-size: 12px @@ -42,6 +43,7 @@ height: $icon-size margin-left: 7px color: currentColor + cursor: pointer svg width: $icon-size diff --git a/resources/views/fluidbook_publication/link_editor.blade.php b/resources/views/fluidbook_publication/link_editor.blade.php index 16a00ce27..7fc382f33 100644 --- a/resources/views/fluidbook_publication/link_editor.blade.php +++ b/resources/views/fluidbook_publication/link_editor.blade.php @@ -75,8 +75,9 @@ data-tooltip="{{__('Importer les liens (Remplacer)')}}"> - +