Passed
Push — master ( afe190...3d5c82 )
by Michael
02:10
created

action_plugin_prosemirror_editor::forceWYSIWYG()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
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)
24
    {
25
        $controller->register_hook('ACTION_HEADERS_SEND', 'BEFORE', $this, 'forceWYSIWYG');
26
        $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'addDataAndToggleButton');
27
        $controller->register_hook('TPL_ACT_RENDER', 'AFTER', $this, 'addAddtionalForms');
28
    }
29
30
    /**
31
     * If the current user is forced to use the WYSIWYG editor, set the cookie accordingly
32
     *
33
     * Triggered by event: ACTION_HEADERS_SEND
34
     *
35
     * @param Doku_Event $event
36
     * @param            $param
37
     */
38
    public function forceWYSIWYG(Doku_Event $event, $param)
39
    {
40
        if ($this->isForceWYSIWYG()) {
41
            set_doku_pref('plugin_prosemirror_useWYSIWYG', true);
42
        }
43
    }
44
45
    /**
46
     * Add the editor toggle button and, if using the WYSIWYG editor, the instructions rendered to json
47
     *
48
     * Triggered by event: HTML_EDITFORM_OUTPUT
49
     *
50
     * @param Doku_Event $event  event object
51
     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
52
     *                           handler was registered]
53
     *
54
     * @return void
55
     */
56
    public function addDataAndToggleButton(Doku_Event $event, $param)
57
    {
58
        if (!$this->allowWYSIWYG()) {
59
            return;
60
        }
61
62
        /** @var Doku_Form $form */
63
        $form =& $event->data;
64
        $useWYSIWYG = get_doku_pref('plugin_prosemirror_useWYSIWYG', false);
65
66
        $prosemirrorJSON = '';
67
        if ($useWYSIWYG) {
68
            global $TEXT;
69
            $instructions = p_get_instructions($TEXT);
70
            try {
71
                $prosemirrorJSON = p_render('prosemirror', $instructions, $info);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $info seems to be never defined.
Loading history...
72
            } catch (Throwable $e) {
73
                $errorMsg = 'Rendering the page\'s syntax for the WYSIWYG editor failed: ' . $e->getMessage();
74
75
                /** @var \helper_plugin_prosemirror $helper */
76
                $helper = plugin_load('helper', 'prosemirror');
77
                if ($helper->tryToLogErrorToSentry($e, ['text' => $TEXT])) {
78
                    $errorMsg .= ' -- The error has been logged to Sentry.';
79
                }
80
81
                msg($errorMsg, -1);
82
                return;
83
            }
84
        }
85
86
        $form->addElement($this->buildToggleButton());
87
88
        // output data and editor field
89
        $form->addHidden('prosemirror_json',$prosemirrorJSON);
90
        $form->insertElement(1, '<div class="prosemirror_wrapper" id="prosemirror__editor"></div>');
91
    }
92
93
    /**
94
     * Create the button to toggle the WYSIWYG editor
95
     *
96
     * Creates it as hidden if forcing WYSIWYG
97
     *
98
     * @return array the pseudo-tag expected by \Doku_Form::addElement
99
     */
100
    protected function buildToggleButton()
101
    {
102
        $attr = [
103
            'class' => 'button plugin_prosemirror_useWYSIWYG'
104
        ];
105
        if ($this->isForceWYSIWYG()) {
106
            $attr['style'] = 'display: none;';
107
        }
108
        return form_makeButton('button', '', $this->getLang('switch_editors'), $attr);
109
    }
110
111
    /**
112
     * Determine if the current user is forced to use the WYSIWYG editor
113
     *
114
     * @return bool
115
     */
116
    protected function isForceWYSIWYG()
117
    {
118
        return $this->getConf('forceWYSIWYG') && !auth_ismanager();
119
    }
120
121
    /**
122
     * Forbid using WYSIWYG editor when editing anything else then sections or the entire page
123
     *
124
     * This would be the case for the edittable editor or the editor of the data plugin
125
     *
126
     * @return bool
127
     */
128
    protected function allowWYSIWYG()
129
    {
130
        global $INPUT;
131
        return !$INPUT->has('target') || $INPUT->str('target') === 'section';
132
    }
133
134
    public function addAddtionalForms(Doku_Event $event)
