Skip to content
This repository was archived by the owner on Feb 10, 2024. It is now read-only.

Commit cdee614

Browse files
committed
Removed setting the InPreviewMode flag
The change we introduced from #117 to trick Umbraco in thinking that it was in full preview mode has had some bad side-effects, see issues #123, #124 and #125 for further details. This change removes us manually setting the `InPreviewMode` property, and adds "`dtgePreview`" to the partial-view's `ViewData`, so a developer can detect whether its rendering in DTGE's preview mode or not. I'm unsure how this change will impact #117, there is an extra level of complexity when attempting cross-package-compatibility, (in this case Vorto).
1 parent dd9ab08 commit cdee614

File tree

4 files changed

+23
-23
lines changed

4 files changed

+23
-23
lines changed

docs/developers-guide.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
1. [Introduction](#introduction)
66
2. [Getting Set Up](#getting-set-up)
7-
a. [System Requirements](#system-requirements)
7+
1. [System Requirements](#system-requirements)
88
3. [Configuring The Doc Type Grid Editor](#configuring-the-doc-type-grid-editor)
99
4. [Hooking Up The Doc Type Grid Editor](#hooking-up-the-doc-type-grid-editor)
1010
5. [Rendering a Doc Type Grid Editor](#rendering-a-doc-type-grid-editor)
11-
a. [Rendering Alternative Preview Content](#rendering-alternative-preview-content)
12-
b. [DocTypeGridEditorSurfaceController](#doctypegrideditorsurfacecontroller)
11+
1. [Rendering Alternative Preview Content](#rendering-alternative-preview-content)
12+
2. [DocTypeGridEditorSurfaceController](#doctypegrideditorsurfacecontroller)
1313
6. [Useful Links](#useful-links)
1414

1515
---
@@ -139,11 +139,11 @@ Because we treat your data as a standard `IPublishedContent` entity, that means
139139

140140
#### Rendering Alternative Preview Content
141141

142-
If your front end view is rather complex, you may decide that you want to feed the back office preview an alternative, less complex view. To do this, within your Razor view/partial, check `UmbracoContext.InPreviewMode` is set to true to detect being in preview mode to provide an alternative view.
142+
If your front end view is rather complex, you may decide that you want to feed the back office preview an alternative, less complex view. To do this, within your Razor view/partial, check `ViewData["dtgePreview"]` is set to true to detect being in preview mode to provide an alternative view.
143143

144144
```
145145
@inherits Umbraco.Web.Mvc.UmbracoViewPage
146-
@if (UmbracoContext.InPreviewMode)
146+
@if (ViewData["dtgePreview"] == true)
147147
{
148148
// Render preview view
149149
}

src/Our.Umbraco.DocTypeGridEditor/Web/Controllers/DocTypeGridEditorApiController.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,6 @@ public HttpResponseMessage GetPreviewMarkup([FromBody] PreviewData data, [FromUr
136136
System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
137137
}
138138

139-
// Set DTGE's preview to be in "preview mode", (storing the original value in a temp variable for resetting it later).
140-
var inPreviewMode = UmbracoContext.InPreviewMode;
141-
UmbracoContext.InPreviewMode = true;
142-
143139
// Get content node object
144140
var content = DocTypeGridEditorHelper.ConvertValueToContent(data.Id, data.ContentTypeAlias, data.Value);
145141

@@ -157,9 +153,6 @@ public HttpResponseMessage GetPreviewMarkup([FromBody] PreviewData data, [FromUr
157153
var partialName = "~/App_Plugins/DocTypeGridEditor/Render/DocTypeGridEditorPreviewer.cshtml";
158154
var markup = Helpers.ViewHelper.RenderPartial(partialName, model, UmbracoContext.HttpContext);
159155

160-
// Restore the "preview mode" to its original value
161-
UmbracoContext.InPreviewMode = inPreviewMode;
162-
163156
// Return response
164157
var response = new HttpResponseMessage
165158
{

src/Our.Umbraco.DocTypeGridEditor/Web/Controllers/DocTypeGridEditorSurfaceController.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Web.Mvc;
1+
using System;
2+
using System.Web.Mvc;
23
using Our.Umbraco.DocTypeGridEditor.Web.Helpers;
34
using Umbraco.Core;
45
using Umbraco.Core.Models;
@@ -29,7 +30,7 @@ public string PreviewViewPath
2930

3031
public bool IsPreview
3132
{
32-
get { return UmbracoContext.InPreviewMode; }
33+
get { return ControllerContext.RouteData.Values.TryGetValue("dtgePreview", out object value) && Convert.ToBoolean(value); }
3334
}
3435

3536
protected PartialViewResult CurrentPartialView(object model = null)
@@ -49,6 +50,8 @@ protected PartialViewResult CurrentPartialView(object model = null)
4950

5051
protected override PartialViewResult PartialView(string viewName, object model)
5152
{
53+
ViewData.Add("dtgePreview", IsPreview);
54+
5255
if (IsPreview && string.IsNullOrWhiteSpace(PreviewViewPath) == false)
5356
{
5457
var previewViewPath = GetFullViewPath(viewName, PreviewViewPath);

src/Our.Umbraco.DocTypeGridEditor/Web/Extensions/HtmlHelperExtensions.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Web;
22
using System.Web.Mvc;
33
using System.Web.Mvc.Html;
4+
using Our.Umbraco.DocTypeGridEditor.Extensions;
45
using Our.Umbraco.DocTypeGridEditor.Web.Helpers;
56
using Our.Umbraco.DocTypeGridEditor.Web.Mvc;
67
using Umbraco.Core;
@@ -46,9 +47,12 @@ public static HtmlString RenderDocTypeGridEditorItem(
4647
{
4748
dtgeModel = content,
4849
dtgeViewPath = viewPath,
49-
dtgePreviewViewPath = previewViewPath
50+
dtgePreviewViewPath = previewViewPath,
51+
dtgePreview = isPreview
5052
};
5153

54+
var viewData = new ViewDataDictionary() { { "dtgePreview", isPreview } };
55+
5256
// Try looking for surface controller with action named after the editor alias
5357
if (string.IsNullOrWhiteSpace(editorAlias) == false && SurfaceControllerHelper.SurfaceControllerExists(controllerName, editorAlias, true))
5458
{
@@ -89,19 +93,19 @@ public static HtmlString RenderDocTypeGridEditorItem(
8993
var fullPreviewViewPath = $"{previewViewPath}{editorAlias}.cshtml";
9094
if (ViewHelper.ViewExists(helper.ViewContext, fullPreviewViewPath, true))
9195
{
92-
return helper.Partial(fullPreviewViewPath, content);
96+
return helper.Partial(fullPreviewViewPath, content, viewData);
9397
}
9498

9599
fullPreviewViewPath = $"{previewViewPath}{content.DocumentTypeAlias}.cshtml";
96100
if (ViewHelper.ViewExists(helper.ViewContext, fullPreviewViewPath, true))
97101
{
98-
return helper.Partial(fullPreviewViewPath, content);
102+
return helper.Partial(fullPreviewViewPath, content, viewData);
99103
}
100104

101105
fullPreviewViewPath = $"{previewViewPath}Default.cshtml";
102106
if (ViewHelper.ViewExists(helper.ViewContext, fullPreviewViewPath, true))
103107
{
104-
return helper.Partial(fullPreviewViewPath, content);
108+
return helper.Partial(fullPreviewViewPath, content, viewData);
105109
}
106110
}
107111

@@ -111,29 +115,29 @@ public static HtmlString RenderDocTypeGridEditorItem(
111115
var fullViewPath = $"{viewPath}{editorAlias}.cshtml";
112116
if (ViewHelper.ViewExists(helper.ViewContext, fullViewPath, true))
113117
{
114-
return helper.Partial(fullViewPath, content);
118+
return helper.Partial(fullViewPath, content, viewData);
115119
}
116120

117121
fullViewPath = $"{viewPath}{content.DocumentTypeAlias}.cshtml";
118122
if (ViewHelper.ViewExists(helper.ViewContext, fullViewPath, true))
119123
{
120-
return helper.Partial(fullViewPath, content);
124+
return helper.Partial(fullViewPath, content, viewData);
121125
}
122126

123127
fullViewPath = $"{viewPath}Default.cshtml";
124128
if (ViewHelper.ViewExists(helper.ViewContext, fullViewPath, true))
125129
{
126-
return helper.Partial(fullViewPath, content);
130+
return helper.Partial(fullViewPath, content, viewData);
127131
}
128132
}
129133

130134
// Resort to standard partial views
131135
if (ViewHelper.ViewExists(helper.ViewContext, editorAlias, true))
132136
{
133-
return helper.Partial(editorAlias, content);
137+
return helper.Partial(editorAlias, content, viewData);
134138
}
135139

136-
return helper.Partial(content.DocumentTypeAlias, content);
140+
return helper.Partial(content.DocumentTypeAlias, content, viewData);
137141
}
138142
}
139143
}

0 commit comments

Comments
 (0)