SearchPage.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <template>
  2. <Window ref="window" height="125px" max-width="600px" :top-shift="-50" @close="close">
  3. <template slot="header">
  4. {{ header }}
  5. </template>
  6. <div class="content">
  7. <span v-show="initStep">{{ initPercentage }}%</span>
  8. <div v-show="!initStep" class="input">
  9. <input ref="input" class="el-input__inner"
  10. placeholder="что ищем"
  11. :value="needle" @input="needle = $event.target.value"/>
  12. <div style="position: absolute; right: 10px; margin-top: 10px; font-size: 16px;">{{ foundText }}</div>
  13. </div>
  14. <el-button-group v-show="!initStep" class="button-group">
  15. <el-button @click="showNext"><i class="el-icon-arrow-down"></i></el-button>
  16. <el-button @click="showPrev"><i class="el-icon-arrow-up"></i></el-button>
  17. </el-button-group>
  18. </div>
  19. </Window>
  20. </template>
  21. <script>
  22. //-----------------------------------------------------------------------------
  23. import Vue from 'vue';
  24. import Component from 'vue-class-component';
  25. import Window from '../../share/Window.vue';
  26. import {sleep} from '../../../share/utils';
  27. export default @Component({
  28. components: {
  29. Window,
  30. },
  31. watch: {
  32. needle: function() {
  33. this.find();
  34. },
  35. foundText: function(newValue) {
  36. this.$refs.input.style.paddingRight = (10 + newValue.length*12) + 'px';
  37. },
  38. },
  39. })
  40. class SearchPage extends Vue {
  41. header = null;
  42. initStep = null;
  43. initPercentage = 0;
  44. needle = null;
  45. foundList = [];
  46. foundCur = -1;
  47. created() {
  48. this.commit = this.$store.commit;
  49. this.reader = this.$store.state.reader;
  50. }
  51. async init(parsed) {
  52. this.$refs.window.init();
  53. if (this.parsed != parsed) {
  54. this.initStep = true;
  55. this.stopInit = false;
  56. this.header = 'Подготовка';
  57. await this.$nextTick();
  58. await sleep(10);
  59. let nextPerc = 0;
  60. let text = '';
  61. for (let i = 0; i < parsed.para.length; i++) {
  62. const p = parsed.para[i];
  63. const parts = parsed.splitToStyle(p.text);
  64. if (this.stopInit)
  65. return;
  66. for (const part of parts)
  67. text += part.text;
  68. const perc = Math.round(i/parsed.para.length*100);
  69. if (perc > nextPerc) {
  70. this.initPercentage = perc;
  71. await sleep(1);
  72. nextPerc = perc + 10;
  73. }
  74. }
  75. this.text = text.toLowerCase();
  76. this.initStep = false;
  77. this.needle = '';
  78. this.foundList = [];
  79. this.foundCur = -1;
  80. this.parsed = parsed;
  81. }
  82. this.header = 'Найти';
  83. await this.$nextTick();
  84. this.$refs.input.focus();
  85. this.$refs.input.select();
  86. }
  87. get foundText() {
  88. if (this.foundList.length && this.foundCur >= 0)
  89. return `${this.foundCur + 1}/${this.foundList.length}`;
  90. else
  91. return '';
  92. }
  93. find() {
  94. let foundList = [];
  95. if (this.needle) {
  96. const needle = this.needle.toLowerCase();
  97. let i = 0;
  98. while (i < this.text.length) {
  99. const found = this.text.indexOf(needle, i);
  100. if (found >= 0)
  101. foundList.push(found);
  102. i = (found >= 0 ? found + 1 : this.text.length);
  103. }
  104. }
  105. this.foundList = foundList;
  106. this.foundCur = -1;
  107. this.showNext();
  108. }
  109. showNext() {
  110. const next = this.foundCur + 1;
  111. if (next < this.foundList.length)
  112. this.foundCur = next;
  113. else
  114. this.foundCur = (this.foundList.length ? 0 : -1);
  115. if (this.foundCur >= 0) {
  116. this.$emit('start-text-search', {needle: this.needle.toLowerCase()});
  117. this.$emit('book-pos-changed', {bookPos: this.foundList[this.foundCur]});
  118. } else {
  119. this.$emit('stop-text-search');
  120. }
  121. this.$refs.input.focus();
  122. }
  123. showPrev() {
  124. const prev = this.foundCur - 1;
  125. if (prev >= 0)
  126. this.foundCur = prev;
  127. else
  128. this.foundCur = this.foundList.length - 1;
  129. if (this.foundCur >= 0) {
  130. this.$emit('start-text-search', {needle: this.needle.toLowerCase()});
  131. this.$emit('book-pos-changed', {bookPos: this.foundList[this.foundCur]});
  132. } else {
  133. this.$emit('stop-text-search');
  134. }
  135. this.$refs.input.focus();
  136. }
  137. close() {
  138. this.stopInit = true;
  139. this.$emit('search-toggle');
  140. }
  141. keyHook(event) {
  142. //недостатки сторонних ui
  143. if (document.activeElement === this.$refs.input && event.type == 'keydown' && event.key == 'Enter') {
  144. this.showNext();
  145. }
  146. if (event.type == 'keydown' && (event.code == 'Escape')) {
  147. this.close();
  148. }
  149. return true;
  150. }
  151. }
  152. //-----------------------------------------------------------------------------
  153. </script>
  154. <style scoped>
  155. .content {
  156. flex: 1;
  157. display: flex;
  158. justify-content: center;
  159. align-items: center;
  160. padding: 10px;
  161. min-width: 430px;
  162. }
  163. .input {
  164. display: flex;
  165. margin: 0;
  166. padding: 0;
  167. width: 100%;
  168. position: relative;
  169. }
  170. .button-group {
  171. width: 150px;
  172. margin: 0;
  173. padding: 0;
  174. }
  175. .el-button {
  176. padding: 9px 17px 9px 17px;
  177. width: 55px;
  178. }
  179. i {
  180. font-size: 20px;
  181. }
  182. </style>