forked from laysense/laysite
92 lines
3.9 KiB
JavaScript
92 lines
3.9 KiB
JavaScript
'use strict';
|
|
|
|
jQuery(function ($) {
|
|
$('[data-favicon-editor]').each(function () {
|
|
const $editor = $(this);
|
|
const $form = $editor.closest('form');
|
|
const $status = $editor.find('[data-favicon-status]');
|
|
const $action = $editor.find('[name=favicon_action]');
|
|
const $png = $editor.find('[name=favicon_png]');
|
|
const $file = $editor.find('[data-favicon-file]');
|
|
const $current = $editor.find('[data-favicon-current]');
|
|
const $controls = $editor.find('[data-favicon-controls]');
|
|
const $zoom = $controls.find('[data-favicon-zoom]');
|
|
const $x = $controls.find('[data-favicon-x]');
|
|
const $y = $controls.find('[data-favicon-y]');
|
|
const $reset = $editor.find('[data-favicon-reset]');
|
|
const $canvas = $editor.find('[data-favicon-canvas]');
|
|
const canvas = $canvas.get(0);
|
|
const context = canvas.getContext('2d');
|
|
let image;
|
|
let selection = 0;
|
|
|
|
function draw() {
|
|
if (!image) return;
|
|
const {naturalWidth: width, naturalHeight: height} = image;
|
|
const side = Math.min(width, height) / Number($zoom.val());
|
|
const x = (width - side) * Number($x.val()) / 100;
|
|
const y = (height - side) * Number($y.val()) / 100;
|
|
context.clearRect(0, 0, canvas.width, canvas.height);
|
|
context.drawImage(image, x, y, side, side, 0, 0, canvas.width, canvas.height);
|
|
$png.val(canvas.toDataURL('image/png').split(',')[1]);
|
|
}
|
|
|
|
async function selectImage(url) {
|
|
const currentSelection = ++selection;
|
|
const $submit = $form.find(':submit');
|
|
$submit.prop('disabled', true);
|
|
$status.text('正在读取图片…');
|
|
try {
|
|
const next = new Image();
|
|
next.src = url;
|
|
await next.decode();
|
|
if (currentSelection !== selection) return;
|
|
if (!next.naturalWidth || !next.naturalHeight || Math.max(next.naturalWidth, next.naturalHeight) > 8192) {
|
|
throw new Error('Unsupported image dimensions');
|
|
}
|
|
image = next;
|
|
$zoom.val(1);
|
|
$x.add($y).val(50);
|
|
draw();
|
|
$action.val('replace');
|
|
$controls.add($canvas).add($reset).prop('hidden', false);
|
|
$current.prop('hidden', true);
|
|
$status.text('调整裁剪范围后,点击「保存设置」。');
|
|
} catch {
|
|
if (currentSelection === selection) $status.text('无法读取图片,请选择边长不超过 8192 px 的常见图片格式。');
|
|
} finally {
|
|
if (currentSelection === selection) $submit.prop('disabled', false);
|
|
if (url.startsWith('blob:')) URL.revokeObjectURL(url);
|
|
}
|
|
}
|
|
|
|
$file.on('change', function () {
|
|
const [file] = this.files;
|
|
if (!file) return;
|
|
if (file.size > 16 * 1024 * 1024) {
|
|
$status.text('请选用不超过 16 MB 的源图片。');
|
|
return;
|
|
}
|
|
selectImage(URL.createObjectURL(file));
|
|
});
|
|
$controls.on('input', 'input', draw);
|
|
$editor.on('click', '[data-favicon-library]', () => {
|
|
$('#media-picker').trigger('studio:pick', ['image/', (file) => {
|
|
$file.val('');
|
|
selectImage(file.url);
|
|
}]);
|
|
});
|
|
$reset.on('click', () => {
|
|
++selection;
|
|
image = null;
|
|
$file.val('');
|
|
$action.val('reset');
|
|
$png.val('');
|
|
$canvas.add($controls).add($reset).prop('hidden', true);
|
|
$current.attr('src', $editor.data('defaultIcon')).prop('hidden', false);
|
|
$form.find(':submit').prop('disabled', false);
|
|
$status.text('保存设置后将恢复默认图标。');
|
|
});
|
|
});
|
|
});
|