Umbraco-保存覆盖时从内容中删除宏

Umbraco-保存覆盖时从内容中删除宏,umbraco,umbraco7,umbraco-mvc,umbraco-macros,Umbraco,Umbraco7,Umbraco Mvc,Umbraco Macros,我有一个方法覆盖Umbraco.Core.Services.ContentService.Saving。它只是向图像添加一些类,并将图像包装在一个div中 但是现在我开始在富文本编辑器中添加一些宏,这些宏保存后就会消失 经过多次挖掘,我发现如果我重写保存方法,宏将如何删除 如果有其他人必须处理这件事,我希望对正在发生的事情有一些见解 以下是我如何覆盖它: Umbraco.Core.Services.ContentService.Saving += OverrideSave.ContentServ

我有一个方法覆盖Umbraco.Core.Services.ContentService.Saving。它只是向图像添加一些类,并将图像包装在一个div中

但是现在我开始在富文本编辑器中添加一些宏,这些宏保存后就会消失

经过多次挖掘,我发现如果我重写保存方法,宏将如何删除

如果有其他人必须处理这件事,我希望对正在发生的事情有一些见解

以下是我如何覆盖它:

Umbraco.Core.Services.ContentService.Saving += OverrideSave.ContentService_Saving;
这是我的方法(减去中间无聊的部分)

