EdText.vue 7.4 KB

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