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.
Route::delete('/{imageId}', 'ImageController@destroy');
});
+ // Ajax routes
+ Route::put('/ajax/page/{id}/save-draft', 'PageController@saveUpdateDraft');
+
// Links
Route::get('/link/{id}', 'PageController@redirectFromLink');
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()
-<?php
-
-namespace BookStack;
+<?php namespace BookStack;
use Illuminate\Database\Eloquent\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;
use Activity;
use BookStack\Book;
use BookStack\Exceptions\NotFoundException;
+use DOMDocument;
use Illuminate\Support\Str;
use BookStack\Page;
use BookStack\PageRevision;
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;
* 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)
*/
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;
/**
* 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
*/
$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) {
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
/**
* 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
*/
--- /dev/null
+<?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');
+ });
+ }
+}
}]);
+ 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
};
}]);
+ 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
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');
-module.exports = {
+var mceOptions = module.exports = {
selector: '#html-editor',
content_css: [
'/css/styles.css'
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;
@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)
@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>
-<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>
<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>
@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"> | </span>