Beispiel #1
0
        private void imageDecoratorCommand_Execute(object sender, EventArgs e)
        {
            using (IUndoUnit undo = _editorContext.CreateUndoUnit())
            {
                ImageDecorator imageDecorator = ((ImageDecorator)((Command)sender).Tag);

                //perform the execution so that the decorator is added to the list of active decorators
                imageDecorator.Command.PerformExecute();

                //since this command was invoked explicitly via a command button, display the editor dialog.
                object state = null;
                if (imageDecorator.Id == CropDecorator.Id)
                {
                    state = ImagePropertiesInfo.Image;
                }

                using (state as IDisposable)
                {
                    DialogResult result = ImageDecoratorHelper.ShowImageDecoratorEditorDialog(imageDecorator,
                                                                                              ImagePropertiesInfo, new ApplyDecoratorCallback(ApplyImageDecorations),
                                                                                              _editorContext, state, _imageEditingContext, _imageEditingContext.DecoratorsManager.CommandManager);

                    if (result == DialogResult.OK)
                    {
                        undo.Commit();
                    }
                }
            }
        }
        protected override void OnSelectedChanged()
        {
            base.OnSelectedChanged();

            //Show the region border if the text is defaulted or empty
            //If the region is selected, clear any defaulted text
            using (IUndoUnit undo = EditorContext.CreateInvisibleUndoUnit())
            {
                CheckForTitleEdits();
                if (Selected)
                {
                    if (defaultedText)
                    {
                        HTMLElement.innerText = null;
                        RegionBorderVisible   = true;
                        defaultedText         = false;
                    }
                    OnEditableRegionFocusChanged(null, new EditableRegionFocusChangedEventArgs(false));
                }
                else
                {
                    if (HTMLElement.innerText == null)
                    {
                        HTMLElement.innerText = DefaultText;
                        defaultedText         = true;
                        RegionBorderVisible   = true;
                    }
                    RegionBorderVisible = defaultedText;
                }
                undo.Commit();
            }
        }
        protected override void OnElementAttached()
        {
            base.OnElementAttached();

            if (PostEditorSettings.AutomationMode)
            {
                //Test automation requires the element to be explicitly named in the accessibility tree, but
                //setting a title causes an annoying tooltip and focus rectangle, so we only show it in automation mode
                HTMLElement2.tabIndex = 0;
                HTMLElement.title     = " Post Title";
            }

            if (string.IsNullOrEmpty(HTMLElement.innerText))
            {
                //set the default text for this editable region
                using (IUndoUnit undo = EditorContext.CreateInvisibleUndoUnit())
                {
                    HTMLElement.innerText = DefaultText;
                    defaultedText         = true;
                    RegionBorderVisible   = !Selected && defaultedText;
                    undo.Commit();
                }
            }

            //fix bug 403230: set the title style to block element so that it takes up the entire line.
            (HTMLElement as IHTMLElement2).runtimeStyle.display = "block";
        }
Beispiel #4
0
        private void marginCommand_MarginChanged(object sender, EventArgs e)
        {
            using (IUndoUnit undo = _editorContext.CreateUndoUnit())
            {
                bool centered = _alignmentCommand.SelectedItem == Alignment.Center;
                if (!_marginCommand.IsZero())
                {
                    ImagePropertiesInfo.InlineImageMargin = new MarginStyle(_marginCommand.Top, _marginCommand.Right, _marginCommand.Bottom, _marginCommand.Left, StyleSizeUnit.PX);
                }
                else if (!centered)
                {
                    ImagePropertiesInfo.InlineImageMargin = null;
                }

                // Changing the right or left margins on a center aligned image resets the alignment.
                if ((_marginCommand.Right > 0 || _marginCommand.Left > 0) && centered)
                {
                    bool isImageSelected         = ImagePropertiesInfo != null;
                    bool isEditableEmbeddedImage = isImageSelected && ImagePropertiesInfo.IsEditableEmbeddedImage();
                    ResetAlignmentChunkCommands(isImageSelected, isEditableEmbeddedImage);
                }

                undo.Commit();
            }
        }
