snippet.js 4.6 KB

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