Exemplo n.º 1
0
        private async void OnPaste(object sender, TextControlPasteEventArgs e)
        {
            // If the user tries to paste RTF content from any TOM control (Visual Studio, Word, Wordpad, browsers)
            // we have to handle the pasting operation manually to allow plaintext only.
            var package = Clipboard.GetContent();

            if (package.Contains(StandardDataFormats.Text) && package.Contains("Rich Text Format"))
            {
                e.Handled = true;

                var formats = package.AvailableFormats.ToList();
                var text    = await package.GetTextAsync();

                var result = Emoticon.Pattern.Replace(text, (match) =>
                {
                    var emoticon = match.Groups[1].Value;
                    var emoji    = Emoticon.Replace(emoticon);
                    if (match.Value.StartsWith(" "))
                    {
                        emoji = $" {emoji}";
                    }

                    return(emoji);
                });

                Document.Selection.SetText(TextSetOptions.None, result);
            }
            else if (package.Contains(StandardDataFormats.StorageItems))
            {
                e.Handled = true;
            }
            else if (package.Contains(StandardDataFormats.Bitmap))
            {
                e.Handled = true;

                var bitmap = await package.GetBitmapAsync();

                var cache = await ApplicationData.Current.LocalFolder.CreateFileAsync("temp\\paste.jpg", CreationCollisionOption.ReplaceExisting);

                using (var stream = await bitmap.OpenReadAsync())
                    using (var reader = new DataReader(stream))
                    {
                        await reader.LoadAsync((uint)stream.Size);

                        var buffer = new byte[(int)stream.Size];
                        reader.ReadBytes(buffer);
                        await FileIO.WriteBytesAsync(cache, buffer);
                    }

                ViewModel.SendPhotoCommand.Execute(new StoragePhoto(cache));
            }
            else if (package.Contains(StandardDataFormats.Text) && package.Contains("application/x-tl-field-tags"))
            {
                // This is our field format
            }
            else if (package.Contains(StandardDataFormats.Text) && package.Contains("application/x-td-field-tags"))
            {
                // This is Telegram Desktop mentions format
            }
        }
Exemplo n.º 2
0
        public void Transform_WithSmiley_TransformsSmiley()
        {
            //arrange
            var emoticon = new Emoticon("[:'(]", "<img src=\"{0}\" />");

            //act
            string result = emoticon.Replace("[:'(]", "http://example.com/");

            //assert
            Assert.AreEqual(@"<img src=""http://example.com/"" />", result);
        }
Exemplo n.º 3
0
        private async void OnPaste(object sender, TextControlPasteEventArgs e)
        {
            // If the user tries to paste RTF content from any TOM control (Visual Studio, Word, Wordpad, browsers)
            // we have to handle the pasting operation manually to allow plaintext only.
            var package = Clipboard.GetContent();

            if (package.AvailableFormats.Contains(StandardDataFormats.Bitmap))
            {
                if (e != null)
                {
                    e.Handled = true;
                }

                await ViewModel.HandlePackageAsync(package);
            }
            //else if (package.AvailableFormats.Contains(StandardDataFormats.WebLink))
            //{

            //}
            else if (package.AvailableFormats.Contains(StandardDataFormats.StorageItems))
            {
                if (e != null)
                {
                    e.Handled = true;
                }

                await ViewModel.HandlePackageAsync(package);
            }
            else if (package.AvailableFormats.Contains(StandardDataFormats.Text) && package.AvailableFormats.Contains("application/x-tl-field-tags"))
            {
                if (e != null)
                {
                    e.Handled = true;
                }

                // This is our field format
                var text = await package.GetTextAsync();

                var data = await package.GetDataAsync("application/x-tl-field-tags") as IRandomAccessStream;

                var reader = new DataReader(data.GetInputStreamAt(0));
                var length = await reader.LoadAsync((uint)data.Size);

                var count    = reader.ReadInt32();
                var entities = new List <TextEntity>(count);

                for (int i = 0; i < count; i++)
                {
                    var entity = new TextEntity {
                        Offset = reader.ReadInt32(), Length = reader.ReadInt32()
                    };
                    var type = reader.ReadByte();

                    switch (type)
                    {
                    case 1:
                        entity.Type = new TextEntityTypeBold();
                        break;

                    case 2:
                        entity.Type = new TextEntityTypeItalic();
                        break;

                    case 3:
                        entity.Type = new TextEntityTypePreCode();
                        break;

                    case 4:
                        entity.Type = new TextEntityTypeTextUrl {
                            Url = reader.ReadString(reader.ReadUInt32())
                        };
                        break;

                    case 5:
                        entity.Type = new TextEntityTypeMentionName {
                            UserId = reader.ReadInt32()
                        };
                        break;
                    }

                    entities.Add(entity);
                }

                InsertText(text, entities);
            }
            else if (package.AvailableFormats.Contains(StandardDataFormats.Text) && package.AvailableFormats.Contains("application/x-td-field-tags"))
            {
                // This is Telegram Desktop mentions format
            }
            else if (package.AvailableFormats.Contains(StandardDataFormats.Text) /*&& package.Contains("Rich Text Format")*/)
            {
                if (e != null)
                {
                    e.Handled = true;
                }

                var text = await package.GetTextAsync();

                var start = Document.Selection.StartPosition;

                var result = Emoticon.Pattern.Replace(text, (match) =>
                {
                    var emoticon = match.Groups[1].Value;
                    var emoji    = Emoticon.Replace(emoticon);
                    if (match.Value.StartsWith(" "))
                    {
                        emoji = $" {emoji}";
                    }

                    return(emoji);
                });

                Document.Selection.SetText(TextSetOptions.None, result);
                Document.Selection.SetRange(start + result.Length, start + result.Length);
            }
        }
