ckeditor5-export/src/export.ts

52 lines
1.3 KiB
TypeScript
Raw Normal View History

import { Plugin, ButtonView } from 'ckeditor5';
import ckeditor5Icon from '../theme/icons/ckeditor.svg';
import { asBlob } from 'html-docx-js-typescript'
import { saveAs } from 'file-saver';
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
} );
// // 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 );
// } );
// 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>
editor.editing.view.focus();
} );
return view;
} );
}
}