Beispiel #5
0
        protected override void OnResizeEnd(Size newSize)
        {
            base.OnResizeEnd(newSize);

            if (newSize != _initialParentSize)
            {
                // Only do this if the size actually changed.
                newSize = UpdateHtmlForResize(newSize, true);
                CompactElementSize(newSize);

                PersistAllEditFields();

                //commit the undo unit
                _resizeUndo.Commit();
            }

            _resizeUndo.Dispose();
            _resizeUndo = null;

            //notify listeners that the resize is complete. This allows the sidebar to synchronize
            //with the updated smart content state.
            if (_resizedListener != null)
            {
                _resizedListener(newSize, true);
            }
        }
        /// <summary>
        /// Inserts the extended entry break into the editor.
        /// </summary>
        internal void InsertExtendedEntryBreak()
        {
            IHTMLDocument3 doc3       = (IHTMLDocument3)HTMLElement.document;
            IHTMLElement2  entryBreak = (IHTMLElement2)doc3.getElementById(EXTENDED_ENTRY_ID);

            if (entryBreak == null)
            {
                using (IUndoUnit undo = EditorContext.CreateUndoUnit())
                {
                    using (EditorContext.DamageServices.CreateDamageTracker(ElementRange.Clone(), true))
                    {
                        MarkupPointer insertionPoint =
                            EditorContext.MarkupServices.CreateMarkupPointer(EditorContext.Selection.SelectedMarkupRange.Start);

                        //delete the parent block element of the insertion point if it is empty (bug 421500)
                        DeleteInsertionTargetBlockIfEmpty(insertionPoint);

                        IHTMLElement extendedEntryBreak = InsertExtendedEntryBreak(insertionPoint);

                        //reselect the insertion point
                        MarkupRange selection = EditorContext.MarkupServices.CreateMarkupRange();
                        insertionPoint.MoveAdjacentToElement(extendedEntryBreak, _ELEMENT_ADJACENCY.ELEM_ADJ_AfterEnd);
                        MarkupPointerMoveHelper.MoveUnitBounded(
                            insertionPoint, MarkupPointerMoveHelper.MoveDirection.RIGHT,
                            MarkupPointerAdjacency.AfterEnterBlock | MarkupPointerAdjacency.BeforeText
                            , HTMLElement);
                        selection.Start.MoveToPointer(insertionPoint);
                        selection.End.MoveToPointer(insertionPoint);
                        selection.ToTextRange().select();
                    }
                    undo.Commit();
                }
            }
        }
Beispiel #7
0
        /// <summary>
        /// Called when the ImagePropertiesInfo object has changed in order to sync the commands and decorators with
        /// the currently selected image.
        /// </summary>
        private void ManageCommands()
        {
            using (IUndoUnit undo = _editorContext.CreateInvisibleUndoUnit())
            {
                if (_imageEditingContext != null)
                {
                    for (int i = 0; i < _imageEditingContext.DecoratorsManager.ImageDecoratorGroups.Length; i++)
                    {
                        ImageDecoratorGroup imageDecoratorGroup = _imageEditingContext.DecoratorsManager.ImageDecoratorGroups[i];
                        foreach (ImageDecorator imageDecorator in imageDecoratorGroup.ImageDecorators)
                        {
                            if (_imagePropertiesInfo == null || _imagePropertiesInfo.IsEditableEmbeddedImage() || imageDecorator.IsWebImageSupported)
                            {
                                imageDecorator.Command.Enabled = true;
                            }
                            else
                            {
                                imageDecorator.Command.Enabled = false;
                            }
                        }
                    }

                    ResetCommands();

                    if (ImagePropertiesInfo != null)
                    {
                        RefreshCommands();
                    }
                }
                undo.Commit();
            }
        }
Beispiel #8
0
 public void ExecuteCommand(object sender, EventArgs e)
 {
     using (IUndoUnit undo = _undoFactory.CreateUndoUnit())
     {
         _commandHandler(sender, e);
         undo.Commit();
     }
 }
Beispiel #9
0
 private void _editor_ContentEdited(object source, EventArgs e)
 {
     using (IUndoUnit undo = _sidebarContext.CreateUndoUnit())
     {
         _editableSmartContent.SaveEditedSmartContent();
         undo.Commit();
     }
 }
