Angular js的小型MCE编辑器不能用于IE

时间:2016-05-11 12:59:23

标签: angularjs tinymce

我对angular js相对比较新,并且尝试将angular-ui-tiny mce编辑器集成到我的站点,但是编辑器没有加载到IE-11上,控制台说错误“加载失败http://snet-dev.subaru1.com:7777/homenew//themes/modern/theme.js “(不知道为什么我们在homenew之后有2个正斜杠)”。 我试图创建grunt任务来将_bower_Components中的theme.js复制到dist文件夹中,这使编辑器加载到本地IE-11上。 现在,当我将代码提升为开发服务器时,编辑器不会在页面上加载“无法加载http://snet-dev.subaru1.com:7777/homenew//themes/modern/theme.js”。

以下是我使用Tiny -MCE的代码。

App.js

我已成功安装了tiny-mce bower组件并注入主模块

 angular.module('dashboardSubaruNetApp', [
   'ui.tinymce',

])

Admin.html

 <textarea ui-tinymce="tinymceOptions" ng-model="data.content" ng-required="!data.url"></textarea> 

GruntFile.js

// Generated on 2014-12-29 using generator-angular 0.9.8
'use strict';

// # Globbing
// for performance reasons we're only matching one level down:
// 'test/spec/{,*/}*.js'
// use this if you want to recursively match all subfolders:
// 'test/spec/**/*.js'

