Skip to content

Commit b877319

Browse files
fix: Preview track changes
1 parent cfbcc1c commit b877319

File tree

7 files changed

+91
-7
lines changed

7 files changed

+91
-7
lines changed

packages/ckeditor5/dist/browser/index.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11822,7 +11822,8 @@ class MathType extends Plugin {
1182211822
imgElement = htmlDataProcessor.toView(htmlContent).getChild(0);
1182311823
} else if (formula) {
1182411824
const mathString = formula.replaceAll('ref="<"', 'ref="&lt;"');
11825-
const imgHtml = Parser.initParse(mathString, integration.getLanguage());
11825+
const lang = integration.getLanguage() || 'en'; // Safe fallback to 'en' in case integration is undefined.
11826+
const imgHtml = Parser.initParse(mathString, lang);
1182611827
imgElement = htmlDataProcessor.toView(imgHtml).getChild(0);
1182711828
// Add HTML element (<img>) to model
1182811829
viewWriter.setAttribute("htmlContent", imgHtml, modelItem);
@@ -11880,11 +11881,30 @@ class MathType extends Plugin {
1188011881
editor.editing.mapper.on("viewToModelPosition", viewToModelPositionOutsideModelElement(editor.model, (viewElement)=>viewElement.hasClass("ck-math-widget")));
1188111882
// Keep a reference to the original get and set function.
1188211883
const { get, set } = editor.data;
11884+
// Listen to the preview command execution to set a flag in localStorage.
11885+
// This flag will be used in the getData() to prevent converting formulas while generating the preview.
11886+
// This is necessary because the preview command uses editor.getData() multiple times internally.
11887+
const previewCommand = editor.commands.get('previewFinalContent');
11888+
if (previewCommand) {
11889+
this.listenTo(previewCommand, 'execute', ()=>{
11890+
localStorage.setItem("isGeneratingPreview", true);
11891+
setTimeout(()=>{
11892+
localStorage.setItem("isGeneratingPreview", false);
11893+
}, 1000);
11894+
}, {
11895+
priority: 'high'
11896+
});
11897+
}
1188311898
/**
1188411899
* Hack to transform $$latex$$ into <math> in editor.getData()'s output.
1188511900
*/ editor.data.on("get", (e)=>{
1188611901
const output = e.return;
1188711902
const parsedResult = Parser.endParse(output);
11903+
// Skip conversion if we are generating the preview, we need the formula images.
11904+
const retrievedObject = localStorage.getItem("isGeneratingPreview");
11905+
if (retrievedObject === "true") {
11906+
return;
11907+
}
1188811908
// Cleans all the semantics tag for safexml
1188911909
// including the handwritten data points
1189011910
e.return = MathML.removeSafeXMLSemantics(parsedResult);

packages/ckeditor5/dist/browser/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/ckeditor5/dist/browser/index.umd.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11824,7 +11824,8 @@
1182411824
imgElement = htmlDataProcessor.toView(htmlContent).getChild(0);
1182511825
} else if (formula) {
1182611826
const mathString = formula.replaceAll('ref="<"', 'ref="&lt;"');
11827-
const imgHtml = Parser.initParse(mathString, integration.getLanguage());
11827+
const lang = integration.getLanguage() || 'en'; // Safe fallback to 'en' in case integration is undefined.
11828+
const imgHtml = Parser.initParse(mathString, lang);
1182811829
imgElement = htmlDataProcessor.toView(imgHtml).getChild(0);
1182911830
// Add HTML element (<img>) to model
1183011831
viewWriter.setAttribute("htmlContent", imgHtml, modelItem);
@@ -11882,11 +11883,30 @@
1188211883
editor.editing.mapper.on("viewToModelPosition", ckeditor5.viewToModelPositionOutsideModelElement(editor.model, (viewElement)=>viewElement.hasClass("ck-math-widget")));
1188311884
// Keep a reference to the original get and set function.
1188411885
const { get, set } = editor.data;
11886+
// Listen to the preview command execution to set a flag in localStorage.
11887+
// This flag will be used in the getData() to prevent converting formulas while generating the preview.
11888+
// This is necessary because the preview command uses editor.getData() multiple times internally.
11889+
const previewCommand = editor.commands.get('previewFinalContent');
11890+
if (previewCommand) {
11891+
this.listenTo(previewCommand, 'execute', ()=>{
11892+
localStorage.setItem("isGeneratingPreview", true);
11893+
setTimeout(()=>{
11894+
localStorage.setItem("isGeneratingPreview", false);
11895+
}, 1000);
11896+
}, {
11897+
priority: 'high'
11898+
});
11899+
}
1188511900
/**
1188611901
* Hack to transform $$latex$$ into <math> in editor.getData()'s output.
1188711902
*/ editor.data.on("get", (e)=>{
1188811903
const output = e.return;
1188911904
const parsedResult = Parser.endParse(output);
11905+
// Skip conversion if we are generating the preview, we need the formula images.
11906+
const retrievedObject = localStorage.getItem("isGeneratingPreview");
11907+
if (retrievedObject === "true") {
11908+
return;
11909+
}
1189011910
// Cleans all the semantics tag for safexml
1189111911
// including the handwritten data points
1189211912
e.return = MathML.removeSafeXMLSemantics(parsedResult);

packages/ckeditor5/dist/browser/index.umd.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/ckeditor5/dist/index.js

Lines changed: 21 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/ckeditor5/dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/ckeditor5/src/plugin.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,9 @@ export default class MathType extends Plugin {
430430
imgElement = htmlDataProcessor.toView(htmlContent).getChild(0);
431431
} else if (formula) {
432432
const mathString = formula.replaceAll('ref="<"', 'ref="&lt;"');
433+
const lang = integration.getLanguage() || 'en'; // Safe fallback to 'en' in case integration is undefined.
434+
const imgHtml = Parser.initParse(mathString, lang);
433435

434-
const imgHtml = Parser.initParse(mathString, integration.getLanguage());
435436
imgElement = htmlDataProcessor.toView(imgHtml).getChild(0);
436437

437438
// Add HTML element (<img>) to model
@@ -507,6 +508,22 @@ export default class MathType extends Plugin {
507508
// Keep a reference to the original get and set function.
508509
const { get, set } = editor.data;
509510

511+
// Listen to the preview command execution to set a flag in localStorage.
512+
// This flag will be used in the getData() to prevent converting formulas while generating the preview.
513+
// This is necessary because the preview command uses editor.getData() multiple times internally.
514+
const previewCommand = editor.commands.get('previewFinalContent');
515+
516+
if (previewCommand) {
517+
this.listenTo(previewCommand, 'execute', () => {
518+
localStorage.setItem("isGeneratingPreview", true);
519+
520+
setTimeout(() => {
521+
localStorage.setItem("isGeneratingPreview", false);
522+
}, 1000);
523+
}, { priority: 'high' });
524+
}
525+
526+
510527
/**
511528
* Hack to transform $$latex$$ into <math> in editor.getData()'s output.
512529
*/
@@ -516,6 +533,13 @@ export default class MathType extends Plugin {
516533
const output = e.return;
517534
const parsedResult = Parser.endParse(output);
518535

536+
// Skip conversion if we are generating the preview, we need the formula images.
537+
const retrievedObject = localStorage.getItem("isGeneratingPreview");
538+
539+
if (retrievedObject === "true") {
540+
return;
541+
}
542+
519543
// Cleans all the semantics tag for safexml
520544
// including the handwritten data points
521545
e.return = MathML.removeSafeXMLSemantics(parsedResult);

0 commit comments

Comments
 (0)