EdText.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. `>` +
  48. `<a` +
  49. ` href="http://${window.apipath}/items/${this.uuid}"` +
  50. ` title="Copy to clipboard ${this.uuid}"` +
  51. ` class="text-item-link"` +
  52. ` @click.prevent="onClickCopyClipboard"` +
  53. `>` +
  54. `<span class="mdi mdi-open-in-new" />` +
  55. `</a>` +
  56. `${this.tei}</div>`
  57. this.parseLinks()
  58. // this.parsePageBreaks()
  59. // console.log('EdText: builded html', this.html)
  60. },
  61. parseLinks () {
  62. let links = this.html.match(/<a[^<]+<\/a>/g)
  63. // console.log('links', links)
  64. if (links) {
  65. // let domparser = new DOMParser()
  66. // let domlink
  67. let linkparts, newlink, uuid
  68. let index = null
  69. for (var i = 0; i < links.length; i++) {
  70. // console.log(`link ${i}:`, links[i])
  71. linkparts = RegExp(/<a class="(.+)" href="(.+)" data-index="(.+)">(.+)<\/a>/g).exec(links[i], 'g')
  72. // console.log('linkparts', linkparts)
  73. if (!linkparts) {
  74. console.warn(`link ${i} malformed:`, links[i])
  75. } else {
  76. index = linkparts[3]
  77. uuid = linkparts[2].replace('#', '')
  78. newlink = `<a` +
  79. ` class="${linkparts[1]} active-link"` +
  80. ` data-index="${index}"` +
  81. ` data-uuid="${uuid}"` +
  82. ` href="/${index}/${uuid}"` +
  83. ` @click.prevent="onClickRef"` +
  84. ` @keyup.enter="onClickRef"` +
  85. ` @mouseover="onHoverLink"` +
  86. ` @mouseleave="onLeaveLink"` +
  87. // prevent click on this one
  88. ` v-touch:tap.prevent="onTapLink"` +
  89. ` v-touch-class="'tapped'"` +
  90. `>${linkparts[4]}` +
  91. `<sup class="mdi mdi-message-text-outline" />` +
  92. `</a>`
  93. // console.log('newlink', newlink)
  94. this.html = this.html.replace(links[i], newlink)
  95. }
  96. }
  97. // console.log('this.html', this.html)
  98. }
  99. },
  100. // parsePageBreaks () {
  101. // let pbs = this.html.match(/<span role="pageBreak"[^>]+><\/span>/g)
  102. // console.log('pagebreaks', pbs)
  103. // if (pbs) {
  104. // let pbparts, newpb, num
  105. // for (var i = 0; i < pbs.length; i++) {
  106. // pbparts = RegExp(/<span role="pageBreak" data-num="(.+)"><\/span>/).exec(pbs[i], 'g')
  107. // if (!pbparts) {
  108. // console.warn(`pageBreak ${i} maformed`, pbs[i])
  109. // } else {
  110. // // console.log('pbparts', pbparts)
  111. // num = pbparts[1]
  112. // this.pages.push(num)
  113. // newpb = `<span` +
  114. // // ` id="page-break-${num}"` +
  115. // ` role="pageBreak"` +
  116. // ` data-num="${num}"` +
  117. // ` data-num-prev="${num - 1}"` +
  118. // ` />`
  119. // // console.log('newpb', newpb)
  120. // this.html = this.html.replace(pbs[i], newpb)
  121. // }
  122. // }
  123. // }
  124. // },
  125. onClickRef (e) {
  126. console.log('onClickRef(e)', e)
  127. if (e.target.classList.contains('tapped')) {
  128. this.$router.push({
  129. name: e.target.dataset.index,
  130. params: { id: e.target.dataset.uuid }
  131. })
  132. }
  133. },
  134. onHoverLink (e) {
  135. console.log('EdText onHoverLink(e)', e)
  136. this.$emit('onHoverLink', {
  137. uuid: e.target.dataset.uuid,
  138. index: e.target.dataset.index,
  139. rect: e.target.getBoundingClientRect()
  140. })
  141. },
  142. onTapLink (e) {
  143. console.log('EdText onTapLink(e)', e)
  144. this.$emit('onHoverLink', {
  145. uuid: e.target.dataset.uuid,
  146. index: e.target.dataset.index,
  147. rect: e.target.getBoundingClientRect()
  148. })
  149. },
  150. onLeaveLink (e) {
  151. // console.log('EdText onLeaveLink(e)', e)
  152. this.$emit('onLeaveLink')
  153. },
  154. onClickCopyClipboard (e) {
  155. e.preventDefault()
  156. console.log('onClickCopyClipboard', e)
  157. // navigator.clipboard.writeText(e.target.getAttribute('href'))
  158. this.$copyText(e.target.getAttribute('href'))
  159. return false
  160. }
  161. },
  162. render (h) {
  163. // console.log('EdText render()')
  164. if (!this.template) {
  165. return h('span', 'Loading ...')
  166. } else {
  167. return this.template.render.call(this)
  168. }
  169. }
  170. }
  171. </script>
  172. <style lang="scss" scoped>
  173. </style>