fix(toolbar): 支持在选中元素上应用样式
This commit is contained in:
24
js/script.js
24
js/script.js
@@ -1680,16 +1680,17 @@
|
||||
function initTextEditorToolbar() {
|
||||
// 加粗按钮
|
||||
boldBtn.addEventListener('click', () => {
|
||||
if (!editingInput) return;
|
||||
const targetInput = editingInput || selectedInput;
|
||||
if (!targetInput) return;
|
||||
const selection = window.getSelection();
|
||||
if (selection.rangeCount > 0 && !selection.isCollapsed) {
|
||||
document.execCommand('bold', false, null);
|
||||
} else {
|
||||
const isBold = boldBtn.classList.contains('active');
|
||||
if (isBold) {
|
||||
editingInput.style.fontWeight = 'normal';
|
||||
targetInput.style.fontWeight = 'normal';
|
||||
} else {
|
||||
editingInput.style.fontWeight = 'bold';
|
||||
targetInput.style.fontWeight = 'bold';
|
||||
}
|
||||
boldBtn.classList.toggle('active');
|
||||
}
|
||||
@@ -1697,16 +1698,17 @@
|
||||
|
||||
// 斜体按钮
|
||||
italicBtn.addEventListener('click', () => {
|
||||
if (!editingInput) return;
|
||||
const targetInput = editingInput || selectedInput;
|
||||
if (!targetInput) return;
|
||||
const selection = window.getSelection();
|
||||
if (selection.rangeCount > 0 && !selection.isCollapsed) {
|
||||
document.execCommand('italic', false, null);
|
||||
} else {
|
||||
const isItalic = italicBtn.classList.contains('active');
|
||||
if (isItalic) {
|
||||
editingInput.style.fontStyle = 'normal';
|
||||
targetInput.style.fontStyle = 'normal';
|
||||
} else {
|
||||
editingInput.style.fontStyle = 'italic';
|
||||
targetInput.style.fontStyle = 'italic';
|
||||
}
|
||||
italicBtn.classList.toggle('active');
|
||||
}
|
||||
@@ -1714,19 +1716,21 @@
|
||||
|
||||
// 颜色选择器
|
||||
colorPicker.addEventListener('input', () => {
|
||||
if (!editingInput) return;
|
||||
const targetInput = editingInput || selectedInput;
|
||||
if (!targetInput) return;
|
||||
const selection = window.getSelection();
|
||||
if (selection.rangeCount > 0 && !selection.isCollapsed) {
|
||||
document.execCommand('foreColor', false, colorPicker.value);
|
||||
} else {
|
||||
editingInput.style.color = colorPicker.value;
|
||||
targetInput.style.color = colorPicker.value;
|
||||
}
|
||||
});
|
||||
|
||||
// 字体选择器
|
||||
fontPicker.addEventListener('change', () => {
|
||||
if (!editingInput) return;
|
||||
editingInput.style.fontFamily = fontPicker.value + ', sans-serif';
|
||||
const targetInput = editingInput || selectedInput;
|
||||
if (!targetInput) return;
|
||||
targetInput.style.fontFamily = fontPicker.value + ', sans-serif';
|
||||
});
|
||||
|
||||
// 字号选择器 - 密码锁式滚轮
|
||||
|
||||
Reference in New Issue
Block a user