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() { 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); } }