Keep TOC in correct place in document

Change-Id: I6ad8cd8cbd7ef902204408673eb096b7405abd24
This commit is contained in:
Ed Sanders 2016-07-11 21:48:02 +01:00
parent 1a0fd81f64
commit fd1adfb3b5
5 changed files with 29 additions and 11 deletions

View file

@ -1613,6 +1613,7 @@
"modules/ve-mw/ui/styles/widgets/ve.ui.MWCategoryItemWidget.css",
"modules/ve-mw/ui/styles/widgets/ve.ui.MWCategoryPopupWidget.css",
"modules/ve-mw/ui/styles/widgets/ve.ui.MWCategoryWidget.css",
"modules/ve-mw/ui/styles/widgets/ve.ui.MWTocWidget.css",
"modules/ve-mw/ui/styles/dialogs/ve.ui.MWMetaDialog.css"
],
"dependencies": [

View file

@ -760,7 +760,6 @@ ve.init.mw.DesktopArticleTarget.prototype.surfaceReady = function () {
// TODO: mwTocWidget should probably live in a ve.ui.MWSurface subclass
if ( mw.config.get( 'wgVisualEditorConfig' ).enableTocWidget ) {
surface.mwTocWidget = new ve.ui.MWTocWidget( this.getSurface() );
surface.$element.before( surface.mwTocWidget.$element );
}
this.transformCategoryLinks( $( '#catlinks' ) );

View file

@ -1982,7 +1982,13 @@ ve.init.mw.ArticleTarget.prototype.restoreEditSection = function () {
section = this.section.toString().indexOf( 'T-' ) === 0 ? +this.section.slice( 2 ) : this.section;
surfaceView = surface.getView();
$documentNode = surfaceView.getDocument().getDocumentNode().$element;
$section = $documentNode.find( 'h1, h2, h3, h4, h5, h6' ).eq( section - 1 );
// Find all headings including those inside templates, not just HeadingNodes
$section = $documentNode.find( 'h1, h2, h3, h4, h5, h6' )
// Ignore headings inside TOC
.filter( function () {
return $( this ).closest( '.ve-ui-mwTocWidget' ).length === 0;
} )
.eq( section - 1 );
headingNode = $section.data( 'view' );
if ( $section.length && new mw.Uri().query.summary === undefined ) {

View file

@ -0,0 +1,6 @@
.ve-ui-mwTocWidget {
cursor: default;
user-select: none;
-moz-user-select: none;
-webkit-user-select: none;
}

View file

@ -32,12 +32,12 @@ ve.ui.MWTocWidget = function VeUiMWTocWidget( surface, config ) {
this.mwTOCDisable = false;
this.$tocList = $( '<ul>' );
this.$element.addClass( 'toc ve-ui-mwTocWidget' ).append(
this.$element.addClass( 'toc ve-ui-mwTocWidget ve-ce-focusableNode' ).append(
$( '<div>' ).addClass( 'toctitle' ).append(
$( '<h2>' ).text( ve.msg( 'toc' ) )
),
this.$tocList
);
).prop( 'contentEditable', 'false' );
// Setup toggle link
mw.hook( 'wikipage.content' ).fire( this.$element );
@ -152,19 +152,22 @@ ve.ui.MWTocWidget.prototype.updateNode = function ( viewNode ) {
* Based on generateTOC in Linker.php
*/
ve.ui.MWTocWidget.prototype.build = function () {
var i, l, level, levelDiff, tocNumber, modelNode, viewNode,
var i, l, level, levelDiff, tocNumber, modelNode, viewNode, tocBeforeNode,
$list, $text, $item, $link,
$newTocList = $( '<ul>' ),
nodes = this.doc.getNodesByType( 'mwHeading', true ),
documentView = this.surface.getView().getDocument(),
surfaceView = this.surface.getView(),
documentView = surfaceView.getDocument(),
lastLevel = 0,
stack = [];
stack = [],
uri = new mw.Uri();
function getItemIndex( $list, n ) {
return $list.children( 'li' ).length + ( n === stack.length - 1 ? 1 : 0 );
}
function linkClickHandler( heading ) {
surfaceView.focus();
ve.init.target.goToHeading( heading );
return false;
}
@ -191,8 +194,10 @@ ve.ui.MWTocWidget.prototype.build = function () {
tocNumber = stack.map( getItemIndex ).join( '.' );
viewNode = documentView.getBranchNodeFromOffset( modelNode.getRange().start );
uri.query.section = ( i + 1 ).toString();
$item = $( '<li>' ).addClass( 'toclevel-' + stack.length ).addClass( 'tocsection-' + ( i + 1 ) );
$link = $( '<a href="#">' ).append( '<span class="tocnumber">' + tocNumber + '</span> ' );
$link = $( '<a>' ).attr( 'href', uri )
.append( '<span class="tocnumber">' + tocNumber + '</span> ' );
$text = $( '<span>' ).addClass( 'toctext' );
viewNode.$tocText = $text;
@ -204,11 +209,12 @@ ve.ui.MWTocWidget.prototype.build = function () {
lastLevel = level;
}
this.$tocList.replaceWith( $newTocList );
this.$tocList = $newTocList;
this.$tocList.empty().append( $newTocList.children() );
if ( nodes.length ) {
this.rootLength = stack[ 0 ].children().length;
this.rootLength = this.$tocList.children().length;
tocBeforeNode = documentView.getBranchNodeFromOffset( nodes[ 0 ].getRange().start );
tocBeforeNode.$element.before( this.$element );
} else {
this.rootLength = 0;
}