Beispiel #10
0
        private void customSizeCommand_Execute(object sender, EventArgs args)
        {
            Command selectedCommand = (Command)sender;

            using (IUndoUnit undo = _editorContext.CreateUndoUnit())
            {
                ImagePropertiesInfo.InlineImageSizeName = (ImageSizeName)selectedCommand.Tag;
                ApplyImageDecorations();
                undo.Commit();
            }
        }
Beispiel #11
0
        void imageSizeCommand_ExecuteWithArgs(object sender, ExecuteEventHandlerArgs args)
        {
            SpinnerCommand command  = (SpinnerCommand)sender;
            int            newValue = Convert.ToInt32(args.GetDecimal(command.CommandId.ToString()));

            using (IUndoUnit undo = _editorContext.CreateUndoUnit())
            {
                if (command.CommandId == CommandId.FormatImageAdjustWidth && newValue != ImagePropertiesInfo.InlineImageWidth)
                {
                    ImagePropertiesInfo.InlineImageWidth = newValue;
                    ApplyImageDecorations(ImagePropertyType.InlineSize, ImageDecoratorInvocationSource.Command);
                    undo.Commit();
                }
                else if (command.CommandId == CommandId.FormatImageAdjustHeight && newValue != ImagePropertiesInfo.InlineImageHeight)
                {
                    ImagePropertiesInfo.InlineImageHeight = newValue;
                    ApplyImageDecorations(ImagePropertyType.InlineSize, ImageDecoratorInvocationSource.Command);
                    undo.Commit();
                }
            }
        }
Beispiel #12
0
 void imageLinkOptionsCommand_Execute(object sender, EventArgs e)
 {
     using (new WaitCursor())
         using (IUndoUnit undo = _editorContext.CreateUndoUnit())
         {
             if (EditTargetOptions(ImagePropertiesInfo.LinkTarget) == DialogResult.OK)
             {
                 ApplyImageDecorations();
                 undo.Commit();
             }
         }
 }
Beispiel #13
0
 protected override void OnKeyDown(HtmlEventArgs e)
 {
     base.OnKeyDown(e);
     if (((Keys)e.htmlEvt.keyCode) == Keys.Back)
     {
         using (IUndoUnit undo = EditorContext.CreateUndoUnit())
         {
             (HTMLElement as IHTMLDOMNode).removeNode(true);
             undo.Commit();
         }
         e.Cancel();
     }
 }
Beispiel #14
0
        private void EditorContext_HandleClear(HtmlEditorSelectionOperationEventArgs ea)
        {
            if (Selected && MultipleCellsSelected && !EntireTableSelected)
            {
                using (IUndoUnit undoUnit = EditorContext.CreateUndoUnit())
                {
                    TableEditor.ClearCells(EditorContext);
                    undoUnit.Commit();
                }

                ea.Handled = true;
            }
        }
Beispiel #15
0
        private void ApplyBlockStyle(_ELEMENT_TAG_ID styleTagId, MarkupRange selection, MarkupRange maximumBounds, MarkupRange postOpSelection)
        {
            Debug.Assert(selection != maximumBounds, "selection and maximumBounds must be distinct objects");
            SelectionPositionPreservationCookie selectionPreservationCookie = null;

            //update the range cling and gravity so it will stick with the re-arranged block content
            selection.Start.PushCling(false);
            selection.Start.PushGravity(_POINTER_GRAVITY.POINTER_GRAVITY_Left);
            selection.End.PushCling(false);
            selection.End.PushGravity(_POINTER_GRAVITY.POINTER_GRAVITY_Right);

            try
            {
                if (selection.IsEmpty())
                {
                    //nothing is selected, so expand the selection to cover the entire parent block element
                    IHTMLElementFilter stopFilter =
                        ElementFilters.CreateCompoundElementFilter(ElementFilters.BLOCK_ELEMENTS,
                                                                   new IHTMLElementFilter(IsSplitStopElement));
                    MovePointerLeftUntilRegionBreak(selection.Start, stopFilter, maximumBounds.Start);
                    MovePointerRightUntilRegionBreak(selection.End, stopFilter, maximumBounds.End);
                }

                using (IUndoUnit undo = _editor.CreateSelectionUndoUnit(selection))
                {
                    selectionPreservationCookie = SelectionPositionPreservationHelper.Save(_markupServices, postOpSelection, selection);
                    if (selection.IsEmptyOfContent())
                    {
                        ApplyBlockFormatToEmptySelection(selection, styleTagId, maximumBounds);
                    }
                    else
                    {
                        ApplyBlockFormatToContentSelection(selection, styleTagId, maximumBounds);
                    }
                    undo.Commit();
                }
            }
            finally
            {
                selection.Start.PopCling();
                selection.Start.PopGravity();
                selection.End.PopCling();
                selection.End.PopGravity();
            }

            if (!SelectionPositionPreservationHelper.Restore(selectionPreservationCookie, selection, selection.Clone()))
            {
                selection.ToTextRange().select();
            }
        }
