Пример #1
0
        /// <summary>
        /// This function is the callback used to execute the command when the menu item is clicked.
        /// See the constructor to see how the menu item is associated with this function using
        /// OleMenuCommandService service and MenuCommand class.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event args.</param>
        private void MenuItemCallback(object sender, EventArgs e)
        {
            var dte = (DTE2)ServiceProvider.GetService(typeof(DTE));

            var docFile      = dte.ActiveDocument.FullName;
            var selectedText = dte.Selection().Text;
            var imageText    = SimpleText.Parse(selectedText) as ImageText;

            if (imageText == null)
            {
                var docType = Path.GetExtension(docFile).ToLower();

                switch (docType)
                {
                case ".md":
                    imageText = MarkdownImageText.Parse(selectedText);
                    break;

                case ".js":
                case ".html":
                    imageText = HtmlImgText.Parse(selectedText);
                    break;
                }
            }

            if (imageText == null)
            {
                imageText = new SimpleText()
                {
                    Text = selectedText
                }
            }
            ;

            imageText.ParentWindow = new SimpleWindow(dte.MainWindow.HWnd);
            imageText.FullFilename = docFile;

            var newText = imageText.PasteFromClipboard();

            if (newText == null)
            {
                return;
            }

            if (newText.Length == 0)
            {
                dte.Selection().CharRight();
                return;
            }

            dte.BeginUpdate("paste image");

            dte.Selection().DestructiveInsert(newText);

            dte.EndUpdate();
        }