返回介绍

Usage with module loaders

发布于 2019-05-06 06:49:50 字数 3143 浏览 961 评论 0 收藏 0

TinyMCE can easily be installed with npm and used with module loaders such as Webpack and Browserify.

First decide how to load your modules.

ES2015 modules

If you are using ES2015 modules, a simple example could look something like this.

// Import TinyMCE
import tinymce from 'tinymce/tinymce';

// A theme is also required
import 'tinymce/themes/modern/theme';

// Any plugins you want to use has to be imported
import 'tinymce/plugins/paste';
import 'tinymce/plugins/link';

// Initialize the app
tinymce.init({
  selector: '#tiny',
  plugins: ['paste', 'link']
});

CommonJS modules

The example is nearly the same if you are using CommonJS modules. However, a different require function is used to import the dependencies.

// Import TinyMCE
var tinymce = require('tinymce/tinymce');

// A theme is also required
require('tinymce/themes/modern/theme');

// Any plugins you want to use has to be imported
require('tinymce/plugins/paste');
require('tinymce/plugins/link');

// Initialize the app
tinymce.init({
  selector: '#tiny',
  plugins: ['paste', 'link']
});

Getting the skin

A skin consists of some fonts and CSS files used by the editor and TinyMCE will not work without them. By default TinyMCE looks for these files in a /skins directory located in your root directory. This can be configured in the init object.

The quickest way to get started is to copy the skin that comes packaged with TinyMCE. This skin is located in node_modules/tinymce/skins - either manually copy the files in finder/file explorer or use the terminal:

MacOS and Linux

cp -r node_modules/tinymce/skins skins

Windows

xcopy /I /E node_modules/tinymce/skins skins

Webpack file-loader

Another option when using Webpack is to use the file loader together with the require.context function to copy the skins directory by adding these lines before the call to the TinyMCE init function:

require.context(
  'file?name=[path][name].[ext]&context=node_modules/tinymce!tinymce/skins',
  true,
  /.*/
);

This copies the skins directory from node_modules/tinymce to your output directory as defined in your Webpack configuration. This is useful as you can easily move the output directory of your build without having to copy over the skins folder manually - webpack will take care of it for you.

Minification with UglifyJS2

Another popular JS minification tool, UglifyJS2, will corrupt TinyMCE unless ascii-only is set.

This can happen when it is used directly or through module bundler. In Webpack, -p CLI option cannot be used to bundle TinyMCE, and instead, you’ll need to configure minification explicitly:

plugins: [
  new webpack.optimize.UglifyJsPlugin({
      /*...*/
      output: {
        "ascii_only": true
      }
  })
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文