angularjs - 使用 Angular js 的 Tinymce 编辑器无法在 IE 上运行

标签 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组件并注入(inject)主模块

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

])

管理.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'
  ]);
};

伙计们,任何人都可以提供一些见解吗?

最佳答案

问题是 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 上运行但在 IE 上不受支持的 document.currentScript

// 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;
})

关于angularjs - 使用 Angular js 的 Tinymce 编辑器无法在 IE 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37163463/

相关文章:

tinyMCE 自动调整大小问题

Angularjs orderBy 与整数字符串

angularjs - Angular JS 命名约定($、camelCase 和 PascalCase)

javascript - Angular 1.5 组件路由器兄弟组件

javascript - 在 AngularJS 中处理页眉和页脚

javascript - TinyMCE返回html标签

angularjs - Firebase 数据库中匹配时发送电子邮件

javascript - 如何将大量Javascript文件注入(inject)CHROME EXTENSION?

javascript - Django-tinymce 未加载 javascript

javascript - 删除 TinyMCE 控件并重新添加