forked from laysense/laysite
Add multiple gradient stops and image fill modes
This commit is contained in:
+26
-25
@@ -396,6 +396,8 @@ jQuery(function ($) {
|
||||
const $fillTabs = $fill.find('[data-fill-tab]');
|
||||
const $fillError = $fill.find('[data-fill-error]');
|
||||
const fillInput = (name) => $fillForm.find('[name="' + name + '"]');
|
||||
const fillControls = window.LaySiteFill;
|
||||
let gradientEditor;
|
||||
const colorLabels = JSON.parse($fill.attr('data-color-labels'));
|
||||
let fillMode;
|
||||
let fillTarget;
|
||||
@@ -469,18 +471,10 @@ jQuery(function ($) {
|
||||
|
||||
function installStyleRule(token) {
|
||||
if (!colorSheet || canvasColors.has(token)) return;
|
||||
const color = token.match(/^studio-style-(color|fill-solid)-([0-9a-f]{6})$/);
|
||||
const gradient = token.match(/^studio-style-fill-gradient-(0|[1-9]\d{0,2})-([0-9a-f]{6})-([0-9a-f]{6})$/);
|
||||
let rule;
|
||||
if (color) rule = (color[1] === 'color' ? 'color:' : 'background:') + '#' + color[2] + '!important';
|
||||
else if (gradient && Number(gradient[1]) <= 360) {
|
||||
rule = 'background:linear-gradient(' + gradient[1] + 'deg,#' + gradient[2] + ',#' + gradient[3] + ')!important';
|
||||
} else if (token.startsWith('studio-style-fill-image-')) {
|
||||
const url = decodeImage(token.slice('studio-style-fill-image-'.length));
|
||||
if (!url) return;
|
||||
const escaped = url.replace(/["<>]/g, (character) => '\\' + character.charCodeAt(0).toString(16) + ' ');
|
||||
rule = 'background-image:url("' + escaped + '")!important;background-position:center!important;background-size:cover!important;background-repeat:no-repeat!important';
|
||||
}
|
||||
const color = token.match(/^studio-style-color-([0-9a-f]{6})$/);
|
||||
const fillPrefix = 'studio-style-fill-';
|
||||
const rule = color ? 'color:#' + color[1] + '!important'
|
||||
: token.startsWith(fillPrefix) && fillControls?.declarations(fillControls.read(token.slice(fillPrefix.length), decodeImage));
|
||||
if (!rule) return;
|
||||
colorSheet.insertRule('.' + token + '{' + rule + '}', colorSheet.cssRules.length);
|
||||
canvasColors.add(token);
|
||||
@@ -1007,12 +1001,13 @@ jQuery(function ($) {
|
||||
const styles = doc.defaultView.getComputedStyle(root);
|
||||
return Object.entries(colorLabels).flatMap(([key, label]) => {
|
||||
const color = colors[key] || cssHex(styles.getPropertyValue('--' + key).trim()) || (key === 'button-ink' ? '#ffffff' : '');
|
||||
return color ? [{color, label}] : [];
|
||||
return color ? [{color, label, key}] : [];
|
||||
});
|
||||
}
|
||||
|
||||
function showFillTab(mode, focus = false) {
|
||||
fillMode = mode;
|
||||
$fillError.prop('hidden', true);
|
||||
$fillTabs.each(function () {
|
||||
const selected = this.dataset.fillTab === mode;
|
||||
$(this).attr('aria-selected', String(selected)).prop('tabIndex', selected ? 0 : -1);
|
||||
@@ -1020,7 +1015,7 @@ jQuery(function ($) {
|
||||
});
|
||||
$fill.find('[data-fill-panel]').each(function () {
|
||||
this.hidden = this.dataset.fillPanel !== mode;
|
||||
$(this).find('input').prop('disabled', this.hidden);
|
||||
$(this).find('input, select').prop('disabled', this.hidden);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1030,15 +1025,18 @@ jQuery(function ($) {
|
||||
textColorDialog = text;
|
||||
const computed = doc.defaultView.getComputedStyle(fillTarget);
|
||||
const palette = activePalette();
|
||||
const [type, ...parts] = (classesOf(fillTarget).fill || '').slice('studio-style-fill-'.length).split('-');
|
||||
const fill = fillControls?.read((classesOf(fillTarget).fill || '').slice('studio-style-fill-'.length), decodeImage);
|
||||
if (!text) {
|
||||
gradientEditor ??= fillControls?.gradientEditor($fill, cssHex);
|
||||
gradientEditor?.setup(fill, palette.find(({key}) => key === 'accent')?.color);
|
||||
}
|
||||
const sample = selectedText()[0]?.node.parentElement || fillTarget;
|
||||
const foreground = cssHex(doc.defaultView.getComputedStyle(sample).color);
|
||||
fillInput('solid').val(text ? foreground || '#111a2e'
|
||||
: type === 'solid' ? '#' + parts[0] : cssHex(computed.backgroundColor) || '#ffffff');
|
||||
fillInput('start').val(type === 'gradient' ? '#' + parts[1] : '#ffffff');
|
||||
fillInput('end').val(type === 'gradient' ? '#' + parts[2] : '#214ed7');
|
||||
fillInput('angle').val(type === 'gradient' ? parts[0] : '90');
|
||||
fillInput('image').val(type === 'image' ? decodeImage(parts.join('-')) : '');
|
||||
: fill?.kind === 'solid' ? fill.color : cssHex(computed.backgroundColor) || '#ffffff');
|
||||
fillInput('image').val(fill?.url || '');
|
||||
fillInput('image_size').val(fill?.size || 'cover');
|
||||
fillInput('image_position').val(fill?.position || 'c');
|
||||
$fill.find('[data-fill-title]').text(text ? '文字颜色' : backgroundLabel(fillTarget));
|
||||
$fill.find('[data-fill-tabs]').prop('hidden', text);
|
||||
$fill.find('[data-fill-panel="solid"]').attr('aria-labelledby', text ? 'canvas-fill-title' : 'fill-tab-solid');
|
||||
@@ -1052,7 +1050,7 @@ jQuery(function ($) {
|
||||
$row.append($('<button>', {type: 'button', 'data-swatch': color, title: label + ' ' + color, 'aria-label': label + ' ' + color}).append(svg));
|
||||
}
|
||||
});
|
||||
showFillTab(['solid', 'gradient', 'image'].includes(type) && !text ? type : 'solid');
|
||||
showFillTab(!text && fill ? fill.kind : 'solid');
|
||||
$fill.get(0).showModal();
|
||||
if (text) fillInput('solid').trigger('focus');
|
||||
}
|
||||
@@ -1073,7 +1071,7 @@ jQuery(function ($) {
|
||||
: ($fillTabs.index(this) + offset + $fillTabs.length) % $fillTabs.length;
|
||||
showFillTab($fillTabs.get(index).dataset.fillTab, true);
|
||||
}).on('click', '[data-swatch]', function () {
|
||||
fillInput(this.parentElement.dataset.fillSwatches).val(this.dataset.swatch);
|
||||
fillInput(this.parentElement.dataset.fillSwatches).val(this.dataset.swatch).trigger('input');
|
||||
}).on('click', '[data-fill-media]', () => {
|
||||
openPicker('image/', (file) => fillInput('image').val(file.url).trigger('focus'));
|
||||
}).on('click', '[data-fill-reset]', () => applyFill(''))
|
||||
@@ -1085,15 +1083,18 @@ jQuery(function ($) {
|
||||
} else if (fillMode === 'solid') {
|
||||
applyFill('solid-' + fillInput('solid').val().slice(1));
|
||||
} else if (fillMode === 'gradient') {
|
||||
const angle = fillInput('angle').val() || '90';
|
||||
applyFill(['gradient', Number(angle), fillInput('start').val().slice(1), fillInput('end').val().slice(1)].join('-'));
|
||||
if (!gradientEditor) {
|
||||
$fillError.text('渐变编辑器暂时无法加载,请刷新页面后重试。').prop('hidden', false);
|
||||
return;
|
||||
}
|
||||
applyFill(gradientEditor.value());
|
||||
} else {
|
||||
const url = fillInput('image').val().trim();
|
||||
if (!safeLink(url, true) || new TextEncoder().encode(url).length > 1000) {
|
||||
$fillError.text('请选择图片,或输入有效的站内路径或 HTTPS 链接(最多 1000 字节)。').prop('hidden', false);
|
||||
return;
|
||||
}
|
||||
applyFill('image-' + encodeImage(url));
|
||||
applyFill(['image-v2', fillInput('image_size').val(), fillInput('image_position').val(), encodeImage(url)].join('-'));
|
||||
}
|
||||
});
|
||||
$color.on('click', () => openFill(true));
|
||||
|
||||
Reference in New Issue
Block a user