8
0

pastefromoffice.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * @license Copyright (c) 2003-2019, 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 normalizers = [];
  46. normalizers.push( new MSWordNormalizer() );
  47. normalizers.push( new GoogleDocsNormalizer() );
  48. editor.plugins.get( 'Clipboard' ).on(
  49. 'inputTransformation',
  50. ( evt, data ) => {
  51. if ( data.isTransformedWithPasteFromOffice ) {
  52. return;
  53. }
  54. const htmlString = data.dataTransfer.getData( 'text/html' );
  55. const activeNormalizer = normalizers.find( normalizer => normalizer.isActive( htmlString ) );
  56. if ( activeNormalizer ) {
  57. activeNormalizer.execute( data );
  58. data.isTransformedWithPasteFromOffice = true;
  59. }
  60. },
  61. { priority: 'high' }
  62. );
  63. }
  64. }