2024-12-23 12:34:44 +07:00
|
|
|
import { Plugin, ButtonView } from 'ckeditor5';
|
|
|
|
|
|
|
|
import ckeditor5Icon from '../theme/icons/ckeditor.svg';
|
|
|
|
|
2024-12-23 14:58:02 +07:00
|
|
|
import { asBlob } from 'html-docx-js-typescript'
|
|
|
|
import { saveAs } from 'file-saver';
|
|
|
|
|
2024-12-23 12:34:44 +07:00
|
|
|
export default class Export extends Plugin {
|
|
|
|
public static get pluginName() {
|
|
|
|
return 'Export' as const;
|
|
|
|
}
|
|
|
|
|
|
|
|
public init(): void {
|
|
|
|
const editor = this.editor;
|
|
|
|
const t = editor.t;
|
|
|
|
const model = editor.model;
|
|
|
|
|
|
|
|
// Add the "exportButton" to feature components.
|
|
|
|
editor.ui.componentFactory.add( 'exportButton', locale => {
|
|
|
|
const view = new ButtonView( locale );
|
|
|
|
|
|
|
|
view.set( {
|
|
|
|
label: t( 'Export' ),
|
|
|
|
icon: ckeditor5Icon,
|
|
|
|
tooltip: true
|
|
|
|
} );
|
|
|
|
|
2024-12-23 14:58:02 +07:00
|
|
|
// // Insert a text into the editor after clicking the button.
|
|
|
|
// this.listenTo( view, 'execute', () => {
|
|
|
|
// model.change( writer => {
|
|
|
|
// const textNode = writer.createText( 'Hello CKEditor 5!' );
|
|
|
|
|
|
|
|
// model.insertContent( textNode );
|
|
|
|
// } );
|
2024-12-23 12:34:44 +07:00
|
|
|
|
2024-12-23 14:58:02 +07:00
|
|
|
// editor.editing.view.focus();
|
|
|
|
// } );
|
|
|
|
|
|
|
|
// Insert a text into the editor after clicking the button.
|
|
|
|
this.listenTo( view, 'execute', async () => {
|
|
|
|
asBlob(this.editor.getData()).then((data: any) => {
|
|
|
|
saveAs(data, 'file.docx') // save as docx file
|
|
|
|
}) // asBlob() return Promise<Blob|Buffer>
|
2024-12-23 12:34:44 +07:00
|
|
|
|
|
|
|
editor.editing.view.focus();
|
|
|
|
} );
|
|
|
|
|
|
|
|
return view;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|