-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "[web] Remove the JS API for url strategy (#42134)"
This reverts commit c1ef0d9.
- Loading branch information
1 parent
e83bcf8
commit b017b9b
Showing
11 changed files
with
235 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// 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. | ||
|
||
export 'navigation/history.dart'; | ||
export 'navigation/js_url_strategy.dart'; | ||
export 'navigation/url_strategy.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// 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. | ||
|
||
@JS() | ||
library js_url_strategy; | ||
|
||
import 'dart:js_interop'; | ||
|
||
import 'package:ui/ui.dart' as ui; | ||
|
||
import '../dom.dart'; | ||
|
||
typedef _PathGetter = String Function(); | ||
|
||
typedef _StateGetter = Object? Function(); | ||
|
||
typedef _AddPopStateListener = ui.VoidCallback Function(DartDomEventListener); | ||
|
||
typedef _StringToString = String Function(String); | ||
|
||
typedef _StateOperation = void Function( | ||
Object? state, String title, String url); | ||
|
||
typedef _HistoryMove = Future<void> Function(double count); | ||
|
||
/// The JavaScript representation of a URL strategy. | ||
/// | ||
/// This is used to pass URL strategy implementations across a JS-interop | ||
/// bridge from the app to the engine. | ||
@JS() | ||
@anonymous | ||
@staticInterop | ||
abstract class JsUrlStrategy { | ||
/// Creates an instance of [JsUrlStrategy] from a bag of URL strategy | ||
/// functions. | ||
external factory JsUrlStrategy({ | ||
required _PathGetter getPath, | ||
required _StateGetter getState, | ||
required _AddPopStateListener addPopStateListener, | ||
required _StringToString prepareExternalUrl, | ||
required _StateOperation pushState, | ||
required _StateOperation replaceState, | ||
required _HistoryMove go, | ||
}); | ||
} | ||
|
||
extension JsUrlStrategyExtension on JsUrlStrategy { | ||
/// Adds a listener to the `popstate` event and returns a function that, when | ||
/// invoked, removes the listener. | ||
external ui.VoidCallback addPopStateListener(DartDomEventListener fn); | ||
|
||
/// Returns the active path in the browser. | ||
external String getPath(); | ||
|
||
/// Returns the history state in the browser. | ||
/// | ||
/// See: https://developer.mozilla.org/en-US/docs/Web/API/History/state | ||
external Object? getState(); | ||
|
||
/// Given a path that's internal to the app, create the external url that | ||
/// will be used in the browser. | ||
external String prepareExternalUrl(String internalUrl); | ||
|
||
/// Push a new history entry. | ||
/// | ||
/// See: https://developer.mozilla.org/en-US/docs/Web/API/History/pushState | ||
external void pushState(Object? state, String title, String url); | ||
|
||
/// Replace the currently active history entry. | ||
/// | ||
/// See: https://developer.mozilla.org/en-US/docs/Web/API/History/replaceState | ||
external void replaceState(Object? state, String title, String url); | ||
|
||
/// Moves forwards or backwards through the history stack. | ||
/// | ||
/// A negative [count] value causes a backward move in the history stack. And | ||
/// a positive [count] value causs a forward move. | ||
/// | ||
/// Examples: | ||
/// | ||
/// * `go(-2)` moves back 2 steps in history. | ||
/// * `go(3)` moves forward 3 steps in hisotry. | ||
/// | ||
/// See: https://developer.mozilla.org/en-US/docs/Web/API/History/go | ||
external Future<void> go(double count); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// 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 'dart:async'; | ||
|
||
import 'package:ui/ui.dart' as ui; | ||
import 'package:ui/ui_web/src/ui_web.dart' as ui_web; | ||
|
||
import '../dom.dart'; | ||
import '../safe_browser_api.dart'; | ||
import 'js_url_strategy.dart'; | ||
|
||
/// Wraps a custom implementation of [ui_web.UrlStrategy] that was previously converted | ||
/// to a [JsUrlStrategy]. | ||
class CustomUrlStrategy extends ui_web.UrlStrategy { | ||
/// Wraps the [delegate] in a [CustomUrlStrategy] instance. | ||
CustomUrlStrategy.fromJs(this.delegate); | ||
|
||
final JsUrlStrategy delegate; | ||
|
||
@override | ||
ui.VoidCallback addPopStateListener(ui_web.PopStateListener fn) => | ||
delegate.addPopStateListener(allowInterop((DomEvent event) => | ||
fn((event as DomPopStateEvent).state) | ||
)); | ||
|
||
@override | ||
String getPath() => delegate.getPath(); | ||
|
||
@override | ||
Object? getState() => delegate.getState(); | ||
|
||
@override | ||
String prepareExternalUrl(String internalUrl) => | ||
delegate.prepareExternalUrl(internalUrl); | ||
|
||
@override | ||
void pushState(Object? state, String title, String url) => | ||
delegate.pushState(state, title, url); | ||
|
||
@override | ||
void replaceState(Object? state, String title, String url) => | ||
delegate.replaceState(state, title, url); | ||
|
||
@override | ||
Future<void> go(int count) => delegate.go(count.toDouble()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.