简体   繁体   English

使用 ES6,将 tinymce 作为 npm 包导入

[英]using ES6, import tinymce as npm package

I have installed tinymce as package in my project using this tutorial .我已经使用本教程在我的项目中安装了 tinymce 作为包。

I'm trying to import tinymce into my project.我正在尝试import tinymce import到我的项目中。 It does something (hides my textarea , expected behaviour) when I call init() , but it does not show me the wysiwyg editor.当我调用init()时它会做一些事情(隐藏我的textarea ,预期的行为init() ,但它没有向我显示所见即所得的编辑器。

If I include the url in my html file it works, but that's not what I want, because I don't include my custom component every time the page gets loaded.如果我将 url 包含在我的 html 文件中,它就可以工作,但这不是我想要的,因为我不会在每次加载页面时都包含我的自定义组件。 The script tag would be overkill for some users.脚本标签对于某些用户来说是多余的。


I want to import tinymce like this (or simular):我想像这样(或类似)导入 tinymce:

import 'tinymce';

and use it like this:并像这样使用它:

constructor(){
  tinymce.init({
    selector: "#mytextarea"
  })
}

This does not give me any error, and it hides the textarea, but it does not build a wysiwyg editor.这不会给我任何错误,它隐藏了文本区域,但它没有构建所见即所得的编辑器。


The only thing working so far:到目前为止唯一有效的方法:

<script src="/path/to/tinymce.min.js"></script>

I prefer not to edit the JavaScript file, because that would break on updates.我不想编辑 JavaScript 文件,因为这会在更新时中断。 If it is the only way though, I'll simply have to.如果这是唯一的方法,我将不得不这样做。

How can I import the library into my ES6 module?如何将库导入我的 ES6 模块?

You can follow the steps mentioned in the below link您可以按照以下链接中提到的步骤操作

https://github.com/tinymce/tinymce/issues/2836 https://github.com/tinymce/tinymce/issues/2836

But still you may need to do some css changes because skins were not loaded.但是你仍然可能需要做一些 css 更改,因为皮肤没有加载。

Run npm i url-loader --save运行 npm i url-loader --save

In webpack config file add this { loader: 'url-loader?limit=100000', test: /.(png|woff|woff2|eot|ttf|svg|gif)$/ }在 webpack 配置文件中添加这个 { loader: 'url-loader?limit=100000', test: /.(png|woff|woff2|eot|ttf|svg|gif)$/ }

There's is a more up to date official documentantion describing how to use tinymce directly from npm , without bundling , and set up your paths, here: https://www.tiny.cloud/docs/configure/editor-appearance/#skin_url有一个更新的官方文档描述了如何直接从 npm 使用 tinymce ,无需捆绑,并设置你的路径,这里: https : //www.tiny.cloud/docs/configure/editor-appearance/#skin_url

In my case:就我而言:

tinymce.init({
  selector: '.editor', 
  skin_url: '/node_modules/tinymce/skins/ui/oxide',
});

While this works, it doesn't seem they are prioritizing much over this since it still tryes to load some css from another path.虽然这有效,但似乎他们并没有优先考虑这一点,因为它仍然试图从另一个路径加载一些 css。

Additionally you can provide a language_url for the tinymce-i18n package此外,您可以为 tinymce-i18n 包提供 language_url

The official documentation points out that the skin files should be copied to the relative folder 'skins' in the public web folder:官方文档指出应该将皮肤文件复制到公共网络文件夹中的相对文件夹'skins'中:

In Linux you could solve this by doing在 Linux 中,您可以通过执行以下操作来解决此问题

cp -r node_modules/tinymce/skins skins

But if you are using Webpack , add the following to the webpack.config.js to the Encore object:但是,如果您使用的是Webpack ,请将以下内容添加到 webpack.config.js 到Encore对象:

.copyFiles({
    from: './node_modules/tinymce/skins',
    to: 'skins/[path][name].[ext]'
})

This will copy all files from the node_modules/tinymce/skins folder to the public folder 'skins', when building the resources.在构建资源时,这会将 node_modules/tinymce/skins 文件夹中的所有文件复制到公共文件夹“skins”。

With this I assume the folder node_modules/tinymce/skins exists relative to the webpack.config.js file.有了这个,我假设文件夹 node_modules/tinymce/skins 相对于 webpack.config.js 文件存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM