Create basic content function
Signed-off-by: Justin Georgi <justin.georgi@gmail.com>
This commit is contained in:
125
includes/AnnotationHandler.php
Normal file
125
includes/AnnotationHandler.php
Normal file
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
namespace MediaWiki\Extension\AnatImageViewer;
|
||||
|
||||
use Devium\Toml\Toml;
|
||||
use MediaWiki\MediaWikiServices;
|
||||
use Content;
|
||||
use TextContentHandler;
|
||||
use MediaWiki\Content\Renderer\ContentParseParams;
|
||||
use ParserOutput;
|
||||
use ConfigFactory;
|
||||
use RequestContext;
|
||||
use Html;
|
||||
|
||||
class AnnotationHandler extends TextContentHandler {
|
||||
private const ANNOT_VERSION = '0.1';
|
||||
|
||||
public function __construct( $modelId = 'annotation' ) {
|
||||
parent::__construct( $modelId, [ CONTENT_FORMAT_TEXT ] );
|
||||
}
|
||||
|
||||
/*
|
||||
public function serializeContent( Content $content, $format = null ) {
|
||||
}
|
||||
|
||||
public function unserializeContent( $blob, $format = null ) {
|
||||
}
|
||||
*/
|
||||
|
||||
public function makeEmptyContent() {
|
||||
return new AnnotationContent();
|
||||
}
|
||||
|
||||
public function supportsDirectEditing() {
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function fillParserOutput( Content $content, ContentParseParams $cpoParams, ParserOutput &$output ) {
|
||||
// define $html somewhere
|
||||
// e.g. $output->setText( $html );
|
||||
|
||||
self::console_log($content, true);
|
||||
|
||||
$metadata = toml_decode($content->getNativeData(), true);
|
||||
|
||||
if (isset($metadata['baseImage'])) {
|
||||
$baseImage = MediaWikiServices::getInstance()->getRepoGroup()->findFile('File:' . $metadata['baseImage']);
|
||||
} else {
|
||||
$baseImage = false;
|
||||
}
|
||||
|
||||
$output->setText( self::buildSvg($metadata,$baseImage) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Build annotated SVG from TOML metadata
|
||||
*
|
||||
* This takes in the metadata text from the file page (or the current editor)
|
||||
* and produces the html string for the svg with base image and annotations.
|
||||
*
|
||||
* @param string $metadata The metadata object parsed from the text
|
||||
* @param string $baseImageUrl The full url pointing to the base image to annotate
|
||||
* @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 function buildSvg($metadata, $baseImage) {
|
||||
//Gather basic data
|
||||
$elBaseImg = '';
|
||||
$vbHeight = 100;
|
||||
$vbWidth = 100;
|
||||
if ($baseImage) {
|
||||
$baseImageUrl = $baseImage->getFullUrl();
|
||||
$baseHeight = $baseImage->getHeight();
|
||||
$baseWidth = $baseImage->getWidth();
|
||||
$baseAspect = $baseHeight / $baseWidth;
|
||||
$vbWidth = 100 / sqrt($baseAspect);
|
||||
$vbHeight = $baseAspect * $vbWidth;
|
||||
$attrBase = array(
|
||||
'class' => 'annot-base',
|
||||
'preserveAspectRatio' => 'none',
|
||||
'width' => $vbWidth,
|
||||
'height' => $vbHeight,
|
||||
'href' => $baseImageUrl
|
||||
);
|
||||
$elBaseImg = Html::rawElement('image', $attrBase);
|
||||
}
|
||||
$mainConfig = ConfigFactory::getDefaultInstance()->makeConfig( 'main' );
|
||||
$context = RequestContext::getMain();
|
||||
|
||||
//Render and return svg
|
||||
$attrSvg = array(
|
||||
'class' => 'annot-svg',
|
||||
'version' => '1.1',
|
||||
'viewBox' => "0 0 {$vbWidth} {$vbHeight}",
|
||||
'xml:space' => 'preserve',
|
||||
'xmlns' => 'http://www.w3.org/2000/svg'
|
||||
);
|
||||
if ($this->parameters['width'] > 0) {
|
||||
$attrSvg['width'] = $this->parameters['width'];
|
||||
}
|
||||
elseif ($this->parameters['height'] > 0) {
|
||||
$attrSvg['height'] = $this->parameters['height'];
|
||||
}
|
||||
|
||||
return Html::rawElement('svg', $attrSvg, $elBaseImg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Small helper function to display information on the browser console
|
||||
*
|
||||
* Usage:
|
||||
* echo '<script>';
|
||||
* self::console_log('logged string');
|
||||
* echo '</script>';
|
||||
*
|
||||
* @param $data information to display
|
||||
* @param bool $add_script_tags true to put information is inside complete script tag
|
||||
*/
|
||||
public static function console_log($data, $add_script_tags = false) {
|
||||
$command = 'console.log('. json_encode($data, JSON_HEX_TAG).');';
|
||||
if ($add_script_tags) {
|
||||
$command = '<script>'. $command . '</script>';
|
||||
}
|
||||
echo $command;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user