Completed
Branch master (28f58d)
by Michael
02:42
created

action_plugin_prosemirror_editor   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 200
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 200
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
B output_editor() 0 49 5
B addAddtionalForms() 0 117 3
A allowWYSIWYG() 0 4 2
1
<?php
2
/**
3
 * DokuWiki Plugin prosemirror (Action Component)
4
 *
5
 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6
 * @author  Andreas Gohr <[email protected]>
7
 */
8
9
// must be run within Dokuwiki
10
if (!defined('DOKU_INC')) {
11
    die();
12
}
13
14
class action_plugin_prosemirror_editor extends DokuWiki_Action_Plugin
1 ignored issue
show
Bug introduced by
The type DokuWiki_Action_Plugin was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
{
16
    /**
17
     * Registers a callback function for a given event
18
     *
19
     * @param Doku_Event_Handler $controller DokuWiki's event controller object
20
     *
21
     * @return void
22
     */
23
    public function register(Doku_Event_Handler $controller)
1 ignored issue
show
Bug introduced by
The type Doku_Event_Handler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
    {
25
        $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'output_editor');
26
        $controller->register_hook('TPL_ACT_RENDER', 'AFTER', $this, 'addAddtionalForms');
27
    }
28
29
    /**
30
     * [Custom event handler which performs action]
31
     *
32
     * Triggered by event: HTML_EDITFORM_OUTPUT
33
     *
34
     * @param Doku_Event $event  event object
35
     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
36
     *                           handler was registered]
37
     *
38
     * @return void
39
     */
40
    public function output_editor(Doku_Event $event, $param)
1 ignored issue
show
Bug introduced by
The type Doku_Event was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
41
    {
42
        if (!$this->allowWYSIWYG()) {
43
            return;
44
        }
45
46
47
        /** @var Doku_Form $form */
48
        $form =& $event->data;
49
        $useWYSIWYG = get_doku_pref('plugin_prosemirror_useWYSIWYG', false);
1 ignored issue
show
Bug introduced by
The function get_doku_pref was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
        $useWYSIWYG = /** @scrutinizer ignore-call */ get_doku_pref('plugin_prosemirror_useWYSIWYG', false);
Loading history...
50
51
        if (!$useWYSIWYG) {
52
            $attr = [
53
                'class' => 'button plugin_prosemirror_useWYSIWYG'
54
            ];
55
            $form->addElement(form_makeButton('submit', 'preview', 'Preview and use WYSIWYG-Editor', $attr));
1 ignored issue
show
Bug introduced by
The function form_makeButton was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
            $form->addElement(/** @scrutinizer ignore-call */ form_makeButton('submit', 'preview', 'Preview and use WYSIWYG-Editor', $attr));
Loading history...
56
            return;
57
        }
58
59
        global $TEXT;
60
        $instructions = p_get_instructions($TEXT);
1 ignored issue
show
Bug introduced by
The function p_get_instructions was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
        $instructions = /** @scrutinizer ignore-call */ p_get_instructions($TEXT);
Loading history...
61
        try {
62
            $prosemirrorJSON =  p_render('prosemirror', $instructions, $info);
1 ignored issue
show
Comprehensibility Best Practice introduced by
The variable $info seems to be never defined.
Loading history...
Bug introduced by
The function p_render was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
            $prosemirrorJSON =  /** @scrutinizer ignore-call */ p_render('prosemirror', $instructions, $info);
Loading history...
63
        } catch (Throwable $e) {
64
            $errorMsg = 'Rendering the page\'s syntax for the WYSIWYG editor failed';
65
66
            /** @var helper_plugin_sentry $sentry */
67
            $sentry = plugin_load('helper', 'sentry');
1 ignored issue
show
Bug introduced by
The function plugin_load was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
            $sentry = /** @scrutinizer ignore-call */ plugin_load('helper', 'sentry');
Loading history...
68
            if ($sentry) {
0 ignored issues
show
introduced by
$sentry is of type helper_plugin_sentry, thus it always evaluated to true. If $sentry can have other possible types, add them to action/editor.php:66
Loading history...
69
                $sentry->logException($e);
70
                $errorMsg .= ' Error has been logged to Sentry.';
71
            }
72
73
            msg($errorMsg, -1);
1 ignored issue
show
Bug introduced by
The function msg was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
            /** @scrutinizer ignore-call */ 
74
            msg($errorMsg, -1);
Loading history...
74
            return;
75
        }
76
77
        $event->stopPropagation();
78
        $event->preventDefault();
79
80
        $attr = [
81
            'class' => 'button plugin_prosemirror_useWYSIWYG'
82
        ];
83
        $form->addElement(form_makeButton('submit', 'preview', 'Preview and use Syntax-Editor', $attr));
84
85
86
        // output data and editor field
87
        $form->addHidden('prosemirror_json',$prosemirrorJSON);
88
        $form->insertElement(1, '<div id="prosemirror__editor"></div>');
89
    }
