Timothy J. Baek 10 месяцев назад
Родитель
Сommit
0e7db89117

+ 20 - 2
src/lib/components/workspace/Functions.svelte

@@ -43,8 +43,26 @@
 
 	let showDeleteConfirm = false;
 
-	const shareHandler = async (tool) => {
-		console.log(tool);
+	const shareHandler = async (item) => {
+		toast.success($i18n.t('Redirecting you to OpenWebUI Community'));
+
+		const url = 'https://openwebui.com';
+
+		const tab = await window.open(`${url}/tools/create`, '_blank');
+
+		// Define the event handler function
+		const messageHandler = (event) => {
+			if (event.origin !== url) return;
+			if (event.data === 'loaded') {
+				tab.postMessage(JSON.stringify(item), '*');
+
+				// Remove the event listener after handling the message
+				window.removeEventListener('message', messageHandler);
+			}
+		};
+
+		window.addEventListener('message', messageHandler, false);
+		console.log(item);
 	};
 
 	const cloneHandler = async (func) => {

+ 20 - 2
src/lib/components/workspace/Tools.svelte

@@ -39,8 +39,26 @@
 
 	let showDeleteConfirm = false;
 
-	const shareHandler = async (tool) => {
-		console.log(tool);
+	const shareHandler = async (item) => {
+		toast.success($i18n.t('Redirecting you to OpenWebUI Community'));
+
+		const url = 'https://openwebui.com';
+
+		const tab = await window.open(`${url}/tools/create`, '_blank');
+
+		// Define the event handler function
+		const messageHandler = (event) => {
+			if (event.origin !== url) return;
+			if (event.data === 'loaded') {
+				tab.postMessage(JSON.stringify(item), '*');
+
+				// Remove the event listener after handling the message
+				window.removeEventListener('message', messageHandler);
+			}
+		};
+
+		window.addEventListener('message', messageHandler, false);
+		console.log(item);
 	};
 
 	const cloneHandler = async (tool) => {

+ 12 - 0
src/routes/(app)/workspace/functions/create/+page.svelte

@@ -36,6 +36,18 @@
 	};
 
 	onMount(() => {
+		window.addEventListener('message', async (event) => {
+			if (
+				!['https://openwebui.com', 'https://www.openwebui.com', 'http://localhost:9999'].includes(
+					event.origin
+				)
+			)
+				return;
+
+			func = JSON.parse(event.data);
+			console.log(func);
+		});
+
 		if (sessionStorage.function) {
 			func = JSON.parse(sessionStorage.function);
 			sessionStorage.removeItem('function');

+ 12 - 0
src/routes/(app)/workspace/tools/create/+page.svelte

@@ -33,6 +33,18 @@
 	};
 
 	onMount(() => {
+		window.addEventListener('message', async (event) => {
+			if (
+				!['https://openwebui.com', 'https://www.openwebui.com', 'http://localhost:9999'].includes(
+					event.origin
+				)
+			)
+				return;
+
+			tool = JSON.parse(event.data);
+			console.log(tool);
+		});
+
 		if (sessionStorage.tool) {
 			tool = JSON.parse(sessionStorage.tool);
 			sessionStorage.removeItem('tool');