Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add send_to_editor filter #681

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions js/build/shortcode-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = Shortcodes;
},{"./../models/shortcode.js":6}],3:[function(require,module,exports){
(function (global){
var Backbone = (typeof window !== "undefined" ? window['Backbone'] : typeof global !== "undefined" ? global['Backbone'] : null),
$ = (typeof window !== "undefined" ? window['jQuery'] : typeof global !== "undefined" ? global['jQuery'] : null),
wp = (typeof window !== "undefined" ? window['wp'] : typeof global !== "undefined" ? global['wp'] : null),
sui = require('./../utils/sui.js'),
Shortcodes = require('./../collections/shortcodes.js');
Expand Down Expand Up @@ -70,12 +71,33 @@ var MediaController = wp.media.controller.State.extend({
},

insert: function() {
var self = this;
var shortcode = this.props.get('currentShortcode');
if ( shortcode ) {
send_to_editor( shortcode.formatShortcode() );
this.reset();
this.frame.close();
}
var okToInsert$ = $.Deferred();

/*
* Filter run before a shortcode is sent to the editor. Can be used for
* client-side validation before closing the media modal.
*
* Called as `shortcode-ui.send_to_editor`.
*
* @param $.Deferred A promise which is expected to either resolve if
* the shortcode can be sent to the editor, or reject
* if not.
* @param Shortcode The current shortcode model.
*/
var sendToEditor$ = wp.shortcake.hooks.applyFilters( 'shortcode-ui.send_to_editor', okToInsert$, shortcode );

// Unless a filter has interfered, resolve the promise, sending the shortcode to the editor.
setTimeout( function() { okToInsert$.resolve(true); } );
Copy link
Member

@davisshaver davisshaver Jun 29, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not that familiar with $.Deferred.() and using it with setTimeout(), so let me try parroting this back first. If there's a hook applied, sendToEditor$ could be a rejected promise. Otherwise it should be the value of okToInsert$, which is an unfulfilled promise.

What does the timeout do? Could we set okToInsert$ as a resolved promise up top and remove the timeout portion? Seems like it might be the same result.


sendToEditor$.then(
function() {
send_to_editor( shortcode.formatShortcode() );
self.reset();
self.frame.close();
}
);
},

reset: function() {
Expand Down
32 changes: 27 additions & 5 deletions js/src/controllers/media-controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var Backbone = require('backbone'),
$ = require('jquery'),
wp = require('wp'),
sui = require('sui-utils/sui'),
Shortcodes = require('sui-collections/shortcodes');
Expand Down Expand Up @@ -33,12 +34,33 @@ var MediaController = wp.media.controller.State.extend({
},

insert: function() {
var self = this;
var shortcode = this.props.get('currentShortcode');
if ( shortcode ) {
send_to_editor( shortcode.formatShortcode() );
this.reset();
this.frame.close();
}
var okToInsert$ = $.Deferred();

/*
* Filter run before a shortcode is sent to the editor. Can be used for
* client-side validation before closing the media modal.
*
* Called as `shortcode-ui.send_to_editor`.
*
* @param $.Deferred A promise which is expected to either resolve if
* the shortcode can be sent to the editor, or reject
* if not.
* @param Shortcode The current shortcode model.
*/
var sendToEditor$ = wp.shortcake.hooks.applyFilters( 'shortcode-ui.send_to_editor', okToInsert$, shortcode );

// Unless a filter has interfered, resolve the promise, sending the shortcode to the editor.
setTimeout( function() { okToInsert$.resolve(true); } );

sendToEditor$.then(
function() {
send_to_editor( shortcode.formatShortcode() );
self.reset();
self.frame.close();
}
);
},

reset: function() {
Expand Down