private void btnOK_Click(object sender, EventArgs e) { if (this.SourceEventArgs != null && this.SourceEventArgs.Element is DomImageElement) { DomDocument document = this.SourceEventArgs.Document; DomImageElement element = (DomImageElement)this.SourceEventArgs.Element; element.OwnerDocument = document; bool logUndo = this.SourceEventArgs.LogUndo && document.CanLogUndo; bool modified = false; string txt = txtID.Text.Trim(); if (txt.Length == 0) { txt = null; } if (txt != element.ID) { if (logUndo) { document.UndoList.AddProperty("ID", element.ID, txt, element); } element.ID = txt; modified = true; } txt = txtTitle.Text.Trim(); if (txt.Length == 0) { txt = null; } if (txt != element.Title) { if (logUndo) { document.UndoList.AddProperty("Title", element.Title, txt, element); } element.Title = txt; modified = true; } txt = txtAlt.Text.Trim(); if (txt.Length == 0) { txt = null; } if (txt != element.Alt) { if (logUndo) { document.UndoList.AddProperty("Alt", element.Alt, txt, element); } element.Alt = txt; modified = true; } txt = txtSource.Text.Trim(); if (txt.Length == 0) { txt = null; } if (txt != element.Source) { if (logUndo) { document.UndoList.AddProperty("Source", element.Source, txt, element); } element.Source = txt; modified = true; element.UpdateImageContent(); } if (chkKeepWidthHeightRate.Checked != element.KeepWidthHeightRate) { if (logUndo) { document.UndoList.AddProperty( "KeepWidthHeightRate", element.KeepWidthHeightRate, chkKeepWidthHeightRate.Checked, element); } element.KeepWidthHeightRate = chkKeepWidthHeightRate.Checked; modified = true; } if (chkSaveContentInFile.Checked != element.SaveContentInFile) { if (logUndo) { document.UndoList.AddProperty( "SaveContentInFile", element.SaveContentInFile, chkSaveContentInFile.Checked, element); } element.SaveContentInFile = chkSaveContentInFile.Checked; modified = true; } if (picImage.Tag != element.Image) { XImageValue img = (XImageValue)picImage.Tag; if (img == null) { img = new XImageValue(); } if (img.HasContent) { if (logUndo) { document.UndoList.AddProperty( "Image", element.Image, img, element); } element.Image = img; modified = true; } } if (modified) { SizeF oldSize = new SizeF(element.Width, element.Height); element.UpdateSize(); SizeF newSize = new SizeF(element.Width, element.Height); if (logUndo) { document.UndoList.AddProperty("Width", oldSize.Width, element.Width, element); document.UndoList.AddProperty("Height", oldSize.Height, element.Height, element); } } if (this.SourceEventArgs.Method == ElementEditMethod.Edit) { if (modified) { element.UpdateContentVersion(); DomContentElement ce = element.ContentElement; ce.SetLinesInvalidateState( element.OwnerLine, element.OwnerLine); ce.UpdateContentElements(true); element.SizeInvalid = true; ce.RefreshPrivateContent(element.ViewIndex); ContentChangedEventArgs args = new ContentChangedEventArgs(); args.Document = element.OwnerDocument; args.Element = element; args.LoadingDocument = false; element.Parent.RaiseBubbleOnContentChanged(args); } } else { this.DialogResult = System.Windows.Forms.DialogResult.OK; } this.Close(); } }
private void CreateElements( HTMLElement rootHtmlElement, DomElement rootDomElement, DocumentContentStyle currentStyle) { if (rootHtmlElement.ChildNodes == null) { return; } foreach (HTMLElement element in rootHtmlElement.ChildNodes) { if (element is HTMLCommentElement) { // 忽略注释 continue; } string tagName = element.FixTagName; switch (tagName) { case "div": { CreateElements(element, rootDomElement, currentStyle); DomElement p = this.DomDocument.CreateElement(typeof(DomParagraphFlagElement)); p.StyleIndex = this.DomDocument.ContentStyles.GetStyleIndex(currentStyle); rootDomElement.Elements.Add(p); } break; case "ul": // 原点式列表 case "ol": // 数值式列表 foreach (HTMLElement li in element.ChildNodes) { if (li.FixTagName == "li") { CreateElements(li, rootDomElement, currentStyle); DomParagraphFlagElement flag = (DomParagraphFlagElement)this.DomDocument.CreateElement(typeof(DomParagraphFlagElement)); DocumentContentStyle s = (DocumentContentStyle)currentStyle.Clone(); if (tagName == "ul") { s.BulletedList = true; } else { s.NumberedList = true; } flag.StyleIndex = this.DomDocument.ContentStyles.GetStyleIndex(s); rootDomElement.Elements.Add(flag); } //if } //foreach break; case "pre": { // 预览文本 DocumentContentStyle s = CreateStyle(element, currentStyle); DomElementList list = this.DomDocument.CreateTextElements(element.InnerText, null, s); rootDomElement.Elements.AddRange(list); DomElement p = this.DomDocument.CreateElement(typeof(DomParagraphFlagElement)); p.StyleIndex = this.DomDocument.ContentStyles.GetStyleIndex(currentStyle); rootDomElement.Elements.Add(p); break; } case "img": { // 图片 DocumentContentStyle s = CreateStyle(element, currentStyle); s.BorderColor = Color.Black; s.BorderLeft = true; s.BorderTop = true; s.BorderRight = true; s.BorderBottom = true; s.BorderWidth = 0; if (element.HasAttribute("border")) { s.BorderWidth = ToInt32(element.GetAttribute("border")); } DomImageElement img = (DomImageElement)this.DomDocument.CreateElement(typeof(DomImageElement)); img.StyleIndex = this.DomDocument.ContentStyles.GetStyleIndex(s); if (element.HasAttribute("width")) { img.Width = (float )ToLength(element.GetAttribute("width")); } if (element.HasAttribute("height")) { img.Height = ( float )ToLength(element.GetAttribute("height")); } if (element.HasAttribute("id")) { img.ID = element.GetAttribute("id"); } if (element.HasAttribute("alt")) { img.Alt = element.GetAttribute("alt"); } if (element.HasAttribute("title")) { img.Title = element.GetAttribute("title"); } if (element.HasAttribute("src")) { XImageValue v = new XImageValue(); string url = this.HtmlDocument.GetAbsoluteURL(element.GetAttribute("src")); try { string msg = string.Format(WriterStrings.Downloading_URL, url); if (this.DomDocument.EditorControl != null) { this.DomDocument.EditorControl.SetStatusText(msg); } if (this.DomDocument.Options.BehaviorOptions.DebugMode) { System.Diagnostics.Debug.Write(msg); } int len = v.Load(url); if (this.DomDocument.Options.BehaviorOptions.DebugMode) { System.Diagnostics.Debug.WriteLine(WriterUtils.FormatByteSize(len)); } } catch (Exception ext) { img.Alt = url + ":" + ext.Message; if (this.DomDocument.Options.BehaviorOptions.DebugMode) { System.Diagnostics.Debug.WriteLine(WriterStrings.Fail); } } if (this.DomDocument.EditorControl != null) { this.DomDocument.EditorControl.SetStatusText(null); } if (v.HasContent == false) { img.Alt = url; if (img.Width == 0) { img.Width = 300; } if (img.Height == 0) { img.Height = 150; } } img.Image = v; } if (img.Width == 0 || img.Height == 0) { img.UpdateSize(); } rootDomElement.Elements.Add(img); } break; case "#text": { // 纯文本片段 string text = DCSoft.Common.StringFormatHelper.NormalizeSpace(element.Text); text = System.Web.HttpUtility.HtmlDecode(text); if (string.IsNullOrEmpty(text) == false) { DomElementList cs = this._DomDocument.CreateTextElements( text, currentStyle, currentStyle); if (cs != null && cs.Count > 0) { rootDomElement.Elements.AddRange(cs); } } } break; case "p": { // 段落 DocumentContentStyle ps = CreateStyle(element, currentStyle); CreateElements(element, rootDomElement, ps); DomParagraphFlagElement flag = ( DomParagraphFlagElement )_DomDocument.CreateElement(typeof(DomParagraphFlagElement)); flag.StyleIndex = _DomDocument.ContentStyles.GetStyleIndex(ps); rootDomElement.Elements.Add(flag); } break; case "br": { // 软回车 DomLineBreakElement lb = (DomLineBreakElement)_DomDocument.CreateElement(typeof(DomLineBreakElement)); rootDomElement.Elements.Add(lb); } break; case "sup": { // 上标 DocumentContentStyle ss = ( DocumentContentStyle )currentStyle.Clone(); ss.Superscript = true; CreateElements(element, rootDomElement, ss); } break; case "sub": { // 下标 DocumentContentStyle ss = (DocumentContentStyle)currentStyle.Clone(); ss.Subscript = true; CreateElements(element, rootDomElement, ss); } break; case "strong": case "b": { // 粗体 DocumentContentStyle ss = (DocumentContentStyle)currentStyle.Clone(); ss.Bold = true; CreateElements(element, rootDomElement, ss); } break; case "i": { // 斜体 DocumentContentStyle ss = (DocumentContentStyle)currentStyle.Clone(); ss.Italic = true; CreateElements(element, rootDomElement, ss); } break; case "strike": { // 删除线 DocumentContentStyle ss = (DocumentContentStyle)currentStyle.Clone(); ss.Strikeout = true; CreateElements(element, rootDomElement, ss); } break; case "a": { // 超链接 DocumentContentStyle ss = (DocumentContentStyle)currentStyle.Clone(); ss.Color = Color.Blue; CreateElements(element, rootDomElement, ss); } break; case "font": { // 字体 DocumentContentStyle ss = CreateStyle(element, currentStyle); if (element.HasAttribute("color")) { // 文字颜色 ss.Color = ToColor(element.GetAttribute("color"), Color.Black); } if (element.HasAttribute("face")) { // 字体名称 ss.FontName = GetFontName(element.GetAttribute("face")); } if (element.HasAttribute("size")) { // 文字大小 int size = ToInt32(element.GetAttribute("size")); switch (size) { case 1: ss.FontSize = 7; break; case 2: ss.FontSize = 10; break; case 3: ss.FontSize = 12; break; case 4: ss.FontSize = 14; break; case 5: ss.FontSize = 18; break; case 6: ss.FontSize = 24; break; case 7: ss.FontSize = 35; break; } } CreateElements(element, rootDomElement, ss); } break; case "h1": case "h2": case "h3": case "h4": case "h5": case "h6": { // 标题 rootDomElement.Elements.Add(this.DomDocument.CreateElement(typeof(DomParagraphFlagElement))); float fz = 9; switch (tagName) { case "h1": fz = 24; break; case "h2": fz = 18; break; case "h3": fz = 13; break; case "h4": fz = 12; break; case "h5": fz = 10; break; case "h6": fz = 8; break; } DocumentContentStyle ss = CreateStyle(element, currentStyle); if (XDependencyObject.HasPropertyValue(ss, "FontSize")) { ss.FontSize = fz; } CreateElements(element, rootDomElement, ss); rootDomElement.Elements.Add(this.DomDocument.CreateElement(typeof(DomParagraphFlagElement))); } break; default: { DocumentContentStyle ds = CreateStyle(element, (DocumentContentStyle)currentStyle); CreateElements(element, rootDomElement, ds); } break; } //switch } //foreach }