SearchPage.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. let prevPerc = 0;
  62. let text = '';
  63. for (let i = 0; i < parsed.para.length; i++) {
  64. const p = parsed.para[i];
  65. const parts = parsed.splitToStyle(p.text);
  66. if (this.stopInit)
  67. return;
  68. for (const part of parts)
  69. text += part.text;
  70. const perc = Math.round(i/parsed.para.length*100);
  71. if (perc != prevPerc) {
  72. this.initPercentage = perc;
  73. await sleep(1);
  74. prevPerc = perc;
  75. }
  76. }
  77. this.text = text.toLowerCase();
  78. this.initStep = false;
  79. this.needle = '';
  80. this.foundList = [];
  81. this.foundCur = -1;
  82. this.parsed = parsed;
  83. }
  84. this.header = 'Найти';
  85. await this.$nextTick();
  86. this.$refs.input.focus();
  87. this.$refs.input.select();
  88. }
  89. get foundText() {
  90. if (this.foundList.length && this.foundCur >= 0)
  91. return `${this.foundCur + 1}/${this.foundList.length}`;
  92. else
  93. return '';
  94. }
  95. find() {
  96. let foundList = [];
  97. if (this.needle) {
  98. const needle = this.needle.toLowerCase();
  99. let i = 0;
  100. while (i < this.text.length) {
  101. const found = this.text.indexOf(needle, i);
  102. if (found >= 0)
  103. foundList.push(found);
  104. i = (found >= 0 ? found + 1 : this.text.length);
  105. }
  106. }
  107. this.foundList = foundList;
  108. this.foundCur = -1;
  109. this.showNext();
  110. }
  111. showNext() {
  112. const next = this.foundCur + 1;
  113. if (next < this.foundList.length)
  114. this.foundCur = next;
  115. else
  116. this.foundCur = (this.foundList.length ? 0 : -1);
  117. if (this.foundCur >= 0) {
  118. this.$emit('start-text-search', {needle: this.needle.toLowerCase()});
  119. this.$emit('book-pos-changed', {bookPos: this.foundList[this.foundCur]});
  120. } else {
  121. this.$emit('stop-text-search');
  122. }
  123. this.$refs.input.focus();
  124. }
  125. showPrev() {
  126. const prev = this.foundCur - 1;
  127. if (prev >= 0)
  128. this.foundCur = prev;
  129. else
  130. this.foundCur = this.foundList.length - 1;
  131. if (this.foundCur >= 0) {
  132. this.$emit('start-text-search', {needle: this.needle.toLowerCase()});
  133. this.$emit('book-pos-changed', {bookPos: this.foundList[this.foundCur]});
  134. } else {
  135. this.$emit('stop-text-search');
  136. }
  137. this.$refs.input.focus();
  138. }
  139. close() {
  140. this.stopInit = true;
  141. this.$emit('search-toggle');
  142. }
  143. keyHook(event) {
  144. //недостатки сторонних ui
  145. if (document.activeElement === this.$refs.input && event.type == 'keydown' && event.key == 'Enter') {
  146. this.showNext();
  147. }
  148. if (event.type == 'keydown' && (event.code == 'Escape')) {
  149. this.close();
  150. }
  151. return true;
  152. }
  153. }
  154. //-----------------------------------------------------------------------------
  155. </script>
  156. <style scoped>
  157. .main {
  158. position: absolute;
  159. width: 100%;
  160. height: 100%;
  161. z-index: 40;
  162. display: flex;
  163. flex-direction: column;
  164. justify-content: center;
  165. align-items: center;
  166. }
  167. .mainWindow {
  168. width: 100%;
  169. max-width: 500px;
  170. height: 125px;
  171. display: flex;
  172. position: relative;
  173. top: -50px;
  174. }
  175. .content {
  176. flex: 1;
  177. display: flex;
  178. justify-content: center;
  179. align-items: center;
  180. padding: 10px;
  181. }
  182. .input {
  183. display: flex;
  184. margin: 0;
  185. padding: 0;
  186. width: 100%;
  187. position: relative;
  188. }
  189. .button-group {
  190. width: 150px;
  191. margin: 0;
  192. padding: 0;
  193. }
  194. .el-button {
  195. padding: 9px 17px 9px 17px;
  196. }
  197. i {
  198. font-size: 20px;
  199. }
  200. </style>