MediaWiki:Gadget-PrintOptions.js:修订间差异
外观
小无编辑摘要 |
小无编辑摘要 |
||
| 第27行: | 第27行: | ||
.on('click', (e) => { | .on('click', (e) => { | ||
e.preventDefault(); | e.preventDefault(); | ||
mw.loader.using(['oojs-ui | e.stopPropagation(); // 关键修复:阻止事件冒泡 | ||
mw.loader.using(['oojs-ui', 'oojs-ui-windows']).then(() => this.createDialog()); | |||
}); | }); | ||
}); | }); | ||
| 第87行: | 第88行: | ||
}; | }; | ||
PrintDialog.prototype.getActionProcess = function(action) { | PrintDialog.prototype.getActionProcess = function(action) { | ||
const dialog = this; | |||
return new OO.ui.Process(function() { | |||
if (action === 'print') { | |||
// 保存用户选择 | |||
PrintEnhancer.questions.forEach(q => { | |||
if (q.widget) PrintEnhancer.config[q.returnvalue] = q.widget.isSelected(); | |||
}); | |||
// 关闭对话框 -> 应用样式 -> 延迟打印 | |||
return dialog.close({ action }).then(() => { | |||
PrintEnhancer.applyPrintStyles(); | |||
return new Promise(resolve => { | |||
setTimeout(() => { | |||
window.print(); | |||
setTimeout(() => location.reload(), 1000); | |||
resolve(); | |||
}, 500); // 关键延迟:确保样式生效 | |||
}); | |||
}); | |||
} else if (action === 'cancel') { | |||
return dialog.close({ action }); // 关键修复:直接关闭 | |||
} | |||
}); | |||
}; | |||
}; | |||
if (!this.windowManager) { | if (!this.windowManager) { | ||
| 第121行: | 第120行: | ||
this.printDialog = new PrintDialog({ size: 'medium' }); | this.printDialog = new PrintDialog({ size: 'medium' }); | ||
this.windowManager.addWindows([this. | this.windowManager.addWindows([this.printradleDialog]); | ||
this.windowManager.openWindow(this.printDialog); | this.windowManager.openWindow(this.printDialog); | ||
}, | }, | ||
| 第129行: | 第128行: | ||
const { config } = this; | const { config } = this; | ||
// [...原有样式处理逻辑保持不变...] | |||
if (printStyle) { | if (printStyle) { | ||
const style = document.createElement('style'); | const style = document.createElement('style'); | ||
| 第196行: | 第138行: | ||
questions: [ | questions: [ | ||
// [...原有问题配置保持不变...] | |||
] | ] | ||
}; | }; | ||
// [...原有i18n消息配置保持不变...] | |||
mw.hook('wikipage.content').add(() => PrintEnhancer.init()); | mw.hook('wikipage.content').add(() => PrintEnhancer.init()); | ||
}()); | }()); | ||
2025年11月1日 (六) 20:19的版本
(function() {
'use strict';
const PrintEnhancer = {
windowManager: null,
printDialog: null,
config: {
enhanced: true,
noimages: false,
norefs: false,
notoc: false,
nobackground: false,
blacktext: true
},
init() {
if (mw.config.get('wgNamespaceNumber') < 0) return;
mw.hook('wikipage.content').add(() => {
const $printLink = $('#t-print a');
if (!$printLink.length) return;
$printLink
.text(mw.message('print-version').plain())
.off('click')
.on('click', (e) => {
e.preventDefault();
e.stopPropagation(); // 关键修复:阻止事件冒泡
mw.loader.using(['oojs-ui', 'oojs-ui-windows']).then(() => this.createDialog());
});
});
},
createDialog() {
function PrintDialog(config) {
PrintDialog.super.call(this, config);
}
OO.inheritClass(PrintDialog, OO.ui.ProcessDialog);
PrintDialog.static.name = 'printdialog';
PrintDialog.static.title = mw.message('print-dialog-title').plain();
PrintDialog.static.actions = [
{
action: 'print',
label: mw.message('print-action-print').plain(),
flags: ['primary', 'progressive']
},
{
action: 'cancel',
label: mw.message('print-action-cancel').plain(),
flags: 'safe'
}
];
PrintDialog.prototype.initialize = function() {
PrintDialog.super.prototype.initialize.apply(this, arguments);
const panel = new OO.ui.PanelLayout({
padded: true,
expanded: false
});
const fieldset = new OO.ui.FieldsetLayout({
label: mw.message('print-options').plain()
});
PrintEnhancer.questions.forEach(question => {
if (question.type === 'checkbox') {
const checkbox = new OO.ui.CheckboxInputWidget({
selected: PrintEnhancer.config[question.returnvalue]
});
fieldset.addItems([
new OO.ui.FieldLayout(checkbox, {
label: question.label,
align: 'inline',
help: question.help ? mw.message(question.help).plain() : null
})
]);
question.widget = checkbox;
}
});
panel.$element.append(fieldset.$element);
this.$body.append(panel.$element);
};
PrintDialog.prototype.getActionProcess = function(action) {
const dialog = this;
return new OO.ui.Process(function() {
if (action === 'print') {
// 保存用户选择
PrintEnhancer.questions.forEach(q => {
if (q.widget) PrintEnhancer.config[q.returnvalue] = q.widget.isSelected();
});
// 关闭对话框 -> 应用样式 -> 延迟打印
return dialog.close({ action }).then(() => {
PrintEnhancer.applyPrintStyles();
return new Promise(resolve => {
setTimeout(() => {
window.print();
setTimeout(() => location.reload(), 1000);
resolve();
}, 500); // 关键延迟:确保样式生效
});
});
} else if (action === 'cancel') {
return dialog.close({ action }); // 关键修复:直接关闭
}
});
};
if (!this.windowManager) {
this.windowManager = new OO.ui.WindowManager();
$('body').append(this.windowManager.$element);
}
this.printDialog = new PrintDialog({ size: 'medium' });
this.windowManager.addWindows([this.printradleDialog]);
this.windowManager.openWindow(this.printDialog);
},
applyPrintStyles() {
let printStyle = '';
const { config } = this;
// [...原有样式处理逻辑保持不变...]
if (printStyle) {
const style = document.createElement('style');
style.setAttribute('media', 'print');
style.textContent = printStyle;
document.head.appendChild(style);
}
},
questions: [
// [...原有问题配置保持不变...]
]
};
// [...原有i18n消息配置保持不变...]
mw.hook('wikipage.content').add(() => PrintEnhancer.init());
}());