index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. <template>
  2. <el-container class="editor">
  3. <!-- 顶部:工具栏 -->
  4. <el-header class="editor-header">
  5. <!-- 左侧操作区 -->
  6. <slot name="toolBarLeft"></slot>
  7. <!-- 中心操作区 -->
  8. <div class="header-center flex flex-1 items-center justify-center">
  9. <span>{{ title }}</span>
  10. </div>
  11. <!-- 右侧操作区 -->
  12. <el-button-group class="header-right">
  13. <el-tooltip content="重置">
  14. <el-button @click="handleReset">
  15. <Icon icon="system-uicons:reset-alt" :size="24" />
  16. </el-button>
  17. </el-tooltip>
  18. <el-tooltip content="预览">
  19. <el-button @click="handlePreview">
  20. <Icon icon="ep:view" :size="24" />
  21. </el-button>
  22. </el-tooltip>
  23. <el-tooltip content="保存">
  24. <el-button @click="handleSave">
  25. <Icon icon="ep:check" :size="24" />
  26. </el-button>
  27. </el-tooltip>
  28. </el-button-group>
  29. </el-header>
  30. <!-- 中心区域 -->
  31. <el-container class="editor-container">
  32. <!-- 左侧:组件库 -->
  33. <ComponentLibrary ref="componentLibrary" :list="libs" v-if="libs && libs.length > 0" />
  34. <!-- 中心设计区域 -->
  35. <div class="editor-center page-prop-area" @click="handlePageSelected">
  36. <!-- 手机顶部 -->
  37. <div class="editor-design-top">
  38. <!-- 手机顶部状态栏 -->
  39. <img src="@/assets/imgs/diy/statusBar.png" alt="" class="status-bar" />
  40. <!-- 手机顶部导航栏 -->
  41. <ComponentContainer
  42. v-if="showNavigationBar"
  43. :component="navigationBarComponent"
  44. :show-toolbar="false"
  45. :active="selectedComponent?.id === navigationBarComponent.id"
  46. @click="handleNavigationBarSelected"
  47. class="cursor-pointer!"
  48. />
  49. </div>
  50. <!-- 手机页面编辑区域 -->
  51. <el-scrollbar
  52. height="100%"
  53. wrap-class="editor-design-center page-prop-area"
  54. view-class="phone-container"
  55. :view-style="{
  56. backgroundColor: pageConfigComponent.property.backgroundColor,
  57. backgroundImage: `url(${pageConfigComponent.property.backgroundImage})`
  58. }"
  59. >
  60. <draggable
  61. class="page-prop-area drag-area"
  62. v-model="pageComponents"
  63. item-key="index"
  64. :animation="200"
  65. filter=".component-toolbar"
  66. ghost-class="draggable-ghost"
  67. :force-fallback="true"
  68. group="component"
  69. @change="handleComponentChange"
  70. >
  71. <template #item="{ element, index }">
  72. <ComponentContainer
  73. :component="element"
  74. :active="selectedComponentIndex === index"
  75. :can-move-up="index > 0"
  76. :can-move-down="index < pageComponents.length - 1"
  77. @move="(direction) => handleMoveComponent(index, direction)"
  78. @copy="handleCopyComponent(index)"
  79. @delete="handleDeleteComponent(index)"
  80. @click="handleComponentSelected(element, index)"
  81. />
  82. </template>
  83. </draggable>
  84. </el-scrollbar>
  85. <!-- 手机底部导航 -->
  86. <div v-if="showTabBar" :class="['editor-design-bottom', 'component', 'cursor-pointer!']">
  87. <ComponentContainer
  88. :component="tabBarComponent"
  89. :show-toolbar="false"
  90. :active="selectedComponent?.id === tabBarComponent.id"
  91. @click="handleTabBarSelected"
  92. />
  93. </div>
  94. </div>
  95. <!-- 右侧属性面板 -->
  96. <el-aside class="editor-right" width="350px" v-if="selectedComponent?.property">
  97. <el-card
  98. shadow="never"
  99. body-class="h-[calc(100%-var(--el-card-padding)-var(--el-card-padding))]"
  100. class="h-full"
  101. >
  102. <!-- 组件名称 -->
  103. <template #header>
  104. <div class="flex items-center gap-8px">
  105. <Icon :icon="selectedComponent.icon" color="gray" />
  106. <span>{{ selectedComponent.name }}</span>
  107. </div>
  108. </template>
  109. <el-scrollbar
  110. class="m-[calc(0px-var(--el-card-padding))]"
  111. view-class="p-[var(--el-card-padding)] p-b-[calc(var(--el-card-padding)+var(--el-card-padding))] property"
  112. >
  113. <component
  114. :is="selectedComponent.id + 'Property'"
  115. v-model="selectedComponent.property"
  116. />
  117. </el-scrollbar>
  118. </el-card>
  119. </el-aside>
  120. </el-container>
  121. </el-container>
  122. </template>
  123. <script lang="ts">
  124. // 注册所有的组件
  125. import { components } from './components/mobile/index'
  126. export default {
  127. components: { ...components }
  128. }
  129. </script>
  130. <script lang="ts" setup>
  131. import draggable from 'vuedraggable'
  132. import ComponentLibrary from './components/ComponentLibrary.vue'
  133. import { cloneDeep, includes } from 'lodash-es'
  134. import { component as PAGE_CONFIG_COMPONENT } from '@/components/DiyEditor/components/mobile/PageConfig/config'
  135. import { component as NAVIGATION_BAR_COMPONENT } from './components/mobile/NavigationBar/config'
  136. import { component as TAB_BAR_COMPONENT } from './components/mobile/TabBar/config'
  137. import { isString } from '@/utils/is'
  138. import { DiyComponent, DiyComponentLibrary, PageConfig } from '@/components/DiyEditor/util'
  139. import { componentConfigs } from '@/components/DiyEditor/components/mobile'
  140. /** 页面装修详情页 */
  141. defineOptions({ name: 'DiyPageDetail' })
  142. // 消息弹窗
  143. const message = useMessage()
  144. // 左侧组件库
  145. const componentLibrary = ref()
  146. // 页面设置组件
  147. const pageConfigComponent = ref<DiyComponent<any>>(cloneDeep(PAGE_CONFIG_COMPONENT))
  148. // 顶部导航栏
  149. const navigationBarComponent = ref<DiyComponent<any>>(cloneDeep(NAVIGATION_BAR_COMPONENT))
  150. // 底部导航菜单
  151. const tabBarComponent = ref<DiyComponent<any>>(cloneDeep(TAB_BAR_COMPONENT))
  152. // 选中的组件,默认选中顶部导航栏
  153. const selectedComponent = ref<DiyComponent<any>>()
  154. // 选中的组件索引
  155. const selectedComponentIndex = ref<number>(-1)
  156. // 组件列表
  157. const pageComponents = ref<DiyComponent<any>[]>([])
  158. // 定义属性
  159. const props = defineProps<{
  160. // 页面配置,支持Json字符串
  161. modelValue: string | PageConfig
  162. // 标题
  163. title: string
  164. // 组件库
  165. libs: DiyComponentLibrary[]
  166. // 是否显示顶部导航栏
  167. showNavigationBar: boolean
  168. // 是否显示底部导航菜单
  169. showTabBar: boolean
  170. // 是否显示页面配置
  171. showPageConfig: boolean
  172. }>()
  173. // 监听传入的页面配置
  174. watch(
  175. () => props.modelValue,
  176. () => {
  177. const modelValue = isString(props.modelValue)
  178. ? (JSON.parse(props.modelValue) as PageConfig)
  179. : props.modelValue
  180. pageConfigComponent.value.property = modelValue?.page || PAGE_CONFIG_COMPONENT.property
  181. navigationBarComponent.value.property =
  182. modelValue?.navigationBar || NAVIGATION_BAR_COMPONENT.property
  183. tabBarComponent.value.property = modelValue?.tabBar || TAB_BAR_COMPONENT.property
  184. // 查找对应的页面组件
  185. pageComponents.value = (modelValue?.components || []).map((item) => {
  186. const component = componentConfigs[item.id]
  187. return { ...component, property: item.property }
  188. })
  189. },
  190. {
  191. immediate: true
  192. }
  193. )
  194. // 保存
  195. const handleSave = () => {
  196. const pageConfig = {
  197. page: pageConfigComponent.value.property,
  198. navigationBar: navigationBarComponent.value.property,
  199. tabBar: tabBarComponent.value.property,
  200. components: pageComponents.value.map((component) => {
  201. // 只保留APP有用的字段
  202. return { id: component.id, property: component.property }
  203. })
  204. } as PageConfig
  205. if (!props.showTabBar) {
  206. delete pageConfig.tabBar
  207. }
  208. // 发送数据更新通知
  209. const modelValue = isString(props.modelValue) ? JSON.stringify(pageConfig) : pageConfig
  210. emits('update:modelValue', modelValue)
  211. // 发送保存通知
  212. emits('save', pageConfig)
  213. }
  214. // 处理页面选中:显示属性表单
  215. const handlePageSelected = (event: any) => {
  216. if (!props.showPageConfig) return
  217. // 配置了样式 page-prop-area 的元素,才显示页面设置
  218. if (includes(event?.target?.classList, 'page-prop-area')) {
  219. handleComponentSelected(unref(pageConfigComponent))
  220. }
  221. }
  222. /**
  223. * 选中组件
  224. *
  225. * @param component 组件
  226. * @param index 组件的索引
  227. */
  228. const handleComponentSelected = (component: DiyComponent<any>, index: number = -1) => {
  229. selectedComponent.value = component
  230. selectedComponentIndex.value = index
  231. }
  232. // 选中顶部导航栏
  233. const handleNavigationBarSelected = () => {
  234. handleComponentSelected(unref(navigationBarComponent))
  235. }
  236. // 选中底部导航菜单
  237. const handleTabBarSelected = () => {
  238. handleComponentSelected(unref(tabBarComponent))
  239. }
  240. // 组件变动
  241. const handleComponentChange = (dragEvent: any) => {
  242. // 新增,即从组件库拖拽添加组件
  243. if (dragEvent.added) {
  244. const { element, newIndex } = dragEvent.added
  245. handleComponentSelected(element, newIndex)
  246. } else if (dragEvent.moved) {
  247. // 拖拽排序
  248. const { newIndex } = dragEvent.moved
  249. // 保持选中
  250. selectedComponentIndex.value = newIndex
  251. }
  252. }
  253. // 交换组件
  254. const swapComponent = (oldIndex: number, newIndex: number) => {
  255. ;[pageComponents.value[oldIndex], pageComponents.value[newIndex]] = [
  256. pageComponents.value[newIndex],
  257. pageComponents.value[oldIndex]
  258. ]
  259. // 保持选中
  260. selectedComponentIndex.value = newIndex
  261. }
  262. /** 移动组件 */
  263. const handleMoveComponent = (index: number, direction: number) => {
  264. const newIndex = index + direction
  265. if (newIndex < 0 || newIndex >= pageComponents.value.length) return
  266. swapComponent(index, newIndex)
  267. }
  268. /** 复制组件 */
  269. const handleCopyComponent = (index: number) => {
  270. const component = cloneDeep(pageComponents.value[index])
  271. pageComponents.value.splice(index + 1, 0, component)
  272. }
  273. /**
  274. * 删除组件
  275. * @param index 当前组件index
  276. */
  277. const handleDeleteComponent = (index: number) => {
  278. // 删除组件
  279. pageComponents.value.splice(index, 1)
  280. if (index < pageComponents.value.length) {
  281. // 1. 不是最后一个组件时,删除后选中下面的组件
  282. let bottomIndex = index
  283. handleComponentSelected(pageComponents.value[bottomIndex], bottomIndex)
  284. } else if (pageComponents.value.length > 0) {
  285. // 2. 不是第一个组件时,删除后选中上面的组件
  286. let topIndex = index - 1
  287. handleComponentSelected(pageComponents.value[topIndex], topIndex)
  288. } else {
  289. // 3. 组件全部删除之后,显示页面设置
  290. handleComponentSelected(unref(pageConfigComponent))
  291. }
  292. }
  293. // 工具栏操作
  294. const emits = defineEmits(['reset', 'preview', 'save', 'update:modelValue'])
  295. // 重置
  296. const handleReset = () => {
  297. message.warning('开发中~')
  298. emits('reset')
  299. }
  300. // 预览
  301. const handlePreview = () => {
  302. message.warning('开发中~')
  303. emits('preview')
  304. }
  305. // 设置默认选中的组件
  306. const setDefaultSelectedComponent = () => {
  307. if (props.showPageConfig) {
  308. selectedComponent.value = unref(pageConfigComponent)
  309. } else if (props.showNavigationBar) {
  310. selectedComponent.value = unref(navigationBarComponent)
  311. } else if (props.showTabBar) {
  312. selectedComponent.value = unref(tabBarComponent)
  313. }
  314. }
  315. watch(
  316. () => [props.showPageConfig, props.showNavigationBar, props.showTabBar],
  317. () => setDefaultSelectedComponent()
  318. )
  319. onMounted(() => setDefaultSelectedComponent())
  320. </script>
  321. <style lang="scss" scoped>
  322. /* 手机宽度 */
  323. $phone-width: 375px;
  324. $toolbar-height: 42px;
  325. /* 根节点样式 */
  326. .editor {
  327. height: 100%;
  328. margin: calc(0px - var(--app-content-padding));
  329. display: flex;
  330. flex-direction: column;
  331. /* 顶部:工具栏 */
  332. .editor-header {
  333. display: flex;
  334. align-items: center;
  335. justify-content: space-between;
  336. height: $toolbar-height;
  337. padding: 0;
  338. border-bottom: solid 1px var(--el-border-color);
  339. background-color: var(--el-bg-color);
  340. /* 工具栏:右侧按钮 */
  341. .header-right {
  342. height: 100%;
  343. .el-button {
  344. height: 100%;
  345. }
  346. }
  347. /* 隐藏工具栏按钮的边框 */
  348. :deep(.el-radio-button__inner),
  349. :deep(.el-button) {
  350. border-top: none !important;
  351. border-bottom: none !important;
  352. border-radius: 0 !important;
  353. }
  354. }
  355. /* 中心操作区 */
  356. .editor-container {
  357. height: calc(
  358. 100vh - var(--top-tool-height) - var(--tags-view-height) - var(--app-footer-height) -
  359. $toolbar-height
  360. );
  361. /* 右侧属性面板 */
  362. .editor-right {
  363. flex-shrink: 0;
  364. box-shadow: -8px 0 8px -8px rgba(0, 0, 0, 0.12);
  365. overflow: hidden;
  366. /* 属性面板顶部:减少内边距 */
  367. :deep(.el-card__header) {
  368. padding: 8px 16px;
  369. }
  370. /* 属性面板分组 */
  371. :deep(.property-group) {
  372. margin: 0 -20px;
  373. &.el-card {
  374. border: none;
  375. }
  376. /* 属性分组名称 */
  377. .el-card__header {
  378. border: none;
  379. background: var(--el-bg-color-page);
  380. padding: 8px 32px;
  381. }
  382. .el-card__body {
  383. border: none;
  384. }
  385. }
  386. }
  387. /* 中心区域 */
  388. .editor-center {
  389. position: relative;
  390. flex: 1 1 0;
  391. background-color: var(--app-content-bg-color);
  392. display: flex;
  393. flex-direction: column;
  394. justify-content: center;
  395. margin: 16px 0 0 0;
  396. overflow: hidden;
  397. width: 100%;
  398. /* 手机顶部 */
  399. .editor-design-top {
  400. width: $phone-width;
  401. margin: 0 auto;
  402. display: flex;
  403. flex-direction: column;
  404. /* 手机顶部状态栏 */
  405. .status-bar {
  406. height: 20px;
  407. width: $phone-width;
  408. background-color: #fff;
  409. }
  410. }
  411. /* 手机底部导航 */
  412. .editor-design-bottom {
  413. width: $phone-width;
  414. margin: 0 auto;
  415. }
  416. /* 手机页面编辑区域 */
  417. :deep(.editor-design-center) {
  418. width: 100%;
  419. /* 主体内容 */
  420. .phone-container {
  421. position: relative;
  422. background-repeat: no-repeat;
  423. background-size: 100% 100%;
  424. height: 100%;
  425. width: $phone-width;
  426. margin: 0 auto;
  427. .drag-area {
  428. height: 100%;
  429. width: 100%;
  430. }
  431. }
  432. }
  433. }
  434. }
  435. }
  436. </style>