xref: /plugin/bpmnio/action/editor.php (revision a494ddcc62e5b9ad640d30acec0360d3b24cbd8a)
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    }
24
25    public function sectionEditButton(Event $event)
26    {
27        if ($this->shallIgnore($event)) return;
28
29        $event->data['name'] = $this->getLang('edit_diagram');
30    }
31
32    public function handleForm(Event $event)
33    {
34        if ($this->shallIgnore($event)) return;
35
36        global $TEXT;
37        global $RANGE;
38        global $INPUT;
39
40        if (!$RANGE) {
41            // section editing failed, use default editor instead
42            $event->data['target'] = 'section';
43            return;
44        }
45
46        $event->stopPropagation();
47        $event->preventDefault();
48
49        /** @var Doku_Form $form */
50        $form = &$event->data['form'];
51        $data = base64_encode($TEXT);
52
53        $type = 'bpmn';
54        if ($event->data['target'] === 'plugin_bpmnio_dmn')
55            $type = 'dmn';
56
57        $form->setHiddenField('plugin_bpmnio_data', $data);
58        $form->addHTML(<<<HTML
59            <div class="plugin-bpmnio" id="plugin_bpmnio__{$type}_editor">
60                <div class="{$type}_js_data">{$data}</div>
61                <div class="{$type}_js_canvas">
62                    <div class="{$type}_js_container"></div>
63                </div>
64                </div>
65            </div>
66            HTML);
67
68        // used during previews
69        $form->setHiddenField('target', "plugin_bpmnio_{$type}");
70        $form->setHiddenField('range', $RANGE);
71    }
72
73    public function handlePost(Event $event)
74    {
75        global $TEXT;
76        global $INPUT;
77
78        if (!$INPUT->post->has('plugin_bpmnio_data')) return;
79
80        $TEXT = base64_decode($INPUT->post->str('plugin_bpmnio_data'));
81    }
82
83    private function shallIgnore(Event $event)
84    {
85        if ($event->data['target'] === 'plugin_bpmnio_bpmn')
86            return false;
87        if ($event->data['target'] === 'plugin_bpmnio_dmn')
88            return false;
89        return true;
90    }
91}
92