Beispiel #16
0
 internal void ClearDefaultText()
 {
     Debug.Assert(_undoRedoCheck != null, "Clearing default text on an unmanaged inline edit field");
     if (IsDefaultText && (!_undoRedoCheck.UndoRedoExecuting()))
     {
         using (IUndoUnit undo = _editorContext.CreateInvisibleUndoUnit())
         {
             _element.innerHTML = "";
             if (!string.IsNullOrEmpty(TextColor))
             {
                 _element.style.color = TextColor;
             }
             IsDefaultText = false;
             undo.Commit();
         }
     }
 }
Beispiel #17
0
        protected virtual void OnImagePropertyChanged(ImagePropertyEvent evt)
        {
            using (IUndoUnit undo = _editorContext.CreateUndoUnit())
            {
                if (ImagePropertyChanged != null)
                {
                    ImagePropertyChanged(this, evt);
                }

                undo.Commit();
            }

            switch (evt.PropertyType)
            {
            case ImagePropertyType.Decorators:
            {
                RefreshSizeChunkCommands();
                RefreshStylesChunkCommands();
                RefreshPropertiesChunkCommands();
                break;
            }

            case ImagePropertyType.InlineSize:
                RefreshSizeChunkCommands();
                break;

            case ImagePropertyType.Source:
                RefreshPropertiesChunkCommands();
                break;

            default:
                RefreshCommands();
                break;
            }

            // If this is a reset, then handle alignment/margins
            if (evt.InvocationSource == ImageDecoratorInvocationSource.Reset)
            {
                // Reset margins and alignments
                bool isImageSelected         = ImagePropertiesInfo != null;
                bool isEditableEmbeddedImage = isImageSelected && ImagePropertiesInfo.IsEditableEmbeddedImage();

                ResetAlignmentChunkCommands(isImageSelected, isEditableEmbeddedImage);
                ResetMarginsChunkCommands(isImageSelected, isEditableEmbeddedImage);
            }
        }
        public void CleanBeforeInsert()
        {
            using (IUndoUnit undo = EditorContext.CreateInvisibleUndoUnit())
            {
                //Fire event if title has changed.
                CheckForTitleEdits();

                // If the title contains the default text..
                if (defaultedText)
                {
                    // Clear it before the insert.
                    HTMLElement.innerText = null;
                    defaultedText         = false;
                }

                undo.Commit();
            }
        }
Beispiel #19
0
        public void SetDefaultText()
        {
            Debug.Assert(_undoRedoCheck != null, "Setting default text on an unmanaged inline edit field");
            // WinLive 210281: Don't update the default text unless really necessary. If an undo forces this function
            // to run, then creating a new undo unit will clear the redo stack.
            if (String.IsNullOrEmpty(_element.innerText) && (!_undoRedoCheck.UndoRedoExecuting()))
            {
                using (IUndoUnit undo = _editorContext.CreateInvisibleUndoUnit())
                {
                    _element.innerText = DefaultText;
                    if (!string.IsNullOrEmpty(DefaultTextColor))
                    {
                        _element.style.color = DefaultTextColor;
                    }
                    IsDefaultText = true;

                    undo.Commit();
                }
            }
        }
