webpack设置应用缓存_如何使用Webpack在Rails应用程序中设置TinyMCE

webpack设置应用缓存

by Joanna Gaudyn

乔安娜·高登(Joanna Gaudyn)

如何使用Webpack在Rails应用程序中设置TinyMCE (How to setup TinyMCE in your Rails app using Webpack)

The popularity of using Webpack to deal with your assets in Rails is steadily increasing. Getting started is really straightforward. If you are starting a new app, you simply run rails new my_app --webpack and Rails takes care of the rest.

在Rails中使用Webpack处理资产的流行度正在稳步提高。 入门非常简单。 如果您要启动一个新应用程序,则只需运行rails new my_app --webpack ,Rails就会处理其余的工作。

Thanks to the webpacker gem, adding Webpack to your existing application is also pretty uncomplicated. You add the gem to your Gemfile, bundle, and finally install webpacker:

由于使用了webpacker gem ,将Webpack添加到现有应用程序中也相当简单。 您将gem添加到您的Gemfile中,进行捆绑,最后安装webpacker:

gem 'webpacker', '~> 3.5'bundlebundle exec rails webpacker:install

This is pretty sweet. Now all you need to do is link your JavaScript pack and the CSS imported in it into the head of your application.html.haml:

这真是太好了。 现在,您需要做的就是将您JavaScript包和其中导入CSS链接到application.html.haml的头部:

<%= javascript_pack_tag 'application' %> <!-- js from app/javascript/packs/application.js -->
<%= stylesheet_pack_tag 'application' %> <!-- CSS imported via Wbpack -->

Once this is done, you are ready to write your modern JavaScript code and make use of all the great libraries out there.

完成此操作后,您就可以编写现代JavaScript代码,并充分利用所有出色的库。

什么是tinyMCE? (What is tinyMCE?)

TinyMCE is a rich text editor in the cloud. To put it simply, it’s like Word that can be implemented into your app.

TinyMCE是云中的富文本编辑器。 简而言之,就像可以在您的应用程序中实现的Word一样。

The project I am working on uses it to let admins edit the content of the front page. Thanks to TinyMCE, it isn’t necessary to build a separate admin interface for that purpose. But the editor’s usage can be much more versatile. Think, for example, of what Wordpress or Evernote allows you to do thanks to their build in tools.

我正在研究的项目使用它来让管理员编辑首页的内容。 感谢TinyMCE,无需为此目的构建单独的管理界面。 但是编辑器的用法可以更加通用。 例如,考虑一下Wordpress或Evernote借助其内置的工具可以做什么。

通过CDN使用 (Usage via CDN)

We originally implemented the editor via CDN (e.g. linking the script in the head of our application.html.haml) like this:

我们最初是通过CDN实现编辑器的(例如,将脚本链接到application.html.haml的开头),如下所示:

!!!%html  %head    %meta ... <!-- some meta content -->    %title ... <!-- MyApp -->    = csrf_meta_tags
%script{src: 'https://cloud.tinymce.com/stable/tinymce.min.js?apiKey=gexncjni90zx3qf0m5rr7kl8l40wd5yuly2xjza0g3kwrljt'}    = stylesheet_link_tag 'application', media: 'all'    = javascript_include_tag 'application'  %body    <!-- the usual body stuff -->

This required adding a file with our customized function in app/assets/javascript/tinyMce.js. Note that we are also using jQuery.

这需要在app/assets/javascript/tinyMce.js添加具有我们自定义功能的文件。 请注意,我们也使用jQuery。

