From d6ff255b86ae6fb8a00817f623f3d497f5a801ac Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Fri, 28 Apr 2017 09:53:14 +0100 Subject: [PATCH 1/6] Add new action after set action update --- js/build/shortcode-ui.js | 3 +++ js/src/controllers/media-controller.js | 3 +++ 2 files changed, 6 insertions(+) diff --git a/js/build/shortcode-ui.js b/js/build/shortcode-ui.js index cdb4f689..00e23470 100644 --- a/js/build/shortcode-ui.js +++ b/js/build/shortcode-ui.js @@ -136,6 +136,9 @@ var MediaController = wp.media.controller.State.extend({ this.frame.mediaController.toggleSidebar( false ); }.bind( this ) ); + var hookName = 'shortcode-ui.after_set_action_update'; + wp.shortcake.hooks.doAction( hookName, currentShortcode ); + }.bind( this ) ); }, diff --git a/js/src/controllers/media-controller.js b/js/src/controllers/media-controller.js index 2dca659e..3e6f0184 100644 --- a/js/src/controllers/media-controller.js +++ b/js/src/controllers/media-controller.js @@ -99,6 +99,9 @@ var MediaController = wp.media.controller.State.extend({ this.frame.mediaController.toggleSidebar( false ); }.bind( this ) ); + var hookName = 'shortcode-ui.after_set_action_update'; + wp.shortcake.hooks.doAction( hookName, currentShortcode ); + }.bind( this ) ); }, From dcb0c68a178198951dec069b0de9b1d906574ed4 Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Fri, 28 Apr 2017 09:54:23 +0100 Subject: [PATCH 2/6] Merge fix state bugs --- js/build/shortcode-ui.js | 19 ++++++++++++++----- js/src/controllers/media-controller.js | 6 ++++++ js/src/shortcode-ui.js | 7 ++----- js/src/utils/shortcode-view-constructor.js | 6 ++++++ 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/js/build/shortcode-ui.js b/js/build/shortcode-ui.js index 00e23470..bdd3682f 100644 --- a/js/build/shortcode-ui.js +++ b/js/build/shortcode-ui.js @@ -52,9 +52,15 @@ var MediaController = wp.media.controller.State.extend({ }); this.props.on( 'change:action', this.refresh, this ); + this.on( 'activate', this.activate, this ); }, + activate: function() { + var $el = this.frame.$el; + _.defer( function() { $el.addClass( 'hide-router' ); } ); + }, + refresh: function() { if ( this.frame && this.frame.toolbar ) { this.frame.toolbar.get().refresh(); @@ -369,11 +375,8 @@ $(document).ready(function(){ // Make sure to reset state when closed. frame.once( 'close submit', function() { - frame.state().props.set('currentShortcode', false); - var menuItem = frame.menu.get().get('shortcode-ui'); - menuItem.options.text = shortcodeUIData.strings.media_frame_title; - menuItem.render(); - frame.setState( 'insert' ); + frame.mediaController.reset(); + frame.mediaController.resetState(); } ); } ); @@ -682,6 +685,12 @@ var shortcodeViewConstructor = { update( shortcode.formatShortcode() ); } ); + // Make sure to reset state when closed. + frame.once( 'close submit', function() { + frame.mediaController.reset(); + frame.mediaController.resetState(); + } ); + /* Trigger render_edit */ /* * Action run after an edit shortcode overlay is rendered. diff --git a/js/src/controllers/media-controller.js b/js/src/controllers/media-controller.js index 3e6f0184..fbd7ce4d 100644 --- a/js/src/controllers/media-controller.js +++ b/js/src/controllers/media-controller.js @@ -15,9 +15,15 @@ var MediaController = wp.media.controller.State.extend({ }); this.props.on( 'change:action', this.refresh, this ); + this.on( 'activate', this.activate, this ); }, + activate: function() { + var $el = this.frame.$el; + _.defer( function() { $el.addClass( 'hide-router' ); } ); + }, + refresh: function() { if ( this.frame && this.frame.toolbar ) { this.frame.toolbar.get().refresh(); diff --git a/js/src/shortcode-ui.js b/js/src/shortcode-ui.js index 8bfcec02..fb1e9fe6 100644 --- a/js/src/shortcode-ui.js +++ b/js/src/shortcode-ui.js @@ -49,11 +49,8 @@ $(document).ready(function(){ // Make sure to reset state when closed. frame.once( 'close submit', function() { - frame.state().props.set('currentShortcode', false); - var menuItem = frame.menu.get().get('shortcode-ui'); - menuItem.options.text = shortcodeUIData.strings.media_frame_title; - menuItem.render(); - frame.setState( 'insert' ); + frame.mediaController.reset(); + frame.mediaController.resetState(); } ); } ); diff --git a/js/src/utils/shortcode-view-constructor.js b/js/src/utils/shortcode-view-constructor.js index ac598c3b..19bc5cdd 100644 --- a/js/src/utils/shortcode-view-constructor.js +++ b/js/src/utils/shortcode-view-constructor.js @@ -183,6 +183,12 @@ var shortcodeViewConstructor = { update( shortcode.formatShortcode() ); } ); + // Make sure to reset state when closed. + frame.once( 'close submit', function() { + frame.mediaController.reset(); + frame.mediaController.resetState(); + } ); + /* Trigger render_edit */ /* * Action run after an edit shortcode overlay is rendered. From 3f114538dbddd630972d43b3861c3ab96cca29ae Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Tue, 2 May 2017 09:55:57 +0100 Subject: [PATCH 3/6] Force update on change --- js/build/shortcode-ui.js | 2 +- js/src/utils/shortcode-view-constructor.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/js/build/shortcode-ui.js b/js/build/shortcode-ui.js index bdd3682f..01a27eee 100644 --- a/js/build/shortcode-ui.js +++ b/js/build/shortcode-ui.js @@ -682,7 +682,7 @@ var shortcodeViewConstructor = { } frame.mediaController.props.set( 'insertCallback', function( shortcode ) { - update( shortcode.formatShortcode() ); + update( shortcode.formatShortcode(), true ); } ); // Make sure to reset state when closed. diff --git a/js/src/utils/shortcode-view-constructor.js b/js/src/utils/shortcode-view-constructor.js index 19bc5cdd..675f59e7 100644 --- a/js/src/utils/shortcode-view-constructor.js +++ b/js/src/utils/shortcode-view-constructor.js @@ -180,7 +180,7 @@ var shortcodeViewConstructor = { } frame.mediaController.props.set( 'insertCallback', function( shortcode ) { - update( shortcode.formatShortcode() ); + update( shortcode.formatShortcode(), true ); } ); // Make sure to reset state when closed. From 4cd3cad41492a9f36395ea1ffb425cca562d730a Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Tue, 2 May 2017 12:49:44 +0100 Subject: [PATCH 4/6] Set active editor --- js/build/shortcode-ui.js | 9 ++++++++- js/src/controllers/media-controller.js | 3 +++ js/src/shortcode-ui.js | 4 +++- js/src/utils/shortcode-view-constructor.js | 2 ++ 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/js/build/shortcode-ui.js b/js/build/shortcode-ui.js index 01a27eee..f1525e92 100644 --- a/js/build/shortcode-ui.js +++ b/js/build/shortcode-ui.js @@ -49,6 +49,7 @@ var MediaController = wp.media.controller.State.extend({ action: 'select', search: null, insertCallback: this.insertCallback, + editor: wpActiveEditor, }); this.props.on( 'change:action', this.refresh, this ); @@ -80,6 +81,8 @@ var MediaController = wp.media.controller.State.extend({ var shortcode = this.props.get( 'currentShortcode' ); var insertCallback = this.props.get( 'insertCallback' ); + window.wpActiveEditor = this.props.get( 'editor' ); + if ( shortcode && insertCallback ) { insertCallback( shortcode ); } @@ -356,7 +359,8 @@ $(document).ready(function(){ options = { frame: 'post', state: 'shortcode-ui', - title: shortcodeUIData.strings.media_frame_title + title: shortcodeUIData.strings.media_frame_title, + editor: this.dataset.editor }; event.preventDefault(); @@ -368,6 +372,7 @@ $(document).ready(function(){ if ( frame ) { frame.mediaController.setActionSelect(); + frame.mediaController.props.set( 'editor', this.dataset.editor ); frame.open(); } else { frame = wp.media.editor.open( editor, options ); @@ -672,12 +677,14 @@ var shortcodeViewConstructor = { if ( frame ) { frame.mediaController.setActionUpdate( currentShortcode ); + frame.mediaController.props.set( 'editor', wpActiveEditor ); frame.open(); } else { frame = wp.media.editor.open( window.wpActiveEditor, { frame : "post", state : 'shortcode-ui', currentShortcode : currentShortcode, + editor : wpActiveEditor, }); } diff --git a/js/src/controllers/media-controller.js b/js/src/controllers/media-controller.js index fbd7ce4d..3a54f540 100644 --- a/js/src/controllers/media-controller.js +++ b/js/src/controllers/media-controller.js @@ -12,6 +12,7 @@ var MediaController = wp.media.controller.State.extend({ action: 'select', search: null, insertCallback: this.insertCallback, + editor: wpActiveEditor, }); this.props.on( 'change:action', this.refresh, this ); @@ -43,6 +44,8 @@ var MediaController = wp.media.controller.State.extend({ var shortcode = this.props.get( 'currentShortcode' ); var insertCallback = this.props.get( 'insertCallback' ); + window.wpActiveEditor = this.props.get( 'editor' ); + if ( shortcode && insertCallback ) { insertCallback( shortcode ); } diff --git a/js/src/shortcode-ui.js b/js/src/shortcode-ui.js index fb1e9fe6..7cac944b 100644 --- a/js/src/shortcode-ui.js +++ b/js/src/shortcode-ui.js @@ -30,7 +30,8 @@ $(document).ready(function(){ options = { frame: 'post', state: 'shortcode-ui', - title: shortcodeUIData.strings.media_frame_title + title: shortcodeUIData.strings.media_frame_title, + editor: this.dataset.editor }; event.preventDefault(); @@ -42,6 +43,7 @@ $(document).ready(function(){ if ( frame ) { frame.mediaController.setActionSelect(); + frame.mediaController.props.set( 'editor', this.dataset.editor ); frame.open(); } else { frame = wp.media.editor.open( editor, options ); diff --git a/js/src/utils/shortcode-view-constructor.js b/js/src/utils/shortcode-view-constructor.js index 675f59e7..36a1aad3 100644 --- a/js/src/utils/shortcode-view-constructor.js +++ b/js/src/utils/shortcode-view-constructor.js @@ -170,12 +170,14 @@ var shortcodeViewConstructor = { if ( frame ) { frame.mediaController.setActionUpdate( currentShortcode ); + frame.mediaController.props.set( 'editor', wpActiveEditor ); frame.open(); } else { frame = wp.media.editor.open( window.wpActiveEditor, { frame : "post", state : 'shortcode-ui', currentShortcode : currentShortcode, + editor : wpActiveEditor, }); } From 435abacf07aca816a6a97e3e69b0119e2f83dbf4 Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Tue, 2 May 2017 18:46:36 +0100 Subject: [PATCH 5/6] Set active editor --- js/build/shortcode-ui.js | 11 ++++++++++- js/src/controllers/media-controller.js | 11 ++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/js/build/shortcode-ui.js b/js/build/shortcode-ui.js index f1525e92..91dd10a2 100644 --- a/js/build/shortcode-ui.js +++ b/js/build/shortcode-ui.js @@ -81,7 +81,7 @@ var MediaController = wp.media.controller.State.extend({ var shortcode = this.props.get( 'currentShortcode' ); var insertCallback = this.props.get( 'insertCallback' ); - window.wpActiveEditor = this.props.get( 'editor' ); + this.setActiveEditor( this.props.get( 'editor' ) ); if ( shortcode && insertCallback ) { insertCallback( shortcode ); @@ -156,6 +156,15 @@ var MediaController = wp.media.controller.State.extend({ this.frame.$el.toggleClass( 'hide-menu', show ); }, + setActiveEditor: function( editorId ) { + var editor = tinymce.get( editorId ); + + if ( editor ) { + tinymce.setActive( editor ); + } + + window.wpActiveEditor = editorId; + }, }); sui.controllers.MediaController = MediaController; diff --git a/js/src/controllers/media-controller.js b/js/src/controllers/media-controller.js index 3a54f540..68f387ad 100644 --- a/js/src/controllers/media-controller.js +++ b/js/src/controllers/media-controller.js @@ -44,7 +44,7 @@ var MediaController = wp.media.controller.State.extend({ var shortcode = this.props.get( 'currentShortcode' ); var insertCallback = this.props.get( 'insertCallback' ); - window.wpActiveEditor = this.props.get( 'editor' ); + this.setActiveEditor( this.props.get( 'editor' ) ); if ( shortcode && insertCallback ) { insertCallback( shortcode ); @@ -119,6 +119,15 @@ var MediaController = wp.media.controller.State.extend({ this.frame.$el.toggleClass( 'hide-menu', show ); }, + setActiveEditor: function( editorId ) { + var editor = tinymce.get( editorId ); + + if ( editor ) { + tinymce.setActive( editor ); + } + + window.wpActiveEditor = editorId; + }, }); sui.controllers.MediaController = MediaController; From 2efa489122eb4c08b7f66f7cfbb51a758cda2567 Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Wed, 3 May 2017 19:16:46 +0100 Subject: [PATCH 6/6] Revert force update --- js/build/shortcode-ui.js | 2 +- js/src/utils/shortcode-view-constructor.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/js/build/shortcode-ui.js b/js/build/shortcode-ui.js index 91dd10a2..a87de166 100644 --- a/js/build/shortcode-ui.js +++ b/js/build/shortcode-ui.js @@ -698,7 +698,7 @@ var shortcodeViewConstructor = { } frame.mediaController.props.set( 'insertCallback', function( shortcode ) { - update( shortcode.formatShortcode(), true ); + update( shortcode.formatShortcode() ); } ); // Make sure to reset state when closed. diff --git a/js/src/utils/shortcode-view-constructor.js b/js/src/utils/shortcode-view-constructor.js index 36a1aad3..b2bfa38a 100644 --- a/js/src/utils/shortcode-view-constructor.js +++ b/js/src/utils/shortcode-view-constructor.js @@ -182,7 +182,7 @@ var shortcodeViewConstructor = { } frame.mediaController.props.set( 'insertCallback', function( shortcode ) { - update( shortcode.formatShortcode(), true ); + update( shortcode.formatShortcode() ); } ); // Make sure to reset state when closed.