8
0

pastefromoffice.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module paste-from-office/pastefromoffice
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import GoogleDocsNormalizer from './normalizers/googledocsnormalizer';
  10. import MSWordNormalizer from './normalizers/mswordnormalizer';
  11. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  12. /**
  13. * The Paste from Office plugin.
  14. *
  15. * This plugin handles content pasted from Office apps and transforms it (if necessary)
  16. * to a valid structure which can then be understood by the editor features.
  17. *
  18. * Transformation is made by a set of predefined {@link module:paste-from-office/normalizer~Normalizer normalizers}.
  19. * This plugin includes following normalizers:
  20. * * {@link module:paste-from-office/normalizers/mswordnormalizer~MSWordNormalizer Microsoft Word normalizer}
  21. * * {@link module:paste-from-office/normalizers/googledocsnormalizer~GoogleDocsNormalizer Google Docs normalizer}
  22. *
  23. * For more information about this feature check the {@glink api/paste-from-office package page}.
  24. *
  25. * @extends module:core/plugin~Plugin
  26. */
  27. export default class PasteFromOffice extends Plugin {
  28. /**
  29. * @inheritDoc
  30. */
  31. static get pluginName() {
  32. return 'PasteFromOffice';
  33. }
  34. /**
  35. * @inheritDoc
  36. */
  37. static get requires() {
  38. return [ Clipboard ];
  39. }
  40. /**
  41. * @inheritDoc
  42. */
  43. init() {
  44. const editor = this.editor;
  45. const viewDocument = editor.editing.view.document;
  46. const normalizers = [];
  47. normalizers.push( new MSWordNormalizer( viewDocument ) );
  48. normalizers.push( new GoogleDocsNormalizer( viewDocument ) );
  49. editor.plugins.get( 'Clipboard' ).on(
  50. 'inputTransformation',
  51. ( evt, data ) => {
  52. if ( data.isTransformedWithPasteFromOffice ) {
  53. return;
  54. }
  55. const htmlString = data.dataTransfer.getData( 'text/html' );
  56. const activeNormalizer = normalizers.find( normalizer => normalizer.isActive( htmlString ) );
  57. if ( activeNormalizer ) {
  58. activeNormalizer.execute( data );
  59. data.isTransformedWithPasteFromOffice = true;
  60. }
  61. },
  62. { priority: 'high' }
  63. );
  64. }
  65. }