ric*_*bbo 16 mediawiki wikipedia visual-editor mediawiki-extensions
我试图在VisualEditor工具栏中插入一个特殊页面的自定义链接.见下图.
我google了很多但没有成功.有人请说道......
我的答案基于以下资源:
另外,据我所知,我很确定,没有任何记录的将工具添加到VE中的工具的方法。尽管可以将工具添加到已添加的组中,但该工具主要用于“插入”工具组,例如Syntaxhighlight_GeSHi中)。可能有一种更简单或“更好”的方式来完成此操作:)
首先,VisualEditor提供了一种在VE的主要部分加载时(通常是在您单击“编辑”按钮时)加载其他模块(称为插件)的方法。这些模块需要通过全局变量进行注册$wgVisualEditorPluginModules(如果您使用的是新的扩展注册,则必须在extension.json中进行注册)。在扩展注册文件中,您应该初始化一个模块(使用添加该工具所需的脚本文件)并将其添加为VE插件。
PHP示例(通过PHP文件注册旧扩展名):
// other setup...
$wgResourceModules['ext.extName.visualeditor'] = array(
'localBasePath' => __DIR__,
'remoteExtPath' => 'extName'
'dependencies' => array(
'ext.visualEditor.mwcore',
),
'scripts' => array(
'javascripts/ve.ui.ExtNameTool.js',
),
'messages' => array(
'extname-ve-toolname',
),
);
$wgVisualEditorPluginModules[] = 'ext.extName.visualeditor';
// other setup...
Run Code Online (Sandbox Code Playgroud)
extension.json(新的基于JSON的扩展注册):
// other setup...
"ResourceModules": {
"ext.geshi.visualEditor": {
"scripts": [
"javascripts/ve.ui.ExtNameTool.js"
],
"dependencies": [
"ext.visualEditor.mwcore"
],
"messages": [
"extname-ve-toolname"
]
}
},
"VisualEditorPluginModules": [
"ext.extName.visualeditor"
],
// other setup...
Run Code Online (Sandbox Code Playgroud)
现在,如果VE启动,它将ext.extName.visualeditor使用脚本加载您在本示例中命名的模块ve.ui.ExtNameTool.js。在此脚本中,您现在可以执行所需的任何操作。例如,这是一种将新模块添加到工具栏中的工具组列表末尾的方法:
ve.ui.ExtNameTool.js的示例:
( function () {
// create a new class, which will inherit ve.ui.Tool,
// which represents one tool
ve.ui.extNameTool = function extNameTool( toolGroup, config ) {
// parent constructor
ve.ui.extNameTool.super.apply( this, arguments );
// the tool should be enabled by default, enable it
this.setDisabled( false );
}
// inherit ve.ui.Tool
OO.inheritClass( ve.ui.extNameTool, ve.ui.Tool );
// every tool needs at least a name, or an icon
// (with the static property icon)
ve.ui.extNameTool.static.name = 'extname';
// don't add the tool to a named group automatically
ve.ui.extNameTool.static.autoAddToGroup = false;
// prevent this tool to be added to a catch-all group (*),
although this tool isn't added to a group
ve.ui.extNameTool.static.autoAddToCatchall = false;
// the title of the group (it's a message key,
// which should be added to the extensions i18n
// en.json file to be translateable)
// can be a string, too
ve.ui.extNameTool.static.title =
OO.ui.deferMsg( 'extname-ve-toolname' );
// onSelect is the handler for a click on the tool
ve.ui.extNameTool.prototype.onSelect = function () {
// show an alert box only, but you can do anything
alert( 'Hello' );
this.setActive( false );
}
// needs to be overwritten, but does nothing so far
ve.ui.extNameTool.prototype.onUpdateState = function () {
ve.ui.extNameTool.super.prototype.onUpdateState.apply( this, arguments );
}
// the tool needs to be registered to the toolFactory
// of the toolbar to be reachable with the given name
ve.ui.toolFactory.register( ve.ui.extNameTool );
// add this tool to the toolbar
ve.init.mw.Target.static.toolbarGroups.push( {
// this will create a new toolgroup with the tools
// named in this include directive. The naem is the name given
// in the static property of the tool
include: [ 'extname' ]
} );
} )();
Run Code Online (Sandbox Code Playgroud)
在您的LocalSettings.phpVE中安装扩展程序并启动VE之后,您应该在工具栏中看到具有给定名称的新工具。单击它会显示一个带有内容“ Hello”的警报框。就像在行内注释中写的一样:在单击处理程序(onSelect)中,您可以执行任何所需的操作,例如,在新选项卡中打开链接,例如,指向特殊页面。为了获得指向特殊页面的链接,我建议使用mw.Title来获取本地化的名称空间。例如:
var relativeUrl = mw.Title.newFromText( 'RecentChanges', -1 ).getUrl();
Run Code Online (Sandbox Code Playgroud)
的第一个参数mw.Title.newFromText()是页面的名称,第二个参数是名称空间的ID(-1是特殊页面的默认值,应该始终有效)。
希望对您有所帮助!
| 归档时间: |
|
| 查看次数: |
619 次 |
| 最近记录: |