Esempio n. 1
0
        protected override void ImportData(XmlNode fieldNode, ImportContext context)
        {
            if (string.IsNullOrEmpty(fieldNode.InnerXml))
            {
                this.SetData(null);
                return;
            }

            var data          = new RichTextFieldValue();
            var childElements = fieldNode.SelectNodes("*");

            if (childElements != null)
            {
                // Backward compatibility: if there is no any child element, the node's text is the Text of the RichText.
                // Note: whitespace only or empty element interpreted as null.
                if (childElements.Count == 0)
                {
                    data.Text = fieldNode.InnerText;
                }
                else
                {
                    foreach (XmlElement childElement in childElements)
                    {
                        switch (childElement.LocalName)
                        {
                        case "Text": data.Text = childElement.InnerText; break;

                        case "Editor": data.Editor = childElement.InnerText; break;
                        }
                    }
                }
            }

            this.SetData(data);
        }
Esempio n. 2
0
        protected override object ConvertTo(object[] handlerValues)
        {
            RichTextFieldValue data = null;

            Type propertyType = this.GetHandlerSlot(0);

            if (propertyType == typeof(string))
            {
                var src = (string)handlerValues[0];
                if (string.IsNullOrEmpty(src))
                {
                    return(null);
                }
                try
                {
                    data = JsonConvert.DeserializeObject <RichTextFieldValue>(src);
                }
                catch (Exception e) // rethrow
                {
                    data = new RichTextFieldValue {
                        Text = src
                    };
                }
            }
            else if (propertyType == typeof(RichTextFieldValue))
            {
                data = (RichTextFieldValue)handlerValues[0];
            }

            return(data);
        }
Esempio n. 3
0
        protected override object[] ConvertFrom(object value)
        {
            // Accepts: string, JObject, RichTextFieldValue
            object converted    = null;
            Type   propertyType = this.GetHandlerSlot(0);

            if (propertyType == typeof(string))
            {
                if (value is string stringValue)
                {
                    converted = EncodeTransferData(new RichTextFieldValue {
                        Text = stringValue
                    });
                }
                if (value is JObject jObject)
                {
                    var src = jObject.ToString();
                    // check
                    var data = JsonConvert.DeserializeObject <RichTextFieldValue>(src);
                    // convert
                    converted = src;
                }
                else if (value is RichTextFieldValue rtf)
                {
                    converted = EncodeTransferData(rtf);
                }
            }
            else if (propertyType == typeof(RichTextFieldValue))
            {
                if (value is string stringValue)
                {
                    converted = new RichTextFieldValue {
                        Text = stringValue
                    };
                }
                if (value is JObject jObject)
                {
                    // check
                    converted = JsonConvert.DeserializeObject <RichTextFieldValue>(jObject.ToString());
                }
                else if (value is RichTextFieldValue rtf)
                {
                    converted = rtf;
                }
            }

            return(new object[] { converted });
        }
Esempio n. 4
0
 private string EncodeTransferData(RichTextFieldValue data)
 {
     return(JsonConvert.SerializeObject(data));
 }