From 0c83273508a7d88b0514d82288380f503cccb7b6 Mon Sep 17 00:00:00 2001 From: Sanskar Soni Date: Tue, 19 Sep 2023 14:38:29 +0530 Subject: [PATCH] Improved: ShopiImg component with vue composition api (#170) --- src/components/ShopifyImg.ts | 52 ----------------------------------- src/components/ShopifyImg.vue | 47 +++++++++++++++++++++++++++++++ src/components/index.ts | 3 +- src/index.ts | 3 +- 4 files changed, 50 insertions(+), 55 deletions(-) delete mode 100644 src/components/ShopifyImg.ts create mode 100644 src/components/ShopifyImg.vue diff --git a/src/components/ShopifyImg.ts b/src/components/ShopifyImg.ts deleted file mode 100644 index 4323be3..0000000 --- a/src/components/ShopifyImg.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { defineComponent } from "vue" -import { shopifyImgContext as context } from "../index"; - -export default defineComponent({ - template: ` - - `, - data() { - return { - imageUrl: context.defaultImgUrl - } - }, - props: ['src', 'size'], - mounted() { - this.setImageUrl(); - }, - updated() { - this.setImageUrl(); - }, - methods: { - prepareImgUrl(src: string, size?: string) { - // return original size if no size is given - if (!size) return src - // remove any current image size then add the new image size - return src - .replace(/_(pico|icon|thumb|small|compact|medium|large|grande|original|1024x1024|2048x2048|master)+\./g, '.') - .replace(/\.jpg|\.png|\.gif|\.jpeg/g, function (match) { - return '_' + size + match; - }) - }, - checkIfImageExists(src: string) { - return new Promise((resolve, reject) => { - const img = new Image(); - img.onload = function () { - resolve(true); - } - img.onerror = function () { - reject(false); - } - img.src = src; - }) - }, - setImageUrl() { - if (this.src) { - const src: string = this.prepareImgUrl(this.src, this.size) - this.checkIfImageExists(src).then(() => { - this.imageUrl = src; - }) - } - } - } -}) \ No newline at end of file diff --git a/src/components/ShopifyImg.vue b/src/components/ShopifyImg.vue new file mode 100644 index 0000000..c7d1b4d --- /dev/null +++ b/src/components/ShopifyImg.vue @@ -0,0 +1,47 @@ + + + \ No newline at end of file diff --git a/src/components/index.ts b/src/components/index.ts index b97ebef..bc7dc87 100644 --- a/src/components/index.ts +++ b/src/components/index.ts @@ -16,6 +16,7 @@ import '@ionic/vue/css/display.css'; import MenuFooterNavigation from './MenuFooterNavigation.vue'; import ProductIdentifier from './ProductIdentifier.vue'; +import ShopifyImg from './ShopifyImg.vue'; -export { MenuFooterNavigation, ProductIdentifier }; +export { MenuFooterNavigation, ProductIdentifier, ShopifyImg }; diff --git a/src/index.ts b/src/index.ts index 910e645..f980b46 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,12 +3,11 @@ import { useProductIdentificationStore } from "./store/productIdentification"; import { useAuthStore } from "./store/auth"; import Login from "./components/Login"; -import ShopifyImg from "./components/ShopifyImg"; import { goToOms } from "./utils"; import { initialiseFirebaseApp } from "./utils/firebase" import piniaPluginPersistedstate from 'pinia-plugin-persistedstate' -import { MenuFooterNavigation, ProductIdentifier } from "./components"; +import { MenuFooterNavigation, ProductIdentifier, ShopifyImg } from "./components"; // TODO: handle cases when the store from app or pinia store are not available // creating a pinia store for the plugin