Merge pull request 'fix(user): move avatar upload to hover overlay' (#325) from worktree-user-avatar-system into main
Reviewed-on: CYL/wordpress-theme-aksrt#325
This commit is contained in:
@@ -136,24 +136,76 @@
|
||||
}
|
||||
|
||||
.uc-header__avatar {
|
||||
position: relative;
|
||||
flex-shrink: 0;
|
||||
width: 140px;
|
||||
height: 140px;
|
||||
border-radius: 6px;
|
||||
}
|
||||
|
||||
/* 对齐 zibll: 140px, 6px 圆角 */
|
||||
.uc-header__avatar img {
|
||||
width: 140px;
|
||||
height: 140px;
|
||||
border-radius: 6px;
|
||||
display: block;
|
||||
box-sizing: border-box;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
border-radius: inherit;
|
||||
border: 4px solid var(--surface-1);
|
||||
box-shadow: 0 4px 12px rgba(0,0,0,0.12);
|
||||
object-fit: cover;
|
||||
transition: transform 0.2s ease;
|
||||
}
|
||||
|
||||
.uc-header__avatar img:hover {
|
||||
.uc-header__avatar-upload {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
z-index: 1;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 7px;
|
||||
width: 100%;
|
||||
padding: 0;
|
||||
border: 0;
|
||||
border-radius: inherit;
|
||||
background: rgba(0, 0, 0, 0.52);
|
||||
color: #fff;
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
line-height: 1;
|
||||
cursor: pointer;
|
||||
opacity: 0;
|
||||
visibility: hidden;
|
||||
transition: opacity 0.2s ease, visibility 0.2s ease;
|
||||
}
|
||||
|
||||
.uc-header__avatar-upload i {
|
||||
font-size: 22px;
|
||||
}
|
||||
|
||||
.uc-header__avatar:hover .uc-header__avatar-upload,
|
||||
.uc-header__avatar:focus-within .uc-header__avatar-upload {
|
||||
opacity: 1;
|
||||
visibility: visible;
|
||||
}
|
||||
|
||||
.uc-header__avatar:hover img,
|
||||
.uc-header__avatar:focus-within img {
|
||||
transform: scale(1.03);
|
||||
}
|
||||
|
||||
.uc-header__avatar-upload:focus-visible {
|
||||
outline: 2px solid #fff;
|
||||
outline-offset: -5px;
|
||||
}
|
||||
|
||||
.uc-header__avatar-upload:disabled {
|
||||
cursor: wait;
|
||||
opacity: 1;
|
||||
visibility: visible;
|
||||
}
|
||||
|
||||
.uc-header__info {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
@@ -289,11 +341,25 @@
|
||||
gap: 14px;
|
||||
}
|
||||
|
||||
.uc-header__avatar img {
|
||||
.uc-header__avatar {
|
||||
width: 90px;
|
||||
height: 90px;
|
||||
}
|
||||
|
||||
.uc-header__avatar img {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.uc-header__avatar-upload {
|
||||
gap: 5px;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.uc-header__avatar-upload i {
|
||||
font-size: 18px;
|
||||
}
|
||||
|
||||
.uc-header__name {
|
||||
font-size: 1.15rem;
|
||||
}
|
||||
|
||||
@@ -206,7 +206,6 @@
|
||||
var button = document.getElementById(upload.buttonId || 'aksrt-upload-avatar-btn');
|
||||
var input = document.getElementById(upload.inputId || 'aksrt-avatar-file-input');
|
||||
var image = document.getElementById(upload.imageId || 'aksrt-user-avatar-image');
|
||||
var removeButton = document.getElementById(upload.removeButtonId || 'aksrt-remove-avatar-btn');
|
||||
if (!button || !input || button.dataset.aksrtAvatarBound === '1') return;
|
||||
button.dataset.aksrtAvatarBound = '1';
|
||||
|
||||
@@ -247,7 +246,6 @@
|
||||
var url = payload.url || (payload.data && payload.data.url) || '';
|
||||
if (!url) throw payload;
|
||||
if (image) image.src = url;
|
||||
if (removeButton) removeButton.hidden = false;
|
||||
notify(payload.message || upload.successText || '头像已更新', 'success');
|
||||
})
|
||||
.catch(function (error) {
|
||||
@@ -258,40 +256,6 @@
|
||||
input.value = '';
|
||||
});
|
||||
});
|
||||
|
||||
if (removeButton) {
|
||||
var removeLabel = removeButton.querySelector('span');
|
||||
var removeIdleText = upload.removeText || (removeLabel ? removeLabel.textContent : '恢复默认头像');
|
||||
removeButton.addEventListener('click', function () {
|
||||
if (!window.confirm(upload.confirmRemoveText || '确定恢复为默认头像?')) return;
|
||||
removeButton.disabled = true;
|
||||
if (removeLabel) removeLabel.textContent = upload.removingText || '恢复中…';
|
||||
|
||||
fetch(upload.restUrl, {
|
||||
method: 'DELETE',
|
||||
credentials: 'same-origin',
|
||||
headers: { 'X-WP-Nonce': upload.restNonce || '' }
|
||||
})
|
||||
.then(function (response) {
|
||||
return response.json().then(function (payload) {
|
||||
if (!response.ok || !payload || !payload.success) throw payload || {};
|
||||
return payload;
|
||||
});
|
||||
})
|
||||
.then(function (payload) {
|
||||
if (image && payload.url) image.src = payload.url;
|
||||
removeButton.hidden = true;
|
||||
notify(payload.message || upload.removeSuccessText || '已恢复默认头像', 'success');
|
||||
})
|
||||
.catch(function (error) {
|
||||
notify((error && error.message) || upload.errorText || '头像操作失败', 'error');
|
||||
})
|
||||
.finally(function () {
|
||||
removeButton.disabled = false;
|
||||
if (removeLabel) removeLabel.textContent = removeIdleText;
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function init() {
|
||||
|
||||
@@ -380,20 +380,15 @@ if (!function_exists('aksrt_user_center_avatar_upload_config')) {
|
||||
'restUrl' => rest_url('aksrt/v1/user/avatar'),
|
||||
'restNonce' => wp_create_nonce('wp_rest'),
|
||||
'buttonId' => 'aksrt-upload-avatar-btn',
|
||||
'removeButtonId' => 'aksrt-remove-avatar-btn',
|
||||
'inputId' => 'aksrt-avatar-file-input',
|
||||
'imageId' => 'aksrt-user-avatar-image',
|
||||
'fieldName' => 'avatar',
|
||||
'maxSize' => $max_size,
|
||||
'idleText' => __('更换头像', 'aksrt'),
|
||||
'loadingText' => __('上传中…', 'aksrt'),
|
||||
'removeText' => __('恢复默认头像', 'aksrt'),
|
||||
'removingText' => __('恢复中…', 'aksrt'),
|
||||
'confirmRemoveText' => __('确定恢复为默认头像?当前上传的头像文件将被删除。', 'aksrt'),
|
||||
'tooLargeText' => sprintf(__('图片大小不能超过 %dMB', 'aksrt'), (int) ($max_size / MB_IN_BYTES)),
|
||||
'successText' => __('头像已更新', 'aksrt'),
|
||||
'removeSuccessText' => __('已恢复默认头像', 'aksrt'),
|
||||
'errorText' => __('头像操作失败', 'aksrt'),
|
||||
'errorText' => __('头像上传失败', 'aksrt'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,13 @@ extract($context, EXTR_SKIP); // phpcs:ignore WordPress.PHP.DontExtract.extract_
|
||||
<div class="uc-header__content">
|
||||
<div class="uc-header__avatar">
|
||||
<?php echo aksrt_get_avatar_html($user_id, 120, ['id' => 'aksrt-user-avatar-image', 'alt' => $target_user->display_name, 'loading' => 'eager']); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
|
||||
<?php if ($show_avatar_edit) : ?>
|
||||
<button type="button" id="aksrt-upload-avatar-btn" class="uc-header__avatar-upload" title="<?php esc_attr_e('更换头像', 'aksrt'); ?>" aria-label="<?php esc_attr_e('更换头像', 'aksrt'); ?>">
|
||||
<i class="fas fa-camera" aria-hidden="true"></i>
|
||||
<span><?php esc_html_e('更换头像', 'aksrt'); ?></span>
|
||||
</button>
|
||||
<input type="file" id="aksrt-avatar-file-input" accept="image/jpeg,image/png,image/webp,image/gif" hidden>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<div class="uc-header__info">
|
||||
<h1 class="uc-header__name">
|
||||
@@ -90,15 +97,6 @@ extract($context, EXTR_SKIP); // phpcs:ignore WordPress.PHP.DontExtract.extract_
|
||||
<?php endif; ?>
|
||||
</a>
|
||||
<?php endif; ?>
|
||||
<?php if ($show_avatar_edit) : ?>
|
||||
<button type="button" id="aksrt-upload-avatar-btn" class="btn-ghost btn-sm">
|
||||
<i class="fas fa-camera" aria-hidden="true"></i> <span><?php esc_html_e('更换头像', 'aksrt'); ?></span>
|
||||
</button>
|
||||
<input type="file" id="aksrt-avatar-file-input" accept="image/jpeg,image/png,image/webp,image/gif" hidden>
|
||||
<button type="button" id="aksrt-remove-avatar-btn" class="btn-ghost btn-sm"<?php echo get_user_meta($user_id, 'aksrt_avatar_attachment_id', true) ? '' : ' hidden'; ?>>
|
||||
<i class="fas fa-undo" aria-hidden="true"></i> <span><?php esc_html_e('恢复默认头像', 'aksrt'); ?></span>
|
||||
</button>
|
||||
<?php endif; ?>
|
||||
<a href="<?php echo esc_url(aksrt_get_user_home_url()); ?>" class="btn-ghost btn-sm">
|
||||
<i class="fas fa-map-marker-alt" aria-hidden="true"></i> <?php esc_html_e('我的主页', 'aksrt'); ?>
|
||||
</a>
|
||||
|
||||
Reference in New Issue
Block a user