Store importTitle in document, and display in sub heading

Change-Id: I431b721d8af3180b78a565e322a45745d06f67a1
This commit is contained in:
Ed Sanders 2018-08-12 16:13:00 +01:00
parent 68f83cab37
commit 5be3be4ba4
4 changed files with 50 additions and 5 deletions

View file

@ -520,7 +520,8 @@
],
"messages" : [
"collabpad",
"collabpad-doctitle"
"collabpad-doctitle",
"collabpad-import-subtitle"
],
"targets": [
"desktop",

View file

@ -63,6 +63,7 @@
"apihelp-visualeditoredit-param-wikitext": "The wikitext to act with.",
"apihelp-visualeditoredit-summary": "Save an HTML5 page to MediaWiki (converted to wikitext via the Parsoid service).",
"collabpad-doctitle": "CollabPad: $1",
"collabpad-import-subtitle": "Imported from $1",
"collabpad": "CollabPad",
"tooltip-ca-createsource": "Create the source code of this page",
"tooltip-ca-edit": "Edit this page using wikitext",

View file

@ -77,6 +77,7 @@
"apihelp-visualeditoredit-param-wikitext": "{{doc-apihelp-param|visualeditoredit|wikitext}}",
"apihelp-visualeditoredit-summary": "{{doc-apihelp-summary|visualeditoredit}}",
"collabpad-doctitle": "Name of CollabPad special page with a document subtitle",
"collabpad-import-subtitle": "Subtitle of CollabPad page, linking to the page the content was imported from",
"collabpad": "Name of CollabPad special page",
"tooltip-ca-createsource": "Tooltip of the {{msg-mw|Visualeditor-ca-createsource}} tab, used if the page does not exist.\n\nSee also:\n* {{msg-mw|Tooltip-ca-editsource}} - tooltip of the {{msg-mw|Visualeditor-ca-editsource}} tab, used if the page already exists",
"tooltip-ca-edit": "Over-ridden tooltip of the wikitext \"Edit source\" tab.",

View file

@ -27,7 +27,7 @@ ve.init.mw.CollabTarget = function VeInitMwCollabTarget( title, rebaserUrl, conf
this.title = title;
this.rebaserUrl = rebaserUrl;
this.importTitle = config.importTitle;
this.importTitle = config.importTitle || null;
// Parent constructor
ve.init.mw.CollabTarget.super.call( this, config );
@ -101,6 +101,7 @@ ve.init.mw.CollabTarget.prototype.transformPage = function () {
*/
ve.init.mw.CollabTarget.prototype.restorePage = function () {
this.$element.parent().append( this.$originalContent.children() );
$( '#contentSub' ).empty();
};
/**
@ -175,17 +176,25 @@ ve.init.mw.CollabTarget.prototype.setSurface = function ( surface ) {
);
synchronizer.once( 'initDoc', function () {
var initPromise;
var initPromise, title;
if ( target.importTitle && !surface.getModel().getDocument().getCompleteHistoryLength() ) {
initPromise = mw.libs.ve.targetLoader.requestParsoidData( target.importTitle.toString(), { targetName: 'collabpad' } ).then( function ( response ) {
var doc, dmDoc,
var doc, dmDoc, fragment,
content = ve.getProp( response, 'visualeditor', 'content' );
if ( content ) {
doc = target.constructor.static.parseDocument( content );
dmDoc = target.constructor.static.createModelFromDom( doc );
surface.getModel().getLinearFragment( new ve.Range( 0, 2 ) ).insertDocument( dmDoc );
fragment = surface.getModel().getLinearFragment( new ve.Range( 0, 2 ) );
fragment.insertDocument( dmDoc );
// Store the importTitle as a hidden meta item
if ( target.importTitle ) {
fragment.collapseToEnd().insertContent( [
{ type: 'alienMeta', attributes: { importTitle: target.importTitle.toString() } },
{ type: '/alienMeta' }
] );
}
surface.getModel().selectFirstContentOffset();
} else {
// Import failed
@ -205,6 +214,17 @@ ve.init.mw.CollabTarget.prototype.setSurface = function ( surface ) {
surface.getModel().selectFirstContentOffset();
// Resolve progress bar
importDeferred.resolve();
if ( ( title = target.getImportTitle() ) ) {
$( '#contentSub' ).html(
ve.htmlMsg(
'collabpad-import-subtitle',
$( '<a>' ).attr( 'href', title.getUrl() ).text( title.getMainText() )
)
);
ve.targetLinksToNewWindow( $( '#contentSub' )[ 0 ] );
} else {
$( '#contentSub' ).empty();
}
} );
} );
@ -215,6 +235,28 @@ ve.init.mw.CollabTarget.prototype.setSurface = function ( surface ) {
ve.init.mw.CollabTarget.super.prototype.setSurface.apply( this, arguments );
};
/**
* Get the title of the imported document, if there was one
*
* @return {mw.Title|null} Title of imported document
*/
ve.init.mw.CollabTarget.prototype.getImportTitle = function () {
var surfaceModel,
target = this;
if ( !this.importTitle ) {
surfaceModel = this.getSurface().getModel();
surfaceModel.getMetaList().getItemsInGroup( 'misc' ).some( function ( item ) {
var importTitle = item.getAttribute( 'importTitle' );
if ( importTitle ) {
target.importTitle = mw.Title.newFromText( importTitle );
return true;
}
} );
}
return this.importTitle;
};
/* Registration */
ve.init.mw.targetFactory.register( ve.init.mw.CollabTarget );