Permalink
Browse files

Merge pull request #359 from Shazbot/pasteURL

Feature #350: URL pasting into document
  • Loading branch information...
2 parents 583f18c + 5e15633 commit 2164c3b3410023e16f59285b748ecef58f0646bd @JakeGinnivan JakeGinnivan committed Mar 23, 2013
@@ -55,6 +55,7 @@ public MarkdownEditor()
editorPreviewKeyDownHandlers = new IHandle<EditorPreviewKeyDownEvent>[] {
new CopyLeadingWhitespaceOnNewLine(),
new PasteImageIntoDocument(),
+ new PasteURLIntoDocument(),
new ControlRightTweakedForMarkdown(),
new HardLineBreak(),
overtypeMode,
@@ -0,0 +1,47 @@
+using System;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Text.RegularExpressions;
+using System.Windows;
+using System.Windows.Input;
+using Caliburn.Micro;
+using MarkPad.Document.Events;
+using MarkPad.Helpers;
+
+namespace MarkPad.Document.EditorBehaviours
+{
+ public class PasteURLIntoDocument : IHandle<EditorPreviewKeyDownEvent>
+ {
+ // "URL: Find in full text (protocol optional)" from the RegexBuddy library, with ^$ anchors added
+ readonly Regex URLInTextRegex = new Regex(@"^\b((?:(?:https?|ftp|file)://|www\.|ftp\.)[-A-Z0-9+&@#/%=~_|$?!:,.]*[A-Z0-9+&@#/%=~_|$]$)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
+
+ public void Handle(EditorPreviewKeyDownEvent e)
+ {
+ if (e.ViewModel == null) return;
+ if (Keyboard.Modifiers != ModifierKeys.Control || e.Args.Key != Key.V) return;
+
+ var pastedText = Clipboard.GetText();
+ var match = URLInTextRegex.Match(pastedText);
+ if (!match.Success) return;
+
+ if (e.Editor.SelectionStart != 0 && (e.Editor.SelectionStart + e.Editor.SelectionLength) != e.Editor.Document.TextLength) // check if at beginning or end of document
+ {
+ if (e.Editor.Document.GetCharAt(e.Editor.SelectionStart - 1) == '[' && e.Editor.Document.GetCharAt(e.Editor.SelectionStart + e.Editor.SelectionLength) == ']')
+ {
+ return;
+ }
+ if (e.Editor.Document.GetCharAt(e.Editor.SelectionStart - 1) == '"' && e.Editor.Document.GetCharAt(e.Editor.SelectionStart + e.Editor.SelectionLength) == '"')
+ {
+ return;
+ }
+ }
+
+ var oldSelectionStart = e.Editor.SelectionStart;
+ e.Editor.TextArea.Selection.ReplaceSelectionWithText(match.Result("[$1]($1)"));
+ e.Editor.SelectionStart = oldSelectionStart + match.Index + 1;
+ e.Editor.SelectionLength = match.Length;
+ e.Args.Handled = true;
+ }
+ }
+}
@@ -175,6 +175,7 @@
<Compile Include="DocumentSources\OpenDocumentFromWebResult.cs" />
<Compile Include="DocumentSources\FileSystem\SingleFileContext.cs" />
<Compile Include="DocumentSources\WebSources\SaveResult.cs" />
+ <Compile Include="Document\EditorBehaviours\PasteURLIntoDocument.cs" />
<Compile Include="Infrastructure\AwaitableDelegateCommand.cs" />
<Compile Include="Infrastructure\AwaitableDelegateCommand`1.cs" />
<Compile Include="Infrastructure\DelegateCommand.cs" />
@@ -336,7 +336,7 @@
</i:Interaction.Triggers>
</Button>
<Button x:Name="PrintDocument" Content="print" Style="{StaticResource OpenDocumentsOnly}" />
- <Button x:Name="PublishDocument" Content="publish" Style="{StaticResource OpenDocumentsOnly}" />
+ <Button x:Name="PublishDocument" Content="publish" Style="{StaticResource OpenDocumentsOnly}" Command="{Binding PublishDocumentCommand}" />
</WrapPanel>
<WrapPanel x:Name="NewItems" Margin="30,0,0,0" Opacity="0.995" VerticalAlignment="Bottom" Height="20" RenderTransformOrigin="0.5,0.5"
d:LayoutOverrides="VerticalAlignment, GridBox">
@@ -1,4 +1,5 @@
-using Markpad.UITests.Infrastructure;
+using System.Windows.Forms;
+using Markpad.UITests.Infrastructure;
using White.Core.WindowsAPI;
using Xunit;
@@ -99,6 +100,53 @@ public void TabIndentsListWithTabs()
Assert.Equal(listContinued, editor.MarkdownText);
}
+
+ [Fact]
+ public void CreateLinkFromPastedURL()
+ {
+ const string textToPaste = "http://www.google.com";
+ var document = MainWindow.NewDocument();
+ var editor = document.Editor();
+
+ Clipboard.SetText(textToPaste);
+ document.PasteClipboard();
+
+ Assert.Equal("[http://www.google.com](http://www.google.com)", editor.MarkdownText);
+
+ // test caret position and text selection by pressing backspace
+ editor.PressKey(KeyboardInput.SpecialKeys.BACKSPACE);
+ Assert.Equal("[](http://www.google.com)", editor.MarkdownText);
+ }
+
+ [Fact]
+ public void DontCreateLinkWhenPastingURLInsideExistingLink()
+ {
+ const string textToPaste = "http://www.google.com";
+ var document = MainWindow.NewDocument();
+ var editor = document.Editor();
+ editor.MarkdownText = "[](http://www.google.com)";
+
+ editor.PressKey(KeyboardInput.SpecialKeys.RIGHT);
+ Clipboard.SetText(textToPaste);
+ document.PasteClipboard();
+
+ Assert.Equal("[http://www.google.com](http://www.google.com)", editor.MarkdownText);
+ }
+
+ [Fact]
+ public void DontCreateLinkWhenPastingURLInsideQuotationMarks()
+ {
+ const string textToPaste = "http://www.google.com";
+ var document = MainWindow.NewDocument();
+ var editor = document.Editor();
+ editor.MarkdownText = "\"\"";
+
+ editor.PressKey(KeyboardInput.SpecialKeys.RIGHT);
+ Clipboard.SetText(textToPaste);
+ document.PasteClipboard();
+
+ Assert.Equal("\"http://www.google.com\"", editor.MarkdownText);
+ }
}
}
}
@@ -12,6 +12,10 @@ public MarkpadFixture()
var directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var environmentLocation = Environment.GetEnvironmentVariable("MarkpadLocation");
var markpadLocation = environmentLocation ?? Path.Combine(directoryName, @"..\..\..\Markpad\bin\Debug\Markpad.exe");
+ if (!File.Exists(markpadLocation))
+ {
+ markpadLocation = Path.Combine(Environment.CurrentDirectory, @"..\..\..\Markpad\bin\Debug\Markpad.exe");
+ }
Application = Application.Launch(markpadLocation);
MainWindow = new MarkpadWindow(Application, Application.GetWindow("MarkPad"));
TemporaryTestFilesDirectory = Path.Combine(Path.GetTempPath(), "MarkpadTest");

0 comments on commit 2164c3b

Please sign in to comment.