mirror of
https://github.com/zadam/trilium.git
synced 2025-12-05 06:54:23 +01:00
41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
import { checkIsOn } from '../utils.js';
|
|
import { Command, ModelElement } from 'ckeditor5';
|
|
|
|
/**
|
|
* The mermaid source view command.
|
|
*
|
|
* Allows to switch to a source view mode.
|
|
*
|
|
* @extends module:core/command~Command
|
|
*/
|
|
export default class MermaidSourceViewCommand 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, 'source' );
|
|
}
|
|
|
|
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' ) !== 'source' ) {
|
|
writer.setAttribute( 'displayMode', 'source', mermaidItem );
|
|
}
|
|
} );
|
|
}
|
|
}
|