2015-09-05 14:23:59 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWExtensionWindow class.
|
|
|
|
*
|
2023-12-01 16:06:11 +00:00
|
|
|
* @copyright See AUTHORS.txt
|
2015-09-05 14:23:59 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mixin for windows for editing generic MediaWiki extensions.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @abstract
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
*/
|
|
|
|
ve.ui.MWExtensionWindow = function VeUiMWExtensionWindow() {
|
|
|
|
this.whitespace = null;
|
|
|
|
this.input = null;
|
2017-01-14 23:03:36 +00:00
|
|
|
this.originalMwData = null;
|
|
|
|
|
2017-03-15 17:45:00 +00:00
|
|
|
this.onChangeHandler = ve.debounce( this.onChange.bind( this ) );
|
2015-09-05 14:23:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.initClass( ve.ui.MWExtensionWindow );
|
|
|
|
|
|
|
|
/* Static properties */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extension is allowed to have empty contents
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @property {boolean}
|
|
|
|
* @inheritable
|
|
|
|
*/
|
|
|
|
ve.ui.MWExtensionWindow.static.allowedEmpty = false;
|
|
|
|
|
2017-10-02 15:50:38 +00:00
|
|
|
/**
|
|
|
|
* Tell Parsoid to self-close tags when the body is empty
|
|
|
|
*
|
|
|
|
* i.e. `<foo></foo>` -> `<foo/>`
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @property {boolean}
|
|
|
|
* @inheritable
|
|
|
|
*/
|
|
|
|
ve.ui.MWExtensionWindow.static.selfCloseEmptyBody = false;
|
|
|
|
|
2015-09-05 14:23:59 +00:00
|
|
|
/**
|
|
|
|
* Inspector's directionality, 'ltr' or 'rtl'
|
|
|
|
*
|
|
|
|
* Leave as null to use the directionality of the current fragment.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @property {string|null}
|
|
|
|
* @inheritable
|
|
|
|
*/
|
|
|
|
ve.ui.MWExtensionWindow.static.dir = null;
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc OO.ui.Window
|
|
|
|
*/
|
|
|
|
ve.ui.MWExtensionWindow.prototype.initialize = function () {
|
|
|
|
this.input = new ve.ui.WhitespacePreservingTextInputWidget( {
|
|
|
|
limit: 1,
|
2015-10-21 15:30:57 +00:00
|
|
|
classes: [ 've-ui-mwExtensionWindow-input' ]
|
2015-09-05 14:23:59 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the placeholder text for the content input area.
|
|
|
|
*
|
|
|
|
* @return {string} Placeholder text
|
|
|
|
*/
|
|
|
|
ve.ui.MWExtensionWindow.prototype.getInputPlaceholder = function () {
|
|
|
|
return '';
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc OO.ui.Window
|
|
|
|
*/
|
|
|
|
ve.ui.MWExtensionWindow.prototype.getSetupProcess = function ( data, process ) {
|
|
|
|
data = data || {};
|
2024-04-30 21:25:39 +00:00
|
|
|
return process.next( () => {
|
2015-09-05 14:23:59 +00:00
|
|
|
// Initialization
|
|
|
|
this.whitespace = [ '', '' ];
|
|
|
|
|
|
|
|
if ( this.selectedNode ) {
|
2022-02-21 18:07:07 +00:00
|
|
|
var mwData = this.selectedNode.getAttribute( 'mw' );
|
2017-08-23 14:05:26 +00:00
|
|
|
// mwData.body can be null in <selfclosing/> extensions
|
2017-10-02 15:26:16 +00:00
|
|
|
this.input.setValueAndWhitespace( ( mwData.body && mwData.body.extsrc ) || '' );
|
2017-08-23 14:05:26 +00:00
|
|
|
this.originalMwData = mwData;
|
2015-09-05 14:23:59 +00:00
|
|
|
} else {
|
|
|
|
if ( !this.constructor.static.modelClasses[ 0 ].static.isContent ) {
|
|
|
|
// New nodes should use linebreaks for blocks
|
|
|
|
this.input.setWhitespace( [ '\n', '\n' ] );
|
|
|
|
}
|
|
|
|
this.input.setValue( '' );
|
|
|
|
}
|
|
|
|
|
|
|
|
this.input.$input.attr( 'placeholder', this.getInputPlaceholder() );
|
|
|
|
|
2022-02-21 18:07:07 +00:00
|
|
|
var dir = this.constructor.static.dir || data.dir;
|
2016-11-09 19:13:09 +00:00
|
|
|
this.input.setDir( dir );
|
2019-02-08 19:18:22 +00:00
|
|
|
this.input.setReadOnly( this.isReadOnly() );
|
2017-01-14 23:03:36 +00:00
|
|
|
|
|
|
|
this.actions.setAbilities( { done: false } );
|
|
|
|
this.input.connect( this, { change: 'onChangeHandler' } );
|
2024-04-30 21:25:39 +00:00
|
|
|
} );
|
2015-09-05 14:23:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc OO.ui.Window
|
|
|
|
*/
|
|
|
|
ve.ui.MWExtensionWindow.prototype.getReadyProcess = function ( data, process ) {
|
|
|
|
return process;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc OO.ui.Window
|
|
|
|
*/
|
|
|
|
ve.ui.MWExtensionWindow.prototype.getTeardownProcess = function ( data, process ) {
|
2024-04-30 21:25:39 +00:00
|
|
|
return process.next( () => {
|
2017-09-07 15:40:36 +00:00
|
|
|
// Don't hold on to the original data, it's only refreshed on setup for existing nodes
|
|
|
|
this.originalMwData = null;
|
2017-10-02 15:20:49 +00:00
|
|
|
this.input.disconnect( this, { change: 'onChangeHandler' } );
|
2024-04-30 21:25:39 +00:00
|
|
|
} );
|
2015-09-05 14:23:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc OO.ui.Dialog
|
|
|
|
*/
|
|
|
|
ve.ui.MWExtensionWindow.prototype.getActionProcess = function ( action, process ) {
|
2024-04-30 21:25:39 +00:00
|
|
|
return process.first( () => {
|
2015-09-05 14:23:59 +00:00
|
|
|
if ( action === 'done' ) {
|
2015-11-20 20:54:50 +00:00
|
|
|
if ( this.constructor.static.allowedEmpty || this.input.getValue() !== '' ) {
|
2015-09-05 14:23:59 +00:00
|
|
|
this.insertOrUpdateNode();
|
|
|
|
} else if ( this.selectedNode && !this.constructor.static.allowedEmpty ) {
|
|
|
|
// Content has been emptied on a node which isn't allowed to
|
|
|
|
// be empty, so delete it.
|
|
|
|
this.removeNode();
|
|
|
|
}
|
|
|
|
}
|
2024-04-30 21:25:39 +00:00
|
|
|
} );
|
2015-09-05 14:23:59 +00:00
|
|
|
};
|
|
|
|
|
2017-01-14 23:03:36 +00:00
|
|
|
/**
|
|
|
|
* Handle change event.
|
|
|
|
*/
|
|
|
|
ve.ui.MWExtensionWindow.prototype.onChange = function () {
|
|
|
|
this.updateActions();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the 'done' action according to whether there are changes
|
|
|
|
*/
|
|
|
|
ve.ui.MWExtensionWindow.prototype.updateActions = function () {
|
2024-04-29 15:16:33 +00:00
|
|
|
this.actions.setAbilities( { done: this.isSaveable() } );
|
2017-01-14 23:03:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2024-04-26 15:52:01 +00:00
|
|
|
* Check if mwData would be modified if window contents were applied.
|
|
|
|
* This is used to determine if it's meaningful for the user to save the
|
|
|
|
* contents into the document; this is likely true of newly-created elements.
|
2017-01-14 23:03:36 +00:00
|
|
|
*
|
|
|
|
* @return {boolean} mwData would be modified
|
|
|
|
*/
|
2024-04-29 15:16:33 +00:00
|
|
|
ve.ui.MWExtensionWindow.prototype.isSaveable = function () {
|
2022-02-21 18:07:07 +00:00
|
|
|
var modified;
|
2017-01-14 23:03:36 +00:00
|
|
|
if ( this.originalMwData ) {
|
2022-02-21 18:07:07 +00:00
|
|
|
var mwDataCopy = ve.copy( this.originalMwData );
|
2017-01-14 23:03:36 +00:00
|
|
|
this.updateMwData( mwDataCopy );
|
|
|
|
modified = !ve.compare( this.originalMwData, mwDataCopy );
|
|
|
|
} else {
|
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
return modified;
|
|
|
|
};
|
|
|
|
|
2024-04-29 15:16:33 +00:00
|
|
|
/**
|
|
|
|
* @deprecated Moved to ve.ui.MWExtensionWindow.prototype.isSaveable
|
|
|
|
* @return {boolean} mwData would be modified
|
|
|
|
*/
|
|
|
|
ve.ui.MWExtensionWindow.prototype.isModified = ve.ui.MWExtensionWindow.prototype.isSaveable;
|
|
|
|
|
2024-04-26 15:52:01 +00:00
|
|
|
/**
|
|
|
|
* Check if mwData has meaningful edits. This is used to determine if it's
|
|
|
|
* meaningful to warn the user before closing the dialog without saving. Unlike
|
|
|
|
* `isModified()` above, we consider a newly-created but unmodified element to
|
|
|
|
* be non-meaningful because the user can simply re-open the dialog to restore
|
|
|
|
* their state.
|
|
|
|
*
|
|
|
|
* @return {boolean} mwData would contain new user input
|
|
|
|
*/
|
|
|
|
ve.ui.MWExtensionWindow.prototype.hasMeaningfulEdits = function () {
|
2024-05-20 17:51:22 +00:00
|
|
|
let mwDataBaseline;
|
2024-04-26 15:52:01 +00:00
|
|
|
if ( this.originalMwData ) {
|
|
|
|
mwDataBaseline = this.originalMwData;
|
|
|
|
} else {
|
|
|
|
mwDataBaseline = this.getNewElement().attributes.mw;
|
|
|
|
}
|
2024-05-20 17:51:22 +00:00
|
|
|
const mwDataCopy = ve.copy( mwDataBaseline );
|
2024-04-26 15:52:01 +00:00
|
|
|
this.updateMwData( mwDataCopy );
|
|
|
|
|
|
|
|
// We have some difficulty here. `updateMwData()` in this class calls on
|
|
|
|
// `this.input.getValueAndWhitespace()`. The 'and whitespace' means that
|
|
|
|
// we cannot directly compare a new element's mwData with a newly-opened
|
|
|
|
// dialog's mwData because it may have additional newlines.
|
|
|
|
// We don't want to touch `this.input` or `prototype.updateMwData` because
|
|
|
|
// they're overridden in subclasses. Therefore, we consider whitespace-only
|
|
|
|
// changes to a new element to be non-meaningful too.
|
2024-05-20 17:51:22 +00:00
|
|
|
const changed = OO.getProp( mwDataCopy, 'body', 'extsrc' );
|
2024-04-26 15:52:01 +00:00
|
|
|
if ( changed !== undefined ) {
|
|
|
|
OO.setProp( mwDataCopy, 'body', 'extsrc', changed.trim() );
|
|
|
|
}
|
|
|
|
|
2024-05-20 17:51:22 +00:00
|
|
|
// Also trim the baseline. In "edit" mode we likely have added whitespace,
|
|
|
|
// and in "insert" mode we don't want to break if the default value starts
|
|
|
|
// or ends with whitespace.
|
|
|
|
const baselineChanged = OO.getProp( mwDataBaseline, 'body', 'extsrc' );
|
|
|
|
if ( baselineChanged !== undefined ) {
|
|
|
|
OO.setProp( mwDataBaseline, 'body', 'extsrc', baselineChanged.trim() );
|
|
|
|
}
|
|
|
|
|
2024-04-26 15:52:01 +00:00
|
|
|
return !ve.compare( mwDataBaseline, mwDataCopy );
|
|
|
|
};
|
|
|
|
|
2015-09-05 14:23:59 +00:00
|
|
|
/**
|
|
|
|
* Create an new data element for the model class associated with this inspector
|
|
|
|
*
|
|
|
|
* @return {Object} Element data
|
|
|
|
*/
|
|
|
|
ve.ui.MWExtensionWindow.prototype.getNewElement = function () {
|
|
|
|
// Extension inspectors which create elements should either match
|
|
|
|
// a single modelClass or override this method.
|
|
|
|
var modelClass = this.constructor.static.modelClasses[ 0 ];
|
|
|
|
return {
|
|
|
|
type: modelClass.static.name,
|
|
|
|
attributes: {
|
|
|
|
mw: {
|
|
|
|
name: modelClass.static.extensionName,
|
|
|
|
attrs: {},
|
|
|
|
body: {
|
|
|
|
extsrc: ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert or update the node in the document model from the new values
|
|
|
|
*/
|
|
|
|
ve.ui.MWExtensionWindow.prototype.insertOrUpdateNode = function () {
|
2022-02-21 18:07:07 +00:00
|
|
|
var surfaceModel = this.getFragment().getSurface();
|
2015-09-05 14:23:59 +00:00
|
|
|
if ( this.selectedNode ) {
|
2022-02-21 18:07:07 +00:00
|
|
|
var mwData = ve.copy( this.selectedNode.getAttribute( 'mw' ) );
|
2015-09-05 14:23:59 +00:00
|
|
|
this.updateMwData( mwData );
|
|
|
|
surfaceModel.change(
|
2016-11-30 22:03:03 +00:00
|
|
|
ve.dm.TransactionBuilder.static.newFromAttributeChanges(
|
2015-09-05 14:23:59 +00:00
|
|
|
surfaceModel.getDocument(),
|
|
|
|
this.selectedNode.getOuterRange().start,
|
|
|
|
{ mw: mwData }
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
2022-02-21 18:07:07 +00:00
|
|
|
var element = this.getNewElement();
|
2015-09-05 14:23:59 +00:00
|
|
|
this.updateMwData( element.attributes.mw );
|
|
|
|
// Collapse returns a new fragment, so update this.fragment
|
|
|
|
this.fragment = this.getFragment().collapseToEnd();
|
|
|
|
this.getFragment().insertContent( [
|
|
|
|
element,
|
|
|
|
{ type: '/' + element.type }
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the node form the document model
|
|
|
|
*/
|
|
|
|
ve.ui.MWExtensionWindow.prototype.removeNode = function () {
|
|
|
|
this.getFragment().removeContent();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2018-10-08 12:34:35 +00:00
|
|
|
* Update mwData object with the new values from the inspector or dialog
|
2015-09-05 14:23:59 +00:00
|
|
|
*
|
|
|
|
* @param {Object} mwData MediaWiki data object
|
|
|
|
*/
|
|
|
|
ve.ui.MWExtensionWindow.prototype.updateMwData = function ( mwData ) {
|
|
|
|
var tagName = mwData.name,
|
2015-11-20 20:54:50 +00:00
|
|
|
value = this.input.getValueAndWhitespace();
|
2015-09-05 14:23:59 +00:00
|
|
|
|
|
|
|
// XML-like tags in wikitext are not actually XML and don't expect their contents to be escaped.
|
|
|
|
// This means that it is not possible for a tag '<foo>…</foo>' to contain the string '</foo>'.
|
2018-08-09 14:10:53 +00:00
|
|
|
// Prevent that by escaping the first angle bracket '<' to '<'. (T59429)
|
2015-09-05 14:23:59 +00:00
|
|
|
value = value.replace( new RegExp( '<(/' + tagName + '\\s*>)', 'gi' ), '<$1' );
|
|
|
|
|
2017-10-02 15:50:38 +00:00
|
|
|
if ( value.trim() === '' && this.constructor.static.selfCloseEmptyBody ) {
|
2018-10-08 12:34:35 +00:00
|
|
|
delete mwData.body;
|
2017-10-02 15:50:38 +00:00
|
|
|
} else {
|
|
|
|
mwData.body = mwData.body || {};
|
|
|
|
mwData.body.extsrc = value;
|
|
|
|
}
|
2015-09-05 14:23:59 +00:00
|
|
|
};
|