public async override void Invoke() { ITextBuffer textBuffer = this.HtmlSmartTag.TextBuffer; ElementNode element = this.HtmlSmartTag.Element; AttributeNode src = element.GetAttribute("src", true); ImageCompressor compressor = new ImageCompressor(); bool isDataUri = src.Value.StartsWith("data:image/", StringComparison.Ordinal); if (isDataUri) { string dataUri = await compressor.CompressDataUriAsync(src.Value); if (dataUri.Length < src.Value.Length) { using (WebEssentialsPackage.UndoContext("Optimize image")) { Span span = Span.FromBounds(src.ValueRangeUnquoted.Start, src.ValueRangeUnquoted.End); textBuffer.Replace(span, dataUri); } } } else { var fileName = ImageQuickInfo.GetFullUrl(src.Value, textBuffer); if (string.IsNullOrEmpty(fileName) || !ImageCompressor.IsFileSupported(fileName) || !File.Exists(fileName)) { return; } await compressor.CompressFilesAsync(fileName); } }
public IEnumerable <ISmartTagAction> GetSmartTagActions(ParseItem item, int position, ITrackingSpan itemTrackingSpan, ITextView view) { UrlItem url = (UrlItem)item; if (!url.IsValid || url.UrlString == null || string.IsNullOrEmpty(url.UrlString.Text)) { yield break; } string text = url.UrlString.Text.Trim('"', '\''); string testSupportFileName = text; if (url.IsDataUri()) { string mime = FileHelpers.GetMimeTypeFromBase64(text); testSupportFileName = "file." + FileHelpers.GetExtension(mime); } if (!ImageCompressor.IsFileSupported(testSupportFileName)) { yield break; } yield return(new OptimizeImageSmartTagAction(itemTrackingSpan, url)); }
public async override void Invoke(CancellationToken cancellationToken) { ImageCompressor compressor = new ImageCompressor(); bool isDataUri = Attribute.Value.StartsWith("data:image/", StringComparison.Ordinal); if (isDataUri) { string dataUri = await compressor.CompressDataUriAsync(Attribute.Value); if (dataUri.Length < Attribute.Value.Length) { using (WebEssentialsPackage.UndoContext(this.DisplayText)) using (ITextEdit edit = TextBuffer.CreateEdit()) { Span span = Span.FromBounds(Attribute.ValueRangeUnquoted.Start, Attribute.ValueRangeUnquoted.End); edit.Replace(span, dataUri); edit.Apply(); } } } else { var fileName = ImageQuickInfo.GetFullUrl(Attribute.Value, TextBuffer); if (string.IsNullOrEmpty(fileName) || !ImageCompressor.IsFileSupported(fileName) || !File.Exists(fileName)) { return; } await compressor.CompressFilesAsync(fileName); } }
public async override void Invoke() { if (string.IsNullOrEmpty(_url.UrlString.Text)) { return; } ImageCompressor compressor = new ImageCompressor(); string url = _url.UrlString.Text.Trim('"', '\''); if (_url.IsDataUri()) { string dataUri = await compressor.CompressDataUriAsync(url); if (dataUri.Length < url.Length) { using (EditorExtensionsPackage.UndoContext("Optimize image")) { Span span = Span.FromBounds(_url.UrlString.Start, _url.UrlString.AfterEnd); _span.TextBuffer.Replace(span, "'" + dataUri + "'"); } } } else { string selection = Uri.UnescapeDataString(url); string fileName = ProjectHelpers.ToAbsoluteFilePath(selection, _span.TextBuffer.GetFileName()); if (string.IsNullOrEmpty(fileName) || !ImageCompressor.IsFileSupported(fileName) || !File.Exists(fileName)) { return; } await compressor.CompressFilesAsync(fileName); } }