From aa0392c12fb22525d2c8b83aab5e2e92edae2bbc Mon Sep 17 00:00:00 2001 From: Sauli Anto Date: Sun, 18 Oct 2020 14:41:08 +0300 Subject: [PATCH] Add math editing button for balloon editor --- package.json | 4 ++-- src/mathui.js | 27 +++++++++++++++++---------- src/ui/mainformview.js | 10 +++++----- src/ui/mathview.js | 2 +- src/utils.js | 25 +++++++++++++++++++++++++ 5 files changed, 50 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index f2a20a388..b45ec4bcb 100644 --- a/package.json +++ b/package.json @@ -48,8 +48,8 @@ "theme" ], "scripts": { - "lint": "eslint --quiet **/*.js", - "lint:fix": "eslint --quiet **/*.js --fix", + "lint": "eslint --quiet src/**/*.js", + "lint:fix": "eslint --quiet src/**/*.js --fix", "stylelint": "stylelint --quiet --allow-empty-input 'theme/**/*.css' 'docs/**/*.css'", "test": "node node_modules/@ckeditor/ckeditor5-dev-tests/bin/test.js" }, diff --git a/src/mathui.js b/src/mathui.js index 10d829ded..ed0527505 100644 --- a/src/mathui.js +++ b/src/mathui.js @@ -4,6 +4,7 @@ import ContextualBalloon from '@ckeditor/ckeditor5-ui/src/panel/balloon/contextu import clickOutsideHandler from '@ckeditor/ckeditor5-ui/src/bindings/clickoutsidehandler'; import uid from '@ckeditor/ckeditor5-utils/src/uid'; import global from '@ckeditor/ckeditor5-utils/src/dom/global'; +import { getBalloonPositionData } from './utils'; import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview'; import MainFormView from './ui/mainformview'; @@ -68,7 +69,7 @@ export default class MathUI extends Plugin { const editor = this.editor; const mathCommand = editor.commands.get( 'math' ); - const mathConfig = this.editor.config.get( 'math' ); + const mathConfig = editor.config.get( 'math' ); const formView = new MainFormView( editor.locale, @@ -116,7 +117,7 @@ export default class MathUI extends Plugin { this._balloon.add( { view: this.formView, - position: this._getBalloonPositionData() + position: getBalloonPositionData( editor ) } ); if ( this._balloon.visibleView === this.formView ) { @@ -175,13 +176,6 @@ export default class MathUI extends Plugin { } } - _getBalloonPositionData() { - const view = this.editor.editing.view; - const viewDocument = view.document; - const target = view.domConverter.viewRangeToDom( viewDocument.selection.getFirstRange() ); - return { target }; - } - _createToolbarMathButton() { const editor = this.editor; const mathCommand = editor.commands.get( 'math' ); @@ -216,8 +210,21 @@ export default class MathUI extends Plugin { } _enableUserBalloonInteractions() { + const editor = this.editor; + if ( editor.constructor.name === 'BalloonEditor' ) { + const viewDocument = this.editor.editing.view.document; + this.listenTo( viewDocument, 'click', () => { + const mathCommand = editor.commands.get( 'math' ); + if ( mathCommand.value ) { + if ( mathCommand.isEnabled ) { + this._showUI(); + } + } + } ); + } + // Close the panel on the Esc key press when the editable has focus and the balloon is visible. - this.editor.keystrokes.set( 'Esc', ( data, cancel ) => { + editor.keystrokes.set( 'Esc', ( data, cancel ) => { if ( this._isUIVisible ) { this._hideUI(); cancel(); diff --git a/src/ui/mainformview.js b/src/ui/mainformview.js index 1389d8184..32e2e8824 100644 --- a/src/ui/mainformview.js +++ b/src/ui/mainformview.js @@ -74,7 +74,7 @@ export default class MainFormView extends View { attributes: { class: [ 'ck', - 'ck-math-form', + 'ck-math-form' ], tabindex: '-1', spellcheck: 'false' @@ -90,8 +90,8 @@ export default class MainFormView extends View { children }, this.saveButtonView, - this.cancelButtonView, - ], + this.cancelButtonView + ] } ); } @@ -100,7 +100,7 @@ export default class MainFormView extends View { // Prevent default form submit event & trigger custom 'submit' submitHandler( { - view: this, + view: this } ); // Register form elements to focusable elements @@ -108,7 +108,7 @@ export default class MainFormView extends View { this.mathInputView, this.displayButtonView, this.saveButtonView, - this.cancelButtonView, + this.cancelButtonView ]; childViews.forEach( v => { diff --git a/src/ui/mathview.js b/src/ui/mathview.js index 40e731b36..eb9261e40 100644 --- a/src/ui/mathview.js +++ b/src/ui/mathview.js @@ -25,7 +25,7 @@ export default class MathView extends View { class: [ 'ck', 'ck-math-preview' - ], + ] } } ); } diff --git a/src/utils.js b/src/utils.js index 51fa7d726..beddbdaa7 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,4 +1,5 @@ import global from '@ckeditor/ckeditor5-utils/src/dom/global'; +import BalloonPanelView from '@ckeditor/ckeditor5-ui/src/panel/balloon/balloonpanelview'; export function getSelectedMathModelWidget( selection ) { const selectedElement = selection.getSelectedElement(); @@ -104,6 +105,30 @@ export async function renderEquation( equation, element, engine = 'katex', lazyL } } +export function getBalloonPositionData( editor ) { + const view = editor.editing.view; + const defaultPositions = BalloonPanelView.defaultPositions; + + const selectedElement = view.document.selection.getSelectedElement(); + if ( selectedElement ) { + return { + target: view.domConverter.viewToDom( selectedElement ), + positions: [ + defaultPositions.southArrowNorth + ] + }; + } + else { + const viewDocument = view.document; + return { + target: view.domConverter.viewRangeToDom( viewDocument.selection.getFirstRange() ), + positions: [ + defaultPositions.southArrowNorth + ] + }; + } +} + function selectRenderMode( element, preview, previewUid, cb ) { if ( preview ) { createPreviewElement( element, previewUid, prewviewEl => {