1<?php
2/**
3 * DokuWiki Plugin copycode (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Nicolas Prigent <mail.nicolasprigent@gmail.com>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) {
11    die();
12}
13
14class action_plugin_copycode_copycode extends DokuWiki_Action_Plugin
15{
16
17    /**
18     * Registers a callback function for a given event
19     *
20     * @param Doku_Event_Handler $controller DokuWiki's event controller object
21     *
22     * @return void
23     */
24    public function register(Doku_Event_Handler $controller)
25    {
26        $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'pass_settings_js');
27        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this,'add_cursor_styling');
28        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this,'hook_copycode_js');
29    }
30
31    /**
32     * [Custom event handler which performs action]
33     *
34     * Called for event:
35     *
36     * @param Doku_Event $event  event object by reference
37     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
38     *                           handler was registered]
39     *
40     * @return void
41     */
42    public function hook_copycode_js(Doku_Event $event, $param)
43    {
44		// this code does not need execution (anymore?), as 'script.js' is automatically merged into global js.php.
45        // $event->data['script'][] = array(
46            // 'type'    => 'text/javascript',
47            // 'charset' => 'utf-8',
48            // '_data'   => '',
49            // 'src'     => DOKU_PLUGIN.'copycode/script.js');
50    }
51
52    /**
53     * Event handler to pass settings to JavaScript via $JSINFO
54     *
55     * Called for event:
56     *
57     * @param Doku_Event $event  event object by reference
58     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
59     *                           handler was registered]
60     *
61     * @return void
62     */
63    public function pass_settings_js(Doku_Event $event, $param)
64    {
65
66		global $JSINFO;
67		if (empty($JSINFO['plugins'])) {
68			$JSINFO['plugins'] = [];
69		}
70		$JSINFO['plugins']['copycode'] = [
71			'EnableForInline' => $this->getConf('enable_for_inline', 0),
72			'EnableForHighlighted' => $this->getConf('enable_for_highlighted', 0),
73			'EnableBlockInline' => $this->getConf('enable_blockinline', 0)
74		];
75	}
76
77    /**
78     * adds cursor styling
79     *
80     */
81    public function add_cursor_styling()
82    {
83        $cursor = $this->getConf('copycode_hover_cursor');
84        echo '<style>.enabled-copycode { cursor:'. $cursor .'; }</style>';
85    }
86
87}
88
89