Offload all model generation to model output

Signed-off-by: Justin Georgi <justin.georgi@gmail.com>
This commit is contained in:
2024-10-26 21:06:31 -07:00
parent 7ee162430f
commit 7264fcc570
5 changed files with 170 additions and 211 deletions

View File

@@ -3,6 +3,7 @@ namespace MediaWiki\Extension\GlModelViewer;
use MediaWiki\MediaWikiServices;
use Html;
use ParserOutput;
class GlModelHooks {
/**
@@ -51,32 +52,6 @@ class GlModelHooks {
}
}
/**
* MWHook: Replace File link rendered image with model-viewer element
*
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ImageBeforeProduceHTML
*
* @param DummyLinker &$linker
* @param Title &$title
* @param FileObject &$file object containing information regarding the actual file for the File page
* @param array &$frameParams any added parameters of the file link such as class etc.
* @param array &$handlerParams
* @param &$time
* @param &$result By reference access to the rendered html result of the image
* @param Parser $parser
* @param string &$query
* @param &$widthOption
* @return bool|void True to continue default processing or false to abort for custom processing
*/
public static function onImageBeforeProduceHTML( &$linker, &$title, &$file, array &$frameParams, array &$handlerParams, &$time, &$result, $parser, string &$query, &$widthOption ) {
if ($file->getMimeType() !== 'model/gltf-binary') {
return true;
}
$result = self::buildViewer($file->getDescriptionText(), $file->getFullUrl(), $frameParams);
return false;
}
/**
* MWHook: Display model on file page
*
@@ -87,16 +62,9 @@ class GlModelHooks {
*/
public static function onImageOpenShowImageInlineBefore( $imagepage, $out ){
$file = $imagepage->getFile();
$mh = $file->getHandler();
$mt = $file->getMediaType();
//$mmt = $mh->getHandler();
echo '<script>';
self::console_log($mh);
self::console_log($mt);
echo '</script>';
if ($file->getMimeType() == 'model/gltf-binary') {
$viewer = self::buildViewer($file->getDescriptionText(), $file->getFullUrl(), ['class' => 'view-default']);
$out->addHtml(Html::rawElement('div',['id' => 'file', 'class' => 'fullModelView'],$viewer));
$out->clearHTML();
$out->addWikiTextAsContent('[[' . $file->getTitle()->getFullText() . '|800x600px]]');
}
}
@@ -116,10 +84,12 @@ class GlModelHooks {
if (!$file || $file->getMimeType() !== 'model/gltf-binary') {
return true;
}
$out = $editor->getContext()->getOutput();
$out->addModules('ext.glmv');
$previewViewer = self::buildViewer($content->getText(), $file->getFullUrl(), ['class' => 'view-default']);
$mvTransform = $file->transform([ 'width' => '800', 'hight' => '600']);
$previewViewer = $mvTransform->toHtml();
$addButtonAttr = array(
'class' => 'AddHotspot',
@@ -131,102 +101,6 @@ class GlModelHooks {
return false;
}
/**
* Build model-viewer and child elements
*
* This takes in the metadata text from the model page (or the current editor)
* and produces the html string for the model-viewer and all relevant child
* elements.
*
* @param string $inText The metadata text which must include a json formatted string inside a pre tag
* @param string $srcUrl The full url pointing to the model file
* @param array $frameParams The additional user defined parameters for the viewer such as hotspot and view classes
* @return string Html string of the complete model-viewer element inside a div container
*/
private static function buildViewer($inText, $srcUrl, $frameParams) {
//Gather basic data
preg_match('/<pre>([\S\s]*?)<\/pre>/',$inText,$modelDescript);
$metadata = json_decode($modelDescript[1], true);
$viewClass = self::extractClassParams('view',$frameParams['class']);
$hsSetClass = self::extractClassParams('hsset',$frameParams['class']);
//Handle annotations and annotation sets
if (isset($metadata['annotations'])) {
$hotspots = [];
$annotations = [];
if ($hsSetClass != 'default' && isset($metadata['annotationSets'])) {
foreach($metadata['annotationSets'][$hsSetClass] as $includeAn) {
if (isset($metadata['annotations'][$includeAn])) {
$annotations[$includeAn] = $metadata['annotations'][$includeAn];
}
}
} else {
$annotations = $metadata['annotations'];
}
foreach($annotations as $label => $an) {
$elAnnot = Html::rawElement('div',['class' => 'HotspotAnnotation HiddenAnnotation'],$label);
$hsDefault = array(
'class' => 'Hotspot',
'slot' => 'hotspot-'.(count($hotspots) +1),
'onmousedown' => 'event.stopPropagation()',
'ontouchstart' => 'event.stopPropagation()',
'onclick' => 'onAnnotation(event)'
);
$attrHotspot = array_merge($hsDefault, $an);
$elHotspot = Html::rawElement('button',$attrHotspot,$elAnnot.(count($hotspots) +1));
array_push($hotspots, $elHotspot);
}
}
//Set viewer configurations or use basic default if none defined
if (isset($metadata['viewerConfig']) && isset($metadata['viewerConfig'][$viewClass])) {
$attrModelView = $metadata['viewerConfig'][$viewClass];
} else {
$attrModelView = array('camera-controls' => true);
}
//Add important additional attributes and render model-viewer with hotspots
$attrModelView = array_merge(['src' => $srcUrl, 'class' => 'mv-model', 'interpolation-decay' => '100'], $attrModelView);
$attrModelView['style'] = 'width: 100%; height: 100%; min-height: 400px;';
$hotspotHtml = (isset($hotspots)) ? implode($hotspots) : '';
$elModel = Html::rawElement('model-viewer', $attrModelView, $hotspotHtml);
//Render and return container element with model-viewer
$attrContainer = array(
'style' => "width: 800px; height: 600px;",
'onmousedown' => 'clearAnnotations()',
'ontouchstart' => 'clearAnnotations()'
);
return Html::rawElement('div', $attrContainer, $elModel);
}
/**
* Detect and return model user params from link classes
*
* The best way to send information in a file link is by adding classes to the
* link. This function parses the array of those classes and returns the information
* or 'default'
*
* @param string $paramType the type of parameter to be extracted 'view'|'hsset'
* @param string $classList the string of class names as returned in $frameParams or default construction
* @return string parameter name or 'default'
*/
private static function extractClassParams(string $paramType, string $classList) {
if (!in_array($paramType, ['view', 'hsset'])) {
return 'default';
}
if (!isset($classList)) {
return 'default';
}
$searchString = '/' . $paramType . '-(\S*)/';
preg_match($searchString,$classList,$extractInfo);
return ($extractInfo[1]) ? $extractInfo[1] : 'default';
}
/**
* Small helper function to display information on the browser console
*