diff --git a/packages/share_plus_web/CHANGELOG.md b/packages/share_plus_web/CHANGELOG.md new file mode 100644 index 0000000000..7be26166a1 --- /dev/null +++ b/packages/share_plus_web/CHANGELOG.md @@ -0,0 +1,3 @@ +# 0.1.0 + +- Initial open-source release. diff --git a/packages/share_plus_web/LICENSE b/packages/share_plus_web/LICENSE new file mode 100644 index 0000000000..c89293372c --- /dev/null +++ b/packages/share_plus_web/LICENSE @@ -0,0 +1,27 @@ +// Copyright 2017 The Chromium Authors. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/packages/share_plus_web/README.md b/packages/share_plus_web/README.md new file mode 100644 index 0000000000..acfaba99b8 --- /dev/null +++ b/packages/share_plus_web/README.md @@ -0,0 +1,12 @@ +# Share Plus Web + +[![Flutter Community: share_plus_web](https://fluttercommunity.dev/_github/header/share_plus_web)](https://github.com/fluttercommunity/community) + +[![pub package](https://img.shields.io/pub/v/share_plus_web.svg)](https://pub.dev/packages/share_plus_web) + +The Web implementation of [`share_plus`](https://pub.dev/packages/share_plus). + +## Usage + +This package is already included as part of the `share_plus` package dependency, and will +be included when using `share_plus` as normal. diff --git a/packages/share_plus_web/lib/share_plus_web.dart b/packages/share_plus_web/lib/share_plus_web.dart new file mode 100644 index 0000000000..92d767bb68 --- /dev/null +++ b/packages/share_plus_web/lib/share_plus_web.dart @@ -0,0 +1,49 @@ +import 'dart:html' as html; +import 'dart:ui'; + +import 'package:flutter_web_plugins/flutter_web_plugins.dart'; +import 'package:meta/meta.dart'; +import 'package:share_plus_platform_interface/share_plus_platform_interface.dart'; +import 'package:url_launcher/url_launcher.dart'; + +/// The web implementation of [SharePlatform]. +class SharePlugin extends SharePlatform { + /// Registers this class as the default instance of [SharePlatform]. + static void registerWith(Registrar registrar) { + SharePlatform.instance = SharePlatform(); + } + + final _navigator; + + /// A constructor that allows tests to override the window object used by the plugin. + SharePlugin({@visibleForTesting html.Navigator debugNavigator}) + : _navigator = debugNavigator ?? html.window.navigator; + + @override + + /// Share text + Future share( + String text, { + String subject, + Rect sharePositionOrigin, + }) async { + try { + await _navigator.share({'title': subject, 'text': text}); + } catch (e) { + //Navigator is not available or the webPage is not served on https + final uri = Uri.encodeFull('mailto:?subject=$subject&body=$text'); + return launch(uri); + } + } + + @override + + /// Share files + Future shareFiles(List paths, + {List mimeTypes, + String subject, + String text, + Rect sharePositionOrigin}) { + throw UnimplementedError('shareFiles() has not been implemented on Web.'); + } +} diff --git a/packages/share_plus_web/pubspec.yaml b/packages/share_plus_web/pubspec.yaml new file mode 100644 index 0000000000..5a4ec7b6ec --- /dev/null +++ b/packages/share_plus_web/pubspec.yaml @@ -0,0 +1,35 @@ +name: share_plus_web +description: Web platform implementation of share_plus +homepage: https://plus.fluttercommunity.dev/ +repository: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/ +# 0.1.y+z is compatible with 1.0.0, if you land a breaking change bump +# the version to 2.0.0. +# See more details: https://github.com/flutter/flutter/wiki/Package-migration-to-1.0.0 +version: 0.1.0 + +flutter: + plugin: + platforms: + web: + pluginClass: SharePlugin + fileName: share_plus_web.dart + +dependencies: + share_plus_platform_interface: ^1.1.0 + url_launcher: ^5.2.5 + flutter: + sdk: flutter + flutter_web_plugins: + sdk: flutter + meta: ^1.1.7 + +dev_dependencies: + flutter_test: + sdk: flutter + pedantic: ^1.8.0 + mockito: ^4.1.1 + integration_test: ^0.9.2 + +environment: + sdk: ">=2.2.0 <3.0.0" + flutter: ">=1.10.0 <2.0.0" diff --git a/packages/share_plus_web/test/lib/main.dart b/packages/share_plus_web/test/lib/main.dart new file mode 100644 index 0000000000..1041520457 --- /dev/null +++ b/packages/share_plus_web/test/lib/main.dart @@ -0,0 +1,22 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/material.dart'; + +void main() { + runApp(MyApp()); +} + +/// App for testing +class MyApp extends StatefulWidget { + @override + _MyAppState createState() => _MyAppState(); +} + +class _MyAppState extends State { + @override + Widget build(BuildContext context) { + return Text('Testing... Look at the console output for results!'); + } +} diff --git a/packages/share_plus_web/test/pubspec.yaml b/packages/share_plus_web/test/pubspec.yaml new file mode 100644 index 0000000000..7004d53a28 --- /dev/null +++ b/packages/share_plus_web/test/pubspec.yaml @@ -0,0 +1,21 @@ +name: regular_integration_tests +publish_to: none + +environment: + sdk: ">=2.2.2 <3.0.0" + +dependencies: + + flutter: + sdk: flutter + +dev_dependencies: + flutter_driver: + sdk: flutter + flutter_test: + sdk: flutter + share_plus_web: + path: ../ + http: ^0.12.2 + mockito: ^4.1.1 + integration_test: ^0.9.2 diff --git a/packages/share_plus_web/test/test_driver/share_plus_web_integration.dart b/packages/share_plus_web/test/test_driver/share_plus_web_integration.dart new file mode 100644 index 0000000000..3a2854c30b --- /dev/null +++ b/packages/share_plus_web/test/test_driver/share_plus_web_integration.dart @@ -0,0 +1,36 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:integration_test/integration_test.dart'; +import 'package:mockito/mockito.dart'; +import 'dart:html' as html; + +import 'package:share_plus_web/share_plus_web.dart'; + +class _MockWindow extends Mock implements html.Window {} + +class _MockNavigator extends Mock implements html.Navigator {} + +void main() { + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); + group('SharePlugin', () { + _MockWindow mockWindow; + _MockNavigator mockNavigator; + + SharePlugin plugin; + + setUp(() { + mockWindow = _MockWindow(); + mockNavigator = _MockNavigator(); + when(mockWindow.navigator).thenReturn(mockNavigator); + + plugin = SharePlugin(debugNavigator: mockNavigator); + }); + group('share', () { + testWidgets('can share url', (WidgetTester _) async { + expect(plugin.share('https://google.com'), completes); + }); + testWidgets('can share text with subject', (WidgetTester _) async { + expect(plugin.share('flutter', subject: 'test'), completes); + }); + }); + }); +} diff --git a/packages/share_plus_web/test/test_driver/share_plus_web_integration_test.dart b/packages/share_plus_web/test/test_driver/share_plus_web_integration_test.dart new file mode 100644 index 0000000000..64e2248a4f --- /dev/null +++ b/packages/share_plus_web/test/test_driver/share_plus_web_integration_test.dart @@ -0,0 +1,7 @@ +// Copyright 2019 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:integration_test/integration_test_driver.dart'; + +Future main() async => integrationDriver(); diff --git a/packages/share_plus_web/test/web/index.html b/packages/share_plus_web/test/web/index.html new file mode 100644 index 0000000000..143be5527d --- /dev/null +++ b/packages/share_plus_web/test/web/index.html @@ -0,0 +1,10 @@ + + + + + example + + + + +