UserHotKeys.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <template>
  2. <div class="table col column no-wrap">
  3. <!-- header -->
  4. <div class="table-row row">
  5. <div class="desc q-pa-sm bg-blue-2">
  6. Команда
  7. </div>
  8. <div class="hotKeys col q-pa-sm bg-blue-2 row no-wrap">
  9. <div style="width: 80px">
  10. Сочетание клавиш
  11. </div>
  12. <q-input ref="input"
  13. v-model="search"
  14. class="q-ml-sm col"
  15. outlined dense rounded
  16. bg-color="grey-4"
  17. placeholder="Найти"
  18. @click.stop
  19. />
  20. <div v-show="!readonly" class="q-ml-sm column justify-center">
  21. <q-btn class="bg-grey-4 text-grey-6" style="height: 35px; width: 35px" rounded flat icon="la la-broom" @click="defaultHotKeyAll">
  22. <q-tooltip :delay="1000" anchor="top middle" self="bottom middle" content-style="font-size: 80%">
  23. Установить все сочетания по умолчанию
  24. </q-tooltip>
  25. </q-btn>
  26. </div>
  27. </div>
  28. </div>
  29. <!-- body -->
  30. <div v-for="(action, index) in tableData" :key="index" class="table-row row">
  31. <div class="desc q-pa-sm">
  32. {{ rstore.readerActions[action] }}
  33. </div>
  34. <div class="hotKeys col q-pa-sm">
  35. <q-chip
  36. v-for="(code, index2) in value[action]" :key="index2"
  37. :color="collisions[code] ? 'red' : 'grey-7'"
  38. :removable="!readonly" :clickable="collisions[code] ? true : false"
  39. text-color="white" @remove="removeCode(action, code)"
  40. @click="collisionWarning(code)"
  41. >
  42. {{ code }}
  43. </q-chip>
  44. </div>
  45. <div v-show="!readonly" class="column q-pa-xs">
  46. <q-icon
  47. v-ripple
  48. :disabled="value[action].length >= maxCodesLength"
  49. name="la la-plus-circle"
  50. class="button bg-green-8 text-white"
  51. @click="addHotKey(action)"
  52. >
  53. <q-tooltip :delay="1000" anchor="top middle" self="bottom middle" content-style="font-size: 80%">
  54. Добавить сочетание клавиш
  55. </q-tooltip>
  56. </q-icon>
  57. <q-icon
  58. v-ripple
  59. name="la la-broom"
  60. class="button text-grey-5"
  61. @click="defaultHotKey(action)"
  62. >
  63. <q-tooltip :delay="1000" anchor="top middle" self="bottom middle" content-style="font-size: 80%">
  64. По умолчанию
  65. </q-tooltip>
  66. </q-icon>
  67. </div>
  68. </div>
  69. </div>
  70. </template>
  71. <script>
  72. //-----------------------------------------------------------------------------
  73. import vueComponent from '../../../vueComponent.js';
  74. import rstore from '../../../../store/modules/reader';
  75. //import * as utils from '../../share/utils';
  76. const componentOptions = {
  77. watch: {
  78. search: function() {
  79. this.updateTableData();
  80. },
  81. modelValue: function() {
  82. this.checkCollisions();
  83. this.updateTableData();
  84. }
  85. },
  86. };
  87. class UserHotKeys {
  88. _options = componentOptions;
  89. _props = {
  90. modelValue: Object,
  91. readonly: Boolean,
  92. };
  93. search = '';
  94. rstore = {};
  95. tableData = [];
  96. collisions = {};
  97. maxCodesLength = 10;
  98. created() {
  99. this.rstore = rstore;
  100. }
  101. mounted() {
  102. this.checkCollisions();
  103. this.updateTableData();
  104. }
  105. get mode() {
  106. return this.$store.state.config.mode;
  107. }
  108. updateTableData() {
  109. let result = rstore.hotKeys.map(hk => hk.name).filter(name => (this.mode == 'liberama.top' || name != 'libs'));
  110. const search = this.search.toLowerCase();
  111. const codesIncludeSearch = (action) => {
  112. for (const code of this.modelValue[action]) {
  113. if (code.toLowerCase().includes(search))
  114. return true;
  115. }
  116. return false;
  117. };
  118. result = result.filter(item => {
  119. return !search ||
  120. rstore.readerActions[item].toLowerCase().includes(search) ||
  121. codesIncludeSearch(item)
  122. });
  123. this.tableData = result;
  124. }
  125. checkCollisions() {
  126. const cols = {};
  127. for (const [action, codes] of Object.entries(this.modelValue)) {
  128. codes.forEach(code => {
  129. if (!cols[code])
  130. cols[code] = [];
  131. if (cols[code].indexOf(action) < 0)
  132. cols[code].push(action);
  133. });
  134. }
  135. const result = {};
  136. for (const [code, actions] of Object.entries(cols)) {
  137. if (actions.length > 1)
  138. result[code] = actions;
  139. }
  140. this.collisions = result;
  141. }
  142. collisionWarning(code) {
  143. if (this.collisions[code]) {
  144. const descs = this.collisions[code].map(action => `<b>${rstore.readerActions[action]}</b>`);
  145. this.$root.stdDialog.alert(`Сочетание '${code}' одновременно назначено<br>следующим командам:<br>${descs.join('<br>')}<br><br>
  146. Возможно неожиданное поведение.`, 'Предупреждение');
  147. }
  148. }
  149. removeCode(action, code) {
  150. let codes = Array.from(this.modelValue[action]);
  151. const index = codes.indexOf(code);
  152. if (index >= 0) {
  153. codes.splice(index, 1);
  154. const newValue = Object.assign({}, this.modelValue, {[action]: codes});
  155. this.$emit('update:modelValue', newValue);
  156. }
  157. }
  158. async addHotKey(action) {
  159. if (this.modelValue[action].length >= this.maxCodesLength)
  160. return;
  161. try {
  162. const result = await this.$root.stdDialog.getHotKey(`Добавить сочетание для:<br><b>${rstore.readerActions[action]}</b>`, '');
  163. if (result) {
  164. let codes = Array.from(this.modelValue[action]);
  165. if (codes.indexOf(result) < 0) {
  166. codes.push(result);
  167. const newValue = Object.assign({}, this.modelValue, {[action]: codes});
  168. this.$emit('update:modelValue', newValue);
  169. this.$nextTick(() => {
  170. this.collisionWarning(result);
  171. });
  172. }
  173. }
  174. } catch (e) {
  175. //
  176. }
  177. }
  178. async defaultHotKey(action) {
  179. try {
  180. if (await this.$root.stdDialog.confirm(`Подтвердите сброс сочетаний клавиш<br>в значения по умолчанию для команды:<br><b>${rstore.readerActions[action]}</b>`, ' ')) {
  181. const codes = Array.from(rstore.settingDefaults.userHotKeys[action]);
  182. const newValue = Object.assign({}, this.modelValue, {[action]: codes});
  183. this.$emit('update:modelValue', newValue);
  184. }
  185. } catch (e) {
  186. //
  187. }
  188. }
  189. async defaultHotKeyAll() {
  190. try {
  191. if (await this.$root.stdDialog.confirm('Подтвердите сброс сочетаний клавиш<br>для ВСЕХ команд в значения по умолчанию:', ' ')) {
  192. const newValue = Object.assign({}, rstore.settingDefaults.userHotKeys);
  193. this.$emit('update:modelValue', newValue);
  194. }
  195. } catch (e) {
  196. //
  197. }
  198. }
  199. }
  200. export default vueComponent(UserHotKeys);
  201. //-----------------------------------------------------------------------------
  202. </script>
  203. <style scoped>
  204. .table {
  205. border-left: 1px solid grey;
  206. border-top: 1px solid grey;
  207. }
  208. .table-row {
  209. border-right: 1px solid grey;
  210. border-bottom: 1px solid grey;
  211. }
  212. .table-row:nth-child(even) {
  213. background-color: #f7f7f7;
  214. }
  215. .table-row:hover {
  216. background-color: #f0f0f0;
  217. }
  218. .desc {
  219. width: 130px;
  220. overflow-wrap: break-word;
  221. word-wrap: break-word;
  222. white-space: normal;
  223. }
  224. .hotKeys {
  225. border-left: 1px solid grey;
  226. }
  227. .button {
  228. font-size: 25px;
  229. border-radius: 25px;
  230. cursor: pointer;
  231. }
  232. </style>