Пример #1
0
        public void UpdateImageLinkOptions(string title, string rel, bool newWindow)
        {
            IHTMLElement anchorElement = GetAnchorElement();

            IHTMLAnchorElement htmlAnchorElement = (anchorElement as IHTMLAnchorElement);

            if (htmlAnchorElement != null)
            {
                //set the default target attribute for the new element
                string target = newWindow ? "_blank" : null;
                if (htmlAnchorElement.target != target) //don't set the target to null if its already null (avoids adding empty target attr)
                {
                    htmlAnchorElement.target = target;
                }
                if (title != String.Empty)
                {
                    anchorElement.setAttribute("title", title, 0);
                }
                else
                {
                    anchorElement.removeAttribute("title", 0);
                }
                if (rel != String.Empty)
                {
                    anchorElement.setAttribute("rel", rel, 0);
                }
                else
                {
                    anchorElement.removeAttribute("rel", 0);
                }
            }
        }
Пример #2
0
        public static ImagePropertiesInfo GetImagePropertiesInfo(IHTMLImgElement imgElement, IBlogPostImageEditingContext editorContext)
        {
            IHTMLElement      imgHtmlElement = (IHTMLElement)imgElement;
            string            imgSrc         = imgHtmlElement.getAttribute("src", 2) as string;
            BlogPostImageData imageData      = null;

            try
            {
                imageData = BlogPostImageDataList.LookupImageDataByInlineUri(editorContext.ImageList, new Uri(imgSrc));
            }
            catch (UriFormatException)
            {
                //this URI is probably relative web URL, so extract the image src letting the
                //DOM fill in the full URL for us based on the base URL.
                imgSrc = imgHtmlElement.getAttribute("src", 0) as string;
            }

            ImagePropertiesInfo info;

            if (imageData != null && imageData.GetImageSourceFile() != null)
            {
                //clone the image data to the sidebar doesn't change it (required for preserving image undo/redo state)
                imageData = (BlogPostImageData)imageData.Clone();
                //this is an attached local image
                info            = new BlogPostImagePropertiesInfo(imageData, new ImageDecoratorsList(editorContext.DecoratorsManager, imageData.ImageDecoratorSettings));
                info.ImgElement = imgHtmlElement;
            }
            else
            {
                //this is not an attached local image, so treat as a web image
                ImageDecoratorsList remoteImageDecoratorsList = new ImageDecoratorsList(editorContext.DecoratorsManager, new BlogPostSettingsBag());
                remoteImageDecoratorsList.AddDecorator(editorContext.DecoratorsManager.GetDefaultRemoteImageDecorators());

                //The source image size is unknown, so calculate the actual image size by removing
                //the size attributes, checking the size, and then placing the size attributes back
                string oldHeight = imgHtmlElement.getAttribute("height", 2) as string;
                string oldWidth  = imgHtmlElement.getAttribute("width", 2) as string;
                imgHtmlElement.removeAttribute("width", 0);
                imgHtmlElement.removeAttribute("height", 0);
                int width  = imgElement.width;
                int height = imgElement.height;

                if (!String.IsNullOrEmpty(oldHeight))
                {
                    imgHtmlElement.setAttribute("height", oldHeight, 0);
                }
                if (!String.IsNullOrEmpty(oldWidth))
                {
                    imgHtmlElement.setAttribute("width", oldWidth, 0);
                }

                info            = new ImagePropertiesInfo(new Uri(imgSrc), new Size(width, height), remoteImageDecoratorsList);
                info.ImgElement = imgHtmlElement;

                // Sets the correct inline image size and image size name for the remote image.
                if (!String.IsNullOrEmpty(oldWidth) && !String.IsNullOrEmpty(oldHeight))
                {
                    int inlineWidth, inlineHeight;
                    if (Int32.TryParse(oldWidth, NumberStyles.Integer, CultureInfo.InvariantCulture, out inlineWidth) &&
                        Int32.TryParse(oldHeight, NumberStyles.Integer, CultureInfo.InvariantCulture, out inlineHeight))
                    {
                        info.InlineImageSize = new Size(inlineWidth, inlineHeight);
                    }
                }

                // Sets the correct border style for the remote image.
                if (new HtmlBorderDecoratorSettings(imgHtmlElement).InheritBorder)
                {
                    if (!info.ImageDecorators.ContainsDecorator(HtmlBorderDecorator.Id))
                    {
                        info.ImageDecorators.AddDecorator(HtmlBorderDecorator.Id);
                    }
                }
                else if (new NoBorderDecoratorSettings(imgHtmlElement).NoBorder)
                {
                    if (!info.ImageDecorators.ContainsDecorator(NoBorderDecorator.Id))
                    {
                        info.ImageDecorators.AddDecorator(NoBorderDecorator.Id);
                    }
                }
            }

            //transfer image data properties
            if (imageData != null)
            {
                info.UploadSettings  = imageData.UploadInfo.Settings;
                info.UploadServiceId = imageData.UploadInfo.ImageServiceId;
                if (info.UploadServiceId == null)
                {
                    info.UploadServiceId = editorContext.ImageServiceId;
                }
            }

            return(info);
        }
        private void htmlEditor_SnapRectEvent(IHTMLElement pIElement, ref RECT prcNEW, _ELEMENT_CORNER elementCorner)
        {
            IHTMLImgElement imgElement = pIElement as IHTMLImgElement;
            if (imgElement != null)
            {
                // see if we are scaling a new image
                if (snapRectImageElement == null || snapRectImageElement.sourceIndex != snapRectImageElement.sourceIndex)
                {
                    // save the image element so that mouseUp handler can regenerate the resized image.
                    snapRectImageElement = pIElement;

                    //save the initial image size so that we can scale the image based on the original size.
                    //Note: this is important to minimize rounding errors that may occurs while the image is snapping
                    //the old logic scaled based on the imgElement height and width attributes, so each snap iteration would
                    //case the image to distort more and more.
                    snapRectInitialImageSize = new Size(imgElement.width, imgElement.height);
                }

                // only preserve constraints if we are sizing a true corner
                if (IsTrueCorner(elementCorner))
                {
                    snapRectPreserveConstraints = true;
                    // get the new width and height
                    int width = prcNEW.right - prcNEW.left;
                    int height = prcNEW.bottom - prcNEW.top;

                    // scale the picture (using its current proportions!) based on the size of the snap rectangle.
                    Size constrainedSize = ImageUtils.GetScaledImageSize(width, height, snapRectInitialImageSize);
                    width = constrainedSize.Width;
                    height = constrainedSize.Height;

                    // adjust the location and size of the image
                    switch (elementCorner)
                    {
                        case _ELEMENT_CORNER.ELEMENT_CORNER_BOTTOMLEFT:
                            prcNEW.left = prcNEW.right - width;
                            prcNEW.bottom = prcNEW.top + height;
                            break;
                        case _ELEMENT_CORNER.ELEMENT_CORNER_BOTTOMRIGHT:
                            prcNEW.right = prcNEW.left + width;
                            prcNEW.bottom = prcNEW.top + height;
                            break;
                        case _ELEMENT_CORNER.ELEMENT_CORNER_TOPLEFT:
                            prcNEW.left = prcNEW.right - width;
                            prcNEW.top = prcNEW.bottom - height;
                            break;
                        case _ELEMENT_CORNER.ELEMENT_CORNER_TOPRIGHT:
                            prcNEW.right = prcNEW.left + width;
                            prcNEW.top = prcNEW.bottom - height;
                            break;
                        default:
                            Trace.Fail("Unexpected element corner: " + elementCorner.ToString());
                            break;
                    }
                }

                //Hack: unset the width and height attributes so that the MSHTMLEditor doesn't set the height/width
                //using the style attribute when it handles the snapRect.  Once its set using style, it can never be unset...
                pIElement.removeAttribute("width", 0);
                pIElement.removeAttribute("height", 0);

            }

            //suppress resizing of the the extended entry splitter
            if (pIElement.id == PostBodyEditingElementBehavior.EXTENDED_ENTRY_ID)
            {
                IHTMLElement2 e2 = (IHTMLElement2)pIElement;
                IHTMLRect rect = e2.getBoundingClientRect();
                prcNEW.top = rect.top;
                prcNEW.bottom = rect.bottom;
                prcNEW.left = rect.left;
                prcNEW.right = rect.right;
            }
        }
