EdText.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <template>
  2. <div class="text-wrapper">
  3. <EdTextRefLink :uuid="uuid" />
  4. <div
  5. class="tei"
  6. :class="{ active: uuid === textid }"
  7. :data-uuid="uuid"
  8. v-html="tei"
  9. />
  10. </div>
  11. </template>
  12. <script>
  13. // import Vue from 'vue'
  14. import EdTextRefLink from './EdTextRefLink'
  15. export default {
  16. name: 'EdText',
  17. components: {
  18. EdTextRefLink
  19. },
  20. props: {
  21. tei: String,
  22. uuid: String,
  23. textid: String
  24. },
  25. data: () => ({
  26. template: null,
  27. html: null,
  28. pages: []
  29. }),
  30. watch: {
  31. tei: function (newtei, oldtei) {
  32. // console.log('tei watcher', oldtei, newtei)
  33. // we need this watcher to update display as route is changing
  34. // this.buildTemplate()
  35. this.parseTei()
  36. }
  37. },
  38. beforeMount () {
  39. console.log('EdText beforeMount', this.tei)
  40. if (this.tei) {
  41. // this.buildTemplate()
  42. this.parseTei()
  43. }
  44. },
  45. // mounted () {
  46. // this.$emit('onNewPageBreaks', this.pages)
  47. // },
  48. methods: {
  49. // buildTemplate () {
  50. // // console.log('EdText buildTemplate: tei', this.tei)
  51. // // to get Vue.compile working we have to use the full build of vuejs
  52. // // https://vuejs.org/v2/guide/installation.html#Explanation-of-Different-Builds
  53. // // in /build/webpack.config.base.js alias -> 'vue': 'vue/dist/vue.js',
  54. // this.buildHtml()
  55. // this.template = Vue.compile(this.html)
  56. // this.$options.staticRenderFns = []
  57. // this._staticTrees = []
  58. // this.template.staticRenderFns.map(fn => (this.$options.staticRenderFns.push(fn)))
  59. // },
  60. // buildHtml () {
  61. // this.html = `<div` +
  62. // ` class="tei${this.uuid === this.textid ? ' active' : ''}"` +
  63. // ` data-uuid="${this.uuid}"` +
  64. // `>` +
  65. // `<a` +
  66. // ` href="http://${window.apipath}/items/${this.uuid}"` +
  67. // ` title="Copy to clipboard ${this.uuid}"` +
  68. // ` class="text-item-link"` +
  69. // ` @click.prevent="onClickCopyClipboard"` +
  70. // `>` +
  71. // `<span class="mdi mdi-open-in-new" />` +
  72. // `</a>` +
  73. // `${this.tei}</div>`
  74. // this.parseLinks()
  75. // this.parseFigures()
  76. // // this.parsePageBreaks()
  77. // // console.log('EdText: builded html', this.html)
  78. // },
  79. parseTei () {
  80. this.parseLinks()
  81. this.parseFigures()
  82. },
  83. parseLinks () {
  84. let links = this.tei.match(/<a[^<]+<\/a>/g)
  85. // console.log('links', links)
  86. if (links) {
  87. // let domparser = new DOMParser()
  88. // let domlink
  89. let linkparts, newlink, uuid
  90. let index = null
  91. for (var i = 0; i < links.length; i++) {
  92. // console.log(`link ${i}:`, links[i])
  93. linkparts = RegExp(/<a class="(.+)" href="(.+)" data-index="(.+)">(.+)<\/a>/g).exec(links[i], 'g')
  94. // console.log('linkparts', linkparts)
  95. if (!linkparts) {
  96. console.warn(`link ${i} malformed:`, links[i])
  97. } else {
  98. index = linkparts[3]
  99. uuid = linkparts[2].replace('#', '')
  100. newlink = `<a` +
  101. ` class="${linkparts[1]} active-link"` +
  102. ` data-index="${index}"` +
  103. ` data-uuid="${uuid}"` +
  104. ` href="/${index}/${uuid}"` +
  105. ` @click.prevent="onClickRef"` +
  106. ` @keyup.enter="onClickRef"` +
  107. ` @mouseover="onHoverLink"` +
  108. ` @mouseleave="onLeaveLink"` +
  109. // prevent click on this one
  110. ` v-touch:tap.prevent="onTapLink"` +
  111. ` v-touch-class="'tapped'"` +
  112. `>${linkparts[4]}` +
  113. `<sup class="mdi mdi-message-text-outline" />` +
  114. `</a>`
  115. // console.log('newlink', newlink)
  116. this.tei = this.tei.replace(links[i], newlink)
  117. }
  118. }
  119. // console.log('this.html', this.html)
  120. }
  121. },
  122. parseFigures () {
  123. console.log('parseFigures this.tei', this.tei)
  124. let imgs = this.tei.match(/<img[^>]*>/g)
  125. console.log('imgs', imgs)
  126. if (imgs) {
  127. let imgparts, newsrc, newimg
  128. for (var i = 0; i < imgs.length; i++) {
  129. // console.log(`link ${i}:`, links[i])
  130. imgparts = RegExp(/<img src="(.+)" alt="(.+)">/g).exec(imgs[i], 'g')
  131. console.log('imgparts', imgparts)
  132. if (!imgparts) {
  133. console.warn(`img ${i} malformed:`, imgs[i])
  134. } else {
  135. newsrc = `${imgparts[1]}/full/1000,/0/native.jpg`
  136. newimg = `<img` +
  137. ` src="${newsrc}"` +
  138. ` alt="${imgparts[2]}"` +
  139. `/>`
  140. // console.log('newlink', newlink)
  141. this.tei = this.tei.replace(imgs[i], newimg)
  142. }
  143. }
  144. }
  145. },
  146. // parsePageBreaks () {
  147. // let pbs = this.html.match(/<span role="pageBreak"[^>]+><\/span>/g)
  148. // console.log('pagebreaks', pbs)
  149. // if (pbs) {
  150. // let pbparts, newpb, num
  151. // for (var i = 0; i < pbs.length; i++) {
  152. // pbparts = RegExp(/<span role="pageBreak" data-num="(.+)"><\/span>/).exec(pbs[i], 'g')
  153. // if (!pbparts) {
  154. // console.warn(`pageBreak ${i} maformed`, pbs[i])
  155. // } else {
  156. // // console.log('pbparts', pbparts)
  157. // num = pbparts[1]
  158. // this.pages.push(num)
  159. // newpb = `<span` +
  160. // // ` id="page-break-${num}"` +
  161. // ` role="pageBreak"` +
  162. // ` data-num="${num}"` +
  163. // ` data-num-prev="${num - 1}"` +
  164. // ` />`
  165. // // console.log('newpb', newpb)
  166. // this.html = this.html.replace(pbs[i], newpb)
  167. // }
  168. // }
  169. // }
  170. // },
  171. onClickRef (e) {
  172. console.log('onClickRef(e)', e)
  173. if (e.target.classList.contains('tapped')) {
  174. this.$router.push({
  175. name: e.target.dataset.index,
  176. params: { id: e.target.dataset.uuid }
  177. })
  178. }
  179. },
  180. onHoverLink (e) {
  181. console.log('EdText onHoverLink(e)', e)
  182. this.$emit('onHoverLink', {
  183. uuid: e.target.dataset.uuid,
  184. index: e.target.dataset.index,
  185. rect: e.target.getBoundingClientRect()
  186. })
  187. },
  188. onTapLink (e) {
  189. console.log('EdText onTapLink(e)', e)
  190. this.$emit('onHoverLink', {
  191. uuid: e.target.dataset.uuid,
  192. index: e.target.dataset.index,
  193. rect: e.target.getBoundingClientRect()
  194. })
  195. },
  196. onLeaveLink (e) {
  197. // console.log('EdText onLeaveLink(e)', e)
  198. this.$emit('onLeaveLink')
  199. }
  200. // ,
  201. // onClickCopyClipboard (e) {
  202. // e.preventDefault()
  203. // console.log('onClickCopyClipboard', e)
  204. // // navigator.clipboard.writeText(e.target.getAttribute('href'))
  205. // this.$copyText(e.target.getAttribute('href'))
  206. // return false
  207. // }
  208. }
  209. // render (h) {
  210. // // console.log('EdText render()')
  211. // if (!this.template) {
  212. // return h('span', 'Loading ...')
  213. // } else {
  214. // return this.template.render.call(this)
  215. // }
  216. // }
  217. }
  218. </script>
  219. <style lang="scss" scoped>
  220. </style>