Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加vue的hooks函数 #42

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 * as vue from './vue';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感觉入口这边不能这么写,非 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 './useOnStandalonePluginInit';
export * from './usePluginSettings';
export * from './usePluginState';
export * from './useWatchPluginMessage';
15 changes: 15 additions & 0 deletions src/client/hooks/vue/useOnStandalonePluginInit.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 useOnStandalonePluginInit = <T = any>(
callback: (payload: PluginPayload<T>) => void,
) => {
onMounted(() => {
lobeChat.getPluginPayload().then((e) => {
if (!e) return;

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

import { lobeChat } from '@/client';

export const usePluginSettings = <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>;
};

watch(
() => value.value,
(newValue) => {
lobeChat.setPluginSettings(newValue);
},
);

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

import { lobeChat } from '@/client';

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

watchEffect(() => {
lobeChat.getPluginState(key).then((e) => {
if (!e) return;

value.value = e;
});
});

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

watch(
() => value.value,
(newValue) => {
lobeChat.setPluginState(key, newValue);
},
);

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

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

export const useWatchPluginMessage = <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 }, '*');
});

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

return result.value;
};