From 3354872837fb9b0431c371a5b9ef9eaec963b7f6 Mon Sep 17 00:00:00 2001 From: Sauli Anto Date: Sat, 7 Nov 2020 22:19:00 +0200 Subject: [PATCH] Add autoformat support --- README.md | 22 ++++++++++++++++++++++ src/autoformatmath.js | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 src/autoformatmath.js diff --git a/README.md b/README.md index ca87acd5f..9e6cafc7a 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ This is TeX-based mathematical plugin for CKEditor 5. You can use it to insert, * [Supported input and output formats](#supported-input-and-output-formats) - [Paste support](#paste-support) * [From plain text](#from-plain-text) +- [Autoformat support](#autoformat-support) - [Preview workaround](#preview-workaround) ## Features @@ -26,6 +27,7 @@ This is TeX-based mathematical plugin for CKEditor 5. You can use it to insert, - Have multiple input and output format - Paste support - from plain text +- Autoformat support # Demos - [Classic editor with MathJax](https://jsfiddle.net/isaul32/qktj9h7x/) @@ -155,6 +157,26 @@ or __\\(__ x=\frac{-b\pm\sqrt{b^2-4ac}}{2a} __\\)__ +### Autoformat support + +#### Inline mode +Ctrl+M can be used to add easily math formulas in inline mode. + +#### Display mode +Autoformat for math can be used to add formula in display mode on a new line by adding `\[` or `$$`. This feature requires additional autoformat plugin to be added. + +Add following lines into your build + +```js +// ... +import AutoformatMathematics from 'ckeditor5-math/src/autoformatmath'; + +InlineEditor.builtinPlugins = [ + // ... + AutoformatMathematics +]; +``` + ## Preview workaround __.ck-reset_all *__ css rules from ckeditor5-ui and ckeditor5-theme-lark break rendering in preview mode. diff --git a/src/autoformatmath.js b/src/autoformatmath.js new file mode 100644 index 000000000..0220ec53a --- /dev/null +++ b/src/autoformatmath.js @@ -0,0 +1,38 @@ +import Plugin from '@ckeditor/ckeditor5-core/src/plugin'; +import Autoformat from '@ckeditor/ckeditor5-autoformat/src/autoformat'; +import blockAutoformatEditing from '@ckeditor/ckeditor5-autoformat/src/blockautoformatediting'; + +import Math from './math'; + +export default class AutoformatMath extends Plugin { + static get requires() { + return [ Math, Autoformat ]; + } + + afterInit() { + const editor = this.editor; + const command = editor.commands.get( 'math' ); + + if ( command ) { + const mathBlockCallback = getCallbackFunctionForBlockAutoformat( editor, command ); + + blockAutoformatEditing( editor, this, /^\\\[$/, mathBlockCallback ); + blockAutoformatEditing( editor, this, /^\$\$$/, mathBlockCallback ); + } + } + + static get pluginName() { + return 'AutoformatMath'; + } +} + +function getCallbackFunctionForBlockAutoformat( editor, command ) { + return () => { + if ( !command.isEnabled ) { + return false; + } + + command.display = true; + editor.plugins.get( 'MathUI' )._showUI(); + } +}