public Element CreateImageSnippet(Element after, Resource res) { int newOrderNr = after.OrderNr + 1; ImageSnippet ns; // assume this is an image! using (var ms = new MemoryStream(res.Content)) { var img = System.Drawing.Image.FromStream(ms); using (var imgMs = new MemoryStream()) { img.Save(imgMs, ImageFormat.Png); ns = new ImageSnippet { Name = res.Name, // remember the Name (will become the title once the user overwrites this Content = imgMs.ToArray(), // copy of image Properties = System.Web.Helpers.Json.Encode(new ImageProperties { OriginalWidth = img.Width, OriginalHeight = img.Height, ImageWidth = img.Width, ImageHeight = img.Height }), MimeType = res.MimeType, OrderNr = newOrderNr }; } } ns.Parent = GetSection(after); this.ReorderLeafElementsAfterElement(ns, newOrderNr); return(ns); }
protected override void Seed(EditorContext context) { base.Seed(context); #region Author Portal Console.WriteLine("DemoContent"); #region create a sample Func <IEnumerable <XElement>, List <Element> > helper = null; var currentChapter = String.Empty; var chapterOrder = 1; Document opus = null; helper = nodes => { var ret = new List <Element>(); int orderNr = 1; foreach (var elm in nodes) { Element newElm = null; # region Detect Element Type switch (elm.Attribute("Type").Value.ToLower()) { case "opus": # region OPUS // create opus newElm = new Document { Name = elm.Attribute("Name").Value }; opus = (Document)newElm; // this is an easy way to implement a workflow break; # endregion case "section": # region SECTION if (elm.FirstNode != null && elm.FirstNode.NodeType == System.Xml.XmlNodeType.Text) { newElm = new Section { Content = System.Text.Encoding.UTF8.GetBytes(((XText)elm.FirstNode).Value.Trim()) }; } else { newElm = new Section { Content = System.Text.Encoding.UTF8.GetBytes("Empty Section") }; } if (elm.Attribute("Name") == null || String.IsNullOrEmpty(elm.Attribute("Name").Value)) { newElm.Name = System.Text.Encoding.UTF8.GetString(newElm.Content); } else { newElm.Name = elm.Attribute("Name").Value; if (elm.FirstNode == null) { newElm.Content = System.Text.Encoding.UTF8.GetBytes(elm.Attribute("Name").Value); } } // Detect Chapter Elements to store resources in subfolders if (elm.Parent.Attribute("Type").Value == "Opus") { currentChapter = elm.Attribute("Name").Value; } break; # endregion case "text": # region TEXT newElm = new TextSnippet { Content = System.Text.Encoding.UTF8.GetBytes(elm.GetInnerXml()), Name = elm.Value.CleanUpString(15) }; break; # endregion case "image": # region IMAGE var imgpath = elm.Value.Trim(); Debug.Assert(Path.GetExtension(imgpath).Length > 0, "no extension " + imgpath); // get and optionally create folder after chapter // var res = new Resource { Name = elm.Attribute("Name").Value, OwnerDocument = opus, TypesOfResource = TypeOfResource.Content, MimeType = "image/" + Path.GetExtension(imgpath).Substring(1) // kick the leading "." }; System.Drawing.Image img = null; var localPath = Path.Combine(Path.GetDirectoryName(GetType().Assembly.Location), "DemoContent", imgpath); if (File.Exists(localPath)) { var bytes = File.ReadAllBytes(localPath); res.Content = bytes; } context.Resources.Add(res); newElm = new ImageSnippet { Content = res.Content, Name = res.Name, Title = res.Name, MimeType = res.MimeType }; var imgprops = new ImageProperties(); if (elm.Attribute("Width") == null || elm.Attribute("Height") == null) { if (img != null) { imgprops.ImageWidth = imgprops.OriginalWidth = img.Width; imgprops.ImageHeight = imgprops.OriginalHeight = img.Height; } else { imgprops.ImageWidth = imgprops.OriginalWidth = 100; imgprops.ImageHeight = imgprops.OriginalHeight = 100; } } else { imgprops.ImageWidth = imgprops.OriginalWidth = Convert.ToInt32(elm.Attribute("Width").GetNullSafeValue()); imgprops.ImageHeight = imgprops.OriginalHeight = Convert.ToInt32(elm.Attribute("Height").GetNullSafeValue()); } ((ImageSnippet)newElm).Properties = new JavaScriptSerializer().Serialize(imgprops); break; # endregion case "listing": # region LISTING newElm = new ListingSnippet { Content = System.Text.UTF8Encoding.UTF8.GetBytes(elm.Value.Trim()), //.Replace("\n", " "); - Causes problems with Listing widget. All data is displayed in one line Name = elm.Attribute("Name") == null ? "Listing" : elm.Attribute("Name").Value, Title = elm.Attribute("Name") == null ? "Listing" : elm.Attribute("Name").Value, Language = elm.Attribute("Language") == null ? "" : elm.Attribute("Language").Value, SyntaxHighlight = elm.Attribute("Highlight") == null ? true : Boolean.Parse(elm.Attribute("Highlight").Value), LineNumbers = elm.Attribute("LineNumbers") == null ? true : Boolean.Parse(elm.Attribute("LineNumbers").Value) }; # endregion break;
public dynamic InsertSnippet(int documentId, int chapterId, int id, string type, string variation, string data) { var level = 0; var opus = ProjectManager.Instance.GetOpus(documentId, HttpContext.Current.User.Identity.Name); var chapter = EditorManager.Instance.GetElement(chapterId) as Snippet; var currentElement = EditorManager.Instance.GetElement(id > 0 ? id : chapterId); JsonBehavior json; var changeElements = new List <JsonBehavior>(); Snippet ns; switch (type.ToLowerInvariant()) { case "chapter": ns = EditorManager.Instance.CreateChapterSnippet(documentId, currentElement); json = ns.GetType().GetCustomAttributes(typeof(EditorServiceWrapperAttribute), true).OfType <EditorServiceWrapperAttribute>().Single().GetJson(opus, chapter, ns as Snippet); break; case "section": level = 3; // opus == 1, chapter == 2 goto case "insertsection"; case "subsection": level = 4; goto case "insertsection"; case "subsubsection": level = 5; goto case "insertsection"; case "subsubsubsection": level = 6; goto case "insertsection"; case "insertsection": ns = EditorManager.Instance.CreateSectionSnippet(documentId, currentElement, level); //json = (EditorServiceWrapper)Attribute.("Section", typeof(EditorServiceWrapper)); //typeof(EditorServiceWrapper).GetType().GetCustomAttributes(typeof(EditorServiceWrapper), true).OfType.Where(e=>e. //var snippet = ns as Snippet; json = (ns).GetType().GetCustomAttributes(typeof(EditorServiceWrapperAttribute), true).OfType <EditorServiceWrapperAttribute>().Single().GetJson(opus, chapter, ns); changeElements = EditorManager.Instance.ConvertElementToJsonSnippet(opus, chapter, EditorManager.Instance.GetSectionsAfterLevel(ns, level)); break; case "text": ns = EditorManager.Instance.CreateTextSnippet(documentId, currentElement); json = (ns).GetType().GetCustomAttributes(typeof(EditorServiceWrapperAttribute), true).OfType <EditorServiceWrapperAttribute>().First().GetJson(opus, chapter, ns); break; case "img": # region Image var resId = Convert.ToInt32(data); // resId is from resources, we need to pull this from blob storage first and save to element if (resId != 0) { // 1. Retrieve Res, 2. read blob, 3. put in content (hard copy of everything) var res = ProjectManager.Instance.GetResource(resId) as ResourceFile; if (res != null) { ns = EditorManager.Instance.CreateImageSnippet(documentId, currentElement, res); json = (ns).GetType().GetCustomAttributes(typeof(EditorServiceWrapperAttribute), true).OfType <EditorServiceWrapperAttribute>().First().GetJson(opus, chapter, ns); } else { ns = new ImageSnippet(); json = null; } } else { ns = new ImageSnippet(); json = null; } # endregion break;