From 706e051d2ed715f8fdc5e194800cde41ee2ed016 Mon Sep 17 00:00:00 2001 From: Lex Lim Date: Thu, 12 May 2022 20:28:34 +0800 Subject: [PATCH] Add cover argument for Tile Widget. --- includes/TileWidget.php | 45 ++++++++++++++++++++++++++++++++++++----- modules/tile/tile.js | 10 ++++++--- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/includes/TileWidget.php b/includes/TileWidget.php index dc0c143..32a0c67 100644 --- a/includes/TileWidget.php +++ b/includes/TileWidget.php @@ -2,6 +2,7 @@ namespace Isekai\Widgets; use Html; +use MediaWiki\MediaWikiServices; use Title; class TileWidget { @@ -11,28 +12,36 @@ class TileWidget { private $href = ''; private $badge = false; private $color = false; + private $cover = false; private $images = []; private $grid = false; private $attributes = []; - public function __construct($args){ + public function __construct($args, $content){ + $this->content = $content; $this->parseArgs($args); } public static function create(string $text, array $args, \Parser $parser, \PPFrame $frame){ $parser->getOutput()->addModules('ext.isekai.tile'); - if($text){ - $args['title'] = $text; + $content = ''; + if ($text) { + $content = $frame->expand($text); + + $title = preg_replace('/\[\[.*?\]\]/', '', $content); + $title = preg_replace('/]+>/', '', $title); + $title = strip_tags(trim($title)); + $args['title'] = $title; } - $tile = new TileWidget($args); + $tile = new TileWidget($args, $content); return [$tile->toHtml(), 'markerType' => 'nowiki']; } private function parseArgs($args){ - $allowedArgs = ['size', 'icon', 'title', 'badge', 'color', 'href', 'grid']; + $allowedArgs = ['size', 'icon', 'title', 'cover', 'badge', 'color', 'href', 'grid']; foreach($args as $name => $arg){ if(in_array($name, $allowedArgs)){ @@ -68,6 +77,10 @@ class TileWidget { } } + private function getCoverArgs(array &$element, array &$content){ + $element['data-cover'] = $this->cover; + } + private function getHrefArgs(array &$element, array &$content){ if(substr($this->href, 0, 2) == '[[' && substr($this->href, -2, 2) == ']]'){ //内部链接 $titleText = substr($this->href, 2, strlen($this->href) - 4); @@ -117,6 +130,27 @@ class TileWidget { } private function getImagesArgs(array &$element, array &$content){ + /*$service = MediaWikiServices::getInstance(); + $this->images = []; + // 提取wikitext图片 + preg_match_all('/\[\[(?.+?:.+?)(\|.*?)?\]\]/', $this->content, $matches); + if (isset($matches['title']) && !empty($matches['title'])) { + foreach ($matches['title'] as $titleText) { + $title = Title::newFromText($titleText); + if ($title->inNamespace(NS_FILE)) { + $file = $service->getRepoGroup()->findFile($title); + $thumb = $file->getUrl(); + $this->images[] = $thumb; + } + } + } + + // 提取html图片 + preg_match_all('/<img .*?src="(?<src>.*?)".*?srcset="(?<srcset>.*?)"[^\>]+>/', $this->content, $matches); + if (isset($matches['src']) && !empty($matches['src'])) { + $this->images = array_merge($this->images, $matches['src']); + }*/ + if(!empty($this->images)){ $element['data-effect'] = 'image-set'; foreach($this->images as $image){ @@ -158,6 +192,7 @@ class TileWidget { $this->getColorArgs($element, $content); $this->getIconArgs($element, $content); $this->getTitleArgs($element, $content); + $this->getCoverArgs($element, $content); $this->getHrefArgs($element, $content); $this->getBadgeArgs($element, $content); $this->getImagesArgs($element, $content); diff --git a/modules/tile/tile.js b/modules/tile/tile.js index 08ef7b6..dd0fa2b 100644 --- a/modules/tile/tile.js +++ b/modules/tile/tile.js @@ -31,6 +31,10 @@ isekai.tile.init = function () { }; (function($){ + function rand(min, max) { // min and max included + return Math.floor(Math.random() * (max - min + 1) + min) + } + var Utils = { isValue: function(val){ return val !== undefined && val !== null && val !== ""; @@ -585,7 +589,7 @@ isekai.tile.init = function () { var temp = this.images.slice(); for(var i = 0; i < 5; i++) { - var rnd_index = $.random(0, temp.length - 1); + var rnd_index = rand(0, temp.length - 1); var div = $("<div>").addClass("img -js-img-"+i).css("background-image", "url("+temp[rnd_index].src+")"); element.prepend(div); temp.splice(rnd_index, 1); @@ -596,12 +600,12 @@ isekai.tile.init = function () { $.setInterval(function(){ var temp = that.images.slice(); var colors = Colors.colors(Colors.PALETTES.ALL), bg; - bg = colors[$.random(0, colors.length - 1)]; + bg = colors[rand(0, colors.length - 1)]; element.css("background-color", bg); for(var i = 0; i < a.length; i++) { - var rnd_index = $.random(0, temp.length - 1); + var rnd_index = rand(0, temp.length - 1); var div = element.find(".-js-img-"+a[i]); switchImage(div, temp[rnd_index].src, i); temp.splice(rnd_index, 1);