是否可以在TinyMCE的选项窗口中添加一个中继器字段?

时间:2016-05-16 作者:ZachTRice

我正在尝试使用TinyMCE的编辑器为WordPress创建一个手风琴快捷码生成器。addCommand/windowManager。打开功能。我希望能够在弹出窗口中添加一个可重复的区域,以添加标题(&a);每个手风琴项目的文本区域,以便在提交时,使用这两个参数(标题和文本区域)输出每个手风琴项目的短代码。

i、 e.:
[手风琴][手风琴项目标题=”]内容[[手风琴项目][手风琴项目标题=”]内容[[手风琴项目][手风琴项目标题=”]内容[[手风琴项目][手风琴]

下面是我的mce按钮的一个示例代码,它使用了一个面板快捷代码生成器,类似于我想要构建的手风琴按钮。

(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: \'panel\',
                    onclick: function() {
                        editor.execCommand(\'panel_shortcode_mce_button_popup\',\'\',{
                            header     : \'\',
                            footer     : \'\',
                            type       : \'default\',
                            content    : \'\',
                            panelclass : \'\'
                        });
                    }
                },
            ]
        });

        var panel_tag = \'panel\'; //Add to the panel shortcode naming; in our case panel works fine.

        //add panel popup
        editor.addCommand(\'panel_shortcode_mce_button_popup\', function(ui, v) {
            //setup defaults
            var header = \'\';
            if (v.header)
                header = v.header;
            var footer = \'\';
            if (v.footer)
                footer = v.footer;
            var type = \'default\';
            if (v.type)
                type = v.type;
            var content = \'\';
            if (v.content)
                content = v.content;
            var panelclass = \'\';
            if (v.panelclass)
                panelclass = v.panelclass;
            //open the popup
            editor.windowManager.open( {
                title: \'Panel Shortcode Builder\',
                body: [
                    {//add header input
                        type: \'textbox\',
                        name: \'header\',
                        label: \'Header\',
                        value: header,
                        tooltip: \'Leave blank for none.\'
                    },
                    {//add footer input
                        type: \'textbox\',
                        name: \'footer\',
                        label: \'Footer\',
                        value: footer,
                        tooltip: \'Leave blank for none.\'
                    },
                    {//add type select
                        type: \'listbox\',
                        name: \'type\',
                        label: \'Type\',
                        value: type,
                        \'values\': [
                            {text: \'Default\', value: \'default\'},
                        ],
                        tooltip: \'Select the type of panel you want.\'
                    },
                    {//add content textarea
                        type: \'textbox\',
                        name: \'content\',
                        label: \'Content\',
                        value: content,
                        multiline: true,
                        minWidth: 300,
                        minHeight: 100
                    },
                    {//add class input
                        type: \'textbox\',
                        name: \'panelclass\',
                        label: \'Class\',
                        value: panelclass,
                        tooltip: \'Add custom classes to the panel wrapper.\'
                    },
                ],
                onsubmit: function( e ) { // when the ok button is clicked
                    //start the shortcode tag
                    var panel_str = \'<p>[\' + panel_tag + \' type="\'+e.data.type+\'" class="\'+e.data.panelclass+\'"]</p>\';

                    //check for header
                    if (typeof e.data.header != \'undefined\' && e.data.header.length)
                        panel_str += \'<p>[\' + panel_tag + \'-header class=""][\' + panel_tag + \'-title class=""]\' + e.data.header + \'[/\' + panel_tag + \'-title][/\' + panel_tag + \'-header]</p>\';

                    //add panel content
                    panel_str += \'<p>[\' + panel_tag + \'-content class=""]\' + e.data.content + \'[/\' + panel_tag + \'-content]</p>\';

                    //check for footer
                    if (typeof e.data.footer != \'undefined\' && e.data.footer.length)
                        panel_str += \'<p>[\' + panel_tag + \'-footer class=""]\' + e.data.footer + \'[/\' + panel_tag + \'-footer]</p>\';

                    //start the shortcode tag
                    panel_str += \'<p>[/\' + panel_tag + \']</p>\';

                    //insert shortcode to TinyMCE
                    editor.insertContent( panel_str );
                }
            });
        });
    });
})();

Image of a tinymce panel shortcode builder

对于accordion快捷码生成器,页脚字段将替换为标题,类型下拉列表将消失。标题(&A);内容区是我想重复的部分。

1 个回复
SO网友:ZachTRice

我无法直接添加重复字段(比如+按钮),但我可以创建一个两步过程,最终用户可以使用数字字段在下一个屏幕上设置可重复区域的数量:

Step 1 - Select ShortcodeStep 2 - Shortcode Builder Step 1/2Step 3 - Shortcode Builder Step 2/2Step 4 - Shortcode Output into WYSIWYG Editor

步骤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);
    }
});
所以,这几乎涵盖了一切。如果有任何进一步的更新,我会在那时发布。希望这将至少帮助一些需要重复现场解决方案的人,并可以改进我在这里展示的内容。

相关推荐

Execute shortcodes in PHP

我在帖子编辑器中添加了一个地理位置快捷码,可以根据用户的位置显示不同的信息,将其放在帖子页面的内容中,效果非常好。如何在主题的PHP文件中执行此短代码[地理位置][地理位置]?我正在使用免费修改Wordpress主题,我想添加一个新的<span>[geolocation][/geolocation]Information text content</span> 但在主题的内部。php文件,我如何才能做到这一点?如果我直接加上<span>[geolocation][/ge