8
0

snippet.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. const message = `
  36. <p>We detected that you tried to paste content from <strong>Microsoft Word</strong> or <strong>Google Docs</strong>.</p>
  37. <p>Please bear in mind that the editor demo to which you try to paste does not have all the features enabled.
  38. Due to that, unsupported formatting is being stripped.</p>
  39. <p>Check out the <a href="/docs/ckeditor5/latest/features/pasting/paste-from-word.html">Paste from Word</a> or
  40. <a href="/docs/ckeditor5/latest/pasting/paste-from-google-docs.html">Paste from Google Docs</a>
  41. demos for the best experience.</p>`;
  42. window.createNotification( title, message );
  43. }
  44. /**
  45. * Creates a notification and appends it to the `.main__content` element.
  46. *
  47. * @param {String} title A title of the notification.
  48. * @param {String} message A message to display in the notification.
  49. *
  50. * @returns {Object} A notification element.
  51. */
  52. window.createNotification = function( title, message ) {
  53. const notificationTemplate = `
  54. <h3 class="main__notification-title">${ title }</h3>
  55. <div class="main__notification-body">
  56. ${ message }
  57. </div>
  58. `;
  59. const notification = document.createElement( 'div' );
  60. const close = document.createElement( 'button' );
  61. close.classList.add( 'main__notification-close' );
  62. close.innerText = '✕';
  63. close.setAttribute( 'aria-label', 'Close the notification' );
  64. notification.classList.add( 'main__notification', 'notice' );
  65. notification.innerHTML = notificationTemplate;
  66. // ATM we support only top-right position.
  67. notification.style.top = window.getViewportTopOffsetConfig() + 10 + 'px';
  68. notification.style.right = '10px';
  69. notification.appendChild( close );
  70. const activeNotifications = document.querySelectorAll( '.main__notification' );
  71. // Translate the position of multiple notifications (just in case).
  72. if ( activeNotifications.length > 0 ) {
  73. const moveOffset = activeNotifications.length * 10;
  74. notification.style.top = parseInt( notification.style.top ) + moveOffset + 'px';
  75. notification.style.right = parseInt( notification.style.right ) + moveOffset + 'px';
  76. }
  77. // Append notification to the `.main__content` element.
  78. const main = document.querySelector( '.main__content' );
  79. main.appendChild( notification );
  80. close.addEventListener( 'click', () => {
  81. main.removeChild( notification );
  82. } );
  83. return notification;
  84. };
  85. /**
  86. * Returns the `config.toolbar.viewportTopOffset` config value for editors using floating toolbars that
  87. * stick to the top of the viewport to remain visible to the user.
  88. *
  89. * The value is determined in styles by the `--ck-snippet-viewport-top-offset` custom property
  90. * and may differ e.g. according to the used media queries.
  91. *
  92. * @returns {Number} The value of the offset.
  93. */
  94. window.getViewportTopOffsetConfig = function() {
  95. const documentElement = document.documentElement;
  96. return parseInt( window.getComputedStyle( documentElement ).getPropertyValue( '--ck-snippet-viewport-top-offset' ) );
  97. };