~canonical-sysadmins/wordpress/4.8.1

« back to all changes in this revision

Viewing changes to wp-includes/js/autosave.js

  • Committer: Barry Price
  • Date: 2016-08-17 04:50:12 UTC
  • mfrom: (1.1.18 upstream)
  • Revision ID: barry.price@canonical.com-20160817045012-qfui81zhqnqv2ba9
Merge WP4.6 from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
( function( $, window ) {
8
8
        function autosave() {
9
9
                var initialCompareString,
10
 
                lastTriggerSave = 0,
11
 
                $document = $(document);
 
10
                        lastTriggerSave = 0,
 
11
                        $document = $(document);
12
12
 
13
13
                /**
14
14
                 * Returns the data saved in both local and remote autosave
19
19
                        var post_name, parent_id, data,
20
20
                                time = ( new Date() ).getTime(),
21
21
                                cats = [],
22
 
                                editor = typeof tinymce !== 'undefined' && tinymce.get('content');
 
22
                                editor = getEditor();
23
23
 
24
24
                        // Don't run editor.save() more often than every 3 sec.
25
25
                        // It is resource intensive and might slow down typing in long posts on slow devices.
26
 
                        if ( editor && ! editor.isHidden() && time - 3000 > lastTriggerSave ) {
 
26
                        if ( editor && editor.isDirty() && ! editor.isHidden() && time - 3000 > lastTriggerSave ) {
27
27
                                editor.save();
28
28
                                lastTriggerSave = time;
29
29
                        }
88
88
                        $document.trigger( 'autosave-enable-buttons' );
89
89
                }
90
90
 
 
91
                function getEditor() {
 
92
                        return typeof tinymce !== 'undefined' && tinymce.get('content');
 
93
                }
 
94
 
91
95
                // Autosave in localStorage
92
96
                function autosaveLocal() {
93
 
                        var restorePostData, undoPostData, blog_id, post_id, hasStorage, intervalTimer,
 
97
                        var blog_id, post_id, hasStorage, intervalTimer,
94
98
                                lastCompareString,
95
99
                                isSuspended = false;
96
100
 
266
270
                                intervalTimer = window.setInterval( save, 15000 );
267
271
 
268
272
                                $( 'form#post' ).on( 'submit.autosave-local', function() {
269
 
                                        var editor = typeof tinymce !== 'undefined' && tinymce.get('content'),
 
273
                                        var editor = getEditor(),
270
274
                                                post_id = $('#post_ID').val() || 0;
271
275
 
272
276
                                        if ( editor && ! editor.isHidden() ) {
310
314
                        function checkPost() {
311
315
                                var content, post_title, excerpt, $notice,
312
316
                                        postData = getSavedPostData(),
313
 
                                        cookie = wpCookies.get( 'wp-saving-post' );
 
317
                                        cookie = wpCookies.get( 'wp-saving-post' ),
 
318
                                        $newerAutosaveNotice = $( '#has-newer-autosave' ).parent( '.notice' );
314
319
 
315
320
                                if ( cookie === post_id + '-saved' ) {
316
321
                                        wpCookies.remove( 'wp-saving-post' );
323
328
                                        return;
324
329
                                }
325
330
 
326
 
                                // There is a newer autosave. Don't show two "restore" notices at the same time.
327
 
                                if ( $( '#has-newer-autosave' ).length ) {
328
 
                                        return;
329
 
                                }
330
 
 
331
331
                                content = $( '#content' ).val() || '';
332
332
                                post_title = $( '#title' ).val() || '';
333
333
                                excerpt = $( '#excerpt' ).val() || '';
338
338
                                        return;
339
339
                                }
340
340
 
341
 
                                restorePostData = postData;
342
 
                                undoPostData = {
343
 
                                        content: content,
344
 
                                        post_title: post_title,
345
 
                                        excerpt: excerpt
346
 
                                };
347
 
 
348
341
                                $notice = $( '#local-storage-notice' )
349
342
                                        .insertAfter( $( '.wrap h1, .wrap h2' ).first() )
350
 
                                        .addClass( 'notice-warning' )
351
 
                                        .show();
352
 
 
353
 
                                $notice.on( 'click.autosave-local', function( event ) {
354
 
                                        var $target = $( event.target );
355
 
 
356
 
                                        if ( $target.hasClass( 'restore-backup' ) ) {
357
 
                                                restorePost( restorePostData );
358
 
                                                $target.parent().hide();
359
 
                                                $(this).find( 'p.undo-restore' ).show();
360
 
                                                $notice.removeClass( 'notice-warning' ).addClass( 'notice-success' );
361
 
                                        } else if ( $target.hasClass( 'undo-restore-backup' ) ) {
362
 
                                                restorePost( undoPostData );
363
 
                                                $target.parent().hide();
364
 
                                                $(this).find( 'p.local-restore' ).show();
365
 
                                                $notice.removeClass( 'notice-success' ).addClass( 'notice-warning' );
366
 
                                        }
367
 
 
368
 
                                        event.preventDefault();
 
343
                                        .addClass( 'notice-warning' );
 
344
 
 
345
                                if ( $newerAutosaveNotice.length ) {
 
346
                                        // If there is a "server" autosave notice, hide it.
 
347
                                        // The data in the session storage is either the same or newer.
 
348
                                        $newerAutosaveNotice.slideUp( 150, function() {
 
349
                                                $notice.slideDown( 150 );
 
350
                                        });
 
351
                                } else {
 
352
                                        $notice.slideDown( 200 );
 
353
                                }
 
354
 
 
355
                                $notice.find( '.restore-backup' ).on( 'click.autosave-local', function() {
 
356
                                        restorePost( postData );
 
357
                                        $notice.fadeTo( 250, 0, function() {
 
358
                                                $notice.slideUp( 150 );
 
359
                                        });
369
360
                                });
370
361
                        }
371
362
 
382
373
                                        }
383
374
 
384
375
                                        $( '#excerpt' ).val( postData.excerpt || '' );
385
 
                                        editor = typeof tinymce !== 'undefined' && tinymce.get('content');
 
376
                                        editor = getEditor();
386
377
 
387
378
                                        if ( editor && ! editor.isHidden() && typeof switchEditors !== 'undefined' ) {
 
379
                                                if ( editor.settings.wpautop && postData.content ) {
 
380
                                                        postData.content = switchEditors.wpautop( postData.content );
 
381
                                                }
 
382
 
388
383
                                                // Make sure there's an undo level in the editor
389
 
                                                editor.undoManager.add();
390
 
                                                editor.setContent( postData.content ? switchEditors.wpautop( postData.content ) : '' );
 
384
                                                editor.undoManager.transact( function() {
 
385
                                                        editor.setContent( postData.content || '' );
 
386
                                                        editor.nodeChanged();
 
387
                                                });
391
388
                                        } else {
392
389
                                                // Make sure the Text editor is selected
393
390
                                                $( '#content-html' ).click();
394
 
                                                $( '#content' ).val( postData.content );
 
391
                                                $( '#content' ).focus();
 
392
                                                // Using document.execCommand() will let the user undo.
 
393
                                                document.execCommand( 'selectAll' );
 
394
                                                document.execCommand( 'insertText', false, postData.content || '' );
395
395
                                        }
396
396
 
397
397
                                        return true;