forked from Ju99ernaut/grapesjs-tailwind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (52 loc) · 1.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import en from './locale/en';
import loadBlocks from './blocks';
import loadCommands from './commands';
export default (editor, opts = {}) => {
const options = {
...{
i18n: {},
// default options
blocks: false,
tailwindPlayCdn: 'https://cdn.tailwindcss.com',
plugins: [],
config: {},
changeThemeText: 'Change Theme',
openCategory: 'Blog',
}, ...opts
};
if (options.blocks) {
// Add blocks
loadBlocks(editor, options);
}
// Add commands
loadCommands(editor, options);
// Load i18n files
editor.I18n && editor.I18n.addMessages({
en,
...options.i18n,
});
const appendTailwindCss = async (frame) => {
const iframe = frame.view.getEl();
if (!iframe) return;
const { tailwindPlayCdn, plugins, config } = options;
const init = () => {
iframe.contentWindow.tailwind.config = config;
}
const script = document.createElement('script');
script.src = tailwindPlayCdn + (plugins.length ? `?plugins=${plugins.join()}` : '');
script.onload = init;
const cssStyle = document.createElement('style');
// checks iframe is ready before loading Tailwind CSS - issue with firefox
const f = setInterval(() => {
const doc = iframe.contentDocument;
if (doc && doc.readyState && doc.readyState === 'complete') {
doc.head.appendChild(script);
doc.head.appendChild(cssStyle);
clearInterval(f);
}
}, 100)
}
editor.Canvas.getModel()['on']('change:frames', (m, frames) => {
frames.forEach(frame => frame.once('loaded', () => appendTailwindCss(frame)));
});
};