Beispiel #20
0
        private void alignmentCommand_AlignmentChanged(object sender, EventArgs e)
        {
            if (ImagePropertiesInfo.InlineImageAlignment != _alignmentCommand.SelectedItem)
            {
                using (IUndoUnit undo = _editorContext.CreateUndoUnit())
                {
                    ImagePropertiesInfo.InlineImageAlignment = _alignmentCommand.SelectedItem;

                    // Center aligning an image overwrites the right and left margins.
                    if (_alignmentCommand.SelectedItem == Alignment.Center)
                    {
                        bool isImageSelected         = ImagePropertiesInfo != null;
                        bool isEditableEmbeddedImage = isImageSelected && ImagePropertiesInfo.IsEditableEmbeddedImage();
                        ResetMarginsChunkCommands(isImageSelected, isEditableEmbeddedImage);
                    }

                    undo.Commit();
                }
            }
        }
Beispiel #21
0
        void imageTargetSelectGalleryCommand_Execute(object sender, EventArgs args)
        {
            // By the time this is called we've already changed the dropdown
            // If user cancels then we need to restore previous dropdown selection.
            Command        selectedCommand   = (Command)sender;
            LinkTargetType newLinkTargetType = (LinkTargetType)selectedCommand.Tag;

            using (new WaitCursor())
                using (IUndoUnit undo = _editorContext.CreateUndoUnit())
                {
                    if (newLinkTargetType == LinkTargetType.URL && EditTargetOptions(newLinkTargetType) != DialogResult.OK)
                    {
                        newLinkTargetType = ImagePropertiesInfo.LinkTarget;
                        _imageLinkTargetDropdown.SelectTag(newLinkTargetType);
                    }

                    ImagePropertiesInfo.LinkTarget = newLinkTargetType;
                    ApplyImageDecorations();
                    undo.Commit();
                }
        }
 public void FireCancelPreview()
 {
     try
     {
         if (OnCancelPreview != null && _inPreview)
         {
             OnCancelPreview(this, EventArgs.Empty);
         }
     }
     finally
     {
         if (undoUnit != null)
         {
             undoUnit.Commit();
             // If we commit, then applying h1 can leave text split apart.
             // If we don't commit, then we end up with the Redo stack changing.
             undoUnit.Dispose();
             undoUnit = null;
         }
         _inPreview = false;
     }
 }
Beispiel #23
0
            public void DoDelete()
            {
                if (_realExtendedEntry.sourceIndex == 0)                //this "real" entry is deleted
                {
                    return;
                }

                using (IUndoUnit undo = _editorContext.CreateInvisibleUndoUnit())
                {
                    IHTMLElementCollection extendedEntryElements = _document.getElementsByName(PostBodyEditingElementBehavior.EXTENDED_ENTRY_ID);
                    foreach (IHTMLElement element in extendedEntryElements)
                    {
                        if (element.sourceIndex != _realExtendedEntry.sourceIndex)
                        {
                            if (element.sourceIndex > -1)                            //just in case its already been deleted
                            {
                                (element as IHTMLDOMNode).removeNode(true);
                            }
                        }
                    }

                    undo.Commit();
                }
            }
Beispiel #24
0
 public void Commit()
 {
     _undo.Commit();
 }
