snippet.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. /* globals window, document, setTimeout */
  6. // Show clipboard input notification when user tries to paste a content from MS Word or Google Docs.
  7. setTimeout( () => {
  8. const editables = document.querySelectorAll( '.ck-content' );
  9. const googleDocsMatch = /id=("|')docs-internal-guid-[-0-9a-f]+("|')/i;
  10. const msWordMatch1 = /<meta\s*name="?generator"?\s*content="?microsoft\s*word\s*\d+"?\/?>/i;
  11. const msWordMatch2 = /xmlns:o="urn:schemas-microsoft-com/i;
  12. editables.forEach( editable => {
  13. const editor = editable.ckeditorInstance;
  14. if ( !editor ) {
  15. return;
  16. }
  17. editor.plugins.get( 'Clipboard' ).listenTo( editor.editing.view.document, 'clipboardInput', ( evt, data ) => {
  18. const htmlString = data.dataTransfer.getData( 'text/html' );
  19. const match = msWordMatch1.test( htmlString ) ||
  20. msWordMatch2.test( htmlString ) ||
  21. googleDocsMatch.test( htmlString );
  22. if ( match ) {
  23. createClipboardInputNotification();
  24. }
  25. } );
  26. } );
  27. }, 3000 );
  28. // The notification should contain the links to the demos where user can test rich formatting from.
  29. function createClipboardInputNotification() {
  30. const title = 'Hello!';
  31. /* eslint-disable max-len */
  32. const message = '<p>We detected that you tried to paste content from <strong>Microsoft Word</strong> or <strong>Google Docs</strong>.</p><p>Please bear in mind that the editor demo to which you try to paste does not have all the features enabled. Due to that, unsupported formatting is being stripped.</p><p>Check out the <a href="/docs/ckeditor5/latest/features/pasting/paste-from-word.html#demo">Paste from Word</a> or <a href="/docs/ckeditor5/latest/pasting/paste-from-google-docs.html#demo">Paste from Google Docs</a> demos for the best experience.</p>';
  33. window.createNotification( title, message );
  34. }
  35. /**
  36. * Creates a notification and appends it to the `.main__content` element.
  37. *
  38. * @param {String} title A title of the notification.
  39. * @param {String} message A message to display in the notification.
  40. *
  41. * @returns {Object} A notification element.
  42. */
  43. window.createNotification = function( title, message ) {
  44. const notificationTemplate = `
  45. <h3 class="main__notification-title">${ title }</h3>
  46. <div class="main__notification-body">
  47. ${ message }
  48. </div>
  49. `;
  50. const notification = document.createElement( 'div' );
  51. const close = document.createElement( 'button' );
  52. close.classList.add( 'main__notification-close' );
  53. close.innerText = '✕';
  54. close.setAttribute( 'aria-label', 'Close the notification' );
  55. notification.classList.add( 'main__notification', 'notice' );
  56. notification.innerHTML = notificationTemplate;
  57. // ATM we support only top-right position.
  58. notification.style.top = window.getViewportTopOffsetConfig() + 10 + 'px';
  59. notification.style.right = '10px';
  60. notification.appendChild( close );
  61. const activeNotifications = document.querySelectorAll( '.main__notification' );
  62. // Translate the position of multiple notifications (just in case).
  63. if ( activeNotifications.length > 0 ) {
  64. const moveOffset = activeNotifications.length * 10;
  65. notification.style.top = parseInt( notification.style.top ) + moveOffset + 'px';
  66. notification.style.right = parseInt( notification.style.right ) + moveOffset + 'px';
  67. }
  68. // Append notification to the `.main__content` element.
  69. const main = document.querySelector( '.main__content' );
  70. main.appendChild( notification );
  71. // A handler for closing a notification.
  72. const onClose = () => {
  73. main.removeChild( notification );
  74. close.removeEventListener( 'click', onClose );
  75. };
  76. close.addEventListener( 'click', onClose );
  77. return notification;
  78. };
  79. /**
  80. * Returns the `config.toolbar.viewportTopOffset` config value for editors using floating toolbars that
  81. * stick to the top of the viewport to remain visible to the user.
  82. *
  83. * The value is determined in styles by the `--ck-snippet-viewport-top-offset` custom property
  84. * and may differ e.g. according to the used media queries.
  85. *
  86. * @returns {Number} The value of the offset.
  87. */
  88. window.getViewportTopOffsetConfig = function() {
  89. const documentElement = document.documentElement;
  90. return parseInt( window.getComputedStyle( documentElement ).getPropertyValue( '--ck-snippet-viewport-top-offset' ) );
  91. };