summaryrefslogtreecommitdiff
path: root/packages/client/src/widgets/widget.ts
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2022-01-08 20:30:01 +0900
committerGitHub <noreply@github.com>2022-01-08 20:30:01 +0900
commit0bbde336b3636f4135de54c0ed75c7aa208534fe (patch)
tree45bb6a5c1e5397b3de351068d0eb1eeb6e3ec6c9 /packages/client/src/widgets/widget.ts
parentbye room (diff)
downloadmisskey-0bbde336b3636f4135de54c0ed75c7aa208534fe.tar.gz
misskey-0bbde336b3636f4135de54c0ed75c7aa208534fe.tar.bz2
misskey-0bbde336b3636f4135de54c0ed75c7aa208534fe.zip
refactor: Widgetのcomposition api移行 (#8125)
* wip * wip * wip * wip * wip * wip * fix
Diffstat (limited to 'packages/client/src/widgets/widget.ts')
-rw-r--r--packages/client/src/widgets/widget.ts71
1 files changed, 71 insertions, 0 deletions
diff --git a/packages/client/src/widgets/widget.ts b/packages/client/src/widgets/widget.ts
new file mode 100644
index 0000000000..81239bfb3b
--- /dev/null
+++ b/packages/client/src/widgets/widget.ts
@@ -0,0 +1,71 @@
+import { reactive, watch } from 'vue';
+import { throttle } from 'throttle-debounce';
+import { Form, GetFormResultType } from '@/scripts/form';
+import * as os from '@/os';
+
+export type Widget<P extends Record<string, unknown>> = {
+ id: string;
+ data: Partial<P>;
+};
+
+export type WidgetComponentProps<P extends Record<string, unknown>> = {
+ widget?: Widget<P>;
+};
+
+export type WidgetComponentEmits<P extends Record<string, unknown>> = {
+ (e: 'updateProps', props: P);
+};
+
+export type WidgetComponentExpose = {
+ name: string;
+ id: string | null;
+ configure: () => void;
+};
+
+export const useWidgetPropsManager = <F extends Form & Record<string, { default: any; }>>(
+ name: string,
+ propsDef: F,
+ props: Readonly<WidgetComponentProps<GetFormResultType<F>>>,
+ emit: WidgetComponentEmits<GetFormResultType<F>>,
+): {
+ widgetProps: GetFormResultType<F>;
+ save: () => void;
+ configure: () => void;
+} => {
+ const widgetProps = reactive(props.widget ? JSON.parse(JSON.stringify(props.widget.data)) : {});
+
+ const mergeProps = () => {
+ for (const prop of Object.keys(propsDef)) {
+ if (widgetProps.hasOwnProperty(prop)) continue;
+ widgetProps[prop] = propsDef[prop].default;
+ }
+ };
+ watch(widgetProps, () => {
+ mergeProps();
+ }, { deep: true, immediate: true, });
+
+ const save = throttle(3000, () => {
+ emit('updateProps', widgetProps)
+ });
+
+ const configure = async () => {
+ const form = JSON.parse(JSON.stringify(propsDef));
+ for (const item of Object.keys(form)) {
+ form[item].default = widgetProps[item];
+ }
+ const { canceled, result } = await os.form(name, form);
+ if (canceled) return;
+
+ for (const key of Object.keys(result)) {
+ widgetProps[key] = result[key];
+ }
+
+ save();
+ };
+
+ return {
+ widgetProps,
+ save,
+ configure,
+ };
+};