-
Notifications
You must be signed in to change notification settings - Fork 83
fix: add support for non-root base-href #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
8892812
31526ae
606ade6
b4114a9
e948054
9e1fd5b
39f6598
f2542bd
b8d0b58
bd3e7a7
4900aaf
ac4e069
a7ddb22
897dca4
f5d5bc4
8f38c22
bb3d4ca
be5d5f5
bb5d60d
12cd725
8f1ed7a
3886996
27f6d07
02d3e61
6a7e957
b3f0784
b91c868
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
holzgeist marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import 'dart:js_interop'; | ||
| import 'dart:ui_web' as ui_web; | ||
|
|
||
| import 'package:web/web.dart'; | ||
| import 'package:web/web.dart' as web; | ||
diegotori marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// This is an implementation of the `import_js_library` plugin that is used | ||
| /// until that plugin is migrated to null safety. | ||
|
|
@@ -18,19 +19,23 @@ Future<void> importJsLibrary( | |
| } | ||
|
|
||
| String _libraryUrl(String url, String pluginName) { | ||
| // Added suggested changes as per | ||
| // https://github.com/fluttercommunity/wakelock_plus/issues/19#issuecomment-2301963609 | ||
| if (url.startsWith('./')) { | ||
| url = url.replaceFirst('./', ''); | ||
| return './assets/packages/$pluginName/$url'; | ||
| } | ||
|
|
||
| if (url.startsWith('assets/')) { | ||
| return './assets/packages/$pluginName/$url'; | ||
| } else { | ||
| return url; | ||
| return ui_web.assetManager.getAssetUrl( | ||
| 'packages/$pluginName/$url', | ||
| ); | ||
| } | ||
|
|
||
| return url; | ||
| } | ||
|
|
||
| HTMLScriptElement _createScriptTag(String library) { | ||
| final script = document.createElement('script') as HTMLScriptElement | ||
| web.HTMLScriptElement _createScriptTag(String library) { | ||
| final script = web.document.createElement('script') as web.HTMLScriptElement | ||
| ..type = 'text/javascript' | ||
| ..charset = 'utf-8' | ||
| ..async = true | ||
|
|
@@ -42,32 +47,44 @@ HTMLScriptElement _createScriptTag(String library) { | |
| /// Future that resolves when all load. | ||
| Future<void> _importJSLibraries(List<String> libraries) { | ||
| final loading = <Future<void>>[]; | ||
| final head = document.head; | ||
| final head = web.document.head; | ||
|
|
||
| for (final library in libraries) { | ||
| if (!_isImported(library)) { | ||
| final scriptTag = _createScriptTag(library); | ||
| head!.appendChild(scriptTag); | ||
| loading.add(scriptTag.onLoad.first); | ||
| scriptTag.onError.listen((event) { | ||
| final scriptElement = event.srcElement is web.HTMLScriptElement | ||
| ? event.srcElement as web.HTMLScriptElement | ||
| : null; | ||
| if (scriptElement != null) { | ||
| loading.add( | ||
| Future.error( | ||
| Exception('Error loading: ${scriptElement.src}'), | ||
| ), | ||
| ); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return Future.wait(loading); | ||
| } | ||
|
|
||
| bool _isImported(String url) { | ||
| final head = document.head!; | ||
| final head = web.document.head!; | ||
| return _isLoaded(head, url); | ||
| } | ||
|
|
||
| bool _isLoaded(HTMLHeadElement head, String url) { | ||
| bool _isLoaded(web.HTMLHeadElement head, String url) { | ||
| if (url.startsWith('./')) { | ||
| url = url.replaceFirst('./', ''); | ||
| } | ||
| for (int i = 0; i < head.children.length; i++) { | ||
| final element = head.children.item(i)!; | ||
| if (element.instanceOfString('HTMLScriptElement')) { | ||
| if ((element as HTMLScriptElement).src.endsWith(url)) { | ||
| if ((element as web.HTMLScriptElement).src.endsWith(url)) { | ||
|
||
| return true; | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.