diff --git a/services/web/frontend/js/features/source-editor/extensions/visual/paste-html.ts b/services/web/frontend/js/features/source-editor/extensions/visual/paste-html.ts index 8b9447c5da..ffaea6e8b4 100644 --- a/services/web/frontend/js/features/source-editor/extensions/visual/paste-html.ts +++ b/services/web/frontend/js/features/source-editor/extensions/visual/paste-html.ts @@ -1,5 +1,6 @@ import { EditorView } from '@codemirror/view' import { EditorSelection, Prec } from '@codemirror/state' +import { ancestorNodeOfType } from '../../utils/tree-query' export const pasteHtml = Prec.highest( EditorView.domEventHandlers({ @@ -29,6 +30,7 @@ export const pasteHtml = Prec.highest( } const html = clipboardData.getData('text/html').trim() + const text = clipboardData.getData('text/plain').trim() if (html.length === 0) { return false @@ -48,6 +50,16 @@ export const pasteHtml = Prec.highest( view.dispatch( view.state.changeByRange(range => { + // avoid pasting formatted content into a math container + if ( + ancestorNodeOfType(view.state, range.anchor, '$MathContainer') + ) { + return { + range: EditorSelection.cursor(range.from + text.length), + changes: { from: range.from, to: range.to, insert: text }, + } + } + return { range: EditorSelection.cursor(range.from + latex.length), changes: { from: range.from, to: range.to, insert: latex }, diff --git a/services/web/test/frontend/features/source-editor/components/codemirror-editor-visual-paste-html.spec.tsx b/services/web/test/frontend/features/source-editor/components/codemirror-editor-visual-paste-html.spec.tsx index eb5aad1127..5cbfdcc3f5 100644 --- a/services/web/test/frontend/features/source-editor/components/codemirror-editor-visual-paste-html.spec.tsx +++ b/services/web/test/frontend/features/source-editor/components/codemirror-editor-visual-paste-html.spec.tsx @@ -45,7 +45,9 @@ describe(' paste HTML in Visual mode', function () { cy.get('@content').trigger('paste', { clipboardData }) cy.get('@content').should('have.text', 'foo') - cy.get('@get-data').should('have.been.calledOnceWithExactly', 'text/html') + cy.get('@get-data').should('have.been.calledTwice') + cy.get('@get-data').should('have.been.calledWithExactly', 'text/html') + cy.get('@get-data').should('have.been.calledWithExactly', 'text/plain') }) it('handles a pasted bullet list', function () {