Extension:CodeMirror
- CodeEditor とは異なります。
リリースの状態: 安定 |
|
|---|---|
| 実装 | ユーザー インターフェイス |
| 説明 | エディター上での構文強調表示を提供する |
| 作者 | |
| メンテナー | Community Tech |
| 最新バージョン | 6.0.0 |
| 互換性の方針 | スナップショットが MediaWiki とともにリリースされます。 master には後方互換性がありません。 |
| PHP | 7.4+ |
|
|
| ライセンス | GNU 一般公衆利用許諾書 2.0 以降 |
| ダウンロード | README |
| ヘルプ | Help:Extension:CodeMirror/ja |
| translatewiki.net で翻訳を利用できる場合は、CodeMirror 拡張機能の翻訳にご協力ください | |
| 問題点 | 未解決のタスク · バグを報告 |
CodeMirror 拡張機能は、CodeMirror ライブラリを使用して、ウィキテキストを含む様々なプログラミング言語とマークアップ言語の構文の強調表示に対応したスタンドアローンなエディターを提供します。 WikiEditor、2017年版ウィキテキストエディター、Proofread Pageなどの他のエディターでも使用できます。 使い方と機能の一覧については、Help:Extension:CodeMirror をご覧ください。
2024から2025年にかけて、この拡張機能は新しいメジャーバージョンの CodeMirror 6 にアップグレードされ、多くの新機能が追加されました。
- 古いバージョンの情報については Extension:CodeMirror/5 を参照してください。
- For help documentation, see Help:Extension:CodeMirror.
- For the JavaScript documentation, see wmdoc:CodeMirror.
インストール
- ダウンロードして、ファイルを
extensions/フォルダー内のCodeMirrorという名前のディレクトリ内に配置します。
開発者とコード寄稿者は、上記の代わりに以下を使用してGitからインストールします:cd extensions/ git clone https://gerrit.wikimedia.org/r/mediawiki/extensions/CodeMirror
- 以下のコードを LocalSettings.php ファイルの末尾に追加します:
wfLoadExtension( 'CodeMirror' );
- Configure as required.
完了 – ウィキの「Special:Version」に移動して、拡張機能が正しくインストールされたことを確認します。
設定
すべての利用者に対して既定で CodeMirror を有効にするには、LocalSettings.php に次のコードを追加します:
# Enables use of CodeMirror by default but still allow users to disable it
$wgDefaultUserOptions[ 'usecodemirror' ] = true;
$wgCodeMirrorV6- CodeMirror 6 への移行を制御するための一時的な機能フラグ(T259059)。
$wgCodeMirrorEnabledModes- Extension:CodeEditor との競合を制御するための一時的な機能フラグ。 See the Using CodeMirror instead of CodeEditor section below for more information.
$wgCodeMirrorConflictingGadgets- 有効にすると CodeMirror が読み込まれなくなるガジェット名の配列。既定値は wikEd です。
$wgCodeMirrorPrimaryPreferences- The preferences to advertse in the in-editor preferences panel. These should have a clear visual effect or be commonly used to warrant easier access to toggling the feature. Preferences not listed here will still be available in the full preferences dialog.
$wgCodeMirrorDefaultPreferences- すべての利用者に対して既定で有効にする機能を制御します。
trueかfalseを用いて機能自体を有効または無効化するか、名前空間ID(整数)またはコンテンツモデル(文字列)を含む配列を指定します。 例えば、JavaScript ページまたはテンプレートのみに自動補完を限定するには、次のように指定します:
# Limit CodeMirror autocompletion to JavaScript pages and templates
$wgCodeMirrorDefaultPreferences[ 'autocomplete' ] = [ CONTENT_MODEL_JAVASCRIPT, NS_TEMPLATE ];
| 機能 | 2017年版エディター との互換性 |
既定値 |
|---|---|---|
activeLine |
| |
autocomplete |
| |
autofocus |
| |
bidiIsolation |
| |
bracketMatching |
| |
codeFolding |
| |
closeBrackets |
| |
highlightRefs |
| |
lineNumbering |
| |
lineWrapping |
| |
lint |
| |
openLinks |
| |
specialChars |
| |
whitespace |
|
CodeMirror 5 との違い
新しい機能
- WikiEditor は不要になりました。
- CodeEditor の代替として、Scribunto、JavaScript、CSS、JSON、Vue をサポート。
- 大幅なパフォーマンスの改善。
- 双方向分離による右書き (RTL) のサポート (T170001)。 (T358804)
- コードの折りたたみ。 (T30684)
- 自動補完。 (T95100)
- Linting. (T381577)
- 修飾キー+クリックでリンクを直接開く。 (T303392)
- 検索パネルの改善。 (T371436)
- CodeMirror の環境設定。 (T359498)
- 議論ツール での構文の強調表示。 (T407918)
- 読み取り専用ページでの構文の強調表示。 (T301615)
- Special:ExpandTemplates での構文の強調表示。 (T384148)
- 拡張機能、ガジェット、利用者スクリプトと連携するための強力な JavaScript API。
廃止およびその他の変更点
- ResourceLoader モジュールが変更されます。移行ガイドをご覧ください。
ext.CodeMirror.switchフックは非推奨となりました。 代わりにext.CodeMirror.toggleを使用してください。- CSS クラス
.cm-mw-mnemonicの名前は.cm-mw-html-entityに変更されました。 .cm-mw-template-name-mnemonicクラスは除去されました。 代わりに.cm-mw-template-ground.cm-html-entityを使用してください。- CSS クラス
.cm-mw-apostrophes-boldおよび.cm-mw-apostrophes-italicは除去されました。 代わりに.cm-mw-apostrophesを使用してください。 <nowiki>、<pre>、または関連付けられた TagMode のないタグの行単位のスタイル設定が除去されました(T351686)。- ウィキテキスト内の混合言語はまだサポートされていません(T357480)。
- ブラウザー固有の検索機能(Ctrl+F)は、CodeMirror の検索機能に置き換えられます。 これはパフォーマンスを維持するために必要です(T303664)。
移行ガイド
| MediaWiki バージョン: | ≧ 1.44 |
このガイドは MediaWiki 1.45 以降に適用されます。MediaWiki 1.46 のリリースまでにすべての移行完了を目標にしてください(リリースタイムライン)。
MediaWiki の設定
$wgCodeMirrorLineNumberingNamespacesは非推奨となりました。代わりに$wgCodeMirrorDefaultPreferencesを設定してください。
ResourceLoader モジュール
新しい .v6 モジュールを使用しているか確認してください。
CodeMirror 6 は WikiEditor に依存しなくなったため、CodeMirror 5 の対応するモジュールと比べて、名称と動作が一部変更されています:
一部のモジュール名は簡潔にするために先頭の ext.CodeMirror を省略しています。使用する際は「…」部分を置き換えてください。
| 旧モジュール | 新モジュール (MW 1.44) | New module (MW 1.45) | 新モジュール(MW 1.46以降) | 説明 |
|---|---|---|---|---|
ext.CodeMirror
|
….v6.WikiEditor.init
|
….v6.init
|
….init
|
#wpTextbox1 (通常の編集テキストエリア) でのウィキエディター用 CodeMirror 統合。
|
| N/A | ….v6.WikiEditor
|
….v6.WikiEditor
|
….WikiEditor
|
Exports the CodeMirrorWikiEditor class |
| N/A | ….v6.init
|
….v6.init
|
….init
|
CodeMirror for #wpTextbox1 and other supported pages.
|
….lib
|
….v6.init
|
….v6.init
|
….lib
|
Exports CodeMirror internals. |
….addons
|
N/A | N/A | N/A | This packaged the bracket matching feature in CodeMirror 5. Bracket matching is default behaviour in CodeMirror 6. |
….mode.mediawiki
|
….v6.mode.mediawiki
|
….v6.mode.mediawiki
|
….mode.mediawiki
|
The MediaWiki language mode. |
| N/A | ext.CodeMirror.v6
|
ext.CodeMirror.v6
|
ext.CodeMirror
|
Exports the CodeMirror class. |
….visualEditor
|
….visualEditor.init
|
….visualEditor.init
|
….init
|
Integration with the 2017年版ウィキテキストエディター. |
….lib.mode.php
|
N/A | N/A | CodeMirror 6 will eventually provide some or all of these modes, but they will not be available as separate modules. | |
….lib.mode.clike
|
||||
….lib.mode.htmlmixed
|
||||
….lib.mode.xml
|
||||
….lib.mode.javascript
|
….v6.mode.javascript
|
….v6.modes
|
….modes
|
これらの言語は CodeMirror 6 でサポートされています。 |
….lib.mode.css
|
….v6.mode.css
| |||
| N/A | ….v6.mode.lua
| |||
| N/A | ….v6.mode.json
| |||
| N/A | ….v6.mode.vue
|
MediaWiki 1.46 のリリースをもって、旧モジュールは新モジュールに置き換えられ、.v6 モジュールはしばらくの間完全に除去されずにエイリアスとして残ります。
ガジェットと利用者スクリプト
The CodeMirror global has been removed entirely.
For example, CodeMirror.fromTextArea( myTextarea ) will no longer work.
Instead, first load the desired ResourceLoader modules, instantiate a CodeMirror object, and call the initialize() method.
If your script relies on the ext.CodeMirror.switch hook to change the way it interacts with the editor, you'll need to use ext.CodeMirror.toggle instead, or alternatively listen to an event.
See the JavaScript integration section for more information.
CSS
.CodeMirror 要素は廃止されました。
CodeMirror の DOM 全体を扱う場合は .cm-editor を、(検索パネルなどを含まない)内部コンテンツを扱う場合は .cm-content を使用してください。
その他の CSS クラスに関しては、廃止およびその他の変更点を参照してください。
連携
MediaWiki 拡張機能
CodeEditor の代わりに CodeMirror を使用
| MediaWiki バージョン: | ≧ 1.44 |
MediaWiki 1.44 以降、CodeMirror はLua、JavaScript、CSS、JSON、Vue 言語の構文強調表示をサポートしています。これは、Extension:CodeEditor の代わりとして使用できます。
ただし、CodeEditor と連携しているすべての拡張機能が CodeMirror をサポートするように更新されているわけではありません。以下の拡張機能を使用している場合は、引き続き CodeEditor をウィキにインストールしておくことをお勧めします。
- Extension:VisualEditor - CodeEditor モジュールを使用して、VisualEditor と連携している他の拡張機能の構文強調表示を強化します(T400014):
- Extension:AbuseFilter - 不正利用フィルターの構文強調表示は CodeEditor に依存します(T399673)。
- 拡張機能:WikiLambda -
ext.wikilambda.appモジュールは CodeEditor に依存します(T400015)。
As of MediaWiki 1.45, for this to work, you'll need to adjust $wgCodeMirrorEnabledModes accordingly, along with the *UseCodeEditor and *UseCodeMirror for any applicable extensions you also have installed:
// Desired modes that should use CodeMirror (mediawiki, i.e. wikitext, is enabled by default)
$wgCodeMirrorEnabledModes['javascript'] = true;
$wgCodeMirrorEnabledModes['json'] = true;
$wgCodeMirrorEnabledModes['css'] = true;
$wgCodeMirrorEnabledModes['lua'] = true;
$wgCodeMirrorEnabledModes['vue'] = true;
// If you're also using CodeEditor, disable the same modes there:
$wgCodeEditorEnabledModes['javascript'] = false;
$wgCodeEditorEnabledModes['json'] = false;
$wgCodeEditorEnabledModes['css'] = false;
$wgCodeEditorEnabledModes['lua'] = false;
$wgCodeEditorEnabledModes['vue'] = false;
// Gadgets (for editing Gadget definition JSON pages)
$wgGadgetsDefinitionsUseCodeEditor = false;
$wgGadgetsDefinitionsUseCodeMirror = true;
// JsonConfig
$wgJsonConfigUseCodeEditor = false;
$wgJsonConfigUseCodeMirror = true;
// Scribunto (for editing Module pages that use the "lua" mode)
$wgScribuntoUseCodeEditor = false;
$wgScribuntoUseCodeMirror = true;
// TemplateStyles
$wgTemplateStylesUseCodeEditor = false;
$wgTemplateStylesUseCodeMirror = true;
// UploadWizard (for editing Campaign JSON pages)
$wgUploadWizardUseCodeEditor = false;
$wgUploadWizardUseCodeMirror = true;
Registering a new tag for MediaWiki
If you simply want CodeMirror to recognize a tag that is added by an extension, you can do so using the CodeMirrorTagModes extension attribute.
For example, to register the tag <foo> as something containing wikitext, you would add the following to extension.json:
{
"attributes": {
"CodeMirror": {
"TagModes": [
"foo": "mediawiki"
]
}
}
}
CodeMirror will then highlight the content inside <foo>...</foo> as wikitext.
Registering a tag so that CodeMirror treats the contents as something other than wikitext is currently not supported (T357480).
If a tag is not registered, CodeMirror will highlight the contents as non-wikitext in the same way it highlights the contents of a <nowiki>...</nowiki> tag.
PHP フック
| MediaWiki バージョン: | ≧ 1.44 |
Since MediaWiki 1.44, the preferred means to integrate with CodeMirror in PHP is implementing the CodeMirrorGetModeHook interface:
class CodeMirrorHooks implements CodeMirrorGetModeHook {
public function onCodeMirrorGetMode(Title $title, ?string &$mode, string $model ): bool {
// Logic to determine if CodeMirror should be used
if ( $title->hasContentModel( 'JsonLikeContentModel' ) ) {
$mode = 'json';
return false;
}
return true;
}
}
Register the hook in extension.json:
"Hooks": {
"CodeMirrorGetMode": "codemirror"
},
"HookHandlers": {
"codemirror": {
"class": "MediaWiki\\Extension\\MyExtension\\CodeMirrorHooks"
}
}
PluginModules
CodeMirrorPluginModules is an extension attribute that allows side-loading a module with CodeMirror.
This unconditionally loads the module whenever the ext.CodeMirror.v6 module is loaded.
extension.json:
{
"attributes": {
"CodeMirror": {
"PluginModules": [
"ext.MyExtension.CodeMirror"
]
}
}
}
JavaScript
The CodeMirror editor is not an actual textarea, but a contenteditable. Usually developers need to ensure their code works with both given that CodeMirror can be toggled off.
For detecting changes to the document, using an event or a hook is likely the simplest. For reading and making changes, jQuery.textSelection may be convenient.
For more complex integrations or those who need better performance, you can add your own extension to a new or an existing CodeMirror instance.
jQuery.textSelectionを使用
If you simply want to fetch or make changes to the document text, jQuery.textSelection is the easiest and most reliable means to do so. Usage of jQuery.textSelection on the textarea is bubbled up to CodeMirror, so you don't need to have any knowledge of whether CodeMirror is enabled:
const $textarea = $( '#wpTextbox1' )
const content = $textarea.textSelection( 'getContents' );
// Append "Foobar" to the content.
$textarea.textSelection( 'setContents', content + '\nFoobar' );
jQuery's .val() on #wpTextbox1 can be used,[3] but this isn't recommended and may not work in all editors, such as the 2017年版ウィキテキストエディター.
ResourceLoaderの使用
The CodeMirror extension provides a number of ResourceLoader modules for use by user scripts, gadgets, and extensions.
To make use of CodeMirror, you'll need at minimum the ext.CodeMirror.v6 module, along with the desired "mode".
Modes can be unpacked from the ResourceLoader modules, using an exported method with the same name as the mode.
For MediaWiki wikitext, you'd use ext.CodeMirror.v6.mode.mediawiki, and for JavaScript, JSON, CSS, Vue and Lua, you'd use ext.CodeMirror.v6.modes:
const require = await mw.loader.using( [ 'ext.CodeMirror.v6', 'ext.CodeMirror.v6.mode.mediawiki' ] );
const CodeMirror = require( 'ext.CodeMirror.v6' );
const { mediawiki } = require( 'ext.CodeMirror.v6.mode.mediawiki' );
const cm = new CodeMirror( myTextarea, mediawiki() );
cm.initialize();
If you also want WikiEditor:
const require = await mw.loader.using( [
'ext.wikiEditor',
'ext.CodeMirror.v6.WikiEditor',
'ext.CodeMirror.v6.mode.mediawiki'
] );
const textarea = document.getElementById( 'wpTextbox1' );
mw.addWikiEditor( $( textarea ) );
const CodeMirrorWikiEditor = require( 'ext.CodeMirror.v6.WikiEditor' );
const { mediawiki } = require( 'ext.CodeMirror.v6.mode.mediawiki' );
const cmWe = new CodeMirrorWikiEditor( textarea, mediawiki() );
cmWe.mode = 'mediawiki';
cmWe.initialize();
Creating a new JavaScript instance:
const require = await mw.loader.using( [ 'ext.CodeMirror.v6', 'ext.CodeMirror.v6.modes' ] );
const CodeMirror = require( 'ext.CodeMirror.v6' );
const { javascript } = require( 'ext.CodeMirror.v6.modes' );
const cm = new CodeMirror( myTextarea, javascript() );
cm.initialize();
| モジュール | 説明 |
|---|---|
ext.CodeMirror.v6.lib
|
The core CodeMirror library. You shouldn't need to require this directly unless you need access to the upstream CodeMirror API. |
ext.CodeMirror.v6
|
The basic CodeMirror integration for MediaWiki editors. This module exports the CodeMirror class. |
ext.CodeMirror.v6.WikiEditor
|
CodeMirror integration for WikiEditor. This module exports the CodeMirrorWikiEditor class. |
ext.CodeMirror.v6.mode.mediawiki
|
Exports the mediawiki mode for highlighting MediaWiki wikitext.
|
ext.CodeMirror.v6.modes
|
Includes the modes javascript, json, css, vue and lua.
|
ext.CodeMirror.v6.init(内部) |
The main entrypoint for action=edit requests. Not intended for external use. |
ext.CodeMirror.visualEditor.init(内部) |
CodeMirror integration with the 2017年版ウィキテキストエディター, and only for wikitext. |
フックの使用
You can also integrate with CodeMirror by using frontend hooks. These allow you to run code just before or after CodeMirror has loaded, or react to changes to the document.
| フック | 説明 |
|---|---|
ext.CodeMirror.initialize
|
Called just before CodeMirror is initialized. This can be used to manipulate the DOM to suit CodeMirror (i.e. if you manipulate WikiEditor's DOM, you may need this).
パラメーター
|
ext.CodeMirror.ready
|
Called just after CodeMirror is initialized.
パラメータ
|
ext.CodeMirror.toggle
|
Called when CodeMirror is toggled on or off.
パラメーター
|
ext.CodeMirror.destroy
|
Called just after CodeMirror is destroyed and the original textarea is restored.
パラメーター
|
ext.CodeMirror.input
|
Called when document changes are made in CodeMirror. Note that the textarea may not be updated yet.
パラメーター
|
ext.CodeMirror.preferences.ready
|
Fired just before CodeMirrorPreferences has been instantiated.
パラメーター
|
ext.CodeMirror.preferences.apply
|
Fired when a CodeMirror preference is enabled or initially applied.
パラメーター
|
ext.CodeMirror.preferences.apply
|
Fired when a CodeMirror preference is changed or initially applied in an editing session.
パラメーター
|
ext.CodeMirror.preferences.display(内部) |
Fired when the preferences panel is constructed, just before it is displayed.
パラメーター
|
ext.CodeMirror.gotoLine(内部) |
Fired when the go-to line panel is opened or closed. |
ext.CodeMirror.keymap(内部) |
Fired when the keyboard shortcut help dialog is opened. |
ext.CodeMirror.search(内部) |
Fired when the search panel is opened or closed. |
イベントの使用
Using these events, you can integrate with CodeMirror using the same code as the original textarea:
myTextarea.addEventListener( 'keyup', ( event ) => {
console.log( event.key );
} );
Extending CodeMirror
You can import the ext.CodeMirror.v6.lib module to get access to the upstream CodeMirror API.
With this you can provide your own Extension when instantiating a CodeMirror or CodeMirrorWikiEditor instance.
For example, to provide your own Extension that reacts to changes made in CodeMirror:
const require = await mw.loader.using( [ 'ext.CodeMirror.v6', 'ext.CodeMirror.v6.mode.mediawiki' ] );
const CodeMirror = require( 'ext.CodeMirror.v6' );
const { mediawiki } = require( 'ext.CodeMirror.v6.mode.mediawiki' );
// ext.CodeMirror.v6.lib is a dependency of ext.CodeMirror.v6, so it's already loaded at this point.
const { EditorView } = require( 'ext.CodeMirror.v6.lib' );
const myExtension = EditorView.updateListener.of( ( /** @type {ViewUpdate} */ update ) => {
if ( update.docChanged ) {
// do something
console.log( update.changes );
}
} );
const cm = new CodeMirror( myTextarea, mediawiki() );
cm.initialize( [ cm.defaultExtensions, myExtension ] );
Or if you need to interact with an existing CodeMirror instance:
// Ensure CodeMirror is initialized first
mw.hook( 'ext.CodeMirror.ready' ).add( ( cm ) => {
const { EditorView } = require( 'ext.CodeMirror.v6.lib' );
const myExtension = EditorView.updateListener.of( ( /** @type {ViewUpdate} */ update ) => {
if ( update.docChanged ) {
// do something
console.log( update.changes );
}
} );
cm.applyExtension( myExtension );
} );
Another means of listening to changes is using the ext.CodeMirror.input hook:
mw.hook( 'ext.CodeMirror.input' ).add( ( update ) => {
// Print the ChangeSet to the console
console.log( update.changes.toJSON() );
} );
関連項目
- User:Remember the dot/Syntax highlighter
- Extension:VisualEditor
- Extension:WikiEditor
- Extension:CodeEditor
- c:Category:MediaWiki extension CodeMirror
注釈
| この拡張機能は 1 つ以上のウィキメディアのプロジェクトで使用されています。 これはおそらく、この拡張機能が安定していて高いトラフィックのウェブサイトでも十分に動作することを意味します。 この拡張機能がインストールされている場所を確認するには、ウィキメディアの設定ファイル CommonSettings.php および InitialiseSettings.php 内で、この拡張機能の名前を探してください。 特定のウィキにインストールされている拡張機能の完全な一覧は、そのウィキの Special:Version ページにあります。 |
| この拡張機能は以下のウィキ ファーム/ウィキ ホスト/パッケージに含まれています: |
- Stable extensions/ja
- User interface extensions/ja
- EditPage::showEditForm:initial extensions/ja
- EditPage::showReadOnlyForm:initial extensions/ja
- GetBetaFeaturePreferences extensions/ja
- GetPreferences extensions/ja
- PreferencesFormPreSave extensions/ja
- SpecialPageBeforeExecute extensions/ja
- UploadForm:initial extensions/ja
- GPL licensed extensions/ja
- Extensions in Wikimedia version control/ja
- All extensions/ja
- Extensions used on Wikimedia/ja
- Extensions included in Canasta/ja
- Extensions included in Fandom/ja
- Extensions included in Miraheze/ja
- Extensions included in MyWikis/ja
- Extensions included in ProWiki/ja
- Extensions included in Telepedia/ja
- Extensions included in wiki.gg/ja
- Syntax highlighting extensions/ja
