Last active
April 13, 2024 07:04
-
-
Save earlgreyxxx/d788b1f35ca88aaed0d8aff2e5c60d60 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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