Ejemplo n.º 1
0
        /// <summary>
        /// Scans the DOM for images that have not yet been properly initialized by the editor.
        /// As part of the initialization, the default image settings and effects will be applied to the image.
        /// </summary>
        internal static void ScanAndInitializeNewImages(IBlogPostHtmlEditor currentEditor, ISupportingFileService fileService, IEditorAccount editorAccount, ContentEditor editor, Control owner, bool useDefaultTargetSettings, bool selectLastImage)
        {
            if (currentEditor is BlogPostHtmlEditorControl)
            {
                // Scanning the images and doing basic initialization is done on the UI thread.
                List <NewImageInfo> newImages = ScanImages(currentEditor, editorAccount, editor, useDefaultTargetSettings);

                if (newImages.Count > 0)
                {
                    // The time-consuming initialization is done in a background thread.
                    ImageInitializationAsyncOperation imageInitializer = new ImageInitializationAsyncOperation(newImages, fileService, owner);
                    imageInitializer.Completed += new EventHandler((sender, e) =>
                                                                   ProcessInitializedImages(newImages, currentEditor, editor, owner, selectLastImage));

                    editor.DisposeOnEditorChange(imageInitializer);

                    imageInitializer.Start();
                }
            }
        }
        /// <summary>
        /// Scans the DOM for images that have not yet been properly initialized by the editor.
        /// As part of the initialization, the default image settings and effects will be applied to the image.
        /// </summary>
        internal static void ScanAndInitializeNewImages(IBlogPostHtmlEditor currentEditor, ISupportingFileService fileService, IEditorAccount editorAccount, ContentEditor editor, Control owner, bool useDefaultTargetSettings, bool selectLastImage)
        {
            if (currentEditor is BlogPostHtmlEditorControl)
            {
                // Scanning the images and doing basic initialization is done on the UI thread.
                List<NewImageInfo> newImages = ScanImages(currentEditor, editorAccount, editor, useDefaultTargetSettings);

                if (newImages.Count > 0)
                {
                    // The time-consuming initialization is done in a background thread.
                    ImageInitializationAsyncOperation imageInitializer = new ImageInitializationAsyncOperation(newImages, fileService, owner);
                    imageInitializer.Completed += new EventHandler((sender, e) =>
                        ProcessInitializedImages(newImages, currentEditor, editor, owner, selectLastImage));

                    editor.DisposeOnEditorChange(imageInitializer);

                    imageInitializer.Start();
                }
            }
        }
