trilium/packages/ckeditor5-mermaid/src/commands/mermaidSplitViewCommand.ts

43 lines
1.2 KiB
TypeScript

/**
* @module mermaid/mermaidsplitviewcommand
*/
import { checkIsOn } from '../utils.js';
import { Command, ModelElement } from 'ckeditor5';
/**
* The mermaid split view command.
*
* Allows to switch to a split view mode.
*/
export default class MermaidSplitViewCommand extends Command {
override refresh() {
const editor = this.editor;
const documentSelection = editor.model.document.selection;
const selectedElement = documentSelection.getSelectedElement();
const isSelectedElementMermaid = selectedElement && selectedElement.name === 'mermaid';
if ( isSelectedElementMermaid || documentSelection.getLastPosition()?.findAncestor( 'mermaid' ) ) {
this.isEnabled = !!selectedElement;
} else {
this.isEnabled = false;
}
this.value = checkIsOn( editor, 'split' );
}
override execute() {
const editor = this.editor;
const model = editor.model;
const documentSelection = this.editor.model.document.selection;
const mermaidItem = (documentSelection.getSelectedElement() || documentSelection.getLastPosition()?.parent) as ModelElement;
model.change( writer => {
if ( mermaidItem.getAttribute( 'displayMode' ) !== 'split' ) {
writer.setAttribute( 'displayMode', 'split', mermaidItem );
}
} );
}
}