module.exports = function(grunt) {

  // Load grunt tasks automatically
  require('load-grunt-tasks')(grunt);

  // Time how long tasks take. Can help when optimizing build times
  require('time-grunt')(grunt);

  // Configurable paths for the application
  var appConfig = {
    app: require('./bower.json').appPath || 'app',
    dist: 'dist'
  };

  // Define the configuration for all the tasks
  grunt.initConfig({

    // Project settings
    yeoman: appConfig,

    // Watches files for changes and runs tasks based on the changed files
    watch: {
      bower: {
        files: ['bower.json'],
        tasks: ['wiredep']
      },
      js: {
        files: ['<%= yeoman.app %>/scripts/{,*/}*.js'],
        tasks: ['newer:jshint:all'],
        options: {
          livereload: '<%= connect.options.livereload %>'
        }
      },
      jsTest: {
        files: ['test/spec/{,*/}*.js'],
        tasks: ['newer:jshint:test', 'karma']
      },
      styles: {
        files: ['<%= yeoman.app %>/styles/{,*/}*.css'],
        tasks: ['newer:copy:styles', 'autoprefixer']
      },
      gruntfile: {
        files: ['Gruntfile.js']
      },
      livereload: {
        options: {
          livereload: '<%= connect.options.livereload %>'
        },
        files: [
          '<%= yeoman.app %>/{,*/}*.html',
          '.tmp/styles/{,*/}*.css',
          '<%= yeoman.app %>/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}'
        ]
      }
    },

    // The actual grunt server settings
    connect: {
      options: {
        port: 9000,
        // Change this to '0.0.0.0' to access the server from outside.
        hostname: 'localhost',
        livereload: 35729
      },
      livereload: {
        options: {
          open: true,
          middleware: function(connect) {
            return [
              connect.static('.tmp'),
              connect().use(
                '/bower_components',
                connect.static('./bower_components')
              ),
              connect.static(appConfig.app)
            ];
          }
        }
      },
      test: {
        options: {
          port: 9001,
          middleware: function(connect) {
            return [
              connect.static('.tmp'),
              connect.static('test'),
              connect().use(
                '/bower_components',
                connect.static('./bower_components')
              ),
              connect.static(appConfig.app)
            ];
          }
        }
      },
      dist: {
        options: {
          open: true,
          base: '<%= yeoman.dist %>'
        }
      }
    },

    // Make sure code styles are up to par and there are no obvious mistakes
    jshint: {
      options: {
        jshintrc: '.jshintrc',
        reporter: require('jshint-stylish')
      },
      all: {
        src: [
          'Gruntfile.js',
          '<%= yeoman.app %>/scripts/{,*/}*.js'
        ]
      },
      test: {
        options: {
          jshintrc: 'test/.jshintrc'
        },
        src: ['test/spec/{,*/}*.js']
      }
    },

    // Empties folders to start fresh
    clean: {
      dist: {
        files: [{
          dot: true,
          src: [
            '.tmp',
            '<%= yeoman.dist %>/{,*/}*',
            '!<%= yeoman.dist %>/.git*'
          ]
        }]
      },
      server: '.tmp'
    },

    // Add vendor prefixed styles
    autoprefixer: {
      options: {
        browsers: ['last 1 version']
      },
      dist: {
        files: [{
          expand: true,
          cwd: '.tmp/styles/',
          src: '{,*/}*.css',
          dest: '.tmp/styles/'
        }]
      }
    },

    // Automatically inject Bower components into the app
    wiredep: {
      app: {
        src: ['<%= yeoman.app %>/home.html'],
        ignorePath: /\.\.\//
      }
    },

    // Renames files for browser caching purposes
    filerev: {
      dist: {
        src: [
          '<%= yeoman.dist %>/scripts/{,*/}*.js',
          '<%= yeoman.dist %>/styles/{,*/}*.css',
          '<%= yeoman.dist %>/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}',
          '<%= yeoman.dist %>/styles/fonts/*'
        ]
      }
    },

    // Reads HTML for usemin blocks to enable smart builds that automatically
    // concat, minify and revision files. Creates configurations in memory so
    // additional tasks can operate on them
    useminPrepare: {
      html: '<%= yeoman.app %>/home.html',
      options: {
        dest: '<%= yeoman.dist %>',
        flow: {
          html: {
            steps: {
              js: ['concat', 'uglifyjs'],
              css: ['cssmin']
            },
            post: {}
          }
        }
      }
    },

    // Performs rewrites based on filerev and the useminPrepare configuration
    usemin: {
      html: ['<%= yeoman.dist %>/{,*/}*.html'],
      css: ['<%= yeoman.dist %>/styles/{,*/}*.css'],
      options: {
        assetsDirs: ['<%= yeoman.dist %>', '<%= yeoman.dist %>/images']
      }
    },

    // The following *-min tasks will produce minified files in the dist folder
    // By default, your `index.html`'s <!-- Usemin block --> will take care of
    // minification. These next options are pre-configured if you do not wish
    // to use the Usemin blocks.
    // cssmin: {
    //   dist: {
    //     files: {
    //       '<%= yeoman.dist %>/styles/main.css': [
    //         '.tmp/styles/{,*/}*.css'
    //       ]
    //     }
    //   }
    // },
    // uglify: {
    //   dist: {
    //     files: {
    //       '<%= yeoman.dist %>/scripts/scripts.js': [
    //         '<%= yeoman.dist %>/scripts/scripts.js'
    //       ]
    //     }
    //   }
    // },
    // concat: {
    //   dist: {}
    // },

    imagemin: {
      dist: {
        files: [{
          expand: true,
          cwd: '<%= yeoman.app %>/images',
          src: '{,*/}*.{png,jpg,jpeg,gif}',
          dest: '<%= yeoman.dist %>/images'
        }]
      }
    },

    svgmin: {
      dist: {
        files: [{
          expand: true,
          cwd: '<%= yeoman.app %>/images',
          src: '{,*/}*.svg',
          dest: '<%= yeoman.dist %>/images'
        }]
      }
    },

    htmlmin: {
      dist: {
        options: {
          collapseWhitespace: true,
          conservativeCollapse: true,
          collapseBooleanAttributes: true,
          removeCommentsFromCDATA: true,
          removeOptionalTags: true
        },
        files: [{
          expand: true,
          cwd: '<%= yeoman.dist %>',
          src: ['*.html', 'views/{,*/}*.html'],
          dest: '<%= yeoman.dist %>'
        }]
      }
    },

    // ng-annotate tries to make the code safe for minification automatically
    // by using the Angular long form for dependency injection.
    ngAnnotate: {
      dist: {
        files: [{
          expand: true,
          cwd: '.tmp/concat/scripts',
          src: ['*.js', '!oldieshim.js'],
          dest: '.tmp/concat/scripts'
        }]
      }
    },

    // Replace Google CDN references
    cdnify: {
      dist: {
        html: ['<%= yeoman.dist %>/*.html']
      }
    },

    // Copies remaining files to places other tasks can use
    copy: {
      dist: {
        files: [{
          expand: true,
          dot: true,
          cwd: '<%= yeoman.app %>',
          dest: '<%= yeoman.dist %>',
          src: [
            '*.{ico,png,txt}',
            '.htaccess',
            '*.html',
            'views/{,*/}*.html',
            'images/{,*/}*.*',
            'fonts/*'
          ]
        }, {
          expand: true,
          cwd: '.tmp/images',
          dest: '<%= yeoman.dist %>/images',
          src: ['generated/*']
        }, {
          expand: true,
          cwd: 'bower_components/bootstrap/dist',
          src: 'fonts/*',
          dest: '<%= yeoman.dist %>'
        }, {
          expand: true,
          cwd: 'bower_components/font-awesome',
          src: 'fonts/*',
          dest: '<%= yeoman.dist %>'
        }, {
          expand: true,
          cwd: 'bower_components/tinymce-dist/themes/modern/',
          src: ['**'],
          dest: '<%= yeoman.dist %>/scripts/themes/modern/'
        }, {
          expand: true,
          cwd: 'bower_components/tinymce-dist/skins/',
          src: ['**'],
          dest: '<%= yeoman.dist %>/scripts/skins/'
        }, {
          expand: true,
          cwd: 'bower_components/tinymce-dist/plugins/link/',
          src: ['**'],
          dest: '<%= yeoman.dist %>/scripts/plugins/link/'
        }]
      },
      styles: {
        expand: true,
        cwd: '<%= yeoman.app %>/styles',
        dest: '.tmp/styles/',
        src: '{,*/}*.css'
      }
    },

    // Run some tasks in parallel to speed up the build process
    concurrent: {
      server: [
        'copy:styles'
      ],
      test: [
        'copy:styles'
      ],
      dist: [
        'copy:styles'
        //'imagemin',
        //'svgmin'
      ]
    },

    // Test settings
    karma: {
      unit: {
        configFile: 'test/karma.conf.js',
        singleRun: true
      }
    }
  });


  grunt.registerTask('serve', 'Compile then start a connect web server', function(target) {
    if (target === 'dist') {
      return grunt.task.run(['build', 'connect:dist:keepalive']);
    }

    grunt.task.run([
      'clean:server',
      'wiredep',
      'concurrent:server',
      'autoprefixer',
      'connect:livereload',
      'watch'
    ]);
  });

  grunt.registerTask('server', 'DEPRECATED TASK. Use the "serve" task instead', function(target) {
    grunt.log.warn('The `server` task has been deprecated. Use `grunt serve` to start a server.');
    grunt.task.run(['serve:' + target]);
  });

  grunt.registerTask('test', [
    'clean:server',
    'concurrent:test',
    'autoprefixer',
    'connect:test',
    'karma'
  ]);

  grunt.registerTask('build', [
    'clean:dist',
    'wiredep',
    'useminPrepare',
    'concurrent:dist',
    'autoprefixer',
    'concat',
    'ngAnnotate',
    'copy:dist',
    'cdnify',
    'cssmin',
    'uglify',
    //'filerev',
    'usemin',
    'htmlmin'
  ]);

  grunt.registerTask('default', [
    //'newer:jshint',
    //'test',
    'build'
  ]);
};

