private Task BuildEditViewModel(WorkDocumentPartEditViewModel vm, WorkDocumentPart part, WorkDocumentPartSettings settings) { // var settings = GetWorkDocumentPartSettings(part); // vm.ContentItem = part.ContentItem; vm.DocumentStyleProvider = GetDocumentStyleProvider(settings); vm.WorkDocumentPart = part; vm.Body = part.Body; vm.PartSettings = settings; return(Task.CompletedTask); }
public override async Task <IDisplayResult> UpdateAsync(WorkDocumentPart model, IUpdateModel updater, UpdatePartEditorContext context) { var vm = new WorkDocumentPartEditViewModel(); if (await updater.TryUpdateModelAsync(vm, Prefix, t => t.Body, t => t.Paths)) { // Deserializing an empty string doesn't return an array var items = string.IsNullOrWhiteSpace(vm.Paths) ? new List <EditMediaFieldItemInfo>() : JsonConvert.DeserializeObject <EditMediaFieldItemInfo[]>(vm.Paths).ToList(); if (items.Any()) { try { await _attachedMediaToContentItemService.HandleFilesOnContentPartUpdateAsync(items, model.ContentItem); //replace body temp file paths with new folder paths //because froala encodes url paths to avoid xss must decode first before finding and replacing paths var decodedHtml = HttpUtility.UrlDecode(vm.Body); foreach (var mediaFieldItemInfo in items) { if (!String.IsNullOrEmpty(mediaFieldItemInfo.NewPath)) { // vm.Body= vm.Body.Replace(mediaFieldItemInfo.Path, mediaFieldItemInfo.NewPath); decodedHtml = decodedHtml?.Replace(mediaFieldItemInfo.Path, mediaFieldItemInfo.NewPath); } } //set back html to viewmodel vm.Body = decodedHtml; } catch (Exception e) { updater.ModelState.AddModelError(Prefix, S["{0}: There was an error handling the files.", context.TypePartDefinition.DisplayName()]); _logger.LogError(e, "Error handling attached media files for field '{Field}'", context.TypePartDefinition.DisplayName()); } } model.Body = HttpUtility.HtmlDecode(vm.Body); //need this for liquidtemplatemanager which requires html in decoded format } ; return(Edit(model, context)); }