mirror of
https://github.com/paricafe/misskey.git
synced 2024-12-13 02:06:43 -06:00
860e8bb5d8
Co-authored-by: syuilo <Syuilotan@yahoo.co.jp> Co-authored-by: syuilo <4439005+syuilo@users.noreply.github.com>
70 lines
2 KiB
TypeScript
70 lines
2 KiB
TypeScript
/*
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
import * as Misskey from 'misskey-js';
|
|
import { MaybeRefOrGetter, Ref, inject, isRef, onActivated, onBeforeUnmount, provide, ref, toValue, watch } from 'vue';
|
|
|
|
export type PageMetadata = {
|
|
title: string;
|
|
subtitle?: string;
|
|
icon?: string | null;
|
|
avatar?: Misskey.entities.User | null;
|
|
userName?: Misskey.entities.User | null;
|
|
needWideArea?: boolean;
|
|
};
|
|
|
|
type PageMetadataGetter = () => PageMetadata;
|
|
type PageMetadataReceiver = (getter: PageMetadataGetter) => void;
|
|
|
|
const RECEIVER_KEY = Symbol('ReceiverKey');
|
|
const setReceiver = (v: PageMetadataReceiver): void => {
|
|
provide<PageMetadataReceiver>(RECEIVER_KEY, v);
|
|
};
|
|
const getReceiver = (): PageMetadataReceiver | undefined => {
|
|
return inject<PageMetadataReceiver>(RECEIVER_KEY);
|
|
};
|
|
|
|
const METADATA_KEY = Symbol('MetadataKey');
|
|
const setMetadata = (v: Ref<PageMetadata | null>): void => {
|
|
provide<Ref<PageMetadata | null>>(METADATA_KEY, v);
|
|
};
|
|
const getMetadata = (): Ref<PageMetadata | null> | undefined => {
|
|
return inject<Ref<PageMetadata | null>>(METADATA_KEY);
|
|
};
|
|
|
|
export const definePageMetadata = (maybeRefOrGetterMetadata: MaybeRefOrGetter<PageMetadata>): void => {
|
|
const metadataRef = ref(toValue(maybeRefOrGetterMetadata));
|
|
const metadataGetter = () => metadataRef.value;
|
|
const receiver = getReceiver();
|
|
|
|
// setup handler
|
|
receiver?.(metadataGetter);
|
|
|
|
// update handler
|
|
onBeforeUnmount(watch(
|
|
() => toValue(maybeRefOrGetterMetadata),
|
|
(metadata) => {
|
|
metadataRef.value = metadata;
|
|
receiver?.(metadataGetter);
|
|
},
|
|
{ deep: true },
|
|
));
|
|
onActivated(() => {
|
|
receiver?.(metadataGetter);
|
|
});
|
|
};
|
|
|
|
export const provideMetadataReceiver = (receiver: PageMetadataReceiver): void => {
|
|
setReceiver(receiver);
|
|
};
|
|
|
|
export const provideReactiveMetadata = (metadataRef: Ref<PageMetadata | null>): void => {
|
|
setMetadata(metadataRef);
|
|
};
|
|
|
|
export const injectReactiveMetadata = (): Ref<PageMetadata | null> => {
|
|
const metadataRef = getMetadata();
|
|
return isRef(metadataRef) ? metadataRef : ref(null);
|
|
};
|