From 6f2c829bd94a3c83886c0628d242f43239916438 Mon Sep 17 00:00:00 2001 From: David Lynch Date: Wed, 13 Dec 2017 11:55:18 -0600 Subject: [PATCH] Add section id to URI fragment on save Bug: T181075 Change-Id: Iea63112301bfa0860c3d68efc35648a5af7e4ae6 --- .../ve.init.mw.DesktopArticleTarget.js | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.js b/modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.js index 108127ec32..45f282b271 100644 --- a/modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.js +++ b/modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.js @@ -1292,7 +1292,7 @@ ve.init.mw.DesktopArticleTarget.prototype.updateHistoryState = function () { * Page modifications for switching back to view mode. */ ve.init.mw.DesktopArticleTarget.prototype.restorePage = function () { - var uri, keys; + var uri, keys, section, $section; // Skins like monobook don't have a tab for view mode and instead just have the namespace tab // selected. We didn't deselect the namespace tab, so we're ready after deselecting #ca-ve-edit. @@ -1316,6 +1316,24 @@ ve.init.mw.DesktopArticleTarget.prototype.restorePage = function () { delete uri.query.veaction; } if ( 'section' in uri.query ) { + // Translate into a fragment for the new URI: + // This should be after replacePageContent if this is post-save, so we can just look + // at the headers on the page. + section = uri.query.section.toString().indexOf( 'T-' ) === 0 ? +uri.query.section.slice( 2 ) : uri.query.section; + $section = this.$editableContent.find( 'h1, h2, h3, h4, h5, h6' ) + // Ignore headings inside TOC + .filter( function () { + return $( this ).closest( '#toc' ).length === 0; + } ); + if ( section === 'new' ) { + // A new section is appended to the end, so take the last one. + section = $section.length; + } + $section = $section.eq( section - 1 ).find( '.mw-headline' ); + + if ( $section.length && $section.attr( 'id' ) ) { + uri.fragment = $section.attr( 'id' ); + } delete uri.query.section; } if ( 'action' in uri.query && $( '#wpTextbox1' ).length === 0 ) {