Exemplo n.º 1
0
        private bool CheckForImageLink(string line, AcePosition pos)
        {
            // Check for images ![](imageUrl)
            var matches = ImageRegex.Matches(line);

            if (matches.Count > 0)
            {
                foreach (Match match in matches)
                {
                    string val = match.Value;
                    if (match.Index <= pos.column && match.Index + val.Length > pos.column)
                    {
                        var mi = new MenuItem
                        {
                            Header = "Edit in Image Editor"
                        };
                        mi.Click += (o, args) =>
                        {
                            var image = HttpUtility.UrlDecode(StringUtils.ExtractString(val, "(", ")"));
                            image = mmFileUtils.NormalizeFilenameWithBasePath(image,
                                                                              Path.GetDirectoryName(Model.ActiveDocument.Filename));
                            mmFileUtils.OpenImageInImageEditor(image);
                        };
                        ContextMenu.Items.Add(mi);

                        var mi2 = new MenuItem
                        {
                            Header = "Edit Image Link"
                        };
                        mi2.Click += (o, args) =>
                        {
                            Model.ActiveEditor.AceEditor.SetSelectionRange(pos.row, match.Index, pos.row,
                                                                           match.Index + val.Length);
                            Model.ActiveEditor.EditorSelectionOperation("image", val);
                        };
                        ContextMenu.Items.Add(mi2);

                        return(true);
                    }
                }
            }

            return(false);
        }
Exemplo n.º 2
0
        private bool CheckForHyperLink(string line, AcePosition pos)
        {
            var matches = HrefRegex.Matches(line);

            if (matches.Count > 0)
            {
                foreach (Match match in matches)
                {
                    string val = match.Value;
                    if (match.Index <= pos.column && match.Index + val.Length > pos.column)
                    {
                        var mi2 = new MenuItem
                        {
                            Header = "Edit Hyperlink"
                        };
                        mi2.Click += (o, args) =>
                        {
                            Model.ActiveEditor.AceEditor.SetSelectionRange(pos.row, match.Index, pos.row,
                                                                           match.Index + val.Length, pos);
                            Model.ActiveEditor.EditorSelectionOperation("hyperlink", val);
                        };
                        ContextMenu.Items.Add(mi2);

                        var mi = new MenuItem
                        {
                            Header = "Remove Hyperlink"
                        };
                        mi.Click += (o, args) =>
                        {
                            Model.ActiveEditor.AceEditor.SetSelectionRange(pos.row, match.Index, pos.row,
                                                                           match.Index + val.Length, pos);
                            string text = StringUtils.ExtractString(val, "[", "]");
                            Model.ActiveEditor.SetSelection(text);
                        };
                        ContextMenu.Items.Add(mi);

                        return(true);
                    }
                }
            }
            return(false);
        }
