-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
73 lines (66 loc) · 1.82 KB
/
main.ts
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
61
62
63
64
65
66
67
68
69
70
71
72
73
import grapesjs, { Editor } from 'grapesjs'
import type { ContentPreview } from './preview'
function example(editor: Editor) {
editor.Components.addType('example', {
model: {
defaults: {
script() {
const containerType = window.self === window.top ? 'top frame' : 'an iframe'
console.log('Initializing in', containerType, this)
},
},
//
// This ensures that the example is always rendered with the `id` attribute
// If there are no component instance styles this component will not have
// the `id` attribute even though it has the `script`.
//
// This is probably a bug in GrapesJS - need to report it!
//
// toHTML() {
// return `<div id="${this.getId()}">${this.getInnerHTML()}</div>`
// }
},
})
editor.Blocks.add('example', {
label: 'Example',
activate: true,
content: {
type: 'example',
components: '<h2>Hello, World!</h2>',
},
})
}
function preview(editor: Editor) {
// Skip the `<body>` element when rendering wrapper
editor.Components.addType('wrapper', {
model: {
toHTML() {
return this.getInnerHTML()
},
},
})
// When loading and saving update the preview
editor.on('load storage:end:store', () => {
const contentPreview = document.querySelector('content-preview') as ContentPreview
if (contentPreview) {
contentPreview.html = editor.getHtml()
contentPreview.css = editor.getCss()
contentPreview.js = editor.getJs()
}
})
}
function openBlocksByDefault(editor: Editor) {
editor.on('load', () => {
editor.Panels.getButton('views', 'open-blocks').set('active', true)
})
}
grapesjs.init({
container: '#gjs',
height: '50vh',
jsInHtml: false,
plugins: [
example,
preview,
openBlocksByDefault,
],
})