-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample.ts
87 lines (68 loc) · 2.21 KB
/
example.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// This file is only used by Vite during development.
// It is ignored when the files are built.
// It is an example, a demonstration.
import 'grapesjs/dist/css/grapes.min.css'
import grapesjs, { usePlugin } from 'grapesjs'
import grapesjsBlocks from 'grapesjs-blocks-basic'
import grapesjsFloat from './plugin'
import { showFloatingCommand, hideFloatingCommand } from './utils/constant'
import type { Component } from 'grapesjs'
import type { CommandOptions } from './types'
function capitalizeValue (value?: string) {
if (typeof value !== 'string') {
return ''
}
return (
value.charAt(0).toUpperCase() +
value.replace(/[_-]+/, ' ').slice(1)
)
}
function runExample () {
const editor = grapesjs.init({
container: '#editor', // same as id in the "index.html" file
height: '100vh',
fromElement: true,
storageManager: false,
plugins: [
usePlugin(grapesjsBlocks, {
flexGrid: true
}),
usePlugin(grapesjsFloat)
]
})
const floatingEl = document.getElementById('floating-element')!
editor.once('load', () => {
console.log('Editor loaded', editor)
})
// Show the floating element around the selected component.
editor.on('component:selected', (selectedComponent: Component) => {
const { type: componentType, name: componentName = '' } = selectedComponent.props()
const selectedEl = selectedComponent.getEl()
if (!selectedEl) {
return
}
const commandOptions: CommandOptions = {
referenceElement: selectedEl,
floatingElement: floatingEl,
isDebugging: true
}
editor.runCommand(showFloatingCommand, commandOptions)
const label = capitalizeValue(componentType) || componentName
floatingEl.textContent = `${label} (styles, settings, ...)`
})
// Hide the floating element.
editor.on('component:deselected', (deselectedComponent: Component) => {
const deselectedEl = deselectedComponent.getEl()
if (!deselectedEl) {
return
}
const commandOptions: CommandOptions = {
referenceElement: deselectedEl,
floatingElement: floatingEl,
isDebugging: true
}
editor.runCommand(hideFloatingCommand, commandOptions)
floatingEl.textContent = ''
})
}
runExample()