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. // 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. editables.forEach( editable => {
  17. const editor = editable.ckeditorInstance;
  18. if ( !editor ) {
  19. return;
  20. }
  21. editor.plugins.get( 'Clipboard' ).listenTo( editor.editing.view.document, 'clipboardInput', ( evt, data ) => {
  22. const htmlString = data.dataTransfer.getData( 'text/html' );
  23. const match = msWordMatch1.test( htmlString ) ||
  24. msWordMatch2.test( htmlString ) ||
  25. googleDocsMatch.test( htmlString );
  26. if ( match ) {
  27. createClipboardInputNotification();
  28. }
  29. } );
  30. } );
  31. }, 3000 );
  32. // The notification should contain the links to the demos where user can test rich formatting from.
  33. function createClipboardInputNotification() {
  34. const title = 'Hello!';
  35. /* eslint-disable max-len */
  36. 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>';
  37. window.createNotification( title, message );
  38. }
  39. /**
  40. * Creates a notification and appends it to the `.main__content` element.
  41. *
  42. * @param {String} title A title of the notification.
  43. * @param {String} message A message to display in the notification.
  44. *
  45. * @returns {Object} A notification element.
  46. */
  47. window.createNotification = function( title, message ) {
  48. const notificationTemplate = `
  49. <h3 class="main__notification-title">${ title }</h3>
  50. <div class="main__notification-body">
  51. ${ message }
  52. </div>
  53. `;
  54. const notification = document.createElement( 'div' );
  55. const close = document.createElement( 'button' );
  56. close.classList.add( 'main__notification-close' );
  57. close.innerText = '✕';
  58. close.setAttribute( 'aria-label', 'Close the notification' );
  59. notification.classList.add( 'main__notification', 'notice' );
  60. notification.innerHTML = notificationTemplate;
  61. // ATM we support only top-right position.
  62. notification.style.top = window.getViewportTopOffsetConfig() + 10 + 'px';
  63. notification.style.right = '10px';
  64. notification.appendChild( close );
  65. const activeNotifications = document.querySelectorAll( '.main__notification' );
  66. // Translate the position of multiple notifications (just in case).
  67. if ( activeNotifications.length > 0 ) {
  68. const moveOffset = activeNotifications.length * 10;
  69. notification.style.top = parseInt( notification.style.top ) + moveOffset + 'px';
  70. notification.style.right = parseInt( notification.style.right ) + moveOffset + 'px';
  71. }
  72. // Append notification to the `.main__content` element.
  73. const main = document.querySelector( '.main__content' );
  74. main.appendChild( notification );
  75. close.addEventListener( 'click', () => {
  76. main.removeChild( notification );
  77. } );
  78. return notification;
  79. };
  80. /**
  81. * Returns the `config.toolbar.viewportTopOffset` config value for editors using floating toolbars that
  82. * stick to the top of the viewport to remain visible to the user.
  83. *
  84. * The value is determined in styles by the `--ck-snippet-viewport-top-offset` custom property
  85. * and may differ e.g. according to the used media queries.
  86. *
  87. * @returns {Number} The value of the offset.
  88. */
  89. window.getViewportTopOffsetConfig = function() {
  90. const documentElement = document.documentElement;
  91. return parseInt( window.getComputedStyle( documentElement ).getPropertyValue( '--ck-snippet-viewport-top-offset' ) );
  92. };