伙计们,任何人,请提供一些见解。

1 个答案:

答案 0 :(得分:5)

问题是TinyMCE使用以下代码来确定托管脚本的位置,并使用它来加载相对于TinyMCE的插件和主题文件。

https://github.com/tinymce/tinymce/blob/b2a50bc17ea903a344f29ef664c596aa3c85c610/js/tinymce/classes/EditorManager.js#L184

首先它试图找到tinymce的脚本标记,但是当你将它与你的代码捆绑在一起时,这个检查失败了。

var scripts = document.getElementsByTagName('script');
for (var i = 0; i < scripts.length; i++) {
    src = scripts[i].src;

    // Script types supported:
    // tinymce.js tinymce.min.js tinymce.dev.js
    // tinymce.jquery.js tinymce.jquery.min.js tinymce.jquery.dev.js
    // tinymce.full.js tinymce.full.min.js tinymce.full.dev.js
    var srcScript = src.substring(src.lastIndexOf('/'));
    if (/tinymce(\.full|\.jquery|)(\.min|\.dev|)\.js/.test(src)) {
        if (srcScript.indexOf('.min') != -1) {
            suffix = '.min';
        }

        baseURL = src.substring(0, src.lastIndexOf('/'));
        break;
    }
}

接下来,它尝试使用适用于Chrome的document.currentScript,但在IE上不受支持。

// We didn't find any baseURL by looking at the script elements
// Try to use the document.currentScript as a fallback
if (!baseURL && document.currentScript) {
    src = document.currentScript.src;

    if (src.indexOf('.min') != -1) {
        suffix = '.min';
    }

    baseURL = src.substring(0, src.lastIndexOf('/'));
}

解决这个问题的两种方法是:

1。将所有TinyMCE与您的代码捆绑在一起

解决此问题的最佳方法是将所有TinyMCE的插件和主题JavaScript与您的代码捆绑在一起,并使用您的CSS捆绑皮肤CSS文件。查看您的Grunt设置,最好的方法是在您的bower.json中添加覆盖,例如

"overrides": {
  "tinymce-dist": {
    "main": [
      "tinymce.js",
      "plugins/lists/plugin.js",
      "plugins/link/plugin.js",
      /* Add all other plugins used here ... */
      "themes/modern/theme.js",
      "skins/lightgray/skin.min.css",
      "skins/lightgray/content.min.css"
    ]
  }
},

要阻止TinyMCE尝试远程加载皮肤CSS文件,您可能需要将skin设置为false,例如

$scope.tinymceOptions = {
    menubar: false,
    height: 150,
    skin: false,
    ...
}

2。设置显式baseUrl

或者你可以通过在TinyMCE的配置上设置baseUrl属性并重新实现代码来查找你的脚本名称来告诉TinyMCE基本URL应该是什么,例如

.factory('uiTinymceConfig', function() {
    var scripts = document.getElementsByTagName("script")
    var config = {};
    for (var i = 0; i < scripts.length; i++) {
        src = scripts[i].src;

        // Change this test to match the name of your script bundle
        var srcScript = src.substring(src.lastIndexOf('/'));
        if (/learn(\.min|)\.js/.test(src)) {
            if (srcScript.indexOf('.min') != -1) {
                config.suffix = '.min';
            }
            config.baseUrl = src.substring(0, src.lastIndexOf('/'));
            break;
        }
    }
    // Do this to set preInit options for suffix as angular-ui-tinymce doesn't respect suffix config
    angular.extend(tinymce, config);
    return config;
})