公共类重写Save
{
公共静态无效内容服务\u保存(Umbraco.Core.Services.IContentService发送方,Umbraco.Core.Events.SaveEventArgs e)
{
foreach(e.SavedEntities中的变量c)
{
var list=c.PropertyTypes.Where(x=>x.propertyEditorias==“Umbraco.TinyMCEv3”).ToList();
如果(list.Count>0)
{
List propList=新列表();
foreach(列表中的变量i)
{
Add(c.Properties.Where(x=>x.Alias==i.Alias.FirstOrDefault());
}
如果(propList.Count>0)
{
foreach(propList中的变量t)
{
//字符串html=t.Value.ToString();
//字符串outputHtml=html;
//HtmlAgilityPack.HtmlDocument doc=新的HtmlAgilityPack.HtmlDocument();
//doc.LoadHtml((字符串)t.Value);
var parser=newhtmlparser();
var doc=parser.Parse((字符串)t.Value);
//if(doc.DocumentNode.SelectNodes(“//img/@src”)!=null)
if(doc.DocumentElement.GetElementsByTagName(“img”)!=null)
{
对于(int i=0;i”;
}
其他的
{
wrapperNode.InnerHtml=doc.DocumentElement.QuerySelectorAll(“img”)[i].OuterHtml;
}
//将图像宽度添加到包装器div
如果(!string.IsNullOrEmpty(doc.DocumentElement.QuerySelectorAll(“img”)[i].Attributes[“style”].Value))
{
字符串样式=doc.DocumentElement.QuerySelectorAll(“img”)[i]。属性[“样式”]。值;
字符串模式=@“(宽度:\s**?;)”;
字符串宽度=Regex.Match(样式、模式、RegexOptions.IgnoreCase)。组[1]。值;
如果(!string.IsNullOrEmpty(宽度))
{
SetAttribute(“样式”,宽度);
}
}
//向div包装器添加适当的类
if(doc.DocumentElement.QuerySelectorAll(“img”)[i].Attributes[“style”].Value.Contains(“float:left;”)
{
SetAttribute(“类”,“标题左对齐的图像”);
}
else if(doc.DocumentElement.QuerySelectorAll(“img”)[i].Attributes[“style”].Value.Contains(“float:right;”)
{
SetAttribute(“类”,“标题向右对齐的图像”);
}
其他的
{
SetAttribute(“类”,“带标题的图像”);
}
//将新节点添加到html-检查以确保div没有加倍
if(doc.DocumentElement.QuerySelectorAll(“img”)[i].ParentElement.TagName==“div”
&&!string.IsNullOrEmpty(doc.DocumentElement.QuerySelectorAll(“img”)[i].ParentElement.GetAttribute(“class”)//检查以确保类属性存在,以便下一部分不会失败
&&doc.DocumentElement.QuerySelectorAll(“img”)[i].ParentElement.Attributes[“class”].Value.Contains(“带标题的图像”)//检查父节点是否与我们想要的节点正确。
{
doc.DocumentElement.QuerySelectorAll(“img”)[i].ParentElement.ParentElement.ReplaceChild(wrapperNode,doc.DocumentElement.QuerySelectorAll(“img”)[i].ParentElement);
}
public class OverrideSave
{
    public static void ContentService_Saving(Umbraco.Core.Services.IContentService sender, Umbraco.Core.Events.SaveEventArgs<Umbraco.Core.Models.IContent> e)
    {
        foreach (var c in e.SavedEntities)
        {
            var list = c.PropertyTypes.Where(x => x.PropertyEditorAlias == "Umbraco.TinyMCEv3").ToList();

            if (list.Count > 0)
            {
                List<Property> propList = new List<Property>();

                foreach (var i in list)
                {
                    propList.Add(c.Properties.Where(x => x.Alias == i.Alias).FirstOrDefault());
                }

                if (propList.Count > 0)
                {
                    foreach (var t in propList)
                    {
                        //string html = t.Value.ToString();
                        //string outputHtml = html;

                        //HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                        //doc.LoadHtml((string)t.Value);
                        var parser = new HtmlParser();
                        var doc = parser.Parse((string)t.Value);

                        //if (doc.DocumentNode.SelectNodes("//img/@src") != null)
                        if (doc.DocumentElement.GetElementsByTagName("img") != null)
                        {
                            for (int i = 0; i < doc.DocumentElement.QuerySelectorAll("img").Count(); i++)
                            {
                                //add S3  URL to images
                                string s3Url = ConfigurationManager.AppSettings["cdnDomain"];
                                if (!doc.DocumentElement.QuerySelectorAll("img")[i].Attributes["src"].Value.Contains(s3Url))
                                {
                                    doc.DocumentElement.QuerySelectorAll("img")[i].SetAttribute("src", s3Url + doc.DocumentElement.QuerySelectorAll("img")[i].Attributes["src"].Value);
                                }

                                var wrapperNode = doc.CreateElement("div");

                                //add description paragraph
                                if (!string.IsNullOrEmpty(doc.DocumentElement.QuerySelectorAll("img")[i].Attributes["alt"].Value))
                                {
                                    wrapperNode.InnerHtml = doc.DocumentElement.QuerySelectorAll("img")[i].OuterHtml + "<p>" + doc.DocumentElement.QuerySelectorAll("img")[i].Attributes["alt"].Value + "</p>";
                                }
                                else
                                {
                                    wrapperNode.InnerHtml = doc.DocumentElement.QuerySelectorAll("img")[i].OuterHtml;
                                }

                                //add image width to wrapper div
                                if (!string.IsNullOrEmpty(doc.DocumentElement.QuerySelectorAll("img")[i].Attributes["style"].Value))
                                {
                                    string style = doc.DocumentElement.QuerySelectorAll("img")[i].Attributes["style"].Value;
                                    string pattern = @"(width:\s*.*?;)";
                                    string width = Regex.Match(style, pattern, RegexOptions.IgnoreCase).Groups[1].Value;
                                    if (!string.IsNullOrEmpty(width))
                                    {
                                        wrapperNode.SetAttribute("style", width);
                                    }
                                }

                                //add appropriate classes to div wrapper
                                if (doc.DocumentElement.QuerySelectorAll("img")[i].Attributes["style"].Value.Contains("float: left;"))
                                {
                                    wrapperNode.SetAttribute("class", "image-with-caption align-left");
                                }
                                else if (doc.DocumentElement.QuerySelectorAll("img")[i].Attributes["style"].Value.Contains("float: right;"))
                                {
                                    wrapperNode.SetAttribute("class", "image-with-caption align-right");
                                }
                                else
                                {
                                    wrapperNode.SetAttribute("class", "image-with-caption");
                                }

                                //add new node to html - check to make sure divs are not doubled
                                if (doc.DocumentElement.QuerySelectorAll("img")[i].ParentElement.TagName == "div"
                                    && !string.IsNullOrEmpty(doc.DocumentElement.QuerySelectorAll("img")[i].ParentElement.GetAttribute("class")) // Check to make sure a class attribute exists so the next part doesn't fail
                                    && doc.DocumentElement.QuerySelectorAll("img")[i].ParentElement.Attributes["class"].Value.Contains("image-with-caption")) // Check to see if the parent node is infact the one we want.
                                {
                                    doc.DocumentElement.QuerySelectorAll("img")[i].ParentElement.ParentElement.ReplaceChild(wrapperNode, doc.DocumentElement.QuerySelectorAll("img")[i].ParentElement);
                                }
                                else
                                {
                                    doc.DocumentElement.QuerySelectorAll("img")[i].ParentElement.ReplaceChild(wrapperNode, doc.DocumentElement.QuerySelectorAll("img")[i]);
                                }
                            }                                
                        }
                        t.Value = (object)doc.DocumentElement.OuterHtml;
                    }
                }
            }
        }
    }
}