1
|
|
|
/** |
2
|
|
|
* Hide Prosemirror and show the default editor |
3
|
|
|
* |
4
|
|
|
* @param {string} text the wiki syntax to be shown in the textarea |
5
|
|
|
*/ |
6
|
|
|
function showDefaultEditor(text) { |
7
|
|
|
window.Prosemirror.destroyProsemirror(); |
8
|
|
|
window.proseMirrorIsActive = false; |
9
|
|
|
dw_locktimer.init(dw_locktimer.timeout/1000, dw_locktimer.draft); |
|
|
|
|
10
|
|
|
jQuery('#wiki__text').val(text).show(); |
11
|
|
|
jQuery('#size__ctl').show(); |
12
|
|
|
jQuery('.editBox > .toolbar').show(); |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Hide the default editor and start a new Prosemirror Editor |
17
|
|
|
* |
18
|
|
|
* @param {string} json the prosemirror document json |
19
|
|
|
*/ |
20
|
|
|
function showProsemirror(json) { |
21
|
|
|
const $textArea = jQuery('#wiki__text'); |
22
|
|
|
const $prosemirrorJsonInput = jQuery('#dw__editform').find('[name=prosemirror_json]').val(json); |
23
|
|
|
try { |
24
|
|
|
window.Prosemirror.enableProsemirror(); |
25
|
|
|
} catch (e) { |
26
|
|
|
console.error(e); |
27
|
|
|
let message = 'There was an error in the WYSIWYG editor. You will be redirected to the syntax editor in 5 seconds.'; |
28
|
|
|
if (window.SentryPlugin) { |
29
|
|
|
SentryPlugin.logSentryException(e, { |
|
|
|
|
30
|
|
|
tags: { |
31
|
|
|
plugin: 'prosemirror', |
32
|
|
|
'id': JSINFO.id, |
|
|
|
|
33
|
|
|
}, |
34
|
|
|
extra: { |
35
|
|
|
'content': $textArea.val(), |
36
|
|
|
'json': $prosemirrorJsonInput.val(), |
37
|
|
|
} |
38
|
|
|
}); |
39
|
|
|
message += ' -- The error has been logged to Sentry.'; |
40
|
|
|
} |
41
|
|
|
showErrorMessage(message); |
42
|
|
|
setTimeout(function() { |
43
|
|
|
jQuery('.plugin_prosemirror_useWYSIWYG').click(); |
44
|
|
|
}, 5000); |
45
|
|
|
} |
46
|
|
|
window.proseMirrorIsActive = true; |
47
|
|
|
$textArea.hide(); |
48
|
|
|
jQuery('#size__ctl').hide(); |
49
|
|
|
jQuery('.editBox > .toolbar').hide(); |
50
|
|
|
|
51
|
|
|
if (dw_locktimer.addField) { |
|
|
|
|
52
|
|
|
// todo remove this guard after the next stable DokuWiki release after Greebo |
53
|
|
|
dw_locktimer.init(dw_locktimer.timeout/1000, dw_locktimer.draft, 'prosemirror__editor'); |
54
|
|
|
dw_locktimer.addField('input[name=prosemirror_json]'); |
55
|
|
|
} else { |
56
|
|
|
console.warn('Draft saving in WYSIWYG is not available. Please upgrade your wiki to the current development snapshot.') |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Initialize the prosemirror framework |
62
|
|
|
* |
63
|
|
|
* (This shouldn't do much until we actually use the editor, but we maybe shouldn't do this twice) |
64
|
|
|
*/ |
65
|
|
|
function initializeProsemirror() { |
66
|
|
|
try { |
|
|
|
|
67
|
|
|
/* DOKUWIKI:include lib/bundle.js */ |
68
|
|
|
} catch (e) { |
|
|
|
|
69
|
|
|
const $textArea = jQuery('#wiki__text'); |
|
|
|
|
70
|
|
|
console.error(e); |
71
|
|
|
let message = 'There was an error initializing the WYSIWYG editor.'; |
|
|
|
|
72
|
|
|
if (window.SentryPlugin) { |
73
|
|
|
SentryPlugin.logSentryException(e, { |
74
|
|
|
tags: { |
75
|
|
|
plugin: 'prosemirror', |
76
|
|
|
'id': JSINFO.id, |
77
|
|
|
}, |
78
|
|
|
extra: { |
79
|
|
|
'content': $textArea.val(), |
80
|
|
|
} |
81
|
|
|
}); |
82
|
|
|
message += ' The error has been logged to sentry.'; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
showErrorMessage(message); |
86
|
|
|
|
87
|
|
|
DokuCookie.setValue('plugin_prosemirror_useWYSIWYG', ''); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Add the error message above the editor |
93
|
|
|
* |
94
|
|
|
* @param {string} errorMsg |
95
|
|
|
*/ |
96
|
|
|
function showErrorMessage(errorMsg) { |
97
|
|
|
jQuery('.editBox').before( |
98
|
|
|
jQuery('<div class="error"></div>').text(errorMsg) |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Switch between WYSIWYG and Syntax editor |
104
|
|
|
*/ |
105
|
|
|
function toggleEditor() { |
106
|
|
|
const $textArea = jQuery('#wiki__text'); |
107
|
|
|
const $jsonField = jQuery('#dw__editform').find('[name=prosemirror_json]'); |
108
|
|
|
jQuery.post(DOKU_BASE + 'lib/exe/ajax.php', { |
|
|
|
|
109
|
|
|
call: 'plugin_prosemirror_switch_editors', |
110
|
|
|
data: window.proseMirrorIsActive ? $jsonField.val() : $textArea.val(), |
111
|
|
|
getJSON: window.proseMirrorIsActive ? '0' : '1', |
112
|
|
|
}).done(function handleSwitchEditorResponse(data) { |
113
|
|
|
if (window.proseMirrorIsActive) { |
114
|
|
|
showDefaultEditor(data.text); |
115
|
|
|
} else { |
116
|
|
|
showProsemirror(data.json); |
117
|
|
|
} |
118
|
|
|
}).fail(function (jqXHR, textStatus, errorThrown) { |
119
|
|
|
console.error(jqXHR, textStatus, errorThrown); // FIXME: proper error handling |
120
|
|
|
if (jqXHR.responseJSON && jqXHR.responseJSON.error) { |
121
|
|
|
showErrorMessage(jqXHR.responseJSON.error); |
122
|
|
|
} else { |
123
|
|
|
let message = 'The request failed with an unexpected error.'; |
124
|
|
|
if (window.SentryPlugin) { |
125
|
|
|
SentryPlugin.logSentryException(new Error(textStatus), { |
|
|
|
|
126
|
|
|
tags: { |
127
|
|
|
plugin: 'prosemirror', |
128
|
|
|
'id': JSINFO.id, |
|
|
|
|
129
|
|
|
status: jqXHR.status |
130
|
|
|
}, |
131
|
|
|
extra: { |
132
|
|
|
'content': $textArea.val(), |
133
|
|
|
'responseText': jqXHR.responseText, |
134
|
|
|
} |
135
|
|
|
}); |
136
|
|
|
message += ' -- The error has been logged to Sentry.'; |
137
|
|
|
} |
138
|
|
|
showErrorMessage(message); |
139
|
|
|
} |
140
|
|
|
}); |
141
|
|
|
|
142
|
|
|
const $current = DokuCookie.getValue('plugin_prosemirror_useWYSIWYG'); |
|
|
|
|
143
|
|
|
DokuCookie.setValue('plugin_prosemirror_useWYSIWYG', $current ? '' : '1'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* If the cookie is set, then show the WYSIWYG editor and add the switch-editor-event to the button |
148
|
|
|
*/ |
149
|
|
|
function handleEditSession() { |
150
|
|
|
const $jsonField = jQuery('#dw__editform').find('[name=prosemirror_json]'); |
151
|
|
|
if (DokuCookie.getValue('plugin_prosemirror_useWYSIWYG')) { |
|
|
|
|
152
|
|
|
showProsemirror($jsonField.val()); |
153
|
|
|
} |
154
|
|
|
const $toggleEditorButton = jQuery('.plugin_prosemirror_useWYSIWYG'); |
155
|
|
|
$toggleEditorButton.on('click', toggleEditor); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if (!window.Prosemirror) { |
159
|
|
|
window.Prosemirror = {}; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if (!window.Prosemirror.pluginSchemas) { |
163
|
|
|
window.Prosemirror.pluginSchemas = []; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
if (!window.Prosemirror.pluginNodeViews) { |
167
|
|
|
window.Prosemirror.pluginNodeViews = {}; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
jQuery(function () { |
172
|
|
|
window.proseMirrorIsActive = false; |
173
|
|
|
|
174
|
|
|
initializeProsemirror(); |
175
|
|
|
|
176
|
|
|
if (jQuery('#dw__editform').find('[name=prosemirror_json]').length) { |
177
|
|
|
handleEditSession(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
jQuery(window).on('fastwiki:afterSwitch', function(evt, viewMode, isSectionEdit, prevViewMode) { |
|
|
|
|
181
|
|
|
if (viewMode === 'edit' || isSectionEdit) { |
182
|
|
|
handleEditSession(); |
183
|
|
|
} |
184
|
|
|
}); |
185
|
|
|
}); |
186
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.