(function () { let initData = null; let grid = null; let currentComponent = null; let cellHeight = 60; // 生成唯一id var generateUniqueId = function () { const timestamp = Date.now().toString(36); const randomNum = Math.random().toString(36).substring(2); return timestamp + randomNum; }; /** 初始化 */ var init = function () { const initDataStorage = localStorage.getItem('initData'); if (!initDataStorage) { initData = { id: `grid_${generateUniqueId()}`, children: [], version: '1.0.0', width: 1920, height: 1080, }; } else { initData = JSON.parse(initDataStorage); conputedInitData('init'); } console.log(initData, 'initData'); // 让gridstack知道如何渲染,组件children中的content直接渲染html GridStack.renderCB = function (el, w) { el.innerHTML = w.content; }; grid = GridStack.init({ // 一行高度 cellHeight, // 间距 margin: 0, minRow: Math.floor(1080 / 2 / cellHeight), maxRow: Math.floor(1080 / 2 / cellHeight), acceptWidgets: true, float: true, removable: '#trash', // subGridOpts: subOptions, // subGridDynamic: true, children: initData.children, }); GridStack.setupDragIn('#components-panel .component-item', undefined, [ { w: 2, h: 2, type: 'image', name: '图片', image: '', eventsType: 'click', eventsAction: '', defaultFocus: false, leftId: '', rightId: '', upId: '', downId: '', }, { w: 2, h: 1, type: 'text', childrenType: '', name: '文本', background: '#fff', fontSize: '14px', color: '#333', text: '文本', fontWeight: 'normal', eventsType: 'click', eventsAction: '', defaultFocus: false, leftId: '', rightId: '', upId: '', downId: '', }, ]); console.log(grid, 'grid实例'); // grid.load(initData.children); grid.engine.nodes.forEach((item) => { handleAddComponent(item); }); grid.on('added', function (_event, itemArray) { console.log(itemArray, '这里触发了添加了added事件'); itemArray.forEach((item) => { handleAddComponent(item); }); }); grid.on('removed', function (_event, itemArray) { console.log(itemArray, '这里触发了移除removed事件'); itemArray.forEach((item) => { handleRemoveComponent(item); }); }); }; // 处理添加组件之后的操作 var handleAddComponent = (component) => { if (!component.id) { component.id = `${component.type}_${generateUniqueId()}`; } setComponentView(component); component.el.addEventListener('click', () => { console.log('点击组件', component); if (currentComponent && currentComponent.id === component.id) { return; } currentComponent = component; // 右侧显示组件属性列表 if (!$('#props-panel').find('form').is(':visible')) { $('#props-panel').find('.wait-box').hide(); $('#props-panel').find('form').show(); } // 将当前选中组件的属性显示在右侧列表中 setCurrentComponentProps(currentComponent); // 设置上下左右移动组件ID setMoveComponentId(currentComponent); }); }; // 设置上下左右移动组件ID var setMoveComponentId = function (component) { const allComponents = grid.save(); if (allComponents.length <= 1) { return; } setMoveComponentHtml('leftId', component, allComponents); setMoveComponentHtml('rightId', component, allComponents); setMoveComponentHtml('upId', component, allComponents); setMoveComponentHtml('downId', component, allComponents); }; var setMoveComponentHtml = function (idName, component, allComponents) { let componentsHtml = [{ value: '', text: '请选择' }]; allComponents .filter((item) => item.id !== component.id) .map((item) => { componentsHtml.push({ value: item.id, text: `${item.name}(${item.id})`, selected: item.id === component[idName] }); }); let el = $(`#${idName}`); el.empty(); componentsHtml.forEach((item) => { el.append( $('