135
    {
136
        if (!$this->allowWYSIWYG()) {
137
            return;
138
        }
139
140
        if (!in_array($event->data, ['edit', 'preview'])) {
141
            return;
142
        }
143
144
        $linkForm = new dokuwiki\Form\Form([
145
            'class' => 'plugin_prosemirror_linkform',
146
            'id' => 'prosemirror-linkform',
147
            'style' => 'display: none;',
148
        ]);
149
        $linkForm->addFieldsetOpen('Links')->addClass('js-link-fieldset');;
150
        $iwOptions = array_keys(getInterwiki());
151
        $linkForm->addDropdown('iwshortcut', $iwOptions, 'InterWiki')->attr('required', 'required');
152
153
        $linkForm->addButton('linkwiz', '⛓️')->attrs([
154
            'type' => 'button',
155
            'class' => 'js-open-linkwiz linkform_linkwiz'
156
        ]);
157
        $linkForm->addTextInput('linktarget', $this->getLang('link target'))->attrs(
158
            [
159
            'required'=> 'required',
160
            'autofocus' => 'autofocus',
161
            ]
162
        );
163
164
        $linkForm->addTagOpen('div')->addClass('radio-wrapper');
165
        $linkForm->addTagOpen('fieldset');
166
        $linkForm->addTagOpen('legend');
167
        $linkForm->addHTML('Link Type');
168
        $linkForm->addTagClose('legend');
169
        $linkForm->addRadioButton('linktype', $this->getLang('type:wiki page'))->val('internallink');
170
        $linkForm->addRadioButton('linktype', $this->getLang('type:interwiki'))->val('interwikilink');
171
        $linkForm->addRadioButton('linktype', $this->getLang('type:email'))->val('emaillink');
172
        $linkForm->addRadioButton('linktype', $this->getLang('type:external'))->val('externallink')->attr('checked', 'checked');
173
        $linkForm->addRadioButton('linktype', $this->getLang('type:other'))->val('other');
174
        $linkForm->addTagClose('fieldset');
175
        $linkForm->addTagClose('div');
176
177
        $linkForm->addTagOpen('div')->addClass('radio-wrapper');
178
        $linkForm->addTagOpen('fieldset');
179
        $linkForm->addTagOpen('legend');
180
        $linkForm->addHTML('Link Name Type');
181
        $linkForm->addTagClose('legend');
182
        $linkForm->addRadioButton('nametype', $this->getLang('type:automatic title'))->val('automatic')->attr('checked', 'checked');
183
        $linkForm->addRadioButton('nametype', $this->getLang('type:custom title'))->val('custom');
184
        $linkForm->addRadioButton('nametype', $this->getLang('type:image'))->val('image');
185
        $linkForm->addTextInput('linkname', 'Link name')->attr('placeholder', $this->getLang('placeholder:link name'));
186
        $linkForm->addTagOpen('div')->addClass('js-media-wrapper');
187
        $linkForm->addTagClose('div');
188
        $linkForm->addTagClose('fieldset');
189
        $linkForm->addTagClose('div');
190
191
192
        $linkForm->addFieldsetClose();
193
        $linkForm->addButton('ok-button', 'OK')->attr('type', 'submit');
194
        $linkForm->addButton('cancel-button', $this->getLang('cancel'))->attr('type', 'button');
195
196
        echo $linkForm->toHTML();
197
198
        $mediaForm = new dokuwiki\Form\Form([
199
            'class' => 'plugin_prosemirror_mediaform',
200
            'id' => 'prosemirror-mediaform',
201
            'style' => 'display: none;',
202
        ]);
203
        $mediaForm->addFieldsetOpen($this->getLang('legend:media'))->addClass('js-media-fieldset');
204
        $mediaForm->addButton('mediamanager', '🖼️')->attrs([
205
            'type' => 'button',
206
            'class' => 'js-open-mediamanager mediaform_mediamanager'
207
        ]);
208
        $mediaForm->addTextInput('mediatarget', $this->getLang('media target'))->attrs(
209
            [
210
                'required'=> 'required',
211
                'autofocus' => 'autofocus',
212
            ]
213
        );
214
        $mediaForm->addTextInput('mediacaption', $this->getLang('label:caption'));
215
        $mediaForm->addTextInput('width', $this->getLang('label:width'))->attr('type', 'number');
216
        $mediaForm->addTextInput('height', $this->getLang('label:height'))->attr('type', 'number');
217
218
        $mediaForm->addTagOpen('div')->addClass('radio-wrapper');
219
        $mediaForm->addTagOpen('fieldset');
220
        $mediaForm->addTagOpen('legend');
221
        $mediaForm->addHTML($this->getLang('legend:alignment'));
222
        $mediaForm->addTagClose('legend');
223
        $mediaForm->addRadioButton('alignment', $this->getLang('label:default alignment'))->val('')->attr('checked', 'checked');
224
        $mediaForm->addRadioButton('alignment', $this->getLang('label:float left'))->val('left');
225
        $mediaForm->addRadioButton('alignment', $this->getLang('label:center alignment'))->val('center');
226
        $mediaForm->addRadioButton('alignment', $this->getLang('label:float right'))->val('right');
227
        $mediaForm->addTagClose('fieldset');
228
        $mediaForm->addTagClose('div');
229
230
        $mediaForm->addTagOpen('div')->addClass('radio-wrapper');
231
        $mediaForm->addTagOpen('fieldset');
232
        $mediaForm->addTagOpen('legend');
233
        $mediaForm->addHTML($this->getLang('legend:linking'));
234
        $mediaForm->addTagClose('legend');
235
        $mediaForm->addRadioButton('linking', $this->getLang('label:default linking'))->val('details')->attr('checked', 'checked');
236
        $mediaForm->addRadioButton('linking', $this->getLang('label:direct linking'))->val('direct');
237
        $mediaForm->addRadioButton('linking', $this->getLang('label:nolink'))->val('nolink');
238
        $mediaForm->addRadioButton('linking', $this->getLang('label:linkonly'))->val('linkonly');
239
        $mediaForm->addTagClose('fieldset');
240
        $mediaForm->addTagClose('div');
241
242
        $mediaForm->addTagOpen('div')->addClass('radio-wrapper');
243
        $mediaForm->addTagOpen('fieldset');
244
        $mediaForm->addTagOpen('legend');
245
        $mediaForm->addHTML($this->getLang('legend:caching'));
246
        $mediaForm->addTagClose('legend');
247
        $mediaForm->addRadioButton('caching', $this->getLang('label:default caching'))->val('')->attr('checked', 'checked');
248
        $mediaForm->addRadioButton('caching', $this->getLang('label:recache'))->val('recache');
249
        $mediaForm->addRadioButton('caching', $this->getLang('label:nocache'))->val('nocache');
250
        $mediaForm->addTagClose('fieldset');
251
        $mediaForm->addTagClose('div');
252
253
        $mediaForm->addFieldsetClose();
254
        $mediaForm->addButton('ok-button', 'OK')->attr('type', 'submit');
255
        $mediaForm->addButton('cancel-button', $this->getLang('cancel'))->attr('type', 'button');
256
257
        // dynamic image hack? https://www.dokuwiki.org/images#dynamic_images
258
259
        echo $mediaForm->toHTML();
260
261
        // phpcs:disable
262
        $languages = explode(' ', '4cs 6502acme 6502kickass 6502tasm 68000devpac abap actionscript3 actionscript ada aimms algol68 apache applescript apt_sources arm asm asp asymptote autoconf autohotkey autoit avisynth awk bascomavr bash basic4gl batch bf biblatex bibtex blitzbasic bnf boo caddcl cadlisp ceylon cfdg cfm chaiscript chapel cil c_loadrunner clojure c_mac cmake cobol coffeescript c cpp cpp-qt cpp-winapi csharp css cuesheet c_winapi dart dcl dcpu16 dcs delphi diff div dos dot d ecmascript eiffel email epc e erlang euphoria ezt f1 falcon fo fortran freebasic freeswitch fsharp gambas gdb genero genie gettext glsl gml gnuplot go groovy gwbasic haskell haxe hicest hq9plus html html4strict html5 icon idl ini inno intercal io ispfpanel java5 java javascript jcl j jquery julia kixtart klonec klonecpp kotlin latex lb ldif lisp llvm locobasic logtalk lolcode lotusformulas lotusscript lscript lsl2 lua m68k magiksf make mapbasic mathematica matlab mercury metapost mirc mk-61 mmix modula2 modula3 mpasm mxml mysql nagios netrexx newlisp nginx nimrod nsis oberon2 objc objeck ocaml-brief ocaml octave oobas oorexx oracle11 oracle8 oxygene oz parasail parigp pascal pcre perl6 perl per pf phix php-brief php pic16 pike pixelbender pli plsql postgresql postscript povray powerbuilder powershell proftpd progress prolog properties providex purebasic pycon pys60 python qbasic qml q racket rails rbs rebol reg rexx robots rpmspec rsplus ruby rust sas sass scala scheme scilab scl sdlbasic smalltalk smarty spark sparql sql standardml stonescript swift systemverilog tclegg tcl teraterm texgraph text thinbasic tsql twig typoscript unicon upc urbi uscript vala vbnet vb vbscript vedit verilog vhdl vim visualfoxpro visualprolog whitespace whois winbatch xbasic xml xojo xorg_conf xpp yaml z80 zxbasic');
263
        // phpcs:enable
264
        $datalistHTML = '<datalist id="codelanguages">';
265
        foreach ($languages as $language) {
266
            $datalistHTML .= "<option value=\"$language\">";
267
        }
268
        $datalistHTML .= '</datalist>';
269
        echo $datalistHTML;
270
    }
271
}
272
273
// vim:ts=4:sw=4:et:
274