多个 TinyMCE 编辑器,但只有一个工具栏?

发布于 2024-09-04 10:55:38 字数 328 浏览 11 评论 0原文

我浏览了论坛,但似乎找不到这个问题的明确答案...

我在我们的网站上使用 jQuery 和 TinyMCE。我已经阅读了 TinyMCE 的文档,但恐怕我仍然迷失方向。我们正在制作一个需要在页面中多个位置进行就地编辑的界面。唯一的事情是,每一个都将在顶部的一个工具栏中包含 TinyMCE 的所有编辑选项。因此,回顾一下,它是多个编辑器(每个编辑器没有自己的工具栏,只是一个编辑或选择文本的地方),并且页面顶部只有一个工具栏来控制当时处于活动状态的文本框。

如何才能实现这一目标?有可能吗?任何帮助,任何朝着正确方向的推动,任何关于这个问题的提示/技巧/知识都将是一个巨大的帮助。

谢谢,詹姆斯

I've looked around the forum, but cannot seem to find a definite answer to this problem...

I'm using jQuery and TinyMCE on our website. I've gone through the docs of TinyMCE, but am still getting lost I'm afraid. We're doing an interface that requires edit-in-place in multiple places in the page. The only thing is, each of these will have all the editing choices from TinyMCE in one toolbar at the top. So, to recap it, it's multiple editors (that each have no toolbars of their own, just a place to edit or select the text) and only one toolbar at the top of the page to control whichever textbox is active at the time.

How could this be achieved? Is it even possible? Any help, any push in the right direction, any hints/tips/knowledge at all on this problem would be a great, great help.

Thanks, James

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风苍溪 2024-09-11 10:55:38

我对 3.x 版本做了这样的操作(它是原型语法):

首先,我创建了一个工具栏包装器(在我的例子中,我将其附加到文档顶部的位置:固定):

<div id="externalToolbarWrapper"></div>

然后我在tinyMCE-中设置了设置函数设置(对于每个编辑器)如下:

[...]
theme_advanced_toolbar_location : "external",
setup : function(ed) {
    ed.onInit.add(function(ed, e) {
        ed.onPostRender.add(function(ed, cm) {
            var toolbar = $(ed.id + '_external');
            // inserts the external toolbar in the external wrapper
            $('externalToolbarWrapper').insert(toolbar.hide());
        });
        ed.onRemove.add(function(ed) {
            if($(ed.id + '_external')) {
                // removes external toolbar
                $(ed.id + '_external').remove();
            }
        });
    });
}

这在我的情况下有效 - 编辑器在激活/停用时显示/隐藏工具栏。

