EdText.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. }),
  14. watch: {
  15. tei: function (newtei, oldtei) {
  16. // console.log('tei watcher', oldtei, newtei)
  17. // we need this watcher to update display as route is changing
  18. this.buildTemplate()
  19. }
  20. },
  21. beforeMount () {
  22. console.log('EdText beforeMount')
  23. if (this.tei) {
  24. this.buildTemplate()
  25. }
  26. },
  27. methods: {
  28. buildTemplate () {
  29. // console.log('EdText buildTemplate: tei', this.tei)
  30. // to get Vue.compile working we have to use the full build of vuejs
  31. // https://vuejs.org/v2/guide/installation.html#Explanation-of-Different-Builds
  32. // in /build/webpack.config.base.js alias -> 'vue': 'vue/dist/vue.js',
  33. this.buildHtml()
  34. this.template = Vue.compile(this.html)
  35. this.$options.staticRenderFns = []
  36. this._staticTrees = []
  37. this.template.staticRenderFns.map(fn => (this.$options.staticRenderFns.push(fn)))
  38. },
  39. buildHtml () {
  40. this.html = `<div` +
  41. ` class="tei${this.uuid === this.textid ? ' active' : ''}"` +
  42. ` data-uuid="${this.uuid}"` +
  43. `>${this.tei}</div>`
  44. this.parseLinks()
  45. // console.log('EdText: builded html', this.html)
  46. },
  47. parseLinks () {
  48. let links = this.html.match(/<a[^<]+<\/a>/g)
  49. console.log('links', links)
  50. if (links) {
  51. // let domparser = new DOMParser()
  52. // let domlink
  53. let linkparts, newlink, uuid
  54. let index = null
  55. for (var i = 0; i < links.length; i++) {
  56. // console.log(`link ${i}:`, links[i])
  57. linkparts = RegExp(/<a class="(.+)" href="(.+)" data-index="(.+)">(.+)<\/a>/g).exec(links[i], 'g')
  58. index = linkparts[3]
  59. uuid = linkparts[2].replace('#', '')
  60. newlink = `<a` +
  61. ` class="${linkparts[1]} active-link"` +
  62. ` data-index="${index}"` +
  63. ` data-uuid="${uuid}"` +
  64. ` href="/${index}/${uuid}"` +
  65. ` @click.prevent="onClickRef"` +
  66. ` @keyup.enter="onClickRef"` +
  67. ` @mouseover="onHoverLink"` +
  68. ` @mouseleave="onLeaveLink"` +
  69. `>${linkparts[4]}` +
  70. `<sup class="mdi mdi-message-text-outline" />` +
  71. `</a>`
  72. // console.log('newlink', newlink)
  73. this.html = this.html.replace(links[i], newlink)
  74. }
  75. // console.log('this.html', this.html)
  76. }
  77. },
  78. onClickRef (e) {
  79. console.log('onClickRef(e)', e)
  80. this.$router.push({
  81. name: e.target.dataset.index,
  82. params: { id: e.target.dataset.uuid }
  83. })
  84. },
  85. onHoverLink (e) {
  86. console.log('EdText onHoverLink(e)', e)
  87. this.$emit('onHoverLink', {
  88. uuid: e.target.dataset.uuid,
  89. index: e.target.dataset.index,
  90. rect: e.target.getBoundingClientRect()
  91. })
  92. },
  93. onLeaveLink (e) {
  94. // console.log('EdText onLeaveLink(e)', e)
  95. this.$emit('onLeaveLink')
  96. }
  97. },
  98. render (h) {
  99. console.log('EdText render()')
  100. if (!this.template) {
  101. return h('span', 'Loading ...')
  102. } else {
  103. return this.template.render.call(this)
  104. }
  105. }
  106. }
  107. </script>
  108. <style lang="scss" scoped>
  109. </style>