/// <summary> /// Converts this <see cref="ClientSideText"/> control to it's html representation /// </summary> /// <returns>Html representation of this <see cref="ClientSideText"/> control</returns> public override string ToHtml() { // Obtain the json data ClientSideTextControlData controlData = new ClientSideTextControlData() { ControlType = this.ControlType, Id = this.InstanceId.ToString("D"), EditorType = "CKEditor" }; jsonControlData = JsonConvert.SerializeObject(controlData); StringBuilder html = new StringBuilder(100); using (var htmlWriter = new HtmlTextWriter(new System.IO.StringWriter(html), "")) { htmlWriter.NewLine = string.Empty; htmlWriter.AddAttribute(CanvasControlAttribute, this.CanvasControlData); htmlWriter.AddAttribute(CanvasDataVersionAttribute, this.DataVersion); htmlWriter.AddAttribute(ControlDataAttribute, this.JsonControlData); htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div); htmlWriter.AddAttribute(TextRteAttribute, this.Rte); htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div); htmlWriter.RenderBeginTag(HtmlTextWriterTag.P); htmlWriter.Write(this.Text); htmlWriter.RenderEndTag(); htmlWriter.RenderEndTag(); htmlWriter.RenderEndTag(); } return(html.ToString()); }
internal override void FromHtml(IElement element) { base.FromHtml(element); var div = element.GetElementsByTagName("div").Where(a => a.HasAttribute(TextRteAttribute)).FirstOrDefault(); this.rte = div.GetAttribute(TextRteAttribute); // By default text is wrapped in a Paragraph, need to drop it to avoid getting multiple paragraphs on page edits if ((div.FirstChild as IElement).TagName.Equals("P", StringComparison.InvariantCultureIgnoreCase)) { this.Text = (div.FirstChild as IElement).InnerHtml; } else { this.Text = div.InnerHtml; } // load data from the data-sp-controldata attribute var jsonSerializerSettings = new JsonSerializerSettings() { MissingMemberHandling = MissingMemberHandling.Ignore }; this.spControlData = JsonConvert.DeserializeObject <ClientSideTextControlData>(element.GetAttribute(CanvasControl.ControlDataAttribute), jsonSerializerSettings); this.controlType = this.spControlData.ControlType; }
/// <summary> /// Converts this <see cref="ClientSideText"/> control to it's html representation /// </summary> /// <param name="controlIndex">The sequence of the control inside the section</param> /// <returns>Html representation of this <see cref="ClientSideText"/> control</returns> public override string ToHtml(float controlIndex) { // Can this control be hosted in this section type? if (this.Section.Type == CanvasSectionTemplate.OneColumnFullWidth) { throw new Exception("You cannot host text controls inside a one column full width section, only an image web part or hero web part are allowed"); } // Obtain the json data ClientSideTextControlData controlData = new ClientSideTextControlData() { ControlType = this.ControlType, Id = this.InstanceId.ToString("D"), Position = new ClientSideCanvasControlPosition() { ZoneIndex = this.Section.Order, SectionIndex = this.Column.Order, SectionFactor = this.Column.ColumnFactor, ControlIndex = controlIndex, }, EditorType = "CKEditor" }; jsonControlData = JsonConvert.SerializeObject(controlData); StringBuilder html = new StringBuilder(100); #if NETSTANDARD2_0 html.Append($@"<div {CanvasControlAttribute}=""{this.CanvasControlData}"" {CanvasDataVersionAttribute}=""{ this.DataVersion}"" {ControlDataAttribute}=""{this.jsonControlData.Replace("\"", """)}"">"); html.Append($@"<div {TextRteAttribute}=""{this.Rte}"">"); html.Append($@"<p>{this.Text}</p>"); html.Append("</div>"); html.Append("</div>"); #else using (var htmlWriter = new HtmlTextWriter(new System.IO.StringWriter(html), "")) { htmlWriter.NewLine = string.Empty; htmlWriter.AddAttribute(CanvasControlAttribute, this.CanvasControlData); htmlWriter.AddAttribute(CanvasDataVersionAttribute, this.DataVersion); htmlWriter.AddAttribute(ControlDataAttribute, this.JsonControlData); htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div); htmlWriter.AddAttribute(TextRteAttribute, this.Rte); htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div); htmlWriter.RenderBeginTag(HtmlTextWriterTag.P); htmlWriter.Write(this.Text); htmlWriter.RenderEndTag(); htmlWriter.RenderEndTag(); htmlWriter.RenderEndTag(); } #endif return(html.ToString()); }
/// <summary> /// Converts this <see cref="ClientSideText"/> control to it's html representation /// </summary> /// <param name="controlIndex">The sequence of the control inside the section</param> /// <returns>Html representation of this <see cref="ClientSideText"/> control</returns> public override string ToHtml(int controlIndex) { // Can this control be hosted in this zone type? if (this.Zone.Type == CanvasZoneTemplate.OneColumnFullWidth) { throw new Exception("You cannot host text controls inside a one column full width zone, only an image web part or hero web part are allowed"); } // Obtain the json data ClientSideTextControlData controlData = new ClientSideTextControlData() { ControlType = this.ControlType, Id = this.InstanceId.ToString("D"), Position = new ClientSideCanvasControlPosition() { ZoneIndex = this.Zone.Order, SectionIndex = this.Section.Order, SectionFactor = this.Section.SectionFactor, ControlIndex = controlIndex, }, EditorType = "CKEditor" }; jsonControlData = JsonConvert.SerializeObject(controlData); StringBuilder html = new StringBuilder(100); using (var htmlWriter = new HtmlTextWriter(new System.IO.StringWriter(html), "")) { htmlWriter.NewLine = string.Empty; htmlWriter.AddAttribute(CanvasControlAttribute, this.CanvasControlData); htmlWriter.AddAttribute(CanvasDataVersionAttribute, this.DataVersion); htmlWriter.AddAttribute(ControlDataAttribute, this.JsonControlData); htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div); htmlWriter.AddAttribute(TextRteAttribute, this.Rte); htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div); htmlWriter.RenderBeginTag(HtmlTextWriterTag.P); htmlWriter.Write(this.Text); htmlWriter.RenderEndTag(); htmlWriter.RenderEndTag(); htmlWriter.RenderEndTag(); } return(html.ToString()); }
internal override void FromHtml(IElement element) { base.FromHtml(element); var div = element.GetElementsByTagName("div").Where(a => a.HasAttribute(TextRteAttribute)).FirstOrDefault(); this.rte = div.GetAttribute(TextRteAttribute); this.Text = div.InnerHtml; // load data from the data-sp-controldata attribute var jsonSerializerSettings = new JsonSerializerSettings() { MissingMemberHandling = MissingMemberHandling.Ignore }; this.spControlData = JsonConvert.DeserializeObject <ClientSideTextControlData>(element.GetAttribute(CanvasControl.ControlDataAttribute), jsonSerializerSettings); this.controlType = this.spControlData.ControlType; }
internal override void FromHtml(IElement element) { base.FromHtml(element); var div = element.GetElementsByTagName("div").Where(a => a.HasAttribute(TextRteAttribute)).FirstOrDefault(); if (div != null) { this.rte = div.GetAttribute(TextRteAttribute); } else { // supporting updated rendering of Text controls, no nested DIV tag with the data-sp-rte attribute...so HTML content is embedded at the root this.rte = ""; div = element; } // By default simple plain text is wrapped in a Paragraph, need to drop it to avoid getting multiple paragraphs on page edits. // Only drop the paragraph tag when there's only one Paragraph element underneath the DIV tag if ((div.FirstChild != null && (div.FirstChild as IElement) != null && (div.FirstChild as IElement).TagName.Equals("P", StringComparison.InvariantCultureIgnoreCase)) && (div.ChildElementCount == 1)) { this.Text = (div.FirstChild as IElement).InnerHtml; } else { this.Text = div.InnerHtml; } // load data from the data-sp-controldata attribute var jsonSerializerSettings = new JsonSerializerSettings() { MissingMemberHandling = MissingMemberHandling.Ignore }; this.spControlData = JsonConvert.DeserializeObject <ClientSideTextControlData>(element.GetAttribute(CanvasControl.ControlDataAttribute), jsonSerializerSettings); this.controlType = this.spControlData.ControlType; }
/// <summary> /// Converts this <see cref="ClientSideText"/> control to it's html representation /// </summary> /// <param name="controlIndex">The sequence of the control inside the section</param> /// <returns>Html representation of this <see cref="ClientSideText"/> control</returns> public override string ToHtml(float controlIndex) { // Can this control be hosted in this section type? if (this.Section.Type == CanvasSectionTemplate.OneColumnFullWidth) { throw new Exception("You cannot host text controls inside a one column full width section, only an image web part or hero web part are allowed"); } // Obtain the json data ClientSideTextControlData controlData = new ClientSideTextControlData() { ControlType = this.ControlType, Id = this.InstanceId.ToString("D"), Position = new ClientSideCanvasControlPosition() { ZoneIndex = this.Section.Order, SectionIndex = this.Column.Order, SectionFactor = this.Column.ColumnFactor, #if !SP2019 LayoutIndex = this.Column.LayoutIndex, #endif ControlIndex = controlIndex, }, Emphasis = new ClientSideSectionEmphasis() { ZoneEmphasis = this.Column.VerticalSectionEmphasis.HasValue ? this.Column.VerticalSectionEmphasis.Value : this.Section.ZoneEmphasis, }, EditorType = "CKEditor" }; #if !SP2019 if (this.section.Type == CanvasSectionTemplate.OneColumnVerticalSection) { if (this.section.Columns.First().Equals(this.Column)) { controlData.Position.SectionFactor = 12; } } #endif jsonControlData = JsonConvert.SerializeObject(controlData); try { var nodeList = new HtmlParser().ParseFragment(this.Text, null); this.previewText = string.Concat(nodeList.Select(x => x.Text())); } catch { } StringBuilder html = new StringBuilder(100); #if NETSTANDARD2_0 html.Append($@"<div {CanvasControlAttribute}=""{this.CanvasControlData}"" {CanvasDataVersionAttribute}=""{ this.DataVersion}"" {ControlDataAttribute}=""{this.jsonControlData.Replace("\"", """)}"">"); html.Append($@"<div {TextRteAttribute}=""{this.Rte}"">"); if (this.Text.Trim().StartsWith("<p>", StringComparison.InvariantCultureIgnoreCase)) { html.Append(this.Text); } else { html.Append($@"<p>{this.Text}</p>"); } html.Append("</div>"); html.Append("</div>"); #else using (var htmlWriter = new HtmlTextWriter(new System.IO.StringWriter(html), "")) { htmlWriter.NewLine = string.Empty; htmlWriter.AddAttribute(CanvasControlAttribute, this.CanvasControlData); htmlWriter.AddAttribute(CanvasDataVersionAttribute, this.CanvasDataVersion); htmlWriter.AddAttribute(ControlDataAttribute, this.JsonControlData); htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div); htmlWriter.AddAttribute(TextRteAttribute, this.Rte); htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div); // Don't wrap in Paragraph if the text already is wrapped in a paragraph if (this.Text.Trim().StartsWith("<p>", StringComparison.InvariantCultureIgnoreCase)) { htmlWriter.Write(this.Text); } else { htmlWriter.RenderBeginTag(HtmlTextWriterTag.P); htmlWriter.Write(this.Text); htmlWriter.RenderEndTag(); } htmlWriter.RenderEndTag(); htmlWriter.RenderEndTag(); } #endif return(html.ToString()); }