SearchPage.vue 6.1 KB

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