feat: 拖拽布局-支持存储到本地,刷新后重新加载

master
LCJ-MinYa 3 weeks ago
parent 04ecb079b7
commit 50912b1bb3

@ -50,7 +50,8 @@ html {
justify-content: center; justify-content: center;
overflow: hidden; overflow: hidden;
} }
.component-item .grid-stack-item-content { .component-item .grid-stack-item-content,
.grid-stack-item .grid-stack-item-content {
width: 100%; width: 100%;
height: 100%; height: 100%;
display: flex; display: flex;
@ -68,13 +69,15 @@ html {
border: 1px solid #e0e0e0; border: 1px solid #e0e0e0;
} }
.component-item i { .component-item i,
.grid-stack-item i {
font-size: 18px; font-size: 18px;
margin-bottom: 5px; margin-bottom: 5px;
color: #007bff; color: #007bff;
} }
.component-item span { .component-item span,
.grid-stack-item span {
font-size: 14px; font-size: 14px;
} }
.action-btn { .action-btn {

@ -1,4 +1,5 @@
(function () { (function () {
let initData = null;
let grid = null; let grid = null;
let currentComponent = null; let currentComponent = null;
@ -11,6 +12,21 @@
/** 初始化 */ /** 初始化 */
var init = function () { var init = function () {
const initDataStorage = localStorage.getItem('initData');
if (!initDataStorage) {
initData = {
id: `grid_${generateUniqueId()}`,
children: [],
};
} else {
initData = JSON.parse(initDataStorage);
}
console.log(initData, 'initData');
// 让gridstack知道如何渲染组件children中的content直接渲染html
GridStack.renderCB = function (el, w) {
el.innerHTML = w.content;
};
grid = GridStack.init({ grid = GridStack.init({
// 一行高度 // 一行高度
cellHeight: 80, cellHeight: 80,
@ -20,6 +36,7 @@
acceptWidgets: true, acceptWidgets: true,
float: true, float: true,
removable: '#trash', removable: '#trash',
children: initData.children,
}); });
GridStack.setupDragIn('#components-panel .component-item', undefined, [ GridStack.setupDragIn('#components-panel .component-item', undefined, [
{ {
@ -38,28 +55,7 @@
]); ]);
console.log(grid, 'grid实例'); console.log(grid, 'grid实例');
var initData = [ // grid.load(initData.children);
// {
// x: 0,
// y: 0,
// w: 2,
// h: 2,
// content: 'item 1',
// data: {
// name: 'item 1',
// },
// },
// {
// x: 2,
// y: 3,
// w: 3,
// content: 'item 2',
// data: {
// name: 'item 2',
// },
// },
];
// grid.load(initData);
grid.engine.nodes.forEach((item) => { grid.engine.nodes.forEach((item) => {
handleAddComponent(item); handleAddComponent(item);
@ -82,7 +78,9 @@
// 处理添加组件之后的操作 // 处理添加组件之后的操作
var handleAddComponent = (component) => { var handleAddComponent = (component) => {
if (!component.id) {
component.id = `${component.type}_${generateUniqueId()}`; component.id = `${component.type}_${generateUniqueId()}`;
}
setComponentView(component); setComponentView(component);
component.el.addEventListener('click', () => { component.el.addEventListener('click', () => {
console.log('点击组件', component); console.log('点击组件', component);
@ -192,6 +190,8 @@
// grid.addWidget({ w: 2, content: 'item 1' }); // grid.addWidget({ w: 2, content: 'item 1' });
console.log(grid.save()); console.log(grid.save());
console.log(grid); console.log(grid);
initData.children = grid.save();
localStorage.setItem('initData', JSON.stringify(initData));
}); });
/** 执行方法 */ /** 执行方法 */

Loading…
Cancel
Save