Skip to content

Instantly share code, notes, and snippets.

@earlgreyxxx
Last active April 13, 2024 07:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save earlgreyxxx/d788b1f35ca88aaed0d8aff2e5c60d60 to your computer and use it in GitHub Desktop.
Save earlgreyxxx/d788b1f35ca88aaed0d8aff2e5c60d60 to your computer and use it in GitHub Desktop.
// jquery
import $ from 'jquery/dist/jquery.slim';
// 静的アセット(画像、CSSファイルなど)
import 'bootstrap/dist/css/bootstrap.css';
import './style.css';
import 'trumbowyg/dist/ui/trumbowyg.css';
// jQueryプラグイン関係のjsファイルは 末尾に ?url を付けてアセットとしてインポートさせ、
// 後から動的にscriptタグで注入する。
import trumbowygURL from 'trumbowyg/dist/trumbowyg.min.js?url';
import langURL from 'trumbowyg/dist/langs/ja.min.js?url';
// プラグインで使用する画像パスを取得
import svgPath from 'trumbowyg/dist/ui/icons.svg';
// windowにjQueryを登録
window.jQuery = $;
// jQueryプラグイン(trumbowyg)のロード
(() => {
const params = {
lang: 'ja',
svgPath
};
scriptLoader(trumbowygURL)
.then(() => scriptLoader(langURL))
.then(() => $('#editor').trumbowyg(params));
})();
// jsファイルを <script>タグを使って読み込む
// Promise対応版
function scriptLoader(url)
{
return new Promise((resolve,reject) => {
try {
const oScript = document.createElement('script');
oScript.addEventListener('load', ev => resolve());
oScript.setAttribute('src',url);
document.body.appendChild(oScript);
} catch(e) {
reject(e);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment