我无法直接添加重复字段(比如+按钮),但我可以创建一个两步过程,最终用户可以使用数字字段在下一个屏幕上设置可重复区域的数量:
步骤1-选择快捷代码
步骤2-快捷代码生成器步骤1/2
步骤3-快捷代码生成器步骤2/2
步骤4-将快捷代码输出到WYSIWYG编辑器
代码:
(function() {
tinymce.PluginManager.add(\'shortcode_mce_button\', function( editor, url ) {
editor.addButton( \'shortcode_mce_button\', {
tooltip: \'Add custom shortcode\',
image: \'/wp-content/plugins/wdst-shortcodes/public/images/shortcodes.gif\',
type: \'menubutton\',
menu: [
{
text: \'accordion\',
onclick: function() {
editor.execCommand(\'accordion_shortcode_mce_button_popup\',\'\',{
type: \'default\',
accordionclass: \'\',
numberofitems: \'2\',
});
}
},
]
});
var accordion_tag = \'accordion\'; //Add to the accordion shortcode naming; in our case accordion works fine.
//add accordion popup
editor.addCommand(\'accordion_shortcode_mce_button_popup\', function(ui, v) {
//setup defaults
var type = \'default\';
if (v.type)
type = v.type;
var accordionclass = \'\';
if (v.accordionclass)
accordionclass = v.accordionclass;
var numberofitems = \'\';
if (v.numberofitems)
numberofitems = v.numberofitems;
//open the popup
editor.windowManager.open( {
title: \'Accordion Shortcode Builder (Step 1 of 2)\',
classes: \'items-panel\',
body: [
{//add type select
type: \'listbox\',
name: \'type\',
label: \'Type\',
value: type,
minWidth:300,
\'values\': [
{text: \'Default\', value: \'default\'},
],
tooltip: \'Select the type of accordion you want.\'
},
{//add class input
type: \'textbox\',
name: \'accordionclass\',
label: \'Class\',
value: accordionclass,
minWidth:300,
tooltip: \'Add custom classes to the accordion wrapper.\'
},
{//add class input
type: \'textbox\',
name: \'numberofitems\',
label: \'Number of Items\',
value: numberofitems,
minWidth:300,
tooltip: \'Set the number of accordion items needed.\'
},
],
onsubmit: function(e) { // when the ok button is clicked
var finaltype = e.data.type;
var finalclass = e.data.accordionclass;
var totalitems = e.data.numberofitems;
var generalFormItems = [];
for(var i = 1; i <= totalitems; i++)
{
generalFormItems.push(
{
title: \'Accordion-Item \'+i,
name:\'item\'+i,
type: \'form\',
items:[
{
label: \'Accordion-Item \' +i,
name: \'itemhtml\'+i,
type: \'container\',
minWidth:300
},
{
label: \'Title\',
name: String(\'title\'+i),
type: \'textbox\',
minWidth:300,
tooltip: \'Leave blank for none.\'
},
{
label: \'Content\',
name: \'content\'+i,
multiline: true,
type: \'textbox\',
minWidth:300
}]
})
}
win = editor.windowManager.open({
autoScroll: true,
minWidth: 600,
resizable: true,
classes: \'largemce-panel\',
title: \'Insert Accordion-Items (Step 2 of 2)\',
body: generalFormItems,
onsubmit: function( e ) { // when the ok button is clicked
//start the shortcode tag
var accordion_str = \'<p>[\' + accordion_tag + \' type="\'+finaltype+\'" class="\'+finalclass+\'"]</p>\';
for(var i = 1; i <= totalitems; i++) {
var gettitle = String(\'e.data.title\' + i);
var title = eval(gettitle);
var getcontent = String(\'e.data.content\' + i);
var content = eval(getcontent);
accordion_str += \'<p>[\' + accordion_tag + \'-header]\';
accordion_str += \'[\' + accordion_tag + \'-title]\' + title + \'[/\' + accordion_tag + \'-title]\';
accordion_str += \'[/\' + accordion_tag + \'-header]</p>\';
accordion_str += \'<p>[\' + accordion_tag + \'-content]\' + content + \'[/\' + accordion_tag + \'-content]</p>\';
}
//start the shortcode tag
accordion_str += \'<p>[/\' + accordion_tag + \']</p>\';
//insert shortcode to TinyMCE
editor.insertContent( accordion_str );
}
});
}
});
});
});
})();
其他注意事项:
在第二个选项屏幕上,我使用autoScroll参数创建了一个滚动条,但在WordPress中,这与他们的TinyMCE样式冲突,因此我必须添加“classes”参数来在这个窗口上设置一个类,然后使用css来解决显示问题。
/*Fix Accordion Window scroll effect*/
.mce-largemce-panel {
top: 22% !important;
max-height: 500px !important;
}
.mce-largemce-panel .mce-reset {
height: 500px !important;;
overflow: auto !important;
margin-left: -16px !important;
margin-right: -16px !important;
}
.mce-largemce-panel .mce-window-head {
margin-left: 16px !important;
margin-right: 16px !important;
}
.mce-largemce-panel .mce-foot {
margin-left: 15px !important;
}
我还在第一个弹出窗口的数字字段上使用了一些jquery验证。这不是最漂亮的代码,我相信其他人也可以找到更好的方法来定位此窗口,但这就是我目前用来验证是否输入了数字的方法。
$(document).on(\'keyup\', \'.mce-items-panel .mce-reset .mce-container-body .mce-last .mce-container-body .mce-last .mce-container-body input.mce-last\', function(event) {
var v = this.value;
if($.isNumeric(v) === false) {
//chop off the last char entered
this.value = this.value.slice(0,-1);
}
});
所以,这几乎涵盖了一切。如果有任何进一步的更新,我会在那时发布。希望这将至少帮助一些需要重复现场解决方案的人,并可以改进我在这里展示的内容。