Skip to content

Build your personal knowledge base with Trilium Notes

refactor(ckeditor5-math): drop the AutoMath paste feature

Elian Doran5f833734c24a
diff --git a/packages/ckeditor5-math/src/automath.ts b/packages/ckeditor5-math/src/automath.ts
deleted file mode 100644
index 7519793c7a..0000000000
--- a/packages/ckeditor5-math/src/automath.ts
+++ /dev/null
@@ -1,137 +0,0 @@
-import { Clipboard, ClipboardPipeline, Plugin, type Editor, ModelLivePosition, ModelLiveRange, Undo } from 'ckeditor5';
-import { extractDelimiters, hasDelimiters, delimitersCounts } from './utils.js';
-
-export default class AutoMath extends Plugin {
- public static get requires() {
- return [ Clipboard, ClipboardPipeline, Undo ] as const;
- }
-
- public static get pluginName() {
- return 'AutoMath' as const;
- }
-
- private _timeoutId: null | number;
- private _positionToInsert: null | ModelLivePosition;
-
- constructor( editor: Editor ) {
- super( editor );
-
- this._timeoutId = null;
-
- this._positionToInsert = null;
- }
-
- public init(): void {
- const editor = this.editor;
- const modelDocument = editor.model.document;
-
- // `inputTransformation` is emitted by ClipboardPipeline, not by the Clipboard facade.
- // Listening on Clipboard silently never fires, which left this whole feature dead.
- this.listenTo( editor.plugins.get( ClipboardPipeline ), 'inputTransformation', () => {
- const firstRange = modelDocument.selection.getFirstRange();
- /* v8 ignore next 3 -- defensive: the document selection always has at least one range */
- if ( !firstRange ) {
- return;
- }
-
- const leftLivePosition = ModelLivePosition.fromPosition( firstRange.start );
- leftLivePosition.stickiness = 'toPrevious';
-
- const rightLivePosition = ModelLivePosition.fromPosition( firstRange.end );
- rightLivePosition.stickiness = 'toNext';
-
- modelDocument.once( 'change:data', () => {
- this._mathBetweenPositions(
- leftLivePosition,
- rightLivePosition
- );
-
- leftLivePosition.detach();
- rightLivePosition.detach();
- },
- { priority: 'high' }
- );
- }
- );
-
- editor.commands.get( 'undo' )?.on( 'execute', () => {
- if ( this._timeoutId ) {
- window.clearTimeout( this._timeoutId );
- this._positionToInsert?.detach();
-
- this._timeoutId = null;
- this._positionToInsert = null;
- }
- }, { priority: 'high' } );
- }
-
- private _mathBetweenPositions(
- leftPosition: ModelLivePosition,
- rightPosition: ModelLivePosition
- ) {
- const editor = this.editor;
-
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- const mathConfig = this.editor.config.get( 'math' );
-
- const equationRange = new ModelLiveRange( leftPosition, rightPosition );
- const walker = equationRange.getWalker( { ignoreElementEnd: true } );
-
- let text = '';
-
- // Get equation text
- for ( const node of walker ) {
- if ( node.item.is( '$textProxy' ) ) {
- text += node.item.data;
- }
- }
-
- text = text.trim();
-
- // Skip if don't have delimiters
- if ( !hasDelimiters( text ) || delimitersCounts( text ) !== 2 ) {
- return;
- }
-
- const mathCommand = editor.commands.get( 'math' );
-
- // Do not anything if math element cannot be inserted at the current position
- if ( !mathCommand?.isEnabled ) {
- return;
- }
-
- this._positionToInsert = ModelLivePosition.fromPosition( leftPosition );
-
- // With timeout user can undo conversation if want use plain text
- this._timeoutId = window.setTimeout( () => {
- editor.model.change( writer => {
- this._timeoutId = null;
-
- writer.remove( equationRange );
-
- let insertPosition: ModelLivePosition | null;
-
- // Check if position where the math element should be inserted is still valid.
- /* v8 ignore next -- defensive: reachable only if the document is rewritten under the 100ms timer */
- if ( this._positionToInsert?.root.rootName !== '$graveyard' ) {
- insertPosition = this._positionToInsert;
- }
-
- editor.model.change( innerWriter => {
- const params = Object.assign( extractDelimiters( text ), {
- type: mathConfig?.outputType
- } );
- const mathElement = innerWriter.createElement( params.display ? 'mathtex-display' : 'mathtex-inline', params
- );
-
- editor.model.insertContent( mathElement, insertPosition );
-
- innerWriter.setSelection( mathElement, 'on' );
- } );
-
- this._positionToInsert?.detach();
- this._positionToInsert = null;
- } );
- }, 100 );
- }
-}
diff --git a/packages/ckeditor5-math/src/math.ts b/packages/ckeditor5-math/src/math.ts
index fd9debb652..8fae5820d0 100644
--- a/packages/ckeditor5-math/src/math.ts
+++ b/packages/ckeditor5-math/src/math.ts
@@ -1,11 +1,10 @@
import { Plugin, Widget } from 'ckeditor5';
import MathEditing from './mathediting.js';
import MathUI from './mathui.js';
-import AutoMath from './automath.js';
export default class Math extends Plugin {
public static get requires() {
- return [ MathEditing, MathUI, AutoMath, Widget ] as const;
+ return [ MathEditing, MathUI, Widget ] as const;
}
public static get pluginName() {
diff --git a/packages/ckeditor5-math/src/utils.ts b/packages/ckeditor5-math/src/utils.ts
index 532c4162b6..b0734cb6e0 100644
--- a/packages/ckeditor5-math/src/utils.ts
+++ b/packages/ckeditor5-math/src/utils.ts
@@ -38,11 +38,6 @@ export function hasDelimiters( text: string ): RegExpMatchArray | null {
return text.match( /^(\\\[.*?\\\]|\\\(.*?\\\))$/ );
}
-// Find delimiters count
-export function delimitersCounts( text: string ): number | undefined {
- return text.match( /(\\\[|\\\]|\\\(|\\\))/g )?.length;
-}
-
// Extract delimiters and figure display mode for the model
export function extractDelimiters( equation: string ): {
equation: string;
diff --git a/packages/ckeditor5-math/tests/automath.ts b/packages/ckeditor5-math/tests/automath.ts
deleted file mode 100644
index b13941761a..0000000000
--- a/packages/ckeditor5-math/tests/automath.ts
+++ /dev/null
@@ -1,210 +0,0 @@
-import Mathematics from '../src/math.js';
-import AutoMath from '../src/automath.js';
-import { ClassicEditor, Clipboard, Paragraph, Undo, Typing, _getModelData as getData, _setModelData as setData } from 'ckeditor5';
-import { describe, beforeEach, it, afterEach, vi, expect } from "vitest";
-
-describe( 'AutoMath - integration', () => {
- let editorElement: HTMLDivElement, editor: ClassicEditor;
-
- beforeEach( async () => {
- editorElement = document.createElement( 'div' );
- document.body.appendChild( editorElement );
-
- return ClassicEditor
- .create( editorElement, {
- plugins: [ Mathematics, AutoMath, Typing, Paragraph ],
- licenseKey: "GPL",
- math: {
- engine: ( equation, element, display ) => {
- if ( display ) {
- element.innerHTML = '\\[' + equation + '\\]';
- } else {
- element.innerHTML = '\\(' + equation + '\\)';
- }
- }
- }
- } )
- .then( newEditor => {
- editor = newEditor;
- } );
- } );
-
- afterEach( () => {
- editorElement.remove();
-
- return editor.destroy();
- } );
-
- it( 'should load Clipboard plugin', () => {
- expect( editor.plugins.get( Clipboard ) ).to.instanceOf( Clipboard );
- } );
-
- it( 'should load Undo plugin', () => {
- expect( editor.plugins.get( Undo ) ).to.instanceOf( Undo );
- } );
-
- it( 'has proper name', () => {
- expect( AutoMath.pluginName ).to.equal( 'AutoMath' );
- } );
-
- describe( 'converting pasted equations', () => {
- beforeEach( () => {
- vi.useFakeTimers();
- } );
-
- afterEach( () => {
- vi.useRealTimers();
- } );
-
- it( 'replaces pasted text with a display equation after 100ms', () => {
- setData( editor.model, '<paragraph>[]</paragraph>' );
- pasteHtml( editor, '\\[x^2\\]' );
-
- expect( getData( editor.model ) ).to.equal(
- '<paragraph>\\[x^2\\][]</paragraph>'
- );
-
- vi.advanceTimersByTime( 100 );
-
- // A display equation is a block element, so it replaces the empty paragraph outright.
- expect( getData( editor.model ) ).to.equal( '[<mathtex-display display="true" equation="x^2" type="script"></mathtex-display>]' );
- } );
-
- it( 'replaces pasted text with an inline equation after 100ms', () => {
- setData( editor.model, '<paragraph>[]</paragraph>' );
- pasteHtml( editor, '\\(x^2\\)' );
-
- expect( getData( editor.model ) ).to.equal(
- '<paragraph>\\(x^2\\)[]</paragraph>'
- );
-
- vi.advanceTimersByTime( 100 );
-
- expect( getData( editor.model ) ).to.equal( '<paragraph>[<mathtex-inline display="false" equation="x^2" type="script"></mathtex-inline>]</paragraph>' );
- } );
-
- it( 'does not convert before the delay elapses', () => {
- setData( editor.model, '<paragraph>[]</paragraph>' );
- pasteHtml( editor, '\\(x^2\\)' );
-
- vi.advanceTimersByTime( 99 );
-
- expect( getData( editor.model ) ).to.equal( '<paragraph>\\(x^2\\)[]</paragraph>' );
- } );
-
- it( 'can undo auto-mathing', () => {
- setData( editor.model, '<paragraph>[]</paragraph>' );
- pasteHtml( editor, '\\[x^2\\]' );
-
- expect( getData( editor.model ) ).to.equal(
- '<paragraph>\\[x^2\\][]</paragraph>'
- );
-
- vi.advanceTimersByTime( 100 );
-
- editor.commands.execute( 'undo' );
-
- expect( getData( editor.model ) ).to.equal(
- '<paragraph>\\[x^2\\][]</paragraph>'
- );
- } );
-
- it( 'cancels the pending conversion when undo runs first', () => {
- setData( editor.model, '<paragraph>[]</paragraph>' );
- pasteHtml( editor, '\\(x^2\\)' );
-
- // Undo before the timeout fires: the scheduled conversion must be dropped, not applied.
- editor.commands.execute( 'undo' );
- vi.advanceTimersByTime( 100 );
-
- expect( getData( editor.model ) ).to.not.contain( 'mathtex' );
- } );
-
- it( 'works for a non-collapsed selection inside a single element', () => {
- setData( editor.model, '<paragraph>[Foo]</paragraph>' );
- pasteHtml( editor, '\\(x^2\\)' );
-
- vi.advanceTimersByTime( 100 );
-
- expect( getData( editor.model ) ).to.equal( '<paragraph>[<mathtex-inline display="false" equation="x^2" type="script"></mathtex-inline>]</paragraph>' );
- } );
-
- it( 'works for a non-collapsed selection over a few elements', () => {
- setData( editor.model, '<paragraph>Fo[o</paragraph><paragraph>Ba]r</paragraph>' );
- pasteHtml( editor, '\\(x^2\\)' );
-
- vi.advanceTimersByTime( 100 );
-
- expect( getData( editor.model ) ).to.equal( '<paragraph>Fo[<mathtex-inline display="false" equation="x^2" type="script"></mathtex-inline>]r</paragraph>' );
- } );
-
- it( 'inserts an inline equation in-place (collapsed selection)', () => {
- setData( editor.model, '<paragraph>Foo []Bar</paragraph>' );
- pasteHtml( editor, '\\(x^2\\)' );
-
- vi.advanceTimersByTime( 100 );
-
- expect( getData( editor.model ) ).to.equal(
- '<paragraph>Foo [<mathtex-inline display="false" equation="x^2" type="script"></mathtex-inline>]Bar</paragraph>'
- );
- } );
-
- it( 'inserts an inline equation in-place (non-collapsed selection)', () => {
- setData( editor.model, '<paragraph>Foo [Bar] Baz</paragraph>' );
- pasteHtml( editor, '\\(x^2\\)' );
-
- vi.advanceTimersByTime( 100 );
-
- expect( getData( editor.model ) ).to.equal(
- '<paragraph>Foo [<mathtex-inline display="false" equation="x^2" type="script"></mathtex-inline>] Baz</paragraph>'
- );
- } );
-
- it( 'splits the paragraph around a pasted display equation', () => {
- setData( editor.model, '<paragraph>Foo []Bar</paragraph>' );
- pasteHtml( editor, '\\[x^2\\]' );
-
- vi.advanceTimersByTime( 100 );
-
- expect( getData( editor.model ) ).to.equal(
- '<paragraph>Foo </paragraph>[<mathtex-display display="true" equation="x^2" type="script"></mathtex-display>]<paragraph>Bar</paragraph>'
- );
- } );
-
- it( 'does nothing if two equations are pasted as text', () => {
- setData( editor.model, '<paragraph>[]</paragraph>' );
- pasteHtml( editor, '\\[x^2\\] \\[\\sqrt{x}2\\]' );
-
- vi.advanceTimersByTime( 100 );
-
- expect( getData( editor.model ) ).to.equal(
- '<paragraph>\\[x^2\\] \\[\\sqrt{x}2\\][]</paragraph>'
- );
- } );
-
- it( 'does nothing for pasted text without delimiters', () => {
- setData( editor.model, '<paragraph>[]</paragraph>' );
- pasteHtml( editor, 'x^2' );
-
- vi.advanceTimersByTime( 100 );
-
- expect( getData( editor.model ) ).to.equal( '<paragraph>x^2[]</paragraph>' );
- } );
- } );
-
- function pasteHtml( editor: ClassicEditor, html: string ) {
- editor.editing.view.document.fire( 'paste', {
- dataTransfer: createDataTransfer( { 'text/html': html } ),
- preventDefault() {},
- stopPropagation() {}
- } );
- }
-
- function createDataTransfer( data: Record<string, string> ) {
- return {
- getData( type: string ) {
- return data[ type ];
- }
- };
- }
-} );
diff --git a/packages/ckeditor5-math/tests/math.ts b/packages/ckeditor5-math/tests/math.ts
index 48d0ca64d2..65d3db1e47 100644
--- a/packages/ckeditor5-math/tests/math.ts
+++ b/packages/ckeditor5-math/tests/math.ts
@@ -2,7 +2,6 @@ import { ClassicEditor, Widget } from 'ckeditor5';
import Mathematics from '../src/math.js';
import MathEditing from '../src/mathediting.js';
import MathUI from '../src/mathui.js';
-import AutoMath from '../src/automath.js';
import { describe, beforeEach, it, afterEach, expect } from "vitest";
describe( 'Math', () => {
@@ -44,10 +43,6 @@ describe( 'Math', () => {
expect( editor.plugins.get( MathUI ) ).to.instanceOf( MathUI );
} );
- it( 'should load AutoMath plugin', () => {
- expect( editor.plugins.get( AutoMath ) ).to.instanceOf( AutoMath );
- } );
-
it( 'has proper name', () => {
expect( Mathematics.pluginName ).to.equal( 'Math' );
} );
diff --git a/packages/ckeditor5-math/tests/utils-pure.ts b/packages/ckeditor5-math/tests/utils-pure.ts
index eb470e98d7..f0a17de0f9 100644
--- a/packages/ckeditor5-math/tests/utils-pure.ts
+++ b/packages/ckeditor5-math/tests/utils-pure.ts
@@ -1,7 +1,6 @@
import { describe, expect, it } from 'vitest';
import {
- delimitersCounts,
extractDelimiters,
hasDelimiters,
isMathJaxVersion2,
@@ -25,17 +24,6 @@ describe( 'utils: delimiter helpers', () => {
} );
} );
- describe( 'delimitersCounts', () => {
- it( 'counts every delimiter token', () => {
- expect( delimitersCounts( '\\[x\\]' ) ).toBe( 2 );
- expect( delimitersCounts( '\\(a\\) and \\(b\\)' ) ).toBe( 4 );
- } );
-
- it( 'returns undefined when there are none', () => {
- expect( delimitersCounts( 'x^2' ) ).toBeUndefined();
- } );
- } );
-
describe( 'extractDelimiters', () => {
it( 'strips display delimiters and reports display mode', () => {
expect( extractDelimiters( '\\[x^2\\]' ) ).toEqual( { equation: 'x^2', display: true } );