MediaWiki:Common.js:修订间差异
外观
「手动换行」按钮改放到保存更改左侧 |
修复分类引导:改为接管 VE 保存对话框入口,解决与保存对话框同时打开、被覆盖无法点击的问题 |
||
| (未显示同一用户的3个中间版本) | |||
| 第1行: | 第1行: | ||
/* | /* 土域百科:可视化编辑器「图片换行」浮动按钮 | ||
* 背景:插入图片后图片处于选中状态,此时按回车不会换行(VE 原生行为是打开图片设置)。 | * 背景:插入图片后图片处于选中状态,此时按回车不会换行(VE 原生行为是打开图片设置)。 | ||
* | * 本脚本:仅当点选图片时,在图片左下角浮出一个「换行」小按钮, | ||
* | * 点击即在图片下方插入新段落;图片失焦后按钮自动消失。不占用工具栏空间。 | ||
*/ | */ | ||
( function () { | ( function () { | ||
'use strict'; | 'use strict'; | ||
var $btn = null; | |||
var hookedSurface = null; | |||
function getSurface() { | function getSurface() { | ||
| 第17行: | 第19行: | ||
} | } | ||
function | function ensureButton() { | ||
if ( $btn ) { | |||
return $btn; | |||
} | |||
$btn = $( '<button type="button" class="ty-ve-br" title="在图片下方插入新的一行">↵ 换行</button>' ); | |||
// mousedown 阻止默认行为:保持编辑器内的图片选中状态不被点击破坏 | |||
$btn.on( 'mousedown', function ( e ) { | |||
e.preventDefault(); | |||
e.stopPropagation(); | |||
} ); | |||
$btn.on( 'click', function ( e ) { | |||
e.preventDefault(); | |||
e.stopPropagation(); | |||
insertAfterFocused(); | |||
} ); | |||
$( document.body ).append( $btn ); | |||
return $btn; | |||
} | |||
function hide() { | |||
if ( $btn ) { | |||
$btn.hide(); | |||
} | |||
} | |||
function insertAfterFocused() { | |||
var surface = getSurface(); | var surface = getSurface(); | ||
if ( !surface ) { | if ( !surface ) { | ||
| 第26行: | 第53行: | ||
var model = surface.getModel(); | var model = surface.getModel(); | ||
var focused = view.getFocusedNode && view.getFocusedNode(); | var focused = view.getFocusedNode && view.getFocusedNode(); | ||
if ( focused | if ( !focused || !focused.getModel || !focused.getModel().getOuterRange ) { | ||
hide(); | |||
return; | return; | ||
} | } | ||
var offset = focused.getModel().getOuterRange().to; | |||
ve. | var tx = ve.dm.TransactionBuilder.static.newFromInsertion( | ||
model.getDocument(), offset, | |||
[ { type: 'paragraph' }, { type: '/paragraph' } ] | |||
); | |||
model.change( tx, new ve.dm.LinearSelection( new ve.Range( offset + 1 ) ) ); | |||
view.focus(); | view.focus(); | ||
} catch ( err ) { | } catch ( err ) { | ||
mw.notify( ' | mw.notify( '换行没有生效:可以把光标移到图片后,按一下键盘 ↓ 方向键。', { type: 'warn' } ); | ||
} | |||
hide(); | |||
} | |||
function isImageNode( node ) { | |||
try { | |||
return /image/i.test( node.getModel().getType() ); | |||
} catch ( e ) { | |||
return false; | |||
} | } | ||
} | } | ||
function | function update() { | ||
var surface = getSurface(); | var surface = getSurface(); | ||
if ( !surface ) { | if ( !surface ) { | ||
hide(); | |||
return; | return; | ||
} | } | ||
var | var focused = surface.getView().getFocusedNode && surface.getView().getFocusedNode(); | ||
if ( !focused || !isImageNode( focused ) ) { | |||
hide(); | |||
return; | return; | ||
} | } | ||
if ( ! | var el = focused.$element && focused.$element[0]; | ||
if ( !el ) { | |||
hide(); | |||
return; | return; | ||
} | } | ||
if ( | var r = el.getBoundingClientRect(); | ||
return; | if ( !r.width || r.bottom < 0 || r.top > window.innerHeight ) { | ||
hide(); | |||
return; | |||
} | } | ||
ensureButton().css( { | |||
left: Math.max( 8, r.left + 4 ) + 'px', | |||
top: Math.min( window.innerHeight - 34, r.bottom + 6 ) + 'px' | |||
} ).show(); | |||
} ) | } | ||
function hookSurface() { | |||
var | var surface = getSurface(); | ||
if ( | if ( !surface || hookedSurface === surface.getModel() ) { | ||
return; | return; | ||
} | } | ||
hookedSurface = surface.getModel(); | |||
hookedSurface.on( 'select', update ); | |||
} | } | ||
mw.hook( 've.activationComplete' ).add( function () { | // 编辑器激活后挂载;时序不确定时重试 | ||
mw.hook( 've.activationComplete' ).add( hookSurface ); | |||
mw.hook( 've.activate' ).add( function () { | |||
var n = 0; | var n = 0; | ||
var timer = setInterval( function () { | var timer = setInterval( function () { | ||
n++; | n++; | ||
if ( | hookSurface(); | ||
if ( hookedSurface || n > 10 ) { | |||
clearInterval( timer ); | clearInterval( timer ); | ||
} | |||
}, 800 ); | |||
} ); | |||
mw.hook( 've.deactivate' ).add( function () { | |||
hookedSurface = null; | |||
hide(); | |||
} ); | |||
// 滚动/缩放时让按钮跟着图片走 | |||
$( window ).on( 'scroll.tyvebr resize.tyvebr', function () { | |||
if ( $btn && $btn.is( ':visible' ) ) { | |||
update(); | |||
} | |||
} ); | |||
}() ); | |||
/* 土域百科:保存前强制引导添加分类 | |||
* 背景:新条目若不带分类,读者无法在首页目录中找到它。 | |||
* 本脚本:在主命名空间触发「保存」时(接管 ve.init.target.showSaveDialog 入口), | |||
* 若条目还没有任何分类,先弹出引导框,按目录分组(可展开)快速勾选分类, | |||
* 至少选一个才能继续保存。仅可视化模式生效;源码模式与已有分类的条目不受影响。 | |||
* 注意:OO.ui 只有在编辑器加载后才可用,故对话框类在使用时才定义。 | |||
*/ | |||
( function () { | |||
'use strict'; | |||
// 与首页目录保持一致的分组 | |||
var GROUPS = [ | |||
{ name: '土域', hint: '土域的历史与设定', cats: [ '土域介绍', '土域历史', '跨时空内容分发节点' ] }, | |||
{ name: '新土共建', hint: '服务器条目主目录', cats: [ '概述', '通用', '服务器历史', '插件功能', '地标大全', '人物档案', '社区趣味', '其他' ], open: true }, | |||
{ name: '其他', hint: '公告等', cats: [ '公告', '咕咕嘎嘎' ] } | |||
]; | |||
var bypassSave = false; | |||
var windowManager = null; | |||
var dialog = null; | |||
function getSurface() { | |||
try { | |||
var target = window.ve && ve.init && ve.init.target; | |||
return target && target.getSurface ? target.getSurface() : null; | |||
} catch ( err ) { | |||
return null; | |||
} | |||
} | |||
// 返回 -1 表示无法判断(不拦截),否则为分类数量 | |||
function getCategoryCount() { | |||
var surface = getSurface(); | |||
if ( !surface ) { | |||
return -1; | |||
} | |||
try { | |||
var model = surface.getModel(); | |||
if ( !model || model.sourceMode ) { | |||
return -1; | |||
} | |||
return model.getDocument().getMetaList().getItemsInGroup( 'mwCategory' ).length; | |||
} catch ( err ) { | |||
return -1; | |||
} | |||
} | |||
function insertCategories( cats ) { | |||
var surface = getSurface(); | |||
if ( !surface ) { | |||
return; | |||
} | |||
var fragment = surface.getModel().getFragment(); | |||
cats.forEach( function ( cat ) { | |||
fragment.insertMeta( { | |||
attributes: { category: 'Category:' + cat, sortkey: '' }, | |||
type: 'mwCategory' | |||
} ); | |||
} ); | |||
} | |||
// 首次用到时才定义(依赖 OO.ui,仅在编辑器加载后存在) | |||
function defineDialog() { | |||
function TyCategoryDialog( config ) { | |||
TyCategoryDialog.super.call( this, config ); | |||
} | |||
OO.inheritClass( TyCategoryDialog, OO.ui.ProcessDialog ); | |||
TyCategoryDialog.static.name = 'tyCategoryGuide'; | |||
TyCategoryDialog.static.title = '先给这篇条目挑个分类'; | |||
TyCategoryDialog.static.actions = [ | |||
{ action: 'save', label: '添加分类并保存', flags: [ 'primary', 'progressive' ] } | |||
]; | |||
TyCategoryDialog.prototype.initialize = function () { | |||
TyCategoryDialog.super.prototype.initialize.call( this ); | |||
this.$content.addClass( 'ty-cg-dialog' ); | |||
this.$body.append( | |||
$( '<p>' ).addClass( 'ty-cg-tip' ).text( | |||
'这篇条目还没有分类。分类能让读者在首页目录里找到它,请至少选择一个(拿不准就选「其他」):' | |||
) | |||
); | |||
var $list = $( '<div>' ).addClass( 'ty-cg-list' ); | |||
GROUPS.forEach( function ( group ) { | |||
var $group = $( '<div>' ).addClass( 'ty-cg-group' ); | |||
var $head = $( '<button type="button">' ).addClass( 'ty-cg-head' ) | |||
.append( $( '<span>' ).addClass( 'ty-cg-arrow' ).text( '▸' ) ) | |||
.append( $( '<span>' ).addClass( 'ty-cg-gname' ).text( group.name ) ) | |||
.append( $( '<span>' ).addClass( 'ty-cg-ghint' ).text( group.hint ) ); | |||
var $body = $( '<div>' ).addClass( 'ty-cg-body' ); | |||
group.cats.forEach( function ( cat ) { | |||
$body.append( | |||
$( '<label>' ).addClass( 'ty-cg-cat' ) | |||
.append( $( '<input type="checkbox">' ).attr( 'value', cat ) ) | |||
.append( $( '<span>' ).text( cat ) ) | |||
); | |||
} ); | |||
$head.on( 'click', function () { | |||
$group.toggleClass( 'ty-cg-open' ); | |||
} ); | |||
if ( group.open ) { | |||
$group.addClass( 'ty-cg-open' ); | |||
} | |||
$group.append( $head, $body ); | |||
$list.append( $group ); | |||
} ); | |||
this.$body.append( $list ); | |||
this.$count = $( '<p>' ).addClass( 'ty-cg-count' ); | |||
this.$body.append( this.$count ); | |||
var dlg = this; | |||
this.$body.on( 'change', 'input[type="checkbox"]', function () { | |||
dlg.updateState(); | |||
} ); | |||
}; | |||
TyCategoryDialog.prototype.getSelected = function () { | |||
var cats = []; | |||
this.$body.find( 'input[type="checkbox"]:checked' ).each( function () { | |||
cats.push( this.value ); | |||
} ); | |||
return cats; | |||
}; | |||
TyCategoryDialog.prototype.updateState = function () { | |||
var n = this.getSelected().length; | |||
this.actions.setAbilities( { save: n > 0 } ); | |||
this.$count.text( n > 0 ? '已选 ' + n + ' 个分类,保存时自动加入条目。' : '还没选择分类,至少选一个才能保存。' ); | |||
}; | |||
TyCategoryDialog.prototype.getSetupProcess = function ( data ) { | |||
return TyCategoryDialog.super.prototype.getSetupProcess.call( this, data ) | |||
.next( function () { | |||
this.$body.find( 'input[type="checkbox"]' ).prop( 'checked', false ); | |||
this.updateState(); | |||
}, this ); | |||
}; | |||
TyCategoryDialog.prototype.getActionProcess = function ( action ) { | |||
if ( action === 'save' ) { | |||
return new OO.ui.Process( function () { | |||
var cats = this.getSelected(); | |||
if ( !cats.length ) { | |||
return; | |||
} | |||
insertCategories( cats ); | |||
this.close( { action: action } ); | |||
bypassSave = true; | |||
try { | |||
ve.init.target.showSaveDialog(); | |||
} catch ( err ) { | |||
mw.notify( '打开保存对话框失败,请再点一次工具栏的「保存」。', { type: 'warn' } ); | |||
} | |||
setTimeout( function () { | |||
bypassSave = false; | |||
}, 1500 ); | |||
}, this ); | |||
} | |||
return TyCategoryDialog.super.prototype.getActionProcess.call( this, action ); | |||
}; | |||
TyCategoryDialog.prototype.getBodyHeight = function () { | |||
return Math.min( 380, Math.max( 220, window.innerHeight - 260 ) ); | |||
}; | |||
return TyCategoryDialog; | |||
} | |||
function openDialog() { | |||
if ( !window.OO || !OO.ui || !OO.ui.ProcessDialog ) { | |||
return; | |||
} | |||
if ( !windowManager ) { | |||
windowManager = new OO.ui.WindowManager(); | |||
windowManager.$element.addClass( 'ty-cg-wm' ); | |||
$( document.body ).append( windowManager.$element ); | |||
dialog = new ( defineDialog() )( { size: 'medium' } ); | |||
windowManager.addWindows( [ dialog ] ); | |||
} | |||
windowManager.openWindow( dialog ); | |||
} | |||
// 接管 VE 的保存对话框入口:工具栏按钮、快捷键等所有「保存」路径都会经过 showSaveDialog, | |||
// 在入口处做判断,比拦截点击事件可靠(点击拦截无法阻止 OOUI 自身的工具响应,会导致两个对话框同时打开)。 | |||
function patchTarget() { | |||
var target = window.ve && ve.init && ve.init.target; | |||
if ( !target || target.__tyCgPatched || typeof target.showSaveDialog !== 'function' ) { | |||
return; | |||
} | |||
var origShow = target.showSaveDialog.bind( target ); | |||
target.showSaveDialog = function () { | |||
if ( !bypassSave && mw.config.get( 'wgNamespaceNumber' ) === 0 && getCategoryCount() === 0 ) { | |||
openDialog(); | |||
return; | return; | ||
} | } | ||
return origShow(); | |||
}, | }; | ||
target.__tyCgPatched = true; | |||
} | |||
mw.hook( 've.activationComplete' ).add( patchTarget ); | |||
mw.hook( 've.activate' ).add( function () { | |||
// activationComplete 可能先于本脚本执行,保险起见重试几次 | |||
var tries = 0; | |||
var timer = setInterval( function () { | |||
var target = window.ve && ve.init && ve.init.target; | |||
if ( target ) { | |||
patchTarget(); | |||
} | |||
if ( ++tries >= 20 || ( target && target.__tyCgPatched ) ) { | |||
clearInterval( timer ); | |||
} | |||
}, 500 ); | |||
} ); | } ); | ||
}() ); | }() ); | ||
2026年10月1日 (四) 18:45的最新版本
/* 土域百科:可视化编辑器「图片换行」浮动按钮
* 背景:插入图片后图片处于选中状态,此时按回车不会换行(VE 原生行为是打开图片设置)。
* 本脚本:仅当点选图片时,在图片左下角浮出一个「换行」小按钮,
* 点击即在图片下方插入新段落;图片失焦后按钮自动消失。不占用工具栏空间。
*/
( function () {
'use strict';
var $btn = null;
var hookedSurface = null;
function getSurface() {
try {
var target = window.ve && ve.init && ve.init.target;
return target && target.getSurface ? target.getSurface() : null;
} catch ( err ) {
return null;
}
}
function ensureButton() {
if ( $btn ) {
return $btn;
}
$btn = $( '<button type="button" class="ty-ve-br" title="在图片下方插入新的一行">↵ 换行</button>' );
// mousedown 阻止默认行为:保持编辑器内的图片选中状态不被点击破坏
$btn.on( 'mousedown', function ( e ) {
e.preventDefault();
e.stopPropagation();
} );
$btn.on( 'click', function ( e ) {
e.preventDefault();
e.stopPropagation();
insertAfterFocused();
} );
$( document.body ).append( $btn );
return $btn;
}
function hide() {
if ( $btn ) {
$btn.hide();
}
}
function insertAfterFocused() {
var surface = getSurface();
if ( !surface ) {
return;
}
try {
var view = surface.getView();
var model = surface.getModel();
var focused = view.getFocusedNode && view.getFocusedNode();
if ( !focused || !focused.getModel || !focused.getModel().getOuterRange ) {
hide();
return;
}
var offset = focused.getModel().getOuterRange().to;
var tx = ve.dm.TransactionBuilder.static.newFromInsertion(
model.getDocument(), offset,
[ { type: 'paragraph' }, { type: '/paragraph' } ]
);
model.change( tx, new ve.dm.LinearSelection( new ve.Range( offset + 1 ) ) );
view.focus();
} catch ( err ) {
mw.notify( '换行没有生效:可以把光标移到图片后,按一下键盘 ↓ 方向键。', { type: 'warn' } );
}
hide();
}
function isImageNode( node ) {
try {
return /image/i.test( node.getModel().getType() );
} catch ( e ) {
return false;
}
}
function update() {
var surface = getSurface();
if ( !surface ) {
hide();
return;
}
var focused = surface.getView().getFocusedNode && surface.getView().getFocusedNode();
if ( !focused || !isImageNode( focused ) ) {
hide();
return;
}
var el = focused.$element && focused.$element[0];
if ( !el ) {
hide();
return;
}
var r = el.getBoundingClientRect();
if ( !r.width || r.bottom < 0 || r.top > window.innerHeight ) {
hide();
return;
}
ensureButton().css( {
left: Math.max( 8, r.left + 4 ) + 'px',
top: Math.min( window.innerHeight - 34, r.bottom + 6 ) + 'px'
} ).show();
}
function hookSurface() {
var surface = getSurface();
if ( !surface || hookedSurface === surface.getModel() ) {
return;
}
hookedSurface = surface.getModel();
hookedSurface.on( 'select', update );
}
// 编辑器激活后挂载;时序不确定时重试
mw.hook( 've.activationComplete' ).add( hookSurface );
mw.hook( 've.activate' ).add( function () {
var n = 0;
var timer = setInterval( function () {
n++;
hookSurface();
if ( hookedSurface || n > 10 ) {
clearInterval( timer );
}
}, 800 );
} );
mw.hook( 've.deactivate' ).add( function () {
hookedSurface = null;
hide();
} );
// 滚动/缩放时让按钮跟着图片走
$( window ).on( 'scroll.tyvebr resize.tyvebr', function () {
if ( $btn && $btn.is( ':visible' ) ) {
update();
}
} );
}() );
/* 土域百科:保存前强制引导添加分类
* 背景:新条目若不带分类,读者无法在首页目录中找到它。
* 本脚本:在主命名空间触发「保存」时(接管 ve.init.target.showSaveDialog 入口),
* 若条目还没有任何分类,先弹出引导框,按目录分组(可展开)快速勾选分类,
* 至少选一个才能继续保存。仅可视化模式生效;源码模式与已有分类的条目不受影响。
* 注意:OO.ui 只有在编辑器加载后才可用,故对话框类在使用时才定义。
*/
( function () {
'use strict';
// 与首页目录保持一致的分组
var GROUPS = [
{ name: '土域', hint: '土域的历史与设定', cats: [ '土域介绍', '土域历史', '跨时空内容分发节点' ] },
{ name: '新土共建', hint: '服务器条目主目录', cats: [ '概述', '通用', '服务器历史', '插件功能', '地标大全', '人物档案', '社区趣味', '其他' ], open: true },
{ name: '其他', hint: '公告等', cats: [ '公告', '咕咕嘎嘎' ] }
];
var bypassSave = false;
var windowManager = null;
var dialog = null;
function getSurface() {
try {
var target = window.ve && ve.init && ve.init.target;
return target && target.getSurface ? target.getSurface() : null;
} catch ( err ) {
return null;
}
}
// 返回 -1 表示无法判断(不拦截),否则为分类数量
function getCategoryCount() {
var surface = getSurface();
if ( !surface ) {
return -1;
}
try {
var model = surface.getModel();
if ( !model || model.sourceMode ) {
return -1;
}
return model.getDocument().getMetaList().getItemsInGroup( 'mwCategory' ).length;
} catch ( err ) {
return -1;
}
}
function insertCategories( cats ) {
var surface = getSurface();
if ( !surface ) {
return;
}
var fragment = surface.getModel().getFragment();
cats.forEach( function ( cat ) {
fragment.insertMeta( {
attributes: { category: 'Category:' + cat, sortkey: '' },
type: 'mwCategory'
} );
} );
}
// 首次用到时才定义(依赖 OO.ui,仅在编辑器加载后存在)
function defineDialog() {
function TyCategoryDialog( config ) {
TyCategoryDialog.super.call( this, config );
}
OO.inheritClass( TyCategoryDialog, OO.ui.ProcessDialog );
TyCategoryDialog.static.name = 'tyCategoryGuide';
TyCategoryDialog.static.title = '先给这篇条目挑个分类';
TyCategoryDialog.static.actions = [
{ action: 'save', label: '添加分类并保存', flags: [ 'primary', 'progressive' ] }
];
TyCategoryDialog.prototype.initialize = function () {
TyCategoryDialog.super.prototype.initialize.call( this );
this.$content.addClass( 'ty-cg-dialog' );
this.$body.append(
$( '<p>' ).addClass( 'ty-cg-tip' ).text(
'这篇条目还没有分类。分类能让读者在首页目录里找到它,请至少选择一个(拿不准就选「其他」):'
)
);
var $list = $( '<div>' ).addClass( 'ty-cg-list' );
GROUPS.forEach( function ( group ) {
var $group = $( '<div>' ).addClass( 'ty-cg-group' );
var $head = $( '<button type="button">' ).addClass( 'ty-cg-head' )
.append( $( '<span>' ).addClass( 'ty-cg-arrow' ).text( '▸' ) )
.append( $( '<span>' ).addClass( 'ty-cg-gname' ).text( group.name ) )
.append( $( '<span>' ).addClass( 'ty-cg-ghint' ).text( group.hint ) );
var $body = $( '<div>' ).addClass( 'ty-cg-body' );
group.cats.forEach( function ( cat ) {
$body.append(
$( '<label>' ).addClass( 'ty-cg-cat' )
.append( $( '<input type="checkbox">' ).attr( 'value', cat ) )
.append( $( '<span>' ).text( cat ) )
);
} );
$head.on( 'click', function () {
$group.toggleClass( 'ty-cg-open' );
} );
if ( group.open ) {
$group.addClass( 'ty-cg-open' );
}
$group.append( $head, $body );
$list.append( $group );
} );
this.$body.append( $list );
this.$count = $( '<p>' ).addClass( 'ty-cg-count' );
this.$body.append( this.$count );
var dlg = this;
this.$body.on( 'change', 'input[type="checkbox"]', function () {
dlg.updateState();
} );
};
TyCategoryDialog.prototype.getSelected = function () {
var cats = [];
this.$body.find( 'input[type="checkbox"]:checked' ).each( function () {
cats.push( this.value );
} );
return cats;
};
TyCategoryDialog.prototype.updateState = function () {
var n = this.getSelected().length;
this.actions.setAbilities( { save: n > 0 } );
this.$count.text( n > 0 ? '已选 ' + n + ' 个分类,保存时自动加入条目。' : '还没选择分类,至少选一个才能保存。' );
};
TyCategoryDialog.prototype.getSetupProcess = function ( data ) {
return TyCategoryDialog.super.prototype.getSetupProcess.call( this, data )
.next( function () {
this.$body.find( 'input[type="checkbox"]' ).prop( 'checked', false );
this.updateState();
}, this );
};
TyCategoryDialog.prototype.getActionProcess = function ( action ) {
if ( action === 'save' ) {
return new OO.ui.Process( function () {
var cats = this.getSelected();
if ( !cats.length ) {
return;
}
insertCategories( cats );
this.close( { action: action } );
bypassSave = true;
try {
ve.init.target.showSaveDialog();
} catch ( err ) {
mw.notify( '打开保存对话框失败,请再点一次工具栏的「保存」。', { type: 'warn' } );
}
setTimeout( function () {
bypassSave = false;
}, 1500 );
}, this );
}
return TyCategoryDialog.super.prototype.getActionProcess.call( this, action );
};
TyCategoryDialog.prototype.getBodyHeight = function () {
return Math.min( 380, Math.max( 220, window.innerHeight - 260 ) );
};
return TyCategoryDialog;
}
function openDialog() {
if ( !window.OO || !OO.ui || !OO.ui.ProcessDialog ) {
return;
}
if ( !windowManager ) {
windowManager = new OO.ui.WindowManager();
windowManager.$element.addClass( 'ty-cg-wm' );
$( document.body ).append( windowManager.$element );
dialog = new ( defineDialog() )( { size: 'medium' } );
windowManager.addWindows( [ dialog ] );
}
windowManager.openWindow( dialog );
}
// 接管 VE 的保存对话框入口:工具栏按钮、快捷键等所有「保存」路径都会经过 showSaveDialog,
// 在入口处做判断,比拦截点击事件可靠(点击拦截无法阻止 OOUI 自身的工具响应,会导致两个对话框同时打开)。
function patchTarget() {
var target = window.ve && ve.init && ve.init.target;
if ( !target || target.__tyCgPatched || typeof target.showSaveDialog !== 'function' ) {
return;
}
var origShow = target.showSaveDialog.bind( target );
target.showSaveDialog = function () {
if ( !bypassSave && mw.config.get( 'wgNamespaceNumber' ) === 0 && getCategoryCount() === 0 ) {
openDialog();
return;
}
return origShow();
};
target.__tyCgPatched = true;
}
mw.hook( 've.activationComplete' ).add( patchTarget );
mw.hook( 've.activate' ).add( function () {
// activationComplete 可能先于本脚本执行,保险起见重试几次
var tries = 0;
var timer = setInterval( function () {
var target = window.ve && ve.init && ve.init.target;
if ( target ) {
patchTarget();
}
if ( ++tries >= 20 || ( target && target.__tyCgPatched ) ) {
clearInterval( timer );
}
}, 500 );
} );
}() );