在CKEditor中禁止块/行内“元标记”

8

我正在开发一个自定义CMS,其中集成了CKEditor(4.5.1),用于友好的HTML内容生成。

我们正在开发的一个功能是将页面的某些部分限制为特定组的用户,而根据我所知,最干净的方法是创建一个新标签,并将其用于内容跟踪,例如: <restrict data-usertype="1,2,3">restricted content for user types 1, 2, 3 here</restrict>,这将被后端删除。

我遇到的问题是,我的自定义标签需要隐式支持块和内联标签,并且我不确定如何正确设置它。

我尝试了各种组合,这些组合要么完全禁止添加任何内容,要么完全禁用插件(因为它违反了ACF自己的健全性检查);目前,我配置的方式可以让我添加<restrict>块,并允许我在对话框中编辑它(包括双击),但不会让我嵌套任何内容,并且在切换回源模式时,CKEditor将抛出“could not read attributes of null”警告。

这个插件的当前配置如下:

    CKEDITOR.dtd.restrict = {
        a: 1, abbr: 1, address: 1, area: 1, article: 1, aside: 1, audio: 1, b: 1, bdi: 1, bdo: 1, blockquote: 1,
        br: 1, button: 1, canvas: 1, cite: 1, code: 1, command: 1, datalist: 1, del: 1, details: 1, dfn: 1, div: 1,
        dl: 1, em: 1, embed: 1, fieldset: 1, figure: 1, footer: 1, form: 1, h1: 1, h2: 1, h3: 1, h4: 1, h5: 1, h6: 1,
        header: 1, hgroup: 1, hr: 1, i: 1, iframe: 1, img: 1, input: 1, ins: 1, kbd: 1, keygen: 1, label: 1, map: 1,
        mark: 1, meter: 1, noscript: 1, object: 1, ol: 1, output: 1, progress: 1, p: 1, pre: 1, q: 1, ruby: 1, s: 1,
        samp: 1, script: 1, section: 1, select: 1, small: 1, span: 1, strong: 1, sub: 1, sup: 1, table: 1,
        textarea: 1, time: 1, u: 1, ul: 1, 'var': 1, video: 1, wbr: 1, '#': 1
    }; // Allow <restrict> as a valid tag.
    CKEDITOR.dtd.$block.restrict = 1;
    CKEDITOR.dtd.$inline.restrict = 1;
    CKEDITOR.dtd.$blockLimit.restrict = 1; // Treat <restrict> as a block limiter tag
    CKEDITOR.dtd.$removeEmpty.restrict = 1; // Remove <restrict /> tags if they are empty
    CKEDITOR.dtd.$transparent.restrict = 1; // Treat the tag as transparent as far as content models go
    CKEDITOR.dtd.body.restrict = 1; // Allow it in the body, div and p tags.
    CKEDITOR.dtd.div.restrict = 1;
    CKEDITOR.dtd.p.restrict = 1;

    var allowedEls = ['restrict'];
    for (var i in CKEDITOR.dtd.restrict) {
        if (CKEDITOR.dtd.restrict.hasOwnProperty(i) && i != '#') {
            allowedEls.push(i);
        }
    }

    // Define the widget.
    editor.widgets.add( 'restrict', {
        button: 'Restricted Content',
        dialog: 'restrictDialog',
        template: '<restrict />',
        editables: {},
        allowedContent: allowedEls.join(' ') + '[*]{*}(*)', // All the above elements, with any attributes, styles or classes.
        requiredContent: 'restrict[data-*]',
        upcast: function (element) {
            return element.name == 'restrict';
        },
        init: function () {
            // Some stuff which iterates through the various
            // properties I care about, grabs from data
            // attributes and pushes to this.setData().
        },
        data: function () {
            // Some stuff that just fetches vars from this.data,
            // sets the relevant data attribute and also sets an
            // attribute on the span created by CKEditor since
            // styling and ::before content is used to show who
            // the block is visible to - the result is much like
            // the Show Blocks plugin. This stuff all works
            // correctly and being omitted changes nothing.
        }
    } );

我想我可能没有正确设置editables,也可能是这个标记的通用允许内容出了问题,但我不知道该如何创建这样的标记,而且我无法想象创建这样一个会在浏览器之外被解析的虚幻标记会成为一个新问题。

提前致谢!

1个回答

1

实际上,我找到了解决这个问题的替代方案,主要是通过稍微重新定义问题。

首先,我意识到几乎所有时候 CMS 会限制内容时,它都会在块上下文中进行而不是行内上下文 - 整个 div 部分将被删除而不是一行的某些部分。

一旦我意识到这一点,我就意识到我真正需要构建的不是一个小部件,而是一个更传统的插件。

然后,我从 CKEditor 的 insert-div 插件开始,并开始自定义其以适应我的需求。在插件的 init 方法中,有以下初始设置:

CKEDITOR.dtd.restrict = {};
CKEDITOR.tools.extend(CKEDITOR.dtd.restrict, CKEDITOR.dtd.div);
CKEDITOR.dtd.$block.restrict = 1;
CKEDITOR.dtd.$blockLimit.restrict = 1; // Treat <restrict> as a block limiter tag
CKEDITOR.dtd.$removeEmpty.restrict = 1; // Remove <restrict /> tags if they are empty
CKEDITOR.dtd.body.restrict = 1; // Allow it in the body, div and p tags.
CKEDITOR.dtd.div.restrict = 1;

这在很大程度上满足了插件的需求,但我正在使用预构建的CKEditor副本,并且没有时间或意愿设置重建/重新压缩CKEditor的过程-这是一个问题,因为$blockLimit仅在启动时使用,并且在插件级别添加它不起作用,因为它被评估的唯一时间已经在加载插件之前运行。为了解决这个问题,插件在运行后克隆了CKEDITOR.dom.editorPath的闭包和定义,以便正确重新评估DTD方法。

除此之外,我适当地将所有的“creatediv”/“editdiv”/“removediv”引用更改为“*restrict”,将检查“div”的位置更改为“restrict”,并且使用实际需要的对话框替换了对话框定义,这非常特定于用例。由于它大多是一个复制/粘贴的工作,因此提供我可以提供的代码似乎并不实用,因为不是复制/粘贴的那些部分是针对产品的特定部分。

这更加复杂的原因在于我还动态地管理着限制元素的样式,以产生类似于“显示块”标记的外观,在标记本身中通过restrict::before { content: '...' }记录了限制列表,该列表指向限制标记本身的属性cke-restrict-description,每次对话框运行时以及编辑器切换到所见即所得模式时(一次在instanceReady上,一次在editor.on('mode')上,其中editor.mode == 'wysiwyg')都会更新。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接