feat: add support for nested tabbers in Codex (#95)

* refactor: Apply some code cleanup

* feat: WIP dynamic nested tabber in codex

* feat: Make deeply nested tabbers work

* doc: fix comment position

---------

Co-authored-by: alistair3149 <alistair3149@users.noreply.github.com>
This commit is contained in:
H. C. Kruse 2023-07-12 02:06:57 +00:00 committed by GitHub
parent da7d95c0ae
commit 7f75899995
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 175 additions and 92 deletions

View file

@ -34,7 +34,8 @@
}, },
"scripts": { "scripts": {
"fix": [ "fix": [
"minus-x fix ." "minus-x fix .",
"phpcbf"
], ],
"test": [ "test": [
"parallel-lint . --exclude vendor --exclude node_modules", "parallel-lint . --exclude vendor --exclude node_modules",

View file

@ -25,7 +25,7 @@ class Hooks implements ParserFirstCallInitHook {
* *
* @param Parser $parser * @param Parser $parser
*/ */
public function onParserFirstCallInit( $parser ) { public function onParserFirstCallInit( $parser ): void {
$parser->setHook( 'tabber', Tabber::class . '::parserHook' ); $parser->setHook( 'tabber', Tabber::class . '::parserHook' );
$parser->setHook( 'tabbertransclude', TabberTransclude::class . '::parserHook' ); $parser->setHook( 'tabbertransclude', TabberTransclude::class . '::parserHook' );
} }

View file

@ -14,40 +14,53 @@ declare( strict_types=1 );
namespace MediaWiki\Extension\TabberNeue; namespace MediaWiki\Extension\TabberNeue;
use JsonException;
use MediaWiki\MediaWikiServices; use MediaWiki\MediaWikiServices;
use Parser; use Parser;
use PPFrame; use PPFrame;
class Tabber { class Tabber {
/**
* Critical rendering styles
* See ext.tabberNeue.inline.less
*
* @var string
*/
public static $criticalInlineStyle = '.client-js .tabber__header{height:2.6em;box-shadow:inset 0 -1px 0 0;opacity:.1}.client-js .tabber__header:after{position:absolute;width:16ch;height:.5em;border-radius:40px;margin-top:1em;margin-left:.75em;background:#000;content:""}.client-js .tabber__noscript,.client-js .tabber__panel:not( :first-child ){display:none}';
/**
* Flag that checks if this is a nested tabber
* @var bool
*/
private static $isNested = false;
private static $useCodex = false;
/** /**
* Parser callback for <tabber> tag * Parser callback for <tabber> tag
* *
* @param string $input * @param string|null $input
* @param array $args * @param array $args
* @param Parser $parser Mediawiki Parser Object * @param Parser $parser Mediawiki Parser Object
* @param PPFrame $frame Mediawiki PPFrame Object * @param PPFrame $frame Mediawiki PPFrame Object
* *
* @return string HTML * @return string HTML
*/ */
public static function parserHook( string $input, array $args, Parser $parser, PPFrame $frame ) { public static function parserHook( ?string $input, array $args, Parser $parser, PPFrame $frame ) {
$tabber = new Tabber(); self::$useCodex = MediaWikiServices::getInstance()->getMainConfig()->get( 'TabberNeueUseCodex' );
$html = $tabber->render( $input, $parser, $frame );
$html = self::render( $input, $parser, $frame );
if ( $input === null ) { if ( $input === null ) {
return; return '';
} }
$useCodex = MediaWikiServices::getInstance()->getMainConfig()->get( 'TabberNeueUseCodex' ); if ( self::$useCodex === true ) {
if ( $useCodex === true ) {
$parser->getOutput()->addModules( [ 'ext.tabberNeue.codex' ] ); $parser->getOutput()->addModules( [ 'ext.tabberNeue.codex' ] );
} else { } else {
// Critial rendering styles // Critical rendering styles
// See ext.tabberNeue.inline.less // See ext.tabberNeue.inline.less
$style = sprintf( $style = sprintf( '<style id="tabber-style">%s</style>', self::$criticalInlineStyle );
'<style id="tabber-style">%s</style>',
'.client-js .tabber__header{height:2.6em;box-shadow:inset 0 -1px 0 0;opacity:.1}.client-js .tabber__header:after{position:absolute;width:16ch;height:.5em;border-radius:40px;margin-top:1em;margin-left:.75em;background:#000;content:""}.client-js .tabber__noscript,.client-js .tabber__panel:not( :first-child ){display:none}'
);
$parser->getOutput()->addHeadItem( $style, true ); $parser->getOutput()->addHeadItem( $style, true );
$parser->getOutput()->addModules( [ 'ext.tabberNeue.legacy' ] ); $parser->getOutput()->addModules( [ 'ext.tabberNeue.legacy' ] );
} }
@ -65,18 +78,28 @@ class Tabber {
* *
* @return string HTML * @return string HTML
*/ */
public static function render( $input, Parser $parser, PPFrame $frame ) { public static function render( string $input, Parser $parser, PPFrame $frame ): string {
$arr = explode( "|-|", $input ); $arr = explode( '|-|', $input );
$htmlTabs = ''; $htmlTabs = '';
foreach ( $arr as $tab ) { foreach ( $arr as $tab ) {
$htmlTabs .= self::buildTab( $tab, $parser, $frame ); $htmlTabs .= self::buildTab( $tab, $parser, $frame );
} }
$html = '<div class="tabber">' . if ( self::$useCodex && self::$isNested ) {
$tab = rtrim( implode( '},', explode( '}', $htmlTabs ) ), ',' );
$tab = strip_tags( html_entity_decode( $tab ) );
$tab = str_replace( ',,', ',', $tab );
$tab = str_replace( ',]', ']', $tab );
return sprintf( '[%s]', $tab );
}
$htmlTabs = preg_replace( '/\\\n/', '', $htmlTabs );
$htmlTabs = preg_replace( '/\\\*/', '', $htmlTabs );
$htmlTabs = str_replace( [ '"[', ']"' ], [ '[', ']' ], $htmlTabs );
return '<div class="tabber">' .
'<header class="tabber__header"></header>' . '<header class="tabber__header"></header>' .
'<section class="tabber__section">' . $htmlTabs . '</section></div>'; '<section class="tabber__section">' . $htmlTabs . '</section></div>';
return $html;
} }
/** /**
@ -87,27 +110,47 @@ class Tabber {
* @param PPFrame $frame Mediawiki PPFrame Object * @param PPFrame $frame Mediawiki PPFrame Object
* *
* @return string HTML * @return string HTML
* @throws JsonException
*/ */
private static function buildTab( $tab, Parser $parser, PPFrame $frame ) { private static function buildTab( string $tab, Parser $parser, PPFrame $frame ): string {
if ( empty( trim( $tab ) ) ) { if ( empty( trim( $tab ) ) ) {
return ''; return '';
} }
// Use array_pad to make sure at least 2 array values are always returned // Use array_pad to make sure at least 2 array values are always returned
list( $tabName, $tabBody ) = array_pad( explode( '=', $tab, 2 ), 2, '' ); [ $tabName, $tabBody ] = array_pad( explode( '=', $tab, 2 ), 2, '' );
// Use language converter to get variant title and also escape html // Use language converter to get variant title and also escape html
$tabName = $parser->getTargetLanguageConverter()->convertHtml( trim( $tabName ) ); $tabName = $parser->getTargetLanguageConverter()->convertHtml( trim( $tabName ) );
$tabBody = $parser->recursiveTagParse( trim( $tabBody ), $frame ); $tabBody = trim( $tabBody );
// A nested tabber which should return json in codex
if ( self::$useCodex && strpos( $tabBody, '{{#tag:tabber' ) !== false ) {
self::$isNested = true;
$tabBody = $parser->recursiveTagParse( $tabBody, $frame );
self::$isNested = false;
// The outermost tabber that must be parsed fully in codex for correct json
} elseif ( self::$useCodex ) {
$tabBody = $parser->recursiveTagParseFully( $tabBody, $frame );
// Normal mode
} else {
$tabBody = $parser->recursiveTagParse( $tabBody, $frame );
}
if ( self::$useCodex && self::$isNested ) {
return json_encode( [
'label' => $tabName,
'content' => $tabBody
],
JSON_THROW_ON_ERROR
);
}
// If $tabBody does not have any HTML element (i.e. just a text node), wrap it in <p/> // If $tabBody does not have any HTML element (i.e. just a text node), wrap it in <p/>
if ( substr( $tabBody, 0, 1 ) !== '<' ) { if ( $tabBody[0] !== '<' ) {
$tabBody = '<p>' . $tabBody . '</p>'; $tabBody = '<p>' . $tabBody . '</p>';
} }
$tab = '<article class="tabber__panel" data-title="' . $tabName . return '<article class="tabber__panel" data-title="' . $tabName .
'">' . $tabBody . '</article>'; '">' . $tabBody . '</article>';
return $tab;
} }
} }

View file

@ -35,47 +35,45 @@ class TabberParsoid extends ExtensionTagHandler implements ExtensionModule {
/** @inheritDoc */ /** @inheritDoc */
public function sourceToDom( ParsoidExtensionAPI $extApi, string $src, array $extArgs ) { public function sourceToDom( ParsoidExtensionAPI $extApi, string $src, array $extArgs ) {
$html = self::render( $extApi, $src ); $html = self::render( $extApi, $src );
$extApi->addModules( [ 'ext.tabberNeue.codex' ] ); $extApi->getMetadata()->addModules( [ 'ext.tabberNeue.codex' ] );
return $extApi->htmlToDom( $html ); return $extApi->htmlToDom( $html );
} }
/** /**
* Renders the necessary HTML for a <tabber> tag. * Renders the necessary HTML for a <tabber> tag.
* *
* @param PParsoidExtensionAPI $extApi * @param ParsoidExtensionAPI $extApi
* @param string $src The input URL between the beginning and ending tags. * @param string $src The input URL between the beginning and ending tags.
* *
* @return string HTML * @return string HTML
*/ */
public static function render( ParsoidExtensionAPI $extApi, string $src ) { public static function render( ParsoidExtensionAPI $extApi, string $src ): string {
$arr = explode( "|-|", $src ); $arr = explode( '|-|', $src );
$htmlTabs = ''; $htmlTabs = '';
foreach ( $arr as $tab ) { foreach ( $arr as $tab ) {
$htmlTabs .= self::buildTab( $extApi, $tab ); $htmlTabs .= self::buildTab( $extApi, $tab );
} }
$html = '<div class="tabber">' . return '<div class="tabber">' .
'<header class="tabber__header"></header>' . '<header class="tabber__header"></header>' .
'<section class="tabber__section">' . $htmlTabs . "</section></div>"; '<section class="tabber__section">' . $htmlTabs . "</section></div>";
return $html;
} }
/** /**
* Build individual tab. * Build individual tab.
* *
* @param PParsoidExtensionAPI $extApi * @param ParsoidExtensionAPI $extApi
* @param string $tab Tab information * @param string $tab Tab information
* *
* @return string HTML * @return string HTML
*/ */
private static function buildTab( ParsoidExtensionAPI $extApi, string $tab ) { private static function buildTab( ParsoidExtensionAPI $extApi, string $tab ): string {
if ( empty( trim( $tab ) ) ) { if ( empty( trim( $tab ) ) ) {
return ''; return '';
} }
// Use array_pad to make sure at least 2 array values are always returned // Use array_pad to make sure at least 2 array values are always returned
list( $tabName, $tabBody ) = array_pad( explode( '=', $tab, 2 ), 2, '' ); [ $tabName, $tabBody ] = array_pad( explode( '=', $tab, 2 ), 2, '' );
/* /*
* Use language converter to get variant title and also escape html * Use language converter to get variant title and also escape html
@ -84,21 +82,19 @@ class TabberParsoid extends ExtensionTagHandler implements ExtensionModule {
*/ */
// $tabName = $parser->getTargetLanguageConverter()->convertHtml( trim( $tabName ) ); // $tabName = $parser->getTargetLanguageConverter()->convertHtml( trim( $tabName ) );
$tabBody = $extApi->domToHTML( $tabBody = $extApi->domToHTML(
$extApi->wikitextToDOM( $extApi->wikitextToDOM(
$tabBody, $tabBody,
[ [
'parseOpts' => [ 'parseOpts' => [
'extTag' => 'tabber', 'extTag' => 'tabber',
'context' => 'inline', 'context' => 'inline',
] ]
], ],
true // sol true // sol
) )
); );
$tab = '<article class="tabber__panel" title="' . $tabName . return '<article class="tabber__panel" title="' . $tabName .
'">' . $tabBody . '</article>'; '">' . $tabBody . '</article>';
return $tab;
} }
} }

View file

@ -14,7 +14,7 @@ declare( strict_types=1 );
namespace MediaWiki\Extension\TabberNeue; namespace MediaWiki\Extension\TabberNeue;
use Hooks; use Exception;
use MediaWiki\MediaWikiServices; use MediaWiki\MediaWikiServices;
use Parser; use Parser;
use PPFrame; use PPFrame;
@ -24,27 +24,23 @@ class TabberTransclude {
/** /**
* Parser callback for <tabbertransclude> tag * Parser callback for <tabbertransclude> tag
* *
* @param string $input * @param string|null $input
* @param array $args * @param array $args
* @param Parser $parser Mediawiki Parser Object * @param Parser $parser Mediawiki Parser Object
* @param PPFrame $frame Mediawiki PPFrame Object * @param PPFrame $frame Mediawiki PPFrame Object
* *
* @return string HTML * @return string HTML
*/ */
public static function parserHook( string $input, array $args, Parser $parser, PPFrame $frame ) { public static function parserHook( ?string $input, array $args, Parser $parser, PPFrame $frame ) {
$tabberTransclude = new TabberTransclude(); $html = self::render( $input, $parser, $frame );
$html = $tabberTransclude->render( $input, $parser, $frame );
if ( $input === null ) { if ( $input === null ) {
return; return '';
} }
// Critial rendering styles // Critical rendering styles
// See ext.tabberNeue.inline.less // See ext.tabberNeue.inline.less
$style = sprintf( $style = sprintf( '<style id="tabber-style">%s</style>', Tabber::$criticalInlineStyle );
'<style id="tabber-style">%s</style>',
'.client-js .tabber__header{height:2.6em;box-shadow:inset 0 -1px 0 0;opacity:.1}.client-js .tabber__header:after{position:absolute;width:16ch;height:.5em;border-radius:40px;margin-top:1em;margin-left:.75em;background:#000;content:""}.client-js .tabber__noscript,.client-js .tabber__panel:not( :first-child ){display:none}'
);
$parser->getOutput()->addHeadItem( $style, true ); $parser->getOutput()->addHeadItem( $style, true );
$parser->getOutput()->addModules( [ 'ext.tabberNeue.legacy' ] ); $parser->getOutput()->addModules( [ 'ext.tabberNeue.legacy' ] );
@ -61,19 +57,22 @@ class TabberTransclude {
* *
* @return string HTML * @return string HTML
*/ */
public static function render( $input, Parser $parser, PPFrame $frame ) { public static function render( string $input, Parser $parser, PPFrame $frame ): string {
$selected = true; $selected = true;
$arr = explode( "\n", $input ); $arr = explode( "\n", $input );
$htmlTabs = ''; $htmlTabs = '';
foreach ( $arr as $tab ) { foreach ( $arr as $tab ) {
$htmlTabs .= self::buildTabTransclude( $tab, $parser, $frame, $selected ); try {
$htmlTabs .= self::buildTabTransclude( $tab, $parser, $frame, $selected );
} catch ( Exception $e ) {
// This can happen if a $currentTitle is null
continue;
}
} }
$html = '<div class="tabber">' . return '<div class="tabber">' .
'<header class="tabber__header"></header>' . '<header class="tabber__header"></header>' .
'<section class="tabber__section">' . $htmlTabs . '</section></div>'; '<section class="tabber__section">' . $htmlTabs . '</section></div>';
return $html;
} }
/** /**
@ -85,23 +84,22 @@ class TabberTransclude {
* @param bool &$selected The tab is the selected one * @param bool &$selected The tab is the selected one
* *
* @return string HTML * @return string HTML
* @throws Exception
*/ */
private static function buildTabTransclude( $tab, Parser $parser, PPFrame $frame, &$selected ) { private static function buildTabTransclude( string $tab, Parser $parser, PPFrame $frame, bool &$selected ): string {
if ( empty( trim( $tab ) ) ) { if ( empty( trim( $tab ) ) ) {
return ''; return '';
} }
$tabBody = '';
$dataProps = []; $dataProps = [];
// Use array_pad to make sure at least 2 array values are always returned // Use array_pad to make sure at least 2 array values are always returned
list( $pageName, $tabName ) = array_pad( explode( '|', $tab, 2 ), 2, '' ); [ $pageName, $tabName ] = array_pad( explode( '|', $tab, 2 ), 2, '' );
$title = Title::newFromText( trim( $pageName ) ); $title = Title::newFromText( trim( $pageName ) );
if ( !$title ) { if ( !$title ) {
if ( empty( $tabName ) ) { if ( empty( $tabName ) ) {
$tabName = $pageName; $tabName = $pageName;
} }
$tabBody = sprintf( '<div class="error">Invalid title: %s</div>', $pageName ); $tabBody = sprintf( '<div class="error">Invalid title: %s</div>', $pageName );
$pageName = '';
} else { } else {
$pageName = $title->getPrefixedText(); $pageName = $title->getPrefixedText();
if ( empty( $tabName ) ) { if ( empty( $tabName ) ) {
@ -127,11 +125,18 @@ class TabberTransclude {
urlencode( $currentTitle->getPrefixedText() ), urlencode( $currentTitle->getPrefixedText() ),
urlencode( $pageName ) urlencode( $pageName )
); );
$dataProps['load-url'] = wfExpandUrl( wfScript( 'api' ) . $query, PROTO_CANONICAL );
$utils = MediaWikiServices::getInstance()->getUrlUtils();
$utils->expand( wfScript( 'api' ) . $query, PROTO_CANONICAL );
$dataProps['load-url'] = $utils->expand( wfScript( 'api' ) . $query, PROTO_CANONICAL );
$oldTabBody = $tabBody; $oldTabBody = $tabBody;
// Allow extensions to update the lazy loaded tab // Allow extensions to update the lazy loaded tab
Hooks::run( 'TabberNeueRenderLazyLoadedTab', [ &$tabBody, &$dataProps, $parser, $frame ] ); MediaWikiServices::getInstance()->getHookContainer()->run(
if ( $oldTabBody != $tabBody ) { 'TabberNeueRenderLazyLoadedTab',
[ &$tabBody, &$dataProps, $parser, $frame ]
);
if ( $oldTabBody !== $tabBody ) {
$parser->getOutput()->recordOption( 'tabberneuelazyupdated' ); $parser->getOutput()->recordOption( 'tabberneuelazyupdated' );
} }
} }

View file

@ -45,6 +45,11 @@ module.exports = exports = defineComponent( {
default: false default: false
} }
}, },
methods: {
escapeId( id ) {
return mw.util.escapeIdForAttribute( id )
}
},
data: function () { data: function () {
return { return {
tabsData: this.tabberData.tabsData, tabsData: this.tabberData.tabsData,

View file

@ -1,5 +1,19 @@
<template> <template>
<cdx-tabs v-if="isChildTabber && tabsData.length > 0" v-model:active="currentTab">
<cdx-tab
v-for="( tab, index ) in tabsData"
:key="index"
:name="escapeId( tab.label )"
:label="tab.label"
>
<tab-content
:html="tab.content"
>
</tab-content>
</cdx-tab>
</cdx-tabs>
<div <div
v-else
v-html="html" v-html="html"
> >
</div> </div>
@ -7,6 +21,7 @@
<script> <script>
const { defineComponent } = require( 'vue' ); const { defineComponent } = require( 'vue' );
const { CdxTabs, CdxTab } = require( '@wikimedia/codex' );
// @vue/component // @vue/component
module.exports = exports = defineComponent( { module.exports = exports = defineComponent( {
@ -17,14 +32,46 @@ module.exports = exports = defineComponent( {
compilerOptions: { compilerOptions: {
whitespace: 'condense' whitespace: 'condense'
}, },
data() {
return {
tabsData: [],
currentTab: ''
};
},
props: { props: {
html: { html: {
type: String, type: String,
required: true required: true
} }
}, },
components: {
CdxTabs: CdxTabs,
CdxTab: CdxTab,
},
methods: {
isChildTabber() {
return Array.isArray(this.html) || this.html.includes("{\"label\":")
},
parse() {
if (Array.isArray(this.html)) {
return this.html
} else {
const tmp = document.createElement('div');
tmp.innerHTML = this.html;
return JSON.parse( tmp.textContent.trim() );
}
},
escapeId( id ) {
return mw.util.escapeIdForAttribute( id )
}
},
mounted: function () { mounted: function () {
this.$el.parentElement.innerHTML = this.$el.innerHTML; if (this.isChildTabber()) {
this.tabsData = this.parse(this.html)
this.currentTab = this.escapeId( this.tabsData[0].label )
}
} }
} ); } );
</script> </script>

View file

@ -17,10 +17,6 @@ function initApp( tabber ) {
tabs.forEach( ( tab ) => { tabs.forEach( ( tab ) => {
const label = tab.getAttribute( 'data-title' ); const label = tab.getAttribute( 'data-title' );
if ( tab.querySelector( '.tabber' ) ) {
throw new Error( 'Nested Tabber is not supported in Codex mode, please use legacy mode instead.' );
}
tabberData.tabsData.push( { tabberData.tabsData.push( {
name: mw.util.escapeIdForAttribute( label ), name: mw.util.escapeIdForAttribute( label ),
label: label, label: label,
@ -30,7 +26,7 @@ function initApp( tabber ) {
tabberData.currentTab = tabberData.tabsData[ 0 ].name; tabberData.currentTab = tabberData.tabsData[ 0 ].name;
// @ts-ignore MediaWiki-specific function //@ts-ignore MediaWiki-specific function
Vue.createMwApp( Vue.createMwApp(
App, Object.assign( { App, Object.assign( {
tabberData: tabberData tabberData: tabberData
@ -45,18 +41,8 @@ function initApp( tabber ) {
*/ */
function main( document ) { function main( document ) {
const tabbers = document.querySelectorAll( '.tabber:not( .tabber--live )' ); const tabbers = document.querySelectorAll( '.tabber:not( .tabber--live )' );
const sortedTabbers = [];
/* Nested Tabber children needed to be rendered before parents */ tabbers.forEach( initApp );
tabbers.forEach( ( tabber ) => {
if ( tabber.querySelector( '.tabber:not( .tabber--live )' ) ) {
sortedTabbers.push( tabber );
} else {
sortedTabbers.unshift( tabber );
}
} );
sortedTabbers.forEach( initApp );
} }
main( document ); main( document );