Skip to content

Build your personal knowledge base with Trilium Notes

1.1 KB
Raw Blame
import { ButtonView, Command, Plugin } from 'ckeditor5';
import markdownIcon from '../icons/markdown-mark.svg?raw';

export const COMMAND_NAME = 'importMarkdownInline';

export default class MarkdownImportPlugin extends Plugin {
	init() {
		const editor = this.editor;
		const t = editor.t;

        editor.commands.add(COMMAND_NAME, new ImportMarkdownCommand(editor));

		editor.ui.componentFactory.add( 'markdownImport', locale => {
			const view = new ButtonView( locale );

			view.set( {
				label: t('Markdown import from clipboard'),
				icon: markdownIcon,
				tooltip: true
			} );

			// Callback executed once the image is clicked.
            const command = editor.commands.get(COMMAND_NAME)!;
			view.bind('isEnabled').to(command, 'isEnabled');
            view.on('execute', () => editor.execute(COMMAND_NAME));

			return view;
		} );
	}
}

class ImportMarkdownCommand extends Command {

    execute() {
		this.editor.config.get('markdownImport')?.open();
    }

}

declare module 'ckeditor5' {
	interface EditorConfig {
		markdownImport?: {
			/** Opens the dialog that converts Markdown into the note at the selection. */
			open(): void;
		};
	}
}