|
| 1 | +import 'package:flutter/cupertino.dart'; |
| 2 | +import 'package:super_editor/super_editor.dart'; |
| 3 | + |
| 4 | +/// [DocumentNode] that represents a video at a URL. |
| 5 | +class VideoNode extends UrlMediaNode { |
| 6 | + static const videoAttribution = NamedAttribution("video"); |
| 7 | + |
| 8 | + VideoNode({ |
| 9 | + required super.id, |
| 10 | + required super.url, |
| 11 | + super.altText = '', |
| 12 | + super.blockAttribution = videoAttribution, |
| 13 | + }); |
| 14 | +} |
| 15 | + |
| 16 | +/// [DocumentNode] that represents an audio source at a URL. |
| 17 | +class AudioNode extends UrlMediaNode { |
| 18 | + static const audioAttribution = NamedAttribution("audio"); |
| 19 | + |
| 20 | + AudioNode({ |
| 21 | + required super.id, |
| 22 | + required super.url, |
| 23 | + super.altText = '', |
| 24 | + super.blockAttribution = audioAttribution, |
| 25 | + }); |
| 26 | +} |
| 27 | + |
| 28 | +/// [DocumentNode] that represents a file at a URL. |
| 29 | +class FileNode extends UrlMediaNode { |
| 30 | + static const fileAttribution = NamedAttribution("file"); |
| 31 | + |
| 32 | + FileNode({ |
| 33 | + required super.id, |
| 34 | + required super.url, |
| 35 | + super.altText = '', |
| 36 | + super.blockAttribution = fileAttribution, |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +/// [DocumentNode] that represents a media source that exists a given [url]. |
| 41 | +class UrlMediaNode extends BlockNode with ChangeNotifier { |
| 42 | + UrlMediaNode({ |
| 43 | + required this.id, |
| 44 | + required String url, |
| 45 | + String altText = '', |
| 46 | + required Attribution blockAttribution, |
| 47 | + Map<String, dynamic>? metadata, |
| 48 | + }) : _url = url, |
| 49 | + _altText = altText { |
| 50 | + this.metadata = metadata; |
| 51 | + putMetadataValue("blockType", blockAttribution); |
| 52 | + } |
| 53 | + |
| 54 | + @override |
| 55 | + final String id; |
| 56 | + |
| 57 | + String get url => _url; |
| 58 | + String _url; |
| 59 | + set url(String newUrl) { |
| 60 | + if (newUrl != _url) { |
| 61 | + _url = newUrl; |
| 62 | + notifyListeners(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + String get altText => _altText; |
| 67 | + String _altText; |
| 68 | + set altText(String newAltText) { |
| 69 | + if (newAltText != _altText) { |
| 70 | + _altText = newAltText; |
| 71 | + notifyListeners(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @override |
| 76 | + String? copyContent(dynamic selection) { |
| 77 | + if (selection is! UpstreamDownstreamNodeSelection) { |
| 78 | + throw Exception('ImageNode can only copy content from a UpstreamDownstreamNodeSelection.'); |
| 79 | + } |
| 80 | + |
| 81 | + return !selection.isCollapsed ? _url : null; |
| 82 | + } |
| 83 | + |
| 84 | + @override |
| 85 | + bool hasEquivalentContent(DocumentNode other) { |
| 86 | + return other is UrlMediaNode && url == other.url && altText == other.altText; |
| 87 | + } |
| 88 | + |
| 89 | + @override |
| 90 | + bool operator ==(Object other) => |
| 91 | + identical(this, other) || |
| 92 | + other is UrlMediaNode && |
| 93 | + runtimeType == other.runtimeType && |
| 94 | + id == other.id && |
| 95 | + _url == other._url && |
| 96 | + _altText == other._altText; |
| 97 | + |
| 98 | + @override |
| 99 | + int get hashCode => id.hashCode ^ _url.hashCode ^ _altText.hashCode; |
| 100 | +} |
0 commit comments