]> BookStack Code Mirror - bookstack/blob - resources/assets/js/controllers.js
Update entities.php
[bookstack] / resources / assets / js / controllers.js
1 "use strict";
2
3 const moment = require('moment');
4 require('moment/locale/en-gb');
5 const editorOptions = require("./pages/page-form");
6
7 moment.locale('en-gb');
8
9 module.exports = function (ngApp, events) {
10
11
12     ngApp.controller('PageEditController', ['$scope', '$http', '$attrs', '$interval', '$timeout', '$sce',
13         function ($scope, $http, $attrs, $interval, $timeout, $sce) {
14
15         $scope.editorOptions = editorOptions();
16         $scope.editContent = '';
17         $scope.draftText = '';
18         let pageId = Number($attrs.pageId);
19         let isEdit = pageId !== 0;
20         let autosaveFrequency = 30; // AutoSave interval in seconds.
21         let isMarkdown = $attrs.editorType === 'markdown';
22         $scope.draftsEnabled = $attrs.draftsEnabled === 'true';
23         $scope.isUpdateDraft = Number($attrs.pageUpdateDraft) === 1;
24         $scope.isNewPageDraft = Number($attrs.pageNewDraft) === 1;
25
26         // Set initial header draft text
27         if ($scope.isUpdateDraft || $scope.isNewPageDraft) {
28             $scope.draftText = trans('entities.pages_editing_draft');
29         } else {
30             $scope.draftText = trans('entities.pages_editing_page');
31         }
32
33         let autoSave = false;
34
35         let currentContent = {
36             title: false,
37             html: false
38         };
39
40         if (isEdit && $scope.draftsEnabled) {
41             setTimeout(() => {
42                 startAutoSave();
43             }, 1000);
44         }
45
46         // Actions specifically for the markdown editor
47         if (isMarkdown) {
48             $scope.displayContent = '';
49             // Editor change event
50             $scope.editorChange = function (content) {
51                 $scope.displayContent = $sce.trustAsHtml(content);
52             }
53         }
54
55         if (!isMarkdown) {
56             $scope.editorChange = function() {};
57         }
58
59         let lastSave = 0;
60
61         /**
62          * Start the AutoSave loop, Checks for content change
63          * before performing the costly AJAX request.
64          */
65         function startAutoSave() {
66             currentContent.title = $('#name').val();
67             currentContent.html = $scope.editContent;
68
69             autoSave = $interval(() => {
70                 // Return if manually saved recently to prevent bombarding the server
71                 if (Date.now() - lastSave < (1000*autosaveFrequency)/2) return;
72                 let newTitle = $('#name').val();
73                 let newHtml = $scope.editContent;
74
75                 if (newTitle !== currentContent.title || newHtml !== currentContent.html) {
76                     currentContent.html = newHtml;
77                     currentContent.title = newTitle;
78                     saveDraft();
79                 }
80
81             }, 1000 * autosaveFrequency);
82         }
83
84         let draftErroring = false;
85         /**
86          * Save a draft update into the system via an AJAX request.
87          */
88         function saveDraft() {
89             if (!$scope.draftsEnabled) return;
90             let data = {
91                 name: $('#name').val(),
92                 html: isMarkdown ? $sce.getTrustedHtml($scope.displayContent) : $scope.editContent
93             };
94
95             if (isMarkdown) data.markdown = $scope.editContent;
96
97             let url = window.baseUrl('/ajax/page/' + pageId + '/save-draft');
98             $http.put(url, data).then(responseData => {
99                 draftErroring = false;
100                 let updateTime = moment.utc(moment.unix(responseData.data.timestamp)).toDate();
101                 $scope.draftText = responseData.data.message + moment(updateTime).format('HH:mm');
102                 if (!$scope.isNewPageDraft) $scope.isUpdateDraft = true;
103                 showDraftSaveNotification();
104                 lastSave = Date.now();
105             }, errorRes => {
106                 if (draftErroring) return;
107                 events.emit('error', trans('errors.page_draft_autosave_fail'));
108                 draftErroring = true;
109             });
110         }
111
112         function showDraftSaveNotification() {
113             $scope.draftUpdated = true;
114             $timeout(() => {
115                 $scope.draftUpdated = false;
116             }, 2000)
117         }
118
119         $scope.forceDraftSave = function() {
120             saveDraft();
121         };
122
123         // Listen to save draft events from editor
124         $scope.$on('save-draft', saveDraft);
125
126         /**
127          * Discard the current draft and grab the current page
128          * content from the system via an AJAX request.
129          */
130         $scope.discardDraft = function () {
131             let url = window.baseUrl('/ajax/page/' + pageId);
132             $http.get(url).then(responseData => {
133                 if (autoSave) $interval.cancel(autoSave);
134                 $scope.draftText = trans('entities.pages_editing_page');
135                 $scope.isUpdateDraft = false;
136                 $scope.$broadcast('html-update', responseData.data.html);
137                 $scope.$broadcast('markdown-update', responseData.data.markdown || responseData.data.html);
138                 $('#name').val(responseData.data.name);
139                 $timeout(() => {
140                     startAutoSave();
141                 }, 1000);
142                 events.emit('success', trans('entities.pages_draft_discarded'));
143             });
144         };
145
146     }]);
147 };