如何结合CKEditor在我的应用程序代码中使用RequireJS, grunt和uglify

How to combine CKEditor in my app code using RequireJS, grunt and uglify?

本文关键字:RequireJS uglify grunt 代码 应用程序 何结合 结合 我的 CKEditor      更新时间:2023-09-26

这是我的'common.js'文件:

requirejs.config({
paths: {
    domReady: '../vendor/requirejs-domready/domReady',
    jquery: 'lib/jquery',
    datatables: '../vendor/datatables/media/js/jquery.dataTables.min',
    tabletools: '../vendor/datatables/extensions/TableTools/js/dataTables.tableTools',
    fixedheader: '../vendor/datatables/extensions/FixedHeader/js/dataTables.fixedHeader.min',
    'datatables-bootstrap': '../vendor/datatables-bootstrap3-plugin/media/js/datatables-bootstrap3.min',
    jeditable: '../vendor/jeditable/jeditable',
    routing: '../../bundles/fosjsrouting/js/router',
    routes: '../vendor/fosjsrouting/fos_js_routes',
    'ckeditor-core':'../vendor/ckeditor/ckeditor',
    'ckeditor-jquery':'../vendor/ckeditor/adapters/jquery',
    selectize: '../vendor/selectize/dist/js/selectize.min',
    sifter: '../vendor/sifter/sifter.min',
    microplugin: '../vendor/microplugin/src/microplugin',
    datepicker: '../vendor/zebra-datepicker/public/javascript/zebra_datepicker',
    bootstrap: '../vendor/bootstrap/dist/js/bootstrap.min'
},
shim: {
    bootstrap: {
        deps: ['jquery']
    },
    jeditable: {
        deps: ['jquery']
    },
    routing: {
        exports: 'Routing'
    },
    routes: {
        deps: ['routing']
    },
    'ckeditor-jquery':{
        deps:['jquery','ckeditor-core']
    },
    selectize: {
        deps: ['jquery', 'sifter', 'microplugin']
    },
    'tabletools': {
        deps: ['datatables']
    },
    'fixedheader': {
        deps: ['datatables']
    }
}
});

. .下面是我的Gruntfile.js的相关部分:

requirejs: {
        main: {
            options: {
                mainConfigFile: '<%= appDir %>/js/common.js',
                appDir: '<%= appDir %>',
                baseUrl: './js',
                dir: '<%= builtDir %>',
                optimizeCss: "none",
                optimize: "none",
                modules: [
                    {
                        name: 'common',
                        include: ['jquery', 'domReady', 'bootstrap', 'ckeditor-jquery', 'selectize', 'jeditable', 'datepicker', 'routing', 'routes']
                    },
                    {
                        name: 'app/mycode',
                        exclude: ['common']
                    },
                    // other app/<mycode> entries that exclue common, as above
                ]
            }
        }
    },
    uglify: {
        options: {
            banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */'n',
            compress: {
                drop_console: true // <-remove console.log statements
            }
        },
        build: {
            files: (function() {
                var files = [];
                jsFilePaths.forEach(function(val) {
                    files.push({
                        expand: true,
                        cwd: '<%= builtDir %>',
                        src: val,
                        dest: '<%= builtDir %>'
                    });
                });
                return files;
            })()
        }
    },

…这就是我如何实例化CKEditor在我的代码:

$('.ckeditor').ckeditor({
                customConfig: '',
                toolbarGroups: [
                    {"name": "basicstyles", "groups": ["basicstyles"]},
                    {"name": "links", "groups": ["links"]},
                    {"name": "paragraph", "groups": ["list", "blocks"]},
                    {"name": "document", "groups": ["mode"]},
                    {"name": "insert", "groups": ["insert"]},
                    {"name": "styles", "groups": ["styles"]},
                    {"name": "about", "groups": ["about"]}
                ],
                removeButtons: 'Underline,Strike,Subscript,Superscript,Anchor,Styles,SpecialChar,About,Source',
                removePlugins: 'magicline'
            });

最后……这些是我在Firebug中得到的错误,当我尝试在生产中使用我的丑化代码加载CKEditor(在我的开发环境中优化之前它可以完美地工作):

    "NetworkError: 404 Not Found - http://ams.lprod/skins/moono/editor_gecko.css?t=F0RF"
editor_...?t=F0RF
"NetworkError: 404 Not Found - http://ams.lprod/lang/en-gb.js?t=F0RF"
en-gb.js?t=F0RF
TypeError: d[a] is undefined
    ...is.detect(b,a));var d=this,b=function( {d[a].dir=d.rtl[a]?"rtl":"ltr",c(a,d[a])}...

我试图设置CKEditor实例化代码内的路径使用'skins: ../path/to/ckeditor/css/files',但这也不起作用。顺便说一句,我也试过从网站下载CKEditor,并安装它新鲜的'boswer安装CKEditor ',但不能让它的工作一旦丑,并结合到我的代码使用grunt。

有人能看出我做错了什么吗?还有人能用这个吗?我敢肯定外面的人一定有它的工作,只是我的无知阻碍了我。

解决了:D

如果,像我一样,你有一个Symfony2应用程序,并且你在RequireJS中使用多页shim设置跟踪这些很棒的幻灯片,那么CKEditor解决上述问题的方法是在你的_requirejs.html中包含以下行。在requirejs.config({..})行上方的小文件:

var CKEDITOR_BASEPATH = '{{ app.request.basePath }}/{{ assetsPath }}/vendor/ckeditor/';

你现在应该能够在优化的生产环境中加载CKEditor实例而不会出现任何错误。享受吧!

你可以直接解决。

使用'ckeditor-jquery'和'ckeditor-core'作为一个单独的模块。所以你的grunt文件必须像这样:

requirejs: {
    main: {
        options: {
            mainConfigFile: '<%= appDir %>/js/common.js',
            appDir: '<%= appDir %>',
            baseUrl: './js',
            dir: '<%= builtDir %>',
            optimizeCss: "none",
            optimize: "none",
            modules: [
                {
                    name: 'common',
                    include: ['jquery', 'domReady', 'bootstrap', 'selectize', 'jeditable', 'datepicker', 'routing', 'routes']
                },
                {
                    name: 'app/mycode',
                    exclude: ['common']
                },
                {
                  name: 'ckeditor-jquery',
                  exclude: ["common"]
                },
                {
                  name: 'ckeditor-core',
                  exclude: ["common"]
                }
                // other app/<mycode> entries that exclue common, as above
            ]
        }
    }
},
uglify: {
    options: {
        banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */'n',
        compress: {
            drop_console: true // <-remove console.log statements
        }
    },
    build: {
        files: (function() {
            var files = [];
            jsFilePaths.forEach(function(val) {
                files.push({
                    expand: true,
                    cwd: '<%= builtDir %>',
                    src: val,
                    dest: '<%= builtDir %>'
                });
            });
            return files;
        })()
    }
},