Посібник:Додаткові кнопки редагування

This page is a translated version of the page Manual:Custom edit buttons and the translation is 37% complete.
WikiEditor
Класична панель редагування

Ви можете додавати додаткові кнопки редагування до панелі редагування, що над вікном редагування використовуючи JavaScript (див. нижче). Ви маєте вирізняти нову панель додану за допомогою Розширення:WikiEditor та стару панель (також відомп як класична панель редагування)

mw.user.options.get( 'usebetatoolbar' ) може використовуватися для перевірки чи користувач використовує wikiEditor (true), чи стару панель (false).

Додавання до JavaScript

Додаткові кнопки використовують JavaScript для реалізації їхньої функції. Для того щоб активувати ваш JavaScript на сторінці редагування існують декілька способів:

  • Власний JavaScript — Відповідний до сервераця функція ввімкнена і для кнопок які ви хочете щоб були в наявності для користувачів які копіюють JavaScript у їх власний JavaScript.
  • Додавання JavaScript — Відповідний тоді коли всі або більшість користувачів Вікі мають доступ користування кнопкою. Це припускає, що ви розробляєте розширення для Media wiki.
  • Ядро MediaWiki Javascript — Відповідний коли нова кнопка дозволена на всіх установках Вікі.

Власний JavaScript

Для додавання нових кнопок ви можете вставити їх у ваш власний JavaScript.

В LocalSettings.php додайте $wgAllowUserJs = true;, або в MediaWiki:Common.js, або як Гаджет.

Додавання JavaScript

Після встановлення базової структури додатку, ядро PHP файлу має буде провести перші два кроки нижче. Для простого додатку, як та яка за наміром додає лише додаткову кнопку, третій крок може бути в ядрі додатку PHP файлу, як в цьому простому зразку, чи може бути в іншому PHP файлі. Тут також можливо є потреба в локалізації, що має бути уставлено в I18N файлі.

Визначиння комплекту завантажувача ресурсів

The best practice for extensions is exploitation of the Resource Loader API, which provides performance optimization as well as a standard way of accessing scripts. This simple example shows the addition of one JavaScript file.

$wgResourceModules['ext.MyExtension']['scripts'][] = 'extensions/MyExtension/js/MyExtension.js';

Reference Hook

One of the hooks offered by the Edit page allows addition of a function reference. The function or method referenced here can be in the main PHP file for the extension if it is a simple extension or in another PHP file.

$wgHooks['EditPage::showEditForm:initial'][] = 'addModule';

Define Hook

The edit page hook allows addition of a reference to the Resource Loader module defined earlier. This example shows adding to every page. There could be complex logic associated with when to display and further conditions would be added in this handler. The argument to the addModules method is the same string as defined in the step defining the bundle.

function addModule(EditPage $editPage, OutputPage $output ) {
$output->addModules( 'ext.MyExtension' );
}

With these three steps completed, the JavaScript file referenced in the resource bundle should be applied to every edit page. The API allows for multiple files and more fine grained control of when the file is called.

Core MediaWiki

The design criteria for additions to the core of MediaWiki exceed what are mentioned here, but the mechanics for adding buttons are described.

JavaScript files for core MediaWiki are referenced in the 'resources/Resources.php' file. The default bundle for the edit page includes 'resources/src/mediawiki.action/mediawiki.action.edit.js'. If the button should be displayed every time, this JavaScript file should be enhanced with the new button. If the button has conditions that the JavaScript file should not be loaded every time, steps similar to an extension should be executed and consideration should be give to whether the function belongs in the core of MediaWiki source code or whether an extension is the right tool to deliver the enhancement.

Розширення:WikiEditor

jQuery( document ).ready( function ( $ ) {
    $( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
        section: 'advanced',
        group: 'format',
        tools: {
            buttonId: {
                label: 'Comment visible only for editors',
                type: 'button',
                icon: '//upload.wikimedia.org/wikipedia/commons/f/f9/Toolbaricon_regular_S_stroke.png',
                action: {
                    type: 'encapsulate',
                    options: {
                        pre: '<!-- ',
                        peri: 'Insert comment here',
                        post: ' -->'
                    }
                }
            }
        }
    } );
} );

See also Toolbar customization for more advanced options and examples.

You may also use the InsertWikiEditorButton script (by user Krinkle) to simplify adding buttons to the wikiEditor.

Classic edit toolbar

mw.hook( 'wikipage.editform' ).add( function () {
    mw.loader.using( 'mediawiki.toolbar' ).then( function () {
        mw.toolbar.addButton( {
            imageFile: '//upload.wikimedia.org/wikipedia/en/3/34/Button_hide_comment.png',
            speedTip: 'Comment visible only for editors',
            tagOpen: '<!-- ',
            tagClose: ' -->',
            sampleText: 'Insert comment here',
            imageId: 'button-comment'
        } );
    } );
} );
  • imageFileis the full URL address to the edit button image.
  • tagOpenis the opening tag, in this example: <!--
  • tagCloseis the closing tag, in this example: -->
  • sampleTextis the sample text that will appear between the opening and closing tags. The editor should replace this sample text with their own text.

See also