【发布时间】:2017-11-23 07:17:02
【问题描述】:
是否可以在编辑时使用 CKEditor 的另一个功能为所见即所得模式添加一个 css 类(而不是结果内容)? (就像拼写检查器/scayt 仅在所见即所得模式下添加带有波浪下划线样式的跨度)
我想要的场景
我已经为 CKEditor 4.7 创建了一个插件,它可以搜索具有特定内容的特定标签(例如,一个可能导致最终网站上的非预期“空格”的空段落)并向标签添加一个 css 类。该类添加了一个红色边框以通知编辑器有关“空”标签的信息。
我实际上使用editor.document.$.getElementsByTagName(tagName); 和纯javascript 来添加css 类rte-empty。
我的问题
我的方法也将 css 类添加到 <textarea /> 的最终内容中。
这是我发布问题时的代码:
/**
* Check for empty tags plugin
*/
'use strict';
(function () {
CKEDITOR.plugins.add('emptytags', {
lang: "de,en",
onLoad: function(editor) {
CKEDITOR.addCss(
'.cke_editable .rte-empty {' +
' border: 1px dotted red;' +
'}'
);
},
init: function (editor) {
// Default Config
var defaultConfig = {
tagsToCheck: {0: 'p'}
};
var config = CKEDITOR.tools.extend(defaultConfig, editor.config.emptytags || {}, true);
editor.addCommand('checkForEmptyTags', {
exec: function (editor) {
var editorContent = editor.getData();
// Stop check and inform editor if the editor has no content.
if (editorContent === '') {
alert(editor.lang.emptytags.AlertEditorContentEmpty)
return;
}
// Check if tag name's to check are set
if (config.tagsToCheck.length > 0 && config.tagsToCheck[0] !== null) {
var index;
for (index = 0; index < config.tagsToCheck.length; ++index) {
var tagName = config.tagsToCheck[index];
var tags = editor.document.$.getElementsByTagName(tagName);
for (var i=0; i < tags.length; i++) {
if (checkForRealEmptyTag(tags[i].innerHTML)
|| checkForEmptyTagWithSpace(tags[i].innerHTML)
|| checkForEmptyTagWithNbsp(tags[i].innerHTML)
) {
if(tags[i].className.indexOf("rte-empty") < 0){
tags[i].className += "rte-empty";
}
var noEmptyTagFound = false;
} else {
tags[i].classList.remove("rte-empty");
}
}
}
// Inform editor that no empty tag can be found (anymore)
if (noEmptyTagFound === true) {
alert(editor.lang.emptytags.AlertEditorNoEmptyTagFound);
}
}
}
});
editor.ui.addButton && editor.ui.addButton('Check for empty tags', {
label: editor.lang.emptytags.ToolbarButton,
command: 'checkForEmptyTags',
toolbar: 'insertcharacters'
});
}
});
function checkForRealEmptyTag(content) {
return content.length === 0;
}
function checkForEmptyTagWithNbsp(content) {
return content === ' ' || content.trim() === '<br>';
}
function checkForEmptyTagWithSpace(content) {
return content.trim().length === 0;
}
})();
因此
我正在寻找类似SCAYT 插件的可能性:添加带有类的跨度标签,用于为字典中找不到的单词添加波浪下划线。
【问题讨论】:
标签: ckeditor wysiwyg ckeditor4.x