2015-10-19 22:27:16 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWSyntaxHighlightWindow class.
|
|
|
|
*
|
2024-02-29 15:13:03 +00:00
|
|
|
* @copyright VisualEditor Team and others
|
2015-10-19 22:27:16 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MediaWiki syntax highlight window.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @abstract
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
ve.ui.MWSyntaxHighlightWindow = function VeUiMWSyntaxHighlightWindow() {
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.initClass( ve.ui.MWSyntaxHighlightWindow );
|
|
|
|
|
|
|
|
/* Static properties */
|
|
|
|
|
|
|
|
ve.ui.MWSyntaxHighlightWindow.static.title = OO.ui.deferMsg( 'syntaxhighlight-visualeditor-mwsyntaxhighlightinspector-title' );
|
|
|
|
|
|
|
|
ve.ui.MWSyntaxHighlightWindow.static.dir = 'ltr';
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWSyntaxHighlightWindow.prototype.initialize = function () {
|
2024-04-23 11:29:42 +00:00
|
|
|
const noneMsg = ve.msg( 'syntaxhighlight-visualeditor-mwsyntaxhighlightinspector-none' );
|
2015-10-19 22:27:16 +00:00
|
|
|
|
2018-10-08 17:37:11 +00:00
|
|
|
this.languageValid = null;
|
|
|
|
|
2016-11-09 19:39:06 +00:00
|
|
|
this.language = new OO.ui.ComboBoxInputWidget( {
|
2017-07-10 22:15:38 +00:00
|
|
|
$overlay: this.$overlay,
|
2015-10-19 22:27:16 +00:00
|
|
|
menu: {
|
|
|
|
filterFromInput: true,
|
2024-06-06 15:32:22 +00:00
|
|
|
items: ve.dm.MWSyntaxHighlightNode.static.getLanguages().map( ( lang ) => new OO.ui.MenuOptionWidget( { data: lang, label: lang || noneMsg } )
|
2024-04-23 11:29:42 +00:00
|
|
|
)
|
2015-10-19 22:27:16 +00:00
|
|
|
},
|
2016-11-15 17:20:54 +00:00
|
|
|
validate: function ( input ) {
|
2015-10-19 22:27:16 +00:00
|
|
|
return ve.dm.MWSyntaxHighlightNode.static.isLanguageSupported( input );
|
2016-11-15 17:20:54 +00:00
|
|
|
}
|
2015-10-19 22:27:16 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
this.showLinesCheckbox = new OO.ui.CheckboxInputWidget();
|
|
|
|
|
2015-06-09 15:00:04 +00:00
|
|
|
this.startLineNumber = new OO.ui.NumberInputWidget( {
|
|
|
|
min: 0,
|
2019-04-16 11:13:18 +00:00
|
|
|
isInteger: true,
|
|
|
|
showButtons: false
|
2015-06-09 15:00:04 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
// Events
|
|
|
|
this.language.connect( this, { change: 'onLanguageInputChange' } );
|
|
|
|
this.showLinesCheckbox.connect( this, { change: 'onShowLinesCheckboxChange' } );
|
|
|
|
this.startLineNumber.connect( this, { change: 'onStartLineNumberChange' } );
|
|
|
|
|
2015-10-19 22:27:16 +00:00
|
|
|
this.languageField = new OO.ui.FieldLayout( this.language, {
|
|
|
|
classes: [ 've-ui-mwSyntaxHighlightWindow-languageField' ],
|
|
|
|
align: 'top',
|
|
|
|
label: ve.msg( 'syntaxhighlight-visualeditor-mwsyntaxhighlightinspector-language' )
|
|
|
|
} );
|
|
|
|
this.codeField = new OO.ui.FieldLayout( this.input, {
|
|
|
|
align: 'top',
|
|
|
|
label: ve.msg( 'syntaxhighlight-visualeditor-mwsyntaxhighlightinspector-code' )
|
|
|
|
} );
|
|
|
|
this.showLinesField = new OO.ui.FieldLayout( this.showLinesCheckbox, {
|
|
|
|
align: 'inline',
|
|
|
|
label: ve.msg( 'syntaxhighlight-visualeditor-mwsyntaxhighlightinspector-showlines' )
|
|
|
|
} );
|
2015-06-09 15:00:04 +00:00
|
|
|
this.startLineField = new OO.ui.FieldLayout( this.startLineNumber, {
|
|
|
|
classes: [ 've-ui-mwSyntaxHighlightWindow-startLineField' ],
|
|
|
|
align: 'left',
|
|
|
|
label: ve.msg( 'syntaxhighlight-visualeditor-mwsyntaxhighlightinspector-startingline' )
|
|
|
|
} );
|
2015-10-19 22:27:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle input change events
|
|
|
|
*
|
|
|
|
* @param {string} value New value
|
|
|
|
*/
|
|
|
|
ve.ui.MWSyntaxHighlightWindow.prototype.onLanguageInputChange = function () {
|
2024-04-23 11:29:42 +00:00
|
|
|
const validity = this.language.getValidity();
|
|
|
|
validity.always( () => {
|
|
|
|
this.languageValid = validity.state() === 'resolved';
|
|
|
|
this.updateActions();
|
2015-10-19 22:27:16 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2015-06-09 15:00:04 +00:00
|
|
|
/**
|
|
|
|
* Handle change events from the show lines chechbox
|
|
|
|
*
|
|
|
|
* @param {boolean} value Widget value
|
|
|
|
*/
|
|
|
|
ve.ui.MWSyntaxHighlightWindow.prototype.onShowLinesCheckboxChange = function () {
|
2024-04-23 11:29:42 +00:00
|
|
|
const showLines = this.showLinesCheckbox.isSelected();
|
2015-06-09 15:00:04 +00:00
|
|
|
this.input.toggleLineNumbers( showLines );
|
|
|
|
this.startLineNumber.setDisabled( !showLines );
|
2018-10-08 17:37:11 +00:00
|
|
|
this.updateActions();
|
2015-06-09 15:00:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle change events from the start line input
|
|
|
|
*
|
|
|
|
* @param {string} value Widget value
|
|
|
|
*/
|
|
|
|
ve.ui.MWSyntaxHighlightWindow.prototype.onStartLineNumberChange = function ( value ) {
|
2024-04-23 11:29:42 +00:00
|
|
|
const input = this.input;
|
2015-06-09 15:00:04 +00:00
|
|
|
|
2024-04-23 11:29:42 +00:00
|
|
|
input.loadingPromise.done( () => {
|
2015-06-09 15:00:04 +00:00
|
|
|
input.editor.setOption( 'firstLineNumber', value !== '' ? +value : 1 );
|
2024-04-23 11:29:42 +00:00
|
|
|
} ).always( () => {
|
|
|
|
this.updateActions();
|
2015-06-09 15:00:04 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2015-10-19 22:27:16 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc OO.ui.Window
|
|
|
|
*/
|
|
|
|
ve.ui.MWSyntaxHighlightWindow.prototype.getReadyProcess = function ( data, process ) {
|
2024-04-23 11:29:42 +00:00
|
|
|
return process.next( () => {
|
2015-10-19 22:27:16 +00:00
|
|
|
this.language.getMenu().toggle( false );
|
2016-11-15 17:20:54 +00:00
|
|
|
if ( !this.language.getValue() ) {
|
|
|
|
this.language.focus();
|
2015-10-19 22:27:16 +00:00
|
|
|
} else {
|
|
|
|
this.input.focus();
|
|
|
|
}
|
2024-04-23 11:29:42 +00:00
|
|
|
} );
|
2015-10-19 22:27:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc OO.ui.Window
|
|
|
|
*/
|
|
|
|
ve.ui.MWSyntaxHighlightWindow.prototype.getSetupProcess = function ( data, process ) {
|
2024-04-23 11:29:42 +00:00
|
|
|
return process.next( () => {
|
|
|
|
const attrs = this.selectedNode ? this.selectedNode.getAttribute( 'mw' ).attrs : {},
|
2018-10-31 12:45:50 +00:00
|
|
|
language = attrs.lang ? attrs.lang.toLowerCase() : '',
|
2015-06-09 15:00:04 +00:00
|
|
|
showLines = attrs.line !== undefined,
|
2019-04-16 11:13:18 +00:00
|
|
|
startLine = attrs.start,
|
|
|
|
isReadOnly = this.isReadOnly();
|
2015-10-19 22:27:16 +00:00
|
|
|
|
2019-04-16 11:13:18 +00:00
|
|
|
this.language.setValue( language ).setReadOnly( isReadOnly );
|
2015-10-19 22:27:16 +00:00
|
|
|
|
2019-04-16 11:13:18 +00:00
|
|
|
this.showLinesCheckbox.setSelected( showLines ).setDisabled( isReadOnly );
|
|
|
|
this.startLineNumber.setValue( startLine ).setReadOnly( isReadOnly );
|
2024-04-23 11:29:42 +00:00
|
|
|
} );
|
2015-10-19 22:27:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc OO.ui.Window
|
|
|
|
*/
|
|
|
|
ve.ui.MWSyntaxHighlightWindow.prototype.getTeardownProcess = function ( data, process ) {
|
|
|
|
return process;
|
|
|
|
};
|
|
|
|
|
2018-10-08 17:37:11 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc ve.ui.MWExtensionWindow
|
|
|
|
*/
|
|
|
|
ve.ui.MWSyntaxHighlightWindow.prototype.updateActions = function () {
|
|
|
|
this.getActions().setAbilities( { done: this.languageValid && this.isModified() } );
|
|
|
|
};
|
|
|
|
|
2015-10-19 22:27:16 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc ve.ui.MWExtensionWindow
|
|
|
|
*/
|
|
|
|
ve.ui.MWSyntaxHighlightWindow.prototype.updateMwData = function ( mwData ) {
|
2024-04-23 11:29:42 +00:00
|
|
|
const language = this.language.getValue(),
|
2015-06-09 15:00:04 +00:00
|
|
|
showLines = this.showLinesCheckbox.isSelected(),
|
|
|
|
startLine = this.startLineNumber.getValue();
|
2015-10-19 22:27:16 +00:00
|
|
|
|
|
|
|
mwData.attrs.lang = language || undefined;
|
2022-10-26 10:54:59 +00:00
|
|
|
if ( !showLines ) {
|
|
|
|
delete mwData.attrs.line;
|
|
|
|
// Keep whatever value was there before to not cause dirty diffs
|
|
|
|
} else if ( !( 'line' in mwData.attrs ) ) {
|
|
|
|
mwData.attrs.line = '1';
|
|
|
|
}
|
2015-06-09 15:00:04 +00:00
|
|
|
mwData.attrs.start = startLine !== '' ? startLine : undefined;
|
2015-10-19 22:27:16 +00:00
|
|
|
};
|