xref: /plugin/bpmnio/action/editor.php (revision 738a44ff661500d783f23960888bd3ae2818f44c)
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    private function loadLinkProcessor(): void
19    {
20        require_once __DIR__ . '/../inc/link_processor.php';
21    }
22
23    public function register(EventHandler $controller): void
24    {
25        $controller->register_hook('HTML_SECEDIT_BUTTON', 'BEFORE', $this, 'sectionEditButton');
26        $controller->register_hook('EDIT_FORM_ADDTEXTAREA', 'BEFORE', $this, 'handleForm');
27        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handlePost');
28        $controller->register_hook('FORM_EDIT_OUTPUT', 'BEFORE', $this, 'handleFormEditOutput');
29    }
30
31    public function handleFormEditOutput(Event $event)
32    {
33    }
34
35    public function sectionEditButton(Event $event)
36    {
37        if ($this->shallIgnore($event)) {
38            return;
39        }
40
41        $event->data['name'] = $this->getLang('edit_diagram');
42    }
43
44    public function handleForm(Event $event)
45    {
46        if ($this->shallIgnore($event)) {
47            return;
48        }
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        $this->loadLinkProcessor();
67        $payload = plugin_bpmnio_link_processor::buildPayload($TEXT);
68        $renderData = base64_encode($payload['xml']);
69        $linkData = base64_encode(json_encode($payload['links']));
70
71        $type = 'bpmn';
72        if ($event->data['target'] === 'plugin_bpmnio_dmn') {
73            $type = 'dmn';
74        }
75
76        $form->setHiddenField('plugin_bpmnio_data', $data);
77        $form->setHiddenField('plugin_bpmnio_links', $linkData);
78
79        $lintAttr = '';
80        if ($type === 'bpmn') {
81            $allowed = ['on', 'off', 'inactive'];
82            $lint = strtolower((string) $this->getConf('lint'));
83            if (!in_array($lint, $allowed, true)) {
84                $lint = 'inactive';
85            }
86            // In the editor the linter toggle must always be available so authors
87            // can inspect issues. "off" is promoted to "inactive" (button present
88            // but overlays start hidden); "inactive" and "on" pass through as-is.
89            if ($lint === 'off') {
90                $lint = 'inactive';
91            }
92            $lintAttr = " data-lint=\"{$lint}\"";
93        }
94
95        $form->addHTML(<<<HTML
96            <div class="plugin-bpmnio" id="plugin_bpmnio__{$type}_editor">
97                <div class="{$type}_js_data">{$renderData}</div>
98                <div class="{$type}_js_links">{$linkData}</div>
99                <div class="{$type}_js_canvas">
100                    <div class="{$type}_js_container"{$lintAttr}></div>
101                </div>
102            </div>
103            HTML);
104
105        // used during previews
106        $form->setHiddenField('target', "plugin_bpmnio_{$type}");
107        $form->setHiddenField('range', $RANGE);
108    }
109
110    public function handlePost(Event $event)
111    {
112        global $TEXT;
113        global $INPUT;
114
115        if (!$INPUT->post->has('plugin_bpmnio_data')) {
116            return;
117        }
118
119        $TEXT = base64_decode($INPUT->post->str('plugin_bpmnio_data'));
120    }
121
122    private function shallIgnore(Event $event)
123    {
124        if ($event->data['target'] === 'plugin_bpmnio_bpmn') {
125            return false;
126        }
127        if ($event->data['target'] === 'plugin_bpmnio_dmn') {
128            return false;
129        }
130        return true;
131    }
132}
133