-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathscript.js
210 lines (193 loc) · 7.04 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
* Hide Prosemirror and show the default editor
*
* @param {string} text the wiki syntax to be shown in the textarea
*/
function showDefaultEditor(text) {
window.Prosemirror.destroyProsemirror();
window.proseMirrorIsActive = false;
dw_locktimer.init(dw_locktimer.timeout/1000, dw_locktimer.draft);
jQuery('#wiki__text').val(text).show();
jQuery('#size__ctl').show();
jQuery('.editBox > .toolbar').show();
}
/**
* Hide the default editor and start a new Prosemirror Editor
*
* @param {string} json the prosemirror document json
*/
function showProsemirror(json) {
const $textArea = jQuery('#wiki__text');
const $prosemirrorJsonInput = jQuery('#dw__editform').find('[name=prosemirror_json]').val(json);
try {
window.Prosemirror.enableProsemirror();
stickyMenubar();
disableNativeFirefoxTableControls();
} catch (e) {
console.error(e);
let message = 'There was an error in the WYSIWYG editor. You will be redirected to the syntax editor in 5 seconds.';
if (window.SentryPlugin) {
SentryPlugin.logSentryException(e, {
tags: {
plugin: 'prosemirror',
'id': JSINFO.id,
},
extra: {
'content': $textArea.val(),
'json': $prosemirrorJsonInput.val(),
}
});
message += ' -- The error has been logged to Sentry.';
}
showErrorMessage(message);
setTimeout(function() {
jQuery('.plugin_prosemirror_useWYSIWYG').click();
}, 5000);
}
window.proseMirrorIsActive = true;
$textArea.hide();
jQuery('#size__ctl').hide();
jQuery('.editBox > .toolbar').hide();
jQuery('div.ProseMirror').focus();
if (dw_locktimer.addField) {
// todo remove this guard after the next stable DokuWiki release after Greebo
dw_locktimer.init(dw_locktimer.timeout/1000, dw_locktimer.draft, 'prosemirror__editor');
dw_locktimer.addField('input[name=prosemirror_json]');
} else {
console.warn('Draft saving in WYSIWYG is not available. Please upgrade your wiki to the current development snapshot.')
}
}
/**
* Disables Firefox's controls for editable tables, they are incompatible with prosemirror
*
* See https://github.com/ProseMirror/prosemirror/issues/432 and https://github.com/ProseMirror/prosemirror-tables/issues/22
*/
function disableNativeFirefoxTableControls() {
document.execCommand("enableObjectResizing", false, "false");
document.execCommand("enableInlineTableEditing", false, "false");
}
/**
* Initialize the prosemirror framework
*
* (This shouldn't do much until we actually use the editor, but we maybe shouldn't do this twice)
*/
function initializeProsemirror() {
try {
/* DOKUWIKI:include lib/bundle.js */
} catch (e) {
const $textArea = jQuery('#wiki__text');
console.error(e);
let message = 'There was an error initializing the WYSIWYG editor.';
if (window.SentryPlugin) {
SentryPlugin.logSentryException(e, {
tags: {
plugin: 'prosemirror',
'id': JSINFO.id,
},
extra: {
'content': $textArea.val(),
}
});
message += ' The error has been logged to sentry.';
}
showErrorMessage(message);
DokuCookie.setValue('plugin_prosemirror_useWYSIWYG', '');
}
}
/**
* Add the error message above the editor
*
* @param {string} errorMsg
*/
function showErrorMessage(errorMsg) {
jQuery('.editBox').before(
jQuery('<div class="error"></div>').text(errorMsg)
);
}
/**
* Switch between WYSIWYG and Syntax editor
*/
function toggleEditor() {
const $textArea = jQuery('#wiki__text');
const $jsonField = jQuery('#dw__editform').find('[name=prosemirror_json]');
jQuery.post(DOKU_BASE + 'lib/exe/ajax.php', {
call: 'plugin_prosemirror_switch_editors',
data: window.proseMirrorIsActive ? $jsonField.val() : $textArea.val(),
getJSON: window.proseMirrorIsActive ? '0' : '1',
id: JSINFO.id
}).done(function handleSwitchEditorResponse(data) {
if (window.proseMirrorIsActive) {
showDefaultEditor(data.text);
} else {
showProsemirror(data.json);
}
}).fail(function (jqXHR, textStatus, errorThrown) {
console.error(jqXHR, textStatus, errorThrown); // FIXME: proper error handling
if (jqXHR.responseJSON && jqXHR.responseJSON.error) {
showErrorMessage(jqXHR.responseJSON.error);
} else {
let message = 'The request failed with an unexpected error.';
if (window.SentryPlugin) {
SentryPlugin.logSentryException(new Error(textStatus), {
tags: {
plugin: 'prosemirror',
'id': JSINFO.id,
status: jqXHR.status
},
extra: {
'content': $textArea.val(),
'responseText': jqXHR.responseText,
}
});
message += ' -- The error has been logged to Sentry.';
}
showErrorMessage(message);
}
});
const $current = DokuCookie.getValue('plugin_prosemirror_useWYSIWYG');
DokuCookie.setValue('plugin_prosemirror_useWYSIWYG', $current ? '' : '1');
}
/**
* If the cookie is set, then show the WYSIWYG editor and add the switch-editor-event to the button
*/
function handleEditSession() {
const $jsonField = jQuery('#dw__editform').find('[name=prosemirror_json]');
if (DokuCookie.getValue('plugin_prosemirror_useWYSIWYG')) {
showProsemirror($jsonField.val());
}
const $toggleEditorButton = jQuery('.plugin_prosemirror_useWYSIWYG');
$toggleEditorButton.on('click', toggleEditor);
}
/**
* when the editor switch button moves out of the view-port, the menubar gets a class
* @see https://codepen.io/hey-nick/pen/mLpmMV
* @see https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API
*/
function stickyMenubar() {
const editorswitch = document.querySelector('button[name=prosemirror]');
const menubar = document.querySelector('#prosemirror__editor div.menubar');
const observer = new IntersectionObserver(
([e]) => {
return menubar.classList.toggle('prosemirror-menubar-fixed', e.intersectionRatio !== 1);
},
{
root: null,
threshold: [0, 1]
}
);
if (editorswitch && menubar) {
observer.observe(editorswitch);
}
}
jQuery(function () {
initializeProsemirror();
window.proseMirrorIsActive = false;
if (jQuery('#dw__editform').find('[name=prosemirror_json]').length) {
handleEditSession();
}
jQuery(window).on('fastwiki:afterSwitch', function(evt, viewMode, isSectionEdit, prevViewMode) {
if (viewMode === 'edit' || isSectionEdit) {
handleEditSession();
}
});
});