You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
7.1 KiB

2 months ago
  1. import { defineMixin } from '../vue'
  2. import { addStyle, deepMerge, addUnit, trim } from '../function/index'
  3. export const style = defineMixin({
  4. props: {
  5. // flex排列方式
  6. flexDirection: {
  7. type: String,
  8. default: ''
  9. },
  10. // flex-direction的简写
  11. fd: {
  12. type: String,
  13. default: ''
  14. },
  15. // 展示类型
  16. display: {
  17. type: String,
  18. default: ''
  19. },
  20. // display简写
  21. d: {
  22. type: String,
  23. default: ''
  24. },
  25. // 主轴排列方式
  26. justifyContent: {
  27. type: String,
  28. default: ''
  29. },
  30. // justifyContent的简写
  31. jc: {
  32. type: String,
  33. default: ''
  34. },
  35. // 纵轴排列方式
  36. alignItems: {
  37. type: String,
  38. default: ''
  39. },
  40. // align-items的简写
  41. ai: {
  42. type: String,
  43. default: ''
  44. },
  45. color: {
  46. type: String,
  47. default: ''
  48. },
  49. // color简写
  50. c: {
  51. type: String,
  52. default: ''
  53. },
  54. // 字体大小
  55. fontSize: {
  56. type: [String, Number],
  57. default: 0
  58. },
  59. // font-size简写
  60. fs: {
  61. type: [String, Number],
  62. default: ''
  63. },
  64. margin: {
  65. type: [String, Number],
  66. default: 0
  67. },
  68. // margin简写
  69. m: {
  70. type: [String, Number],
  71. default: 0
  72. },
  73. // margin-top
  74. marginTop: {
  75. type: [String, Number],
  76. default: 0
  77. },
  78. // margin-top简写
  79. mt: {
  80. type: [String, Number],
  81. default: 0
  82. },
  83. // margin-right
  84. marginRight: {
  85. type: [String, Number],
  86. default: 0
  87. },
  88. // margin-right简写
  89. mr: {
  90. type: [String, Number],
  91. default: 0
  92. },
  93. // margin-bottom
  94. marginBottom: {
  95. type: [String, Number],
  96. default: 0
  97. },
  98. // margin-bottom简写
  99. mb: {
  100. type: [String, Number],
  101. default: 0
  102. },
  103. // margin-left
  104. marginLeft: {
  105. type: [String, Number],
  106. default: 0
  107. },
  108. // margin-left简写
  109. ml: {
  110. type: [String, Number],
  111. default: 0
  112. },
  113. // padding-left
  114. paddingLeft: {
  115. type: [String, Number],
  116. default: 0
  117. },
  118. // padding-left简写
  119. pl: {
  120. type: [String, Number],
  121. default: 0
  122. },
  123. // padding-top
  124. paddingTop: {
  125. type: [String, Number],
  126. default: 0
  127. },
  128. // padding-top简写
  129. pt: {
  130. type: [String, Number],
  131. default: 0
  132. },
  133. // padding-right
  134. paddingRight: {
  135. type: [String, Number],
  136. default: 0
  137. },
  138. // padding-right简写
  139. pr: {
  140. type: [String, Number],
  141. default: 0
  142. },
  143. // padding-bottom
  144. paddingBottom: {
  145. type: [String, Number],
  146. default: 0
  147. },
  148. // padding-bottom简写
  149. pb: {
  150. type: [String, Number],
  151. default: 0
  152. },
  153. // border-radius
  154. borderRadius: {
  155. type: [String, Number],
  156. default: 0
  157. },
  158. // border-radius简写
  159. radius: {
  160. type: [String, Number],
  161. default: 0
  162. },
  163. // transform
  164. transform: {
  165. type: String,
  166. default: ''
  167. },
  168. // 定位
  169. position: {
  170. type: String,
  171. default: ''
  172. },
  173. // position简写
  174. pos: {
  175. type: String,
  176. default: ''
  177. },
  178. // 宽度
  179. width: {
  180. type: [String, Number],
  181. default: null
  182. },
  183. // width简写
  184. w: {
  185. type: [String, Number],
  186. default: null
  187. },
  188. // 高度
  189. height: {
  190. type: [String, Number],
  191. default: null
  192. },
  193. // height简写
  194. h: {
  195. type: [String, Number],
  196. default: null
  197. },
  198. top: {
  199. type: [String, Number],
  200. default: 0
  201. },
  202. right: {
  203. type: [String, Number],
  204. default: 0
  205. },
  206. bottom: {
  207. type: [String, Number],
  208. default: 0
  209. },
  210. left: {
  211. type: [String, Number],
  212. default: 0
  213. }
  214. },
  215. computed: {
  216. viewStyle() {
  217. const style = {}
  218. const addStyleTmp = addStyle(this.width || this.w)
  219. && (style.width = addStyle(this.width || this.w))(this.height || this.h)
  220. && (style.height = addStyle(this.height || this.h))(this.margin || this.m)
  221. && (style.margin = addStyle(this.margin || this.m))(this.marginTop || this.mt)
  222. && (style.marginTop = addStyle(this.marginTop || this.mt))(this.marginRight || this.mr)
  223. && (style.marginRight = addStyle(this.marginRight || this.mr))(this.marginBottom || this.mb)
  224. && (style.marginBottom = addStyle(this.marginBottom || this.mb))(this.marginLeft || this.ml)
  225. && (style.marginLeft = addStyle(this.marginLeft || this.ml))(this.padding || this.p)
  226. && (style.padding = addStyle(this.padding || this.p))(this.paddingTop || this.pt)
  227. && (style.paddingTop = addStyle(this.paddingTop || this.pt))(this.paddingRight || this.pr)
  228. && (style.paddingRight = addStyle(this.paddingRight || this.pr))(this.paddingBottom || this.pb)
  229. && (style.paddingBottom = addStyle(this.paddingBottom || this.pb))(this.paddingLeft || this.pl)
  230. && (style.paddingLeft = addStyle(this.paddingLeft || this.pl))(this.color || this.c)
  231. && (style.color = this.color || this.c)(this.fontSize || this.fs)
  232. && (style.fontSize = this.fontSize || this.fs)(this.borderRadius || this.radius)
  233. && (style.borderRadius = this.borderRadius || this.radius)(this.position || this.pos)
  234. && (this.position = this.position || this.pos)(this.flexDirection || this.fd)
  235. && (this.flexDirection = this.flexDirection || this.fd)(this.justifyContent || jc)
  236. && (this.justifyContent = this.justifyContent || jc)(this.alignItems || ai)
  237. && (this.alignItems = this.alignItems || ai)
  238. return deepMerge(style, addStyleTmp(this.customStyle))
  239. }
  240. },
  241. methods: {
  242. // 获取margin或者padding的单位,比如padding: 0 20转为padding: 0 20px
  243. getUnit(unit = '') {
  244. // 取出两端空格,分隔成数组,再对数组的每个元素添加单位,最后再合并成字符串
  245. return trim(unit).split(' ').map((item) => addUnit(item)).join(' ')
  246. }
  247. }
  248. })