Skip to content

Commit

Permalink
Add Edit Hyperlink and Remove Hyperlink options to the Context menu.
Browse files Browse the repository at this point in the history
  • Loading branch information
RickStrahl committed Sep 5, 2017
1 parent b0b7f79 commit 4d4b891
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 10 deletions.
9 changes: 9 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
> #### Upgrades: Explicitly uninstall pre-1.4.10 Versions!
> If you have versions **prior to 1.4.10** installed, we recommend you do a **full uninstall using Programs and Features** before updating Markdown Monster. Because the install location changed, the new low rights installer can run into permission problems updating in the old **Program Files** location. A manual Uninstall/Re-install ensures that files get moved to **%LocalAppData%** which doesn't require admin privileges.
### 1.5.15
*<small>not released yet</small>*

* **Edit and Remove Hyperlink Context Menu Options**
Added menu options to edit hyper links in the link editor or to remove the hyperlink and just retain the text of the link.

* **Fix: Command Refactoring**
The various Command objects used to define menu options have been refactored in the code with seperate configuration wrapper methods to make it easier to find and edit the menu options.

### 1.5.12
*<small>September 1st, 2017</small>*

Expand Down
74 changes: 66 additions & 8 deletions MarkdownMonster/Controls/EditorContextMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ public void AddCopyPaste()
ContextMenu.Items.Add(new Separator());
}

/// <summary>
/// Adds context sensitive links for Image Links, Hyper Links
/// </summary>
public void AddEditorContext()
{
var model = Model;
Expand All @@ -137,8 +140,66 @@ public void AddEditorContext()
if (pos.row == -1)
return;

CheckForImageLink(line, pos);
CheckForHyperLink(line, pos);


if (ContextMenu.Items.Count > 0)
ContextMenu.Items.Add(new Separator());

}


static readonly Regex HrefRegex = new Regex(@"(?<!\!)\[.*?\]\(.*?\)", RegexOptions.IgnoreCase);

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;
}

static readonly Regex ImageRegex = new Regex(@"!\[.*?\]\(.*?\)", RegexOptions.IgnoreCase);

private bool CheckForImageLink(string line, AcePosition pos)
{
// Check for images ![](imageUrl)
var matches = Regex.Matches(line, @"!\[.*?\]\(.*?\)", RegexOptions.IgnoreCase);
var matches = ImageRegex.Matches(line);
if (matches.Count > 0)
{
foreach (Match match in matches)
Expand All @@ -154,7 +215,7 @@ public void AddEditorContext()
{
var image = StringUtils.ExtractString(val, "(", ")");
image = mmFileUtils.NormalizeFilename(image,
Path.GetDirectoryName(model.ActiveDocument.Filename));
Path.GetDirectoryName(Model.ActiveDocument.Filename));
mmFileUtils.OpenImageInImageEditor(image);
};
ContextMenu.Items.Add(mi);
Expand All @@ -170,17 +231,14 @@ public void AddEditorContext()
Model.ActiveEditor.EditorSelectionOperation("image", val);
};
ContextMenu.Items.Add(mi2);
}

if (ContextMenu.Items.Count > 0)
ContextMenu.Items.Add(new Separator());
return true;
}

return;
}
}

return false;
}


}
}
25 changes: 23 additions & 2 deletions MarkdownMonster/_Classes/MarkdownDocumentEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1160,9 +1160,30 @@ public string EditorSelectionOperation(string action, string text)
}
}
}
else if (action == "link")
else if (action == "hyperlink")
{
MessageBox.Show("Link to Edit", text);
string label = StringUtils.ExtractString(text, "[", "]");
string link = StringUtils.ExtractString(text, "](", ")");

var form = new PasteHref()
{
Owner = Window,
Link = link,
LinkText = label,
MarkdownFile = MarkdownDocument.Filename
};

bool? res = form.ShowDialog();
if (res != null && res.Value)
{
string html = $"[{form.LinkText}]({form.Link})";
if (!string.IsNullOrEmpty(html))
{
SetSelection(html);
PreviewMarkdownCallback();
}
}

}
else if (action == "code")
{
Expand Down

0 comments on commit 4d4b891

Please sign in to comment.