Exemplo n.º 4
0
        private async void OnPaste(object sender, TextControlPasteEventArgs e)
        {
            // If the user tries to paste RTF content from any TOM control (Visual Studio, Word, Wordpad, browsers)
            // we have to handle the pasting operation manually to allow plaintext only.
            var package = Clipboard.GetContent();

            if (package.AvailableFormats.Contains(StandardDataFormats.Bitmap))
            {
                if (e != null)
                {
                    e.Handled = true;
                }

                var bitmap = await package.GetBitmapAsync();

                var media = new ObservableCollection <StorageMedia>();

                var fileName = string.Format("image_{0:yyyy}-{0:MM}-{0:dd}_{0:HH}-{0:mm}-{0:ss}.png", DateTime.Now);
                var cache    = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(fileName, CreationCollisionOption.GenerateUniqueName);

                using (var stream = await bitmap.OpenReadAsync())
                {
                    var result = await ImageHelper.TranscodeAsync(stream, cache, BitmapEncoder.PngEncoderId);

                    var photo = await StoragePhoto.CreateAsync(result, true);

                    if (photo == null)
                    {
                        return;
                    }

                    media.Add(photo);
                }

                if (package.AvailableFormats.Contains(StandardDataFormats.Text))
                {
                    media[0].Caption = new FormattedText(await package.GetTextAsync(), new TextEntity[0]);
                }

                ViewModel.SendMediaExecute(media, media[0]);
            }
            else if (package.AvailableFormats.Contains(StandardDataFormats.WebLink))
            {
            }
            else if (package.AvailableFormats.Contains(StandardDataFormats.StorageItems))
            {
                if (e != null)
                {
                    e.Handled = true;
                }

                var items = await package.GetStorageItemsAsync();

                var media = new ObservableCollection <StorageMedia>();
                var files = new List <StorageFile>(items.Count);

                foreach (var file in items.OfType <StorageFile>())
                {
                    if (file.ContentType.Equals("image/jpeg", StringComparison.OrdinalIgnoreCase) ||
                        file.ContentType.Equals("image/png", StringComparison.OrdinalIgnoreCase) ||
                        file.ContentType.Equals("image/bmp", StringComparison.OrdinalIgnoreCase) ||
                        file.ContentType.Equals("image/gif", StringComparison.OrdinalIgnoreCase))
                    {
                        var photo = await StoragePhoto.CreateAsync(file, true);

                        if (photo != null)
                        {
                            media.Add(photo);
                        }
                    }
                    else if (file.ContentType == "video/mp4")
                    {
                        var video = await StorageVideo.CreateAsync(file, true);

                        if (video != null)
                        {
                            media.Add(video);
                        }
                    }

                    files.Add(file);
                }

                // Send compressed __only__ if user is dropping photos and videos only
                if (media.Count > 0 && media.Count == files.Count)
                {
                    ViewModel.SendMediaExecute(media, media[0]);
                }
                else if (files.Count > 0)
                {
                    ViewModel.SendFileExecute(files);
                }
            }
            else if (package.AvailableFormats.Contains(StandardDataFormats.Text) && package.AvailableFormats.Contains("application/x-tl-field-tags"))
            {
                if (e != null)
                {
                    e.Handled = true;
                }

                // This is our field format
                var text = await package.GetTextAsync();

                var data = await package.GetDataAsync("application/x-tl-field-tags") as IRandomAccessStream;

                var reader = new DataReader(data.GetInputStreamAt(0));
                var length = await reader.LoadAsync((uint)data.Size);

                var count    = reader.ReadInt32();
                var entities = new List <TextEntity>(count);

                for (int i = 0; i < count; i++)
                {
                    var entity = new TextEntity {
                        Offset = reader.ReadInt32(), Length = reader.ReadInt32()
                    };
                    var type = reader.ReadByte();

                    switch (type)
                    {
                    case 1:
                        entity.Type = new TextEntityTypeBold();
                        break;

                    case 2:
                        entity.Type = new TextEntityTypeItalic();
                        break;

                    case 3:
                        entity.Type = new TextEntityTypePreCode();
                        break;

                    case 4:
                        entity.Type = new TextEntityTypeTextUrl {
                            Url = reader.ReadString(reader.ReadUInt32())
                        };
                        break;

                    case 5:
                        entity.Type = new TextEntityTypeMentionName {
                            UserId = reader.ReadInt32()
                        };
                        break;
                    }

                    entities.Add(entity);
                }

                InsertText(text, entities);
            }
            else if (package.AvailableFormats.Contains(StandardDataFormats.Text) && package.AvailableFormats.Contains("application/x-td-field-tags"))
            {
                // This is Telegram Desktop mentions format
            }
            else if (package.AvailableFormats.Contains(StandardDataFormats.Text) /*&& package.Contains("Rich Text Format")*/)
            {
                if (e != null)
                {
                    e.Handled = true;
                }

                var text = await package.GetTextAsync();

                var start = Document.Selection.StartPosition;

                var result = Emoticon.Pattern.Replace(text, (match) =>
                {
                    var emoticon = match.Groups[1].Value;
                    var emoji    = Emoticon.Replace(emoticon);
                    if (match.Value.StartsWith(" "))
                    {
                        emoji = $" {emoji}";
                    }

                    return(emoji);
                });

                Document.Selection.SetText(TextSetOptions.None, result);
                Document.Selection.SetRange(start + result.Length, start + result.Length);
            }
        }