From 9b8f4c1a9a9242bb87b741d1379ed5e0acdb95ee Mon Sep 17 00:00:00 2001 From: Nathan Woulfe Date: Fri, 29 Jun 2018 20:27:28 +1000 Subject: [PATCH 1/5] check for existing file content when creating/saving new template --- src/Umbraco.Core/Services/FileService.cs | 40 ++++++++++++++++++- src/Umbraco.Core/Services/IFileService.cs | 9 ++++- src/Umbraco.Web/Editors/TemplateController.cs | 11 +++++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/Umbraco.Core/Services/FileService.cs b/src/Umbraco.Core/Services/FileService.cs index 916c036a69..4428175fa9 100644 --- a/src/Umbraco.Core/Services/FileService.cs +++ b/src/Umbraco.Core/Services/FileService.cs @@ -291,6 +291,14 @@ namespace Umbraco.Core.Services {"ContentTypeAlias", contentTypeAlias}, }; + // check that the template hasn't been created on disk before creating the content type + // if it exists, set the new template content to the existing file content + string content = GetViewContent(contentTypeAlias); + if (content.IsNullOrWhiteSpace() == false) + { + template.Content = content; + } + using (var uow = UowProvider.GetUnitOfWork()) { var saveEventArgs = new SaveEventArgs(template, true, evtMsgs, additionalData); @@ -1045,6 +1053,19 @@ namespace Umbraco.Core.Services : Attempt.Fail(); } + internal Attempt TryGetViewPath(string fileName) + { + if (fileName.EndsWith(".cshtml") == false) + { + fileName += ".cshtml"; + } + + string viewPath = IOHelper.MapPath(SystemDirectories.MvcViews + "/" + fileName); + return System.IO.File.Exists(viewPath) + ? Attempt.Succeed(viewPath) + : Attempt.Fail(); + } + private IPartialViewRepository GetPartialViewRepository(PartialViewType partialViewType, IUnitOfWork uow) { switch (partialViewType) @@ -1076,6 +1097,23 @@ namespace Umbraco.Core.Services return GetPartialViewMacroSnippetContent(snippetName, PartialViewType.PartialViewMacro); } + public string GetViewContent(string filename) + { + if (filename.IsNullOrWhiteSpace()) + throw new ArgumentNullException(nameof(filename)); + + Attempt viewAttempt = TryGetViewPath(filename); + if (viewAttempt.Success == false) + { + return string.Empty; + } + + using (var view = new StreamReader(System.IO.File.OpenRead(viewAttempt.Result))) + { + return view.ReadToEnd().Trim(); + } + } + private string GetPartialViewMacroSnippetContent(string snippetName, PartialViewType partialViewType) { if (snippetName.IsNullOrWhiteSpace()) @@ -1265,4 +1303,4 @@ namespace Umbraco.Core.Services #endregion } -} \ No newline at end of file +} diff --git a/src/Umbraco.Core/Services/IFileService.cs b/src/Umbraco.Core/Services/IFileService.cs index 74de0861d8..0031562697 100644 --- a/src/Umbraco.Core/Services/IFileService.cs +++ b/src/Umbraco.Core/Services/IFileService.cs @@ -427,5 +427,12 @@ namespace Umbraco.Core.Services /// The filesystem path to the partial view. /// The size of the partial view. long GetPartialViewFileSize(string filepath); + + /// + /// Gets the content of a view. + /// + /// The name of the view. + /// + string GetViewContent(string filename); } -} \ No newline at end of file +} diff --git a/src/Umbraco.Web/Editors/TemplateController.cs b/src/Umbraco.Web/Editors/TemplateController.cs index 78855c95c2..3df6cb2aba 100644 --- a/src/Umbraco.Web/Editors/TemplateController.cs +++ b/src/Umbraco.Web/Editors/TemplateController.cs @@ -1,9 +1,12 @@ using System.Collections.Generic; +using System.IO; using System.Linq; using System.Net; using System.Net.Http; +using System.Web; using System.Web.Http; using AutoMapper; +using Umbraco.Core; using Umbraco.Core.IO; using Umbraco.Core.Models; using Umbraco.Core.Services; @@ -178,6 +181,14 @@ namespace Umbraco.Web.Editors else { //create + + // file might already be on disk, if so grab the content to avoid overwriting + string content = Services.FileService.GetViewContent(display.Alias); + if (string.IsNullOrEmpty(content) == false) + { + display.Content = content; + } + ITemplate master = null; if (string.IsNullOrEmpty(display.MasterTemplateAlias) == false) { From e6842a10206721e9be7bd35e6474d4b34683212f Mon Sep 17 00:00:00 2001 From: Nathan Woulfe Date: Fri, 29 Jun 2018 20:34:04 +1000 Subject: [PATCH 2/5] clean up usings --- src/Umbraco.Web/Editors/TemplateController.cs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/Umbraco.Web/Editors/TemplateController.cs b/src/Umbraco.Web/Editors/TemplateController.cs index 3df6cb2aba..132e804a95 100644 --- a/src/Umbraco.Web/Editors/TemplateController.cs +++ b/src/Umbraco.Web/Editors/TemplateController.cs @@ -1,18 +1,13 @@ -using System.Collections.Generic; -using System.IO; +using AutoMapper; +using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; -using System.Web; using System.Web.Http; -using AutoMapper; -using Umbraco.Core; using Umbraco.Core.IO; using Umbraco.Core.Models; -using Umbraco.Core.Services; using Umbraco.Web.Models.ContentEditing; using Umbraco.Web.Mvc; -using Umbraco.Web.WebApi; using Umbraco.Web.WebApi.Filters; using Constants = Umbraco.Core.Constants; From e34926acd62f0590f4929dfdcd5e2ccedfeed514 Mon Sep 17 00:00:00 2001 From: Nathan Woulfe Date: Fri, 29 Jun 2018 20:27:28 +1000 Subject: [PATCH 3/5] check for existing file content when creating/saving new template --- src/Umbraco.Core/Services/FileService.cs | 40 ++++++++++++++++++- src/Umbraco.Core/Services/IFileService.cs | 9 ++++- src/Umbraco.Web/Editors/TemplateController.cs | 11 +++++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/Umbraco.Core/Services/FileService.cs b/src/Umbraco.Core/Services/FileService.cs index 916c036a69..4428175fa9 100644 --- a/src/Umbraco.Core/Services/FileService.cs +++ b/src/Umbraco.Core/Services/FileService.cs @@ -291,6 +291,14 @@ namespace Umbraco.Core.Services {"ContentTypeAlias", contentTypeAlias}, }; + // check that the template hasn't been created on disk before creating the content type + // if it exists, set the new template content to the existing file content + string content = GetViewContent(contentTypeAlias); + if (content.IsNullOrWhiteSpace() == false) + { + template.Content = content; + } + using (var uow = UowProvider.GetUnitOfWork()) { var saveEventArgs = new SaveEventArgs(template, true, evtMsgs, additionalData); @@ -1045,6 +1053,19 @@ namespace Umbraco.Core.Services : Attempt.Fail(); } + internal Attempt TryGetViewPath(string fileName) + { + if (fileName.EndsWith(".cshtml") == false) + { + fileName += ".cshtml"; + } + + string viewPath = IOHelper.MapPath(SystemDirectories.MvcViews + "/" + fileName); + return System.IO.File.Exists(viewPath) + ? Attempt.Succeed(viewPath) + : Attempt.Fail(); + } + private IPartialViewRepository GetPartialViewRepository(PartialViewType partialViewType, IUnitOfWork uow) { switch (partialViewType) @@ -1076,6 +1097,23 @@ namespace Umbraco.Core.Services return GetPartialViewMacroSnippetContent(snippetName, PartialViewType.PartialViewMacro); } + public string GetViewContent(string filename) + { + if (filename.IsNullOrWhiteSpace()) + throw new ArgumentNullException(nameof(filename)); + + Attempt viewAttempt = TryGetViewPath(filename); + if (viewAttempt.Success == false) + { + return string.Empty; + } + + using (var view = new StreamReader(System.IO.File.OpenRead(viewAttempt.Result))) + { + return view.ReadToEnd().Trim(); + } + } + private string GetPartialViewMacroSnippetContent(string snippetName, PartialViewType partialViewType) { if (snippetName.IsNullOrWhiteSpace()) @@ -1265,4 +1303,4 @@ namespace Umbraco.Core.Services #endregion } -} \ No newline at end of file +} diff --git a/src/Umbraco.Core/Services/IFileService.cs b/src/Umbraco.Core/Services/IFileService.cs index 74de0861d8..0031562697 100644 --- a/src/Umbraco.Core/Services/IFileService.cs +++ b/src/Umbraco.Core/Services/IFileService.cs @@ -427,5 +427,12 @@ namespace Umbraco.Core.Services /// The filesystem path to the partial view. /// The size of the partial view. long GetPartialViewFileSize(string filepath); + + /// + /// Gets the content of a view. + /// + /// The name of the view. + /// + string GetViewContent(string filename); } -} \ No newline at end of file +} diff --git a/src/Umbraco.Web/Editors/TemplateController.cs b/src/Umbraco.Web/Editors/TemplateController.cs index 78855c95c2..3df6cb2aba 100644 --- a/src/Umbraco.Web/Editors/TemplateController.cs +++ b/src/Umbraco.Web/Editors/TemplateController.cs @@ -1,9 +1,12 @@ using System.Collections.Generic; +using System.IO; using System.Linq; using System.Net; using System.Net.Http; +using System.Web; using System.Web.Http; using AutoMapper; +using Umbraco.Core; using Umbraco.Core.IO; using Umbraco.Core.Models; using Umbraco.Core.Services; @@ -178,6 +181,14 @@ namespace Umbraco.Web.Editors else { //create + + // file might already be on disk, if so grab the content to avoid overwriting + string content = Services.FileService.GetViewContent(display.Alias); + if (string.IsNullOrEmpty(content) == false) + { + display.Content = content; + } + ITemplate master = null; if (string.IsNullOrEmpty(display.MasterTemplateAlias) == false) { From 416cea796e7cd5493c41fcbd929be356ebda4a3b Mon Sep 17 00:00:00 2001 From: Nathan Woulfe Date: Fri, 29 Jun 2018 20:34:04 +1000 Subject: [PATCH 4/5] clean up usings --- src/Umbraco.Web/Editors/TemplateController.cs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/Umbraco.Web/Editors/TemplateController.cs b/src/Umbraco.Web/Editors/TemplateController.cs index 3df6cb2aba..132e804a95 100644 --- a/src/Umbraco.Web/Editors/TemplateController.cs +++ b/src/Umbraco.Web/Editors/TemplateController.cs @@ -1,18 +1,13 @@ -using System.Collections.Generic; -using System.IO; +using AutoMapper; +using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; -using System.Web; using System.Web.Http; -using AutoMapper; -using Umbraco.Core; using Umbraco.Core.IO; using Umbraco.Core.Models; -using Umbraco.Core.Services; using Umbraco.Web.Models.ContentEditing; using Umbraco.Web.Mvc; -using Umbraco.Web.WebApi; using Umbraco.Web.WebApi.Filters; using Constants = Umbraco.Core.Constants; From f9761ad2f848c6ab421814b09fe3fb1916c043aa Mon Sep 17 00:00:00 2001 From: Nathan Woulfe Date: Thu, 12 Jul 2018 14:32:13 +1000 Subject: [PATCH 5/5] auto save on create to fetch existing view --- .../src/views/templates/edit.controller.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Umbraco.Web.UI.Client/src/views/templates/edit.controller.js b/src/Umbraco.Web.UI.Client/src/views/templates/edit.controller.js index fa30571708..1e2934028b 100644 --- a/src/Umbraco.Web.UI.Client/src/views/templates/edit.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/templates/edit.controller.js @@ -27,7 +27,7 @@ vm.page.keyboardShortcutsOverview.push(templateHelper.getTemplateEditorShortcuts()); - vm.save = function () { + vm.save = function (suppressNotification) { vm.page.saveButtonState = "busy"; vm.template.content = vm.editor.getValue(); @@ -44,11 +44,13 @@ rebindCallback: function (orignal, saved) {} }).then(function (saved) { + if (!suppressNotification) { localizationService.localizeMany(["speechBubbles_templateSavedHeader", "speechBubbles_templateSavedText"]).then(function(data){ var header = data[0]; var message = data[1]; notificationsService.success(header, message); }); + } vm.page.saveButtonState = "success"; @@ -134,6 +136,21 @@ vm.page.loading = false; vm.template = template; + // if this is a new template, bind to the blur event on the name + if ($routeParams.create) { + $timeout(function() { + var nameField = angular.element(document.querySelector('[data-element="editor-name-field"]')); + if (nameField) { + nameField.bind('blur', function(event) { + if (event.target.value) { + vm.save(true); + } + }); + } + }); + } + + //sync state editorState.set(vm.template); navigationService.syncTree({ tree: "templates", path: vm.template.path, forceReload: true }).then(function (syncArgs) {