relation.js 3.1 KB

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