Enable transformations in preview mode

We remove the [reply] and [subscribe] links when they should not be
visible (controlled by 'enableSectionEditLinks' option, which is
disabled when previewing).

Bug: T309423
Change-Id: Ie0d3fba2c4d166daac3ea2e117a246c9584284ca
This commit is contained in:
Bartosz Dziewoński 2022-06-01 23:43:42 +02:00 committed by Ed Sanders
parent caefb1ae39
commit 2bc76dabd7
6 changed files with 55 additions and 4 deletions

View file

@ -475,6 +475,7 @@
"MinervaNeueTalkPageOverlay": "\\MediaWiki\\Extension\\DiscussionTools\\Hooks\\MobileHooks::onMinervaNeueTalkPageOverlay", "MinervaNeueTalkPageOverlay": "\\MediaWiki\\Extension\\DiscussionTools\\Hooks\\MobileHooks::onMinervaNeueTalkPageOverlay",
"LoadExtensionSchemaUpdates": "installer", "LoadExtensionSchemaUpdates": "installer",
"ParserAfterTidy": "parser", "ParserAfterTidy": "parser",
"ParserOutputPostCacheTransform": "parser",
"ArticleViewHeader": "page", "ArticleViewHeader": "page",
"BeforeDisplayNoArticleText": "page", "BeforeDisplayNoArticleText": "page",
"BeforePageDisplay": "page", "BeforePageDisplay": "page",

View file

@ -278,6 +278,29 @@ class CommentFormatter {
return XMLSerializer::serialize( $container, [ 'innerXML' => true, 'smartQuote' => false ] )['html']; return XMLSerializer::serialize( $container, [ 'innerXML' => true, 'smartQuote' => false ] )['html'];
} }
/**
* Replace placeholders for all interactive tools with nothing. This is intended for cases where
* interaction is unexpected, e.g. reply links while previewing an edit.
*
* @param string $text
* @return string
*/
public static function removeInteractiveTools( string $text ) {
$text = strtr( $text, [
'<!--__DTREPLY__-->' => '',
'<!--__DTREPLYBRACKETOPEN__-->' => '',
'<!--__DTREPLYBRACKETCLOSE__-->' => '',
'<!--__DTELLIPSISBUTTON__-->' => '',
'<!--__DTEMPTYTALKPAGE__-->' => '',
] );
$text = preg_replace( '/<!--__DTSUBSCRIBE__(.*?)-->/', '', $text );
$text = preg_replace( '/<!--__DTSUBSCRIBELINK__(.*?)-->/', '', $text );
$text = preg_replace( '/<!--__DTSUBSCRIBEBUTTON__(.*?)-->/', '', $text );
return $text;
}
/** /**
* Replace placeholders for topic subscription buttons with the real thing. * Replace placeholders for topic subscription buttons with the real thing.
* *

View file

@ -229,8 +229,8 @@ class HookUtils {
* @return bool * @return bool
*/ */
public static function isFeatureEnabledForOutput( OutputPage $output, ?string $feature = null ): bool { public static function isFeatureEnabledForOutput( OutputPage $output, ?string $feature = null ): bool {
// Don't show on edit pages, history, etc. // Only show on normal page views (not history etc.), and in edit mode for previews
if ( $feature !== static::NEWTOPICTOOL && $output->getActionName() !== 'view' ) { if ( !in_array( $output->getActionName(), [ 'view', 'edit', 'submit' ] ) ) {
return false; return false;
} }

View file

@ -12,9 +12,14 @@ namespace MediaWiki\Extension\DiscussionTools\Hooks;
use ConfigFactory; use ConfigFactory;
use MediaWiki\Extension\DiscussionTools\CommentFormatter; use MediaWiki\Extension\DiscussionTools\CommentFormatter;
use MediaWiki\Hook\ParserAfterTidyHook; use MediaWiki\Hook\ParserAfterTidyHook;
use MediaWiki\Hook\ParserOutputPostCacheTransformHook;
use Parser; use Parser;
use ParserOutput;
class ParserHooks implements ParserAfterTidyHook { class ParserHooks implements
ParserAfterTidyHook,
ParserOutputPostCacheTransformHook
{
/** @var ConfigFactory */ /** @var ConfigFactory */
private $configFactory; private $configFactory;
@ -34,7 +39,7 @@ class ParserHooks implements ParserAfterTidyHook {
* @param string &$text * @param string &$text
*/ */
public function onParserAfterTidy( $parser, &$text ) { public function onParserAfterTidy( $parser, &$text ) {
if ( $parser->getOptions()->getInterfaceMessage() || $parser->getOptions()->getIsPreview() ) { if ( $parser->getOptions()->getInterfaceMessage() ) {
return; return;
} }
@ -70,4 +75,17 @@ class ParserHooks implements ParserAfterTidyHook {
] ); ] );
} }
} }
/**
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserOutputPostCacheTransform
*
* @param ParserOutput $parserOutput
* @param string &$text
* @param array &$options
*/
public function onParserOutputPostCacheTransform( $parserOutput, &$text, &$options ): void {
if ( !$options['enableSectionEditLinks'] ) {
$text = CommentFormatter::removeInteractiveTools( $text );
}
}
} }

View file

@ -133,6 +133,10 @@ ReplyLinksController.prototype.onAnyLinkClick = function ( e ) {
}; };
ReplyLinksController.prototype.isActivationEvent = function ( e ) { ReplyLinksController.prototype.isActivationEvent = function ( e ) {
if ( mw.config.get( 'wgAction' ) !== 'view' ) {
// Don't do anything when we're editing/previewing
return false;
}
if ( e.type === 'keypress' && e.which !== OO.ui.Keys.ENTER && e.which !== OO.ui.Keys.SPACE ) { if ( e.type === 'keypress' && e.which !== OO.ui.Keys.ENTER && e.which !== OO.ui.Keys.SPACE ) {
// Only handle keypresses on the "Enter" or "Space" keys // Only handle keypresses on the "Enter" or "Space" keys
return false; return false;

View file

@ -240,6 +240,11 @@
padding-top: 0; padding-top: 0;
margin-top: 0.25em; margin-top: 0.25em;
} }
.ext-discussiontools-init-section-bar {
// Looks just a little weird to display this in preview of your own new topic (T309423)
display: none;
}
} }
&-bodyWrapper { &-bodyWrapper {