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)
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        /** @var Doku_Form $form */
29        $form = &$event->data;
30
31        // TODO: commented out for now, as it seems to create trouble for users
32        // remove the preview button, as it does not work with our editor
33        // $previewButtonPosition = $form->findPositionByAttribute('id', 'edbtn__preview');
34        // if ($previewButtonPosition !== false) {
35        //     $form->removeElement($previewButtonPosition);
36        // }
37    }
38
39    public function sectionEditButton(Event $event)
40    {
41        if ($this->shallIgnore($event)) return;
42
43        $event->data['name'] = $this->getLang('edit_diagram');
44    }
45
46    public function handleForm(Event $event)
47    {
48        if ($this->shallIgnore($event)) return;
49
50        global $TEXT;
51        global $RANGE;
52        global $INPUT;
53
54        if (!$RANGE) {
55            // section editing failed, use default editor instead
56            $event->data['target'] = 'section';
57            return;
58        }
59
60        $event->stopPropagation();
61        $event->preventDefault();
62
63        /** @var Doku_Form $form */
64        $form = &$event->data['form'];
65        $data = base64_encode($TEXT);
66
67        $type = 'bpmn';
68        if ($event->data['target'] === 'plugin_bpmnio_dmn')
69            $type = 'dmn';
70
71        $form->setHiddenField('plugin_bpmnio_data', $data);
72        $form->addHTML(<<<HTML
73            <div class="plugin-bpmnio" id="plugin_bpmnio__{$type}_editor">
74                <div class="{$type}_js_data">{$data}</div>
75                <div class="{$type}_js_canvas">
76                    <div class="{$type}_js_container"></div>
77                </div>
78                </div>
79            </div>
80            HTML);
81
82        // used during previews
83        $form->setHiddenField('target', "plugin_bpmnio_{$type}");
84        $form->setHiddenField('range', $RANGE);
85    }
86
87    public function handlePost(Event $event)
88    {
89        global $TEXT;
90        global $INPUT;
91
92        if (!$INPUT->post->has('plugin_bpmnio_data')) return;
93
94        $TEXT = base64_decode($INPUT->post->str('plugin_bpmnio_data'));
95    }
96
97    private function shallIgnore(Event $event)
98    {
99        if ($event->data['target'] === 'plugin_bpmnio_bpmn')
100            return false;
101        if ($event->data['target'] === 'plugin_bpmnio_dmn')
102            return false;
103        return true;
104    }
105}
106