Completed
Push — master ( 3b8b0c...578b04 )
by Michael
02:39
created

addDataAndToggleButton()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 22
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 38
rs 9.2568
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('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'addDataAndToggleButton');
26
        $controller->register_hook('TPL_ACT_RENDER', 'AFTER', $this, 'addAddtionalForms');
27
    }
28
29
    /**
30
     * Add the editor toggle button and, if using the WYSIWYG editor, the instructions rendered to json
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 addDataAndToggleButton(Doku_Event $event, $param)
41
    {
42
        if (!$this->allowWYSIWYG()) {
43
            return;
44
        }
45
46
        /** @var Doku_Form $form */
47
        $form =& $event->data;
48
        $useWYSIWYG = get_doku_pref('plugin_prosemirror_useWYSIWYG', false);
49
50
        $prosemirrorJSON = '';
51
        if ($useWYSIWYG) {
52
            global $TEXT;
53
            $instructions = p_get_instructions($TEXT);
54
            try {
55
                $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...
56
            } catch (Throwable $e) {
57
                $errorMsg = 'Rendering the page\'s syntax for the WYSIWYG editor failed: ' . $e->getMessage();
58
59
                /** @var \helper_plugin_prosemirror $helper */
60
                $helper = plugin_load('helper', 'prosemirror');
61
                if ($helper->tryToLogErrorToSentry($e, ['text' => $TEXT])) {
62
                    $errorMsg .= ' -- The error has been logged to Sentry.';
63
                }
64
65
                msg($errorMsg, -1);
66
                return;
67
            }
68
        }
69
70
        $attr = [
71
            'class' => 'button plugin_prosemirror_useWYSIWYG'
72
        ];
73
        $form->addElement(form_makeButton('button', '', $this->getLang('switch_editors'), $attr));
74
75
        // output data and editor field
76
        $form->addHidden('prosemirror_json',$prosemirrorJSON);
77
        $form->insertElement(1, '<div class="prosemirror_wrapper" id="prosemirror__editor"></div>');
78
    }
79
80
    /**
81
     * Forbid using WYSIWYG editor when editing anything else then sections or the entire page
82
     *
83
     * This would be the case for the edittable editor or the editor of the data plugin
84
     *
85
     * @return bool
86
     */
87
    protected function allowWYSIWYG()
88
    {
89
        global $INPUT;
90
        return !$INPUT->has('target') || $INPUT->str('target') === 'section';
91
    }
92
93
    public function addAddtionalForms(Doku_Event $event)
