|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +@JS() |
| 6 | +library js_url_strategy; |
| 7 | + |
| 8 | +import 'dart:js_interop'; |
| 9 | + |
| 10 | +import 'package:ui/ui.dart' as ui; |
| 11 | + |
| 12 | +import '../dom.dart'; |
| 13 | + |
| 14 | +typedef _PathGetter = String Function(); |
| 15 | + |
| 16 | +typedef _StateGetter = Object? Function(); |
| 17 | + |
| 18 | +typedef _AddPopStateListener = ui.VoidCallback Function(DartDomEventListener); |
| 19 | + |
| 20 | +typedef _StringToString = String Function(String); |
| 21 | + |
| 22 | +typedef _StateOperation = void Function( |
| 23 | + Object? state, String title, String url); |
| 24 | + |
| 25 | +typedef _HistoryMove = Future<void> Function(double count); |
| 26 | + |
| 27 | +/// The JavaScript representation of a URL strategy. |
| 28 | +/// |
| 29 | +/// This is used to pass URL strategy implementations across a JS-interop |
| 30 | +/// bridge from the app to the engine. |
| 31 | +@JS() |
| 32 | +@anonymous |
| 33 | +@staticInterop |
| 34 | +abstract class JsUrlStrategy { |
| 35 | + /// Creates an instance of [JsUrlStrategy] from a bag of URL strategy |
| 36 | + /// functions. |
| 37 | + external factory JsUrlStrategy({ |
| 38 | + required _PathGetter getPath, |
| 39 | + required _StateGetter getState, |
| 40 | + required _AddPopStateListener addPopStateListener, |
| 41 | + required _StringToString prepareExternalUrl, |
| 42 | + required _StateOperation pushState, |
| 43 | + required _StateOperation replaceState, |
| 44 | + required _HistoryMove go, |
| 45 | + }); |
| 46 | +} |
| 47 | + |
| 48 | +extension JsUrlStrategyExtension on JsUrlStrategy { |
| 49 | + /// Adds a listener to the `popstate` event and returns a function that, when |
| 50 | + /// invoked, removes the listener. |
| 51 | + external ui.VoidCallback addPopStateListener(DartDomEventListener fn); |
| 52 | + |
| 53 | + /// Returns the active path in the browser. |
| 54 | + external String getPath(); |
| 55 | + |
| 56 | + /// Returns the history state in the browser. |
| 57 | + /// |
| 58 | + /// See: https://developer.mozilla.org/en-US/docs/Web/API/History/state |
| 59 | + external Object? getState(); |
| 60 | + |
| 61 | + /// Given a path that's internal to the app, create the external url that |
| 62 | + /// will be used in the browser. |
| 63 | + external String prepareExternalUrl(String internalUrl); |
| 64 | + |
| 65 | + /// Push a new history entry. |
| 66 | + /// |
| 67 | + /// See: https://developer.mozilla.org/en-US/docs/Web/API/History/pushState |
| 68 | + external void pushState(Object? state, String title, String url); |
| 69 | + |
| 70 | + /// Replace the currently active history entry. |
| 71 | + /// |
| 72 | + /// See: https://developer.mozilla.org/en-US/docs/Web/API/History/replaceState |
| 73 | + external void replaceState(Object? state, String title, String url); |
| 74 | + |
| 75 | + /// Moves forwards or backwards through the history stack. |
| 76 | + /// |
| 77 | + /// A negative [count] value causes a backward move in the history stack. And |
| 78 | + /// a positive [count] value causs a forward move. |
| 79 | + /// |
| 80 | + /// Examples: |
| 81 | + /// |
| 82 | + /// * `go(-2)` moves back 2 steps in history. |
| 83 | + /// * `go(3)` moves forward 3 steps in hisotry. |
| 84 | + /// |
| 85 | + /// See: https://developer.mozilla.org/en-US/docs/Web/API/History/go |
| 86 | + external Future<void> go(double count); |
| 87 | +} |
0 commit comments