mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 12:24:38 +03:00
Implement UI event dispatcher/listener (#4492)
* page change event * expose event to plugin api * Update UIPluginApi.md * Add to example plugin --------- Co-authored-by: WithoutPants <53250216+WithoutPants@users.noreply.github.com>
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
interface IPluginApi {
|
interface IPluginApi {
|
||||||
React: typeof React;
|
React: typeof React;
|
||||||
GQL: any;
|
GQL: any;
|
||||||
|
Event: {
|
||||||
|
addEventListener: (event: string, callback: (e: CustomEvent) => void) => void;
|
||||||
|
};
|
||||||
libraries: {
|
libraries: {
|
||||||
ReactRouterDOM: {
|
ReactRouterDOM: {
|
||||||
Link: React.FC<any>;
|
Link: React.FC<any>;
|
||||||
@@ -53,6 +56,8 @@ interface IPluginApi {
|
|||||||
NavUtils
|
NavUtils
|
||||||
} = PluginApi.utils;
|
} = PluginApi.utils;
|
||||||
|
|
||||||
|
PluginApi.Event.addEventListener("stash:location", (e) => console.log("Page Changed", e.detail.data.location.pathname, e.detail.data.location.search))
|
||||||
|
|
||||||
const ScenePerformer: React.FC<{
|
const ScenePerformer: React.FC<{
|
||||||
performer: any;
|
performer: any;
|
||||||
}> = ({ performer }) => {
|
}> = ({ performer }) => {
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ import { lazyComponent } from "./utils/lazyComponent";
|
|||||||
import { isPlatformUniquelyRenderedByApple } from "./utils/apple";
|
import { isPlatformUniquelyRenderedByApple } from "./utils/apple";
|
||||||
import useScript, { useCSS } from "./hooks/useScript";
|
import useScript, { useCSS } from "./hooks/useScript";
|
||||||
import { useMemoOnce } from "./hooks/state";
|
import { useMemoOnce } from "./hooks/state";
|
||||||
|
import Event from "./hooks/event";
|
||||||
import { uniq } from "lodash-es";
|
import { uniq } from "lodash-es";
|
||||||
|
|
||||||
import { PluginRoutes } from "./plugins";
|
import { PluginRoutes } from "./plugins";
|
||||||
@@ -249,6 +250,11 @@ export const App: React.FC = () => {
|
|||||||
const history = useHistory();
|
const history = useHistory();
|
||||||
const setupMatch = useRouteMatch(["/setup", "/migrate"]);
|
const setupMatch = useRouteMatch(["/setup", "/migrate"]);
|
||||||
|
|
||||||
|
// dispatch event when location changes
|
||||||
|
useEffect(() => {
|
||||||
|
Event.dispatch("location", "", { location });
|
||||||
|
}, [location]);
|
||||||
|
|
||||||
// redirect to setup or migrate as needed
|
// redirect to setup or migrate as needed
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!systemStatusData) {
|
if (!systemStatusData) {
|
||||||
|
|||||||
@@ -136,3 +136,11 @@ Registers an after function. An after function is called after the render functi
|
|||||||
| `fn` | `Function` | The after function. It accepts the same arguments as the original render function, plus the result of the original render function, and is expected to return the rendered component. |
|
| `fn` | `Function` | The after function. It accepts the same arguments as the original render function, plus the result of the original render function, and is expected to return the rendered component. |
|
||||||
|
|
||||||
Returns `void`.
|
Returns `void`.
|
||||||
|
|
||||||
|
#### `PluginApi.Event`
|
||||||
|
|
||||||
|
Allows plugins to listen for Stash's events.
|
||||||
|
|
||||||
|
```js
|
||||||
|
PluginApi.Event.addEventListener("stash:location", (e) => console.log("Page Changed", e.detail.data.location.pathname))
|
||||||
|
```
|
||||||
|
|||||||
19
ui/v2.5/src/hooks/event.ts
Normal file
19
ui/v2.5/src/hooks/event.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
class StashEvent extends EventTarget {
|
||||||
|
dispatch(event: string, id?: string, data?: object) {
|
||||||
|
event = `stash:${event}${id ? `:${id}` : ""}`;
|
||||||
|
|
||||||
|
this.dispatchEvent(
|
||||||
|
new CustomEvent(event, {
|
||||||
|
detail: {
|
||||||
|
event: event,
|
||||||
|
...(id ? { id } : {}),
|
||||||
|
...(data ? { data } : {}),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const Event = new StashEvent();
|
||||||
|
|
||||||
|
export default Event;
|
||||||
@@ -13,6 +13,7 @@ import * as FontAwesomeSolid from "@fortawesome/free-solid-svg-icons";
|
|||||||
import * as FontAwesomeRegular from "@fortawesome/free-regular-svg-icons";
|
import * as FontAwesomeRegular from "@fortawesome/free-regular-svg-icons";
|
||||||
import { useSpriteInfo } from "./hooks/sprite";
|
import { useSpriteInfo } from "./hooks/sprite";
|
||||||
import { useToast } from "./hooks/Toast";
|
import { useToast } from "./hooks/Toast";
|
||||||
|
import Event from "./hooks/event";
|
||||||
import { before, instead, after, components, RegisterComponent } from "./patch";
|
import { before, instead, after, components, RegisterComponent } from "./patch";
|
||||||
|
|
||||||
// due to code splitting, some components may not have been loaded when a plugin
|
// due to code splitting, some components may not have been loaded when a plugin
|
||||||
@@ -106,6 +107,7 @@ export const PluginApi = {
|
|||||||
// and the result of the original function
|
// and the result of the original function
|
||||||
after,
|
after,
|
||||||
},
|
},
|
||||||
|
Event: Event,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default PluginApi;
|
export default PluginApi;
|
||||||
|
|||||||
Reference in New Issue
Block a user