logo

pleroma-fe

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

virtual_directives_tab.js (3903B)


  1. import { ref, computed, watch, inject } from 'vue'
  2. import Select from 'src/components/select/select.vue'
  3. import SelectMotion from 'src/components/select/select_motion.vue'
  4. import ShadowControl from 'src/components/shadow_control/shadow_control.vue'
  5. import ColorInput from 'src/components/color_input/color_input.vue'
  6. import { serializeShadow } from 'src/services/theme_data/iss_serializer.js'
  7. // helper for debugging
  8. // eslint-disable-next-line no-unused-vars
  9. const toValue = (x) => JSON.parse(JSON.stringify(x === undefined ? 'null' : x))
  10. export default {
  11. components: {
  12. Select,
  13. SelectMotion,
  14. ShadowControl,
  15. ColorInput
  16. },
  17. props: ['modelValue'],
  18. emits: ['update:modelValue'],
  19. setup (props, context) {
  20. const exports = {}
  21. const emit = context.emit
  22. exports.emit = emit
  23. exports.computeColor = inject('computeColor')
  24. exports.staticVars = inject('staticVars')
  25. const selectedVirtualDirectiveId = ref(0)
  26. exports.selectedVirtualDirectiveId = selectedVirtualDirectiveId
  27. const selectedVirtualDirective = computed({
  28. get () {
  29. return props.modelValue[selectedVirtualDirectiveId.value]
  30. },
  31. set (value) {
  32. const newVD = [...props.modelValue]
  33. newVD[selectedVirtualDirectiveId.value] = value
  34. emit('update:modelValue', newVD)
  35. }
  36. })
  37. exports.selectedVirtualDirective = selectedVirtualDirective
  38. exports.selectedVirtualDirectiveValType = computed({
  39. get () {
  40. return props.modelValue[selectedVirtualDirectiveId.value].valType
  41. },
  42. set (value) {
  43. const newValType = value
  44. let newValue
  45. switch (value) {
  46. case 'shadow':
  47. newValue = '0 0 0 #000000 / 1'
  48. break
  49. case 'color':
  50. newValue = '#000000'
  51. break
  52. default:
  53. newValue = 'none'
  54. }
  55. const newName = props.modelValue[selectedVirtualDirectiveId.value].name
  56. props.modelValue[selectedVirtualDirectiveId.value] = {
  57. name: newName,
  58. value: newValue,
  59. valType: newValType
  60. }
  61. }
  62. })
  63. const draftVirtualDirectiveValid = ref(true)
  64. const draftVirtualDirective = ref({})
  65. exports.draftVirtualDirective = draftVirtualDirective
  66. const normalizeShadows = inject('normalizeShadows')
  67. watch(
  68. selectedVirtualDirective,
  69. (directive) => {
  70. switch (directive.valType) {
  71. case 'shadow': {
  72. if (Array.isArray(directive.value)) {
  73. draftVirtualDirective.value = normalizeShadows(directive.value)
  74. } else {
  75. const splitShadow = directive.value.split(/,/g).map(x => x.trim())
  76. draftVirtualDirective.value = normalizeShadows(splitShadow)
  77. }
  78. break
  79. }
  80. case 'color':
  81. draftVirtualDirective.value = directive.value
  82. break
  83. default:
  84. draftVirtualDirective.value = directive.value
  85. break
  86. }
  87. },
  88. { immediate: true }
  89. )
  90. watch(
  91. draftVirtualDirective,
  92. (directive) => {
  93. try {
  94. switch (selectedVirtualDirective.value.valType) {
  95. case 'shadow': {
  96. props.modelValue[selectedVirtualDirectiveId.value].value =
  97. directive.map(x => serializeShadow(x)).join(', ')
  98. break
  99. }
  100. default:
  101. props.modelValue[selectedVirtualDirectiveId.value].value = directive
  102. }
  103. draftVirtualDirectiveValid.value = true
  104. } catch (e) {
  105. console.error('Invalid virtual directive value', e)
  106. draftVirtualDirectiveValid.value = false
  107. }
  108. },
  109. { immediate: true }
  110. )
  111. exports.getNewVirtualDirective = () => ({
  112. name: 'newDirective',
  113. valType: 'generic',
  114. value: 'foobar'
  115. })
  116. return exports
  117. }
  118. }