2025-03-13 18:27:05 +02:00
|
|
|
/**
|
|
|
|
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
|
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2025-03-13 19:02:10 +02:00
|
|
|
* @module admonition/admonitionui
|
2025-03-13 18:27:05 +02:00
|
|
|
*/
|
|
|
|
|
2025-03-13 20:07:55 +02:00
|
|
|
import { Plugin, } from 'ckeditor5/src/core.js';
|
|
|
|
import { addListToDropdown, createDropdown, ListDropdownButtonDefinition, ViewModel } from 'ckeditor5/src/ui.js';
|
2025-03-13 18:27:05 +02:00
|
|
|
|
|
|
|
import '../theme/blockquote.css';
|
2025-03-13 19:10:07 +02:00
|
|
|
import admonitionIcon from '../theme/icons/admonition.svg';
|
2025-03-13 20:07:55 +02:00
|
|
|
import { Collection } from '@ckeditor/ckeditor5-utils';
|
|
|
|
|
|
|
|
interface AdmonitionDefinition {
|
|
|
|
title: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ADMONITION_TYPES: Record<string, AdmonitionDefinition> = {
|
|
|
|
"note": {
|
|
|
|
title: "Note"
|
|
|
|
},
|
|
|
|
"tip": {
|
|
|
|
title: "Tip"
|
|
|
|
},
|
|
|
|
"important": {
|
|
|
|
title: "Important"
|
|
|
|
},
|
|
|
|
"caution": {
|
|
|
|
title: "Caution"
|
|
|
|
},
|
|
|
|
"warning": {
|
|
|
|
title: "Warning"
|
|
|
|
}
|
|
|
|
};
|
2025-03-13 18:27:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The block quote UI plugin.
|
|
|
|
*
|
2025-03-13 19:02:10 +02:00
|
|
|
* It introduces the `'admonition'` button.
|
2025-03-13 18:27:05 +02:00
|
|
|
*
|
|
|
|
* @extends module:core/plugin~Plugin
|
|
|
|
*/
|
2025-03-13 18:33:39 +02:00
|
|
|
export default class AdmonitionUI extends Plugin {
|
2025-03-13 18:27:05 +02:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public static get pluginName() {
|
2025-03-13 18:41:37 +02:00
|
|
|
return 'AdmonitionUI' as const;
|
2025-03-13 18:27:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public init(): void {
|
|
|
|
const editor = this.editor;
|
|
|
|
|
2025-03-13 19:02:10 +02:00
|
|
|
editor.ui.componentFactory.add( 'admonition', () => {
|
2025-03-13 20:07:55 +02:00
|
|
|
const buttonView = this._createButton();
|
2025-03-13 18:27:05 +02:00
|
|
|
|
|
|
|
return buttonView;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2025-03-13 19:05:20 +02:00
|
|
|
* Creates a button for admonition command to use either in toolbar or in menu bar.
|
2025-03-13 18:27:05 +02:00
|
|
|
*/
|
2025-03-13 20:07:55 +02:00
|
|
|
private _createButton() {
|
2025-03-13 18:27:05 +02:00
|
|
|
const editor = this.editor;
|
|
|
|
const locale = editor.locale;
|
2025-03-13 19:02:10 +02:00
|
|
|
const command = editor.commands.get( 'admonition' )!;
|
2025-03-13 20:07:55 +02:00
|
|
|
const view = createDropdown(locale);
|
2025-03-13 18:27:05 +02:00
|
|
|
const t = locale.t;
|
|
|
|
|
2025-03-13 20:07:55 +02:00
|
|
|
addListToDropdown(view, this._getDropdownItems())
|
|
|
|
|
|
|
|
view.buttonView.set( {
|
2025-03-13 19:05:20 +02:00
|
|
|
label: t( 'Admonition' ),
|
2025-03-13 19:10:07 +02:00
|
|
|
icon: admonitionIcon,
|
2025-03-13 20:07:55 +02:00
|
|
|
isToggleable: true,
|
|
|
|
tooltip: true
|
2025-03-13 18:27:05 +02:00
|
|
|
} );
|
|
|
|
|
|
|
|
view.bind( 'isEnabled' ).to( command, 'isEnabled' );
|
2025-03-13 20:07:55 +02:00
|
|
|
// view.buttonView.bind( 'isOn' ).to( command, 'value' );
|
2025-03-13 18:27:05 +02:00
|
|
|
|
|
|
|
// Execute the command.
|
|
|
|
this.listenTo( view, 'execute', () => {
|
2025-03-13 19:02:10 +02:00
|
|
|
editor.execute( 'admonition' );
|
2025-03-13 18:27:05 +02:00
|
|
|
editor.editing.view.focus();
|
|
|
|
} );
|
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
2025-03-13 20:07:55 +02:00
|
|
|
|
|
|
|
private _getDropdownItems() {
|
|
|
|
const itemDefinitions = new Collection<ListDropdownButtonDefinition>();
|
|
|
|
|
|
|
|
for (const [ type, admonition ] of Object.entries(ADMONITION_TYPES)) {
|
|
|
|
const definition: ListDropdownButtonDefinition = {
|
|
|
|
type: "button",
|
|
|
|
model: new ViewModel({
|
|
|
|
commandParam: type,
|
|
|
|
label: admonition.title,
|
|
|
|
withText: true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
itemDefinitions.add(definition);
|
|
|
|
}
|
|
|
|
|
|
|
|
return itemDefinitions;
|
|
|
|
}
|
2025-03-13 18:27:05 +02:00
|
|
|
}
|