Пример #4
0
        // Set the alignment of the image
        internal void SetImageHtmlFromAlignment(ImgAlignment value)
        {
            bool needToSelectImage = false;

            switch (value)
            {
            case ImgAlignment.NONE:
                // If we removed the centering node, we need to reselect the image since the selection
                // is invalidated/changed as a result of removing the node.
                needToSelectImage = RemoveCenteringNode();

                _element.removeAttribute("align", 0);
                _element.style.display    = "inline";
                _element.style.styleFloat = null;

                if (_element.style.marginLeft != null && _element.style.marginLeft.ToString() == "auto")
                {
                    _element.style.marginLeft = 0;
                }
                if (_element.style.marginRight != null && _element.style.marginRight.ToString() == "auto")
                {
                    _element.style.marginRight = 0;
                }

                break;

            case ImgAlignment.LEFT:
            case ImgAlignment.RIGHT:
                // If we removed the centering node, we need to reselect the image since the selection
                // is invalidated/changed as a result of removing the node.
                needToSelectImage = RemoveCenteringNode();

                _element.style.display    = "inline";
                _element.style.styleFloat = value.ToString().ToLower(CultureInfo.InvariantCulture);
                if (_element.style.marginLeft != null && _element.style.marginLeft.ToString() == "auto")
                {
                    _element.style.marginLeft = 0;
                }
                if (_element.style.marginRight != null && _element.style.marginRight.ToString() == "auto")
                {
                    _element.style.marginRight = 0;
                }
                // For all other types of alignment we just set the align property on the image
                _element.setAttribute("align", value.ToString().ToLower(CultureInfo.InvariantCulture), 0);

                break;

            case ImgAlignment.CENTER:
                _element.removeAttribute("align", 0);
                _element.style.styleFloat = null;

                if (GlobalEditorOptions.SupportsFeature(ContentEditorFeature.CenterImageWithParagraph))
                {
                    IHTMLElement element = FindCenteringNode();
                    if (element == null)
                    {
                        // There is no existing centering node, we need to create a new one.
                        // Creating the new centering node invalidates/changes the existing selection
                        // so we need to reselect the image.
                        needToSelectImage = true;
                        element           = CreateNodeForCentering();
                    }
                    if (element != null)
                    {
                        element.setAttribute("align", "center", 0);
                    }
                }
                else
                {
                    _element.style.display     = "block";
                    _element.style.styleFloat  = "none";
                    _element.style.marginLeft  = "auto";
                    _element.style.marginRight = "auto";
                }
                break;

            default:
                Trace.Fail("Unknown image alignment: " + value.ToString());
                break;
            }

            if (needToSelectImage)
            {
                // If we need to reselect the image, do it after we have set the right
                // alignment in the element above so that when the selection change event
                // refreshes the ribbon commands using the html doc, it sees the new values.
                SelectImage();
            }
        }