Skip to content

Commit

Permalink
example of use with es5 and es6
Browse files Browse the repository at this point in the history
  • Loading branch information
lexoyo committed Dec 26, 2023
1 parent 6311fae commit 0591646
Show file tree
Hide file tree
Showing 2 changed files with 226 additions and 0 deletions.
119 changes: 119 additions & 0 deletions es5.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Symbols for GrapesJS</title>
<link href="https://unpkg.com/grapesjs/dist/css/grapes.min.css" rel="stylesheet">
<script src="https://unpkg.com/grapesjs"></script>
<link rel="stylesheet" href="https://grapesjs.com/stylesheets/grapesjs-preset-webpage.min.css">
<script src="https://unpkg.com/grapesjs-preset-webpage"></script>
<script xxdata-main="dist/es5/index.js" src="node_modules/requirejs/require.js"></script>
<script>
require.config({
baseUrl: "dist/es5/",
paths: {
"backbone": "/node_modules/backbone/backbone",
"underscore": "/node_modules/underscore/underscore",
"jquery": "/node_modules/jquery/dist/jquery",
},
});

require(['dist/es5/index.js'], function () {});
</script>
<style>
body,
html {
height: 100%;
margin: 0;
}
.gjs-editor-cont { height: 100% !important; }
</style>
</head>
<body>

<div id='basic-actions' class="panel__basic-actions" style="z-index: 9; left: 120px;"></div>
<div id="gjs">
</div>

<script type="text/javascript">
window.onload = () => {
window.editor = grapesjs.init({
container: '#gjs',
//storageManager: false,
autosave: false,
noticeOnUnload: false,
plugins: [
'grapesjs-preset-webpage',
'@silexlabs/grapesjs-symbols',
],
pluginsOpts: {
'grapesjs-preset-webpage': {},
'@silexlabs/grapesjs-symbols': {
appendTo: '.gjs-pn-views-container',
},
},
pageManager: {
pages: [{
id: 'page-1',
name: 'Page 1',
},
{
id: 'page-2',
name: 'Page 2',
},
],
}
})

var idx = 0
editor.on('load', () => {
const colors = ['red', 'blue', 'grey', 'yello', 'black', 'orange', 'chocolate', 'cadetblue', 'blueviolet']
const color = colors[Math.floor(Math.random() * colors.length)]
editor.addComponents(`<div style="min-height: 100px; background: ${color};"></div>`);

editor.Panels.addPanel({
id: 'basic-actions',
el: '.panel__basic-actions',
buttons: [
{
id: 'create-button',
label: 'Create symbol',
command(editor) {
var label = prompt('Label', 'Symbol ' + ++idx)
var icon = prompt('Icon', 'fa-list')
editor.runCommand('symbols:add', { label, icon })
},
},
{
id: 'separator',
label: '|',
},
].concat(editor.Pages.getAll().flatMap(p => ([{
id: 'button' + p.cid,
label: p.getName() || 'no name?',
command(editor) {
editor.Pages.select(p)
},
},
{
id: 'separator' + p.cid,
label: '|',
},
])))
})
//setTimeout(() => {
// const data = JSON.parse(localStorage.getItem('init'))
// if(data) editor.loadData(data)
//})
})
editor.addComponents([`
<div class="main" style="padding: 20px; background: lightblue;">
<h1 class="title">Test title</h1>
<p class="content">Test content text lorem ipsum</p>
</div>
`])
}
</script>
</body>
</html>

107 changes: 107 additions & 0 deletions es6.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Symbols for GrapesJS</title>
<link href="https://unpkg.com/grapesjs/dist/css/grapes.min.css" rel="stylesheet">
<script src="https://unpkg.com/grapesjs"></script>
<link rel="stylesheet" href="https://grapesjs.com/stylesheets/grapesjs-preset-webpage.min.css">
<script src="https://unpkg.com/grapesjs-preset-webpage"></script>
<script src="./dist/index.js"></script>
<style>
body,
html {
height: 100%;
margin: 0;
}
.gjs-editor-cont { height: 100% !important; }
</style>
</head>
<body>

<div id='basic-actions' class="panel__basic-actions" style="z-index: 9; left: 120px;"></div>
<div id="gjs">
</div>

<script type="text/javascript">
window.onload = () => {
window.editor = grapesjs.init({
container: '#gjs',
//storageManager: false,
autosave: false,
noticeOnUnload: false,
plugins: [
'grapesjs-preset-webpage',
'@silexlabs/grapesjs-symbols',
],
pluginsOpts: {
'grapesjs-preset-webpage': {},
'@silexlabs/grapesjs-symbols': {
appendTo: '.gjs-pn-views-container',
},
},
pageManager: {
pages: [{
id: 'page-1',
name: 'Page 1',
},
{
id: 'page-2',
name: 'Page 2',
},
],
}
})

var idx = 0
editor.on('load', () => {
const colors = ['red', 'blue', 'grey', 'yello', 'black', 'orange', 'chocolate', 'cadetblue', 'blueviolet']
const color = colors[Math.floor(Math.random() * colors.length)]
editor.addComponents(`<div style="min-height: 100px; background: ${color};"></div>`);

editor.Panels.addPanel({
id: 'basic-actions',
el: '.panel__basic-actions',
buttons: [
{
id: 'create-button',
label: 'Create symbol',
command(editor) {
var label = prompt('Label', 'Symbol ' + ++idx)
var icon = prompt('Icon', 'fa-list')
editor.runCommand('symbols:add', { label, icon })
},
},
{
id: 'separator',
label: '|',
},
].concat(editor.Pages.getAll().flatMap(p => ([{
id: 'button' + p.cid,
label: p.getName() || 'no name?',
command(editor) {
editor.Pages.select(p)
},
},
{
id: 'separator' + p.cid,
label: '|',
},
])))
})
//setTimeout(() => {
// const data = JSON.parse(localStorage.getItem('init'))
// if(data) editor.loadData(data)
//})
})
editor.addComponents([`
<div class="main" style="padding: 20px; background: lightblue;">
<h1 class="title">Test title</h1>
<p class="content">Test content text lorem ipsum</p>
</div>
`])
}
</script>
</body>
</html>

0 comments on commit 0591646

Please sign in to comment.