94
    {
95
        if (!$this->allowWYSIWYG()) {
96
            return;
97
        }
98
99
        if (!in_array($event->data, ['edit', 'preview'])) {
100
            return;
101
        }
102
103
        $linkForm = new dokuwiki\Form\Form([
104
            'class' => 'plugin_prosemirror_linkform',
105
            'id' => 'prosemirror-linkform',
106
            'style' => 'display: none;',
107
        ]);
108
        $linkForm->addFieldsetOpen('Links')->addClass('js-link-fieldset');;
109
        $iwOptions = array_keys(getInterwiki());
110
        $linkForm->addDropdown('iwshortcut', $iwOptions, 'InterWiki')->attr('required', 'required');
111
112
        $linkForm->addButton('linkwiz', '⛓️')->attrs([
113
            'type' => 'button',
114
            'class' => 'js-open-linkwiz linkform_linkwiz'
115
        ]);
116
        $linkForm->addTextInput('linktarget', 'Link target')->attrs(
117
            [
118
            'required'=> 'required',
119
            'autofocus' => 'autofocus',
120
            ]
121
        );
122
123
        $linkForm->addTagOpen('div')->addClass('radio-wrapper');
124
        $linkForm->addTagOpen('fieldset');
125
        $linkForm->addTagOpen('legend');
126
        $linkForm->addHTML('Link Type');
127
        $linkForm->addTagClose('legend');
128
        $linkForm->addRadioButton('linktype', 'Wiki page')->val('internallink');
129
        $linkForm->addRadioButton('linktype', 'Interwiki')->val('interwikilink');
130
        $linkForm->addRadioButton('linktype', 'email')->val('emaillink');
131
        $linkForm->addRadioButton('linktype', 'external')->val('externallink')->attr('checked', 'checked');
132
        $linkForm->addRadioButton('linktype', 'Other')->val('other');
133
        $linkForm->addTagClose('fieldset');
134
        $linkForm->addTagClose('div');
135
136
        $linkForm->addTagOpen('div')->addClass('radio-wrapper');
137
        $linkForm->addTagOpen('fieldset');
138
        $linkForm->addTagOpen('legend');
139
        $linkForm->addHTML('Link Name Type');
140
        $linkForm->addTagClose('legend');
141
        $linkForm->addRadioButton('nametype', 'automatic')->val('automatic')->attr('checked', 'checked');
142
        $linkForm->addRadioButton('nametype', 'custom')->val('custom');
143
        $linkForm->addRadioButton('nametype', 'image')->val('image');
144
        $linkForm->addTextInput('linkname', 'Link name')->attr('placeholder', '(automatic)');
145
        $linkForm->addTagOpen('div')->addClass('js-media-wrapper');
146
        $linkForm->addTagClose('div');
147
        $linkForm->addTagClose('fieldset');
148
        $linkForm->addTagClose('div');
149
150
151
        $linkForm->addFieldsetClose();
152
        $linkForm->addButton('ok-button', 'OK')->attr('type', 'submit');
153
        $linkForm->addButton('cancel-button', 'Cancel')->attr('type', 'button');
154
155
        echo $linkForm->toHTML();
156
157
        $mediaForm = new dokuwiki\Form\Form([
158
            'class' => 'plugin_prosemirror_mediaform',
159
            'id' => 'prosemirror-mediaform',
160
            'style' => 'display: none;',
161
        ]);
162
        $mediaForm->addFieldsetOpen('Media')->addClass('js-media-fieldset');
163
        $mediaForm->addButton('mediamanager', '🖼️')->attrs([
164
            'type' => 'button',
165
            'class' => 'js-open-mediamanager mediaform_mediamanager'
166
        ]);
167
        $mediaForm->addTextInput('mediatarget', 'Media')->attrs(
168
            [
169
                'required'=> 'required',
170
                'autofocus' => 'autofocus',
171
            ]
172
        );
173
        $mediaForm->addTextInput('mediacaption', 'Caption');
174
        $mediaForm->addTextInput('width', 'Width (px)')->attr('type', 'number');
175
        $mediaForm->addTextInput('height', 'Height (px)')->attr('type', 'number');
176
177
        $mediaForm->addTagOpen('div')->addClass('radio-wrapper');
178
        $mediaForm->addTagOpen('fieldset');
179
        $mediaForm->addTagOpen('legend');
180
        $mediaForm->addHTML('Alignment');
181
        $mediaForm->addTagClose('legend');
182
        $mediaForm->addRadioButton('alignment', 'default')->val('')->attr('checked', 'checked');
183
        $mediaForm->addRadioButton('alignment', 'float left')->val('left');
184
        $mediaForm->addRadioButton('alignment', 'center')->val('center');
185
        $mediaForm->addRadioButton('alignment', 'float right')->val('right');
186
        $mediaForm->addTagClose('fieldset');
187
        $mediaForm->addTagClose('div');
188
189
        $mediaForm->addTagOpen('div')->addClass('radio-wrapper');
190
        $mediaForm->addTagOpen('fieldset');
191
        $mediaForm->addTagOpen('legend');
192
        $mediaForm->addHTML('Linking');
193
        $mediaForm->addTagClose('legend');
194
        $mediaForm->addRadioButton('linking', 'default')->val('details')->attr('checked', 'checked');
195
        $mediaForm->addRadioButton('linking', 'direct')->val('direct');
196
        $mediaForm->addRadioButton('linking', 'nolink')->val('nolink');
197
        $mediaForm->addRadioButton('linking', 'linkonly')->val('linkonly');
198
        $mediaForm->addTagClose('fieldset');
199
        $mediaForm->addTagClose('div');
200
201
        $mediaForm->addTagOpen('div')->addClass('radio-wrapper');
202
        $mediaForm->addTagOpen('fieldset');
203
        $mediaForm->addTagOpen('legend');
204
        $mediaForm->addHTML('Caching');
205
        $mediaForm->addTagClose('legend');
206
        $mediaForm->addRadioButton('caching', 'default')->val('')->attr('checked', 'checked');
207
        $mediaForm->addRadioButton('caching', 'recache')->val('recache');
208
        $mediaForm->addRadioButton('caching', 'nocache')->val('nocache');
209
        $mediaForm->addTagClose('fieldset');
210
        $mediaForm->addTagClose('div');
211
212
        $mediaForm->addFieldsetClose();
213
        $mediaForm->addButton('ok-button', 'OK')->attr('type', 'submit');
214
        $mediaForm->addButton('cancel-button', 'Cancel')->attr('type', 'button');
215
216
        // dynamic image hack? https://www.dokuwiki.org/images#dynamic_images
217
218
        echo $mediaForm->toHTML();
219
220
        // phpcs:disable
221
        $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');
222
        // phpcs:enable
223
        $datalistHTML = '<datalist id="codelanguages">';
224
        foreach ($languages as $language) {
225
            $datalistHTML .= "<option value=\"$language\">";
226
        }
227
        $datalistHTML .= '</datalist>';
228
        echo $datalistHTML;
229
    }
230
}
231
232
// vim:ts=4:sw=4:et:
233