trilium/packages/ckeditor5-mermaid/src/commands/insertMermaidCommand.ts
2025-05-04 17:12:49 +03:00

42 lines
846 B
TypeScript

import { Command } from "ckeditor5";
const MOCK_MERMAID_MARKUP = `flowchart TB
A --> B
B --> C`;
/**
* The insert mermaid command.
*
* Allows to insert mermaid.
*/
export default class InsertMermaidCommand extends Command {
override refresh() {
const documentSelection = this.editor.model.document.selection;
const selectedElement = documentSelection.getSelectedElement();
if ( selectedElement && selectedElement.name === 'mermaid' ) {
this.isEnabled = false;
} else {
this.isEnabled = true;
}
}
override 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;
}
}