Merge pull request #2742 from nathanwoulfe/temp-U4-6946

U4 6946 - Existing template file is overwritten when creating a new template in the backoffice
This commit is contained in:
Sebastiaan Janssen
2018-07-13 11:12:16 +02:00
committed by GitHub
4 changed files with 75 additions and 7 deletions

View File

@@ -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<ITemplate>(template, true, evtMsgs, additionalData);
@@ -1045,6 +1053,19 @@ namespace Umbraco.Core.Services
: Attempt<string>.Fail();
}
internal Attempt<string> TryGetViewPath(string fileName)
{
if (fileName.EndsWith(".cshtml") == false)
{
fileName += ".cshtml";
}
string viewPath = IOHelper.MapPath(SystemDirectories.MvcViews + "/" + fileName);
return System.IO.File.Exists(viewPath)
? Attempt<string>.Succeed(viewPath)
: Attempt<string>.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<string> 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
}
}
}

View File

@@ -427,5 +427,12 @@ namespace Umbraco.Core.Services
/// <param name="filepath">The filesystem path to the partial view.</param>
/// <returns>The size of the partial view.</returns>
long GetPartialViewFileSize(string filepath);
/// <summary>
/// Gets the content of a view.
/// </summary>
/// <param name="filename">The name of the view.</param>
/// <returns></returns>
string GetViewContent(string filename);
}
}
}

View File

@@ -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) {

View File

@@ -1,15 +1,13 @@
using System.Collections.Generic;
using AutoMapper;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using AutoMapper;
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;
@@ -178,6 +176,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)
{