Dan Brown

Moved page editing to angular controller and started work on update drafts

...@@ -143,6 +143,23 @@ class PageController extends Controller ...@@ -143,6 +143,23 @@ class PageController extends Controller
143 } 143 }
144 144
145 /** 145 /**
146 + * Save a draft update as a revision.
147 + * @param Request $request
148 + * @param $pageId
149 + * @return \Illuminate\Http\JsonResponse
150 + */
151 + public function saveUpdateDraft(Request $request, $pageId)
152 + {
153 + $this->validate($request, [
154 + 'name' => 'required|string|max:255'
155 + ]);
156 + $page = $this->pageRepo->getById($pageId);
157 + $this->checkOwnablePermission('page-update', $page);
158 + $this->pageRepo->saveUpdateDraft($page, $request->only(['name', 'html']));
159 + return response()->json(['status' => 'success', 'message' => 'Draft successfully saved']);
160 + }
161 +
162 + /**
146 * Redirect from a special link url which 163 * Redirect from a special link url which
147 * uses the page id rather than the name. 164 * uses the page id rather than the name.
148 * @param $pageId 165 * @param $pageId
......
...@@ -75,6 +75,9 @@ Route::group(['middleware' => 'auth'], function () { ...@@ -75,6 +75,9 @@ Route::group(['middleware' => 'auth'], function () {
75 Route::delete('/{imageId}', 'ImageController@destroy'); 75 Route::delete('/{imageId}', 'ImageController@destroy');
76 }); 76 });
77 77
78 + // Ajax routes
79 + Route::put('/ajax/page/{id}/save-draft', 'PageController@saveUpdateDraft');
80 +
78 // Links 81 // Links
79 Route::get('/link/{id}', 'PageController@redirectFromLink'); 82 Route::get('/link/{id}', 'PageController@redirectFromLink');
80 83
......
...@@ -34,7 +34,7 @@ class Page extends Entity ...@@ -34,7 +34,7 @@ class Page extends Entity
34 34
35 public function revisions() 35 public function revisions()
36 { 36 {
37 - return $this->hasMany('BookStack\PageRevision')->orderBy('created_at', 'desc'); 37 + return $this->hasMany('BookStack\PageRevision')->where('type', '=', 'version')->orderBy('created_at', 'desc');
38 } 38 }
39 39
40 public function getUrl() 40 public function getUrl()
......
1 -<?php 1 +<?php namespace BookStack;
2 -
3 -namespace BookStack;
4 2
5 use Illuminate\Database\Eloquent\Model; 3 use Illuminate\Database\Eloquent\Model;
6 4
...@@ -8,16 +6,28 @@ class PageRevision extends Model ...@@ -8,16 +6,28 @@ class PageRevision extends Model
8 { 6 {
9 protected $fillable = ['name', 'html', 'text']; 7 protected $fillable = ['name', 'html', 'text'];
10 8
9 + /**
10 + * Get the user that created the page revision
11 + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
12 + */
11 public function createdBy() 13 public function createdBy()
12 { 14 {
13 return $this->belongsTo('BookStack\User', 'created_by'); 15 return $this->belongsTo('BookStack\User', 'created_by');
14 } 16 }
15 17
18 + /**
19 + * Get the page this revision originates from.
20 + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
21 + */
16 public function page() 22 public function page()
17 { 23 {
18 return $this->belongsTo('BookStack\Page'); 24 return $this->belongsTo('BookStack\Page');
19 } 25 }
20 26
27 + /**
28 + * Get the url for this revision.
29 + * @return string
30 + */
21 public function getUrl() 31 public function getUrl()
22 { 32 {
23 return $this->page->getUrl() . '/revisions/' . $this->id; 33 return $this->page->getUrl() . '/revisions/' . $this->id;
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
4 use Activity; 4 use Activity;
5 use BookStack\Book; 5 use BookStack\Book;
6 use BookStack\Exceptions\NotFoundException; 6 use BookStack\Exceptions\NotFoundException;
7 +use DOMDocument;
7 use Illuminate\Support\Str; 8 use Illuminate\Support\Str;
8 use BookStack\Page; 9 use BookStack\Page;
9 use BookStack\PageRevision; 10 use BookStack\PageRevision;
...@@ -66,9 +67,10 @@ class PageRepo extends EntityRepo ...@@ -66,9 +67,10 @@ class PageRepo extends EntityRepo
66 public function findPageUsingOldSlug($pageSlug, $bookSlug) 67 public function findPageUsingOldSlug($pageSlug, $bookSlug)
67 { 68 {
68 $revision = $this->pageRevision->where('slug', '=', $pageSlug) 69 $revision = $this->pageRevision->where('slug', '=', $pageSlug)
69 - ->whereHas('page', function($query) { 70 + ->whereHas('page', function ($query) {
70 $this->restrictionService->enforcePageRestrictions($query); 71 $this->restrictionService->enforcePageRestrictions($query);
71 }) 72 })
73 + ->where('type', '=', 'version')
72 ->where('book_slug', '=', $bookSlug)->orderBy('created_at', 'desc') 74 ->where('book_slug', '=', $bookSlug)->orderBy('created_at', 'desc')
73 ->with('page')->first(); 75 ->with('page')->first();
74 return $revision !== null ? $revision->page : null; 76 return $revision !== null ? $revision->page : null;
...@@ -100,8 +102,8 @@ class PageRepo extends EntityRepo ...@@ -100,8 +102,8 @@ class PageRepo extends EntityRepo
100 * Save a new page into the system. 102 * Save a new page into the system.
101 * Input validation must be done beforehand. 103 * Input validation must be done beforehand.
102 * @param array $input 104 * @param array $input
103 - * @param Book $book 105 + * @param Book $book
104 - * @param int $chapterId 106 + * @param int $chapterId
105 * @return Page 107 * @return Page
106 */ 108 */
107 public function saveNew(array $input, Book $book, $chapterId = null) 109 public function saveNew(array $input, Book $book, $chapterId = null)
...@@ -128,9 +130,9 @@ class PageRepo extends EntityRepo ...@@ -128,9 +130,9 @@ class PageRepo extends EntityRepo
128 */ 130 */
129 protected function formatHtml($htmlText) 131 protected function formatHtml($htmlText)
130 { 132 {
131 - if($htmlText == '') return $htmlText; 133 + if ($htmlText == '') return $htmlText;
132 libxml_use_internal_errors(true); 134 libxml_use_internal_errors(true);
133 - $doc = new \DOMDocument(); 135 + $doc = new DOMDocument();
134 $doc->loadHTML(mb_convert_encoding($htmlText, 'HTML-ENTITIES', 'UTF-8')); 136 $doc->loadHTML(mb_convert_encoding($htmlText, 'HTML-ENTITIES', 'UTF-8'));
135 137
136 $container = $doc->documentElement; 138 $container = $doc->documentElement;
...@@ -239,8 +241,8 @@ class PageRepo extends EntityRepo ...@@ -239,8 +241,8 @@ class PageRepo extends EntityRepo
239 241
240 /** 242 /**
241 * Updates a page with any fillable data and saves it into the database. 243 * Updates a page with any fillable data and saves it into the database.
242 - * @param Page $page 244 + * @param Page $page
243 - * @param int $book_id 245 + * @param int $book_id
244 * @param string $input 246 * @param string $input
245 * @return Page 247 * @return Page
246 */ 248 */
...@@ -297,6 +299,7 @@ class PageRepo extends EntityRepo ...@@ -297,6 +299,7 @@ class PageRepo extends EntityRepo
297 $revision->book_slug = $page->book->slug; 299 $revision->book_slug = $page->book->slug;
298 $revision->created_by = auth()->user()->id; 300 $revision->created_by = auth()->user()->id;
299 $revision->created_at = $page->updated_at; 301 $revision->created_at = $page->updated_at;
302 + $revision->type = 'version';
300 $revision->save(); 303 $revision->save();
301 // Clear old revisions 304 // Clear old revisions
302 if ($this->pageRevision->where('page_id', '=', $page->id)->count() > 50) { 305 if ($this->pageRevision->where('page_id', '=', $page->id)->count() > 50) {
...@@ -307,6 +310,36 @@ class PageRepo extends EntityRepo ...@@ -307,6 +310,36 @@ class PageRepo extends EntityRepo
307 } 310 }
308 311
309 /** 312 /**
313 + * Save a page update draft.
314 + * @param Page $page
315 + * @param array $data
316 + * @return PageRevision
317 + */
318 + public function saveUpdateDraft(Page $page, $data = [])
319 + {
320 + $userId = auth()->user()->id;
321 + $drafts = $this->pageRevision->where('created_by', '=', $userId)
322 + ->where('type', 'update_draft')
323 + ->where('page_id', '=', $page->id)
324 + ->orderBy('created_at', 'desc')->get();
325 +
326 + if ($drafts->count() > 0) {
327 + $draft = $drafts->first();
328 + } else {
329 + $draft = $this->pageRevision->newInstance();
330 + $draft->page_id = $page->id;
331 + $draft->slug = $page->slug;
332 + $draft->book_slug = $page->book->slug;
333 + $draft->created_by = $userId;
334 + $draft->type = 'update_draft';
335 + }
336 +
337 + $draft->fill($data);
338 + $draft->save();
339 + return $draft;
340 + }
341 +
342 + /**
310 * Gets a single revision via it's id. 343 * Gets a single revision via it's id.
311 * @param $id 344 * @param $id
312 * @return mixed 345 * @return mixed
...@@ -333,7 +366,7 @@ class PageRepo extends EntityRepo ...@@ -333,7 +366,7 @@ class PageRepo extends EntityRepo
333 /** 366 /**
334 * Changes the related book for the specified page. 367 * Changes the related book for the specified page.
335 * Changes the book id of any relations to the page that store the book id. 368 * Changes the book id of any relations to the page that store the book id.
336 - * @param int $bookId 369 + * @param int $bookId
337 * @param Page $page 370 * @param Page $page
338 * @return Page 371 * @return Page
339 */ 372 */
......
1 +<?php
2 +
3 +use Illuminate\Database\Schema\Blueprint;
4 +use Illuminate\Database\Migrations\Migration;
5 +
6 +class AddPageRevisionTypes extends Migration
7 +{
8 + /**
9 + * Run the migrations.
10 + *
11 + * @return void
12 + */
13 + public function up()
14 + {
15 + Schema::table('page_revisions', function (Blueprint $table) {
16 + $table->string('type')->default('version');
17 + $table->index('type');
18 + });
19 + }
20 +
21 + /**
22 + * Reverse the migrations.
23 + *
24 + * @return void
25 + */
26 + public function down()
27 + {
28 + Schema::table('page_revisions', function (Blueprint $table) {
29 + $table->dropColumn('type');
30 + });
31 + }
32 +}
...@@ -213,4 +213,49 @@ module.exports = function (ngApp, events) { ...@@ -213,4 +213,49 @@ module.exports = function (ngApp, events) {
213 }]); 213 }]);
214 214
215 215
216 + ngApp.controller('PageEditController', ['$scope', '$http', '$attrs', '$interval', function ($scope, $http, $attrs, $interval) {
217 +
218 + $scope.editorOptions = require('./pages/page-form');
219 + $scope.editorHtml = '';
220 + $scope.draftText = '';
221 + var pageId = Number($attrs.pageId);
222 + var isEdit = pageId !== 0;
223 +
224 + if (isEdit) {
225 + startAutoSave();
226 + }
227 +
228 + $scope.editorChange = function() {
229 + $scope.draftText = '';
230 + }
231 +
232 + function startAutoSave() {
233 + var currentTitle = $('#name').val();
234 + var currentHtml = $scope.editorHtml;
235 +
236 + console.log('Starting auto save');
237 +
238 + $interval(() => {
239 + var newTitle = $('#name').val();
240 + var newHtml = $scope.editorHtml;
241 +
242 + if (newTitle !== currentTitle || newHtml !== currentHtml) {
243 + currentHtml = newHtml;
244 + currentTitle = newTitle;
245 + saveDraftUpdate(newTitle, newHtml);
246 + }
247 + }, 1000*5);
248 + }
249 +
250 + function saveDraftUpdate(title, html) {
251 + $http.put('/ajax/page/' + pageId + '/save-draft', {
252 + name: title,
253 + html: html
254 + }).then((responseData) => {
255 + $scope.draftText = 'Draft saved'
256 + })
257 + }
258 +
259 + }]);
260 +
216 }; 261 };
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -162,5 +162,31 @@ module.exports = function (ngApp, events) { ...@@ -162,5 +162,31 @@ module.exports = function (ngApp, events) {
162 }; 162 };
163 }]); 163 }]);
164 164
165 + ngApp.directive('tinymce', [function() {
166 + return {
167 + restrict: 'A',
168 + scope: {
169 + tinymce: '=',
170 + ngModel: '=',
171 + ngChange: '='
172 + },
173 + link: function (scope, element, attrs) {
174 +
175 + function tinyMceSetup(editor) {
176 + editor.on('keyup', (e) => {
177 + var content = editor.getContent();
178 + scope.$apply(() => {
179 + scope.ngModel = content;
180 + });
181 + scope.ngChange(content);
182 + });
183 + }
184 +
185 + scope.tinymce.extraSetups.push(tinyMceSetup);
186 + tinymce.init(scope.tinymce);
187 + }
188 + }
189 + }])
190 +
165 191
166 }; 192 };
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -119,11 +119,5 @@ function elemExists(selector) { ...@@ -119,11 +119,5 @@ function elemExists(selector) {
119 return document.querySelector(selector) !== null; 119 return document.querySelector(selector) !== null;
120 } 120 }
121 121
122 -// TinyMCE editor
123 -if (elemExists('#html-editor')) {
124 - var tinyMceOptions = require('./pages/page-form');
125 - tinymce.init(tinyMceOptions);
126 -}
127 -
128 // Page specific items 122 // Page specific items
129 -require('./pages/page-show');
...\ No newline at end of file ...\ No newline at end of file
123 +require('./pages/page-show');
......
1 -module.exports = { 1 +var mceOptions = module.exports = {
2 selector: '#html-editor', 2 selector: '#html-editor',
3 content_css: [ 3 content_css: [
4 '/css/styles.css' 4 '/css/styles.css'
...@@ -51,8 +51,15 @@ module.exports = { ...@@ -51,8 +51,15 @@ module.exports = {
51 args.content = ''; 51 args.content = '';
52 } 52 }
53 }, 53 },
54 + extraSetups: [],
54 setup: function (editor) { 55 setup: function (editor) {
55 56
57 + console.log(mceOptions.extraSetups);
58 +
59 + for (var i = 0; i < mceOptions.extraSetups.length; i++) {
60 + mceOptions.extraSetups[i](editor);
61 + }
62 +
56 (function () { 63 (function () {
57 var wrap; 64 var wrap;
58 65
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
8 8
9 @section('content') 9 @section('content')
10 10
11 - <div class="flex-fill flex" ng-non-bindable> 11 + <div class="flex-fill flex">
12 <form action="{{$book->getUrl() . '/page'}}" method="POST" class="flex flex-fill"> 12 <form action="{{$book->getUrl() . '/page'}}" method="POST" class="flex flex-fill">
13 @include('pages/form') 13 @include('pages/form')
14 @if($chapter) 14 @if($chapter)
......
...@@ -8,8 +8,8 @@ ...@@ -8,8 +8,8 @@
8 8
9 @section('content') 9 @section('content')
10 10
11 - <div class="flex-fill flex" ng-non-bindable> 11 + <div class="flex-fill flex">
12 - <form action="{{$page->getUrl()}}" method="POST" class="flex flex-fill"> 12 + <form action="{{$page->getUrl()}}" data-page-id="{{ $page->id }}" method="POST" class="flex flex-fill">
13 <input type="hidden" name="_method" value="PUT"> 13 <input type="hidden" name="_method" value="PUT">
14 @include('pages/form', ['model' => $page]) 14 @include('pages/form', ['model' => $page])
15 </form> 15 </form>
......
1 1
2 2
3 3
4 -<div class="page-editor flex-fill flex" ng-non-bindable> 4 +<div class="page-editor flex-fill flex" ng-controller="PageEditController" page-id="{{ $model->id or 0 }}">
5 5
6 {{ csrf_field() }} 6 {{ csrf_field() }}
7 <div class="faded-small toolbar"> 7 <div class="faded-small toolbar">
...@@ -12,7 +12,10 @@ ...@@ -12,7 +12,10 @@
12 <a onclick="$('body>header').slideToggle();" class="text-button text-primary"><i class="zmdi zmdi-swap-vertical"></i>Toggle Header</a> 12 <a onclick="$('body>header').slideToggle();" class="text-button text-primary"><i class="zmdi zmdi-swap-vertical"></i>Toggle Header</a>
13 </div> 13 </div>
14 </div> 14 </div>
15 - <div class="col-sm-8 faded"> 15 + <div class="col-sm-4 faded text-center">
16 + <span ng-bind="draftText"></span>
17 + </div>
18 + <div class="col-sm-4 faded">
16 <div class="action-buttons"> 19 <div class="action-buttons">
17 <a href="{{ back()->getTargetUrl() }}" class="text-button text-primary"><i class="zmdi zmdi-close"></i>Cancel</a> 20 <a href="{{ back()->getTargetUrl() }}" class="text-button text-primary"><i class="zmdi zmdi-close"></i>Cancel</a>
18 <button type="submit" id="save-button" class="text-button text-pos"><i class="zmdi zmdi-floppy"></i>Save Page</button> 21 <button type="submit" id="save-button" class="text-button text-pos"><i class="zmdi zmdi-floppy"></i>Save Page</button>
...@@ -22,13 +25,13 @@ ...@@ -22,13 +25,13 @@
22 </div> 25 </div>
23 </div> 26 </div>
24 27
25 - <div class="title-input page-title clearfix"> 28 + <div class="title-input page-title clearfix" ng-non-bindable>
26 <div class="input"> 29 <div class="input">
27 @include('form/text', ['name' => 'name', 'placeholder' => 'Page Title']) 30 @include('form/text', ['name' => 'name', 'placeholder' => 'Page Title'])
28 </div> 31 </div>
29 </div> 32 </div>
30 <div class="edit-area flex-fill flex"> 33 <div class="edit-area flex-fill flex">
31 - <textarea id="html-editor" name="html" rows="5" 34 + <textarea id="html-editor" tinymce="editorOptions" ng-change="editorChange" ng-model="editorHtml" name="html" rows="5"
32 @if($errors->has('html')) class="neg" @endif>@if(isset($model) || old('html')){{htmlspecialchars( old('html') ? old('html') : $model->html)}}@endif</textarea> 35 @if($errors->has('html')) class="neg" @endif>@if(isset($model) || old('html')){{htmlspecialchars( old('html') ? old('html') : $model->html)}}@endif</textarea>
33 @if($errors->has('html')) 36 @if($errors->has('html'))
34 <div class="text-neg text-small">{{ $errors->first('html') }}</div> 37 <div class="text-neg text-small">{{ $errors->first('html') }}</div>
......
...@@ -24,10 +24,10 @@ ...@@ -24,10 +24,10 @@
24 24
25 <table class="table"> 25 <table class="table">
26 <tr> 26 <tr>
27 - <th>Name</th> 27 + <th width="40%">Name</th>
28 - <th colspan="2">Created By</th> 28 + <th colspan="2" width="20%">Created By</th>
29 - <th>Revision Date</th> 29 + <th width="20%">Revision Date</th>
30 - <th>Actions</th> 30 + <th width="20%">Actions</th>
31 </tr> 31 </tr>
32 @foreach($page->revisions as $revision) 32 @foreach($page->revisions as $revision)
33 <tr> 33 <tr>
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
38 @endif 38 @endif
39 </td> 39 </td>
40 <td> @if($revision->createdBy) {{$revision->createdBy->name}} @else Deleted User @endif</td> 40 <td> @if($revision->createdBy) {{$revision->createdBy->name}} @else Deleted User @endif</td>
41 - <td><small>{{$revision->created_at->format('jS F, Y H:i:s')}} ({{$revision->created_at->diffForHumans()}})</small></td> 41 + <td><small>{{$revision->created_at->format('jS F, Y H:i:s')}} <br> ({{$revision->created_at->diffForHumans()}})</small></td>
42 <td> 42 <td>
43 <a href="{{$revision->getUrl()}}" target="_blank">Preview</a> 43 <a href="{{$revision->getUrl()}}" target="_blank">Preview</a>
44 <span class="text-muted">&nbsp;|&nbsp;</span> 44 <span class="text-muted">&nbsp;|&nbsp;</span>
......