@wiris/mathtype-ckeditor5 8.15.1 → 8.15.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/index.js +2110 -452
- package/dist/browser/index.js.map +1 -1
- package/dist/browser/index.umd.js +2110 -452
- package/dist/browser/index.umd.js.map +1 -1
- package/dist/index.js +413 -109
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/integration.js +468 -122
- package/src/plugin.js +64 -6
package/dist/index.js
CHANGED
|
@@ -132,49 +132,68 @@ import Telemeter from '@wiris/mathtype-html-integration-devkit/src/telemeter.js'
|
|
|
132
132
|
* @param {String} mathml MathML to update old one or insert
|
|
133
133
|
* @returns {module:engine/model/element~Element} The model element corresponding to the inserted image
|
|
134
134
|
*/ insertMathml(mathml) {
|
|
135
|
-
// This returns the value returned by the callback function (writer => {...})
|
|
136
135
|
return this.editorObject.model.change((writer)=>{
|
|
137
|
-
const
|
|
136
|
+
const { isNewElement, temporalImage } = this.getCore().editionProperties;
|
|
138
137
|
const selection = this.editorObject.model.document.selection;
|
|
138
|
+
const attributes = Object.fromEntries(selection.getAttributes());
|
|
139
139
|
const modelElementNew = writer.createElement("mathml", {
|
|
140
140
|
formula: mathml,
|
|
141
|
-
...
|
|
141
|
+
...attributes
|
|
142
142
|
});
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
// Don't bother inserting anything at all if the MathML is empty.
|
|
146
|
-
if (!mathml) return;
|
|
147
|
-
const viewSelection = this.core.editionProperties.selection || this.editorObject.editing.view.document.selection;
|
|
148
|
-
const modelPosition = this.editorObject.editing.mapper.toModelPosition(viewSelection.getLastPosition());
|
|
149
|
-
this.editorObject.model.insertObject(modelElementNew, modelPosition);
|
|
150
|
-
// Remove selection
|
|
151
|
-
if (!viewSelection.isCollapsed) {
|
|
152
|
-
for (const range of viewSelection.getRanges()){
|
|
153
|
-
const modelRange = this.editorObject.editing.mapper.toModelRange(range);
|
|
154
|
-
const modelSelection = this.editorObject.model.createSelection(modelRange);
|
|
155
|
-
this.editorObject.model.deleteContent(modelSelection);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
// Set carret after the formula
|
|
159
|
-
const position = this.editorObject.model.createPositionAfter(modelElementNew);
|
|
160
|
-
writer.setSelection(position);
|
|
161
|
-
} else {
|
|
162
|
-
const img = core.editionProperties.temporalImage;
|
|
163
|
-
const viewElement = this.editorObject.editing.view.domConverter.domToView(img).parent;
|
|
164
|
-
const modelElementOld = this.editorObject.editing.mapper.toModelElement(viewElement);
|
|
165
|
-
// Insert the new <mathml> and remove the old one
|
|
166
|
-
const position = this.editorObject.model.createPositionBefore(modelElementOld);
|
|
167
|
-
// If the given MathML is empty, don't insert a new formula.
|
|
168
|
-
if (mathml) {
|
|
169
|
-
this.editorObject.model.insertObject(modelElementNew, position);
|
|
170
|
-
}
|
|
171
|
-
this.editorObject.model.deleteContent(this.editorObject.model.createSelection(modelElementOld, 'on'));
|
|
143
|
+
if (isNewElement) {
|
|
144
|
+
return this.insertNewFormula(writer, mathml, modelElementNew);
|
|
172
145
|
}
|
|
173
|
-
|
|
174
|
-
return modelElementNew;
|
|
146
|
+
return this.replaceExistingFormula(mathml, modelElementNew, temporalImage);
|
|
175
147
|
});
|
|
176
148
|
}
|
|
177
149
|
/**
|
|
150
|
+
* Inserts a new formula at the current selection position.
|
|
151
|
+
*/ insertNewFormula(writer, mathml, modelElement) {
|
|
152
|
+
if (!mathml) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
const viewSelection = this.core.editionProperties.selection || this.editorObject.editing.view.document.selection;
|
|
156
|
+
const modelPosition = this.editorObject.editing.mapper.toModelPosition(viewSelection.getLastPosition());
|
|
157
|
+
this.editorObject.model.insertObject(modelElement, modelPosition);
|
|
158
|
+
this.deleteViewSelection(viewSelection);
|
|
159
|
+
// Set carret after the formula.
|
|
160
|
+
const position = this.editorObject.model.createPositionAfter(modelElement);
|
|
161
|
+
writer.setSelection(position);
|
|
162
|
+
return modelElement;
|
|
163
|
+
}
|
|
164
|
+
deleteViewSelection(viewSelection) {
|
|
165
|
+
if (viewSelection.isCollapsed) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
for (const range of viewSelection.getRanges()){
|
|
169
|
+
const modelRange = this.editorObject.editing.mapper.toModelRange(range);
|
|
170
|
+
const modelSelection = this.editorObject.model.createSelection(modelRange);
|
|
171
|
+
this.editorObject.model.deleteContent(modelSelection);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Replaces an existing formula with updated MathML.
|
|
176
|
+
*/ replaceExistingFormula(mathml, modelElement, temporalImage) {
|
|
177
|
+
const viewNode = this.editorObject.editing.view.domConverter.domToView(temporalImage);
|
|
178
|
+
// Check if image exists in view to do standard formula editing
|
|
179
|
+
if (viewNode?.parent) {
|
|
180
|
+
const modelElementOld = this.editorObject.editing.mapper.toModelElement(viewNode.parent);
|
|
181
|
+
// Insert the new <mathml> and remove the old one
|
|
182
|
+
const position = this.editorObject.model.createPositionBefore(modelElementOld);
|
|
183
|
+
if (mathml) {
|
|
184
|
+
this.editorObject.model.insertObject(modelElement, position);
|
|
185
|
+
}
|
|
186
|
+
this.editorObject.model.deleteContent(this.editorObject.model.createSelection(modelElementOld, "on"));
|
|
187
|
+
return modelElement;
|
|
188
|
+
}
|
|
189
|
+
// Otherwise it's LaTeX editing, so we insert at current selection
|
|
190
|
+
if (!mathml) {
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
this.editorObject.model.insertContent(modelElement);
|
|
194
|
+
return modelElement;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
178
197
|
* Finds the text node corresponding to given DOM text element.
|
|
179
198
|
* @param {element} viewElement Element to find corresponding text node of.
|
|
180
199
|
* @returns {module:engine/model/text~Text|undefined} Text node corresponding to the given element or undefined if it doesn't exist.
|
|
@@ -201,81 +220,26 @@ import Telemeter from '@wiris/mathtype-html-integration-devkit/src/telemeter.js'
|
|
|
201
220
|
}
|
|
202
221
|
}
|
|
203
222
|
}
|
|
204
|
-
/** @inheritdoc */ insertFormula(
|
|
223
|
+
/** @inheritdoc */ insertFormula(_focusElement, windowTarget, mathml, _wirisProperties) {
|
|
205
224
|
// eslint-disable-line no-unused-vars
|
|
206
225
|
const returnObject = {};
|
|
207
226
|
let mathmlOrigin;
|
|
208
227
|
if (!mathml) {
|
|
209
228
|
this.insertMathml("");
|
|
210
229
|
} else if (this.core.editMode === "latex") {
|
|
211
|
-
returnObject
|
|
212
|
-
returnObject.node = windowTarget.document.createTextNode(`$$${returnObject.latex}$$`);
|
|
213
|
-
this.editorObject.model.change((writer)=>{
|
|
214
|
-
const { latexRange } = this.core.editionProperties;
|
|
215
|
-
// Add null check for latexRange.
|
|
216
|
-
// When the editor is initialized in a textarea element, latexRange may not be set on initial load.
|
|
217
|
-
// This check ensures formulas can still be inserted by falling back to MathML insertion if latexRange is not available.
|
|
218
|
-
if (!latexRange) {
|
|
219
|
-
// Fallback to regular MathML insertion if latexRange is not available
|
|
220
|
-
this.insertMathml(mathml);
|
|
221
|
-
return;
|
|
222
|
-
}
|
|
223
|
-
const startNode = this.findText(latexRange.startContainer);
|
|
224
|
-
const endNode = this.findText(latexRange.endContainer);
|
|
225
|
-
let startPosition = writer.createPositionAt(startNode.parent, startNode.startOffset + latexRange.startOffset);
|
|
226
|
-
let endPosition = writer.createPositionAt(endNode.parent, endNode.startOffset + latexRange.endOffset);
|
|
227
|
-
let range = writer.createRange(startPosition, endPosition);
|
|
228
|
-
// When Latex is next to image/formula.
|
|
229
|
-
if (latexRange.startContainer.nodeType === 3 && latexRange.startContainer.previousSibling?.nodeType === 1) {
|
|
230
|
-
// Get the position of the latex to be replaced.
|
|
231
|
-
const latexEdited = `$$${Latex.getLatexFromMathML(MathML.safeXmlDecode(this.core.editionProperties.temporalImage.dataset.mathml))}$$`;
|
|
232
|
-
let data = latexRange.startContainer.data;
|
|
233
|
-
// Remove invisible characters.
|
|
234
|
-
data = data.replaceAll(String.fromCharCode(8288), "");
|
|
235
|
-
// Get to the start of the latex we are editing.
|
|
236
|
-
const offset = data.indexOf(latexEdited);
|
|
237
|
-
const dataOffset = data.substring(offset);
|
|
238
|
-
const second$ = dataOffset.substring(2).indexOf("$$") + 4;
|
|
239
|
-
const substring = dataOffset.substr(0, second$);
|
|
240
|
-
data = data.replace(substring, "");
|
|
241
|
-
if (!data) {
|
|
242
|
-
startPosition = writer.createPositionBefore(startNode);
|
|
243
|
-
range = startNode;
|
|
244
|
-
} else {
|
|
245
|
-
startPosition = startPosition = writer.createPositionAt(startNode.parent, startNode.startOffset + offset);
|
|
246
|
-
endPosition = writer.createPositionAt(endNode.parent, endNode.startOffset + second$ + offset);
|
|
247
|
-
range = writer.createRange(startPosition, endPosition);
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
const modelSelection = this.editorObject.model.createSelection(range);
|
|
251
|
-
this.editorObject.model.deleteContent(modelSelection);
|
|
252
|
-
writer.insertText(`$$${returnObject.latex}$$`, startNode.getAttributes(), startPosition);
|
|
253
|
-
});
|
|
230
|
+
this.handleLatexInsertion(returnObject, windowTarget, mathml);
|
|
254
231
|
} else {
|
|
255
|
-
mathmlOrigin = this.
|
|
256
|
-
try {
|
|
257
|
-
returnObject.node = this.editorObject.editing.view.domConverter.viewToDom(this.editorObject.editing.mapper.toViewElement(this.insertMathml(mathml)), windowTarget.document);
|
|
258
|
-
} catch (e) {
|
|
259
|
-
const x = e.toString();
|
|
260
|
-
if (x.includes("CKEditorError: Cannot read property 'parent' of undefined")) {
|
|
261
|
-
this.core.modalDialog.cancelAction();
|
|
262
|
-
}
|
|
263
|
-
}
|
|
232
|
+
mathmlOrigin = this.handleMathmlInsertion(returnObject, windowTarget, mathml);
|
|
264
233
|
}
|
|
265
|
-
// Build the telemeter payload separated to delete null/undefined entries.
|
|
266
234
|
const payload = {
|
|
267
|
-
|
|
268
|
-
mathml: mathml ? MathML.safeXmlDecode(mathml) : mathml,
|
|
235
|
+
mathml: mathml ? MathML.safeXmlDecode(mathml) : undefined,
|
|
269
236
|
elapsed_time: Date.now() - this.core.editionProperties.editionStartTime,
|
|
270
|
-
editor_origin: null,
|
|
271
237
|
toolbar: this.core.modalDialog.contentManager.toolbar,
|
|
272
238
|
size: mathml?.length
|
|
273
239
|
};
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
});
|
|
278
|
-
// Call Telemetry service to track the event.
|
|
240
|
+
if (mathmlOrigin) {
|
|
241
|
+
payload.mathml_origin = MathML.safeXmlDecode(mathmlOrigin);
|
|
242
|
+
}
|
|
279
243
|
try {
|
|
280
244
|
Telemeter.telemeter.track("INSERTED_FORMULA", {
|
|
281
245
|
...payload
|
|
@@ -283,15 +247,312 @@ import Telemeter from '@wiris/mathtype-html-integration-devkit/src/telemeter.js'
|
|
|
283
247
|
} catch (error) {
|
|
284
248
|
console.error("Error tracking INSERTED_FORMULA", error);
|
|
285
249
|
}
|
|
286
|
-
/* Due to PLUGINS-1329, we add the onChange event to the CK4 insertFormula.
|
|
287
|
-
We probably should add it here as well, but we should look further into how */ // this.editorObject.fire('change');
|
|
288
|
-
// Remove temporal image of inserted formula
|
|
289
250
|
this.core.editionProperties.temporalImage = null;
|
|
290
251
|
return returnObject;
|
|
291
252
|
}
|
|
253
|
+
handleLatexInsertion(returnObject, windowTarget, mathml) {
|
|
254
|
+
returnObject.latex = Latex.getLatexFromMathML(mathml);
|
|
255
|
+
returnObject.node = windowTarget.document.createTextNode(`$$${returnObject.latex}$$`);
|
|
256
|
+
const { latexRange } = this.core.editionProperties;
|
|
257
|
+
// When latexRange exists (meaning the whole LaTeX was selected or the editor was opened),
|
|
258
|
+
// find the node contaning the LaTeX and replace it fully.
|
|
259
|
+
if (latexRange) {
|
|
260
|
+
const startNode = this.findText(latexRange.startContainer);
|
|
261
|
+
const endNode = this.findText(latexRange.endContainer);
|
|
262
|
+
// If nodes found, use standard replacement.
|
|
263
|
+
if (startNode && endNode) {
|
|
264
|
+
this.replaceLatexWithNodes(startNode, endNode, latexRange, returnObject.latex);
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
this.replaceLatexUsingModelSearch(returnObject.latex);
|
|
269
|
+
}
|
|
270
|
+
handleMathmlInsertion(returnObject, windowTarget, mathml) {
|
|
271
|
+
const mathmlOrigin = this.core.editionProperties.temporalImage?.dataset.mathml;
|
|
272
|
+
try {
|
|
273
|
+
const modelElement = this.insertMathml(mathml);
|
|
274
|
+
const viewElement = this.editorObject.editing.mapper.toViewElement(modelElement);
|
|
275
|
+
returnObject.node = this.editorObject.editing.view.domConverter.viewToDom(viewElement, windowTarget.document);
|
|
276
|
+
} catch (error) {
|
|
277
|
+
if (error.toString().includes("Cannot read property 'parent' of undefined")) {
|
|
278
|
+
this.core.modalDialog.cancelAction();
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return mathmlOrigin;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Gets selection attributes excluding track changes tags.
|
|
285
|
+
*/ getCleanSelectionAttributes() {
|
|
286
|
+
const attributes = {};
|
|
287
|
+
for (const [key, value] of this.editorObject.model.document.selection.getAttributes()){
|
|
288
|
+
if (!key.startsWith("suggestion:") && !key.startsWith("comment:")) {
|
|
289
|
+
attributes[key] = value;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
return attributes;
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Searches for the original LaTeX in the model and replaces it.
|
|
296
|
+
* Fallback when findText() cannot locate DOM nodes (like when there are track changes modifications).
|
|
297
|
+
*/ replaceLatexUsingModelSearch(newLatex) {
|
|
298
|
+
const foundRange = this.findLatexBlockNearSelection();
|
|
299
|
+
if (foundRange) {
|
|
300
|
+
this.editorObject.model.change((writer)=>writer.setSelection(foundRange));
|
|
301
|
+
this.replaceRangeWithLatex(newLatex);
|
|
302
|
+
} else {
|
|
303
|
+
// Insert at current position as a last resort.
|
|
304
|
+
this.editorObject.model.change((writer)=>{
|
|
305
|
+
const newLatexText = writer.createText(`$$${newLatex}$$`, this.getCleanSelectionAttributes());
|
|
306
|
+
this.editorObject.model.insertContent(newLatexText);
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
this.core.editionProperties.extractedLatex = null;
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Checks if a text proxy has a track changes deletion marker.
|
|
313
|
+
*/ isDeletedText(text) {
|
|
314
|
+
for (const [key, value] of text.getAttributes()){
|
|
315
|
+
if (key.startsWith("suggestion:") && value === "deletion") {
|
|
316
|
+
return true;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return false;
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Finds a LaTeX block ($$...$$) near the current selection.
|
|
323
|
+
* Handles track changes by considering the "accepted" version of text.
|
|
324
|
+
*/ findLatexBlockNearSelection() {
|
|
325
|
+
const position = this.editorObject.model.document.selection.getFirstPosition();
|
|
326
|
+
if (!position?.parent) {
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
// Build LaTeX with track changes accepted suggestions, if any.
|
|
330
|
+
const { textParts, acceptedText } = this.collectTextParts(position.parent);
|
|
331
|
+
if (!acceptedText.includes("$$")) {
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
// To handle multiple LaTeX on same line.
|
|
335
|
+
const targetLatex = this.core.editionProperties.extractedLatex;
|
|
336
|
+
const fullLatex = `$$${targetLatex}$$`;
|
|
337
|
+
const startIndex = acceptedText.indexOf(fullLatex);
|
|
338
|
+
if (startIndex === -1) {
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
const latexBoundaries = {
|
|
342
|
+
start: startIndex,
|
|
343
|
+
end: startIndex + fullLatex.length
|
|
344
|
+
};
|
|
345
|
+
return this.convertAcceptedOffsetsToModelRange(textParts, latexBoundaries);
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Collects all text fragments from a paragraph, tracking both model and accepted text positions.
|
|
349
|
+
* This is necessary to handle track changes where some LaTeX may have suggestions.
|
|
350
|
+
*/ collectTextParts(paragraph) {
|
|
351
|
+
const textParts = [];
|
|
352
|
+
let acceptedTextOffset = 0;
|
|
353
|
+
let acceptedText = "";
|
|
354
|
+
for (const item of this.editorObject.model.createRangeIn(paragraph).getItems()){
|
|
355
|
+
if (item.is("$textProxy")) {
|
|
356
|
+
const isDeleted = this.isDeletedText(item);
|
|
357
|
+
textParts.push({
|
|
358
|
+
text: item.data,
|
|
359
|
+
startOffset: item.startOffset,
|
|
360
|
+
endOffset: item.startOffset + item.data.length,
|
|
361
|
+
parent: item.textNode.parent,
|
|
362
|
+
acceptedStart: isDeleted ? null : acceptedTextOffset,
|
|
363
|
+
acceptedEnd: isDeleted ? null : acceptedTextOffset + item.data.length,
|
|
364
|
+
isDeleted
|
|
365
|
+
});
|
|
366
|
+
if (!isDeleted) {
|
|
367
|
+
acceptedText += item.data;
|
|
368
|
+
acceptedTextOffset += item.data.length;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
return {
|
|
373
|
+
textParts,
|
|
374
|
+
acceptedText
|
|
375
|
+
};
|
|
376
|
+
}
|
|
292
377
|
/**
|
|
293
|
-
*
|
|
294
|
-
*/
|
|
378
|
+
* Converts LaTeX with track changes accepted suggestions to a CKEditor model Range.
|
|
379
|
+
*/ convertAcceptedOffsetsToModelRange(textParts, latexBoundaries) {
|
|
380
|
+
let startPartIndex = -1, endPartIndex = -1;
|
|
381
|
+
let startOffsetInPart = 0, endOffsetInPart = 0;
|
|
382
|
+
// Find which text parts contain the LaTeX block boundaries
|
|
383
|
+
for(let i = 0; i < textParts.length; i++){
|
|
384
|
+
const part = textParts[i];
|
|
385
|
+
if (part.isDeleted) continue;
|
|
386
|
+
if (startPartIndex === -1 && latexBoundaries.start >= part.acceptedStart && latexBoundaries.start <= part.acceptedEnd) {
|
|
387
|
+
startPartIndex = i;
|
|
388
|
+
startOffsetInPart = latexBoundaries.start - part.acceptedStart;
|
|
389
|
+
}
|
|
390
|
+
if (latexBoundaries.end >= part.acceptedStart && latexBoundaries.end <= part.acceptedEnd) {
|
|
391
|
+
endPartIndex = i;
|
|
392
|
+
endOffsetInPart = latexBoundaries.end - part.acceptedStart;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
if (startPartIndex === -1 || endPartIndex === -1) {
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
// Extend range to include any consecutive deleted parts after the block.
|
|
399
|
+
let finalEndIndex = endPartIndex;
|
|
400
|
+
let finalEndOffset = endOffsetInPart;
|
|
401
|
+
for(let i = endPartIndex + 1; i < textParts.length && textParts[i].isDeleted; i++){
|
|
402
|
+
finalEndIndex = i;
|
|
403
|
+
finalEndOffset = textParts[i].text.length;
|
|
404
|
+
}
|
|
405
|
+
const startPart = textParts[startPartIndex];
|
|
406
|
+
const endPart = textParts[finalEndIndex];
|
|
407
|
+
return this.editorObject.model.createRange(this.editorObject.model.createPositionAt(startPart.parent, startPart.startOffset + startOffsetInPart), this.editorObject.model.createPositionAt(endPart.parent, endPart.startOffset + finalEndOffset));
|
|
408
|
+
}
|
|
409
|
+
replaceRangeWithLatex(newLatex) {
|
|
410
|
+
this.editorObject.model.change((writer)=>{
|
|
411
|
+
this.editorObject.model.deleteContent(this.editorObject.model.document.selection);
|
|
412
|
+
const newLatexText = writer.createText(`$$${newLatex}$$`, this.getCleanSelectionAttributes());
|
|
413
|
+
this.editorObject.model.insertContent(newLatexText);
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* Replaces the whole LaTeX in the CKEditor5 model.
|
|
418
|
+
*/ replaceLatexWithNodes(startNode, endNode, latexRange, newLatex) {
|
|
419
|
+
this.editorObject.model.change((writer)=>{
|
|
420
|
+
const startOffset = startNode.startOffset + latexRange.startOffset;
|
|
421
|
+
const endOffset = endNode.startOffset + latexRange.endOffset;
|
|
422
|
+
let startPosition = writer.createPositionAt(startNode.parent, startOffset);
|
|
423
|
+
let endPosition = writer.createPositionAt(endNode.parent, endOffset);
|
|
424
|
+
// Adjust positions when LaTeX is adjacent to a formula.
|
|
425
|
+
const startContainer = latexRange.startContainer;
|
|
426
|
+
if (startContainer.nodeType === Node.TEXT_NODE && startContainer.previousSibling?.nodeType === Node.ELEMENT_NODE) {
|
|
427
|
+
const originalLatex = `$$${Latex.getLatexFromMathML(MathML.safeXmlDecode(this.core.editionProperties.temporalImage.dataset.mathml))}$$`;
|
|
428
|
+
const textData = startContainer.data.replaceAll(String.fromCodePoint(8288), "");
|
|
429
|
+
const latexOffset = textData.indexOf(originalLatex);
|
|
430
|
+
if (latexOffset !== -1) {
|
|
431
|
+
const closingDelimiterOffset = textData.substring(latexOffset + 2).indexOf("$$") + 4;
|
|
432
|
+
startPosition = writer.createPositionAt(startNode.parent, startNode.startOffset + latexOffset);
|
|
433
|
+
endPosition = writer.createPositionAt(endNode.parent, endNode.startOffset + closingDelimiterOffset + latexOffset);
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
writer.setSelection(writer.createRange(startPosition, endPosition));
|
|
437
|
+
});
|
|
438
|
+
this.replaceRangeWithLatex(newLatex);
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Inherited method from IntegrationModel.
|
|
442
|
+
* Gets the MathML from a text node containing LaTeX.
|
|
443
|
+
* Handles track changes by simulating "accept all changes" before conversion.
|
|
444
|
+
*/ getMathmlFromTextNode(textNode, caretPosition) {
|
|
445
|
+
const standardResult = Latex.getLatexFromTextNode(textNode, caretPosition);
|
|
446
|
+
const acceptedLatex = this.extractAcceptedLatexFromDOM(textNode, caretPosition);
|
|
447
|
+
// Prioritize accepted LaTeX if it differs from standard extraction (for track changes compatibility).
|
|
448
|
+
// Important node: use explicit undefined check to allow empty LaTeX strings, otherwise it would not detect $$$$ as valid LaTeX.
|
|
449
|
+
const latex = acceptedLatex !== undefined && acceptedLatex !== standardResult?.latex ? acceptedLatex : standardResult?.latex;
|
|
450
|
+
// Do not continue if no LaTeX found by either method.
|
|
451
|
+
// This is necessary since both parameters can be independently undefined in some edge cases.
|
|
452
|
+
if (latex === undefined && acceptedLatex === undefined) {
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
// Verify caret is inside LaTeX block for track changes edge cases.
|
|
456
|
+
if (!standardResult && acceptedLatex !== undefined && !this.isCaretInsideLatexBlock(textNode, caretPosition)) {
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
const finalLatex = latex === undefined ? acceptedLatex : latex;
|
|
460
|
+
this.storeLatexRangeWithFallback(textNode, caretPosition, finalLatex);
|
|
461
|
+
return Latex.getMathMLFromLatex(finalLatex);
|
|
462
|
+
}
|
|
463
|
+
isCaretInsideLatexBlock(textNode, caretPosition = 0) {
|
|
464
|
+
// If LaTeX is found, the caret is inside one.
|
|
465
|
+
return this.extractAcceptedLatexFromDOM(textNode, caretPosition) !== undefined;
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* Stores the LaTeX range for its replacement later.
|
|
469
|
+
*/ storeLatexRangeWithFallback(textNode, caretPosition, latex) {
|
|
470
|
+
const parentTag = textNode.parentElement?.tagName?.toLowerCase();
|
|
471
|
+
if (!textNode.parentElement || parentTag === "textarea") {
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
const latexResult = Latex.getLatexFromTextNode(textNode, caretPosition);
|
|
475
|
+
if (latexResult) {
|
|
476
|
+
const range = document.createRange();
|
|
477
|
+
range.setStart(latexResult.startNode, latexResult.startPosition);
|
|
478
|
+
range.setEnd(latexResult.endNode, latexResult.endPosition);
|
|
479
|
+
this.core.editionProperties.latexRange = range;
|
|
480
|
+
} else {
|
|
481
|
+
this.core.editionProperties.latexRange = null;
|
|
482
|
+
}
|
|
483
|
+
this.core.editionProperties.extractedLatex = latex;
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* Finds a container element containing a complete LaTeX block.
|
|
487
|
+
* Necessary for track changes handling, to find the full LaTeX even with the suggestions.
|
|
488
|
+
*/ findLatexContainerElement(textNode) {
|
|
489
|
+
const MAX_DEPTH = 10; // Prevent excessive loops.
|
|
490
|
+
let element = textNode.parentElement;
|
|
491
|
+
for(let i = 0; i < MAX_DEPTH && element; i++){
|
|
492
|
+
const text = element.textContent || "";
|
|
493
|
+
const openDelim = text.indexOf("$$");
|
|
494
|
+
if (openDelim !== -1 && text.includes("$$", openDelim + 2)) {
|
|
495
|
+
return element;
|
|
496
|
+
}
|
|
497
|
+
element = element.parentElement;
|
|
498
|
+
}
|
|
499
|
+
return null;
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* Extracts LaTeX from DOM, skipping track changes deletion markers.
|
|
503
|
+
*/ extractAcceptedLatexFromDOM(textNode, caretPositionInNode = 0) {
|
|
504
|
+
const container = this.findLatexContainerElement(textNode);
|
|
505
|
+
if (!container) {
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
const acceptedText = this.getAcceptedTextContent(container);
|
|
509
|
+
// Calculate caret offset that will be used later to find the correct LaTeX block.
|
|
510
|
+
// This includes all accepted text before textNode, plus the caret position within textNode.
|
|
511
|
+
const walker = document.createTreeWalker(container, NodeFilter.SHOW_TEXT);
|
|
512
|
+
let node = walker.nextNode();
|
|
513
|
+
let caretOffset = 0;
|
|
514
|
+
while(node && node !== textNode){
|
|
515
|
+
if (!node.parentElement?.classList?.contains("ck-suggestion-marker-deletion")) {
|
|
516
|
+
caretOffset += node.textContent?.length || 0;
|
|
517
|
+
}
|
|
518
|
+
node = walker.nextNode();
|
|
519
|
+
}
|
|
520
|
+
// Add the caret position within the text node, only if textNode is not deleted by Track Changes.
|
|
521
|
+
if (node === textNode && !textNode.parentElement?.classList?.contains("ck-suggestion-marker-deletion")) {
|
|
522
|
+
caretOffset += caretPositionInNode;
|
|
523
|
+
}
|
|
524
|
+
// Find the LaTeX block that contains the caret.
|
|
525
|
+
let nextSearchIndex = 0;
|
|
526
|
+
while(nextSearchIndex < acceptedText.length){
|
|
527
|
+
const openDelim = acceptedText.indexOf("$$", nextSearchIndex);
|
|
528
|
+
if (openDelim === -1) {
|
|
529
|
+
break;
|
|
530
|
+
}
|
|
531
|
+
const closeDelim = acceptedText.indexOf("$$", openDelim + 2);
|
|
532
|
+
if (closeDelim === -1) {
|
|
533
|
+
break;
|
|
534
|
+
}
|
|
535
|
+
if (caretOffset >= openDelim && caretOffset <= closeDelim + 2) {
|
|
536
|
+
return acceptedText.substring(openDelim + 2, closeDelim);
|
|
537
|
+
}
|
|
538
|
+
nextSearchIndex = closeDelim + 2;
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* Recursively extracts text content, skipping track changes tags.
|
|
543
|
+
*/ getAcceptedTextContent(node) {
|
|
544
|
+
if (node.nodeType === Node.TEXT_NODE) {
|
|
545
|
+
return node.textContent || "";
|
|
546
|
+
}
|
|
547
|
+
if (node.nodeType === Node.ELEMENT_NODE) {
|
|
548
|
+
if (node.classList?.contains("ck-suggestion-marker-deletion")) {
|
|
549
|
+
return "";
|
|
550
|
+
}
|
|
551
|
+
return Array.from(node.childNodes).map((child)=>this.getAcceptedTextContent(child)).join("");
|
|
552
|
+
}
|
|
553
|
+
return "";
|
|
554
|
+
}
|
|
555
|
+
/** Called when the modal window is closed. */ notifyWindowClosed() {
|
|
295
556
|
this.editorObject.editing.view.focus();
|
|
296
557
|
}
|
|
297
558
|
}
|
|
@@ -369,7 +630,7 @@ var mathIcon = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<!-- Generator: Adob
|
|
|
369
630
|
|
|
370
631
|
var chemIcon = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<!-- Generator: Adobe Illustrator 22.0.1, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->\n<svg version=\"1.1\" id=\"Layer_1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\" y=\"0px\"\n\t viewBox=\"0 0 40.3 49.5\" style=\"enable-background:new 0 0 40.3 49.5;\" xml:space=\"preserve\">\n<style type=\"text/css\">\n\t.st0{fill:#A4CF61;}\n</style>\n<path class=\"st0\" d=\"M39.2,12.1c0-1.9-1.1-3.6-2.7-4.4L24.5,0.9l0,0c-0.7-0.4-1.5-0.6-2.4-0.6c-0.9,0-1.7,0.2-2.4,0.6l0,0L2.3,10.8\n\tl0,0C0.9,11.7,0,13.2,0,14.9h0v19.6h0c0,1.7,0.9,3.3,2.3,4.1l0,0l17.4,9.9l0,0c0.7,0.4,1.5,0.6,2.4,0.6c0.9,0,1.7-0.2,2.4-0.6l0,0\n\tl12.2-6.9h0c1.5-0.8,2.6-2.5,2.6-4.3c0-2.7-2.2-4.9-4.9-4.9c-0.9,0-1.8,0.3-2.5,0.7l0,0l-9.7,5.6l-12.3-7V17.8l12.3-7l9.9,5.7l0,0\n\tc0.7,0.4,1.5,0.6,2.4,0.6C37,17,39.2,14.8,39.2,12.1\"/>\n</svg>\n";
|
|
371
632
|
|
|
372
|
-
var version = "8.15.
|
|
633
|
+
var version = "8.15.3";
|
|
373
634
|
var packageInfo = {
|
|
374
635
|
version: version};
|
|
375
636
|
|
|
@@ -721,11 +982,7 @@ class MathType extends Plugin {
|
|
|
721
982
|
* we must create a new EmptyElement which is independent of the
|
|
722
983
|
* DataProcessor being used by this editor instance
|
|
723
984
|
*/ if (imgElement) {
|
|
724
|
-
return viewWriter.createEmptyElement("img", imgElement.getAttributes()
|
|
725
|
-
renderUnsafeAttributes: [
|
|
726
|
-
"src"
|
|
727
|
-
]
|
|
728
|
-
});
|
|
985
|
+
return viewWriter.createEmptyElement("img", imgElement.getAttributes());
|
|
729
986
|
}
|
|
730
987
|
return null;
|
|
731
988
|
}
|
|
@@ -823,8 +1080,10 @@ class MathType extends Plugin {
|
|
|
823
1080
|
e.return = MathML.removeSafeXMLSemantics(previewOutput);
|
|
824
1081
|
return;
|
|
825
1082
|
}
|
|
826
|
-
//
|
|
827
|
-
const
|
|
1083
|
+
// Clean track changes markers only from LaTeX content before converting to MathML.
|
|
1084
|
+
const latexParsedOutput = this._endParseEditModeWithTrackChangesSupport(output);
|
|
1085
|
+
// Convert formula images to MathML. It's important to use the save mode to prevent issues.
|
|
1086
|
+
const parsedResult = Parser.endParseSaveMode(latexParsedOutput);
|
|
828
1087
|
// Remove all semantic annotations (including handwritten data points)
|
|
829
1088
|
// to ensure clean, standard MathML format for storage
|
|
830
1089
|
e.return = MathML.removeSafeXMLSemantics(parsedResult);
|
|
@@ -866,6 +1125,38 @@ class MathType extends Plugin {
|
|
|
866
1125
|
});
|
|
867
1126
|
}
|
|
868
1127
|
/**
|
|
1128
|
+
* When track changes markers are present inside a LaTeX block:
|
|
1129
|
+
* - The LaTeX is preserved as text (not converted to MathML) to maintain suggestions.
|
|
1130
|
+
* - It also prevents the issue where the suggestions were placed as MathML tags.
|
|
1131
|
+
*
|
|
1132
|
+
* When no track changes markers are inside a LaTeX block:
|
|
1133
|
+
* - The LaTeX is converted to MathML normally. (previous default behavior)
|
|
1134
|
+
*
|
|
1135
|
+
* This is to ensure that:
|
|
1136
|
+
* 1. LaTeX without suggestions gets converted to MathML for final output.
|
|
1137
|
+
* 2. LaTeX with pending suggestions is preserved so setData(getData()) works correctly.
|
|
1138
|
+
*/ _endParseEditModeWithTrackChangesSupport(code) {
|
|
1139
|
+
if (!Configuration.get("parseModes").includes("latex")) {
|
|
1140
|
+
return code;
|
|
1141
|
+
}
|
|
1142
|
+
const latexBlockRegex = /\$\$([\s\S]*?)\$\$/g;
|
|
1143
|
+
const trackChangesRegex = /<(suggestion|comment)-(start|end)/i;
|
|
1144
|
+
//TODO: Validate if replace all is needed instead of just replace when it is all implemented.
|
|
1145
|
+
return code.replace(latexBlockRegex, (fullMatch, latexContent)=>{
|
|
1146
|
+
// Check if this LaTeX contains track changes markers to prevent conversion.
|
|
1147
|
+
if (trackChangesRegex.test(latexContent)) {
|
|
1148
|
+
return fullMatch;
|
|
1149
|
+
}
|
|
1150
|
+
// When LaTeX has no suggestion, it can be converted to MathML.
|
|
1151
|
+
const decodedLatex = Util.htmlEntitiesDecode(latexContent);
|
|
1152
|
+
let mathml = Util.htmlSanitize(Latex.getMathMLFromLatex(decodedLatex, true));
|
|
1153
|
+
if (!Configuration.get("saveHandTraces")) {
|
|
1154
|
+
mathml = MathML.removeAnnotation(mathml, "application/json");
|
|
1155
|
+
}
|
|
1156
|
+
return mathml;
|
|
1157
|
+
});
|
|
1158
|
+
}
|
|
1159
|
+
/**
|
|
869
1160
|
* Expose the WirisPlugin variable to the window
|
|
870
1161
|
*/ // eslint-disable-next-line class-methods-use-this
|
|
871
1162
|
_exposeWiris() {
|
|
@@ -892,8 +1183,21 @@ class MathType extends Plugin {
|
|
|
892
1183
|
// Adds custom label replacing the default 'mathml'.
|
|
893
1184
|
// Handles both singular and plural forms.
|
|
894
1185
|
trackChangesEditing.descriptionFactory.registerElementLabel("mathml", (quantity)=>(quantity > 1 ? `${quantity} ` : "") + StringManager.get(quantity > 1 ? "formulas" : "formula", integration?.getLanguage() || "en"));
|
|
1186
|
+
this._registerLatexTrackChangesAdapter(integration);
|
|
895
1187
|
}
|
|
896
1188
|
}
|
|
1189
|
+
/**
|
|
1190
|
+
* Register a custom adapter for handling LaTeX text changes.
|
|
1191
|
+
* This ensures that LaTeX formulas ($$...$$) are treated as atomic units
|
|
1192
|
+
* when used by the track changes feature and avoid partial edits.
|
|
1193
|
+
*/ _registerLatexTrackChangesAdapter(integration) {
|
|
1194
|
+
const { editor } = this;
|
|
1195
|
+
editor.model.document.on("change:data", ()=>{
|
|
1196
|
+
if (integration) {
|
|
1197
|
+
integration._trackChangesEnabled = editor.commands.get("trackChanges")?.value ?? false;
|
|
1198
|
+
}
|
|
1199
|
+
});
|
|
1200
|
+
}
|
|
897
1201
|
}
|
|
898
1202
|
|
|
899
1203
|
export { CKEditor5Integration, ChemTypeCommand, MathTypeCommand, MathType as default };
|