2015-12-11 14:57:49 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor MediaWiki Initialization Target class.
|
|
|
|
*
|
2020-01-08 17:13:04 +00:00
|
|
|
* @copyright 2011-2020 VisualEditor Team and others; see AUTHORS.txt
|
2015-12-11 14:57:49 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialization MediaWiki target.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends ve.init.Target
|
|
|
|
*
|
|
|
|
* @constructor
|
2021-09-11 08:05:31 +00:00
|
|
|
* @param {Object} config
|
|
|
|
* @cfg {string[]} [surfaceClasses=[]] Surface classes to apply
|
2015-12-11 14:57:49 +00:00
|
|
|
*/
|
|
|
|
ve.init.mw.Target = function VeInitMwTarget( config ) {
|
2021-05-17 21:39:22 +00:00
|
|
|
this.surfaceClasses = config.surfaceClasses || [];
|
|
|
|
|
2015-12-11 14:57:49 +00:00
|
|
|
// Parent constructor
|
|
|
|
ve.init.mw.Target.super.call( this, config );
|
|
|
|
|
2016-11-22 02:02:12 +00:00
|
|
|
this.active = false;
|
2017-05-20 21:24:10 +00:00
|
|
|
this.pageName = mw.config.get( 'wgRelevantPageName' );
|
2019-04-17 16:28:48 +00:00
|
|
|
this.recovered = false;
|
|
|
|
this.fromEditedState = false;
|
|
|
|
this.originalHtml = null;
|
2016-11-22 02:02:12 +00:00
|
|
|
|
2015-12-11 14:57:49 +00:00
|
|
|
// Initialization
|
|
|
|
this.$element.addClass( 've-init-mw-target' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.init.mw.Target, ve.init.Target );
|
|
|
|
|
|
|
|
/* Static Properties */
|
|
|
|
|
2016-04-21 11:28:00 +00:00
|
|
|
/**
|
|
|
|
* Symbolic name for this target class.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @property {string}
|
|
|
|
* @inheritable
|
|
|
|
*/
|
|
|
|
ve.init.mw.Target.static.name = null;
|
|
|
|
|
2015-12-11 14:57:49 +00:00
|
|
|
ve.init.mw.Target.static.toolbarGroups = [
|
|
|
|
{
|
2018-05-12 15:18:07 +00:00
|
|
|
name: 'history',
|
|
|
|
include: [ 'undo', 'redo' ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'format',
|
2015-12-11 14:57:49 +00:00
|
|
|
type: 'menu',
|
|
|
|
title: OO.ui.deferMsg( 'visualeditor-toolbar-format-tooltip' ),
|
|
|
|
include: [ { group: 'format' } ],
|
|
|
|
promote: [ 'paragraph' ],
|
|
|
|
demote: [ 'preformatted', 'blockquote', 'heading1' ]
|
|
|
|
},
|
|
|
|
{
|
2018-05-12 15:18:07 +00:00
|
|
|
name: 'style',
|
2015-12-11 14:57:49 +00:00
|
|
|
type: 'list',
|
|
|
|
icon: 'textStyle',
|
|
|
|
title: OO.ui.deferMsg( 'visualeditor-toolbar-style-tooltip' ),
|
2020-03-13 21:03:50 +00:00
|
|
|
label: OO.ui.deferMsg( 'visualeditor-toolbar-style-tooltip' ),
|
|
|
|
invisibleLabel: true,
|
2015-12-11 14:57:49 +00:00
|
|
|
include: [ { group: 'textStyle' }, 'language', 'clear' ],
|
|
|
|
forceExpand: [ 'bold', 'italic', 'clear' ],
|
|
|
|
promote: [ 'bold', 'italic' ],
|
2016-08-05 17:50:06 +00:00
|
|
|
demote: [ 'strikethrough', 'code', 'underline', 'language', 'big', 'small', 'clear' ]
|
2015-12-11 14:57:49 +00:00
|
|
|
},
|
|
|
|
{
|
2018-05-12 15:18:07 +00:00
|
|
|
name: 'link',
|
|
|
|
include: [ 'link' ]
|
|
|
|
},
|
|
|
|
// Placeholder for reference tools (e.g. Cite and/or Citoid)
|
|
|
|
{
|
|
|
|
name: 'reference'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'structure',
|
2015-12-11 14:57:49 +00:00
|
|
|
type: 'list',
|
|
|
|
icon: 'listBullet',
|
2016-03-29 11:05:06 +00:00
|
|
|
title: OO.ui.deferMsg( 'visualeditor-toolbar-structure' ),
|
2020-03-13 21:03:50 +00:00
|
|
|
label: OO.ui.deferMsg( 'visualeditor-toolbar-structure' ),
|
|
|
|
invisibleLabel: true,
|
2015-12-11 14:57:49 +00:00
|
|
|
include: [ { group: 'structure' } ],
|
|
|
|
demote: [ 'outdent', 'indent' ]
|
|
|
|
},
|
|
|
|
{
|
2018-05-12 15:18:07 +00:00
|
|
|
name: 'insert',
|
2015-12-11 14:57:49 +00:00
|
|
|
label: OO.ui.deferMsg( 'visualeditor-toolbar-insert' ),
|
2016-03-29 11:05:06 +00:00
|
|
|
title: OO.ui.deferMsg( 'visualeditor-toolbar-insert' ),
|
2015-12-11 14:57:49 +00:00
|
|
|
include: '*',
|
|
|
|
forceExpand: [ 'media', 'transclusion', 'insertTable' ],
|
|
|
|
promote: [ 'media', 'transclusion', 'insertTable' ]
|
|
|
|
},
|
2018-05-12 15:18:07 +00:00
|
|
|
{
|
|
|
|
name: 'specialCharacter',
|
|
|
|
include: [ 'specialCharacter' ]
|
|
|
|
}
|
2015-12-11 14:57:49 +00:00
|
|
|
];
|
|
|
|
|
2019-04-09 17:46:19 +00:00
|
|
|
ve.init.mw.Target.static.importRules = ve.copy( ve.init.mw.Target.static.importRules );
|
|
|
|
|
2019-04-23 18:48:49 +00:00
|
|
|
ve.init.mw.Target.static.importRules.external.removeOriginalDomElements = true;
|
|
|
|
|
2019-04-09 17:46:19 +00:00
|
|
|
ve.init.mw.Target.static.importRules.external.blacklist = ve.extendObject( {
|
|
|
|
// Annotations
|
|
|
|
'textStyle/underline': true,
|
|
|
|
'meta/language': true,
|
|
|
|
'textStyle/datetime': true,
|
|
|
|
'link/mwExternal': !mw.config.get( 'wgVisualEditorConfig' ).allowExternalLinkPaste,
|
|
|
|
// Node
|
|
|
|
article: true,
|
|
|
|
section: true
|
|
|
|
}, ve.init.mw.Target.static.importRules.external.blacklist );
|
|
|
|
|
2019-09-04 16:14:38 +00:00
|
|
|
ve.init.mw.Target.static.importRules.external.htmlBlacklist.remove = ve.extendObject( {
|
2019-09-10 11:32:10 +00:00
|
|
|
// TODO: Create a plugin system for extending the blacklist, so this code
|
|
|
|
// can be moved to the Cite extension.
|
2019-04-09 17:46:19 +00:00
|
|
|
// Remove reference numbers copied from MW read mode (T150418)
|
2019-09-10 11:32:10 +00:00
|
|
|
'sup.reference:not( [typeof] )': true,
|
|
|
|
// ...sometimes we need a looser match if the HTML has been mangled
|
|
|
|
// in a third-party editor e.g. LibreOffice (T232461)
|
|
|
|
// This selector would fail if the "cite_reference_link_prefix" message
|
|
|
|
// were ever modified, but currently it isn't.
|
|
|
|
'a[ href *= "#cite_note" ]': true
|
2019-09-04 16:14:38 +00:00
|
|
|
}, ve.init.mw.Target.static.importRules.external.htmlBlacklist.remove );
|
2019-02-27 19:54:55 +00:00
|
|
|
|
2015-12-11 14:57:49 +00:00
|
|
|
/**
|
|
|
|
* Type of integration. Used by ve.init.mw.trackSubscriber.js for event tracking.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @property {string}
|
|
|
|
* @inheritable
|
|
|
|
*/
|
|
|
|
ve.init.mw.Target.static.integrationType = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Type of platform. Used by ve.init.mw.trackSubscriber.js for event tracking.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @property {string}
|
|
|
|
* @inheritable
|
|
|
|
*/
|
|
|
|
ve.init.mw.Target.static.platformType = null;
|
|
|
|
|
2021-04-26 15:34:38 +00:00
|
|
|
/**
|
|
|
|
* Enable conversion of formatted text to wikitext in source mode
|
|
|
|
*
|
|
|
|
* Unstable, temporary flag.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @property {boolean}
|
|
|
|
* @inheritable
|
|
|
|
*/
|
|
|
|
ve.init.mw.Target.static.convertToWikitextOnPaste = true;
|
|
|
|
|
2015-12-11 14:57:49 +00:00
|
|
|
/* Static Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fix the base URL from Parsoid if necessary.
|
|
|
|
*
|
|
|
|
* Absolutizes the base URL if it's relative, and sets a base URL based on wgArticlePath
|
|
|
|
* if there was no base URL at all.
|
|
|
|
*
|
|
|
|
* @param {HTMLDocument} doc Parsoid document
|
|
|
|
*/
|
|
|
|
ve.init.mw.Target.static.fixBase = function ( doc ) {
|
|
|
|
ve.fixBase( doc, document, ve.resolveUrl(
|
|
|
|
// Don't replace $1 with the page name, because that'll break if
|
|
|
|
// the page name contains a slash
|
|
|
|
mw.config.get( 'wgArticlePath' ).replace( '$1', '' ),
|
|
|
|
document
|
|
|
|
) );
|
|
|
|
};
|
|
|
|
|
2017-06-08 13:35:31 +00:00
|
|
|
/**
|
2018-09-06 12:11:16 +00:00
|
|
|
* @inheritdoc
|
2017-06-08 13:35:31 +00:00
|
|
|
*/
|
2018-09-06 12:11:16 +00:00
|
|
|
ve.init.mw.Target.static.createModelFromDom = function ( doc, mode, options ) {
|
|
|
|
var conf = mw.config.get( 'wgVisualEditor' );
|
2017-06-08 13:35:31 +00:00
|
|
|
|
2018-09-06 12:11:16 +00:00
|
|
|
options = ve.extendObject( {
|
|
|
|
lang: conf.pageLanguageCode,
|
|
|
|
dir: conf.pageLanguageDir
|
|
|
|
}, options );
|
|
|
|
|
|
|
|
// Parent method
|
|
|
|
return ve.init.mw.Target.super.static.createModelFromDom.call( this, doc, mode, options );
|
2017-06-08 13:35:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Deprecated alias
|
|
|
|
ve.init.mw.Target.prototype.createModelFromDom = function () {
|
|
|
|
return this.constructor.static.createModelFromDom.apply( this.constructor.static, arguments );
|
|
|
|
};
|
|
|
|
|
2015-12-11 14:57:49 +00:00
|
|
|
/**
|
2016-11-30 18:41:50 +00:00
|
|
|
* @inheritdoc
|
2019-04-16 15:17:29 +00:00
|
|
|
* @param {string} documentString
|
|
|
|
* @param {string} mode
|
2020-03-31 20:04:30 +00:00
|
|
|
* @param {string|null} [section] Section. Use null to unwrap all sections.
|
2019-02-13 13:21:26 +00:00
|
|
|
* @param {boolean} [onlySection] Only return the requested section, otherwise returns the
|
|
|
|
* whole document with just the requested section still wrapped (visual mode only).
|
2019-11-01 15:45:27 +00:00
|
|
|
* @return {HTMLDocument|string} HTML document, or document string (source mode)
|
2015-12-11 14:57:49 +00:00
|
|
|
*/
|
2019-02-13 13:21:26 +00:00
|
|
|
ve.init.mw.Target.static.parseDocument = function ( documentString, mode, section, onlySection ) {
|
2021-10-13 12:57:45 +00:00
|
|
|
var doc;
|
2016-11-30 12:03:56 +00:00
|
|
|
if ( mode === 'source' ) {
|
2016-11-30 18:41:50 +00:00
|
|
|
// Parent method
|
2017-06-21 00:51:51 +00:00
|
|
|
doc = ve.init.mw.Target.super.static.parseDocument.call( this, documentString, mode );
|
2016-11-14 16:07:13 +00:00
|
|
|
} else {
|
2016-11-30 18:41:50 +00:00
|
|
|
// Parsoid documents are XHTML so we can use parseXhtml which fixed some IE issues.
|
2016-11-14 16:07:13 +00:00
|
|
|
doc = ve.parseXhtml( documentString );
|
2018-03-12 12:24:18 +00:00
|
|
|
if ( section !== undefined ) {
|
2019-02-13 13:21:26 +00:00
|
|
|
if ( onlySection ) {
|
2021-10-13 12:57:45 +00:00
|
|
|
var sectionNode = doc.body.querySelector( '[data-mw-section-id="' + section + '"]' );
|
2019-02-13 13:21:26 +00:00
|
|
|
doc.body.innerHTML = '';
|
|
|
|
if ( sectionNode ) {
|
|
|
|
doc.body.appendChild( sectionNode );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Strip Parsoid sections
|
2020-03-20 17:08:24 +00:00
|
|
|
mw.libs.ve.unwrapParsoidSections( doc.body, section );
|
2018-03-12 12:24:18 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-06 20:24:49 +00:00
|
|
|
// Strip legacy IDs, for example in section headings
|
2020-03-20 17:08:24 +00:00
|
|
|
mw.libs.ve.stripParsoidFallbackIds( doc.body );
|
2018-09-06 12:11:16 +00:00
|
|
|
// Fix relative or missing base URL if needed
|
|
|
|
this.fixBase( doc );
|
2016-11-14 16:07:13 +00:00
|
|
|
}
|
2016-12-19 21:08:58 +00:00
|
|
|
|
2015-12-11 14:57:49 +00:00
|
|
|
return doc;
|
|
|
|
};
|
|
|
|
|
2017-06-08 13:43:09 +00:00
|
|
|
/* Methods */
|
|
|
|
|
2015-12-11 14:57:49 +00:00
|
|
|
/**
|
|
|
|
* Handle both DOM and modules being loaded and ready.
|
|
|
|
*
|
2018-09-06 12:11:16 +00:00
|
|
|
* @param {HTMLDocument|string} doc HTML document or source text
|
2015-12-11 14:57:49 +00:00
|
|
|
*/
|
|
|
|
ve.init.mw.Target.prototype.documentReady = function ( doc ) {
|
2018-08-25 12:59:24 +00:00
|
|
|
this.setupSurface( doc );
|
2015-12-11 14:57:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-03-22 20:35:42 +00:00
|
|
|
* Once surface is ready, initialize the UI
|
2015-12-11 14:57:49 +00:00
|
|
|
*
|
|
|
|
* @fires surfaceReady
|
|
|
|
*/
|
|
|
|
ve.init.mw.Target.prototype.surfaceReady = function () {
|
|
|
|
this.emit( 'surfaceReady' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2019-11-01 15:45:27 +00:00
|
|
|
* @deprecated Moved to mw.libs.ve.targetSaver.getHtml
|
|
|
|
* @param {HTMLDocument} newDoc
|
|
|
|
* @param {HTMLDocument} [oldDoc]
|
|
|
|
* @return {string}
|
2015-12-11 14:57:49 +00:00
|
|
|
*/
|
|
|
|
ve.init.mw.Target.prototype.getHtml = function ( newDoc, oldDoc ) {
|
2019-11-01 15:45:27 +00:00
|
|
|
OO.ui.warnDeprecation( 've.init.mw.Target#getHtml is deprecated. Use mw.libs.ve.targetSaver.getHtml.' );
|
|
|
|
return mw.libs.ve.targetSaver.getHtml( newDoc, oldDoc );
|
2015-12-11 14:57:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Track an event
|
|
|
|
*
|
|
|
|
* @param {string} name Event name
|
|
|
|
*/
|
|
|
|
ve.init.mw.Target.prototype.track = function () {};
|
|
|
|
|
2021-05-17 21:39:22 +00:00
|
|
|
/**
|
|
|
|
* Get a list of CSS classes to be added to surfaces, and target widget surfaces
|
|
|
|
*
|
|
|
|
* @return {string[]} CSS classes
|
|
|
|
*/
|
|
|
|
ve.init.mw.Target.prototype.getSurfaceClasses = function () {
|
|
|
|
return this.surfaceClasses;
|
|
|
|
};
|
|
|
|
|
2016-03-02 15:22:57 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2016-11-20 22:53:34 +00:00
|
|
|
ve.init.mw.Target.prototype.createTargetWidget = function ( config ) {
|
2019-10-09 13:55:40 +00:00
|
|
|
return new ve.ui.MWTargetWidget( ve.extendObject( {
|
2016-11-14 16:07:13 +00:00
|
|
|
// Reset to visual mode for target widgets
|
2019-10-09 13:55:40 +00:00
|
|
|
modes: [ 'visual' ],
|
2020-03-11 14:20:03 +00:00
|
|
|
defaultMode: 'visual',
|
2021-05-17 21:39:22 +00:00
|
|
|
toolbarGroups: this.toolbarGroups,
|
|
|
|
surfaceClasses: this.getSurfaceClasses()
|
2019-10-09 13:55:40 +00:00
|
|
|
}, config ) );
|
2016-03-02 15:22:57 +00:00
|
|
|
};
|
|
|
|
|
2015-12-11 14:57:49 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2016-11-14 16:07:13 +00:00
|
|
|
ve.init.mw.Target.prototype.createSurface = function ( dmDoc, config ) {
|
2016-11-30 12:03:56 +00:00
|
|
|
if ( config && config.mode === 'source' ) {
|
2021-10-13 12:57:45 +00:00
|
|
|
var importRules = ve.copy( this.constructor.static.importRules );
|
2016-11-14 16:07:13 +00:00
|
|
|
importRules.all = importRules.all || {};
|
|
|
|
// Preserve empty linebreaks on paste in source editor
|
|
|
|
importRules.all.keepEmptyContentBranches = true;
|
2016-11-30 12:03:56 +00:00
|
|
|
config = this.getSurfaceConfig( ve.extendObject( {}, config, {
|
2016-11-14 16:07:13 +00:00
|
|
|
importRules: importRules
|
2016-11-30 12:03:56 +00:00
|
|
|
} ) );
|
2017-01-17 22:00:30 +00:00
|
|
|
return new ve.ui.MWWikitextSurface( dmDoc, config );
|
2016-11-14 16:07:13 +00:00
|
|
|
}
|
2015-12-11 14:57:49 +00:00
|
|
|
|
2018-05-05 15:42:34 +00:00
|
|
|
return new ve.ui.MWSurface( dmDoc, this.getSurfaceConfig( config ) );
|
2017-10-10 20:14:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.init.mw.Target.prototype.getSurfaceConfig = function ( config ) {
|
|
|
|
// If we're not asking for a specific mode's config, use the default mode.
|
|
|
|
config = ve.extendObject( { mode: this.defaultMode }, config );
|
2021-05-17 21:39:22 +00:00
|
|
|
// eslint-disable-next-line mediawiki/class-doc
|
2017-10-10 20:14:25 +00:00
|
|
|
return ve.init.mw.Target.super.prototype.getSurfaceConfig.call( this, ve.extendObject( {
|
|
|
|
// Provide the wikitext versions of the registries, if we're using source mode
|
|
|
|
commandRegistry: config.mode === 'source' ? ve.ui.wikitextCommandRegistry : ve.ui.commandRegistry,
|
|
|
|
sequenceRegistry: config.mode === 'source' ? ve.ui.wikitextSequenceRegistry : ve.ui.sequenceRegistry,
|
2021-05-17 21:39:22 +00:00
|
|
|
dataTransferHandlerFactory: config.mode === 'source' ? ve.ui.wikitextDataTransferHandlerFactory : ve.ui.dataTransferHandlerFactory,
|
|
|
|
classes: this.getSurfaceClasses()
|
2017-10-10 20:14:25 +00:00
|
|
|
}, config ) );
|
2015-12-11 14:57:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Switch to editing mode.
|
|
|
|
*
|
2018-09-06 12:11:16 +00:00
|
|
|
* @param {HTMLDocument|string} doc HTML document or source text
|
2015-12-11 14:57:49 +00:00
|
|
|
*/
|
2018-08-25 12:59:24 +00:00
|
|
|
ve.init.mw.Target.prototype.setupSurface = function ( doc ) {
|
2015-12-11 14:57:49 +00:00
|
|
|
var target = this;
|
|
|
|
setTimeout( function () {
|
|
|
|
// Build model
|
|
|
|
target.track( 'trace.convertModelFromDom.enter' );
|
2021-10-13 12:57:45 +00:00
|
|
|
var dmDoc = target.constructor.static.createModelFromDom( doc, target.getDefaultMode() );
|
2015-12-11 14:57:49 +00:00
|
|
|
target.track( 'trace.convertModelFromDom.exit' );
|
|
|
|
|
|
|
|
// Build DM tree now (otherwise it gets lazily built when building the CE tree)
|
|
|
|
target.track( 'trace.buildModelTree.enter' );
|
|
|
|
dmDoc.buildNodeTree();
|
|
|
|
target.track( 'trace.buildModelTree.exit' );
|
|
|
|
|
|
|
|
setTimeout( function () {
|
2018-08-31 12:16:21 +00:00
|
|
|
target.addSurface( dmDoc );
|
2015-12-11 14:57:49 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
};
|
2016-05-26 10:56:08 +00:00
|
|
|
|
2018-08-25 12:59:24 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.init.mw.Target.prototype.addSurface = function () {
|
2021-10-13 12:57:45 +00:00
|
|
|
var target = this;
|
2018-08-25 12:59:24 +00:00
|
|
|
|
|
|
|
// Clear dummy surfaces
|
|
|
|
// TODO: Move to DesktopArticleTarget
|
|
|
|
this.clearSurfaces();
|
|
|
|
|
|
|
|
// Create ui.Surface (also creates ce.Surface and dm.Surface and builds CE tree)
|
|
|
|
this.track( 'trace.createSurface.enter' );
|
|
|
|
// Parent method
|
2021-10-13 12:57:45 +00:00
|
|
|
var surface = ve.init.mw.Target.super.prototype.addSurface.apply( this, arguments );
|
2018-08-25 12:59:24 +00:00
|
|
|
// Add classes specific to surfaces attached directly to the target,
|
|
|
|
// as opposed to TargetWidget surfaces
|
2021-06-04 15:26:01 +00:00
|
|
|
if ( !surface.inTargetWidget ) {
|
|
|
|
surface.$element.addClass( 've-init-mw-target-surface' );
|
|
|
|
}
|
2018-08-25 12:59:24 +00:00
|
|
|
this.track( 'trace.createSurface.exit' );
|
|
|
|
|
|
|
|
this.setSurface( surface );
|
|
|
|
|
|
|
|
setTimeout( function () {
|
|
|
|
// Initialize surface
|
|
|
|
target.track( 'trace.initializeSurface.enter' );
|
|
|
|
|
|
|
|
target.active = true;
|
|
|
|
// Now that the surface is attached to the document and ready,
|
|
|
|
// let it initialize itself
|
|
|
|
surface.initialize();
|
|
|
|
|
|
|
|
target.track( 'trace.initializeSurface.exit' );
|
|
|
|
target.surfaceReady();
|
|
|
|
} );
|
|
|
|
|
|
|
|
return surface;
|
|
|
|
};
|
|
|
|
|
2016-05-26 10:56:08 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.init.mw.Target.prototype.setSurface = function ( surface ) {
|
|
|
|
if ( !surface.$element.parent().length ) {
|
|
|
|
this.$element.append( surface.$element );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parent method
|
|
|
|
ve.init.mw.Target.super.prototype.setSurface.apply( this, arguments );
|
|
|
|
};
|
2017-05-20 21:24:10 +00:00
|
|
|
|
2019-04-17 16:28:48 +00:00
|
|
|
/**
|
|
|
|
* Intiailise autosave, recovering changes if applicable
|
|
|
|
*/
|
|
|
|
ve.init.mw.Target.prototype.initAutosave = function () {
|
|
|
|
var target = this,
|
|
|
|
surfaceModel = this.getSurface().getModel();
|
|
|
|
if ( this.recovered ) {
|
|
|
|
// Restore auto-saved transactions if document state was recovered
|
|
|
|
try {
|
|
|
|
surfaceModel.restoreChanges();
|
|
|
|
ve.init.platform.notify(
|
|
|
|
ve.msg( 'visualeditor-autosave-recovered-text' ),
|
|
|
|
ve.msg( 'visualeditor-autosave-recovered-title' )
|
|
|
|
);
|
|
|
|
} catch ( e ) {
|
|
|
|
mw.log.warn( e );
|
|
|
|
ve.init.platform.notify(
|
|
|
|
ve.msg( 'visualeditor-autosave-not-recovered-text' ),
|
|
|
|
ve.msg( 'visualeditor-autosave-not-recovered-title' ),
|
|
|
|
{ type: 'error' }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// ...otherwise store this document state for later recovery
|
|
|
|
if ( this.fromEditedState ) {
|
|
|
|
// Store immediately if the document was previously edited
|
|
|
|
// (e.g. in a different mode)
|
|
|
|
this.storeDocState( this.originalHtml );
|
|
|
|
} else {
|
|
|
|
// Only store after the first change if this is an unmodified document
|
|
|
|
surfaceModel.once( 'undoStackChange', function () {
|
|
|
|
// Check the surface hasn't been destroyed
|
|
|
|
if ( target.getSurface() ) {
|
|
|
|
target.storeDocState( target.originalHtml );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Start auto-saving transactions
|
|
|
|
surfaceModel.startStoringChanges();
|
|
|
|
// TODO: Listen to autosaveFailed event to notify user
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a snapshot of the current document state.
|
|
|
|
*
|
|
|
|
* @param {string} [html] Document HTML, will generate from current state if not provided
|
|
|
|
*/
|
|
|
|
ve.init.mw.Target.prototype.storeDocState = function ( html ) {
|
|
|
|
var mode = this.getSurface().getMode();
|
|
|
|
this.getSurface().getModel().storeDocState( { mode: mode }, html );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear any stored document state
|
|
|
|
*/
|
|
|
|
ve.init.mw.Target.prototype.clearDocState = function () {
|
|
|
|
if ( this.getSurface() ) {
|
|
|
|
this.getSurface().getModel().removeDocStateAndChanges();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.init.mw.Target.prototype.teardown = function () {
|
|
|
|
// If target is closed cleanly (after save or deliberate close) then remove autosave state
|
|
|
|
this.clearDocState();
|
|
|
|
|
|
|
|
// Parent method
|
|
|
|
return ve.init.mw.Target.super.prototype.teardown.call( this );
|
|
|
|
};
|
|
|
|
|
2017-05-20 21:24:10 +00:00
|
|
|
/**
|
|
|
|
* Refresh our stored edit/csrf token
|
|
|
|
*
|
|
|
|
* This should be called in response to a badtoken error, to resolve whether the
|
|
|
|
* token was expired / the user changed. If the user did change, this updates
|
|
|
|
* the current user.
|
|
|
|
*
|
2018-05-04 13:30:10 +00:00
|
|
|
* @param {ve.dm.Document} [doc] Document to associate with the API request
|
2020-02-15 01:22:39 +00:00
|
|
|
* @return {jQuery.Promise} Promise resolved with new username, or null if anonymous
|
2017-05-20 21:24:10 +00:00
|
|
|
*/
|
2020-02-15 01:22:39 +00:00
|
|
|
ve.init.mw.Target.prototype.refreshUser = function ( doc ) {
|
2020-02-15 02:50:16 +00:00
|
|
|
return this.getContentApi( doc ).get( {
|
2017-05-20 21:24:10 +00:00
|
|
|
action: 'query',
|
2020-02-15 01:22:39 +00:00
|
|
|
meta: 'userinfo'
|
2020-02-15 02:50:16 +00:00
|
|
|
} ).then( function ( data ) {
|
|
|
|
var userInfo = data.query && data.query.userinfo;
|
|
|
|
|
|
|
|
if ( !userInfo ) {
|
|
|
|
return ve.createDeferred().reject();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( userInfo.anon !== undefined ) {
|
|
|
|
// New session is an anonymous user
|
|
|
|
mw.config.set( {
|
|
|
|
// wgUserId is unset for anonymous users, not set to null
|
|
|
|
wgUserId: undefined,
|
|
|
|
// wgUserName is explicitly set to null for anonymous users,
|
|
|
|
// functions like mw.user.isAnon rely on this.
|
|
|
|
wgUserName: null
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
// New session is a logged in user
|
|
|
|
mw.config.set( {
|
|
|
|
wgUserId: userInfo.id,
|
|
|
|
wgUserName: userInfo.name
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
return mw.user.getName();
|
|
|
|
} );
|
2017-05-20 21:24:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a wikitext fragment from a document
|
|
|
|
*
|
2021-06-04 11:58:18 +00:00
|
|
|
* @param {ve.dm.Document} doc
|
2020-06-19 12:02:51 +00:00
|
|
|
* @param {boolean} [useRevision=true] Whether to use the revision ID + ETag
|
2017-05-20 21:24:10 +00:00
|
|
|
* @return {jQuery.Promise} Abortable promise which resolves with a wikitext string
|
|
|
|
*/
|
2020-06-19 12:02:51 +00:00
|
|
|
ve.init.mw.Target.prototype.getWikitextFragment = function ( doc, useRevision ) {
|
2020-04-29 16:32:09 +00:00
|
|
|
// Shortcut for empty document
|
|
|
|
if ( !doc.data.hasContent() ) {
|
2020-06-19 12:02:51 +00:00
|
|
|
return ve.createDeferred().resolve( '' );
|
2017-05-20 21:24:10 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 12:57:45 +00:00
|
|
|
var params = {
|
2020-04-29 16:32:09 +00:00
|
|
|
action: 'visualeditoredit',
|
|
|
|
paction: 'serialize',
|
2020-06-19 12:02:51 +00:00
|
|
|
html: ve.dm.converter.getDomFromModel( doc ).body.innerHTML,
|
|
|
|
page: this.getPageName()
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( useRevision === undefined || useRevision ) {
|
|
|
|
params.oldid = this.revid;
|
|
|
|
params.etag = this.etag;
|
|
|
|
}
|
2017-05-20 21:24:10 +00:00
|
|
|
|
2021-10-13 12:57:45 +00:00
|
|
|
var xhr = this.getContentApi( doc ).postWithToken( 'csrf',
|
2017-05-20 21:24:10 +00:00
|
|
|
params,
|
|
|
|
{ contentType: 'multipart/form-data' }
|
|
|
|
);
|
|
|
|
|
2020-02-15 02:50:16 +00:00
|
|
|
return xhr.then( function ( response ) {
|
2017-05-20 21:24:10 +00:00
|
|
|
if ( response.visualeditoredit ) {
|
|
|
|
return response.visualeditoredit.content;
|
|
|
|
}
|
2020-06-19 12:02:51 +00:00
|
|
|
return ve.createDeferred().reject();
|
2020-02-15 02:50:16 +00:00
|
|
|
} ).promise( { abort: xhr.abort } );
|
2017-05-20 21:24:10 +00:00
|
|
|
};
|
2018-04-06 11:30:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a fragment of wikitext into HTML
|
|
|
|
*
|
2021-06-04 11:58:18 +00:00
|
|
|
* @param {string} wikitext
|
2018-04-06 11:30:57 +00:00
|
|
|
* @param {boolean} pst Perform pre-save transform
|
2018-05-04 13:30:10 +00:00
|
|
|
* @param {ve.dm.Document} [doc] Parse for a specific document, defaults to current surface's
|
2018-04-06 11:30:57 +00:00
|
|
|
* @return {jQuery.Promise} Abortable promise
|
|
|
|
*/
|
2018-05-04 13:30:10 +00:00
|
|
|
ve.init.mw.Target.prototype.parseWikitextFragment = function ( wikitext, pst, doc ) {
|
|
|
|
return this.getContentApi( doc ).post( {
|
2018-04-06 11:30:57 +00:00
|
|
|
action: 'visualeditor',
|
|
|
|
paction: 'parsefragment',
|
2018-05-04 13:30:10 +00:00
|
|
|
page: this.getPageName( doc ),
|
2018-04-06 11:30:57 +00:00
|
|
|
wikitext: wikitext,
|
|
|
|
pst: pst
|
|
|
|
} );
|
|
|
|
};
|
2018-05-04 13:30:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the page name associated with a specific document
|
|
|
|
*
|
|
|
|
* @param {ve.dm.Document} [doc] Document, defaults to current surface's
|
|
|
|
* @return {string} Page name
|
|
|
|
*/
|
|
|
|
ve.init.mw.Target.prototype.getPageName = function () {
|
|
|
|
return this.pageName;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an API object associated with the wiki where the document
|
|
|
|
* content is hosted.
|
|
|
|
*
|
|
|
|
* This would be overridden if editing content on another wiki.
|
|
|
|
*
|
|
|
|
* @param {ve.dm.Document} [doc] API for a specific document, should default to document of current surface.
|
|
|
|
* @param {Object} [options] API options
|
2021-07-07 10:09:22 +00:00
|
|
|
* @param {Object} [options.parameters] Default query parameters for all API requests. Defaults
|
|
|
|
* include action=query, format=json, and formatversion=2 if not specified otherwise.
|
2021-06-04 11:58:18 +00:00
|
|
|
* @return {mw.Api}
|
2018-05-04 13:30:10 +00:00
|
|
|
*/
|
|
|
|
ve.init.mw.Target.prototype.getContentApi = function ( doc, options ) {
|
2019-04-30 18:30:50 +00:00
|
|
|
options = options || {};
|
|
|
|
options.parameters = ve.extendObject( { formatversion: 2 }, options.parameters );
|
2018-05-04 13:30:10 +00:00
|
|
|
return new mw.Api( options );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an API object associated with the local wiki.
|
|
|
|
*
|
|
|
|
* For example you would always use getLocalApi for actions
|
|
|
|
* associated with the current user.
|
|
|
|
*
|
|
|
|
* @param {Object} [options] API options
|
2021-06-04 11:58:18 +00:00
|
|
|
* @return {mw.Api}
|
2018-05-04 13:30:10 +00:00
|
|
|
*/
|
|
|
|
ve.init.mw.Target.prototype.getLocalApi = function ( options ) {
|
2019-04-30 18:30:50 +00:00
|
|
|
options = options || {};
|
|
|
|
options.parameters = ve.extendObject( { formatversion: 2 }, options.parameters );
|
2018-05-04 13:30:10 +00:00
|
|
|
return new mw.Api( options );
|
|
|
|
};
|