152 lines
4.0 KiB
TypeScript
Raw Normal View History

2022-03-04 13:39:39 +01:00
/**
* @module mermaid/mermaidui
*/
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';
import { ButtonView, Editor, Element, Locale, Observable, Plugin } from 'ckeditor5';
import InsertMermaidCommand from './commands/insertMermaidCommand.js';
2022-03-04 13:39:39 +01:00
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' as const;
2022-03-04 13:39:39 +01:00
}
/**
* @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
editor.ui.componentFactory.add( 'mermaid', (locale: Locale) => {
2022-03-04 13:39:39 +01:00
const buttonView = new ButtonView( locale );
const command = editor.commands.get( 'insertMermaidCommand' ) as InsertMermaidCommand;
if (!command) {
throw new Error("Missing command.");
}
2022-03-04 13:39:39 +01:00
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 as (Observable & { value: boolean; } & { isEnabled: boolean; }), 'value', 'isEnabled' );
2022-03-04 13:39:39 +01:00
// Execute the command when the button is clicked.
command.listenTo( buttonView, 'execute', () => {
const mermaidItem = editor.execute( 'insertMermaidCommand' ) as Element;
2022-03-10 13:13:54 +01:00
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 );
2022-03-14 08:33:19 +01:00
if ( mermaidItemDomElement ) {
(mermaidItemDomElement.querySelector( '.ck-mermaid__editing-view' ) as HTMLElement)?.focus();
2022-03-14 08:33:19 +01:00
}
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
*/
_createToolbarButton( editor: Editor, name: string, label: string, icon: string ) {
2022-03-04 13:39:39 +01:00
const t = editor.t;
editor.ui.componentFactory.add( name, locale => {
const buttonView = new ButtonView( locale );
const command = editor.commands.get( `${ name }Command` );
if (!command) {
throw new Error("Missing command.");
}
2022-03-04 13:39:39 +01:00
buttonView.set( {
label: t( label ),
icon,
tooltip: true
} );
buttonView.bind( 'isOn', 'isEnabled' ).to( command as (Observable & { value: boolean; } & { isEnabled: boolean; }), 'value', 'isEnabled' );
2022-03-04 13:39:39 +01:00
// 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;
} );
}
}