瀏覽代碼

Prevent from displaying clipboard notification more than once at a time

panr 5 年之前
父節點
當前提交
fbfc96c01a
共有 1 個文件被更改,包括 16 次插入3 次删除
  1. 16 3
      docs/assets/snippet.js

+ 16 - 3
docs/assets/snippet.js

@@ -17,6 +17,10 @@ setTimeout( () => {
 	const msWordMatch1 = /<meta\s*name="?generator"?\s*content="?microsoft\s*word\s*\d+"?\/?>/i;
 	const msWordMatch2 = /xmlns:o="urn:schemas-microsoft-com/i;
 
+	// A global variable indication if clipboard notification is visible.
+	// We use the variable for displaying only one instance of the clipboard notification.
+	window.isClipboardInputNotificationVisible = false;
+
 	editables.forEach( editable => {
 		const editor = editable.ckeditorInstance;
 
@@ -30,7 +34,7 @@ setTimeout( () => {
 				msWordMatch2.test( htmlString ) ||
 				googleDocsMatch.test( htmlString );
 
-			if ( match ) {
+			if ( match && !window.isClipboardInputNotificationVisible ) {
 				createClipboardInputNotification();
 			}
 		} );
@@ -49,7 +53,11 @@ function createClipboardInputNotification() {
 	<a href="/docs/ckeditor5/latest/pasting/paste-from-google-docs.html">Paste from Google Docs</a>
 	demos for the best experience.</p>`;
 
-	window.createNotification( title, message );
+	window.createNotification( title, message, () => {
+		window.isClipboardInputNotificationVisible = false;
+	} );
+
+	window.isClipboardInputNotificationVisible = true;
 }
 
 /**
@@ -57,10 +65,11 @@ function createClipboardInputNotification() {
 *
 * @param {String} title A title of the notification.
 * @param {String} message A message to display in the notification.
+* @param {Function} [onClose] A callback function that executes on closing the notification.
 *
 * @returns {Object} A notification element.
 */
-window.createNotification = function( title, message ) {
+window.createNotification = function( title, message, onClose ) {
 	const notificationTemplate = `
 		<h3 class="main__notification-title">${ title }</h3>
 		<div class="main__notification-body">
@@ -98,6 +107,10 @@ window.createNotification = function( title, message ) {
 
 	close.addEventListener( 'click', () => {
 		main.removeChild( notification );
+
+		if ( onClose && typeof onClose == 'function' ) {
+			onClose();
+		}
 	} );
 
 	return notification;