90
91
    protected function allowWYSIWYG()
92
    {
93
        global $INPUT;
94
        return !$INPUT->has('target') || $INPUT->str('target') === 'section';
95
    }
96
97
    public function addAddtionalForms(Doku_Event $event)
98
    {
99
        if (!$this->allowWYSIWYG()) {
100
            return;
101
        }
102
103
        if (!in_array($event->data, ['edit', 'preview'])) {
104
            return;
105
        }
106
107
        $linkForm = new dokuwiki\Form\Form([
1 ignored issue
show
Bug introduced by
The type dokuwiki\Form\Form was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
108
            'class' => 'plugin_prosemirror_linkform',
109
            'id' => 'prosemirror-linkform',
110
            'style' => 'display: none;',
111
        ]);
112
        $linkForm->addFieldsetOpen('Links')->addClass('js-link-fieldset');;
113
        $iwOptions = array_keys(getInterwiki());
1 ignored issue
show
Bug introduced by
The function getInterwiki was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

113
        $iwOptions = array_keys(/** @scrutinizer ignore-call */ getInterwiki());
Loading history...
114
        $linkForm->addDropdown('iwshortcut', $iwOptions, 'InterWiki')->attr('required', 'required');
115
        $linkForm->addTextInput('linktarget', 'Link target')->attrs(
116
            [
117
            'required'=> 'required',
118
            'autofocus' => 'autofocus',
119
            ]
120
        );
121
122
        $linkForm->addTagOpen('div')->addClass('radio-wrapper');
123
        $linkForm->addTagOpen('fieldset');
124
        $linkForm->addTagOpen('legend');
125
        $linkForm->addHTML('Link Type');
126
        $linkForm->addTagClose('legend');
127
        $linkForm->addRadioButton('linktype', 'Wiki page')->val('internallink');
128
        $linkForm->addRadioButton('linktype', 'Interwiki')->val('interwikilink');
129
        $linkForm->addRadioButton('linktype', 'email')->val('emaillink');
130
        $linkForm->addRadioButton('linktype', 'external')->val('externallink')->attr('checked', 'checked');
131
        $linkForm->addRadioButton('linktype', 'Other')->val('other');
132
        $linkForm->addTagClose('fieldset');
133
        $linkForm->addTagClose('div');
134
135
        $linkForm->addTagOpen('div')->addClass('radio-wrapper');
136
        $linkForm->addTagOpen('fieldset');
137
        $linkForm->addTagOpen('legend');
138
        $linkForm->addHTML('Link Name Type');
139
        $linkForm->addTagClose('legend');
140
        $linkForm->addRadioButton('nametype', 'automatic')->val('automatic')->attr('checked', 'checked');
141
        $linkForm->addRadioButton('nametype', 'custom')->val('custom');
142
        $linkForm->addRadioButton('nametype', 'image')->val('image');
143
        $linkForm->addTextInput('linkname', 'Link name')->attr('placeholder', '(automatic)');
144
        $linkForm->addTagOpen('div')->addClass('js-media-wrapper');
145
        $linkForm->addTagClose('div');
146
        $linkForm->addTagClose('fieldset');
147
        $linkForm->addTagClose('div');
148
149
150
        $linkForm->addFieldsetClose();
151
        $linkForm->addButton('ok-button', 'OK')->attr('type', 'submit');
152
        $linkForm->addButton('cancel-button', 'Cancel')->attr('type', 'button');
153
154
        echo $linkForm->toHTML();
155
156
        $mediaForm = new dokuwiki\Form\Form([
157
            'class' => 'plugin_prosemirror_mediaform',
158
            'id' => 'prosemirror-mediaform',
159
            'style' => 'display: none;',
160
        ]);
161
        $mediaForm->addFieldsetOpen('Media')->addClass('js-media-fieldset');
162
        $mediaForm->addTextInput('mediatarget', 'Media')->attrs(
163
            [
164
                'required'=> 'required',
165
                'autofocus' => 'autofocus',
166
            ]
167
        );
168
        $mediaForm->addTextInput('mediacaption', 'Caption');
169
        $mediaForm->addTextInput('width', 'Width (px)')->attr('type', 'number');
170
        $mediaForm->addTextInput('height', 'Height (px)')->attr('type', 'number');
171
172
        $mediaForm->addTagOpen('div')->addClass('radio-wrapper');
173
        $mediaForm->addTagOpen('fieldset');
174
        $mediaForm->addTagOpen('legend');
175
        $mediaForm->addHTML('Alignment');
176
        $mediaForm->addTagClose('legend');
177
        $mediaForm->addRadioButton('alignment', 'default')->val('')->attr('checked', 'checked');
178
        $mediaForm->addRadioButton('alignment', 'float left')->val('left');
179
        $mediaForm->addRadioButton('alignment', 'center')->val('center');
180
        $mediaForm->addRadioButton('alignment', 'float right')->val('right');
181
        $mediaForm->addTagClose('fieldset');
182
        $mediaForm->addTagClose('div');
183
184
        $mediaForm->addTagOpen('div')->addClass('radio-wrapper');
185
        $mediaForm->addTagOpen('fieldset');
186
        $mediaForm->addTagOpen('legend');
187
        $mediaForm->addHTML('Linking');
188
        $mediaForm->addTagClose('legend');
189
        $mediaForm->addRadioButton('linking', 'default')->val('details')->attr('checked', 'checked');
190
        $mediaForm->addRadioButton('linking', 'direct')->val('direct');
191
        $mediaForm->addRadioButton('linking', 'nolink')->val('nolink');
192
        $mediaForm->addRadioButton('linking', 'linkonly')->val('linkonly');
193
        $mediaForm->addTagClose('fieldset');
194
        $mediaForm->addTagClose('div');
195
196
        $mediaForm->addTagOpen('div')->addClass('radio-wrapper');
197
        $mediaForm->addTagOpen('fieldset');
198
        $mediaForm->addTagOpen('legend');
199
        $mediaForm->addHTML('Caching');
200
        $mediaForm->addTagClose('legend');
201
        $mediaForm->addRadioButton('caching', 'default')->val('')->attr('checked', 'checked');
202
        $mediaForm->addRadioButton('caching', 'recache')->val('recache');
203
        $mediaForm->addRadioButton('caching', 'nocache')->val('nocache');
204
        $mediaForm->addTagClose('fieldset');
205
        $mediaForm->addTagClose('div');
206
207
        $mediaForm->addFieldsetClose();
208
        $mediaForm->addButton('ok-button', 'OK')->attr('type', 'submit');
209
        $mediaForm->addButton('cancel-button', 'Cancel')->attr('type', 'button');
210
211
        // dynamic image hack? https://www.dokuwiki.org/images#dynamic_images
212
213
        echo $mediaForm->toHTML();
214
    }
215
}
216
217
// vim:ts=4:sw=4:et:
218