trilium/_regroup/ckeditor5-mermaid/src/commands/mermaidPreviewCommand.js
Elian Doran 178903f6b2 Add '_regroup/ckeditor5-mermaid/' from commit 'c15257da7e57b6303fda9744ee4153d1c5311d6f'
git-subtree-dir: _regroup/ckeditor5-mermaid
git-subtree-mainline: 90c0f417131d254e86a4a4ab391122cad0930db7
git-subtree-split: c15257da7e57b6303fda9744ee4153d1c5311d6f
2025-05-04 15:23:12 +03:00

51 lines
1.3 KiB
JavaScript

/**
* @module mermaid/mermaidpreviewcommand
*/
import { Command } from 'ckeditor5/src/core.js';
import { checkIsOn } from '../utils.js';
/**
* The mermaid preview command.
*
* Allows to switch to a preview mode.
*
* @extends module:core/command~Command
*/
export default class MermaidPreviewCommand extends Command {
/**
* @inheritDoc
*/
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, 'preview' );
}
/**
* @inheritDoc
*/
execute() {
const editor = this.editor;
const model = editor.model;
const documentSelection = this.editor.model.document.selection;
const mermaidItem = documentSelection.getSelectedElement() || documentSelection.getLastPosition().parent;
model.change( writer => {
if ( mermaidItem.getAttribute( 'displayMode' ) !== 'preview' ) {
writer.setAttribute( 'displayMode', 'preview', mermaidItem );
}
} );
}
}