- $scope.saveImageDetails = function (event) {
- event.preventDefault();
- var url = '/images/update/' + $scope.selectedImage.id;
- $http.put(url, this.selectedImage).then((response) => {
- $scope.imageUpdateSuccess = true;
- $timeout(() => {
- $scope.imageUpdateSuccess = false;
- }, 3000);
- }, (response) => {
- var errors = response.data;
- var message = '';
- Object.keys(errors).forEach((key) => {
- message += errors[key].join('\n');
- });
- $scope.imageUpdateFailure = message;
- $timeout(() => {
- $scope.imageUpdateFailure = false;
- }, 5000);
- });
- };
+ if (isEdit && $scope.draftsEnabled) {
+ setTimeout(() => {
+ startAutoSave();
+ }, 1000);
+ }
+
+ // Actions specifically for the markdown editor
+ if (isMarkdown) {
+ $scope.displayContent = '';
+ // Editor change event
+ $scope.editorChange = function (content) {
+ $scope.displayContent = $sce.trustAsHtml(content);
+ }
+ }
+
+ if (!isMarkdown) {
+ $scope.editorChange = function() {};
+ }
+
+ let lastSave = 0;
+
+ /**
+ * Start the AutoSave loop, Checks for content change
+ * before performing the costly AJAX request.
+ */
+ function startAutoSave() {
+ currentContent.title = $('#name').val();
+ currentContent.html = $scope.editContent;
+
+ autoSave = $interval(() => {
+ // Return if manually saved recently to prevent bombarding the server
+ if (Date.now() - lastSave < (1000*autosaveFrequency)/2) return;
+ let newTitle = $('#name').val();
+ let newHtml = $scope.editContent;
+
+ if (newTitle !== currentContent.title || newHtml !== currentContent.html) {
+ currentContent.html = newHtml;
+ currentContent.title = newTitle;
+ saveDraft();
+ }