2022-04-12 22:41:27 +02:00
|
|
|
const $ = require('jquery')
|
|
|
|
const GrapesJs = require('grapesjs')
|
2022-04-18 19:22:09 +02:00
|
|
|
|
|
|
|
require('grapesjs-blocks-bootstrap4').default
|
|
|
|
require('grapesjs-preset-webpage').default
|
|
|
|
require('grapesjs-preset-newsletter').default
|
2022-05-01 18:23:04 +02:00
|
|
|
require('grapesjs-plugin-header').default
|
2022-04-12 22:41:27 +02:00
|
|
|
|
|
|
|
const makeId = () => {
|
|
|
|
let result = ''
|
|
|
|
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789'
|
|
|
|
const charactersLength = characters.length
|
|
|
|
|
|
|
|
for (let i = 0; i < 20; i++) {
|
|
|
|
result += characters.charAt(Math.floor(Math.random() * charactersLength))
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'grapesjs-' + result
|
|
|
|
}
|
|
|
|
|
2022-04-18 19:22:09 +02:00
|
|
|
const modes = {
|
|
|
|
bootstrap4: {
|
|
|
|
id: 'grapesjs-blocks-bootstrap4',
|
|
|
|
options: {
|
|
|
|
blocks: {},
|
|
|
|
blockCategories: {},
|
|
|
|
labels: {},
|
|
|
|
gridDevicesPanel: true,
|
|
|
|
formPredefinedActions: [
|
|
|
|
]
|
|
|
|
},
|
|
|
|
canvas: {
|
|
|
|
styles: [
|
|
|
|
'https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css'
|
|
|
|
],
|
|
|
|
scripts: [
|
|
|
|
'https://code.jquery.com/jquery-3.5.1.slim.min.js',
|
|
|
|
'https://unpkg.com/@popperjs/core@2',
|
|
|
|
'https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js'
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
presetWebpage: {
|
|
|
|
id: 'gjs-preset-webpage',
|
|
|
|
options: {
|
|
|
|
},
|
|
|
|
canvas: {
|
|
|
|
}
|
|
|
|
},
|
|
|
|
presetNewsletter: {
|
|
|
|
id: 'gjs-preset-newsletter',
|
|
|
|
options: {
|
|
|
|
},
|
|
|
|
canvas: {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 22:41:27 +02:00
|
|
|
const doInitEditor = () => {
|
|
|
|
$('textarea[data-grapesjs]').each((i, v) => {
|
|
|
|
const textarea = $(v)
|
|
|
|
const element = textarea.parent().prev()
|
|
|
|
const id = element.attr('id') ? element.attr('id') : makeId()
|
|
|
|
|
2022-04-18 19:22:09 +02:00
|
|
|
let mode = textarea.attr('data-grapesjs')
|
|
|
|
const pluginsOpts = {}
|
|
|
|
|
|
|
|
if (!mode || typeof modes[mode] === 'undefined') {
|
|
|
|
mode = 'bootstrap4'
|
|
|
|
}
|
|
|
|
|
|
|
|
pluginsOpts[modes[mode].id] = modes[mode].options
|
2022-04-12 22:41:27 +02:00
|
|
|
element.attr('id', id)
|
|
|
|
|
|
|
|
const editor = GrapesJs.init({
|
|
|
|
container: '#' + id,
|
|
|
|
fromElement: false,
|
|
|
|
height: '900px',
|
|
|
|
width: 'auto',
|
|
|
|
storageManager: {
|
|
|
|
autoload: false
|
|
|
|
},
|
|
|
|
noticeOnUnload: 0,
|
|
|
|
showOffsets: 1,
|
|
|
|
showDevices: false,
|
2022-05-05 14:39:26 +02:00
|
|
|
plugins: [
|
|
|
|
modes[mode].id,
|
|
|
|
'grapesjs-plugin-header',
|
|
|
|
'grapesjs-component-code-editor',
|
|
|
|
'grapesjs-parser-postcss'
|
|
|
|
],
|
2022-04-12 22:41:27 +02:00
|
|
|
colorPicker: {
|
|
|
|
appendTo: 'parent',
|
|
|
|
offset: {
|
|
|
|
top: 26,
|
|
|
|
left: -166
|
|
|
|
}
|
|
|
|
},
|
2022-04-18 19:22:09 +02:00
|
|
|
pluginsOpts: pluginsOpts,
|
|
|
|
canvas: modes[mode].canvas
|
2022-04-12 22:41:27 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
const deviceManager = editor.DeviceManager
|
|
|
|
|
|
|
|
const devices = [
|
|
|
|
'Extra Small',
|
|
|
|
'Small',
|
|
|
|
'Medium',
|
|
|
|
'Large',
|
|
|
|
'Extra Large',
|
|
|
|
'Desktop',
|
|
|
|
'Tablet',
|
|
|
|
'mobileLandscape',
|
|
|
|
'mobilePortrait'
|
|
|
|
]
|
|
|
|
|
|
|
|
for (const device of devices) {
|
|
|
|
deviceManager.remove(device)
|
|
|
|
}
|
|
|
|
|
|
|
|
deviceManager.add('All', '100%')
|
|
|
|
|
|
|
|
editor.Panels.getPanels().remove('devices-buttons')
|
2022-05-05 14:39:26 +02:00
|
|
|
editor.Panels.addButton('views', {
|
|
|
|
id: 'open-code',
|
|
|
|
attributes: {
|
|
|
|
title: 'Open Code'
|
|
|
|
},
|
|
|
|
className: 'fa fa-edit',
|
|
|
|
command: 'open-code'
|
|
|
|
})
|
2022-04-12 22:41:27 +02:00
|
|
|
|
|
|
|
editor.on('update', () => {
|
|
|
|
textarea.val(JSON.stringify(editor.storeData()))
|
|
|
|
})
|
|
|
|
|
|
|
|
try {
|
|
|
|
editor.loadData(JSON.parse(textarea.val()))
|
|
|
|
} catch (e) {
|
|
|
|
editor.loadData({ html: '' })
|
|
|
|
}
|
2022-05-05 16:22:27 +02:00
|
|
|
|
|
|
|
$(element).parents('form').keypress((e) => {
|
|
|
|
if ($(e.target).parents('#' + id).length === 0) {
|
2022-05-05 16:44:05 +02:00
|
|
|
return true
|
2022-05-05 16:22:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (e.keyCode || e.which || e.charCode || 0) !== 13
|
|
|
|
})
|
2022-04-12 22:41:27 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = () => {
|
|
|
|
$(() => {
|
|
|
|
doInitEditor()
|
|
|
|
})
|
|
|
|
}
|