2022-03-04 13:39:39 +01:00
|
|
|
/**
|
|
|
|
* @module mermaid/mermaidpreviewcommand
|
|
|
|
*/
|
|
|
|
|
2024-05-20 14:27:21 +02:00
|
|
|
import { Command } from 'ckeditor5/src/core.js';
|
2022-03-04 13:39:39 +01:00
|
|
|
|
2024-05-20 14:27:21 +02:00
|
|
|
import { checkIsOn } from '../utils.js';
|
2022-03-04 13:39:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|