Skip to content
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

Use Map instead of JSObject to update window.intercomSettings #410

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions intercom_flutter_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.1.1

* Updated window.intercomSettings as Map instead of JSObject.

## 1.1.0

* Migrated to js_interop to be compatible with WASM.
Expand Down
28 changes: 18 additions & 10 deletions intercom_flutter_web/lib/intercom_flutter_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform {
web.HTMLScriptElement script =
web.document.createElement("script") as web.HTMLScriptElement;
script.text = """
window.intercomSettings = ${updateIntercomSettings('app_id', "'$appId'").dartify()};
window.intercomSettings = ${updateIntercomSettings('app_id', "'$appId'")};
(function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',w.intercomSettings);}else{var d=document;var i=function(){i.c(arguments);};i.q=[];i.c=function(args){i.q.push(args);};w.Intercom=i;var l=function(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/' + '$appId';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s, x);};if(document.readyState==='complete'){l();}else if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})();
""";
if (web.document.body != null) {
Expand All @@ -62,7 +62,7 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform {
globalContext.callMethod(
'Intercom'.toJS,
'boot'.toJS,
updateIntercomSettings('app_id', appId),
updateIntercomSettings('app_id', appId).jsify(),
);
}
print("initialized");
Expand Down Expand Up @@ -205,7 +205,7 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform {
updateIntercomSettings(
'hide_default_launcher',
visibility == IntercomVisibility.visible ? false : true,
),
).jsify(),
);

print("Showing launcher: $visibility");
Expand Down Expand Up @@ -256,7 +256,7 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform {
updateIntercomSettings(
'vertical_padding',
padding,
),
).jsify(),
);

print("Bottom padding set");
Expand Down Expand Up @@ -286,20 +286,28 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform {
print("Launched Tickets space");
}

/// get the [window.IntercomSettings]
JSObject getIntercomSettings() {
/// get the [window.intercomSettings]
Map<dynamic, dynamic> getIntercomSettings() {
if (globalContext.hasProperty('intercomSettings'.toJS).toDart) {
return globalContext.getProperty('intercomSettings'.toJS) as JSObject;
var settings =
globalContext.getProperty('intercomSettings'.toJS).dartify();
// settings are of type LinkedMap<Object?, Object?>
return settings as Map;
}

return JSObject();
return {};
}

/// add/update property to [window.IntercomSettings]
/// add/update property to [window.intercomSettings]
/// and returns the updated object
JSObject updateIntercomSettings(String key, dynamic value) {
Map<dynamic, dynamic> updateIntercomSettings(String key, dynamic value) {
var intercomSettings = getIntercomSettings();
intercomSettings[key] = value;

// Update the [window.intercomSettings]
globalContext.setProperty(
"intercomSettings".toJS, intercomSettings.jsify());

return intercomSettings;
}
}
2 changes: 1 addition & 1 deletion intercom_flutter_web/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: intercom_flutter_web
description: Web platform implementation of intercom_flutter
version: 1.1.0
version: 1.1.1
homepage: https://github.com/v3rm0n/intercom_flutter

flutter:
Expand Down
Loading