Skip to content

Commit f66d2ee

Browse files
committed
feat: add initial code to communicate with plugins
1 parent b25761c commit f66d2ee

6 files changed

Lines changed: 66 additions & 7 deletions

File tree

src/App.svelte

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import Layout from "@/Layout.svelte";
77
import { getCurrentSettingsState } from "@/states/settings/settings.svelte";
88
import { initializeExtensions } from "@/lib/extensions/initialize-extensions";
9+
import { initializeWindow } from "@/lib/helpers/initialize-window";
910
1011
initializeConfig();
1112
initializeExtensions();
@@ -14,6 +15,12 @@
1415
getCurrentSettingsState().current.transitions,
1516
);
1617
18+
// to communicate with extensions
19+
initializeWindow({
20+
// we don't care about reactivity
21+
"toEnableTransitions": getCurrentSettingsState().current.transitions,
22+
});
23+
1724
$effect(() => {
1825
if (toEnableTransitions) {
1926
document.body.removeAttribute("data-transitions-disabled");

src/components/base/Player.svelte

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
<script lang="ts">
2+
import { ApplicationNamespace } from "@/constants/app";
3+
4+
let {
5+
title,
6+
episode,
7+
}: {
8+
"title" : string;
9+
"episode": number;
10+
} = $props();
11+
12+
$effect(() => {
13+
window[ApplicationNamespace].dynamic = {
14+
...window[ApplicationNamespace].dynamic,
15+
"episode": episode,
16+
"title" : title,
17+
};
18+
window.postMessage("tsuki_updated_window", "*");
19+
});
20+
</script>
21+
122
<div
223
id="extensions-player-id"
324
class="aspect-media relative col-span-1 rounded-md bg-neutral-100 lg:col-span-2 dark:bg-neutral-900"

src/declarations.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,23 @@ declare global {
33
interface Window {
44
"__TSUKI__": {
55
// won't change
6-
"static": {
6+
"fixed": {
77
"appName" : string;
88
"appRootId": string;
99
"baseUrl" : string;
1010
};
1111
// window message event will be fired on change
1212
"dynamic": {
13+
// have user enabled smooth transitions or no
14+
"smooth" : boolean;
1315
// user color scheme
14-
"theme" : "light" | "dark";
15-
// user's search (needed for those plugins that can't use MAL ID)
16-
"search": string;
16+
"theme" : "light" | "dark";
17+
// anime title (needed for those plugins that can't use MAL ID)
18+
"title" : string;
19+
// anime MAL ID
20+
"idMal" : number;
21+
// current anime episode
22+
"episode": number;
1723
};
1824
};
1925
}

src/lib/extensions/toggle-extension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ export function toggleExtension(key: string) {
1919

2020
if (toDisable) {
2121
// notify the extension that it was disabled
22-
window.postMessage(`tsuki_disable_${key}`);
22+
window.postMessage(`tsuki_disable_${key}`, "*");
2323

2424
return;
2525
}
2626

2727
// notify the extension that it was enabled
28-
window.postMessage(`tsuki_enable_${key}`);
28+
window.postMessage(`tsuki_enable_${key}`, "*");
2929
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { ApplicationName, ApplicationNamespace, ApplicationRootID, BaseURL } from "@/constants/app";
2+
3+
export function initializeWindow({
4+
toEnableTransitions,
5+
}: {
6+
"toEnableTransitions": boolean;
7+
}) {
8+
window[ApplicationNamespace] = {
9+
"fixed": {
10+
"appName" : ApplicationName,
11+
"appRootId": ApplicationRootID,
12+
"baseUrl" : BaseURL,
13+
},
14+
"dynamic": {
15+
"smooth" : toEnableTransitions,
16+
"theme" : "dark",
17+
"title" : "",
18+
"idMal" : 0,
19+
"episode": 1,
20+
},
21+
};
22+
}

src/pages/Anime.svelte

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,10 @@
144144
<!-- and will be on the two different rows on smaller screens -->
145145
<div class="grid cols-1 rows-3 h-full w-full gap-4 lg:cols-3 lg:rows-1 sm:rows-2">
146146
<!-- all player extensions will mount on this element -->
147-
<Player />
147+
<Player
148+
title={title.toLowerCase()}
149+
episode={selectedEpisode}
150+
/>
148151
<!-- min-h-48 here to fix some issues with old browsers -->
149152
<div class="relative col-span-1 row-span-2 min-h-48 sm:row-span-1">
150153
<!-- the only way to ensure that episode selector will not exceed player's height -->

0 commit comments

Comments
 (0)