Files
Notes/src/commands/insertMermaidCommand.js
2024-05-20 14:27:21 +02:00

53 lines
983 B
JavaScript

/**
* @module mermaid/insertmermaidcommand
*/
import { Command } from 'ckeditor5/src/core.js';
const MOCK_MERMAID_MARKUP = `flowchart TB
A --> B
B --> C`;
/**
* The insert mermaid command.
*
* Allows to insert mermaid.
*
* @extends module:core/command~Command
*/
export default class InsertMermaidCommand extends Command {
/**
* @inheritDoc
*/
refresh() {
const documentSelection = this.editor.model.document.selection;
const selectedElement = documentSelection.getSelectedElement();
if ( selectedElement && selectedElement.name === 'mermaid' ) {
this.isEnabled = false;
} else {
this.isEnabled = true;
}
}
/**
* @inheritDoc
*/
execute() {
const editor = this.editor;
const model = editor.model;
let mermaidItem;
model.change( writer => {
mermaidItem = writer.createElement( 'mermaid', {
displayMode: 'split',
source: MOCK_MERMAID_MARKUP
} );
model.insertContent( mermaidItem );
} );
return mermaidItem;
}
}