Sbisson has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/398523 )

Change subject: [EXPERIMENTAL] Running an isomorphic react app
......................................................................

[EXPERIMENTAL] Running an isomorphic react app

Change-Id: Ibf3885e8d0cc9e792227382035b61995eb2a30f7
---
M .gitignore
M extension.json
M includes/UrlGenerator.php
M includes/View.php
4 files changed, 70 insertions(+), 9 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Flow 
refs/changes/23/398523/1

diff --git a/.gitignore b/.gitignore
index 8fe72ff..2324629 100644
--- a/.gitignore
+++ b/.gitignore
@@ -8,3 +8,5 @@
 \#*#
 /composer.lock
 /docs
+modules/fleact-client.js
+modules/fleact-server.js
diff --git a/extension.json b/extension.json
index e238aa4..83d2dc6 100644
--- a/extension.json
+++ b/extension.json
@@ -760,6 +760,16 @@
                                "desktop",
                                "mobile"
                        ]
+               },
+               "fleact-server": {
+                       "scripts": [
+                               "fleact-server.js"
+                       ]
+               },
+               "fleact-client": {
+                       "scripts": [
+                               "fleact-client.js"
+                       ]
                }
        },
        "ResourceFileModulePaths": {
diff --git a/includes/UrlGenerator.php b/includes/UrlGenerator.php
index 5d176ea..3c0f78b 100644
--- a/includes/UrlGenerator.php
+++ b/includes/UrlGenerator.php
@@ -433,10 +433,15 @@
         * @param UUID $workflowId
         * @return Anchor
         */
-       public function workflowLink( Title $title = null, UUID $workflowId ) {
+       public function workflowLink( Title $title = null, UUID $workflowId, 
$version = null ) {
+               $query = [];
+               if ( $version ) {
+                       $query[ 'version' ] = $version;
+               }
                return new Anchor(
                        wfMessage( 'flow-workflow' ),
-                       $this->resolveTitle( $title, $workflowId )
+                       $this->resolveTitle( $title, $workflowId ),
+                       [ 'version' => $version ]
                );
        }
 
diff --git a/includes/View.php b/includes/View.php
index 22d2183..0ea2d64 100644
--- a/includes/View.php
+++ b/includes/View.php
@@ -9,11 +9,14 @@
 use Flow\Model\HtmlRenderingInformation;
 use Flow\Model\UUID;
 use Flow\Model\Workflow;
+use FormatJson;
 use Html;
 use Hooks;
 use IContextSource;
 use Message;
 use OutputPage;
+use ResourceLoader;
+use ResourceLoaderContext;
 use Title;
 
 class View extends ContextSource {
@@ -67,15 +70,14 @@
                $apiResponse = $this->buildApiResponse( $loader, $blocks, 
$action, $parameters );
 
                $output = $this->getOutput();
-               $output->enableOOUI();
-               $this->addModules( $output, $action );
+
                // Please note that all blocks can set page title, which may 
cause them
                // to override one another's titles
                foreach ( $blocks as $block ) {
                        $block->setPageTitle( $output );
                }
 
-               $this->renderApiResponse( $apiResponse );
+               $this->renderApiResponse( $apiResponse, $action );
        }
 
        protected function addModules( OutputPage $out, $action ) {
@@ -227,6 +229,9 @@
                                $anchor = $value;
                                $value = $value->toArray();
 
+                               // trying to preserve version of the app in URLs
+                               $anchor->query[ 'version' ] = 
$this->getRequest()->getVal( 'version' );
+
                                // TODO: We're looking into another approach 
for this
                                // using a parser function, so the URL doesn't 
have to be
                                // fully qualified.
@@ -243,13 +248,51 @@
                return $apiResponse;
        }
 
-       protected function renderApiResponse( array $apiResponse ) {
+       protected function renderApiResponse( array $apiResponse, $action ) {
+               if ( $this->getRequest()->getVal( 'version' ) === '2' ) {
+                       $this->renderWithReactjs( $apiResponse, $action );
+               } else {
+                       $this->renderWithHandlebar( $apiResponse, $action );
+               }
+       }
+
+       private function renderWithReactjs( array $apiResponse, $action ) {
+               $out = $this->getOutput();
+               $result = $this->preRenderWithReactjs( $apiResponse, $action );
+               $state = $result ? $result['state'] : $apiResponse;
+               $markup = $result ? $result['markup'] : '';
+               $out->addHTML( Html::rawElement(
+                       'div',
+                       [ 'id' => 'fleact-root' ],
+                       $markup
+               ) );
+
+               $out->addJsConfigVars( 'wgFlowData', $state );
+               $out->addModules( 'fleact-client' );
+       }
+
+       private function preRenderWithReactjs( array $apiResponse, $action ) {
+               if ( class_exists( 'V8Js' ) ) {
+                       $js = "var global = {};";
+                       $r = new ResourceLoader();
+                       $js .= $r->getModule( 'fleact-server' )->getScript( 
ResourceLoaderContext::newDummyContext() );
+                       $js .= "\nglobal.renderFleactServer( " . 
FormatJson::encode( $apiResponse ) . ", '" . $action . "' );\n";
+                       $v8 = new \V8Js();
+                       $this->getOutput()->addJsConfigVars( 
'wgFlowPreRendered', true );
+                       $result = $v8->executeString( $js );
+                       return [ 'state' => FormatJson::decode( $result->state 
), 'markup' => $result->markup ];
+               }
+       }
+
+       private function renderWithHandlebar( array $apiResponse, $action ) {
+               $out = $this->getOutput();
+               $out->enableOOUI();
+               $this->addModules( $out, $action );
+
                // Render the flow-component wrapper
                if ( empty( $apiResponse['blocks'] ) ) {
                        return [];
                }
-
-               $out = $this->getOutput();
 
                $jsonBlobResponse = $apiResponse;
 
@@ -333,7 +376,8 @@
        protected function redirect( Workflow $workflow ) {
                $link = $this->urlGenerator->workflowLink(
                        $workflow->getArticleTitle(),
-                       $workflow->getId()
+                       $workflow->getId(),
+                       $this->getRequest()->getVal( 'version' )
                );
                $this->getOutput()->redirect( $link->getFullURL() );
        }

-- 
To view, visit https://gerrit.wikimedia.org/r/398523
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibf3885e8d0cc9e792227382035b61995eb2a30f7
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Flow
Gerrit-Branch: master
Gerrit-Owner: Sbisson <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to