/
utils.js
233 lines (207 loc) · 7.02 KB
/
utils.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { BalloonPanelView } from 'ckeditor5/src/ui';
import { global } from 'ckeditor5/src/utils';
export function getSelectedMathModelWidget( selection ) {
const selectedElement = selection.getSelectedElement();
if ( selectedElement && ( selectedElement.is( 'element', 'mathtex-inline' ) || selectedElement.is( 'element', 'mathtex-display' ) ) ) {
return selectedElement;
}
return null;
}
// Simple MathJax 3 version check
export function isMathJaxVersion3( version ) {
return version && typeof version === 'string' && version.split( '.' ).length === 3 && version.split( '.' )[ 0 ] === '3';
}
// Check if equation has delimiters.
export function hasDelimiters( text ) {
return text.match( /^(\\\[.*?\\\]|\\\(.*?\\\))$/ );
}
// Find delimiters count
export function delimitersCounts( text ) {
return text.match( /(\\\[|\\\]|\\\(|\\\))/g ).length;
}
// Extract delimiters and figure display mode for the model
export function extractDelimiters( equation ) {
equation = equation.trim();
// Remove delimiters (e.g. \( \) or \[ \])
const hasInlineDelimiters = equation.includes( '\\(' ) && equation.includes( '\\)' );
const hasDisplayDelimiters = equation.includes( '\\[' ) && equation.includes( '\\]' );
if ( hasInlineDelimiters || hasDisplayDelimiters ) {
equation = equation.substring( 2, equation.length - 2 ).trim();
}
return {
equation,
display: hasDisplayDelimiters
};
}
export async function renderEquation(
equation, element, engine = 'katex', lazyLoad, display = false, preview = false, previewUid, previewClassName = [],
katexRenderOptions = {}
) {
if ( engine === 'mathjax' && typeof MathJax !== 'undefined' ) {
if ( isMathJaxVersion3( MathJax.version ) ) {
selectRenderMode( element, preview, previewUid, previewClassName, el => {
renderMathJax3( equation, el, display, () => {
if ( preview ) {
moveAndScaleElement( element, el );
el.style.visibility = 'visible';
}
} );
} );
} else {
selectRenderMode( element, preview, previewUid, previewClassName, el => {
// Fixme: MathJax typesetting cause occasionally math processing error without asynchronous call
global.window.setTimeout( () => {
renderMathJax2( equation, el, display );
// Move and scale after rendering
if ( preview ) {
// eslint-disable-next-line
MathJax.Hub.Queue( () => {
moveAndScaleElement( element, el );
el.style.visibility = 'visible';
} );
}
} );
} );
}
} else if ( engine === 'katex' && typeof katex !== 'undefined' ) {
selectRenderMode( element, preview, previewUid, previewClassName, el => {
katex.render( equation, el, {
throwOnError: false,
displayMode: display,
...katexRenderOptions
} );
if ( preview ) {
moveAndScaleElement( element, el );
el.style.visibility = 'visible';
}
} );
} else if ( typeof engine === 'function' ) {
engine( equation, element, display );
} else {
if ( typeof lazyLoad !== 'undefined' ) {
try {
if ( !global.window.CKEDITOR_MATH_LAZY_LOAD ) {
global.window.CKEDITOR_MATH_LAZY_LOAD = lazyLoad();
}
element.innerHTML = equation;
await global.window.CKEDITOR_MATH_LAZY_LOAD;
renderEquation( equation, element, engine, undefined, display, preview, previewUid, previewClassName, katexRenderOptions );
}
catch ( err ) {
element.innerHTML = equation;
console.error( `math-tex-typesetting-lazy-load-failed: Lazy load failed: ${ err }` );
}
} else {
element.innerHTML = equation;
console.warn( `math-tex-typesetting-missing: Missing the mathematical typesetting engine (${ engine }) for tex.` );
}
}
}
export function getBalloonPositionData( editor ) {
const view = editor.editing.view;
const defaultPositions = BalloonPanelView.defaultPositions;
const selectedElement = view.document.selection.getSelectedElement();
if ( selectedElement ) {
return {
target: view.domConverter.viewToDom( selectedElement ),
positions: [
defaultPositions.southArrowNorth,
defaultPositions.southArrowNorthWest,
defaultPositions.southArrowNorthEast
]
};
}
else {
const viewDocument = view.document;
return {
target: view.domConverter.viewRangeToDom( viewDocument.selection.getFirstRange() ),
positions: [
defaultPositions.southArrowNorth,
defaultPositions.southArrowNorthWest,
defaultPositions.southArrowNorthEast
]
};
}
}
function selectRenderMode( element, preview, previewUid, previewClassName, cb ) {
if ( preview ) {
createPreviewElement( element, previewUid, previewClassName, previewEl => {
cb( previewEl );
} );
} else {
cb( element );
}
}
function renderMathJax3( equation, element, display, cb ) {
let promiseFunction = undefined;
if ( typeof MathJax.tex2chtmlPromise !== 'undefined' ) {
promiseFunction = MathJax.tex2chtmlPromise;
} else if ( typeof MathJax.tex2svgPromise !== 'undefined' ) {
promiseFunction = MathJax.tex2svgPromise;
}
if ( typeof promiseFunction !== 'undefined' ) {
promiseFunction( equation, { display } ).then( node => {
if ( element.firstChild ) {
element.removeChild( element.firstChild );
}
element.appendChild( node );
cb();
} );
}
}
function renderMathJax2( equation, element, display ) {
if ( display ) {
element.innerHTML = '\\[' + equation + '\\]';
} else {
element.innerHTML = '\\(' + equation + '\\)';
}
// eslint-disable-next-line
MathJax.Hub.Queue( [ 'Typeset', MathJax.Hub, element ] );
}
function createPreviewElement( element, previewUid, previewClassName, render ) {
const previewEl = getPreviewElement( element, previewUid, previewClassName );
render( previewEl );
}
function getPreviewElement( element, previewUid, previewClassName ) {
let previewEl = global.document.getElementById( previewUid );
// Create if not found
if ( !previewEl ) {
previewEl = global.document.createElement( 'div' );
previewEl.setAttribute( 'id', previewUid );
previewEl.classList.add( ...previewClassName );
previewEl.style.visibility = 'hidden';
global.document.body.appendChild( previewEl );
let ticking = false;
const renderTransformation = () => {
if ( !ticking ) {
global.window.requestAnimationFrame( () => {
moveElement( element, previewEl );
ticking = false;
} );
ticking = true;
}
};
// Create scroll listener for following
global.window.addEventListener( 'resize', renderTransformation );
global.window.addEventListener( 'scroll', renderTransformation );
}
return previewEl;
}
function moveAndScaleElement( parent, child ) {
// Move to right place
moveElement( parent, child );
// Scale parent element same as preview
const domRect = child.getBoundingClientRect();
parent.style.width = domRect.width + 'px';
parent.style.height = domRect.height + 'px';
}
function moveElement( parent, child ) {
const domRect = parent.getBoundingClientRect();
const left = global.window.scrollX + domRect.left;
const top = global.window.scrollY + domRect.top;
child.style.position = 'absolute';
child.style.left = left + 'px';
child.style.top = top + 'px';
child.style.zIndex = 'var(--ck-z-panel)';
child.style.pointerEvents = 'none';
}