我有一个使用承诺的有效解决方案,但它很难看,我当然会感谢提示。我还没有弄清楚如何实例化promises的子类,因此代码重复且效率低下,并且还实例化了wp。媒体框两次。此外,承诺中没有“拒绝”指令,因此如果承诺不能解决问题,它将永远挂在空白处。
我不会接受这个答案,因为我觉得这是如此糟糕的代码,我相信有人可以想出更好的方法!
/**
* TinyMCE buttons for custom shortcodes
*/
( function() {
var open_media_window = function () {
var first={};
var window = wp.media({
title: \'Insert a media\',
library: {type: \'image\'},
multiple: false,
button: {text: \'Insert\'}
});
window.on(\'select\', function(){
var files = window.state().get(\'selection\').toArray();
first = files[0].toJSON();
console.log ("first is: " + first);
return first.id;
});
window.open();
return first.id;
};
tinymce.create( \'tinymce.plugins.hhImage\', {
init: function( ed, url ) {
ed.addButton( \'hh_image\', {
title: \'Insert Historical Image Overlay\',
image: url + \'/hist-image.png\',
onclick: function() {
//old = prompt( "Enter id of old photo", "" );
var promise = new Promise(function(resolve,reject) {
var window = wp.media({
title: \'Choose HISTORICAL image\',
library: {type: \'image\'},
multiple: false,
button: {text: \'Use as HISTORICAL image\'}
});
window.on(\'select\', function(){
var files = window.state().get(\'selection\').toArray();
first = files[0].toJSON();
console.log ("first is: " + first);
resolve(first.id);
});
window.open();
});
promise.then(function(data) {
var old = data;
var promise2 = new Promise(function (resolve,reject) {
var window = wp.media({
title: \'Choose CONTEMPORARY image\',
library: {type: \'image\'},
multiple: false,
button: {text: \'Use as CONTEMPORARY image\'}
});
window.on(\'select\', function(){
var files = window.state().get(\'selection\').toArray();
first = files[0].toJSON();
console.log ("first is: " + first);
resolve(first.id);
});
window.open();
});
promise2.then(function(data2) {
var newImage = data2;
ed.execCommand( \'mceInsertContent\', false, \'[image-fade old="\' + old + \'" new="\' + newImage + \'"]\' );
});
});
// open_media_window();
// old = promise.done();
//newImage = prompt( "Enter id of new photo", "" );
//newImage = open_media_window();
//ed.execCommand( \'mceInsertContent\', false, \'[image-fade old="\' + old + \'" new="\' + newImage + \'"]\' );
}
});
},
createControl: function( n, cm ) { return null; },
});
tinymce.PluginManager.add( \'hh_image\', tinymce.plugins.hhImage );
})();