tinymce plugin developemn
此album插件开发用于后台编辑器的图片插入,通过后台读取图片文件列表,album插件负责载入album view,即通过 iframe 载入页面,点击album view里的图片,调用window.parent 即tinymce运行的页面window对像设置的回掉函数来进行页面间的数据通信。以下是笔记内容,代码裁剪重要部分。
https://www.tiny.cloud/docs/advanced/creating-a-plugin/
https://www.tiny.cloud/docs/api/tinymce/tinymce.windowmanager/#open
Example of the plugin file structure
/tinymce/plugins/example/plugin.js
/tinymce/plugins/example/plugin.min.js
This new example plugin can now be loaded using the tinymce.init plugins option.
tinymce.init({
selector: 'textarea',
plugins: 'example'
});
Example of loading the plugin from another URL
<script src="/tinymce/js/tinymce.min.js"></script>
<script src="/scripts/my.tinymce.plugin.js"></script>
Example plugin
tinymce.PluginManager.add('album', function(editor, url) {
window.pumppipe = function(e){
// alert("pumppipe"+JSON.stringify(e));
// var dialogArguments = top.tinymce.activeEditor.windowManager.getParams();
top.tinymce.activeEditor.insertContent('<img src="'+e+'" alt=""/>');
}
function albumView() {
// Open window with a specific url
var w = editor.windowManager.open({
title: '<?php echo lang("Album View");?>',
url: '<?php echo CONF::BASEURL."/album/index/0/50/0/pumppipe"; ?>',
width: 800,
height: 600,
buttons: [{
text: 'Close',
onclick: 'close'
}]
});
}
// Add a button that opens a window
editor.addButton('album', {
text: '<?php echo lang("Album plugin");?>',
icon: false,
onclick: albumView
});
// Adds a menu item to the tools menu
editor.addMenuItem('album', {
text: '<?php echo lang("Album plugin");?>',
context: 'tools',
onclick: albumView
});
return {
getMetadata: function() {
return {
name: "album plugin",
url: "https://www.aiirobo.com"
};
}
};
});
communication between iframe and main window
<img class="item" src="<?php echo "$url/$value"; ?>" onclick="pumpback(this.src);">
<script>
function pumpback(e){
window.parent && window.parent.<?php echo $pumppipe; ?> && window.parent.<?php echo $pumppipe; ?>(e);
}
</script>
Example init
Here is an example of how to use the new toolbar button.
tinymce.init({
selector: 'textarea',
plugins: 'album',
toolbar: 'album'
});
效果截图