我实现了一个TinyMCE按钮来增加字母间距http://fiddle.tinymce.com/Z9eaab/31。如果在底部的文本区域中输入“some text”字样,然后选择“some”并多次点击“增加字母间距”按钮,字母间距只会在第一次增加。如果选择第二个单词“文本”,则每次点击“增大字母间距”时,间距都会增加。正如它应该。自定义TinyMCE 4.x按钮增加字母间距不起作用
我可以从第9行的console.log看到,当它不工作时,这是因为当前的间距读取并不反映最后一次增加,所以它只是重做第一次。
<script type="text/javascript">
tinymce.PluginManager.add('example', function(editor, url) {
// Add a button that opens a window
editor.addButton('example1', {
text: 'Increase letter spacing',
icon: false,
onclick: function() {
var currentSpacing = new Number($(tinyMCE.activeEditor.selection.getNode()).css('letter-spacing').replace('px', ''));
console.log("spacing read is" + currentSpacing);
currentSpacing = currentSpacing + 1;
tinymce.activeEditor.formatter.register('increaseSpacing', {
inline: 'span',
styles: {
'letter-spacing': currentSpacing + 'px'
}
});
tinymce.activeEditor.formatter.apply('increaseSpacing');
}
});
editor.addButton('example2', {
text: 'Decrease letter spacing',
icon: false,
onclick: function() {
var currentSpacing = new Number($(tinyMCE.activeEditor.selection.getNode()).css('letter-spacing').replace('px', ''));
currentSpacing = currentSpacing - 1;
tinymce.activeEditor.formatter.register('decreaseSpacing', {
inline: 'span',
styles: {
'letter-spacing': currentSpacing + 'px'
}
});
tinymce.activeEditor.formatter.apply('decreaseSpacing');
}
});
// Adds a menu item to the tools menu
editor.addMenuItem('example', {
text: 'Example plugin',
context: 'tools',
onclick: function() {
// Open window with a specific url
editor.windowManager.open({
title: 'TinyMCE site',
url: 'http://www.tinymce.com',
width: 400,
height: 300,
buttons: [{
text: 'Close',
onclick: 'close'
}]
});
}
});
});
tinymce.init({
selector: "textarea",
plugins: "example",
toolbar: "example1 example2 undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
});
</script>
<form method="post" action="dump.php">
<textarea name="content"></textarea>
</form>
有谁知道这是怎么回事?
你会发现,我感动的注册格式申报到' tinymce.init'部分来提高性能 - 所以它不会尝试重新注册每个按钮点击。我在'formatter.apply()'调用中传递动态值。您可能想考虑从incr/decr函数中提取公共代码。只是一个建议。 –
谢谢!优秀! – Steve