Ejemplo n.º 3
0
        private static void ProcessInitializedImages(List <NewImageInfo> newImages, IBlogPostHtmlEditor currentEditor, ContentEditor editor, Control owner, bool selectLastImage)
        {
            // Remove all invalid images first
            List <NewImageInfo> newImagesToUpdate = new List <NewImageInfo>();

            for (int i = 0; i < newImages.Count; i++)
            {
                NewImageInfo info = newImages[i];

                if (info.Remove)
                {
                    using (ContentEditor.EditorUndoUnit undo = new ContentEditor.EditorUndoUnit(currentEditor, true))
                    {
                        info.DisabledImageBehavior.DetachFromElement();
                        HTMLElementHelper.RemoveElement(info.Element);
                        undo.Commit();
                    }
                }
                else
                {
                    newImagesToUpdate.Add(info);
                }
            }

            // Queue up processing for any remaining valid images
            MultiTaskHelper tasks = new MultiTaskHelper(30);

            for (int i = 0; i < newImagesToUpdate.Count; i++)
            {
                bool         lastElement = i == (newImagesToUpdate.Count - 1);
                NewImageInfo info        = newImagesToUpdate[i];

                tasks.AddWork(delegate
                {
                    // WinLive 214012: If the original insertion operation is undone before we get to this point make
                    // sure we don't attempt to update the HTML. Elements that are removed via an undo, but that we
                    // still hold a reference to, are put into another document whose readyState is "uninitialized".
                    // It's also possible that the picture has already been updated if a user undoes and redoes the
                    // initial insertion multiple times.
                    IHTMLDocument2 doc = (IHTMLDocument2)info.Element.document;
                    if (doc == null || doc.readyState.Equals("uninitialized", StringComparison.OrdinalIgnoreCase) ||
                        BlogPostImageDataList.LookupImageDataByInlineUri(editor.ImageList, info.ImageData.InlineImageFile.Uri) != null)
                    {
                        info.DisabledImageBehavior.DetachFromElement();
                        return;
                    }

                    using (new QuickTimer("ProcessInitializedImage"))
                        using (ContentEditor.EditorUndoUnit undo = new ContentEditor.EditorUndoUnit(currentEditor, true))
                        {
                            editor.ImageList.AddImage(info.ImageData);

                            info.Element.setAttribute("src", UrlHelper.SafeToAbsoluteUri(info.ImageData.InlineImageFile.Uri), 0);
                            info.Element.removeAttribute("srcDelay", 0);
                            // Create the decorators
                            ImageEditingPropertyHandler.UpdateImageSource(info.ImageInfo, info.Element, editor, new ImageInsertHandler(), ImageDecoratorInvocationSource.InitialInsert);

                            // Manually detach the behavior so that the image can be selected and resized.
                            info.DisabledImageBehavior.DetachFromElement();

                            if (lastElement)
                            {
                                ApplicationPerformance.EndEvent("InsertImage");
                                HandleNewImage((BlogPostHtmlEditorControl)currentEditor, owner, info.Element, selectLastImage);
                            }

                            undo.Commit();

                            if (editor.ETWProvider != null)
                            {
                                editor.ETWProvider.WriteEvent("InlinePhotoEnd");
                            }
                        }
                });
            }

            // If there were no images to update, stop the perf timer
            if (newImagesToUpdate.Count == 0)
            {
                ApplicationPerformance.EndEvent("InsertImage");
            }

            // When the editor is closing, or changing to a new blog post we need to get rid of this object
            // which will then stop all the unfinished images from continuing to load.
            editor.DisposeOnEditorChange(tasks);
            tasks.Start();
        }
        private static void ProcessInitializedImages(List<NewImageInfo> newImages, IBlogPostHtmlEditor currentEditor, ContentEditor editor, Control owner, bool selectLastImage)
        {
            // Remove all invalid images first
            List<NewImageInfo> newImagesToUpdate = new List<NewImageInfo>();
            for (int i = 0; i < newImages.Count; i++)
            {
                NewImageInfo info = newImages[i];

                if (info.Remove)
                {
                    using (ContentEditor.EditorUndoUnit undo = new ContentEditor.EditorUndoUnit(currentEditor, true))
                    {
                        info.DisabledImageBehavior.DetachFromElement();
                        HTMLElementHelper.RemoveElement(info.Element);
                        undo.Commit();
                    }
                }
                else
                {
                    newImagesToUpdate.Add(info);
                }
            }

            // Queue up processing for any remaining valid images
            MultiTaskHelper tasks = new MultiTaskHelper(30);
            for (int i = 0; i < newImagesToUpdate.Count; i++)
            {
                bool lastElement = i == (newImagesToUpdate.Count - 1);
                NewImageInfo info = newImagesToUpdate[i];

                tasks.AddWork(delegate
                {
                    // WinLive 214012: If the original insertion operation is undone before we get to this point make
                    // sure we don't attempt to update the HTML. Elements that are removed via an undo, but that we
                    // still hold a reference to, are put into another document whose readyState is "uninitialized".
                    // It's also possible that the picture has already been updated if a user undoes and redoes the
                    // initial insertion multiple times.
                    IHTMLDocument2 doc = (IHTMLDocument2)info.Element.document;
                    if (doc == null || doc.readyState.Equals("uninitialized", StringComparison.OrdinalIgnoreCase) ||
                        BlogPostImageDataList.LookupImageDataByInlineUri(editor.ImageList, info.ImageData.InlineImageFile.Uri) != null)
                    {
                        info.DisabledImageBehavior.DetachFromElement();
                        return;
                    }

                    using (new QuickTimer("ProcessInitializedImage"))
                    using (ContentEditor.EditorUndoUnit undo = new ContentEditor.EditorUndoUnit(currentEditor, true))
                    {
                        editor.ImageList.AddImage(info.ImageData);

                        info.Element.setAttribute("src", UrlHelper.SafeToAbsoluteUri(info.ImageData.InlineImageFile.Uri), 0);
                        info.Element.removeAttribute("srcDelay", 0);
                        // Create the decorators
                        ImageEditingPropertyHandler.UpdateImageSource(info.ImageInfo, info.Element, editor, new ImageInsertHandler(), ImageDecoratorInvocationSource.InitialInsert);

                        // Manually detach the behavior so that the image can be selected and resized.
                        info.DisabledImageBehavior.DetachFromElement();

                        if (lastElement)
                        {
                            ApplicationPerformance.EndEvent("InsertImage");
                            HandleNewImage((BlogPostHtmlEditorControl)currentEditor, owner, info.Element, selectLastImage);
                        }

                        undo.Commit();

                        if (editor.ETWProvider != null)
                            editor.ETWProvider.WriteEvent("InlinePhotoEnd");
                    }
                });
            }

            // If there were no images to update, stop the perf timer
            if (newImagesToUpdate.Count == 0)
            {
                ApplicationPerformance.EndEvent("InsertImage");
            }

            // When the editor is closing, or changing to a new blog post we need to get rid of this object
            // which will then stop all the unfinished images from continuing to load.
            editor.DisposeOnEditorChange(tasks);
            tasks.Start();
        }