$( document ).on('turbolinks:load', function() {
tinyMCE.init({         selector: 'textarea.tinymce',             // some other settings, like height, language,         // order of buttons on your toolbar etc.             plugins: [            'table', 'lists' // whatever plugins you want to add        ]    });});

In addition to that, we had to include a translation file (which is not necessary if you’re using English in your app). For everything to display correctly in production, you’ll also need to get a free Tiny Cloud API key .

除此之外,我们还必须包含一个翻译文件 (如果您在应用程序中使用英语,则不需要此文件 )。 为了使所有内容在生产中正确显示,您还需要获取免费的Tiny Cloud API密钥 。

Webpack和tinyMCE (Webpack and tinyMCE)

Everything was working great for a couple of months, but we decided that it was time for the transition towards Webpack.

几个月来一切都很好,但是我们认为是时候过渡到Webpack了。

Webpack is supposed to make your life easier and, coupled with yarn, lets you focus on the important stuff. Say you want to use package A. It so happens, that package A relies on packages B and C. And package B depends on D, E and F. Rather than spending hours figuring out what the dependencies are and installing them all individually, what you want is to say yarn add package-A, and have it figured out for you. And this is almost the case.

Webpack可以使您的生活更轻松,并且与毛线结合使用,可以使您专注于重要的事情。 假设您要使用程序包A。碰巧的是,程序包A依赖于程序包B和C。而程序包B取决于D,E和F。与其花费大量时间来弄清它们之间的依赖关系,然后分别安装它们,您要说的是yarn add package-A ,并为您解决。 几乎是这种情况。

This transition when it came to tinyMCE was way more painful than it should have been. And that’s why I decided to write this post. I hope it saves someone some time and frustration.

当涉及到tinyMCE时,这种过渡比原本应该痛苦得多。 这就是为什么我决定写这篇文章的原因。 我希望它可以节省一些时间和挫败感。

If you previously had tinyMCE implemented via CDN, you’d like to get rid of some stuff, to start clean. Remove the script link from application.html.haml. Then comment out the content of the tinyMce.js file (and the language file if you’re using one). I also decided to get rid of the jQuery dependency (in our case it meant removing gem 'jquery-rails' from the Gemfile, and in the app/assets/javascripts/application.js removing //= require jquery and replacing //= require jquery_ujs with //= require rails-ujs).

如果您以前通过CDN实现tinyMCE ,则想摆脱一些东西,开始清理。 从application.html.haml删除脚本链接。 然后注释掉tinyMce.js文件(如果使用的是语言文件)的内容。 我还决定摆脱jQuery依赖关系(在我们的例子中,这意味着从Gemfile中删除gem'jquery gem 'jquery-rails' ,并在app/assets/javascripts/application.js删除//= require jquery并替换//= require jquery_ujs//= require rails-ujs )。

Note: Proceed with caution if you have more external libraries in your project that rely on jQuery (e.g. Bootstrap or Select2). Ultimately your goal would probably be to move all of them to Webpack, but the bigger the project, the more complex that task could be, so just bear it in mind. Nothing stops you from keeping your traditional implementation parallel with the Webpack one. In that case I would still recommend commenting it out for the time of tinyMCE implementation.

注意:如果项目中有更多依赖jQuery的外部库(例如Bootstrap或Select2),请谨慎操作。 最终,您的目标可能是将所有这些都移至Webpack,但是项目越大,任务可能越复杂,因此请记住这一点。 没有什么可以阻止您使传统实现与Webpack保持并行。 在那种情况下,我仍然建议在tinyMCE实施时将其注释掉。

All these steps will ensure that things we’ll be implementing from now on work, and the old implementation doesn’t function as a fallback.

所有这些步骤将确保我们从现在开始将要执行的事情,并且旧的实现不会充当后备功能。

第1步。如果您想通过webpack使用jQuery (Step 1. If you want to use jQuery via webpack)

Adding jQuery through Webpack is as simple as running yarn add jquery and adding the following code to the config/webpack/environment.js:

通过Webpack添加jQuery就像运行yarn add jquery并将以下代码添加到config/webpack/environment.js

const { environment } = require('@rails/webpacker')const webpack = require('webpack')environment.plugins.prepend('Provide',  new webpack.ProvidePlugin({    $: 'jquery',    jQuery: 'jquery'  }))module.exports = environment

第2步。获取tinyMCE软件包 (Step 2. Get the tinyMCE package)

That is also pretty straightforward: run yarn add tinymce.

这也非常简单:运行yarn add tinymce

Then create a new file where we’ll place our function. I ended up with app/javascript/vendor/tinyMce.js with the following content:

然后创建一个新文件,在其中放置函数。 我最终得到了app/javascript/vendor/tinyMce.js ,内容如下:

import tinymce from 'tinymce/tinymce';import 'tinymce/themes/modern/theme';import 'tinymce/plugins/table';import 'tinymce/plugins/lists';
function tinyMce() {    tinymce.init({        selector: 'textarea.tinymce',
// some other settings, like height, language,         // order of buttons on your toolbar etc.
plugins: [            'table', 'lists'        ]    });}
// if you're using a language file, you can place its content here
export { tinyMce };

步骤3.将所有内容导入到application.js (Step 3. Import everything to the application.js)

We can import our function like so:

我们可以这样导入我们的函数:

import { tinyMce } from "../vendor/tinyMce";

import { tinyMce } from "../vendor/tinyMce";

and then call it:

然后调用它:

document.addEventListener(“turbolinks:load”, function () {    tinyMce(); });

As you can see, we also replaced the jQuery code with ES6.

如您所见,我们还用ES6替换了jQuery代码。

步骤4.使tinyMCE皮肤正常工作 (Step 4. Get the tinyMCE skin to work)

If you run your webpack-dev-server and rails s you might be surprised to see that your text editor is not there. One look in the console and you’ll see the following error:

如果您运行webpack-dev-serverrails s ,可能会惊讶地发现文本编辑器不存在。 在控制台中一看,您将看到以下错误:

This is because tinyMCE will not work without a skin, and pulling it in through Webpack requires its explicit import. We can do this by including this line in our tinyMce.js file, right after the other import statements. The path is relative to the node_modules folder:

这是因为tinyMCE没有皮肤就无法工作,并且通过Webpack引入它需要其显式导入。 为此,我们可以在其他import语句之后tinyMce.jstinyMce.js文件中。 该路径是相对于node_modules文件夹的:

import ‘tinymce/skins/lightgray/skin.min.css’;

At this point you should have your editor working.

此时,您应该使编辑器正常工作。

But… if you look into the console, you might be disappointed to see that you are still getting 2 errors:

但是……如果您查看控制台,可能会感到失望,仍然看到2个错误:

Don’t despair! There is an easy fix. Just add skin: false to your function tinyMce() config and it should do the trick. This will prevent the default files from loading.

别失望! 有一个简单的解决方法。 只需添加skin: falsefunction tinyMce()配置中,它就可以解决问题。 这将防止加载默认文件。

Congrats! You just got your tinyMCE up and running!

恭喜! 您只需启动并运行tinyMCE!

翻译自: https://www.freecodecamp.org/news/how-to-setup-tinymce-in-your-rails-app-using-webpack-edf030915332/

webpack设置应用缓存

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/393022.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

springmvc ajax 页面无法重定向问题!!!!

诶诶诶。这个问题困扰了我一天&#xff0c;百度了很多都不行。 刚实战ssm框架&#xff0c;做登录跳转的时候&#xff0c;我是用ajax提交数据到后台&#xff0c;然后后天返回数据进前台&#xff0c;前台再给用户一些比较友好的提示&#xff0c;比如用户名或密码错误之类的。 所以…

linux svn log 乱码,解决p42svn中文log乱码的问题

现象&#xff1a;将perforce代码库迁移至SVN时log乱码。p42svn.pl在windows下运行至"-|"时会报错&#xff0c;于是安装了linux虚拟机&#xff0c;从虚拟linux中运行p42svn.pl生成dump文件&#xff0c;再传至windows下用svnadmin load。可是在svn查看log时&#xff0…

Django开发中常用的命令总结

1. 创建一个Django Project#使用下面的命令可以创建一个projectdjango-admin.py startproject mysite #创建好之后可以看到如下的pro... 1. 创建一个Django Project 1 2 3 4 5 6 7 8 9 10 11 #使用下面的命令可以创建一个project django-admin.py startproject mysite #创…

xml解析-jaxp添加结点

jaxp添加结点 eg&#xff1a; //在第一个下面添加nv / 1.创建解析器工厂 * 2.根据解析器工厂创建解析器 * 3.解析xml返回document * * 4.得到第一个p1 * -得到所有p1使用item方法得到第一个p1 * * 5.创建sex标签 createElement * 6.创建文本 createTextNode * 7.把文本添加到se…

leetcode107. 二叉树的层次遍历 II

给定一个二叉树&#xff0c;返回其节点值自底向上的层次遍历。 &#xff08;即按从叶子节点所在层到根节点所在的层&#xff0c;逐层从左向右遍历&#xff09;例如&#xff1a; 给定二叉树 [3,9,20,null,null,15,7],3/ \9 20/ \15 7 返回其自底向上的层次遍历为&#xff1a…

javascript 图表_JavaScript 2018年的三个有争议的图表

javascript 图表by Sacha Greif由Sacha Greif JavaScript 2018年的三个有争议的图表 (Three Controversial Charts From the State of JavaScript 2018) 您认为统计数据和图表很无聊吗&#xff1f; 再想一想… (You thought stats and graphs were boring? Think again…) “…

签入在服务器上之后,别人获取了,在解决方案资源管理器中找不到。

签入在服务器上之后&#xff0c;别人获取了&#xff0c;在解决方案资源管理器中找不到。 这个问题具体原因我也不太清楚&#xff0c;但是我找到了一个解决方案。直接在解决方案上右键&#xff0c;添加&#xff0c;添加现有项。把在解决方案资源管理器上看不见的选中&#xff0c…

03JavaScript程序设计修炼之道-2019-06-20_20-31-49

## DomDocument object model 文档对象模型Dom树html|head body| |meta title div|ul|li li li在js世界中&#xff0c;把dom树的每个元素都看成一个对象&#xff0c;对象就有属性和方法dom学什么 dom节点操作 查找元素 元素增删改查 样式操作 事件绑定等## 事件三要素 1 事件源…

linux 独占 cpu,宋宝华:谈一谈Linux让实时 高性能任务独占CPU的事

本文主要讨论在高实时要求、高效能计算、DPDK等领域&#xff0c;Linux如何让某一个线程排他性独占CPU&#xff1b;独占CPU涉及的线程、中断隔离原理&#xff1b;以及如何在排他性独占的状况下&#xff0c;甚至让系统的timer tick也不打断独占任务&#xff0c;从而实现最低的延迟…

leetcode347. 前 K 个高频元素(排序)

给定一个非空的整数数组&#xff0c;返回其中出现频率前 k 高的元素。 示例 1: 输入: nums [1,1,1,2,2,3], k 2 输出: [1,2] 示例 2: 输入: nums [1], k 1 输出: [1] 代码 class Solution {public int[] topKFrequent(int[] nums, int k) {Map<Integer,Integer>…

如何在React中从其父组件更改子组件的状态

by Johny Thomas约翰尼托马斯(Johny Thomas) 如何在React中从其父组件更改子组件的状态 (How to change the state of a child component from its parent in React) We will be building a simple React app which shows the real name of a superhero on a button click.我们…

vue-property-decorator 提供 OO 的风格 Vue Component 方便类型声明

Prop 父子组件之间传值 Install: npm install --save vue-property-decoratorChild: <template><div>{{fullMessage}}</div> </template><script lang"ts">import Vue from vue import {Component, Prop} from vue-property-decorato…

python学习笔记(1)

变量的命名 变量名只能包含字母、数字、下划线&#xff0c;不能以数字打头不要用Python关键字、函数名、保留用于特殊用途的单词作变量名变量名应短且有描述性慎用小写l和大写O字符串 就是一系列字符 在Python中&#xff0c;用引号扩起的都是字符串&#xff0c;引号可以是单引号…

使用这些HTTP标头保护您的Web应用程序

by Alex Nadalin通过亚历克斯纳达林 使用这些HTTP标头保护您的Web应用程序 (Secure your web application with these HTTP headers) This is part 3 of a series on web security: part 2 was “Web Security: an introduction to HTTP”这是有关Web安全的系列文章的第3部分&…

leetcode547. 朋友圈(并查集)

班上有 N 名学生。其中有些人是朋友&#xff0c;有些则不是。他们的友谊具有是传递性。如果已知 A 是 B 的朋友&#xff0c;B 是 C 的朋友&#xff0c;那么我们可以认为 A 也是 C 的朋友。所谓的朋友圈&#xff0c;是指所有朋友的集合。 给定一个 N * N 的矩阵 M&#xff0c;表…

linux ssh Unused,安装openssh-portable时遇到的问题及解决办法

问题1&#xff1a;configure: error: Your OpenSSL headers do not match yourlibrary. Check config.log for details.If you are sure your installation is consistent, you can disable the checkby running “./configure –without-openssl-header-check”.Also see cont…

windows 删除删除不掉的文件

DEL /F /A /Q \\?\%1RD /S /Q \\?\%1 windows下删除删除不掉的文件&#xff1a; 1、打开记事本&#xff0c;把上面的命令复制进去 2、保存&#xff0c;后缀名改为.bat&#xff0c;ok 3、把想要删除的文件托放到这个文件的图标上 转载于:https://www.cnblogs.com/Mike_Chang/p…

云计算技术的跃进睿云智合专业先进水平

对于未来的云计算数据中心&#xff0c;网络虚拟化方案需要适应计算和存储虚拟化的浪潮&#xff0c;快速的实现云计算业务的发放&#xff0c;以及能够满足动态的应用程序工作负载的需求;同时需要帮助管理员更简单的管理物理网络和虚拟网络&#xff0c;实现网络可视化。睿云智合&…

CSS 选择器权重计算规则

CSS 选择器&#xff08;Selector&#xff09;的权重&#xff08;Specificity&#xff09;决定了对于同一元素&#xff0c;到底哪一条 CSS 规则会生效。且仅有当多条 CSS 规则都对同一元素声明了相应样式时&#xff0c;才会涉及到权重计算的问题。 选择器的分类 正式计算选择器权…

本地构建和自动化构建_如何构建最强大,最安全的家庭自动化系统

本地构建和自动化构建by Amir Off由Amir Off 如何构建最强大&#xff0c;最安全的家庭自动化系统 (How to build the most robust and secure home automation system) In this article, I’ll discuss how I built a Smart Home Automation System with Angular and Node.js …