fix(editor): 修复字号读取及工具栏逻辑
重构编辑器工具栏字号调整逻辑,优先读取内联样式字号, 修复字号显示不同步问题,并更新 README 文档
This commit is contained in:
72
js/script.js
72
js/script.js
@@ -1931,7 +1931,9 @@
|
||||
// 更新字号选择器
|
||||
const fontSizeValueEl = fontSizePicker.querySelector('.roller-value');
|
||||
if (fontSizeValueEl) {
|
||||
const displaySize = isNaN(fontSize) ? 16 : fontSize;
|
||||
// 优先读取 inline style(用户调整过的字号),再回退到 computed style
|
||||
const inlineSize = parseInt(element.style.fontSize);
|
||||
const displaySize = inlineSize || (isNaN(fontSize) ? 16 : fontSize);
|
||||
fontSizeValueEl.textContent = displaySize.toString();
|
||||
fontSizeValue = displaySize;
|
||||
}
|
||||
@@ -2451,47 +2453,47 @@
|
||||
const fontSizeUpBtn = fontSizeRoller.querySelector('.roller-up');
|
||||
const fontSizeDownBtn = fontSizeRoller.querySelector('.roller-down');
|
||||
|
||||
function updateFontSize(newSize, forceUpdate = false) {
|
||||
newSize = Math.max(10, Math.min(42, newSize));
|
||||
const targetInput = editingInput || selectedInput;
|
||||
|
||||
if (forceUpdate || targetInput) {
|
||||
targetInput.style.fontSize = newSize + 'px';
|
||||
fontSizeValue = newSize;
|
||||
fontSizeValueEl.textContent = newSize;
|
||||
} else {
|
||||
fontSizeValue = newSize;
|
||||
fontSizeValueEl.textContent = newSize;
|
||||
}
|
||||
// 获取当前选中输入框的实际字号
|
||||
function getCurrentFontSize() {
|
||||
var target = editingInput || selectedInput;
|
||||
if (!target) return fontSizeValue;
|
||||
var size = parseInt(window.getComputedStyle(target).fontSize);
|
||||
return isNaN(size) ? 16 : size;
|
||||
}
|
||||
|
||||
fontSizeUpBtn.addEventListener('click', () => {
|
||||
const targetInput = editingInput || selectedInput;
|
||||
if (targetInput) {
|
||||
const currentSize = parseInt(window.getComputedStyle(targetInput).fontSize) || fontSizeValue;
|
||||
updateFontSize(currentSize + 1, true);
|
||||
saveState();
|
||||
}
|
||||
function updateFontSize(newSize, forceUpdate) {
|
||||
newSize = Math.max(10, Math.min(42, newSize));
|
||||
var target = editingInput || selectedInput;
|
||||
if (!target) return;
|
||||
target.style.fontSize = newSize + 'px';
|
||||
fontSizeValue = newSize;
|
||||
fontSizeValueEl.textContent = newSize;
|
||||
}
|
||||
|
||||
fontSizeUpBtn.addEventListener('click', function() {
|
||||
var target = editingInput || selectedInput;
|
||||
if (!target) return;
|
||||
var currentSize = parseInt(target.style.fontSize) || parseInt(window.getComputedStyle(target).fontSize) || fontSizeValue;
|
||||
updateFontSize(currentSize + 1);
|
||||
saveState();
|
||||
});
|
||||
|
||||
fontSizeDownBtn.addEventListener('click', () => {
|
||||
const targetInput = editingInput || selectedInput;
|
||||
if (targetInput) {
|
||||
const currentSize = parseInt(window.getComputedStyle(targetInput).fontSize) || fontSizeValue;
|
||||
updateFontSize(currentSize - 1, true);
|
||||
saveState();
|
||||
}
|
||||
fontSizeDownBtn.addEventListener('click', function() {
|
||||
var target = editingInput || selectedInput;
|
||||
if (!target) return;
|
||||
var currentSize = parseInt(target.style.fontSize) || parseInt(window.getComputedStyle(target).fontSize) || fontSizeValue;
|
||||
updateFontSize(currentSize - 1);
|
||||
saveState();
|
||||
});
|
||||
|
||||
fontSizeRoller.addEventListener('wheel', (e) => {
|
||||
fontSizeRoller.addEventListener('wheel', function(e) {
|
||||
e.preventDefault();
|
||||
const targetInput = editingInput || selectedInput;
|
||||
if (targetInput) {
|
||||
const currentSize = parseInt(window.getComputedStyle(targetInput).fontSize) || fontSizeValue;
|
||||
const delta = e.deltaY > 0 ? -1 : 1;
|
||||
updateFontSize(currentSize + delta, true);
|
||||
saveState();
|
||||
}
|
||||
var target = editingInput || selectedInput;
|
||||
if (!target) return;
|
||||
var currentSize = parseInt(target.style.fontSize) || parseInt(window.getComputedStyle(target).fontSize) || fontSizeValue;
|
||||
var delta = e.deltaY > 0 ? -1 : 1;
|
||||
updateFontSize(currentSize + delta);
|
||||
saveState();
|
||||
}, { passive: false });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user