|
| 1 | +import 'package:web/web.dart' as web; |
| 2 | +import 'meta_tag_manager_interface.dart'; |
| 3 | + |
| 4 | +/// Creates the web implementation of meta tag manager |
| 5 | +MetaTagManagerInterface createMetaTagManager() { |
| 6 | + return MetaTagManagerWeb(); |
| 7 | +} |
| 8 | + |
| 9 | +/// Web implementation of meta tag management using package:web |
| 10 | +class MetaTagManagerWeb implements MetaTagManagerInterface { |
| 11 | + static const String baseUrl = 'https://roszkowski.dev/shaders_gallery'; |
| 12 | + |
| 13 | + @override |
| 14 | + void updateForShader({ |
| 15 | + required String shaderName, |
| 16 | + required String shaderTitle, |
| 17 | + required String description, |
| 18 | + required String author, |
| 19 | + required String imageUrl, |
| 20 | + String? sourceUrl, |
| 21 | + }) { |
| 22 | + final fullUrl = '$baseUrl/shader/$shaderName'; |
| 23 | + final fullTitle = '$shaderTitle - Flutter Shader Gallery'; |
| 24 | + final fullDescription = '$description by $author. Interactive GLSL shader gallery.'; |
| 25 | + |
| 26 | + // Update page title |
| 27 | + web.document.title = fullTitle; |
| 28 | + |
| 29 | + // Update basic meta tags |
| 30 | + _updateMetaTag('description', fullDescription); |
| 31 | + _updateMetaTag('keywords', 'shader, flutter, GLSL, $shaderName, $author, webgl, interactive, graphics'); |
| 32 | + |
| 33 | + // Update OpenGraph tags |
| 34 | + _updateMetaProperty('og:url', fullUrl); |
| 35 | + _updateMetaProperty('og:title', fullTitle); |
| 36 | + _updateMetaProperty('og:description', fullDescription); |
| 37 | + _updateMetaProperty('og:image', imageUrl); |
| 38 | + _updateMetaProperty('og:type', 'article'); |
| 39 | + |
| 40 | + // Update Twitter Card tags |
| 41 | + _updateMetaProperty('twitter:card', 'summary_large_image'); |
| 42 | + _updateMetaProperty('twitter:url', fullUrl); |
| 43 | + _updateMetaProperty('twitter:title', fullTitle); |
| 44 | + _updateMetaProperty('twitter:description', fullDescription); |
| 45 | + _updateMetaProperty('twitter:image', imageUrl); |
| 46 | + |
| 47 | + // Add article-specific tags |
| 48 | + _updateMetaProperty('article:author', author); |
| 49 | + if (sourceUrl != null) { |
| 50 | + _updateMetaProperty('article:section', 'Shaders'); |
| 51 | + _addCanonicalLink(sourceUrl); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @override |
| 56 | + void updateForHomePage() { |
| 57 | + const title = 'Flutter Shader Gallery'; |
| 58 | + const description = 'A collection of shaders made to work with Flutter'; |
| 59 | + const url = '$baseUrl/'; |
| 60 | + const imageUrl = '$baseUrl/assets/screenshots/preview.jpeg'; |
| 61 | + |
| 62 | + // Update page title |
| 63 | + web.document.title = title; |
| 64 | + |
| 65 | + // Update basic meta tags |
| 66 | + _updateMetaTag('description', description); |
| 67 | + _updateMetaTag('keywords', 'shaders, flutter, GLSL, graphics, webgl, interactive, gallery'); |
| 68 | + |
| 69 | + // Update OpenGraph tags |
| 70 | + _updateMetaProperty('og:url', url); |
| 71 | + _updateMetaProperty('og:title', title); |
| 72 | + _updateMetaProperty('og:description', description); |
| 73 | + _updateMetaProperty('og:image', imageUrl); |
| 74 | + _updateMetaProperty('og:type', 'website'); |
| 75 | + |
| 76 | + // Update Twitter Card tags |
| 77 | + _updateMetaProperty('twitter:card', 'summary_large_image'); |
| 78 | + _updateMetaProperty('twitter:url', url); |
| 79 | + _updateMetaProperty('twitter:title', title); |
| 80 | + _updateMetaProperty('twitter:description', description); |
| 81 | + _updateMetaProperty('twitter:image', imageUrl); |
| 82 | + |
| 83 | + // Remove any article-specific tags |
| 84 | + _removeMetaProperty('article:author'); |
| 85 | + _removeMetaProperty('article:section'); |
| 86 | + _removeCanonicalLink(); |
| 87 | + } |
| 88 | + |
| 89 | + @override |
| 90 | + String getShaderImageUrl(String shaderPath) { |
| 91 | + // Convert shader path to image filename |
| 92 | + final imageName = '${shaderPath.replaceAll('-', '_')}.png'; |
| 93 | + return '$baseUrl/assets/screenshots/$imageName'; |
| 94 | + } |
| 95 | + |
| 96 | + /// Updates or creates a meta tag with name attribute |
| 97 | + void _updateMetaTag(String name, String content) { |
| 98 | + final existing = web.document.querySelector('meta[name="$name"]'); |
| 99 | + if (existing != null) { |
| 100 | + existing.setAttribute('content', content); |
| 101 | + } else { |
| 102 | + final meta = web.document.createElement('meta'); |
| 103 | + meta.setAttribute('name', name); |
| 104 | + meta.setAttribute('content', content); |
| 105 | + web.document.head!.appendChild(meta); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /// Updates or creates a meta tag with property attribute (for OpenGraph) |
| 110 | + void _updateMetaProperty(String property, String content) { |
| 111 | + final existing = web.document.querySelector('meta[property="$property"]'); |
| 112 | + if (existing != null) { |
| 113 | + existing.setAttribute('content', content); |
| 114 | + } else { |
| 115 | + final meta = web.document.createElement('meta'); |
| 116 | + meta.setAttribute('property', property); |
| 117 | + meta.setAttribute('content', content); |
| 118 | + web.document.head!.appendChild(meta); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /// Removes a meta tag with property attribute |
| 123 | + void _removeMetaProperty(String property) { |
| 124 | + final existing = web.document.querySelector('meta[property="$property"]'); |
| 125 | + existing?.remove(); |
| 126 | + } |
| 127 | + |
| 128 | + /// Adds or updates canonical link |
| 129 | + void _addCanonicalLink(String url) { |
| 130 | + final existing = web.document.querySelector('link[rel="canonical"]'); |
| 131 | + if (existing != null) { |
| 132 | + existing.setAttribute('href', url); |
| 133 | + } else { |
| 134 | + final link = web.document.createElement('link'); |
| 135 | + link.setAttribute('rel', 'canonical'); |
| 136 | + link.setAttribute('href', url); |
| 137 | + web.document.head!.appendChild(link); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /// Removes canonical link |
| 142 | + void _removeCanonicalLink() { |
| 143 | + final existing = web.document.querySelector('link[rel="canonical"]'); |
| 144 | + existing?.remove(); |
| 145 | + } |
| 146 | +} |
0 commit comments