Skip to content

Commit

Permalink
Merge pull request #227 from humhub/f-50-internal-links-from-tags-ope…
Browse files Browse the repository at this point in the history
…n-in-browser

F 50 internal links from tags open in browser
  • Loading branch information
luke- authored Sep 10, 2024
2 parents 3b34be5 + 4783128 commit 74b2288
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/flavored/web_view.f.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class FlavoredWebViewState extends ConsumerState<WebViewF> {
logDebug("onCreateWindow");
final urlToOpen = createWindowAction.request.url;
if (urlToOpen == null) return Future.value(false);
if (urlToOpen.rawValue.contains('file/download')) {
if (WebViewGlobalController.openCreateWindowInWebView(ref, urlToOpen.rawValue)) {
controller.loadUrl(urlRequest: createWindowAction.request);
return Future.value(false);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/web_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class WebViewAppState extends ConsumerState<WebView> {
WebUri? urlToOpen = createWindowAction.request.url;

if (urlToOpen == null) return Future.value(false);
if (urlToOpen.rawValue.contains('file/download')) {
if (WebViewGlobalController.openCreateWindowInWebView(ref, urlToOpen.rawValue)) {
controller.loadUrl(urlRequest: createWindowAction.request);
return Future.value(false);
}
Expand Down
31 changes: 22 additions & 9 deletions lib/util/web_view_global_controller.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
import 'dart:convert';

import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:humhub/util/providers.dart';

class WebViewGlobalController {
static InAppWebViewController? _value;

static InAppWebViewController? get value => _value;

/// [openCreateWindowInWebView]
///
/// Determines if a URL should open in a new browser window or within the current web view.
///
/// - Opens in a new window if:
/// - The URL is for file downloads (`file/file/download` after base URL).
/// - The URL is a @username profile redirect (`/u` after base URL).
/// - The URL is a @space redirect (`/s` after base URL).
///
/// [ref] is reference to the app state.
/// [url] is the URL to evaluate.
/// @return `true` if the URL should open in a new window, `false` otherwise.
static bool openCreateWindowInWebView(WidgetRef ref, String url) {
String? baseUrl = ref.read(humHubProvider).manifest?.baseUrl;
if (url.startsWith('$baseUrl/file/file/download')) return true;
if (url.startsWith('$baseUrl/u')) return true;
if (url.startsWith('$baseUrl/s')) return true;
return false;
}

static void setValue(InAppWebViewController newValue) {
_value = newValue;
}
Expand All @@ -20,15 +42,6 @@ class WebViewGlobalController {
data: $data,
headers: $jsonHeaders,
async: false, // IMPORTANT: it needs to be async
success: function(data) {
console.log('MD-1222');
},
error: function(xhr, status, error) {
console.log('MD-1333');
console.log(error);
console.log(status);
console.log(xhr);
}
});
""";
value?.evaluateJavascript(source: jsCode4);
Expand Down

0 comments on commit 74b2288

Please sign in to comment.