Skip to content

Commit

Permalink
fix: force update with vite.config's option
Browse files Browse the repository at this point in the history
  • Loading branch information
motea927 committed Jan 30, 2024
1 parent 326af3a commit 049c8e4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 10 deletions.
33 changes: 26 additions & 7 deletions popup/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,53 @@
:is-draggable="state.isDraggable"
:image-url="state.layoutPreview.imageUrl"
:opacity="state.layoutPreview.opacity"
:user-style="state.layoutPreview.style"
/>
</Teleport>
</main>
</template>

<script setup lang="ts">
import { useStorage } from '@vueuse/core'
import { onMounted } from 'vue'
import IconEye from '@/components/icons/IconEye.vue'
import IconDraggable from '@/components/icons/IconDraggable.vue'
import IconImageUpload from '@/components/icons/IconImageUpload.vue'
import IconOpacity from '@/components/icons/IconOpacity.vue'
import LayoutPreview from '@/components/LayoutPreview.vue'
import { deepEqual } from '@/utils'
const getDefaultLayoutPreview = () => {
return {
style: window._unpluginOverlayLayout.layoutPreview?.style || '',
imageUrl: window._unpluginOverlayLayout.layoutPreview?.imageUrl || '',
opacity: window._unpluginOverlayLayout.layoutPreview?.opacity || 50
}
}
const state = useStorage(
'unplugin-overlay-layout',
{
cache: window._unpluginOverlayLayout,
isOpen: true,
isDraggable: false,
layoutPreview: {
style: '',
imageUrl: window._unpluginOverlayLayout.layoutPreview?.imageUrl || '',
opacity: window._unpluginOverlayLayout.layoutPreview?.opacity || 50
}
layoutPreview: getDefaultLayoutPreview()
},
sessionStorage
sessionStorage,
{ mergeDefaults: true }
)
onMounted(() => {
const isEqual = deepEqual(state.value.cache, window._unpluginOverlayLayout)
if (isEqual) return
// force update
state.value.cache = window._unpluginOverlayLayout
state.value.layoutPreview = getDefaultLayoutPreview()
})
const handleClickOperation = (offset: number) => {
if (offset > 0) {
state.value.layoutPreview.opacity = Math.min(100, state.value.layoutPreview.opacity + offset)
Expand All @@ -58,3 +76,4 @@ const handleClickOperation = (offset: number) => {
}
}
</script>
./utils
5 changes: 2 additions & 3 deletions popup/src/components/LayoutPreview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const props = defineProps<{
isDraggable: boolean
imageUrl: string
opacity: number
userStyle: StyleValue
}>()
const defaultStyle = computed<StyleValue>(() => {
Expand All @@ -20,8 +21,6 @@ const defaultStyle = computed<StyleValue>(() => {
}
})
const userStyle = window._unpluginOverlayLayout?.layoutPreview?.style || {}
const el = ref<HTMLElement | null>(null)
const { style: dragStyle } = useDraggable(el)
Expand All @@ -45,6 +44,6 @@ const controlStyle = computed<StyleValue[]>(() => {
})
const mergedStyle = computed<StyleValue>(() => {
return [defaultStyle.value, userStyle, ...controlStyle.value]
return [defaultStyle.value, props.userStyle, ...controlStyle.value]
})
</script>
35 changes: 35 additions & 0 deletions popup/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export function deepEqual(obj1: any, obj2: any, excludeProps: string[] = []): boolean {
// Check if both objects are of the same type
if (typeof obj1 !== typeof obj2) {
return false
}

// Check if both objects are primitive types or null
if (obj1 == null || obj2 == null || typeof obj1 !== 'object') {
return obj1 === obj2
}

// Get the keys of the objects
const keys1 = Object.keys(obj1)
const keys2 = Object.keys(obj2)

// Check if the number of keys is the same
if (keys1.length !== keys2.length) {
return false
}

// Iterate through the keys and recursively compare the values
for (const key of keys1) {
// Check if the current key should be excluded
if (excludeProps.includes(key)) {
continue
}

if (!deepEqual(obj1[key], obj2[key], excludeProps)) {
return false
}
}

// If all comparisons passed, the objects are deep equal
return true
}

0 comments on commit 049c8e4

Please sign in to comment.