EdText.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <script>
  2. import Vue from 'vue'
  3. export default {
  4. name: 'EdText',
  5. props: {
  6. tei: String,
  7. uuid: String,
  8. textid: String
  9. },
  10. data: () => ({
  11. template: null,
  12. html: null,
  13. pages: []
  14. }),
  15. watch: {
  16. tei: function (newtei, oldtei) {
  17. // console.log('tei watcher', oldtei, newtei)
  18. // we need this watcher to update display as route is changing
  19. this.buildTemplate()
  20. }
  21. },
  22. beforeMount () {
  23. // console.log('EdText beforeMount')
  24. if (this.tei) {
  25. this.buildTemplate()
  26. }
  27. },
  28. // mounted () {
  29. // this.$emit('onNewPageBreaks', this.pages)
  30. // },
  31. methods: {
  32. buildTemplate () {
  33. // console.log('EdText buildTemplate: tei', this.tei)
  34. // to get Vue.compile working we have to use the full build of vuejs
  35. // https://vuejs.org/v2/guide/installation.html#Explanation-of-Different-Builds
  36. // in /build/webpack.config.base.js alias -> 'vue': 'vue/dist/vue.js',
  37. this.buildHtml()
  38. this.template = Vue.compile(this.html)
  39. this.$options.staticRenderFns = []
  40. this._staticTrees = []
  41. this.template.staticRenderFns.map(fn => (this.$options.staticRenderFns.push(fn)))
  42. },
  43. buildHtml () {
  44. this.html = `<div` +
  45. ` class="tei${this.uuid === this.textid ? ' active' : ''}"` +
  46. ` data-uuid="${this.uuid}"` +
  47. `>${this.tei}</div>`
  48. this.parseLinks()
  49. // this.parsePageBreaks()
  50. // console.log('EdText: builded html', this.html)
  51. },
  52. parseLinks () {
  53. let links = this.html.match(/<a[^<]+<\/a>/g)
  54. // console.log('links', links)
  55. if (links) {
  56. // let domparser = new DOMParser()
  57. // let domlink
  58. let linkparts, newlink, uuid
  59. let index = null
  60. for (var i = 0; i < links.length; i++) {
  61. // console.log(`link ${i}:`, links[i])
  62. linkparts = RegExp(/<a class="(.+)" href="(.+)" data-index="(.+)">(.+)<\/a>/g).exec(links[i], 'g')
  63. // console.log('linkparts', linkparts)
  64. if (!linkparts) {
  65. console.warn(`link ${i} malformed:`, links[i])
  66. } else {
  67. index = linkparts[3]
  68. uuid = linkparts[2].replace('#', '')
  69. newlink = `<a` +
  70. ` class="${linkparts[1]} active-link"` +
  71. ` data-index="${index}"` +
  72. ` data-uuid="${uuid}"` +
  73. ` href="/${index}/${uuid}"` +
  74. ` @click.prevent="onClickRef"` +
  75. ` @keyup.enter="onClickRef"` +
  76. ` @mouseover="onHoverLink"` +
  77. ` @mouseleave="onLeaveLink"` +
  78. // prevent click on this one
  79. ` v-touch:tap.prevent="onTapLink"` +
  80. ` v-touch-class="'tapped'"` +
  81. `>${linkparts[4]}` +
  82. `<sup class="mdi mdi-message-text-outline" />` +
  83. `</a>`
  84. // console.log('newlink', newlink)
  85. this.html = this.html.replace(links[i], newlink)
  86. }
  87. }
  88. // console.log('this.html', this.html)
  89. }
  90. },
  91. // parsePageBreaks () {
  92. // let pbs = this.html.match(/<span role="pageBreak"[^>]+><\/span>/g)
  93. // console.log('pagebreaks', pbs)
  94. // if (pbs) {
  95. // let pbparts, newpb, num
  96. // for (var i = 0; i < pbs.length; i++) {
  97. // pbparts = RegExp(/<span role="pageBreak" data-num="(.+)"><\/span>/).exec(pbs[i], 'g')
  98. // if (!pbparts) {
  99. // console.warn(`pageBreak ${i} maformed`, pbs[i])
  100. // } else {
  101. // // console.log('pbparts', pbparts)
  102. // num = pbparts[1]
  103. // this.pages.push(num)
  104. // newpb = `<span` +
  105. // // ` id="page-break-${num}"` +
  106. // ` role="pageBreak"` +
  107. // ` data-num="${num}"` +
  108. // ` data-num-prev="${num - 1}"` +
  109. // ` />`
  110. // // console.log('newpb', newpb)
  111. // this.html = this.html.replace(pbs[i], newpb)
  112. // }
  113. // }
  114. // }
  115. // },
  116. onClickRef (e) {
  117. console.log('onClickRef(e)', e)
  118. if (e.target.classList.contains('tapped')) {
  119. this.$router.push({
  120. name: e.target.dataset.index,
  121. params: { id: e.target.dataset.uuid }
  122. })
  123. }
  124. },
  125. onHoverLink (e) {
  126. console.log('EdText onHoverLink(e)', e)
  127. this.$emit('onHoverLink', {
  128. uuid: e.target.dataset.uuid,
  129. index: e.target.dataset.index,
  130. rect: e.target.getBoundingClientRect()
  131. })
  132. },
  133. onTapLink (e) {
  134. console.log('EdText onTapLink(e)', e)
  135. this.$emit('onHoverLink', {
  136. uuid: e.target.dataset.uuid,
  137. index: e.target.dataset.index,
  138. rect: e.target.getBoundingClientRect()
  139. })
  140. },
  141. onLeaveLink (e) {
  142. // console.log('EdText onLeaveLink(e)', e)
  143. this.$emit('onLeaveLink')
  144. }
  145. },
  146. render (h) {
  147. // console.log('EdText render()')
  148. if (!this.template) {
  149. return h('span', 'Loading ...')
  150. } else {
  151. return this.template.render.call(this)
  152. }
  153. }
  154. }
  155. </script>
  156. <style lang="scss" scoped>
  157. </style>