]> BookStack Code Mirror - bookstack/commitdiff
Moved page editing to angular controller and started work on update drafts
authorDan Brown <redacted>
Wed, 9 Mar 2016 22:32:07 +0000 (22:32 +0000)
committerDan Brown <redacted>
Wed, 9 Mar 2016 22:32:07 +0000 (22:32 +0000)
14 files changed:
app/Http/Controllers/PageController.php
app/Http/routes.php
app/Page.php
app/PageRevision.php
app/Repos/PageRepo.php
database/migrations/2016_03_09_203143_add_page_revision_types.php [new file with mode: 0644]
resources/assets/js/controllers.js
resources/assets/js/directives.js
resources/assets/js/global.js
resources/assets/js/pages/page-form.js
resources/views/pages/create.blade.php
resources/views/pages/edit.blade.php
resources/views/pages/form.blade.php
resources/views/pages/revisions.blade.php

index 19e4744ea5643e5412a1b24689b4ad03de545314..c6228a8bcc31b234744b78c1fa80350427e6cf17 100644 (file)
@@ -142,6 +142,23 @@ class PageController extends Controller
         return redirect($page->getUrl());
     }
 
+    /**
+     * Save a draft update as a revision.
+     * @param Request $request
+     * @param $pageId
+     * @return \Illuminate\Http\JsonResponse
+     */
+    public function saveUpdateDraft(Request $request, $pageId)
+    {
+        $this->validate($request, [
+            'name' => 'required|string|max:255'
+        ]);
+        $page = $this->pageRepo->getById($pageId);
+        $this->checkOwnablePermission('page-update', $page);
+        $this->pageRepo->saveUpdateDraft($page, $request->only(['name', 'html']));
+        return response()->json(['status' => 'success', 'message' => 'Draft successfully saved']);
+    }
+
     /**
      * Redirect from a special link url which
      * uses the page id rather than the name.
index 81bbb16bc0f2bfad7f4c8a402d7773b2b2d88525..e16d4f8f9454c554210c4fa88357ee838fa1bcdd 100644 (file)
@@ -75,6 +75,9 @@ Route::group(['middleware' => 'auth'], function () {
         Route::delete('/{imageId}', 'ImageController@destroy');
     });
 
+    // Ajax routes
+    Route::put('/ajax/page/{id}/save-draft', 'PageController@saveUpdateDraft');
+
     // Links
     Route::get('/link/{id}', 'PageController@redirectFromLink');
 
index 53724ec20d5921fcea668f3abaa54ef3440daa5e..34dee2f2fd2cd65cfacf893057998694d1959e9e 100644 (file)
@@ -34,7 +34,7 @@ class Page extends Entity
 
     public function revisions()
     {
-        return $this->hasMany('BookStack\PageRevision')->orderBy('created_at', 'desc');
+        return $this->hasMany('BookStack\PageRevision')->where('type', '=', 'version')->orderBy('created_at', 'desc');
     }
 
     public function getUrl()
index 52c37e390f9fb1e87f63846a6a94fc5f9dd1d5d0..f1b4bc5870575492e43f4268e805e86c05deefb6 100644 (file)
@@ -1,6 +1,4 @@
-<?php
-
-namespace BookStack;
+<?php namespace BookStack;
 
 use Illuminate\Database\Eloquent\Model;
 
@@ -8,16 +6,28 @@ class PageRevision extends Model
 {
     protected $fillable = ['name', 'html', 'text'];
 
+    /**
+     * Get the user that created the page revision
+     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
+     */
     public function createdBy()
     {
         return $this->belongsTo('BookStack\User', 'created_by');
     }
 
+    /**
+     * Get the page this revision originates from.
+     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
+     */
     public function page()
     {
         return $this->belongsTo('BookStack\Page');
     }
 
