-
Notifications
You must be signed in to change notification settings - Fork 3
/
TinyEditor.cs
254 lines (234 loc) · 10 KB
/
TinyEditor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
using Sitecore;
using Sitecore.Configuration;
using Sitecore.Diagnostics;
using Sitecore.Globalization;
using Sitecore.Text;
using Sitecore.Web;
using Sitecore.Web.UI.HtmlControls;
using Sitecore.Web.UI.Sheer;
using System;
using System.Web;
namespace TinyMCERTE
{
public class TinyEditor : Frame {
/// <summary>The handle.</summary>
private string handle;
/// <summary>The item version.</summary>
private string itemVersion;
/// <summary>The set value on pre render.</summary>
private bool setValueOnPreRender;
public TinyEditor() {
this.Class = "scContentControlHtml2";
this.Activation = true;
this.Attributes["tabindex"] = "-1";
}
/// <summary>Gets or sets the field ID.</summary>
/// <value>The field ID.</value>
public string FieldID {
get {
return this.GetViewStateString(ExtensionMethods.nameof(() => FieldID));
}
set {
Assert.ArgumentNotNull((object)value, ExtensionMethods.nameof(() => value));
this.SetViewStateString(ExtensionMethods.nameof(() => FieldID), value);
}
}
/// <summary>Gets or sets the item ID.</summary>
/// <value>The item ID.</value>
public string ItemID {
get {
return this.GetViewStateString(ExtensionMethods.nameof(() => ItemID));
}
set {
Assert.ArgumentNotNull((object)value, ExtensionMethods.nameof(() => value));
this.SetViewStateString(ExtensionMethods.nameof(() => ItemID), value);
}
}
/// <summary>Gets or sets the item language.</summary>
/// <value>The item language.</value>
public string ItemLanguage {
get {
return this.GetViewStateString(ExtensionMethods.nameof(() => ItemLanguage));
}
set {
Assert.ArgumentNotNull((object)value, ExtensionMethods.nameof(() => value));
this.SetViewStateString(ExtensionMethods.nameof(() => ItemLanguage), value);
}
}
/// <summary>Gets or sets the item version.</summary>
/// <value>The item version.</value>
public string ItemVersion {
get {
return this.itemVersion;
}
set {
Assert.ArgumentNotNull((object)value, ExtensionMethods.nameof(() => value));
this.itemVersion = value;
}
}
/// <summary>Gets or sets the source.</summary>
/// <value>The source.</value>
public string Source {
get {
return this.GetViewStateString(ExtensionMethods.nameof(() => Source));
}
set {
Assert.ArgumentNotNull((object)value, ExtensionMethods.nameof(() => value));
this.SetViewStateString(ExtensionMethods.nameof(() => Source), value);
}
}
/// <summary>
/// Gets or sets a value indicating whether this instance is tracking modified.
/// </summary>
/// <value><c>true</c> if this instance is tracking modified; otherwise, <c>false</c>.</value>
public bool TrackModified {
get {
return this.GetViewStateBool(ExtensionMethods.nameof(() => TrackModified), true);
}
set {
this.SetViewStateBool(ExtensionMethods.nameof(() => TrackModified), value, true);
}
}
/// <summary>Handles the message.</summary>
/// <param name="message">The message.</param>
public override void HandleMessage(Message message) {
Assert.ArgumentNotNull((object)message, ExtensionMethods.nameof(() => message));
base.HandleMessage(message);
if (!(message["id"] == this.ID))
return;
switch (message.Name) {
case "tinyrte:edit":
Sitecore.Context.ClientPage.Start((object)this, "EditHtmlTinyMCE");
break;
}
}
/// <summary>Edits the text.</summary>
/// <param name="args">The args.</param>
protected void EditHtmlTinyMCE(ClientPipelineArgs args) {
TinyEditorConfigurationResult configurationResult = Utils.LoadTinyEditorConfiguration();
int windowWidth, windowHeight;
if (!int.TryParse(configurationResult.EditorWindowWidth, out windowWidth)) {
windowWidth = 1220;
}
if (!int.TryParse(configurationResult.EditorWindowHeight, out windowHeight)) {
windowHeight = 730;
}
Assert.ArgumentNotNull((object)args, ExtensionMethods.nameof(() => args));
if (this.Disabled)
return;
if (args.IsPostBack) {
if (args.Result == null || !(args.Result != "undefined"))
return;
this.UpdateHtml(args);
} else {
TinyMCEEditorUrl richTextEditorUrl = new TinyMCEEditorUrl() {
Conversion = TinyMCEEditorUrl.HtmlConversion.DoNotConvert,
Disabled = this.Disabled,
FieldID = this.FieldID,
ID = this.ID,
ItemID = this.ItemID,
Language = this.ItemLanguage,
Mode = string.Empty,
ShowInFrameBasedDialog = true,
Source = this.Source,
Url = "/sitecore/shell/Controls/TinyMCE Editor/EditorPage.aspx",
Value = this.Value,
Version = this.ItemVersion
};
UrlString url = richTextEditorUrl.GetUrl();
this.handle = richTextEditorUrl.Handle;
SheerResponse.ShowModalDialog(new ModalDialogOptions(url.ToString()) {
Width = string.Format("{0}px", windowWidth),
Height = string.Format("{0}px", windowHeight),
Response = true,
Header = Translate.Text("Rich Text Editor")
});
args.WaitForPostBack();
}
}
/// <summary>Loads the post data.</summary>
/// <param name="value">The value.</param>
/// <returns>The load post data.</returns>
protected override bool LoadPostData(string value) {
Assert.ArgumentNotNull((object)value, ExtensionMethods.nameof(() => value));
if (!(value != this.Value))
return false;
this.Value = value;
return true;
}
/// <summary>Raises the <see cref="E:System.Web.UI.Control.Load"></see> event.</summary>
/// <param name="e">The <see cref="T:System.EventArgs"></see> object that contains the event data.</param>
protected override void OnLoad(EventArgs e) {
Assert.ArgumentNotNull((object)e, ExtensionMethods.nameof(() => e));
base.OnLoad(e);
if (Sitecore.Context.ClientPage.IsEvent)
return;
TinyMCEEditorUrl tinyMCEEditorUrl = new TinyMCEEditorUrl() {
Conversion = TinyMCEEditorUrl.HtmlConversion.DoNotConvert,
Disabled = this.Disabled,
FieldID = this.FieldID,
ID = this.ID,
ItemID = this.ItemID,
Language = this.ItemLanguage,
Mode = "ContentEditor",
ShowInFrameBasedDialog = true,
Source = this.Source,
Url = string.Empty,
Value = this.Value,
Version = this.ItemVersion
};
UrlString url = tinyMCEEditorUrl.GetUrl();
this.handle = tinyMCEEditorUrl.Handle;
this.setValueOnPreRender = true;
this.SourceUri = url.ToString();
}
protected override void OnPreRender(EventArgs e) {
Assert.ArgumentNotNull((object)e, ExtensionMethods.nameof(() => e));
base.OnPreRender(e);
if (this.setValueOnPreRender)
HttpContext.Current.Session[this.handle] = (object)this.Value;
this.ServerProperties["ItemLanguage"] = this.ServerProperties["ItemLanguage"];
this.ServerProperties["Source"] = this.ServerProperties["Source"];
this.ServerProperties["FieldID"] = this.ServerProperties["FieldID"];
}
/// <summary>Sets the modified.</summary>
protected virtual void SetModified() {
if (!this.TrackModified)
return;
Sitecore.Context.ClientPage.Modified = true;
SheerResponse.Eval("scContent.startValidators()");
}
/// <summary>Gets or sets the value.</summary>
/// <value>The value.</value>
public override string Value {
get {
return base.Value;
}
set {
base.Value = value;
}
}
/// <summary>Updates the HTML.</summary>
/// <param name="args">The arguments.</param>
protected virtual void UpdateHtml(ClientPipelineArgs args) {
Assert.ArgumentNotNull((object)args, ExtensionMethods.nameof(() => args));
string str = args.Result;
if (str == "__#!$No value$!#__")
str = string.Empty;
string text = this.ProcessValidateScripts(str);
if (text != this.Value)
this.SetModified();
SheerResponse.Eval("scForm.browser.getControl('" + this.ID + "').contentWindow.scSetText(" + StringUtil.EscapeJavascriptString(text) + ")");
SheerResponse.Eval("scContent.startValidators()");
}
/// <summary>Processes the validate scripts.</summary>
/// <param name="value">The value.</param>
/// <returns>Result of the value.</returns>
protected string ProcessValidateScripts(string value) {
if (Settings.HtmlEditor.RemoveScripts) {
value = WebUtil.RemoveInlineScripts(value);
}
return value;
}
}
}