151 lines
3.7 KiB
JavaScript
Raw Normal View History

2022-03-04 13:39:39 +01:00
/**
* @module mermaid/mermaidui
*/
import { Plugin } from 'ckeditor5/src/core.js';
import { ButtonView } from 'ckeditor5/src/ui.js';
2022-03-04 13:39:39 +01:00
import insertMermaidIcon from '../theme/icons/insert.svg';
2022-03-08 11:07:02 +01:00
import previewModeIcon from '../theme/icons/preview-mode.svg';
import splitModeIcon from '../theme/icons/split-mode.svg';
import sourceModeIcon from '../theme/icons/source-mode.svg';
2022-03-04 13:39:39 +01:00
import infoIcon from '../theme/icons/info.svg';
2022-03-10 13:13:54 +01:00
/* global window, document */
2022-03-04 13:39:39 +01:00
export default class MermaidUI extends Plugin {
/**
* @inheritDoc
*/
static get pluginName() {
return 'MermaidUI';
}
/**
* @inheritDoc
*/
init() {
this._addButtons();
}
/**
* Adds all mermaid-related buttons.
*
* @private
*/
_addButtons() {
const editor = this.editor;
this._addInsertMermaidButton();
this._addMermaidInfoButton();
this._createToolbarButton( editor, 'mermaidPreview', 'Preview', previewModeIcon );
this._createToolbarButton( editor, 'mermaidSourceView', 'Source view', sourceModeIcon );
this._createToolbarButton( editor, 'mermaidSplitView', 'Split view', splitModeIcon );
}
/**
* Adds the button for inserting mermaid.
*
* @private
*/
_addInsertMermaidButton() {
const editor = this.editor;
const t = editor.t;
2022-03-10 13:13:54 +01:00
const view = editor.editing.view;
2022-03-04 13:39:39 +01:00
2022-03-08 11:11:59 +01:00
editor.ui.componentFactory.add( 'mermaid', locale => {
2022-03-04 13:39:39 +01:00
const buttonView = new ButtonView( locale );
const command = editor.commands.get( 'insertMermaidCommand' );
buttonView.set( {
2022-03-08 11:26:58 +01:00
label: t( 'Insert Mermaid diagram' ),
2022-03-04 13:39:39 +01:00
icon: insertMermaidIcon,
tooltip: true
} );
buttonView.bind( 'isOn', 'isEnabled' ).to( command, 'value', 'isEnabled' );
// Execute the command when the button is clicked.
command.listenTo( buttonView, 'execute', () => {
2022-03-10 13:13:54 +01:00
const mermaidItem = editor.execute( 'insertMermaidCommand' );
const mermaidItemViewElement = editor.editing.mapper.toViewElement( mermaidItem );
view.scrollToTheSelection();
view.focus();
2022-03-14 08:33:19 +01:00
if ( mermaidItemViewElement ) {
const mermaidItemDomElement = view.domConverter.viewToDom( mermaidItemViewElement, document );
if ( mermaidItemDomElement ) {
mermaidItemDomElement.querySelector( '.ck-mermaid__editing-view' ).focus();
}
2022-03-10 13:13:54 +01:00
}
2022-03-04 13:39:39 +01:00
} );
return buttonView;
} );
}
/**
* Adds the button linking to the mermaid guide.
*
* @private
*/
_addMermaidInfoButton() {
const editor = this.editor;
const t = editor.t;
editor.ui.componentFactory.add( 'mermaidInfo', locale => {
const buttonView = new ButtonView( locale );
2022-03-08 14:48:59 +01:00
const link = 'https://ckeditor.com/blog/basic-overview-of-creating-flowcharts-using-mermaid/';
2022-03-04 13:39:39 +01:00
buttonView.set( {
2022-03-08 11:04:25 +01:00
label: t( 'Read more about Mermaid diagram syntax' ),
2022-03-04 13:39:39 +01:00
icon: infoIcon,
tooltip: true
} );
buttonView.on( 'execute', () => {
window.open( link, '_blank', 'noopener' );
} );
return buttonView;
} );
}
/**
* Adds the mermaid balloon toolbar button.
*
* @private
* @param {module:core/editor/editor~Editor} editor
* @param {String} name Name of the button.
* @param {String} label Label for the button.
* @param {String} icon The button icon.
*/
_createToolbarButton( editor, name, label, icon ) {
const t = editor.t;
editor.ui.componentFactory.add( name, locale => {
const buttonView = new ButtonView( locale );
const command = editor.commands.get( `${ name }Command` );
buttonView.set( {
label: t( label ),
icon,
tooltip: true
} );
buttonView.bind( 'isOn', 'isEnabled' ).to( command, 'value', 'isEnabled' );
// Execute the command when the button is clicked.
command.listenTo( buttonView, 'execute', () => {
editor.execute( `${ name }Command` );
editor.editing.view.scrollToTheSelection();
editor.editing.view.focus();
} );
return buttonView;
} );
}
}