Skip to content

Commit

Permalink
Merge pull request #7 from motea927/dev
Browse files Browse the repository at this point in the history
fix: force update with vite.config's option
  • Loading branch information
motea927 authored Jan 30, 2024
2 parents 4eee965 + 6bee20b commit a36a85f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 20 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
23 changes: 13 additions & 10 deletions popup/src/components/LayoutPreview.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ beforeEach(() => {
window._unpluginOverlayLayout = {}
})

const mountElement = (props?: { isDraggable?: boolean; imageUrl?: string; opacity?: number }) => {
const mountElement = (props?: {
isDraggable?: boolean
imageUrl?: string
opacity?: number
userStyle?: string
}) => {
const defaultProps = {
isDraggable: true,
imageUrl: '',
opacity: 100
opacity: 100,
userStyle: ''
}
return shallowMount(LayoutPreview, {
props: {
Expand All @@ -22,15 +28,12 @@ const mountElement = (props?: { isDraggable?: boolean; imageUrl?: string; opacit

describe('LayoutPreview.vue', () => {
it('global style config can work', () => {
const style = 'width: 20px'
window._unpluginOverlayLayout = {
layoutPreview: {
style
}
}
const userStyle = 'width: 20px'

const wrapper = mountElement()
expect(wrapper.attributes('style')).toContain(style)
const wrapper = mountElement({
userStyle
})
expect(wrapper.attributes('style')).toContain(userStyle)
})

it('computes the style correctly', () => {
Expand Down
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 a36a85f

Please sign in to comment.