Skip to content

Commit ceec10e

Browse files
committed
refactor: minor code movement
1 parent 59cd46f commit ceec10e

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed

lib/ModernCropper.vue

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ onMounted(async () => {
102102
hooksList = []
103103
})
104104
105-
// process top-level props
105+
// process top-level props (i.e: src, crossorigin)
106106
onCropperMounted(({ image }) => {
107107
image.$image.crossOrigin = crossorigin
108108
@@ -120,35 +120,13 @@ onCropperMounted(({ image }) => {
120120
}, { immediate: true })
121121
})
122122
123-
function setElementAttributes(element: HTMLElement, attributes: Record<any, any>) {
124-
let doReset = false
125-
126-
Object.entries(attributes).forEach(([attribute, value]) => {
127-
if (value === undefined)
128-
return
129-
130-
if (attribute.includes('initial') && resetOnInitialAttributes)
131-
doReset = true
132-
133-
if (value === false || value === null)
134-
return element.removeAttribute(attribute)
135-
136-
element.setAttribute(attribute.replaceAll(/([a-z])([A-Z])/g, (_match, p1: string, p2: string) => `${p1}-${p2.toLowerCase()}`), value)
137-
})
138-
139-
if (doReset) {
140-
console.warn(`[ModernCropper]: "initial"-type attribute detected, will call $reset(), but the selection may disappear due to https://github.com/fengyuanchen/cropperjs/issues/1157`)
141-
// @ts-expect-error $reset don't exist on HTMLElement
142-
element.$reset()
143-
}
144-
}
145123
// process passThrough options
146124
onCropperMounted(({ image, canvas, selection, selections }) => {
147125
watch(
148126
() => passThrough?.image,
149127
(newImagePassThrough) => {
150128
if (newImagePassThrough?.attributes)
151-
setElementAttributes(image, newImagePassThrough.attributes)
129+
_setElementAttributes(image, newImagePassThrough.attributes)
152130
},
153131
{ deep: true, immediate: true },
154132
)
@@ -157,7 +135,7 @@ onCropperMounted(({ image, canvas, selection, selections }) => {
157135
() => passThrough?.canvas,
158136
(newCanvasPassThrough) => {
159137
if (newCanvasPassThrough?.attributes)
160-
setElementAttributes(canvas, newCanvasPassThrough.attributes)
138+
_setElementAttributes(canvas, newCanvasPassThrough.attributes)
161139
},
162140
{ deep: true, immediate: true },
163141
)
@@ -166,7 +144,7 @@ onCropperMounted(({ image, canvas, selection, selections }) => {
166144
() => passThrough?.selection,
167145
(newSelectionPassThrough) => {
168146
if (newSelectionPassThrough?.attributes)
169-
setElementAttributes(selection, newSelectionPassThrough.attributes)
147+
_setElementAttributes(selection, newSelectionPassThrough.attributes)
170148
},
171149
{ deep: true, immediate: true },
172150
)
@@ -175,7 +153,7 @@ onCropperMounted(({ image, canvas, selection, selections }) => {
175153
() => passThrough?.selections,
176154
(newSelectionsPassThrough) => {
177155
if (newSelectionsPassThrough?.attributes)
178-
selections.forEach(selection => setElementAttributes(selection, newSelectionsPassThrough.attributes!))
156+
selections.forEach(selection => _setElementAttributes(selection, newSelectionsPassThrough.attributes!))
179157
},
180158
{ deep: true, immediate: true },
181159
)
@@ -193,6 +171,29 @@ defineExpose(reactive({
193171
cropperMounted,
194172
onCropperMounted,
195173
}) as DiscriminatedExpose)
174+
175+
function _setElementAttributes(element: HTMLElement, attributes: Record<any, any>) {
176+
let doReset = false
177+
178+
Object.entries(attributes).forEach(([attribute, value]) => {
179+
if (value === undefined)
180+
return
181+
182+
if (attribute.includes('initial') && resetOnInitialAttributes)
183+
doReset = true
184+
185+
if (value === false || value === null)
186+
return element.removeAttribute(attribute)
187+
188+
element.setAttribute(attribute.replaceAll(/([a-z])([A-Z])/g, (_match, p1: string, p2: string) => `${p1}-${p2.toLowerCase()}`), value)
189+
})
190+
191+
if (doReset) {
192+
console.warn(`[ModernCropper]: "initial"-type attribute detected, will call $reset(), but the selection may disappear due to https://github.com/fengyuanchen/cropperjs/issues/1157`)
193+
// @ts-expect-error $reset don't exist on HTMLElement
194+
element.$reset()
195+
}
196+
}
196197
</script>
197198

198199
<template>

0 commit comments

Comments
 (0)