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 HTML); 76 77 // used during previews 78 $form->setHiddenField('target', "plugin_bpmnio_{$type}"); 79 $form->setHiddenField('range', $RANGE); 80 } 81 82 public function handlePost(Event $event) 83 { 84 global $TEXT; 85 global $INPUT; 86 87 if (!$INPUT->post->has('plugin_bpmnio_data')) { 88 return; 89 } 90 91 $TEXT = base64_decode($INPUT->post->str('plugin_bpmnio_data')); 92 } 93 94 private function shallIgnore(Event $event) 95 { 96 if ($event->data['target'] === 'plugin_bpmnio_bpmn') { 97 return false; 98 } 99 if ($event->data['target'] === 'plugin_bpmnio_dmn') { 100 return false; 101 } 102 return true; 103 } 104} 105