grapesjs实现网页可视化编辑选择dom元素设置自定义组件重复使用效果代码

代码语言:html

所属分类:其他

代码描述:grapesjs实现网页可视化编辑选择dom元素设置自定义组件重复使用效果代码,可选择设计框中的dom元素为组件,然后可在组件库中拖拽复用组件。

代码标签: grapes 网页 可视化 编辑 选择 dom 元素 设置 自定义 组件 重复 使用

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>GrapesJS Custom Component Example</title>
    <link type="text/css" rel="stylesheet" href="//repo.bfw.wiki/bfwrepo/css/grapesjs-preset-webpage.min.css">
    <link type="text/css" rel="stylesheet" href="//repo.bfw.wiki/bfwrepo/css/grapes.min.css">
    <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/grapes.min.js"></script>

    <!--引入grapesjs-preset-webpage-->
    <link type="text/css" rel="stylesheet" href="//repo.bfw.wiki/bfwrepo/css/grapes.min.css">
    <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/grapesjs-preset-webpage.min.js"></script>
    <style>
        body, html {
            height: 100%;
            margin: 0;
        }
        #gjs {
            border: 3px solid #444;
        }
        #rightpanel{
            position: fixed;
            width: 20vw;
            right:0;
            top:0;
            z-index: 1111;
        }
    </style>
</head>
<body>
   <div id="rightpanel">
        <div id="blocks"></div>
        <div id="styles-container"></div>
    <div id="layers-container"></div>
     
   </div>
    <div id="gjs"></div>
    <script>
        document.addEventListener('DOMContentLoaded', (event) => {
            const editor = grapesjs.init({
                container: '#gjs',
                height: '100%',
                fromElement: false,
                storageManager: false,
                panels: { defaults: [] },
                blockManager: {
                    appendTo: '#blocks',
                },
                styleManager: {
                    appendTo: '#styles-container',
                },
                layerManager: {
                    appendTo: '#layers-container'
                },
            });

            editor.BlockManager.add('my-block-id', {
                label: 'Simple block',
                content: '<div class="my-block">This is a simple block</div>',
            });

            editor.Panels.addPanel({
                id: 'custom-panel',
                visible: true,
                buttons: [
                    {
                        id: 'create-component',
                        className: 'btn-create-component',
                        label: '自定义组件',
                        command(editor) {
                            const selectedComponent = editor.getSelected();
    .........完整代码请登录后点击上方下载按钮下载查看

网友评论0