I did with the 3.x version like this (it's Prototype syntax):

First, I created a toolbar wrapper (in my case I attached it with position:fixed at top of the document:

<div id="externalToolbarWrapper"></div>

Then I set the setup function in the tinyMCE-settings (for each editor) like this:

[...]
theme_advanced_toolbar_location : "external",
setup : function(ed) {
    ed.onInit.add(function(ed, e) {
        ed.onPostRender.add(function(ed, cm) {
            var toolbar = $(ed.id + '_external');
            // inserts the external toolbar in the external wrapper
            $('externalToolbarWrapper').insert(toolbar.hide());
        });
        ed.onRemove.add(function(ed) {
            if($(ed.id + '_external')) {
                // removes external toolbar
                $(ed.id + '_external').remove();
            }
        });
    });
}

This worked in my case - the editors show/hide the toolbars on activation/deactivation.

深府石板幽径 2024-09-11 10:55:38

我知道有一种方法可以在文本区域聚焦时显示工具栏,然后在文本区域模糊事件上隐藏 - 所以这可能是一条路线。

我做了类似的事情(使用多个文本区域),其中我按需加载tinyMCE,所以像按需加载然后在完成(模糊事件)时销毁之类的东西可能就是您所追求的。

我无法向您提供我的所有代码,因为它实际上是我雇主 IP 的一部分,但这里有一个粗略的概述,希望对您有所帮助。 tinyMCE_GZ 是 gzip 的一部分,位于tinyMCE 站点之外。

isTinyMCE_Loaded = false;

jQuery('.my-textarea').one("click", function(){
    BuildWYSIWYG.Full(jQuery(this));
})

BuildWYSIWYG.OnDemand: function(callback){
    tinyMCE_GZ.init({
        plugins : 'style,table,advhr,advimage,advlink,insertdatetime,preview,searchreplace,contextmenu,paste,fullscreen,visualchars,nonbreaking,xhtmlxtras,safari,tinybrowser',
        themes : 'simple,advanced',
        languages : 'en',
        disk_cache : true,
        debug : false
    }, function(){ 
        isTinyMCE_Loaded = true; 
        callback();
    });
};
BuildWYSIWYG.Full: function(el){   
    settings.elements = jQuery(el).attr("id");

    // Build advanced wysiwyg
    if (isTinyMCE_Loaded){
        tinyMCE.init(settings);
    } else {
        BuildWYSIWYG.OnDemand(function(){
            tinyMCE.init(settings);
        });
    }
}

I know there is a way to show the toolbar when a textarea is focused into, and then hide on textarea blur event - so that could be one route.

I do a similar sort of thing (with multiple textareas) where i load in demand the tinyMCE, so something like loading on demand and then destroy when finished with (blur event) might be what you're after.

I can't give you all of my code as it's actually part of my employer's I.P, but here is a rough outline to it, hopefully should help. The tinyMCE_GZ is part of the gzip which is off the tinyMCE site.

isTinyMCE_Loaded = false;

jQuery('.my-textarea').one("click", function(){
    BuildWYSIWYG.Full(jQuery(this));
})

BuildWYSIWYG.OnDemand: function(callback){
    tinyMCE_GZ.init({
        plugins : 'style,table,advhr,advimage,advlink,insertdatetime,preview,searchreplace,contextmenu,paste,fullscreen,visualchars,nonbreaking,xhtmlxtras,safari,tinybrowser',
        themes : 'simple,advanced',
        languages : 'en',
        disk_cache : true,
        debug : false
    }, function(){ 
        isTinyMCE_Loaded = true; 
        callback();
    });
};
BuildWYSIWYG.Full: function(el){   
    settings.elements = jQuery(el).attr("id");

    // Build advanced wysiwyg
    if (isTinyMCE_Loaded){
        tinyMCE.init(settings);
    } else {
        BuildWYSIWYG.OnDemand(function(){
            tinyMCE.init(settings);
        });
    }
}
油焖大侠 2024-09-11 10:55:38

可能还有另一种方法。看一下这个例子。 http://tinymce.moxiecode.com/examples/example_23.php

您可以使用底部的链接(显示、隐藏、粗体、获取内容等)作为菜单(可能需要一些样式)。然后,获取当前焦点文本区域的 id 并将其传递给菜单 (#current) 并使用它来更改该文本区域。

要实现您所描述的目的:

  1. 首先禁用所有单独的 TinyMCE 菜单项。
  2. 禁用它们后,用 HTML 创建您自己的 TinyMCE 菜单并相应地设置其样式。
  3. 确定聚焦的 TinyMCE 文本区域
  4. 将新菜单中的操作应用到聚焦的文本区域

现在编写一些代码(可能需要一些调试...)

首先,初始化 TinyMCE 并禁用菜单。

tinyMCE configs 
({
    mode : "textareas",
    theme : "advanced",
    editor_selector : "editable"
    theme_advanced_buttons1 : "",
    theme_advanced_buttons2 : "",
    theme_advanced_buttons3 : "",
    theme_advanced_toolbar_location : "botton",
    theme_advanced_statusbar_location : "bottom" });

我想你也可以编辑tiny_mce/themes/advanced/editor_template_src.js中的_addToolbars函数,然后打包。

然后使用 jQuery 绑定确定当前处于焦点的文本区域:

$().ready(function() {
        var current;
        $('.editable').focus(
            current = this.id;
        );
        $('.editable').blur(
            //maybe hide the menu (?)
        );

}

然后使用我们的文本区域和菜单创建 HTML

<form method="post" action="somepage">  
<div id="independent_menu">
    <!-- The Menu, Please Style Accordingly -->
    <a href="javascript:;" onmousedown="$('#current').tinymce().show();">[Show]</a>
    <a href="javascript:;" onmousedown="$('#current').tinymce().hide();">[Hide]</a>
    <a href="javascript:;" onmousedown="$('#current').tinymce().execCommand('Bold');">[Bold]</a>
    <a href="javascript:;" onmousedown="alert($('#current').html());">[Get contents]</a>
    <a href="javascript:;" onmousedown="alert($('#current').tinymce().selection.getContent());">[Get selected HTML]</a>
    <a href="javascript:;" onmousedown="alert($('#current').tinymce().selection.getContent({format : 'text'}));">[Get selected text]</a>
    <a href="javascript:;" onmousedown="alert($('#current').tinymce().selection.getNode().nodeName);">[Get selected element]</a>
    <a href="javascript:;" onmousedown="$('#current').tinymce().execCommand('mceInsertContent',false,'<b>Hello world!!</b>');">[Insert HTML]</a>
    <a href="javascript:;" onmousedown="$('#current').tinymce().execCommand('mceReplaceContent',false,'<b>{$selection}</b>');">[Replace selection]</a>
</div>

<!-- The Text Areas  -->

<textarea class="editable" id="one">Some Text Here</textarea>

<textarea class="editable" id="two">Yet another text area</textarea>

<textarea class="editable" id="three">Final Text Area</textarea>

There might be another way. Take a look at this example. http://tinymce.moxiecode.com/examples/example_23.php

You can use the links at the bottom (Show,Hide,Bold,Get Contents etc) as a menu (may require some styling). Then, get the id of the textarea currently in focus and pass it to the menu (#current) and use it to change that textarea.

To achieve what you are describing:

  1. First disable all the indivudual TinyMCE menu items.
  2. Once they are disabled, create your own TinyMCE menu in HTML and style it accordingly.
  3. Determine which TinyMCE textarea in focus
  4. Apply the actions from your new menu to the Textarea that is focused

Now for some code (may require some debugging...)

First, Initialize TinyMCE and disable menus.

tinyMCE configs 
({
    mode : "textareas",
    theme : "advanced",
    editor_selector : "editable"
    theme_advanced_buttons1 : "",
    theme_advanced_buttons2 : "",
    theme_advanced_buttons3 : "",
    theme_advanced_toolbar_location : "botton",
    theme_advanced_statusbar_location : "bottom" });

I think you can also edit the _addToolbars function in tiny_mce/themes/advanced/editor_template_src.js and then pack it.

Then determine the text area that is currently in focus using jQuery bind:

$().ready(function() {
        var current;
        $('.editable').focus(
            current = this.id;
        );
        $('.editable').blur(
            //maybe hide the menu (?)
        );

}

Then create the HTML with our textareas and the menu

<form method="post" action="somepage">  
<div id="independent_menu">
    <!-- The Menu, Please Style Accordingly -->
    <a href="javascript:;" onmousedown="$('#current').tinymce().show();">[Show]</a>
    <a href="javascript:;" onmousedown="$('#current').tinymce().hide();">[Hide]</a>
    <a href="javascript:;" onmousedown="$('#current').tinymce().execCommand('Bold');">[Bold]</a>
    <a href="javascript:;" onmousedown="alert($('#current').html());">[Get contents]</a>
    <a href="javascript:;" onmousedown="alert($('#current').tinymce().selection.getContent());">[Get selected HTML]</a>
    <a href="javascript:;" onmousedown="alert($('#current').tinymce().selection.getContent({format : 'text'}));">[Get selected text]</a>
    <a href="javascript:;" onmousedown="alert($('#current').tinymce().selection.getNode().nodeName);">[Get selected element]</a>
    <a href="javascript:;" onmousedown="$('#current').tinymce().execCommand('mceInsertContent',false,'<b>Hello world!!</b>');">[Insert HTML]</a>
    <a href="javascript:;" onmousedown="$('#current').tinymce().execCommand('mceReplaceContent',false,'<b>{$selection}</b>');">[Replace selection]</a>
</div>

<!-- The Text Areas  -->

<textarea class="editable" id="one">Some Text Here</textarea>

<textarea class="editable" id="two">Yet another text area</textarea>

<textarea class="editable" id="three">Final Text Area</textarea>

七色彩虹 2024-09-11 10:55:38

我用旧版本的tinymce做到了这一点:

http://tinymce.moxiecode.com /punbb/viewtopic.php?id=10197
http://examples.dtbaker.com.au/code/tinymce/

虽然还没有用最新版本重新生成这个:(

但基本上有两种方法可以做到这一点:

1)创建自己的菜单来调用tinymce活动的tinymce编辑器的参数。

2)将tinymce工具栏/菜单设置为外部,以便当您聚焦编辑器时它会出现。然后使用 CSS 将每个工具栏放置在同一位置。所以看起来只有一个工具栏,但实际上是多个工具栏出现在同一个地方。

希望有帮助。

I did this with an older version of tinymce:

http://tinymce.moxiecode.com/punbb/viewtopic.php?id=10197
http://examples.dtbaker.com.au/code/tinymce/

Haven't re-produced this with the latest version though :(

But basically there's two ways to do it, either:

1) Create your own menu that calls tinymce arguments on the active tinymce editor.

2) Set the tinymce toolbar/menu to be external, so that it appears when you focus an editor. Then using CSS you position each toolbar to appear in the same place. So it looks like there is a single toolbar but in fact it is multiple toolbars appearing in the same place.

Hope that helps.

哑剧 2024-09-11 10:55:38

我正在为这个问题提供解决方案,以防有人仍然来这里寻求解决方案。您可以使用 execCommand 在单击自定义按钮时执行您喜欢的各种样式的文本。例如:

// bold, italic and underline
$('#bold').click(function(){
    tinymce.execCommand('Bold');
});
$('#italic').click(function(){
    tinyMCE.execCommand('Italic');
});
$('#underline').click(function(){
    tinyMCE.execCommand('Underline');
});

//commands for left align, right align and center align
$('#l-a').click(function(){
    tinyMCE.execCommand('JustifyLeft');
});
$('#c-a').click(function(){
    tinyMCE.execCommand('JustifyCenter');
});
$('#r-a').click(function(){
    tinyMCE.execCommand('JustifyRight');
});

//commands for inserting ul or ol
$('#ul').click(function(){
    tinyMCE.execCommand('InsertUnorderedList');
});
$('#ol').click(function(){
    tinyMCE.execCommand('InsertOrderedList');
});

其中 #bold、#italic、#ul 等是您用来实现样式的 html 元素的 id。例如:

<button id="bold">B</button>

此按钮会将文本加粗,无论文本位于tinymce 的哪个实例中。
尽管此功能的唯一缺点是您必须做大量工作才能显示特定按钮打开或关闭时的效果。如果您不关心这一点,那么这就会成功。

我希望它能有所帮助......

I am providing solution for this question in case anyone still comes here for one. You can use execCommand to execute various styles of text you like while clicking on your custom buttons. For example:

// bold, italic and underline
$('#bold').click(function(){
    tinymce.execCommand('Bold');
});
$('#italic').click(function(){
    tinyMCE.execCommand('Italic');
});
$('#underline').click(function(){
    tinyMCE.execCommand('Underline');
});

//commands for left align, right align and center align
$('#l-a').click(function(){
    tinyMCE.execCommand('JustifyLeft');
});
$('#c-a').click(function(){
    tinyMCE.execCommand('JustifyCenter');
});
$('#r-a').click(function(){
    tinyMCE.execCommand('JustifyRight');
});

//commands for inserting ul or ol
$('#ul').click(function(){
    tinyMCE.execCommand('InsertUnorderedList');
});
$('#ol').click(function(){
    tinyMCE.execCommand('InsertOrderedList');
});

where #bold, #italic, #ul etc. are ids of the html elements which you are using to do implement styling. For example:

<button id="bold">B</button>

This button will bold the text, no matter the text is in which instance of tinymce.
Though the only drawback with this functionality is that you will have to do a lot of work for showing the effect on particular button when its on or off. If you are not concerned with that then this will do the trick.

I hope it helps...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文