index.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import type { Page } from '@/types/page'
  2. import type { Screen } from '@/types/screen'
  3. import type { ScreenConfig } from '@/types/appMeta'
  4. import type { Bin } from '@/types/bins'
  5. import type { Resource } from '@/types/resource'
  6. import type { IComponentModelConfig } from '@/lvgl-widgets/type'
  7. import { v4 } from 'uuid'
  8. import { klona } from 'klona'
  9. import { BaseWidget } from '@/types/baseWidget'
  10. import { getUUID } from '@/utils'
  11. import { DEFAULT_THEME_KEY } from '@/constants'
  12. import LvglWidgets from '@/lvgl-widgets'
  13. import { bfsWalk } from 'simple-mind-map/src/utils'
  14. const withDefaultTheme = (styles: any[] = []) => {
  15. return klona(styles).map((item: any) => ({
  16. ...item,
  17. theme: item?.theme || DEFAULT_THEME_KEY
  18. }))
  19. }
  20. /**
  21. * 创建屏幕
  22. * @param config 屏幕配置
  23. * @returns Screen
  24. */
  25. export const createScreen = (config: ScreenConfig): Screen => {
  26. return {
  27. // ID
  28. id: v4(),
  29. // 名称
  30. name: 'screen',
  31. // 类型
  32. type: 'screen',
  33. // 屏幕宽
  34. width: config.width,
  35. // 屏幕高
  36. height: config.height,
  37. // 颜色格式
  38. colorFormat: config.colorFormat,
  39. // 颜色深度
  40. colorDepth: config.colorDepth,
  41. // 屏幕方向
  42. screenDirection: config.screenDirection,
  43. // 页面
  44. pages: [createPage()]
  45. }
  46. }
  47. /**
  48. * 创建页面
  49. */
  50. export const createPage = (): Page => {
  51. return {
  52. // 页面ID
  53. id: v4(),
  54. // 页面名称
  55. name: 'new_page',
  56. // 类型
  57. type: 'page',
  58. // 隐藏
  59. hidden: false,
  60. // 锁定
  61. locked: false,
  62. // 参考线
  63. referenceLine: [],
  64. // 属性
  65. props: klona(LvglWidgets['page'].defaultSchema.props || {}),
  66. // 样式
  67. style: withDefaultTheme(LvglWidgets['page'].defaultSchema.styles || []),
  68. // 事件
  69. events: [],
  70. // 页面变量
  71. variables: [],
  72. // 子组件
  73. children: []
  74. }
  75. }
  76. /**
  77. * 创建BIN
  78. * @param index BIN索引
  79. */
  80. export const createBin = (index: number): Bin => {
  81. return {
  82. // BIN索引
  83. id: v4(),
  84. // BIN名称
  85. name: `bin_${index}`,
  86. // BIN文件
  87. path: ''
  88. }
  89. }
  90. /**
  91. * 创建文件资源
  92. * @param path 文件路径
  93. * @param type 类型
  94. */
  95. export const createFileResource = (path: string, type: 'image' | 'font' | 'other'): Resource => {
  96. const fileName = path.split('\\').pop() || ''
  97. switch (type) {
  98. // 图片资源
  99. case 'image': {
  100. return {
  101. id: v4(),
  102. fileName,
  103. fileType: fileName.split('.').pop() || '',
  104. path,
  105. compressFormat: 'none',
  106. colorFormat: 'RGB565A8',
  107. // alphaColor: '#FFFFFF00',
  108. // alpha: 255,
  109. bin: '',
  110. ditheringAlgorithm: 'None',
  111. storageMode: 'BIN'
  112. }
  113. }
  114. // 字体资源
  115. case 'font': {
  116. return {
  117. id: v4(),
  118. fileName,
  119. fileType: fileName.split('.').pop() || '',
  120. path,
  121. range: ['all'],
  122. codeRange: [],
  123. extText: '',
  124. bin: '',
  125. renderConfig: {
  126. bitDepth: 4,
  127. antiAlias: 'normal',
  128. hinting: 'normal'
  129. },
  130. compress: {
  131. type: 'none'
  132. }
  133. }
  134. }
  135. default: {
  136. // 其他资源
  137. return {
  138. id: v4(),
  139. fileName,
  140. fileType: fileName.split('.').pop() || '',
  141. path,
  142. bin: ''
  143. }
  144. }
  145. }
  146. }
  147. /**
  148. * 创建函数
  149. */
  150. export const createMethod = () => {
  151. return {
  152. id: v4(),
  153. name: 'func_name' + '_' + getUUID(6, 'number'),
  154. action: ''
  155. }
  156. }
  157. /**
  158. * 创建控件数据
  159. * @param schema 组件模型
  160. */
  161. export const createWidget = (
  162. schema: IComponentModelConfig | BaseWidget,
  163. index: number,
  164. isCustom?: boolean
  165. ): BaseWidget => {
  166. // 自定义组件
  167. if (isCustom) {
  168. const newComp = klona(schema as BaseWidget)
  169. bfsWalk(newComp, (child) => {
  170. child.id = v4()
  171. })
  172. // todo:处理关联性问题
  173. newComp.id = v4()
  174. return newComp as BaseWidget
  175. }
  176. const { defaultSchema, hasChildren, parts } = schema
  177. const componentSchema: BaseWidget = {
  178. id: v4(),
  179. name: defaultSchema?.name + '_' + index,
  180. type: schema.key,
  181. isCopy: false,
  182. copyFrom: '',
  183. hidden: false,
  184. locked: false,
  185. part: parts?.[0]?.name,
  186. state: parts?.[0]?.stateList?.[0],
  187. props: klona(defaultSchema.props || {}),
  188. style: withDefaultTheme(defaultSchema.styles || []),
  189. events: []
  190. }
  191. if (hasChildren) {
  192. componentSchema.children = []
  193. }
  194. if (defaultSchema?.children) {
  195. componentSchema.children = klona(defaultSchema.children)
  196. }
  197. return componentSchema
  198. }