UserHotKeys.vue 8.5 KB

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