Skip to content
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
17 changes: 17 additions & 0 deletions src/ClientWidgetApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
IDownloadFileActionFromWidgetActionRequest,
IDownloadFileActionFromWidgetResponseData,
} from "./interfaces/DownloadFileAction";
import { IThemeChangeActionRequestData } from "./interfaces/ThemeChangeAction";

/**
* API handler for the client side of widgets. This raises events
Expand Down Expand Up @@ -435,7 +436,7 @@
if (request.data.room_ids) {
askRoomIds = request.data.room_ids as string[];
if (!Array.isArray(askRoomIds)) {
askRoomIds = [askRoomIds as any as string];

Check warning on line 439 in src/ClientWidgetApi.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
}
for (const roomId of askRoomIds) {
if (!this.canUseRoomTimeline(roomId)) {
Expand Down Expand Up @@ -896,6 +897,22 @@
}
}

/**
* Informs the widget that the client's theme has changed.
* @param theme The theme data, as an object with arbitrary contents.
Comment on lines +901 to +902
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx for the docstrings!

*/
public updateTheme(theme: IThemeChangeActionRequestData): Promise<IWidgetApiResponseData> {
return this.transport.send(WidgetApiToWidgetAction.ThemeChange, theme);
}

/**
* Informs the widget that the client's language has changed.
* @param lang The BCP 47 identifier representing the client's current language.
*/
public updateLanguage(lang: string): Promise<IWidgetApiResponseData> {
return this.transport.send(WidgetApiToWidgetAction.LanguageChange, { lang });
}

/**
* Takes a screenshot of the widget.
* @returns Resolves to the widget's screenshot.
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces/ApiVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export enum MatrixApiVersion {
export enum UnstableApiVersion {
MSC2762 = "org.matrix.msc2762",
MSC2871 = "org.matrix.msc2871",
MSC2873 = "org.matrix.msc2873",
MSC2931 = "org.matrix.msc2931",
MSC2974 = "org.matrix.msc2974",
MSC2876 = "org.matrix.msc2876",
Expand All @@ -41,6 +42,7 @@ export const CurrentApiVersions: ApiVersion[] = [
//MatrixApiVersion.V010,
UnstableApiVersion.MSC2762,
UnstableApiVersion.MSC2871,
UnstableApiVersion.MSC2873,
UnstableApiVersion.MSC2931,
UnstableApiVersion.MSC2974,
UnstableApiVersion.MSC2876,
Expand Down
35 changes: 35 additions & 0 deletions src/interfaces/LanguageChangeAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2024 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { IWidgetApiRequest, IWidgetApiRequestData } from "./IWidgetApiRequest";
import { WidgetApiToWidgetAction } from "./WidgetApiAction";
import { IWidgetApiAcknowledgeResponseData } from "./IWidgetApiResponse";

export interface ILanguageChangeActionRequestData extends IWidgetApiRequestData {
/**
* The BCP 47 identifier for the client's current language.
*/
lang: string;
}

export interface ILanguageChangeActionRequest extends IWidgetApiRequest {
action: WidgetApiToWidgetAction.LanguageChange;
data: ILanguageChangeActionRequestData;
}

export interface ILanguageChangeActionResponse extends ILanguageChangeActionRequest {
response: IWidgetApiAcknowledgeResponseData;
}
32 changes: 32 additions & 0 deletions src/interfaces/ThemeChangeAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2024 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { IWidgetApiRequest, IWidgetApiRequestData } from "./IWidgetApiRequest";
import { WidgetApiToWidgetAction } from "./WidgetApiAction";
import { IWidgetApiAcknowledgeResponseData } from "./IWidgetApiResponse";

export interface IThemeChangeActionRequestData extends IWidgetApiRequestData {
// The format of a theme is deliberately unstandardized
}

export interface IThemeChangeActionRequest extends IWidgetApiRequest {
action: WidgetApiToWidgetAction.ThemeChange;
data: IThemeChangeActionRequestData;
}

export interface IThemeChangeActionResponse extends IThemeChangeActionRequest {
response: IWidgetApiAcknowledgeResponseData;
}
2 changes: 2 additions & 0 deletions src/interfaces/WidgetApiAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export enum WidgetApiToWidgetAction {
SupportedApiVersions = "supported_api_versions",
Capabilities = "capabilities",
NotifyCapabilities = "notify_capabilities",
ThemeChange = "theme_change",
LanguageChange = "language_change",
TakeScreenshot = "screenshot",
UpdateVisibility = "visibility",
OpenIDCredentials = "openid_credentials",
Expand Down
12 changes: 11 additions & 1 deletion test/ClientWidgetApi-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { IWidgetApiRequest } from '../src/interfaces/IWidgetApiRequest';
import { IReadRelationsFromWidgetActionRequest } from '../src/interfaces/ReadRelationsAction';
import { ISupportedVersionsActionRequest } from '../src/interfaces/SupportedVersionsAction';
import { IUserDirectorySearchFromWidgetActionRequest } from '../src/interfaces/UserDirectorySearchAction';
import { WidgetApiFromWidgetAction } from '../src/interfaces/WidgetApiAction';
import { WidgetApiFromWidgetAction, WidgetApiToWidgetAction } from '../src/interfaces/WidgetApiAction';
import { WidgetApiDirection } from '../src/interfaces/WidgetApiDirection';
import { Widget } from '../src/models/Widget';
import { PostmessageTransport } from '../src/transport/PostmessageTransport';
Expand Down Expand Up @@ -2194,4 +2194,14 @@ describe('ClientWidgetApi', () => {
});
});
});

it('updates theme', () => {
clientWidgetApi.updateTheme({ name: 'dark' });
expect(transport.send).toHaveBeenCalledWith(WidgetApiToWidgetAction.ThemeChange, { name: 'dark' });
});

it('updates language', () => {
clientWidgetApi.updateLanguage('tlh');
expect(transport.send).toHaveBeenCalledWith(WidgetApiToWidgetAction.LanguageChange, { lang: 'tlh' });
});
});
Loading