Skip to content

Commit

Permalink
✨ feat: 增加vue的hooks函数
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-boide committed May 10, 2024
1 parent b90f1d9 commit ac12d02
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@
"stylelint": "^15",
"ts-json-schema-generator": "^1.4.0",
"typescript": "^5",
"vitest": "^1"
"vitest": "^1",
"vue": "^3.4.27"
},
"peerDependencies": {
"react": ">=18",
Expand Down
1 change: 1 addition & 0 deletions src/client/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './useOnStandalonePluginInit';
export * from './usePluginSettings';
export * from './usePluginState';
export * from './useWatchPluginMessage';
export * from './vue';
4 changes: 4 additions & 0 deletions src/client/hooks/vue/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './useOnStandalonePluginInitVue';
export * from './usePluginSettingsVue';
export * from './usePluginStateVue';
export * from './useWatchPluginMessageVue';
15 changes: 15 additions & 0 deletions src/client/hooks/vue/useOnStandalonePluginInitVue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { onMounted } from 'vue';

import { PluginPayload, lobeChat } from '@/client';

export const useOnStandalonePluginInitVue = <T = any>(
callback: (payload: PluginPayload<T>) => void,
) => {
onMounted(() => {
lobeChat.getPluginPayload().then((e) => {
if (!e) return;

callback(e);
});
});
};
20 changes: 20 additions & 0 deletions src/client/hooks/vue/usePluginSettingsVue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { UnwrapRef, onMounted, ref } from 'vue';

import { lobeChat } from '@/client';

export const usePluginSettingsVue = <T>(initialValue: T) => {
const value = ref(initialValue);
onMounted(() => {
lobeChat.getPluginSettings().then((e) => {
if (!e) return;
value.value = e;
});
});

const updateValue = (newValue: T) => {
value.value = newValue as UnwrapRef<T>;
lobeChat.setPluginSettings(newValue);
};

return [value, updateValue] as const;
};
28 changes: 28 additions & 0 deletions src/client/hooks/vue/usePluginStateVue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { UnwrapRef, ref, watch } from 'vue';

import { lobeChat } from '@/client';

export const usePluginStateVue = <T>(key: string, initialValue: T) => {
const value = ref(initialValue);

watch(
() => key,
(newValue) => {
lobeChat.getPluginState(newValue).then((e) => {
if (!e) return;

value.value = e;
});
},
{
immediate: true,
},
);

const updateValue = (newValue: T) => {
value.value = newValue as UnwrapRef<T>;
lobeChat.setPluginState(key, value);
};

return [value, updateValue] as const;
};
30 changes: 30 additions & 0 deletions src/client/hooks/vue/useWatchPluginMessageVue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { UnwrapRef, onMounted, ref } from 'vue';

import { PluginChannel } from '../../const';
import { PluginRenderProps } from '../../type';
import { onReceiveData } from '../../utils';

export const useWatchPluginMessageVue = <T = any>() => {
const result = ref<{ data: T; loading: boolean }>({
data: undefined as T,
loading: true,
});

const receiverData = (e: MessageEvent) => {
onReceiveData(e, (data: PluginRenderProps<T>) => {
result.value = { data: data.content as UnwrapRef<T>, loading: false };
});
};

onMounted(() => {
window?.addEventListener('message', receiverData);

top?.postMessage({ type: PluginChannel.pluginReadyForRender }, '*');

return () => {
window?.removeEventListener('message', receiverData);
};
});

return result;
};

0 comments on commit ac12d02

Please sign in to comment.