From 641909aa31b2683b89fe62a219f7d5f56fbad934 Mon Sep 17 00:00:00 2001 From: Blaster4385 Date: Thu, 22 Feb 2024 11:02:06 +0530 Subject: feat: copy url to clipboard on save --- client/src/components/Editor/Editor.jsx | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/client/src/components/Editor/Editor.jsx b/client/src/components/Editor/Editor.jsx index dc7f901..0a78ca8 100644 --- a/client/src/components/Editor/Editor.jsx +++ b/client/src/components/Editor/Editor.jsx @@ -47,6 +47,32 @@ const Editor = () => { }); const data = await response.json(); if (response.ok) { + const isURL = URL_REGEX.test(text); + if (isURL) { + navigator.clipboard.writeText(`${CLIENT_BASE_URL}/r/${data.id}`).then(function () { + alert("Short URL copied to clipboard!"); + }, function (err) { + try { + var successful = document.execCommand('copy'); + alert("Short URL copied to clipboard!"); + } catch (err) { + console.log('Oops, unable to copy'); + } + }); + } + else { + navigator.clipboard.writeText(`${CLIENT_BASE_URL}/r/${data.id}`).then(function () { + navigator.clipboard.writeText(`${CLIENT_BASE_URL}/${data.id}`); + alert("URL copied to clipboard!"); + }, function (err) { + try { + var successful = document.execCommand('copy'); + alert("URL copied to clipboard!"); + } catch (err) { + console.log('Oops, unable to copy'); + } + }); + } navigate(`/${data.id}`); } else { console.error(data); -- cgit v1.2.3-73-gaa49b