BoxSpaceInput.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <template>
  2. <div class="_fd-box-space-input">
  3. <div class="_padding">
  4. <span class="_padding-title">
  5. {{ t('style.margin') }}
  6. </span>
  7. <input class="_fd-input _fd-top" placeholder=" " :value="boxStyle.marginTop" type="text"
  8. @blur="(e)=>setValue('margin','Top', e)" @input="(e)=>change('marginTop', e)">
  9. <input class="_fd-input _fd-right" placeholder=" " :value="boxStyle.marginRight" type="text"
  10. @blur="(e)=>setValue('margin','Right', e)" @input="(e)=>change('marginRight', e)">
  11. <input class="_fd-input _fd-bottom" placeholder=" " :value="boxStyle.marginBottom" type="text"
  12. @blur="(e)=>setValue('margin','Bottom', e)" @input="(e)=>change('marginBottom', e)">
  13. <input class="_fd-input _fd-left" placeholder=" " :value="boxStyle.marginLeft" type="text"
  14. @blur="(e)=>setValue('margin','Left', e)" @input="(e)=>change('marginLeft', e)">
  15. <div class="_fd-help">
  16. <i class="fc-icon icon-link2" title="lock" :class="marginLock ? 'active' : ''"
  17. @click="lock('margin')"></i>
  18. <i class="fc-icon icon-delete-circle" title="clear" @click="clear('margin')"></i>
  19. </div>
  20. <div class="_margin">
  21. <span class="_margin-title">
  22. {{ t('style.padding') }}
  23. </span>
  24. <div class="_fd-help">
  25. <i class="fc-icon icon-link2" title="lock" :class="paddingLock ? 'active' : ''"
  26. @click="lock('padding')"></i>
  27. <i class="fc-icon icon-delete-circle" title="clear" @click="clear('padding')"></i>
  28. </div>
  29. <input class="_fd-input _fd-top" placeholder=" " :value="boxStyle.paddingTop" type="text"
  30. @blur="(e)=>setValue('padding','Top', e)" @input="(e)=>change('paddingTop', e)">
  31. <input class="_fd-input _fd-right" placeholder=" " :value="boxStyle.paddingRight" type="text"
  32. @blur="(e)=>setValue('padding','Right', e)" @input="(e)=>change('paddingRight', e)">
  33. <input class="_fd-input _fd-bottom" placeholder=" " :value="boxStyle.paddingBottom" type="text"
  34. @blur="(e)=>setValue('padding','Bottom', e)" @input="(e)=>change('paddingBottom', e)">
  35. <input class="_fd-input _fd-left" placeholder=" " :value="boxStyle.paddingLeft" type="text"
  36. @blur="(e)=>setValue('padding','Left', e)" @input="(e)=>change('paddingLeft', e)">
  37. <div class="_box">
  38. </div>
  39. </div>
  40. </div>
  41. </div>
  42. </template>
  43. <script>
  44. import {defineComponent} from 'vue';
  45. import ConfigItem from './ConfigItem.vue';
  46. export default defineComponent({
  47. name: 'BoxSpaceInput',
  48. components: {ConfigItem},
  49. props: {
  50. modelValue: {
  51. type: Object,
  52. default: () => ({}),
  53. }
  54. },
  55. inject: ['designer'],
  56. emits: ['update:modelValue', 'change'],
  57. data() {
  58. return {
  59. position: ['Top', 'Right', 'Bottom', 'Left'],
  60. boxStyle: {
  61. margin: '',
  62. padding: '',
  63. marginLeft: '',
  64. marginRight: '',
  65. marginTop: '',
  66. marginBottom: '',
  67. paddingLeft: '',
  68. paddingRight: '',
  69. paddingTop: '',
  70. paddingBottom: '',
  71. },
  72. marginLock: false,
  73. paddingLock: false,
  74. }
  75. },
  76. watch: {
  77. modelValue() {
  78. this.tidyValue();
  79. },
  80. },
  81. computed: {
  82. t() {
  83. return this.designer.setupState.t;
  84. }
  85. },
  86. methods: {
  87. tidyValue() {
  88. this.boxStyle = {};
  89. ['margin', 'padding'].forEach(k => {
  90. this.boxStyle[k] = this.modelValue[k] || '';
  91. this.position.forEach(p => {
  92. this.boxStyle[k + p] = this.tidySize(this.modelValue[k + p] || this.modelValue[k] || '');
  93. });
  94. })
  95. },
  96. onInput() {
  97. const style = Object.keys(this.boxStyle).reduce((acc, key) => {
  98. if (this.boxStyle[key] !== '') {
  99. acc[key] = this.boxStyle[key]
  100. }
  101. return acc
  102. }, {})
  103. this.$emit('update:modelValue', style)
  104. this.$emit('change', style)
  105. },
  106. tidySize(val) {
  107. const regex = /^(\d*\.?\d+)(px|rem|%|vh|vw|em)$/
  108. if (!regex.test(val)) {
  109. if (val === 'auto') {
  110. return val;
  111. }
  112. const number = parseInt(val);
  113. if (isNaN(number)) {
  114. return '';
  115. } else {
  116. return number + 'px';
  117. }
  118. }
  119. return val;
  120. },
  121. setValue(type, key, e) {
  122. const value = this.tidySize(e.target.value);
  123. if (!type) {
  124. this.boxStyle[key] = value;
  125. } else if (this[type + 'Lock']) {
  126. this.position.forEach(v => {
  127. this.boxStyle[type + v] = value;
  128. })
  129. } else {
  130. this.boxStyle[type + key] = value;
  131. }
  132. this.onInput();
  133. },
  134. change(type, e) {
  135. this.boxStyle[type] = e.target.value;
  136. },
  137. clear(type) {
  138. this.position.forEach(v => {
  139. this.boxStyle[type + v] = '';
  140. })
  141. this.onInput();
  142. },
  143. lock(type) {
  144. const key = type + 'Lock';
  145. this[key] = !this[key];
  146. },
  147. },
  148. created() {
  149. this.tidyValue();
  150. }
  151. });
  152. </script>
  153. <style>
  154. ._fd-box-space-input {
  155. color: #000000;
  156. }
  157. ._fd-box-space-input ._padding, ._fd-box-space-input ._margin {
  158. width: 100%;
  159. height: 180px;
  160. background-color: #F2CEA5;
  161. padding: 40px 55px;
  162. box-sizing: border-box;
  163. position: relative;
  164. }
  165. html.dark ._fd-box-space-input ._padding, ._fd-box-space-input ._margin {
  166. background-color: #A9855C;
  167. }
  168. ._fd-box-space-input ._margin {
  169. width: 100%;
  170. height: 100px;
  171. background-color: #C6CF92;
  172. }
  173. ._fd-box-space-input ._fd-input {
  174. display: inline-block;
  175. width: 30%;
  176. max-width: 40px;
  177. height: 20px;
  178. border: 0 none;
  179. padding: 0;
  180. margin: 0;
  181. outline: 0 none;
  182. text-align: center;
  183. font-size: 12px;
  184. background-color: unset;
  185. text-decoration: underline;
  186. }
  187. ._fd-box-space-input ._fd-input:hover, ._fd-box-space-input ._fd-input:focus {
  188. background-color: #ECECEC;
  189. opacity: 0.9;
  190. color: #262626;
  191. }
  192. ._fd-box-space-input ._fd-left, ._fd-box-space-input ._fd-right {
  193. position: absolute;
  194. left: 7px;
  195. top: 50%;
  196. margin-top: -10px;
  197. }
  198. ._fd-box-space-input ._fd-top, ._fd-box-space-input ._fd-bottom {
  199. position: absolute;
  200. left: 50%;
  201. top: 5px;
  202. margin-left: -20px;
  203. }
  204. ._fd-box-space-input ._fd-bottom {
  205. top: unset;
  206. bottom: 15px;
  207. }
  208. ._fd-box-space-input ._fd-right {
  209. left: unset;
  210. right: 2px;
  211. }
  212. ._fd-box-space-input ._box {
  213. width: 100%;
  214. height: 100%;
  215. background-color: #94B5C0;
  216. display: flex;
  217. align-items: center;
  218. justify-content: center;
  219. }
  220. ._fd-box-space-input ._padding-title, ._fd-box-space-input ._margin-title {
  221. position: absolute;
  222. top: 2px;
  223. left: 4px;
  224. }
  225. ._fd-box-space-input ._fd-help {
  226. display: flex;
  227. align-items: center;
  228. position: absolute;
  229. right: 5px;
  230. top: 5px;
  231. color: #AAAAAA;
  232. }
  233. ._fd-box-space-input ._padding .fc-icon {
  234. cursor: pointer;
  235. color: #262626;
  236. font-size: 12px;
  237. }
  238. ._fd-box-space-input ._padding .fc-icon + .fc-icon {
  239. margin-left: 2px;
  240. }
  241. ._fd-box-space-input .fc-icon.active {
  242. color: #2E73FF;
  243. }
  244. ._fd-box-space-input ._fd-x {
  245. margin: 0 5px;
  246. }
  247. </style>