Notes/_regroup/ckeditor5-mermaid/src/commands/insertMermaidCommand.js

53 lines
983 B
JavaScript
Raw Normal View History

2022-03-04 13:39:39 +01:00
/**
* @module mermaid/insertmermaidcommand
*/
import { Command } from 'ckeditor5/src/core.js';
2022-03-04 13:39:39 +01:00
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;
2022-03-10 13:13:54 +01:00
let mermaidItem;
2022-03-04 13:39:39 +01:00
model.change( writer => {
2022-03-10 13:13:54 +01:00
mermaidItem = writer.createElement( 'mermaid', {
2022-03-04 13:39:39 +01:00
displayMode: 'split',
source: MOCK_MERMAID_MARKUP
} );
2022-03-10 13:13:54 +01:00
model.insertContent( mermaidItem );
2022-03-04 13:39:39 +01:00
} );
2022-03-10 13:13:54 +01:00
return mermaidItem;
2022-03-04 13:39:39 +01:00
}
}