Skip to content

Commit

Permalink
Added SVG preview window
Browse files Browse the repository at this point in the history
  • Loading branch information
madskristensen committed Dec 17, 2013
1 parent 1e87dab commit 1e604b9
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 1 deletion.
26 changes: 26 additions & 0 deletions EditorExtensions/Classifications/SVG/SvgContentTypeDefinition.cs
@@ -0,0 +1,26 @@
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Utilities;

namespace MadsKristensen.EditorExtensions
{
/// <summary>
/// Exports the Svg content type and file extension
/// </summary>
public class SvgContentTypeDefinition
{
public const string SvgContentType = "svg";

/// <summary>
/// Exports the Svg HTML content type
/// </summary>
[Export(typeof(ContentTypeDefinition))]
[Name(SvgContentType)]
[BaseDefinition("xml")]
public ContentTypeDefinition ISvgContentType { get; set; }

[Export(typeof(FileExtensionToContentTypeDefinition))]
[ContentType(SvgContentType)]
[FileExtension(".svg")]
public FileExtensionToContentTypeDefinition SvgFileExtension { get; set; }
}
}
4 changes: 3 additions & 1 deletion EditorExtensions/Margin/EditorMarginFactory.cs
Expand Up @@ -15,6 +15,7 @@ namespace MadsKristensen.EditorExtensions
[ContentType("CoffeeScript")]
//[ContentType("TypeScript")]
[ContentType("Markdown")]
[ContentType("svg")]
[TextViewRole(PredefinedTextViewRoles.Debuggable)]
public sealed class MarginFactory : IWpfTextViewMarginProvider
{
Expand All @@ -25,7 +26,8 @@ public sealed class MarginFactory : IWpfTextViewMarginProvider
{
{ "LESS", (source, document) => new LessMargin("CSS", source, WESettings.GetBoolean(WESettings.Keys.ShowLessPreviewWindow), document) },
{ "CoffeeScript", (source, document) => new CoffeeScriptMargin("JavaScript", source, WESettings.GetBoolean(WESettings.Keys.ShowCoffeeScriptPreviewWindow), document) },
{ "Markdown", (source, document) => new MarkdownMargin("text", source, WESettings.GetBoolean(WESettings.Keys.MarkdownShowPreviewWindow), document) }
{ "Markdown", (source, document) => new MarkdownMargin("text", source, WESettings.GetBoolean(WESettings.Keys.MarkdownShowPreviewWindow), document) },
{ "Svg", (source, document) => new SvgMargin("svg", source, WESettings.GetBoolean(WESettings.Keys.SvgShowPreviewWindow), document) }
};

public IWpfTextViewMargin CreateMargin(IWpfTextViewHost wpfTextViewHost, IWpfTextViewMargin marginContainer)
Expand Down
92 changes: 92 additions & 0 deletions EditorExtensions/Margin/SvgMargin.cs
@@ -0,0 +1,92 @@
using System.IO;
using System.Windows;
using System.Windows.Controls;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;

namespace MadsKristensen.EditorExtensions
{
internal class SvgMargin : MarginBase
{
public const string MarginName = "SvgMargin";
private WebBrowser _browser;
private string _fileName;

public SvgMargin(string contentType, string source, bool showMargin, ITextDocument document)
: base(source, MarginName, contentType, showMargin, document)
{
_fileName = document.FilePath;
}

protected override void StartCompiler(string source)
{
if (_browser != null && File.Exists(_fileName))
{
_browser.Navigate(_fileName);
}
}

protected override void CreateControls(IWpfTextViewHost host, string source)
{
int width = WESettings.GetInt(SettingsKey);
width = width == -1 ? 400 : width;

_browser = new WebBrowser();
_browser.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;

Grid grid = new Grid();
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(0, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(5, GridUnitType.Pixel) });
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(width) });
grid.RowDefinitions.Add(new RowDefinition());

grid.Children.Add(_browser);
this.Children.Add(grid);

Grid.SetColumn(_browser, 2);
Grid.SetRow(_browser, 0);

GridSplitter splitter = new GridSplitter();
splitter.Width = 5;
splitter.ResizeDirection = GridResizeDirection.Columns;
splitter.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
splitter.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
splitter.DragCompleted += splitter_DragCompleted;

grid.Children.Add(splitter);
Grid.SetColumn(splitter, 1);
Grid.SetRow(splitter, 0);
}

void splitter_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
Settings.SetValue(SettingsKey, (int)this.ActualWidth);
Settings.Save();
}

