【发布时间】:2011-10-01 15:09:08
【问题描述】:
CKEditor 目前内置了一个 iframe 插件,它允许将 iframe 插入到编辑器中,但无论 iframe 中有什么,都会显示一个相当难看的占位符。我正在开发一个插件,该插件从用户那里获取视频 ID,并构建一个 iframe,该 iframe 指向用于在我们网站上显示视频的页面。我希望我的用户看到一个占位符,清楚地表明他们已插入视频:
丑陋的 iframe 占位符:
漂亮的视频占位符:
为了做到这一点,我在插件的 onOk 函数中添加了这段 Javascript。我已经包含了上下文:
onOk: function () {
var videoID = this.getValueOf('info', 'video_id');
var caption = this.getValueOf('info', 'caption');
var display = this.getValueOf('info', 'display');
var size = this.getValueOf('info', 'size');
var width = size * 160;
var height = size * 120;
if (videoID.length > 0) {
if (display == 0) {
e.insertHtml("<a onclick=window.open(this.href,this.target,'width=" + width + ",height=" + height + "'); return false; target=newVideo href=/kbs/Video.aspx?videoId=" + videoID + "&Width=" + width + "&Height=" + height + ">" + caption + "</a>");
} else {/*Here is the relevant code that is applied when display is set to embedded*/
var iframeNode;
//iframeNode = "<iframe height='" + (height + 40) + "' width='" + (width + 40) + "' src='/kbs/Video.aspx?videoId=1&height=" + height + "&width=" + width + "' frameBorder='0' scrolling='no'></iframe>");
iframeNode = new CKEDITOR.dom.element('iframe');
iframeNode.setAttribute('class', 'GROK-Video');
iframeNode.setAttribute('height', height+40);
iframeNode.setAttribute('width', width + 40);
iframeNode.setAttribute('src', '/kbs/Video.aspx?videoId=1&height=' + height + '&width=' + width);
iframeNode.setAttribute('frameBorder', 0);
iframeNode.setAttribute('scrolling', 'no');
var newFakeImage = e.createFakeElement(iframeNode, 'cke_GROKVideo', 'iframe', true);
e.insertElement(newFakeImage);
}
}
}
神奇之处在于编辑器的 createFakeElement 函数。它用一个虚拟元素替换了我的 iframe,该元素显示为我在 plugin.js 中使用这段 CSS 选择的任何图像:
editor.addCss(
'img.cke_GROKVideo' +
'{' +
'background-image: url(' + CKEDITOR.getUrl(this.path + 'images/YouTubePlayButton.png') + ');' +
'background-position: center center;' +
'background-repeat: no-repeat;' +
'border: 1px solid #a9a9a9;' +
'width: 80px;' +
'height: 80px;' +
'}'
);
好吧,我已经走了很远,但是当查看源代码时就会出现问题。图像变回 iframe,iframe 插件将其替换为自己的占位符!更糟糕的是,当文档被保存并重新打开进行编辑时,同样的事情也会发生。如何在不破坏或替换内置 iframe 插件占位符的情况下保留播放按钮占位符? pagebreak 插件似乎与 div 做了类似的事情,但我不确定 CKEditor 在将假元素放入编辑器时如何区分 pagebreak div 和普通 div。有没有人尝试过这样的事情?我不希望仅仅为此在我的网站上创建自定义 HTML 元素。
【问题讨论】:
-
如果有人需要任何想法,我在 CKEditor 论坛上找到了这个帖子:cksource.com/forums/viewtopic.php?f=11&t=18793&start=10
标签: javascript plugins iframe ckeditor placeholder