Beispiel #25
0
        /// <summary>
        /// Grabs HTML copied in the clipboard and pastes it into the document (pulls in a copy of embedded content too)
        /// </summary>
        protected override bool DoInsertData(DataAction action, MarkupPointer begin, MarkupPointer end)
        {
            using (new WaitCursor())
            {
                try
                {
                    string baseUrl = UrlHelper.GetBasePathUrl(DataMeister.HTMLData.SourceURL);
                    string html    = DataMeister.HTMLData.HTMLSelection;

                    //Check to see if the selection has an incomplete unordered list
                    var finder = new IncompleteListFinder(html);
                    finder.Parse();

                    if ((!EditorContext.CleanHtmlOnPaste) || finder.HasIncompleteList)
                    {
                        using (IUndoUnit undoUnit = EditorContext.CreateInvisibleUndoUnit())
                        {
                            // Create a new MarkupContainer off of EditorContext's document that contains the source HTML
                            // with comments marking the start and end selection.
                            MarkupContainer sourceContainer = EditorContext.MarkupServices.ParseString(DataMeister.HTMLData.HTMLWithMarkers);

                            // MSHTML's ParseString implementation clears all the attributes on the <body> element, so we
                            // have to manually add them back in.
                            CopyBodyAttributes(DataMeister.HTMLData.HTMLWithMarkers, sourceContainer.Document.body);

                            MarkupRange sourceRange = FindMarkedFragment(sourceContainer.Document,
                                                                         HTMLDataObject.START_FRAGMENT_MARKER, HTMLDataObject.END_FRAGMENT_MARKER);
                            MshtmlMarkupServices sourceContainerMarkupServices =
                                new MshtmlMarkupServices((IMarkupServicesRaw)sourceContainer.Document);

                            // Some applications may not add the correct fragment markers (e.g. copying from Fiddler from
                            // the Web Sessions view). We'll just select the entire <body> of the clipboard in this case.
                            if (sourceRange == null)
                            {
                                sourceRange = sourceContainerMarkupServices.CreateMarkupRange(sourceContainer.Document.body, false);
                            }
                            else
                            {
                                // Make sure that we don't try to copy just parts of a table/list. We need to include the
                                // parent table/list.
                                if (!EditorContext.CleanHtmlOnPaste)
                                {
                                    ExpandToIncludeTables(sourceRange, sourceContainerMarkupServices);
                                }
                                ExpandToIncludeLists(sourceRange, sourceContainerMarkupServices);
                            }

                            if (sourceRange != null)
                            {
                                if (!EditorContext.CleanHtmlOnPaste)
                                {
                                    // WinLive 273280: Alignment on a table acts like a float, which can throw off the layout of the rest of
                                    // the document. If there is nothing before or after the table, then we can safely remove the alignment.
                                    RemoveAlignmentIfSingleTable(sourceRange);

                                    // Serialize the source HTML to a string while keeping the source formatting.
                                    MarkupRange destinationRange = EditorContext.MarkupServices.CreateMarkupRange(begin.Clone(), end.Clone());
                                    html = KeepSourceFormatting(sourceRange, destinationRange);
                                }
                                else
                                {
                                    html = sourceRange.HtmlText;
                                }
                            }

                            undoUnit.Commit();
                        }

                        Trace.Assert(html != null, "Inline source CSS failed!");
                    }

                    if (html == null)
                    {
                        html = DataMeister.HTMLData.HTMLSelection;
                    }

                    if (IsPasteFromSharedCanvas(DataMeister))
                    {
                        if (action == DataAction.Copy)
                        {
                            // WinLive 96840 - Copying and pasting images within shared canvas should persist source
                            // decorator settings. "wlCopySrcUrl" is inserted while copy/pasting within canvas.
                            html = EditorContext.FixImageReferences(ImageCopyFixupHelper.FixupSourceUrlForCopy(html),
                                                                    DataMeister.HTMLData.SourceURL);
                        }
                    }
                    else
                    {
                        html = EditorContext.FixImageReferences(html, DataMeister.HTMLData.SourceURL);

                        HtmlCleanupRule cleanupRule = HtmlCleanupRule.Normal;
                        if (IsOfficeHtml(DataMeister.HTMLData))
                        {
                            cleanupRule = HtmlCleanupRule.PreserveTables;
                        }

                        // In Mail, we want to preserve the style of the html that is on the clipboard
                        // Whereas in Writer we by default want to remove formatting so it looks like your blog theme
                        if (EditorContext.CleanHtmlOnPaste)
                        {
                            // optionally cleanup the html
                            html = EditorContext.HtmlGenerationService.CleanupHtml(html, baseUrl, cleanupRule);
                        }
                        else
                        {
                            html = HtmlCleaner.StripNamespacedTagsAndCommentsAndMarkupDirectives(html);
                        }

                        // standard fixups
                        html = EditorContext.HtmlGenerationService.GenerateHtmlFromHtmlFragment(html, baseUrl);
                    }

                    // insert the content
                    if (EditorContext.MarshalHtmlSupported)
                    {
                        EditorContext.InsertHtml(begin, end, html, DataMeister.HTMLData.SourceURL);
                    }
                    else if (EditorContext.MarshalTextSupported)
                    {
                        // This is called only in the case that we're attempting to marshal HTML, but only
                        // text is supported. In this case, we should down convert to text and provide that.
                        html = HTMLDocumentHelper.HTMLToPlainText(html);
                        EditorContext.InsertHtml(begin, end, html, DataMeister.HTMLData.SourceURL);
                    }
                    else
                    {
                        Debug.Assert(false, "Html being inserted when text or html isn't supported.");
                    }

                    // Now select what was just inserted
                    EditorContext.MarkupServices.CreateMarkupRange(begin, end).ToTextRange().select();

                    //place the caret at the end of the inserted content
                    //EditorContext.MoveCaretToMarkupPointer(end, true);
                    return(true);
                }
                catch (Exception e)
                {
                    //bugfix 1696, put exceptions into the trace log.
                    Trace.Fail("Exception while inserting HTML: " + e.Message, e.StackTrace);
                    return(false);
                }
            }
        }
