logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://anongit.hacktivis.me/git/pleroma-fe.git/

shadow_control.js (5056B)


  1. import ColorInput from 'src/components/color_input/color_input.vue'
  2. import OpacityInput from 'src/components/opacity_input/opacity_input.vue'
  3. import Select from 'src/components/select/select.vue'
  4. import SelectMotion from 'src/components/select/select_motion.vue'
  5. import Checkbox from 'src/components/checkbox/checkbox.vue'
  6. import Popover from 'src/components/popover/popover.vue'
  7. import ComponentPreview from 'src/components/component_preview/component_preview.vue'
  8. import { rgb2hex } from 'src/services/color_convert/color_convert.js'
  9. import { serializeShadow } from 'src/services/theme_data/iss_serializer.js'
  10. import { deserializeShadow } from 'src/services/theme_data/iss_deserializer.js'
  11. import { getCssShadow, getCssShadowFilter } from 'src/services/theme_data/css_utils.js'
  12. import { findShadow, findColor } from 'src/services/theme_data/theme_data_3.service.js'
  13. import { library } from '@fortawesome/fontawesome-svg-core'
  14. import { throttle, flattenDeep } from 'lodash'
  15. import {
  16. faTimes,
  17. faChevronDown,
  18. faChevronUp,
  19. faPlus
  20. } from '@fortawesome/free-solid-svg-icons'
  21. library.add(
  22. faChevronDown,
  23. faChevronUp,
  24. faTimes,
  25. faPlus
  26. )
  27. const toModel = (input) => {
  28. if (typeof input === 'object') {
  29. return {
  30. x: 0,
  31. y: 0,
  32. blur: 0,
  33. spread: 0,
  34. inset: false,
  35. color: '#000000',
  36. alpha: 1,
  37. ...input
  38. }
  39. } else if (typeof input === 'string') {
  40. return input
  41. }
  42. }
  43. export default {
  44. props: [
  45. 'modelValue',
  46. 'fallback',
  47. 'separateInset',
  48. 'noPreview',
  49. 'disabled',
  50. 'staticVars',
  51. 'compact'
  52. ],
  53. emits: ['update:modelValue', 'subShadowSelected'],
  54. data () {
  55. return {
  56. selectedId: 0,
  57. invalid: false
  58. }
  59. },
  60. components: {
  61. ColorInput,
  62. OpacityInput,
  63. Select,
  64. SelectMotion,
  65. Checkbox,
  66. Popover,
  67. ComponentPreview
  68. },
  69. computed: {
  70. cValue: {
  71. get () {
  72. return (this.modelValue ?? this.fallback ?? []).map(toModel)
  73. },
  74. set (newVal) {
  75. this.$emit('update:modelValue', newVal)
  76. }
  77. },
  78. selectedType: {
  79. get () {
  80. return typeof this.selected
  81. },
  82. set (newType) {
  83. this.selected = toModel(newType === 'object' ? {} : '')
  84. }
  85. },
  86. selected: {
  87. get () {
  88. const selected = this.cValue[this.selectedId]
  89. if (selected && typeof selected === 'object') {
  90. return { ...selected }
  91. } else if (typeof selected === 'string') {
  92. return selected
  93. }
  94. return null
  95. },
  96. set (value) {
  97. this.cValue[this.selectedId] = toModel(value)
  98. this.$emit('update:modelValue', this.cValue)
  99. }
  100. },
  101. present () {
  102. return this.selected != null && this.modelValue != null
  103. },
  104. shadowsAreNull () {
  105. return this.modelValue == null
  106. },
  107. currentFallback () {
  108. return this.fallback?.[this.selectedId]
  109. },
  110. getColorFallback () {
  111. if (this.staticVars && this.selected?.color) {
  112. try {
  113. const computedColor = findColor(this.selected.color, { dynamicVars: {}, staticVars: this.staticVars }, true)
  114. if (computedColor) return rgb2hex(computedColor)
  115. return null
  116. } catch (e) {
  117. console.warn(e)
  118. return null
  119. }
  120. } else {
  121. return this.currentFallback?.color
  122. }
  123. },
  124. style () {
  125. try {
  126. let result
  127. const serialized = this.cValue.map(x => serializeShadow(x)).join(',')
  128. serialized.split(/,/).map(deserializeShadow) // validate
  129. const expandedShadow = flattenDeep(findShadow(this.cValue, { dynamicVars: {}, staticVars: this.staticVars }))
  130. const fixedShadows = expandedShadow.map(x => ({ ...x, color: console.log(x) || rgb2hex(x.color) }))
  131. if (this.separateInset) {
  132. result = {
  133. filter: getCssShadowFilter(fixedShadows),
  134. boxShadow: getCssShadow(fixedShadows, true)
  135. }
  136. } else {
  137. result = {
  138. boxShadow: getCssShadow(fixedShadows)
  139. }
  140. }
  141. this.invalid = false
  142. return result
  143. } catch (e) {
  144. console.error('Invalid shadow', e)
  145. this.invalid = true
  146. }
  147. }
  148. },
  149. watch: {
  150. selected (value) {
  151. this.$emit('subShadowSelected', this.selectedId)
  152. }
  153. },
  154. methods: {
  155. getNewSubshadow () {
  156. return toModel(this.selected)
  157. },
  158. onSelectChange (id) {
  159. this.selectedId = id
  160. },
  161. getSubshadowLabel (shadow, index) {
  162. if (typeof shadow === 'object') {
  163. return shadow?.name ?? this.$t('settings.style.shadows.shadow_id', { value: index })
  164. } else if (typeof shadow === 'string') {
  165. return shadow || this.$t('settings.style.shadows.empty_expression')
  166. }
  167. },
  168. updateProperty: throttle(function (prop, value) {
  169. this.cValue[this.selectedId][prop] = value
  170. if (prop === 'inset' && value === false && this.separateInset) {
  171. this.cValue[this.selectedId].spread = 0
  172. }
  173. this.$emit('update:modelValue', this.cValue)
  174. }, 100)
  175. }
  176. }