+    /**
+     * Get the url for this revision.
+     * @return string
+     */
     public function getUrl()
     {
         return $this->page->getUrl() . '/revisions/' . $this->id;
index 4784ad407605e2be2444378df55ef44d66be3953..ca97fc1e98a6a3bf67fda268e0c61c5ba8072927 100644 (file)
@@ -4,6 +4,7 @@
 use Activity;
 use BookStack\Book;
 use BookStack\Exceptions\NotFoundException;
+use DOMDocument;
 use Illuminate\Support\Str;
 use BookStack\Page;
 use BookStack\PageRevision;
@@ -66,9 +67,10 @@ class PageRepo extends EntityRepo
     public function findPageUsingOldSlug($pageSlug, $bookSlug)
     {
         $revision = $this->pageRevision->where('slug', '=', $pageSlug)
-            ->whereHas('page', function($query) {
+            ->whereHas('page', function ($query) {
                 $this->restrictionService->enforcePageRestrictions($query);
             })
+            ->where('type', '=', 'version')
             ->where('book_slug', '=', $bookSlug)->orderBy('created_at', 'desc')
             ->with('page')->first();
         return $revision !== null ? $revision->page : null;
@@ -100,8 +102,8 @@ class PageRepo extends EntityRepo
      * Save a new page into the system.
      * Input validation must be done beforehand.
      * @param array $input
-     * @param Book  $book
-     * @param int   $chapterId
+     * @param Book $book
+     * @param int $chapterId
      * @return Page
      */
     public function saveNew(array $input, Book $book, $chapterId = null)
@@ -128,9 +130,9 @@ class PageRepo extends EntityRepo
      */
     protected function formatHtml($htmlText)
     {
-        if($htmlText == '') return $htmlText;
+        if ($htmlText == '') return $htmlText;
         libxml_use_internal_errors(true);
-        $doc = new \DOMDocument();
+        $doc = new DOMDocument();
         $doc->loadHTML(mb_convert_encoding($htmlText, 'HTML-ENTITIES', 'UTF-8'));
 
         $container = $doc->documentElement;
@@ -239,8 +241,8 @@ class PageRepo extends EntityRepo
 
     /**
      * Updates a page with any fillable data and saves it into the database.
-     * @param Page   $page
-     * @param int    $book_id
+     * @param Page $page
+     * @param int $book_id
      * @param string $input
      * @return Page
      */
@@ -297,6 +299,7 @@ class PageRepo extends EntityRepo
         $revision->book_slug = $page->book->slug;
         $revision->created_by = auth()->user()->id;
         $revision->created_at = $page->updated_at;
+        $revision->type = 'version';
         $revision->save();
         // Clear old revisions
         if ($this->pageRevision->where('page_id', '=', $page->id)->count() > 50) {
@@ -306,6 +309,36 @@ class PageRepo extends EntityRepo
         return $revision;
     }
 
+    /**
+     * Save a page update draft.
+     * @param Page $page
+     * @param array $data
+     * @return PageRevision
+     */
+    public function saveUpdateDraft(Page $page, $data = [])
+    {
+        $userId = auth()->user()->id;
+        $drafts = $this->pageRevision->where('created_by', '=', $userId)
+            ->where('type', 'update_draft')
+            ->where('page_id', '=', $page->id)
+            ->orderBy('created_at', 'desc')->get();
+
+        if ($drafts->count() > 0) {
+            $draft = $drafts->first();
+        } else {
+            $draft = $this->pageRevision->newInstance();
+            $draft->page_id = $page->id;
+            $draft->slug = $page->slug;
+            $draft->book_slug = $page->book->slug;
+            $draft->created_by = $userId;
+            $draft->type = 'update_draft';
+        }
+
+        $draft->fill($data);
+        $draft->save();
+        return $draft;
+    }
+
     /**
      * Gets a single revision via it's id.
      * @param $id
@@ -333,7 +366,7 @@ class PageRepo extends EntityRepo
     /**
      * Changes the related book for the specified page.
      * Changes the book id of any relations to the page that store the book id.
-     * @param int  $bookId
+     * @param int $bookId
      * @param Page $page
      * @return Page
      */
diff --git a/database/migrations/2016_03_09_203143_add_page_revision_types.php b/database/migrations/2016_03_09_203143_add_page_revision_types.php
new file mode 100644 (file)
index 0000000..e39c77d
--- /dev/null
@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class AddPageRevisionTypes extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('page_revisions', function (Blueprint $table) {
+            $table->string('type')->default('version');
+            $table->index('type');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('page_revisions', function (Blueprint $table) {
+            $table->dropColumn('type');
+        });
+    }
+}
index 1f73888590d2fc18ffb92d3566ae5aec28af01c7..305e0c3c1ab81fb215eeb4c81f33cb9dcd1961b1 100644 (file)
@@ -213,4 +213,49 @@ module.exports = function (ngApp, events) {
     }]);
 
 
+    ngApp.controller('PageEditController',  ['$scope', '$http', '$attrs', '$interval', function ($scope, $http, $attrs, $interval) {
+
+        $scope.editorOptions = require('./pages/page-form');
+        $scope.editorHtml = '';
+        $scope.draftText = '';
+        var pageId = Number($attrs.pageId);
+        var isEdit = pageId !== 0;
+
+        if (isEdit) {
+            startAutoSave();
+        }
+
+        $scope.editorChange = function() {
+            $scope.draftText = '';
+        }
+
+        function startAutoSave() {
+            var currentTitle = $('#name').val();
+            var currentHtml = $scope.editorHtml;
+
+            console.log('Starting auto save');
+
+            $interval(() => {
+                var newTitle = $('#name').val();
+                var newHtml = $scope.editorHtml;
+
+                if (newTitle !== currentTitle || newHtml !== currentHtml) {
+                    currentHtml = newHtml;
+                    currentTitle = newTitle;
+                    saveDraftUpdate(newTitle, newHtml);
+                }
+            }, 1000*5);
+        }
+
+        function saveDraftUpdate(title, html) {
+            $http.put('/ajax/page/' + pageId + '/save-draft', {
+                name: title,
+                html: html
+            }).then((responseData) => {
+                $scope.draftText = 'Draft saved'
+            })
+        }
+
+    }]);
+
 };
\ No newline at end of file
index 60abde6e928be202ffd808f62d785b97e2cc4c88..b6c41bb3cfadb332f4edf041b76c82f16182f736 100644 (file)
@@ -162,5 +162,31 @@ module.exports = function (ngApp, events) {
         };
     }]);
 
