四零语境前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.5 KiB

  1. <template>
  2. <view class="yingbing-content" v-html="contentSync"></view>
  3. </template>
  4. <script>
  5. export default {
  6. inject: ['getFontSize', 'getFontFamily', 'getLineHeight', 'getColor', 'getSelectable'],
  7. props: {
  8. item: {
  9. type: Object,
  10. default () {
  11. return new Object
  12. }
  13. }
  14. },
  15. computed: {
  16. color () {
  17. return this.getColor()
  18. },
  19. fontSize () {
  20. return this.getFontSize()
  21. },
  22. fontFamily () {
  23. return this.getFontFamily()
  24. },
  25. lineHeight () {
  26. return this.getLineHeight()
  27. },
  28. selectable () {
  29. return this.getSelectable()
  30. },
  31. contentSync () {
  32. let content = this.item.content || ''
  33. content = content.replace('<whole-render', '<div').replace('<\/whole-render>', '<\/div>')
  34. return `<div class="yingbing-reader-content-html ${this.selectable ? 'user-selectable' : 'user-selectclose'}" style="box-sizing: border-box;color:${this.color};font-family:${this.fontFamily};font-size:${this.fontSize}px;line-height:${this.lineHeight}px;">` + content + '</div>'
  35. }
  36. },
  37. data () {
  38. return {
  39. selectShow: false,
  40. selectTop: 0,
  41. selectLeft: 0,
  42. selectArrowLeft: 0
  43. }
  44. },
  45. mounted() {
  46. // #ifdef H5
  47. // document.addEventListener('selectionchange',() => {
  48. // const selection = window.getSelection();// 获取当前的选区对象
  49. // // 如果选区不为空
  50. // if (selection.toString()) {
  51. // // 获取选区中的第一个范围
  52. // const range = selection.getRangeAt(0);
  53. // // 将选区的边界用于定位操作框
  54. // const rect = range.getBoundingClientRect();
  55. // const box = document.querySelector('.yingbing-reader-select-box')
  56. // this.selectArrowLeft = rect.left
  57. // this.selectTop = rect.top + (box.offsetHeight / 2)
  58. // this.selectLeft = rect.left - (box.offsetWidth / 2)
  59. // const right = box.offsetWidth + this.selectLeft
  60. // const bottom = box.offsetHeight + this.selectTop
  61. // if ( right > document.body.offsetWidth ) this.selectLeft = this.selectLeft - (right - document.body.offsetWidth)
  62. // if ( bottom > document.body.offsetHeight ) this.selectTop = this.selectTop - (bottom - document.body.offsetHeight)
  63. // this.selectShow = true
  64. // } else {
  65. // this.selectShow = false
  66. // }
  67. // });
  68. // #endif
  69. }
  70. }
  71. </script>
  72. <style scoped>
  73. /deep/ .yingbing-img {
  74. max-width: 100%!important;
  75. box-sizing: border-box!important;
  76. }
  77. /deep/ .user-selectable {
  78. -webkit-user-select: text; /*webkit浏览器*/
  79. -khtml-user-select: text; /*早期浏览器*/
  80. -moz-user-select: text; /*火狐*/
  81. -ms-user-select: text; /*IE10*/
  82. user-select: text;
  83. }
  84. /deep/ .user-selectclose {
  85. -webkit-touch-callout: none; /*系统默认菜单被禁用*/
  86. -webkit-user-select: none; /*webkit浏览器*/
  87. -khtml-user-select: none; /*早期浏览器*/
  88. -moz-user-select: none; /*火狐*/
  89. -ms-user-select: none; /*IE10*/
  90. user-select: none;
  91. }
  92. .yingbing-content {
  93. width: 100%;
  94. height: 100%;
  95. box-sizing: border-box;
  96. }
  97. .yingbing-reader-select-box {
  98. position: fixed;
  99. display: flex;
  100. flex-direction: row;
  101. background-color: #fff;
  102. box-shadow: 0 0 10px rgba(0,0,0,.1);
  103. border-radius: 5px;
  104. padding: 15px 10px;
  105. }
  106. .yingbing-reader-select-box .arrow {
  107. position: absolute;
  108. width: 0;
  109. height: 0;
  110. border-top: 6px solid transparent;
  111. border-left: 6px solid transparent;
  112. border-right: 6px solid transparent;
  113. border-bottom: 6px solid #fff;
  114. top: -12px;
  115. }
  116. .yingbing-reader-select-box .select-box-item {
  117. padding: 0 10px;
  118. font-size: 13px;
  119. flex-shrink: 0;
  120. }
  121. </style>