xref: /plugin/bpmnio/action/editor.php (revision d4abf4f00ed385621d0aefbc15088975099af248)
1<?php
2
3/**
4 * @license    See LICENSE file
5 * @author     Jaap de Haan <jaap.dehaan@color-of-code.de>
6 */
7
8// See help:
9// * https://www.dokuwiki.org/devel:section_editor
10// * https://www.dokuwiki.org/devel:releases:refactor2021
11
12use dokuwiki\Form\Form;
13use dokuwiki\Utf8;
14
15class action_plugin_bpmnio_editor extends DokuWiki_Action_Plugin
16{
17    public function register(Doku_Event_Handler $controller)
18    {
19        $controller->register_hook('HTML_SECEDIT_BUTTON', 'BEFORE', $this, 'secedit_button');
20
21        $controller->register_hook('EDIT_FORM_ADDTEXTAREA', 'BEFORE', $this, 'handle_form');
22        $controller->register_hook('HTML_EDIT_FORMSELECTION', 'BEFORE', $this, 'handle_form');
23
24        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_post');
25    }
26
27    function secedit_button(Doku_Event $event)
28    {
29        if ($this->_shall_ignore($event)) return;
30
31        $event->data['name'] = $this->getLang('edit_diagram');
32    }
33
34    function handle_form(Doku_Event $event)
35    {
36        if ($this->_shall_ignore($event)) return;
37
38        global $TEXT;
39        global $RANGE;
40        global $INPUT;
41
42        if (!$RANGE) {
43            // section editing failed, use default editor instead
44            $event->data['target'] = 'section';
45            return;
46        }
47
48        $event->stopPropagation();
49        $event->preventDefault();
50
51        $form = &$event->data['form'];
52        $data = base64_encode($TEXT);
53
54        $this->_addHidden($form, 'plugin_bpmnio_data', $data);
55        $this->_addHTML($form, <<<HTML
56            <div class="plugin-bpmnio" id="plugin_bpmnio__editor">
57                <div class="bpmn_js_data">{$data}</div>
58                <div class="bpmn_js_canvas">
59                    <div class="bpmn_js_container"></div>
60                </div>
61                </div>
62            </div>
63            HTML);
64
65        // used during previews
66        $this->_addHidden($form, 'target', 'plugin_bpmnio');
67        $this->_addHidden($form, 'range', $RANGE);
68    }
69
70    function handle_post(Doku_Event $event)
71    {
72        global $TEXT;
73        global $INPUT;
74
75        if (!$INPUT->post->has('plugin_bpmnio_data')) return;
76
77        $TEXT = base64_decode($INPUT->post->str('plugin_bpmnio_data'));
78    }
79
80    private function _addHidden($form, $field, $data)
81    {
82        if (is_a($form, Form::class)) { // $event->name is EDIT_FORM_ADDTEXTAREA
83            $form->setHiddenField($field, $data);
84        } else { // $event->name is HTML_EDIT_FORMSELECTION
85            $form->addHidden($field, $data);
86        }
87    }
88
89    private function _addHTML($form, $data)
90    {
91        $form->addHTML($data);
92    }
93
94    private function _shall_ignore(Doku_Event $event)
95    {
96        if ($event->data['target'] !== 'plugin_bpmnio')
97            return true;
98        return false;
99    }
100}
101