public override void MinifyFile(string fileName, string source)
{
// Nothing to minify
}

public override bool IsSaveFileEnabled
{
get { return false; }
}

protected override bool CanWriteToDisk(string source)
{
return false;
}

public override bool CompileEnabled
{
get { return true; }
}

public override string CompileToLocation
{
get { return string.Empty; }
}
}
}
7 changes: 7 additions & 0 deletions EditorExtensions/Options/General.cs
Expand Up @@ -15,6 +15,7 @@ public override void SaveSettingsToStorage()
Settings.SetValue(WESettings.Keys.KeepImportantComments, KeepImportantComments);
Settings.SetValue(WESettings.Keys.EnableBrowserLinkMenu, EnableBrowserLinkMenu);
Settings.SetValue(WESettings.Keys.AllMessagesToOutputWindow, AllMessagesToOutputWindow);
Settings.SetValue(WESettings.Keys.SvgShowPreviewWindow, SvgShowPreviewWindow);

Settings.Save();
}
Expand All @@ -24,6 +25,7 @@ public override void LoadSettingsFromStorage()
KeepImportantComments = WESettings.GetBoolean(WESettings.Keys.KeepImportantComments);
EnableBrowserLinkMenu = WESettings.GetBoolean(WESettings.Keys.EnableBrowserLinkMenu);
AllMessagesToOutputWindow = WESettings.GetBoolean(WESettings.Keys.AllMessagesToOutputWindow);
SvgShowPreviewWindow = WESettings.GetBoolean(WESettings.Keys.SvgShowPreviewWindow);
}

// MISC
Expand All @@ -41,5 +43,10 @@ public override void LoadSettingsFromStorage()
[Description("Redirect messages/notifications to output window.")]
[Category("Messages")]
public bool AllMessagesToOutputWindow { get; set; }

[LocDisplayName("Show preview window")]
[Description("Shows the preview window when editing an SVG file.")]
[Category("SVG")]
public bool SvgShowPreviewWindow { get; set; }
}
}
1 change: 1 addition & 0 deletions EditorExtensions/Options/ProjectSettingsStore.cs
Expand Up @@ -234,6 +234,7 @@ private static string GetUserFilePath()
// Misc
dic.Add(Keys.EnableBrowserLinkMenu, true);
dic.Add(Keys.BrowserLink_ShowMenu, true);
dic.Add(Keys.SvgShowPreviewWindow, true);

// HTML
dic.Add(Keys.EnableEnterFormat, true);
Expand Down
3 changes: 3 additions & 0 deletions EditorExtensions/Options/WebEssentialsSettings.cs
Expand Up @@ -46,6 +46,9 @@ public static class Keys
public const string MarkdownEncodeProblemUrlCharacters = "MarkdownEncodeProblemUrlCharacters";
public const string MarkdownStrictBoldItalic = "MarkdownStrictBoldItalic";

// SVG
public const string SvgShowPreviewWindow = "SvgShowPreviewWindow";

// CSS
public const string ValidateStarSelector = "CssValidateStarSelector";
public const string ValidateOverQualifiedSelector = "CSSValidateOverQualifiedSelector";
Expand Down
2 changes: 2 additions & 0 deletions EditorExtensions/WebEssentials2013.csproj
Expand Up @@ -339,13 +339,15 @@
<Compile Include="Classifications\Markdown\MarkdownClassifier.cs" />
<Compile Include="Classifications\Markdown\MarkdownContentTypeDefinition.cs" />
<Compile Include="Classifications\CSS\ModernizrClassifier.cs" />
<Compile Include="Classifications\SVG\SvgContentTypeDefinition.cs" />
<Compile Include="Commands\CommandExceptionFilter.cs" />
<Compile Include="Completion\HTML\InputIdAttributeCompletion.cs" />
<Compile Include="Completion\Markdown\MarkdownCompletionFilter.cs" />
<Compile Include="DropTargets\HtmlImageDrop.cs" />
<Compile Include="Helpers\Disposable.cs" />
<Compile Include="ExtensionMethods\Extensions.cs" />
<Compile Include="Helpers\TabAwareCharacterStream.cs" />
<Compile Include="Margin\SvgMargin.cs" />
<Compile Include="MenuItems\AddIntellisenseFile.cs" />
<Compile Include="Commands\Code\IntellisenseWriter.cs" />
<Compile Include="Commands\Code\ScriptIntellisenseListener.cs" />
Expand Down

0 comments on commit 1e604b9

Please sign in to comment.