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.pswp.cn/news/393022.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/393022.shtml
英文地址,請注明出處:http://en.pswp.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 …