1<?php 2/** 3 * DokuWiki Plugin VegaLite (Action Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Robert Weinmeister <develop@weinmeister.org> 7 */ 8 9declare(strict_types=1); 10 11if (!defined('DOKU_INC')) { 12 die(); 13} 14 15class action_plugin_vegalite extends \dokuwiki\Extension\ActionPlugin 16{ 17 /** @inheritDoc */ 18 public function register(Doku_Event_Handler $controller): void { 19 $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'load'); 20 $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAjaxRequest'); 21 } 22 23 private function addLocalScript(Doku_Event $event): void { 24 $event->data['script'][] = [ 25 'type' => 'text/javascript', 26 'charset' => 'utf-8', 27 'src' => DOKU_BASE . 'lib/plugins/vegalite/vega.min.js', 28 ]; 29 30 $event->data['script'][] = [ 31 'type' => 'text/javascript', 32 'charset' => 'utf-8', 33 'src' => DOKU_BASE . 'lib/plugins/vegalite/vega-lite.min.js', 34 ]; 35 36 $event->data['script'][] = [ 37 'type' => 'text/javascript', 38 'charset' => 'utf-8', 39 'src' => DOKU_BASE . 'lib/plugins/vegalite/vega-embed.min.js', 40 ]; 41 } 42 43 private function addScript(Doku_Event $event): void { 44 $event->data['script'][] = [ 45 'type' => 'text/javascript', 46 'charset' => 'utf-8', 47 'src' => "https://cdn.jsdelivr.net/npm/vega/build/vega.min.js", 48 ]; 49 50 $event->data['script'][] = [ 51 'type' => 'text/javascript', 52 'charset' => 'utf-8', 53 'src' => "https://cdn.jsdelivr.net/npm/vega-lite/build/vega-lite.min.js", 54 ]; 55 56 $event->data['script'][] = [ 57 'type' => 'text/javascript', 58 'charset' => 'utf-8', 59 'src' => "https://cdn.jsdelivr.net/npm/vega-embed/build/vega-embed.min.js", 60 ]; 61 } 62 63 private function pageIncludesVegaLite(): bool { 64 // true if the vegalite tag is used 65 // the include plugin can hide this fact, so we need a separate check for it 66 global $ACT; 67 global $TEXT; 68 if ('preview'==$ACT) { 69 $wikiText = $TEXT; 70 } else { 71 $wikiText = rawWiki(getID()); 72 } 73 74 if ('edit'==$ACT) { 75 return false; 76 } 77 if (str_contains($wikiText, '<vegalite') || str_contains($wikiText, '{{page>') || str_contains($wikiText, '{{section>') || str_contains($wikiText, '{{namespace>') || str_contains($wikiText, '{{tagtopic>')) { 78 return true; 79 } 80 81 return false; 82 } 83 84 /** 85 * Load the Vega-Lite library and configuration into the page. 86 * 87 * @param Doku_Event $event DokuWiki event object 88 * @param mixed $param Unused parameter. 89 */ 90 public function load(Doku_Event $event, $param): void { 91 // only load Vega-Lite if it is needed 92 if (!$this->pageIncludesVegaLite()) { 93 return; 94 } 95 96 $location = $this->getConf('location'); 97 // add the appropriate Vega-Lite script based on the location configuration 98 match ($location) { 99 'local' => $this->addLocalScript($event), 100 default => $this->addScript($event) 101 }; 102 103 $event->data['link'][] = [ 104 'rel' => 'stylesheet', 105 'type' => 'text/css', 106 'href' => DOKU_BASE . "lib/plugins/vegalite/vegalite.css", 107 ]; 108 } 109} 110