xref: /plugin/bpmnio/action/editor.php (revision 43854469855b0d3261173416b47cbad6e62bc444)
1<?php
2
3/**
4 * @license    See LICENSE file
5 */
6
7// See help:
8// * https://www.dokuwiki.org/devel:section_editor
9// * https://www.dokuwiki.org/devel:releases:refactor2021
10use dokuwiki\Extension\ActionPlugin;
11use dokuwiki\Extension\EventHandler;
12use dokuwiki\Extension\Event;
13use dokuwiki\Form\Form;
14use dokuwiki\Utf8;
15
16class action_plugin_bpmnio_editor extends ActionPlugin
17{
18    public function register(EventHandler $controller): void
19    {
20        $controller->register_hook('HTML_SECEDIT_BUTTON', 'BEFORE', $this, 'sectionEditButton');
21        $controller->register_hook('EDIT_FORM_ADDTEXTAREA', 'BEFORE', $this, 'handleForm');
22        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handlePost');
23        $controller->register_hook('FORM_EDIT_OUTPUT', 'BEFORE', $this, 'handleFormEditOutput');
24    }
25
26    public function handleFormEditOutput(Event $event)
27    {
28    }
29
30    public function sectionEditButton(Event $event)
31    {
32        if ($this->shallIgnore($event)) {
33            return;
34        }
35
36        $event->data['name'] = $this->getLang('edit_diagram');
37    }
38
39    public function handleForm(Event $event)
40    {
41        if ($this->shallIgnore($event)) {
42            return;
43        }
44
45        global $TEXT;
46        global $RANGE;
47        global $INPUT;
48
49        if (!$RANGE) {
50            // section editing failed, use default editor instead
51            $event->data['target'] = 'section';
52            return;
53        }
54
55        $event->stopPropagation();
56        $event->preventDefault();
57
58        /** @var Doku_Form $form */
59        $form = &$event->data['form'];
60        $data = base64_encode($TEXT);
61
62        $type = 'bpmn';
63        if ($event->data['target'] === 'plugin_bpmnio_dmn') {
64            $type = 'dmn';
65        }
66
67        $form->setHiddenField('plugin_bpmnio_data', $data);
68        $form->addHTML(<<<HTML
69            <div class="plugin-bpmnio" id="plugin_bpmnio__{$type}_editor">
70                <div class="{$type}_js_data">{$data}</div>
71                <div class="{$type}_js_canvas">
72                    <div class="{$type}_js_container"></div>
73                </div>
74                </div>
75            </div>
76            HTML);
77
78        // used during previews
79        $form->setHiddenField('target', "plugin_bpmnio_{$type}");
80        $form->setHiddenField('range', $RANGE);
81    }
82
83    public function handlePost(Event $event)
84    {
85        global $TEXT;
86        global $INPUT;
87
88        if (!$INPUT->post->has('plugin_bpmnio_data')) {
89            return;
90        }
91
92        $TEXT = base64_decode($INPUT->post->str('plugin_bpmnio_data'));
93    }
94
95    private function shallIgnore(Event $event)
96    {
97        if ($event->data['target'] === 'plugin_bpmnio_bpmn') {
98            return false;
99        }
100        if ($event->data['target'] === 'plugin_bpmnio_dmn') {
101            return false;
102        }
103        return true;
104    }
105}
106