拖拽的形式生成页面的工具

要解决的问题

从实现原理来说,其实需要解决的就是以下几个问题:

需要有哪些可以编辑的元素?

文本、图片、形状、音频、链接等,二期以后会逐步增加更多的可编辑元素。

如何编辑元素?

通过点击或者上传的形式新增,通过拖拽来调整大小尺寸及位置,通过编辑面板来修改样式。同时,不同的元素将拥有不同的编辑面板,如文字类型,可以修改字体、颜色、大小、对齐方式等,而图片类型,则可以进行缩放、裁剪、圆角、阴影等调整。

如何编辑和预览动画效果?

动画效果将模仿其他产品,合并至编辑面板,并通过点击图标的形式,更换不同的入场动画,更换的同时,触发本动画的实际效果预览。另外也可以点击独立的预览按钮,可以对已经编辑完毕的页面进行预览。

如何实现与后台的数据交互?

按页和页内元素组合成一个json对象,附带音频信息传递至后台数据接口,读取时同样处理。

如何将数据转换成手机端网页(所谓H5页面)?

借助vue的createElement方法,将json 逐一解析成对应的组件,渲染即可。
使用slider插件实现上下或者左右翻页。

如何实现兼容手机端网页?

目前市面上,手动开发这类型网页,一般有两种兼容方式,即固定尺寸兼容及百分比兼容,我称之为主动兼容和被动兼容,区别主要是在与元素css的尺寸计算方式以及viewport的写法,这类型文章网上已经有不少,这里就不再敷述。而在本项目当中,最终选择的是两者相结合的方式来实现,原因在之后的文章中会提及。

核心原理解析

将 JSON 转换成 H5

相信阅读过 Vue JSX 文档 的同学对下面的代码应该不会陌生

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

// 以下代码来自:https://cn.vuejs.org/v2/guide/render-function.html#createElement-参数

// @returns {VNode}
createElement(
// {String | Object | Function}
// An HTML tag name, component options, or async
// function resolving to one of these. Required.
'div',

// {Object}
// A data object corresponding to the attributes
// you would use in a template. Optional.
{
// (see details in the next section below)
},

// {String | Array}
// Children VNodes, built using `createElement()`,
// or using strings to get 'text VNodes'. Optional.
[
'Some text comes first.',
createElement('h1', 'A headline'),
createElement(MyComponent, {
props: {
someProp: 'foobar'
}
})
]
)

抽象抽象再抽象

我们对上面的 demo 进行进一步的抽象:

1、移除注释
2、把 createElement(tagName || componentOptions, {data}, children) 对应到上面的代码中,把 children 部分单独抽象成一个数组
3、整理之后的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

// a component options demo:
const MyComponent = {
props:['someProp'],
render(h) {
return h('span', this.someProp)
},
}

new Vue({
el: '#app',
// 这里的 render(createElement) 我们更常见的写法是:render(h)
// 关于这部分的解释,可以参见: https://segmentfault.com/q/1010000007130348?_ea=17466196
render(createElement) {
const pageJSON = [
'Some text comes first.',
createElement('h1', 'A headline'),
createElement(MyComponent /** component options */, {
props: {
someProp: 'foobar'
}
})
]
return h('div', {}, pageJSON)
}
})


// 再抽象一步
new Vue({
el: '#app',
render(h) {
const pageJSON = [
{component: 'span', text: 'Some text comes first.'},
{component: 'h1', text: 'A headline'},
{component: 'MyComponent', data: {props: {someProp: 'foobar'}} }
]
return h('div', {}, pageJSON.map(ele => {
return h(ele.component, ele.text ? ele.text : ele.data)
}))
}
})


// 再抽象一步(哎,不对啊,作者,我咋感觉你这一步啥都没做啊😂,你说对了)
const PageJSON = [
{component: 'span', text: 'Some text comes first.'},
{component: 'h1', text: 'A headline'},
{component: 'MyComponent', data: {props: {someProp: 'foobar'}} }
]
new Vue({
el: '#app',
render(h) {
return h('div', {}, pageJSON.map(ele => {
return h(ele.component, ele.text ? ele.text : ele.data)
}))
}
})

// 再抽象一步
const WorkJSON = {
title: '我是作品标题',
description: '我是作品描述',
created_time: '2019-09-01',
updated_time: '2019-09-01',
pages: [
elements: [
{component: 'span', text: 'Some text comes first.'},
{component: 'h1', text: 'A headline'},
{component: 'MyComponent', data: {props: {someProp: 'foobar'}} }
],
],
}
new Vue({
el: '#app',
render(h) {
return h('div', {}, WorkJSON.pages[0].elements.map(ele => {
return h(ele.component, ele.text ? ele.text : ele.data)
}))
}
})


相信到最后以这一步大家应该有些头绪了吧(要没有的话 😂😂😂,接着往下看吧)
其实鲁班H5的一个作品其实是一个就是一个大JSON(结构和上面的 WorkJSON 几乎一致)

这个大JSON 里面包含了很多页面,每个页面里面包含了很多元素
最终这个JSON 会传给 render(h) 进行解析渲染
到这里,也就能解答这一小节的问题了:

当前网速较慢或者你使用的浏览器不支持博客特定功能,请尝试刷新或换用Chrome、Firefox等现代浏览器