Skip to content

Commit f36f35e

Browse files
committed
fix: full attributes are re-assign upon reactivity
Fixes multiple `initial` resets and improve perf
1 parent 5023179 commit f36f35e

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

lib/ModernCropper.vue

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { SetNonNullable } from 'type-fest'
66
77
export interface PassThroughOptions {
88
/**
9-
* The new Cropper() class construction
9+
* The `new Cropper()` class construction
1010
*/
1111
cropper?: { constructOptions?: CropperOptions }
1212
@@ -182,30 +182,35 @@ function _setElementAttributes(element: HTMLElement, attributes: Record<any, any
182182
let doReset = false
183183
184184
Object.entries(attributes).forEach(([attribute, value]) => {
185-
if (value === undefined)
185+
// Convert camelCase to kebab-case
186+
const _attribute = attribute.replaceAll(/([a-z])([A-Z])/g, (_match, p1: string, p2: string) => `${p1}-${p2.toLowerCase()}`) as keyof HTMLElement
187+
const currentValue = element[_attribute] ?? element.getAttribute(_attribute)
188+
189+
if (value === undefined || currentValue === value)
186190
return
187191
188-
if (attribute.includes('initial') && resetOnInitialAttributes)
192+
if (_attribute.includes('initial') && resetOnInitialAttributes)
189193
doReset = true
190194
191195
if (value === false || value === null)
192-
return element.removeAttribute(attribute)
196+
return element.removeAttribute(_attribute)
193197
194198
// Handle event attributes
195-
if (/^on[a-z]+$/.test(attribute)) {
199+
if (/^on[a-z]+$/.test(_attribute)) {
196200
if (useEventListeners)
197-
element.addEventListener(attribute.replace(/^on/, ''), value)
198-
else
199-
// @ts-expect-error index signature unknown
200-
element[attribute] = value
201+
element.addEventListener(_attribute.replace(/^on/, ''), value)
202+
else {
203+
// @ts-expect-error cannot assign to readonly property
204+
element[_attribute] = value
205+
}
201206
return
202207
}
203208
204-
element.setAttribute(attribute.replaceAll(/([a-z])([A-Z])/g, (_match, p1: string, p2: string) => `${p1}-${p2.toLowerCase()}`), value)
209+
element.setAttribute(_attribute, value)
205210
})
206211
207212
if (doReset) {
208-
console.warn(`[ModernCropper]: "initial"-type attribute detected, will call $reset(), but the selection may disappear due to https://github.com/fengyuanchen/cropperjs/issues/1157`)
213+
console.warn(`[ModernCropper]: "initial"-type attribute detected, will call $reset()`)
209214
// @ts-expect-error $reset don't exist on HTMLElement
210215
element.$reset()
211216
}

0 commit comments

Comments
 (0)