index.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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 :size="24" icon="system-uicons:reset-alt" />
  16. </el-button>
  17. </el-tooltip>
  18. <el-tooltip v-if="previewUrl" content="预览">
  19. <el-button @click="handlePreview">
  20. <Icon :size="24" icon="ep:view" />
  21. </el-button>
  22. </el-tooltip>
  23. <el-tooltip content="保存">
  24. <el-button @click="handleSave">
  25. <Icon :size="24" icon="ep:check" />
  26. </el-button>
  27. </el-tooltip>
  28. </el-button-group>
  29. </el-header>
  30. <!-- 中心区域 -->
  31. <el-container class="editor-container">
  32. <!-- 左侧:组件库(ComponentLibrary) -->
  33. <ComponentLibrary v-if="libs && libs.length > 0" ref="componentLibrary" :list="libs" />
  34. <!-- 中心:设计区域(ComponentContainer) -->
  35. <div class="editor-center page-prop-area" @click="handlePageSelected">
  36. <!-- 手机顶部 -->
  37. <div class="editor-design-top">
  38. <!-- 手机顶部状态栏 -->
  39. <img alt="" class="status-bar" src="@/assets/imgs/diy/statusBar.png" />
  40. <!-- 手机顶部导航栏 -->
  41. <ComponentContainer
  42. v-if="showNavigationBar"
  43. :active="selectedComponent?.id === navigationBarComponent.id"
  44. :component="navigationBarComponent"
  45. :show-toolbar="false"
  46. class="cursor-pointer!"
  47. @click="handleNavigationBarSelected"
  48. />
  49. </div>
  50. <!-- 绝对定位的组件:例如 弹窗、浮动按钮等 -->
  51. <div
  52. v-for="(component, index) in pageComponents"
  53. :key="index"
  54. @click="handleComponentSelected(component, index)"
  55. >
  56. <component
  57. :is="component.id"
  58. v-if="component.position === 'fixed' && selectedComponent?.uid === component.uid"
  59. :property="component.property"
  60. />
  61. </div>
  62. <!-- 手机页面编辑区域 -->
  63. <el-scrollbar
  64. :view-style="{
  65. backgroundColor: pageConfigComponent.property.backgroundColor,
  66. backgroundImage: `url(${pageConfigComponent.property.backgroundImage})`
  67. }"
  68. height="100%"
  69. view-class="phone-container"
  70. wrap-class="editor-design-center page-prop-area"
  71. >
  72. <draggable
  73. v-model="pageComponents"
  74. :animation="200"
  75. :force-fallback="true"
  76. class="page-prop-area drag-area"
  77. filter=".component-toolbar"
  78. ghost-class="draggable-ghost"
  79. group="component"
  80. item-key="index"
  81. @change="handleComponentChange"
  82. >
  83. <template #item="{ element, index }">
  84. <ComponentContainer
  85. v-if="!element.position || element.position === 'center'"
  86. :active="selectedComponentIndex === index"
  87. :can-move-down="index < pageComponents.length - 1"
  88. :can-move-up="index > 0"
  89. :component="element"
  90. @click="handleComponentSelected(element, index)"
  91. @copy="handleCopyComponent(index)"
  92. @delete="handleDeleteComponent(index)"
  93. @move="(direction) => handleMoveComponent(index, direction)"
  94. />
  95. </template>
  96. </draggable>
  97. </el-scrollbar>
  98. <!-- 手机底部导航 -->
  99. <div v-if="showTabBar" :class="['editor-design-bottom', 'component', 'cursor-pointer!']">
  100. <ComponentContainer
  101. :active="selectedComponent?.id === tabBarComponent.id"
  102. :component="tabBarComponent"
  103. :show-toolbar="false"
  104. @click="handleTabBarSelected"
  105. />
  106. </div>
  107. <!-- 固定布局的组件 操作按钮区 -->
  108. <div class="fixed-component-action-group">
  109. <el-tag
  110. v-if="showPageConfig"
  111. :effect="selectedComponent?.uid === pageConfigComponent.uid ? 'dark' : 'plain'"
  112. :type="selectedComponent?.uid === pageConfigComponent.uid ? 'primary' : 'info'"
  113. size="large"
  114. @click="handleComponentSelected(pageConfigComponent)"
  115. >
  116. <Icon :icon="pageConfigComponent.icon" :size="12" />
  117. <span>{{ pageConfigComponent.name }}</span>
  118. </el-tag>
  119. <template v-for="(component, index) in pageComponents" :key="index">
  120. <el-tag
  121. v-if="component.position === 'fixed'"
  122. :effect="selectedComponent?.uid === component.uid ? 'dark' : 'plain'"
  123. :type="selectedComponent?.uid === component.uid ? 'primary' : 'info'"
  124. closable
  125. size="large"
  126. @click="handleComponentSelected(component)"
  127. @close="handleDeleteComponent(index)"
  128. >
  129. <Icon :icon="component.icon" :size="12" />
  130. <span>{{ component.name }}</span>
  131. </el-tag>
  132. </template>
  133. </div>
  134. </div>
  135. <!-- 右侧:属性面板(ComponentContainerProperty) -->
  136. <el-aside v-if="selectedComponent?.property" class="editor-right" width="350px">
  137. <el-card
  138. body-class="h-[calc(100%-var(--el-card-padding)-var(--el-card-padding))]"
  139. class="h-full"
  140. shadow="never"
  141. >
  142. <!-- 组件名称 -->
  143. <template #header>
  144. <div class="flex items-center gap-8px">
  145. <Icon :icon="selectedComponent?.icon" color="gray" />
  146. <span>{{ selectedComponent?.name }}</span>
  147. </div>
  148. </template>
  149. <el-scrollbar
  150. class="m-[calc(0px-var(--el-card-padding))]"
  151. view-class="p-[var(--el-card-padding)] p-b-[calc(var(--el-card-padding)+var(--el-card-padding))] property"
  152. >
  153. <component
  154. :is="selectedComponent?.id + 'Property'"
  155. :key="selectedComponent?.uid || selectedComponent?.id"
  156. v-model="selectedComponent.property"
  157. />
  158. </el-scrollbar>
  159. </el-card>
  160. </el-aside>
  161. </el-container>
  162. </el-container>
  163. <!-- 预览弹框 -->
  164. <Dialog v-model="previewDialogVisible" title="预览" width="700">
  165. <div class="flex justify-around">
  166. <IFrame
  167. :src="previewUrl"
  168. class="w-375px border-4px border-rounded-8px border-solid p-2px h-667px!"
  169. />
  170. <div class="flex flex-col">
  171. <el-text>手机扫码预览</el-text>
  172. <Qrcode :text="previewUrl" logo="/logo.gif" />
  173. </div>
  174. </div>
  175. </Dialog>
  176. </template>
  177. <script lang="ts">
  178. // 注册所有的组件
  179. import { components } from './components/mobile/index'
  180. export default {
  181. components: { ...components }
  182. }
  183. </script>
  184. <script lang="ts" setup>
  185. import draggable from 'vuedraggable'
  186. import ComponentLibrary from './components/ComponentLibrary.vue'
  187. import { cloneDeep, includes } from 'lodash-es'
  188. import { component as PAGE_CONFIG_COMPONENT } from '@/components/DiyEditor/components/mobile/PageConfig/config'
  189. import { component as NAVIGATION_BAR_COMPONENT } from './components/mobile/NavigationBar/config'
  190. import { component as TAB_BAR_COMPONENT } from './components/mobile/TabBar/config'
  191. import { isEmpty, isString } from '@/utils/is'
  192. import { DiyComponent, DiyComponentLibrary, PageConfig } from '@/components/DiyEditor/util'
  193. import { componentConfigs } from '@/components/DiyEditor/components/mobile'
  194. import { array, oneOfType } from 'vue-types'
  195. import { propTypes } from '@/utils/propTypes'
  196. /** 页面装修详情页 */
  197. defineOptions({ name: 'DiyPageDetail' })
  198. // 左侧组件库
  199. const componentLibrary = ref()
  200. // 页面设置组件
  201. const pageConfigComponent = ref<DiyComponent<any>>(cloneDeep(PAGE_CONFIG_COMPONENT))
  202. // 顶部导航栏
  203. const navigationBarComponent = ref<DiyComponent<any>>(cloneDeep(NAVIGATION_BAR_COMPONENT))
  204. // 底部导航菜单
  205. const tabBarComponent = ref<DiyComponent<any>>(cloneDeep(TAB_BAR_COMPONENT))
  206. // 选中的组件,默认选中顶部导航栏
  207. const selectedComponent = ref<DiyComponent<any>>()
  208. // 选中的组件索引
  209. const selectedComponentIndex = ref<number>(-1)
  210. // 组件列表
  211. const pageComponents = ref<DiyComponent<any>[]>([])
  212. // 定义属性
  213. const props = defineProps({
  214. // 页面配置,支持Json字符串
  215. modelValue: oneOfType<string | PageConfig>([String, Object]).isRequired,
  216. // 标题
  217. title: propTypes.string.def(''),
  218. // 组件库
  219. libs: array<DiyComponentLibrary>(),
  220. // 是否显示顶部导航栏
  221. showNavigationBar: propTypes.bool.def(true),
  222. // 是否显示底部导航菜单
  223. showTabBar: propTypes.bool.def(false),
  224. // 是否显示页面配置
  225. showPageConfig: propTypes.bool.def(true),
  226. // 预览地址:提供了预览地址,才会显示预览按钮
  227. previewUrl: propTypes.string.def('')
  228. })
  229. // 监听传入的页面配置
  230. // 解析出 pageConfigComponent 页面整体的配置,navigationBarComponent、pageComponents、tabBarComponent 页面上、中、下的配置
  231. watch(
  232. () => props.modelValue,
  233. () => {
  234. const modelValue =
  235. isString(props.modelValue) && !isEmpty(props.modelValue)
  236. ? (JSON.parse(props.modelValue) as PageConfig)
  237. : props.modelValue
  238. pageConfigComponent.value.property =
  239. (typeof modelValue !== 'string' && modelValue?.page) || PAGE_CONFIG_COMPONENT.property
  240. navigationBarComponent.value.property =
  241. (typeof modelValue !== 'string' && modelValue?.navigationBar) ||
  242. NAVIGATION_BAR_COMPONENT.property
  243. tabBarComponent.value.property =
  244. (typeof modelValue !== 'string' && modelValue?.tabBar) || TAB_BAR_COMPONENT.property
  245. // 查找对应的页面组件
  246. pageComponents.value = ((typeof modelValue !== 'string' && modelValue?.components) || []).map(
  247. (item) => {
  248. const component = componentConfigs[item.id]
  249. return { ...component, property: item.property }
  250. }
  251. )
  252. },
  253. {
  254. immediate: true
  255. }
  256. )
  257. /** 选择组件修改其属性后更新它的配置 */
  258. watch(
  259. selectedComponent,
  260. (val: any) => {
  261. if (!val || selectedComponentIndex.value === -1) {
  262. return
  263. }
  264. pageComponents.value[selectedComponentIndex.value] = selectedComponent.value!
  265. },
  266. { deep: true }
  267. )
  268. // 保存
  269. const handleSave = () => {
  270. // 发送保存通知
  271. emits('save')
  272. }
  273. // 监听配置修改
  274. const pageConfigChange = () => {
  275. const pageConfig = {
  276. page: pageConfigComponent.value.property,
  277. navigationBar: navigationBarComponent.value.property,
  278. tabBar: tabBarComponent.value.property,
  279. components: pageComponents.value.map((component) => {
  280. // 只保留APP有用的字段
  281. return { id: component.id, property: component.property }
  282. })
  283. } as PageConfig
  284. if (!props.showTabBar) {
  285. delete pageConfig.tabBar
  286. }
  287. // 发送数据更新通知
  288. const modelValue = isString(props.modelValue) ? JSON.stringify(pageConfig) : pageConfig
  289. emits('update:modelValue', modelValue)
  290. }
  291. watch(
  292. () => [
  293. pageConfigComponent.value.property,
  294. navigationBarComponent.value.property,
  295. tabBarComponent.value.property,
  296. pageComponents.value
  297. ],
  298. () => {
  299. pageConfigChange()
  300. },
  301. { deep: true }
  302. )
  303. // 处理页面选中:显示属性表单
  304. const handlePageSelected = (event: any) => {
  305. if (!props.showPageConfig) return
  306. // 配置了样式 page-prop-area 的元素,才显示页面设置
  307. if (includes(event?.target?.classList, 'page-prop-area')) {
  308. handleComponentSelected(unref(pageConfigComponent))
  309. }
  310. }
  311. /**
  312. * 选中组件
  313. *
  314. * @param component 组件
  315. * @param index 组件的索引
  316. */
  317. const handleComponentSelected = (component: DiyComponent<any>, index: number = -1) => {
  318. selectedComponent.value = component
  319. selectedComponentIndex.value = index
  320. }
  321. // 选中顶部导航栏
  322. const handleNavigationBarSelected = () => {
  323. handleComponentSelected(unref(navigationBarComponent))
  324. }
  325. // 选中底部导航菜单
  326. const handleTabBarSelected = () => {
  327. handleComponentSelected(unref(tabBarComponent))
  328. }
  329. // 组件变动(拖拽)
  330. const handleComponentChange = (dragEvent: any) => {
  331. // 新增,即从组件库拖拽添加组件
  332. if (dragEvent.added) {
  333. const { element, newIndex } = dragEvent.added
  334. handleComponentSelected(element, newIndex)
  335. } else if (dragEvent.moved) {
  336. // 拖拽排序
  337. const { newIndex } = dragEvent.moved
  338. // 保持选中
  339. selectedComponentIndex.value = newIndex
  340. }
  341. }
  342. // 交换组件
  343. const swapComponent = (oldIndex: number, newIndex: number) => {
  344. ;[pageComponents.value[oldIndex], pageComponents.value[newIndex]] = [
  345. pageComponents.value[newIndex],
  346. pageComponents.value[oldIndex]
  347. ]
  348. // 保持选中
  349. selectedComponentIndex.value = newIndex
  350. }
  351. /** 移动组件(上移、下移) */
  352. const handleMoveComponent = (index: number, direction: number) => {
  353. const newIndex = index + direction
  354. if (newIndex < 0 || newIndex >= pageComponents.value.length) return
  355. swapComponent(index, newIndex)
  356. }
  357. /** 复制组件 */
  358. const handleCopyComponent = (index: number) => {
  359. const component = cloneDeep(pageComponents.value[index])
  360. component.uid = new Date().getTime()
  361. pageComponents.value.splice(index + 1, 0, component)
  362. }
  363. /**
  364. * 删除组件
  365. * @param index 当前组件index
  366. */
  367. const handleDeleteComponent = (index: number) => {
  368. // 删除组件
  369. pageComponents.value.splice(index, 1)
  370. if (index < pageComponents.value.length) {
  371. // 1. 不是最后一个组件时,删除后选中下面的组件
  372. let bottomIndex = index
  373. handleComponentSelected(pageComponents.value[bottomIndex], bottomIndex)
  374. } else if (pageComponents.value.length > 0) {
  375. // 2. 不是第一个组件时,删除后选中上面的组件
  376. let topIndex = index - 1
  377. handleComponentSelected(pageComponents.value[topIndex], topIndex)
  378. } else {
  379. // 3. 组件全部删除之后,显示页面设置
  380. handleComponentSelected(unref(pageConfigComponent))
  381. }
  382. }
  383. // 工具栏操作
  384. const emits = defineEmits(['reset', 'preview', 'save', 'update:modelValue'])
  385. // 注入无感刷新页面函数
  386. const reload = inject<() => void>('reload')
  387. // 重置
  388. const handleReset = () => {
  389. if (reload) reload()
  390. emits('reset')
  391. }
  392. // 预览
  393. const previewDialogVisible = ref(false)
  394. const handlePreview = () => {
  395. previewDialogVisible.value = true
  396. emits('preview')
  397. }
  398. // 设置默认选中的组件
  399. const setDefaultSelectedComponent = () => {
  400. if (props.showPageConfig) {
  401. selectedComponent.value = unref(pageConfigComponent)
  402. } else if (props.showNavigationBar) {
  403. selectedComponent.value = unref(navigationBarComponent)
  404. } else if (props.showTabBar) {
  405. selectedComponent.value = unref(tabBarComponent)
  406. }
  407. }
  408. watch(
  409. () => [props.showPageConfig, props.showNavigationBar, props.showTabBar],
  410. () => setDefaultSelectedComponent()
  411. )
  412. onMounted(() => setDefaultSelectedComponent())
  413. </script>
  414. <style lang="scss" scoped>
  415. /* 手机宽度 */
  416. $phone-width: 375px;
  417. $toolbar-height: 42px;
  418. /* 根节点样式 */
  419. .editor {
  420. display: flex;
  421. height: 100%;
  422. margin: calc(0px - var(--app-content-padding));
  423. flex-direction: column;
  424. /* 顶部:工具栏 */
  425. .editor-header {
  426. display: flex;
  427. height: $toolbar-height;
  428. padding: 0;
  429. background-color: var(--el-bg-color);
  430. border-bottom: solid 1px var(--el-border-color);
  431. align-items: center;
  432. justify-content: space-between;
  433. /* 工具栏:右侧按钮 */
  434. .header-right {
  435. height: 100%;
  436. .el-button {
  437. height: 100%;
  438. }
  439. }
  440. /* 隐藏工具栏按钮的边框 */
  441. :deep(.el-radio-button__inner),
  442. :deep(.el-button) {
  443. border-top: none !important;
  444. border-bottom: none !important;
  445. border-radius: 0 !important;
  446. }
  447. }
  448. /* 中心操作区 */
  449. .editor-container {
  450. height: calc(
  451. 100vh - var(--top-tool-height) - var(--tags-view-height) - var(--app-footer-height) -
  452. $toolbar-height
  453. );
  454. /* 右侧属性面板 */
  455. .editor-right {
  456. overflow: hidden;
  457. box-shadow: -8px 0 8px -8px rgb(0 0 0 / 12%);
  458. flex-shrink: 0;
  459. /* 属性面板顶部:减少内边距 */
  460. :deep(.el-card__header) {
  461. padding: 8px 16px;
  462. }
  463. /* 属性面板分组 */
  464. :deep(.property-group) {
  465. margin: 0 -20px;
  466. &.el-card {
  467. border: none;
  468. }
  469. /* 属性分组名称 */
  470. .el-card__header {
  471. padding: 8px 32px;
  472. background: var(--el-bg-color-page);
  473. border: none;
  474. }
  475. .el-card__body {
  476. border: none;
  477. }
  478. }
  479. }
  480. /* 中心区域 */
  481. .editor-center {
  482. position: relative;
  483. display: flex;
  484. width: 100%;
  485. margin: 16px 0 0;
  486. overflow: hidden;
  487. background-color: var(--app-content-bg-color);
  488. flex: 1 1 0;
  489. flex-direction: column;
  490. justify-content: center;
  491. /* 手机顶部 */
  492. .editor-design-top {
  493. display: flex;
  494. width: $phone-width;
  495. margin: 0 auto;
  496. flex-direction: column;
  497. /* 手机顶部状态栏 */
  498. .status-bar {
  499. width: $phone-width;
  500. height: 20px;
  501. background-color: #fff;
  502. }
  503. }
  504. /* 手机底部导航 */
  505. .editor-design-bottom {
  506. width: $phone-width;
  507. margin: 0 auto;
  508. }
  509. /* 手机页面编辑区域 */
  510. :deep(.editor-design-center) {
  511. width: 100%;
  512. /* 主体内容 */
  513. .phone-container {
  514. position: relative;
  515. width: $phone-width;
  516. height: 100%;
  517. margin: 0 auto;
  518. background-repeat: no-repeat;
  519. background-size: 100% 100%;
  520. .drag-area {
  521. width: 100%;
  522. height: 100%;
  523. }
  524. }
  525. }
  526. /* 固定布局的组件 操作按钮区 */
  527. .fixed-component-action-group {
  528. position: absolute;
  529. top: 0;
  530. right: 16px;
  531. display: flex;
  532. flex-direction: column;
  533. gap: 8px;
  534. :deep(.el-tag) {
  535. box-shadow: 0 2px 8px 0 rgba(0, 0, 0, 0.1);
  536. border: none;
  537. .el-tag__content {
  538. width: 100%;
  539. display: flex;
  540. align-items: center;
  541. justify-content: flex-start;
  542. .el-icon {
  543. margin-right: 4px;
  544. }
  545. }
  546. }
  547. }
  548. }
  549. }
  550. }
  551. </style>