Beispiel #26
0
        private void PasteCellsIntoTable(IHTMLTable sourceTable, TableSelection tableSelection)
        {
            // collect up the top level cells we are pasting
            ArrayList cellsToPaste = new ArrayList();

            foreach (IHTMLTableRow row in sourceTable.rows)
            {
                foreach (IHTMLElement cell in row.cells)
                {
                    cellsToPaste.Add(cell);
                }
            }

            // get the list of cells we are pasting into
            int       appendRows  = 0;
            int       cellsPerRow = 0;
            ArrayList targetCells;

            if (tableSelection.SelectedCells.Count > 1)
            {
                targetCells = tableSelection.SelectedCells;
            }
            else
            {
                targetCells = new ArrayList();
                bool accumulatingCells = false;
                foreach (IHTMLTableRow row in tableSelection.Table.rows)
                {
                    cellsPerRow = row.cells.length;
                    foreach (IHTMLElement cell in row.cells)
                    {
                        if (!accumulatingCells && HTMLElementHelper.ElementsAreEqual(cell, tableSelection.BeginCell as IHTMLElement))
                        {
                            accumulatingCells = true;
                        }

                        if (accumulatingCells)
                        {
                            targetCells.Add(cell);
                        }
                    }
                }

                // if the target cells aren't enough to paste all of the cells, then
                // calculate the number of rows we need to append to fit all of the
                // cells being pasted
                int cellGap = cellsToPaste.Count - targetCells.Count;
                if (cellGap > 0 && cellsPerRow > 0)
                {
                    appendRows = cellGap / cellsPerRow + (cellGap % cellsPerRow == 0 ? 0 : 1);
                }
            }

            // perform the paste
            using (IUndoUnit undoUnit = EditorContext.CreateUndoUnit())
            {
                // append rows if needed
                if (appendRows > 0)
                {
                    // see if we can cast our editor context to the one required
                    // by the table editor
                    IHtmlEditorComponentContext editorContext = EditorContext as IHtmlEditorComponentContext;
                    if (editorContext != null)
                    {
                        // markup range based on last target cell
                        IHTMLElement lastCell      = targetCells[targetCells.Count - 1] as IHTMLElement;
                        MarkupRange  lastCellRange = EditorContext.MarkupServices.CreateMarkupRange(lastCell);
                        for (int i = 0; i < appendRows; i++)
                        {
                            IHTMLTableRow row = TableEditor.InsertRowBelow(editorContext, lastCellRange);
                            foreach (IHTMLElement cell in row.cells)
                            {
                                targetCells.Add(cell);
                            }
                            lastCellRange = EditorContext.MarkupServices.CreateMarkupRange(row as IHTMLElement);
                        }
                    }
                    else
                    {
                        Debug.Fail("Couldn't cast EditorContext!");
                    }
                }

                // do the paste
                for (int i = 0; i < cellsToPaste.Count && i < targetCells.Count; i++)
                {
                    (targetCells[i] as IHTMLElement).innerHTML = (cellsToPaste[i] as IHTMLElement).innerHTML;
                }
                undoUnit.Commit();
            }
        }