relation.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. const styles = (v ='') => v.split(';').filter(v => v && !/^[\n\s]+$/.test(v)).map(v => {
  2. const key = v.slice(0, v.indexOf(':'))
  3. const value = v.slice(v.indexOf(':')+1)
  4. return {
  5. [key
  6. .replace(/-([a-z])/g, function() { return arguments[1].toUpperCase()})
  7. .replace(/\s+/g, '')
  8. ]: value.replace(/^\s+/, '').replace(/\s+$/, '') || ''
  9. }
  10. })
  11. export function parent(parent) {
  12. return {
  13. provide() {
  14. return {
  15. [parent]: this
  16. }
  17. },
  18. data() {
  19. return {
  20. el: {
  21. css: {},
  22. views: []
  23. },
  24. }
  25. },
  26. watch: {
  27. css: {
  28. handler(v) {
  29. if(this.canvasId) {
  30. this.el.css = (typeof v == 'object' ? v : v && Object.assign(...styles(v))) || {}
  31. this.canvasWidth = this.el.css && this.el.css.width || this.canvasWidth
  32. this.canvasHeight = this.el.css && this.el.css.height || this.canvasHeight
  33. }
  34. },
  35. immediate: true
  36. }
  37. }
  38. }
  39. }
  40. export function children(parent, options = {}) {
  41. const indexKey = options.indexKey || 'index'
  42. return {
  43. inject: {
  44. [parent]: {
  45. default: null
  46. }
  47. },
  48. watch: {
  49. el: {
  50. handler(v, o) {
  51. if(JSON.stringify(v) != JSON.stringify(o))
  52. this.bindRelation()
  53. },
  54. deep: true,
  55. immediate: true
  56. },
  57. src: {
  58. handler(v, o) {
  59. if(v != o)
  60. this.bindRelation()
  61. },
  62. immediate: true
  63. },
  64. text: {
  65. handler(v, o) {
  66. if(v != o) this.bindRelation()
  67. },
  68. immediate: true
  69. },
  70. css: {
  71. handler(v, o) {
  72. if(v != o)
  73. this.el.css = (typeof v == 'object' ? v : v && Object.assign(...styles(v))) || {}
  74. },
  75. immediate: true
  76. },
  77. replace: {
  78. handler(v, o) {
  79. if(JSON.stringify(v) != JSON.stringify(o))
  80. this.bindRelation()
  81. },
  82. deep: true,
  83. immediate: true
  84. }
  85. },
  86. created() {
  87. if(!this._uid) {
  88. this._uid = this._.uid
  89. }
  90. Object.defineProperty(this, 'parent', {
  91. get: () => this[parent] || [],
  92. })
  93. Object.defineProperty(this, 'index', {
  94. get: () => {
  95. this.bindRelation();
  96. const {parent: {el: {views=[]}={}}={}} = this
  97. return views.indexOf(this.el)
  98. },
  99. });
  100. this.el.type = this.type
  101. this.bindRelation()
  102. },
  103. // #ifdef VUE3
  104. beforeUnmount() {
  105. this.removeEl()
  106. },
  107. // #endif
  108. // #ifdef VUE2
  109. beforeDestroy() {
  110. this.removeEl()
  111. },
  112. // #endif
  113. methods: {
  114. removeEl() {
  115. if (this.parent) {
  116. this.parent.el.views = this.parent.el.views.filter(
  117. (item) => item._uid !== this._uid
  118. );
  119. }
  120. },
  121. bindRelation() {
  122. if(!this.el._uid) {
  123. this.el._uid = this._uid
  124. }
  125. if(['text','qrcode'].includes(this.type)) {
  126. this.el.text = this.$slots && this.$slots.default && this.$slots.default[0].text || `${this.text || ''}`.replace(/\\n/g, '\n')
  127. }
  128. if(this.type == 'image') {
  129. this.el.src = this.src
  130. }
  131. if (!this.parent) {
  132. return;
  133. }
  134. let views = this.parent.el.views || [];
  135. if(views.indexOf(this.el) !== -1) {
  136. this.parent.el.views = views.map(v => v._uid == this._uid ? this.el : v)
  137. } else {
  138. this.parent.el.views = [...views, this.el];
  139. }
  140. }
  141. },
  142. mounted() {
  143. // this.bindRelation()
  144. },
  145. }
  146. }