1<?php 2 3use dokuwiki\Extension\ActionPlugin; 4use dokuwiki\Extension\EventHandler; 5use dokuwiki\Extension\Event; 6 7/** 8 * DokuWiki Plugin myshortcuts (Action Component) 9 * 10 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 11 * @author David Jiménez <davidjimenez75@gmail.com> 12 */ 13class action_plugin_myshortcuts extends ActionPlugin 14{ 15 /** @inheritDoc */ 16 public function register(EventHandler $controller) 17 { 18 $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'handleMetaheaderOutput'); 19 $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'loadAssets'); 20 } 21 22 /** 23 * Load JavaScript and CSS files 24 */ 25 public function loadAssets() 26 { 27 global $conf; 28 29 // Add JavaScript file 30 $JSINFO['plugins']['myshortcuts'] = true; 31 } 32 33 /** 34 * Pass configuration to JavaScript 35 * 36 * @param Event $event Event object 37 * @param mixed $param optional parameter passed when event was registered 38 * @return void 39 */ 40 public function handleMetaheaderOutput(Event $event, $param) 41 { 42 // Get plugin configuration 43 $shortcutEdit = $this->getConf('shortcut_edit'); 44 $shortcutSave = $this->getConf('shortcut_save'); 45 $shortcutSnippet = $this->getConf('shortcut_snippet'); 46 47 // Build snippets array from individual configurations 48 // Order: 1-9, then 0 (as per keyboard layout) 49 $snippets = []; 50 $snippetNumbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']; 51 52 foreach ($snippetNumbers as $num) { 53 $snippetText = $this->getConf('snippet_' . $num); 54 if (!empty($snippetText)) { 55 $snippets[] = [ 56 'label' => $num, 57 'number' => $num, 58 'text' => $snippetText 59 ]; 60 } 61 } 62 63 // Add CSS file 64 $event->data['link'][] = [ 65 'type' => 'text/css', 66 'rel' => 'stylesheet', 67 'href' => DOKU_BASE . 'lib/plugins/myshortcuts/style.css' 68 ]; 69 70 // Create JavaScript configuration object 71 $script = '/* MyShortcuts Plugin Config */' . "\n"; 72 $script .= 'var MYSHORTCUTS_CONFIG = ' . json_encode([ 73 'shortcutEdit' => $shortcutEdit, 74 'shortcutSave' => $shortcutSave, 75 'shortcutSnippet' => $shortcutSnippet, 76 'snippets' => $snippets 77 ], JSON_PRETTY_PRINT) . ';'; 78 79 // Add inline script to page (must come before main script) 80 $event->data['script'][] = [ 81 'type' => 'text/javascript', 82 '_data' => $script, 83 ]; 84 85 // Add main JavaScript file 86 $event->data['script'][] = [ 87 'type' => 'text/javascript', 88 'src' => DOKU_BASE . 'lib/plugins/myshortcuts/script.js', 89 ]; 90 } 91} 92