Exemplo n.º 3
0
        private bool CheckForTable(string line, AcePosition pos)
        {
            if (string.IsNullOrEmpty(line))
            {
                return(false);
            }

            line = line.Trim();

            if (line.Contains("|") ||
                line.StartsWith("+-") ||
                line.StartsWith("+="))
            {
                var mi = new MenuItem
                {
                    Header = "Edit Table"
                };
                mi.Click += (o, args) =>
                {
                    var tableMarkdown = SelectPipeAndGridTableMarkdown();
                    Model.ActiveEditor.EditorSelectionOperation("table", tableMarkdown);
                };
                ContextMenu.Items.Add(mi);

                mi = new MenuItem
                {
                    Header = "Format Table"
                };
                mi.Click += (o, args) =>
                {
                    string mdTableHtml = SelectPipeAndGridTableMarkdown();
                    if (string.IsNullOrEmpty(mdTableHtml))
                    {
                        return;
                    }

                    var parser    = new TableParser();
                    var formatted = parser.FormatMarkdownTable(mdTableHtml);
                    if (formatted == null)
                    {
                        return;
                    }

                    Model.ActiveEditor.SetSelectionAndFocus(formatted);
                    Model.ActiveEditor.PreviewMarkdownCallback();
                };
                ContextMenu.Items.Add(mi);

                return(true);
            }
            else if (line.StartsWith("<td", StringComparison.InvariantCultureIgnoreCase) ||
                     line.StartsWith("<tr", StringComparison.InvariantCultureIgnoreCase) ||
                     line.StartsWith("<th", StringComparison.InvariantCultureIgnoreCase) ||
                     line.StartsWith("<table>", StringComparison.InvariantCultureIgnoreCase) ||
                     line.StartsWith("<table ", StringComparison.InvariantCultureIgnoreCase) ||
                     line.Equals("<thead>", StringComparison.InvariantCultureIgnoreCase) ||
                     line.Equals("<tbody>", StringComparison.InvariantCultureIgnoreCase))
            {
                StringBuilder sbTableMarkdown = new StringBuilder();

                var mi = new MenuItem
                {
                    Header = "Edit Table"
                };
                mi.Click += (o, args) =>
                {
                    string mdTableHtml = SelectHtmlTableMarkdown();
                    if (string.IsNullOrEmpty(mdTableHtml))
                    {
                        return;
                    }

                    Model.ActiveEditor.EditorSelectionOperation("table", mdTableHtml);
                };
                ContextMenu.Items.Add(mi);
                mi = new MenuItem
                {
                    Header = "Format Table"
                };
                mi.Click += (o, args) =>
                {
                    string mdTableHtml = SelectHtmlTableMarkdown();
                    if (string.IsNullOrEmpty(mdTableHtml))
                    {
                        return;
                    }

                    var parser    = new TableParser();
                    var formatted = parser.FormatMarkdownTable(mdTableHtml);
                    if (formatted == null)
                    {
                        return;
                    }

                    Model.ActiveEditor.SetSelectionAndFocus(formatted);
                    Model.ActiveEditor.PreviewMarkdownCallback();
                };

                ContextMenu.Items.Add(mi);
                return(true);
            }

            return(false);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Adds menu options for image editing, embedding and a few other
        /// image operations conditionally.
        /// </summary>
        /// <param name="line"></param>
        /// <param name="pos"></param>
        /// <returns></returns>
        private bool CheckForImageLink(string line, AcePosition pos)
        {
            // Check for images ![](imageUrl)
            var matches = ImageRegex.Matches(line);

            if (matches.Count > 0)
            {
                foreach (Match match in matches)
                {
                    string val       = match.Value;
                    var    imageLink = HttpUtility.UrlDecode(match.Groups[1].Value);


                    if (match.Index <= pos.column && match.Index + val.Length > pos.column)
                    {
                        bool isWebLink = imageLink.StartsWith("http");

                        if (!isWebLink)
                        {
                            var mi = new MenuItem {
                                Header = "Edit in Image Editor"
                            };
                            mi.Click += (o, args) =>
                            {
                                imageLink = mmFileUtils.NormalizeFilenameWithBasePath(imageLink,
                                                                                      Path.GetDirectoryName(Model.ActiveDocument.Filename));
                                mmFileUtils.OpenImageInImageEditor(imageLink);
                            };
                            ContextMenu.Items.Add(mi);
                        }

                        var mi2 = new MenuItem
                        {
                            Header = "Edit Image Url"
                        };
                        mi2.Click += (o, args) =>
                        {
                            Model.ActiveEditor.AceEditor.SetSelectionRange(pos.row, match.Index, pos.row,
                                                                           match.Index + val.Length);
                            Model.ActiveEditor.EditorSelectionOperation("image", val);
                        };
                        ContextMenu.Items.Add(mi2);

                        if (isWebLink)
                        {
                            var mi = new MenuItem {
                                Header = "Download and embed as Local Image"
                            };
                            mi.Click += async(o, args) =>
                            {
                                var sd = new SaveFileDialog
                                {
                                    Filter = "Image files (*.png;*.jpg;*.gif;)|*.png;*.jpg;*.jpeg;*.gif|" +
                                             "All Files (*.*)|*.*",
                                    FilterIndex      = 1,
                                    Title            = "Save Image from URL as",
                                    CheckFileExists  = false,
                                    OverwritePrompt  = true,
                                    CheckPathExists  = true,
                                    RestoreDirectory = true,
                                    ValidateNames    = true,
                                    FileName         = Path.GetFileName(imageLink)
                                };
                                var doc    = Model.ActiveDocument;
                                var editor = Model.ActiveEditor;

                                if (!string.IsNullOrEmpty(doc.LastImageFolder))
                                {
                                    sd.InitialDirectory = doc.LastImageFolder;
                                }
                                else if (!string.IsNullOrEmpty(doc.Filename) &&
                                         !doc.Filename.Equals("untitled", StringComparison.OrdinalIgnoreCase))
                                {
                                    sd.InitialDirectory = Path.GetDirectoryName(doc.Filename);
                                }
                                else
                                {
                                    sd.InitialDirectory =
                                        Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
                                }

                                sd.ShowDialog();

                                if (string.IsNullOrEmpty(sd.FileName))
                                {
                                    return;
                                }

                                try
                                {
                                    var wc = new WebClient();
                                    await wc.DownloadFileTaskAsync(new Uri(imageLink), sd.FileName);

                                    string filename;
                                    if (doc.Filename.Equals("untitled", StringComparison.OrdinalIgnoreCase))
                                    {
                                        filename = doc.LastImageFolder; // let doc figure out the path
                                    }
                                    else
                                    {
                                        filename = FileUtils.GetRelativePath(sd.FileName,
                                                                             Path.GetDirectoryName(doc.Filename));
                                    }

                                    // group hold just the filename
                                    var group = match.Groups[1];
                                    editor.SetSelectionRange(pos.row, group.Index, pos.row,
                                                             group.Index + group.Value.Length);
                                    editor.SetSelection(WebUtility.UrlEncode(filename));
                                }
                                catch (Exception ex)
                                {
                                    Model.Window.ShowStatusError($"Image failed to download: {ex.Message}");
                                }
                            };
                            ContextMenu.Items.Add(mi);
                        }


                        return(true);
                    }
                }
            }

            return(false);
        }
Exemplo n.º 5
0
        private bool CheckForHyperLink(string line, AcePosition pos)
        {
            var matches = HrefRegex.Matches(line);

            if (matches.Count > 0)
            {
                foreach (Match match in matches)
                {
                    string val = match.Value;
                    if (match.Index <= pos.column && match.Index + val.Length > pos.column)
                    {
                        var url          = StringUtils.ExtractString(val, "](", ")");
                        var editorSyntax = mmFileUtils.GetEditorSyntaxFromFileType(url);

                        MenuItem mi2;
                        if (url.Contains("(http"))
                        {
                            mi2 = new MenuItem {
                                Header = "Navigate Hyperlink"
                            };
                            mi2.Click += (o, args) =>
                            {
                                if (!string.IsNullOrEmpty(url))
                                {
                                    try
                                    {
                                        ShellUtils.GoUrl(url);
                                    }
                                    catch
                                    {
                                        Model.Window.ShowStatusError("Invalid link: Couldn't navigate to " + url);
                                    }
                                }
                            };
                            ContextMenu.Items.Add(mi2);
                        }
                        else if (!string.IsNullOrEmpty(editorSyntax))
                        {
                            mi2 = new MenuItem {
                                Header = "Open Document in new Tab"
                            };
                            mi2.Click += (o, args) =>
                            {
                                if (!string.IsNullOrEmpty(url))
                                {
                                    if (!url.Contains(":/"))
                                    {
                                        url = Path.Combine(Path.GetDirectoryName(Model.ActiveDocument.Filename), url);
                                    }

                                    try
                                    {
                                        Model.Window.ActivateTab(url, openIfNotFound: true);
                                    }
                                    catch
                                    {
                                        Model.Window.ShowStatusError("Invalid link: Couldn't open " + url);
                                    }
                                }
                            };
                            ContextMenu.Items.Add(mi2);
                        }


                        mi2 = new MenuItem
                        {
                            Header = "Edit Hyperlink"
                        };
                        mi2.Click += (o, args) =>
                        {
                            Model.ActiveEditor.AceEditor.SetSelectionRange(pos.row, match.Index, pos.row,
                                                                           match.Index + val.Length);
                            Model.ActiveEditor.EditorSelectionOperation("hyperlink", val);
                        };
                        ContextMenu.Items.Add(mi2);

                        if (val.Contains("](#"))
                        {
                            mi2 = new MenuItem
                            {
                                Header = "Jump to Anchor"
                            };
                            mi2.Click += (o, args) =>
                            {
                                var anchor = StringUtils.ExtractString(val, "](#", ")");

                                var docModel = new DocumentOutlineModel();
                                int lineNo   = docModel.FindHeaderHeadline(Model.ActiveEditor?.GetMarkdown(), anchor);

                                if (lineNo != -1)
                                {
                                    Model.ActiveEditor.GotoLine(lineNo);
                                }
                            };
                            ContextMenu.Items.Add(mi2);
                        }

                        var mi = new MenuItem
                        {
                            Header = "Remove Hyperlink"
                        };
                        mi.Click += (o, args) =>
                        {
                            Model.ActiveEditor.AceEditor.SetSelectionRange(pos.row, match.Index, pos.row,
                                                                           match.Index + val.Length);
                            string text = StringUtils.ExtractString(val, "[", "]");
                            Model.ActiveEditor.SetSelection(text);
                        };
                        ContextMenu.Items.Add(mi);

                        return(true);
                    }
                }
            }
            return(false);
        }
Exemplo n.º 6
0
        private bool CheckForTable(string line, AcePosition pos)
        {
            if (string.IsNullOrEmpty(line))
            {
                return(false);
            }

            if (line.Contains("|") ||
                line.Trim().StartsWith("+-") && line.Trim().EndsWith("-+"))

            {
                var mi = new MenuItem
                {
                    Header = "Edit Table"
                };
                mi.Click += (o, args) =>
                {
                    var editor = Model.ActiveEditor;

                    var lineText = editor?.GetCurrentLine();
                    if (string.IsNullOrEmpty(lineText) ||
                        !(lineText.Contains("|") &&
                          !(lineText.Trim().StartsWith("+") && lineText.Trim().EndsWith("+"))))
                    {
                        return;
                    }

                    var startPos = editor.GetCursorPosition();
                    var row      = startPos.row;
                    var startRow = row;
                    for (int i = row - 1; i > -1; i--)
                    {
                        lineText = editor.GetLine(i);
                        if (!lineText.Contains("|") &&
                            !(lineText.Trim().StartsWith("+") && lineText.Trim().EndsWith("+")))
                        {
                            startRow = i + 1;
                            break;
                        }

                        if (i == 0)
                        {
                            startRow = 0;
                            break;
                        }
                    }

                    var endRow = startPos.row;
                    for (int i = row + 1; i < 99999999; i++)
                    {
                        lineText = editor.GetLine(i);
                        if (!lineText.Contains("|") &&
                            !(lineText.Trim().StartsWith("+") && lineText.Trim().EndsWith("+")))
                        {
                            endRow = i - 1;
                            break;
                        }
                    }

                    if (endRow == startRow || endRow < startRow + 2)
                    {
                        return;
                    }

                    StringBuilder sb = new StringBuilder();
                    for (int i = startRow; i <= endRow; i++)
                    {
                        sb.AppendLine(editor.GetLine(i));
                    }

                    // select the entire table
                    Model.ActiveEditor.AceEditor.SetSelectionRange(startRow, 0, endRow + 1, 0);

                    Model.ActiveEditor.EditorSelectionOperation("table", sb.ToString());
                };
                ContextMenu.Items.Add(mi);
                return(true);
            }
            else if (line.Trim().StartsWith("<td ", StringComparison.InvariantCultureIgnoreCase) ||
                     line.Trim().StartsWith("<tr ", StringComparison.InvariantCultureIgnoreCase) ||
                     line.Trim().StartsWith("<th ", StringComparison.InvariantCultureIgnoreCase) ||
                     line.Trim().StartsWith("<table>", StringComparison.InvariantCultureIgnoreCase) ||
                     line.Trim().StartsWith("<table ", StringComparison.InvariantCultureIgnoreCase) ||
                     line.Trim().Equals("<thead>", StringComparison.InvariantCultureIgnoreCase) ||
                     line.Trim().Equals("<tbody>", StringComparison.InvariantCultureIgnoreCase))
            {
                var mi = new MenuItem
                {
                    Header = "Edit Table"
                };
                mi.Click += (o, args) =>
                {
                    var editor = Model.ActiveEditor;

                    var lineText = editor.GetCurrentLine();

                    var startPos = editor.GetCursorPosition();
                    var row      = startPos.row;
                    var startRow = -1;

                    for (int i = row - 1; i > -1; i--)
                    {
                        lineText = editor.GetLine(i);
                        if (lineText.Trim().StartsWith("<table", StringComparison.InvariantCultureIgnoreCase))
                        {
                            startRow = i;
                            break;
                        }
                    }

                    if (startRow == -1)
                    {
                        return;
                    }

                    var endRow = startRow;
                    for (int i = row + 1; i < 99999999; i++)
                    {
                        lineText = editor.GetLine(i);
                        if (lineText.Trim().Equals("</table>", StringComparison.InvariantCultureIgnoreCase))
                        {
                            endRow = i;
                            break;
                        }
                    }

                    if (endRow == startRow)
                    {
                        return;
                    }

                    StringBuilder sb = new StringBuilder();
                    for (int i = startRow; i <= endRow; i++)
                    {
                        sb.AppendLine(editor.GetLine(i));
                    }

                    // select the entire table
                    Model.ActiveEditor.AceEditor.SetSelectionRange(startRow - 1, 0, endRow + 1, 0);

                    Model.ActiveEditor.EditorSelectionOperation("table", sb.ToString());
                };
                ContextMenu.Items.Add(mi);
                return(true);
            }

            return(false);
        }
Exemplo n.º 7
0
        private bool CheckForHyperLink(string line, AcePosition pos)
        {
            var matches = HrefRegex.Matches(line);

            if (matches.Count > 0)
            {
                foreach (Match match in matches)
                {
                    string val = match.Value;
                    if (match.Index <= pos.column && match.Index + val.Length > pos.column)
                    {
                        var mi2 = new MenuItem
                        {
                            Header = "Edit Hyperlink"
                        };
                        mi2.Click += (o, args) =>
                        {
                            Model.ActiveEditor.AceEditor.SetSelectionRange(pos.row, match.Index, pos.row,
                                                                           match.Index + val.Length);
                            Model.ActiveEditor.EditorSelectionOperation("hyperlink", val);
                        };
                        ContextMenu.Items.Add(mi2);

                        if (val.Contains("](#"))
                        {
                            mi2 = new MenuItem
                            {
                                Header = "Jump to Anchor"
                            };
                            mi2.Click += (o, args) =>
                            {
                                var anchor = StringUtils.ExtractString(val, "](#", ")");

                                var docModel = new DocumentOutlineModel();
                                int lineNo   = docModel.FindHeaderHeadline(Model.ActiveEditor?.GetMarkdown(), anchor);

                                if (lineNo != -1)
                                {
                                    Model.ActiveEditor.GotoLine(lineNo);
                                }
                            };
                            ContextMenu.Items.Add(mi2);
                        }

                        var mi = new MenuItem
                        {
                            Header = "Remove Hyperlink"
                        };
                        mi.Click += (o, args) =>
                        {
                            Model.ActiveEditor.AceEditor.SetSelectionRange(pos.row, match.Index, pos.row,
                                                                           match.Index + val.Length);
                            string text = StringUtils.ExtractString(val, "[", "]");
                            Model.ActiveEditor.SetSelection(text);
                        };
                        ContextMenu.Items.Add(mi);

                        return(true);
                    }
                }
            }
            return(false);
        }
Exemplo n.º 8
0
        private bool CheckForTable(string line, AcePosition pos)
        {
            if (string.IsNullOrEmpty(line))
            {
                return(false);
            }

            if (line.Trim().StartsWith("|") && line.Trim().EndsWith("|") ||
                line.Trim().StartsWith("+-") && line.Trim().EndsWith("-+"))

            {
                var mi = new MenuItem
                {
                    Header = "Edit Table"
                };
                mi.Click += (o, args) =>
                {
                    var editor = Model.ActiveEditor;

                    var lineText = editor.GetCurrentLine();
                    if (!(lineText.Trim().StartsWith("|") && lineText.Trim().EndsWith("|") ||
                          lineText.Trim().StartsWith("+") && lineText.Trim().EndsWith("+")))
                    {
                        return;
                    }

                    var startPos = editor.GetCursorPosition();
                    var row      = startPos.row;
                    var startRow = row;
                    for (int i = row - 1; i > -1; i--)
                    {
                        lineText = editor.GetLine(i);
                        if (!(lineText.Trim().StartsWith("|") && lineText.Trim().EndsWith("|") ||
                              lineText.Trim().StartsWith("+") && lineText.Trim().EndsWith("+")))
                        {
                            startRow = i + 1;
                            break;
                        }
                    }

                    var endRow = startPos.row;
                    for (int i = row + 1; i < 99999999; i++)
                    {
                        lineText = editor.GetLine(i);
                        if (!(lineText.Trim().StartsWith("|") && lineText.Trim().EndsWith("|") ||
                              lineText.Trim().StartsWith("+") && lineText.Trim().EndsWith("+")))
                        {
                            endRow = i - 1;
                            break;
                        }
                    }

                    if (endRow == startRow)
                    {
                        return;
                    }

                    StringBuilder sb = new StringBuilder();
                    for (int i = startRow; i <= endRow; i++)
                    {
                        sb.AppendLine(editor.GetLine(i));
                    }

                    // select the entire table
                    Model.ActiveEditor.AceEditor.SetSelectionRange(startRow - 1, 0, endRow + 1, 0, pos);

                    Model.ActiveEditor.EditorSelectionOperation("table", sb.ToString());
                };
                ContextMenu.Items.Add(mi);
                return(true);
            }
            else if (line.Trim().StartsWith("<td>") && line.Trim().EndsWith("</td>") ||
                     line.Trim().StartsWith("<tr>") && line.Trim().EndsWith("</tr>") ||
                     line.Trim().StartsWith("<th>") && line.Trim().EndsWith("</th>") ||
                     line.Trim() == "<table>" || line.Trim() == "<thead>")
            {
                var mi = new MenuItem
                {
                    Header = "Edit Table"
                };
                mi.Click += (o, args) =>
                {
                    var editor = Model.ActiveEditor;

                    var lineText = editor.GetCurrentLine();

                    var startPos = editor.GetCursorPosition();
                    var row      = startPos.row;
                    var startRow = -1;

                    for (int i = row - 1; i > -1; i--)
                    {
                        lineText = editor.GetLine(i);
                        if (lineText.Trim() == "<table>")
                        {
                            startRow = i;
                            break;
                        }
                    }

                    if (startRow == -1)
                    {
                        return;
                    }

                    var endRow = startRow;
                    for (int i = row + 1; i < 99999999; i++)
                    {
                        lineText = editor.GetLine(i);
                        if (lineText.Trim() == "</table>")
                        {
                            endRow = i;
                            break;
                        }
                    }

                    if (endRow == startRow)
                    {
                        return;
                    }

                    StringBuilder sb = new StringBuilder();
                    for (int i = startRow; i <= endRow; i++)
                    {
                        sb.AppendLine(editor.GetLine(i));
                    }

                    MessageBox.Show(sb.ToString());
                    // select the entire table
                    Model.ActiveEditor.AceEditor.SetSelectionRange(startRow - 1, 0, endRow + 1, 0, pos);

                    Model.ActiveEditor.EditorSelectionOperation("table", sb.ToString());
                };
                ContextMenu.Items.Add(mi);
                return(true);
            }

            return(false);
        }