[visual] Avoid pasting formatted content into a math container (#14475)

GitOrigin-RevId: e563e27bf80092df7b296878536ca2d4549c4d2a
This commit is contained in:
Alf Eaton 2023-08-29 11:04:54 +01:00 committed by Copybot
parent cf319b61b4
commit 1e286c263c
2 changed files with 15 additions and 1 deletions

View file

@ -1,5 +1,6 @@
import { EditorView } from '@codemirror/view' import { EditorView } from '@codemirror/view'
import { EditorSelection, Prec } from '@codemirror/state' import { EditorSelection, Prec } from '@codemirror/state'
import { ancestorNodeOfType } from '../../utils/tree-query'
export const pasteHtml = Prec.highest( export const pasteHtml = Prec.highest(
EditorView.domEventHandlers({ EditorView.domEventHandlers({
@ -29,6 +30,7 @@ export const pasteHtml = Prec.highest(
} }
const html = clipboardData.getData('text/html').trim() const html = clipboardData.getData('text/html').trim()
const text = clipboardData.getData('text/plain').trim()
if (html.length === 0) { if (html.length === 0) {
return false return false
@ -48,6 +50,16 @@ export const pasteHtml = Prec.highest(
view.dispatch( view.dispatch(
view.state.changeByRange(range => { 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 { return {
range: EditorSelection.cursor(range.from + latex.length), range: EditorSelection.cursor(range.from + latex.length),
changes: { from: range.from, to: range.to, insert: latex }, changes: { from: range.from, to: range.to, insert: latex },

View file

@ -45,7 +45,9 @@ describe('<CodeMirrorEditor/> paste HTML in Visual mode', function () {
cy.get('@content').trigger('paste', { clipboardData }) cy.get('@content').trigger('paste', { clipboardData })
cy.get('@content').should('have.text', 'foo') 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 () { it('handles a pasted bullet list', function () {