+    ngApp.directive('tinymce', [function() {
+        return {
+            restrict: 'A',
+            scope: {
+                tinymce: '=',
+                ngModel: '=',
+                ngChange: '='
+            },
+            link: function (scope, element, attrs) {
+
+                function tinyMceSetup(editor) {
+                    editor.on('keyup', (e) => {
+                        var content = editor.getContent();
+                        scope.$apply(() => {
+                            scope.ngModel = content;
+                        });
+                        scope.ngChange(content);
+                    });
+                }
+
+                scope.tinymce.extraSetups.push(tinyMceSetup);
+                tinymce.init(scope.tinymce);
+            }
+        }
+    }])
+
 
 };
\ No newline at end of file
index 5400a8af01d7037f3c46158178b75f69f196926f..aa5e60ce450fa2a91864d0ca17dabc9a72895dfd 100644 (file)
@@ -119,11 +119,5 @@ function elemExists(selector) {
     return document.querySelector(selector) !== null;
 }
 
-// TinyMCE editor
-if (elemExists('#html-editor')) {
-    var tinyMceOptions = require('./pages/page-form');
-    tinymce.init(tinyMceOptions);
-}
-
 // Page specific items
-require('./pages/page-show');
\ No newline at end of file
+require('./pages/page-show');
index 290b7c6534da0f853c9b5dda424c02a43ae7f427..0310b5fa2cdca566ac08db22bfd8e6b22ce2e432 100644 (file)
@@ -1,4 +1,4 @@
-module.exports = {
+var mceOptions = module.exports = {
     selector: '#html-editor',
     content_css: [
         '/css/styles.css'
@@ -51,8 +51,15 @@ module.exports = {
             args.content = '';
         }
     },
+    extraSetups: [],
     setup: function (editor) {
 
+        console.log(mceOptions.extraSetups);
+
+        for (var i = 0; i < mceOptions.extraSetups.length; i++) {
+            mceOptions.extraSetups[i](editor);
+        }
+
         (function () {
             var wrap;
 
index 69c5f7c94c39835053696cfc8e1a736853abb4ab..441379eaec9b670029e290d58b6c8e73ea45ebdc 100644 (file)
@@ -8,7 +8,7 @@
 
 @section('content')
 
-    <div class="flex-fill flex" ng-non-bindable>
+    <div class="flex-fill flex">
         <form action="{{$book->getUrl() . '/page'}}" method="POST" class="flex flex-fill">
             @include('pages/form')
             @if($chapter)
index 6dde47c637360683af43c6c0c64dde9cde65f99c..0832f63b451fb2f07345f44fd176839c9bad882e 100644 (file)
@@ -8,8 +8,8 @@
 
 @section('content')
 
-    <div class="flex-fill flex" ng-non-bindable>
-        <form action="{{$page->getUrl()}}" method="POST" class="flex flex-fill">
+    <div class="flex-fill flex">
+        <form action="{{$page->getUrl()}}" data-page-id="{{ $page->id }}" method="POST" class="flex flex-fill">
             <input type="hidden" name="_method" value="PUT">
             @include('pages/form', ['model' => $page])
         </form>
index f1f54d97ffe8d029b227e686faf60d3566ac5bde..e32ef537a110e206c0ba96c397f56512f61d6844 100644 (file)
@@ -1,7 +1,7 @@
 
 
 
-<div class="page-editor flex-fill flex" ng-non-bindable>
+<div class="page-editor flex-fill flex" ng-controller="PageEditController" page-id="{{ $model->id or 0 }}">
 
     {{ csrf_field() }}
     <div class="faded-small toolbar">
                         <a onclick="$('body>header').slideToggle();" class="text-button text-primary"><i class="zmdi zmdi-swap-vertical"></i>Toggle Header</a>
                     </div>
                 </div>
-                <div class="col-sm-8 faded">
+                <div class="col-sm-4 faded text-center">
+                    <span ng-bind="draftText"></span>
+                </div>
+                <div class="col-sm-4 faded">
                     <div class="action-buttons">
                         <a href="{{ back()->getTargetUrl() }}" class="text-button text-primary"><i class="zmdi zmdi-close"></i>Cancel</a>
                         <button type="submit" id="save-button" class="text-button  text-pos"><i class="zmdi zmdi-floppy"></i>Save Page</button>
         </div>
     </div>
 
-    <div class="title-input page-title clearfix">
+    <div class="title-input page-title clearfix" ng-non-bindable>
         <div class="input">
             @include('form/text', ['name' => 'name', 'placeholder' => 'Page Title'])
         </div>
     </div>
     <div class="edit-area flex-fill flex">
-        <textarea id="html-editor" name="html" rows="5"
+        <textarea id="html-editor" tinymce="editorOptions" ng-change="editorChange" ng-model="editorHtml" name="html" rows="5"
                   @if($errors->has('html')) class="neg" @endif>@if(isset($model) || old('html')){{htmlspecialchars( old('html') ? old('html') : $model->html)}}@endif</textarea>
         @if($errors->has('html'))
             <div class="text-neg text-small">{{ $errors->first('html') }}</div>
index e3782ef6edc0e0188cf3ab91162ef96b401b8da7..a73f16a4ff8bbd882f453e1d1d38b0d0bc442028 100644 (file)
 
             <table class="table">
                 <tr>
-                    <th>Name</th>
-                    <th colspan="2">Created By</th>
-                    <th>Revision Date</th>
-                    <th>Actions</th>
+                    <th width="40%">Name</th>
+                    <th colspan="2" width="20%">Created By</th>
+                    <th width="20%">Revision Date</th>
+                    <th width="20%">Actions</th>
                 </tr>
                 @foreach($page->revisions as $revision)
                     <tr>
@@ -38,7 +38,7 @@
                             @endif
                         </td>
                         <td> @if($revision->createdBy) {{$revision->createdBy->name}} @else Deleted User @endif</td>
-                        <td><small>{{$revision->created_at->format('jS F, Y H:i:s')}} ({{$revision->created_at->diffForHumans()}})</small></td>
+                        <td><small>{{$revision->created_at->format('jS F, Y H:i:s')}} <br> ({{$revision->created_at->diffForHumans()}})</small></td>
                         <td>
                             <a href="{{$revision->getUrl()}}" target="_blank">Preview</a>
                             <span class="text-muted">&nbsp;|&nbsp;</span>