SettingsPage.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. <template>
  2. <Window ref="window" width="600px" @close="close">
  3. <template #header>
  4. Настройки
  5. </template>
  6. <div class="col row">
  7. <a ref="download" style="display: none;" target="_blank"></a>
  8. <div class="full-height">
  9. <q-tabs
  10. ref="tabs"
  11. v-model="selectedTab"
  12. class="bg-grey-3 text-black"
  13. left-icon="la la-caret-up"
  14. right-icon="la la-caret-down"
  15. active-color="white"
  16. active-bg-color="primary"
  17. indicator-color="black"
  18. vertical
  19. no-caps
  20. stretch
  21. inline-label
  22. >
  23. <div v-show="tabsScrollable" class="q-pt-lg" />
  24. <q-tab class="tab" name="profiles" icon="la la-users" label="Профили" />
  25. <q-tab class="tab" name="view" icon="la la-eye" label="Вид" />
  26. <q-tab class="tab" name="toolbar" icon="la la-grip-horizontal" label="Панель" />
  27. <q-tab class="tab" name="keys" icon="la la-gamepad" label="Управление" />
  28. <q-tab class="tab" name="pagemove" icon="la la-school" label="Листание" />
  29. <q-tab class="tab" name="convert" icon="la la-magic" label="Конвертир." />
  30. <q-tab class="tab" name="update" icon="la la-sync" label="Обновление" />
  31. <q-tab class="tab" name="others" icon="la la-list-ul" label="Прочее" />
  32. <q-tab class="tab" name="reset" icon="la la-broom" label="Сброс" />
  33. <div v-show="tabsScrollable" class="q-pt-lg" />
  34. </q-tabs>
  35. </div>
  36. <div class="col fit">
  37. <!-- Профили --------------------------------------------------------------------->
  38. <ProfilesTab v-if="selectedTab == 'profiles'" :form="form" />
  39. <!-- Вид ------------------------------------------------------------------------->
  40. <!--div v-if="selectedTab == 'view'" class="fit column">
  41. <q-tabs
  42. v-model="selectedViewTab"
  43. active-color="black"
  44. active-bg-color="white"
  45. indicator-color="white"
  46. dense
  47. no-caps
  48. class="no-mp bg-grey-4 text-grey-7"
  49. >
  50. <q-tab name="mode" label="Режим" />
  51. <q-tab name="color" label="Цвет" />
  52. <q-tab name="font" label="Шрифт" />
  53. <q-tab name="text" label="Текст" />
  54. <q-tab name="status" label="Строка статуса" />
  55. </q-tabs>
  56. <div class="q-mb-sm" />
  57. <div class="col tab-panel">
  58. <div v-if="selectedViewTab == 'mode'">
  59. @@include('./ViewTab/Mode.inc');
  60. </div>
  61. <div v-if="selectedViewTab == 'color'">
  62. @@include('./ViewTab/Color.inc');
  63. </div>
  64. <div v-if="selectedViewTab == 'font'">
  65. @@include('./ViewTab/Font.inc');
  66. </div>
  67. <div v-if="selectedViewTab == 'text'">
  68. @@include('./ViewTab/Text.inc');
  69. </div>
  70. <div v-if="selectedViewTab == 'status'">
  71. @@include('./ViewTab/Status.inc');
  72. </div>
  73. </div>
  74. </div-->
  75. <!-- Кнопки ---------------------------------------------------------------------->
  76. <ToolBarTab v-if="selectedTab == 'toolbar'" :form="form" />
  77. <!-- Управление ------------------------------------------------------------------>
  78. <KeysTab v-if="selectedTab == 'keys'" :form="form" />
  79. <!-- Листание -------------------------------------------------------------------->
  80. <!--div v-if="selectedTab == 'pagemove'" class="fit tab-panel">
  81. @@include('./PageMoveTab.inc');
  82. </div-->
  83. <!-- Конвертирование ------------------------------------------------------------->
  84. <!--div v-if="selectedTab == 'convert'" class="fit tab-panel">
  85. @@include('./ConvertTab.inc');
  86. </div-->
  87. <!-- Обновление ------------------------------------------------------------------>
  88. <!--div v-if="selectedTab == 'update'" class="fit tab-panel">
  89. @@include('./UpdateTab.inc');
  90. </div-->
  91. <!-- Прочее ---------------------------------------------------------------------->
  92. <!--div v-if="selectedTab == 'others'" class="fit tab-panel">
  93. @@include('./OthersTab.inc');
  94. </div-->
  95. <!-- Сброс ----------------------------------------------------------------------->
  96. <!--div v-if="selectedTab == 'reset'" class="fit tab-panel">
  97. @@include('./ResetTab.inc');
  98. </div-->
  99. </div>
  100. </div>
  101. </Window>
  102. </template>
  103. <script>
  104. //-----------------------------------------------------------------------------
  105. import vueComponent from '../../vueComponent.js';
  106. import _ from 'lodash';
  107. //stuff
  108. import * as utils from '../../../share/utils';
  109. import * as cryptoUtils from '../../../share/cryptoUtils';
  110. import Window from '../../share/Window.vue';
  111. import NumInput from '../../share/NumInput.vue';
  112. import wallpaperStorage from '../share/wallpaperStorage';
  113. import readerApi from '../../../api/reader';
  114. import rstore from '../../../store/modules/reader';
  115. import defPalette from './defPalette';
  116. //pages
  117. import ProfilesTab from './ProfilesTab/ProfilesTab.vue';
  118. import ToolBarTab from './ToolBarTab/ToolBarTab.vue';
  119. import KeysTab from './KeysTab/KeysTab.vue';
  120. const hex = /^#[0-9a-fA-F]{3}([0-9a-fA-F]{3})?$/;
  121. const componentOptions = {
  122. components: {
  123. Window,
  124. NumInput,
  125. //pages
  126. ProfilesTab,
  127. ToolBarTab,
  128. KeysTab,
  129. },
  130. watch: {
  131. settings: function() {
  132. this.settingsChanged();//no await
  133. },
  134. form: {
  135. handler(newValue) {
  136. if (this.inited && !this.setsChanged) {
  137. this.commit('reader/setSettings', _.cloneDeep(newValue));
  138. }
  139. },
  140. deep: true,
  141. },
  142. fontBold: function(newValue) {
  143. this.fontWeight = (newValue ? 'bold' : '');
  144. },
  145. fontItalic: function(newValue) {
  146. this.fontStyle = (newValue ? 'italic' : '');
  147. },
  148. vertShift: function(newValue) {
  149. const font = (this.webFontName ? this.webFontName : this.fontName);
  150. if (this.fontShifts[font] != newValue || this.fontVertShift != newValue) {
  151. this.fontShifts = Object.assign({}, this.fontShifts, {[font]: newValue});
  152. this.fontVertShift = newValue;
  153. }
  154. },
  155. fontName: function(newValue) {
  156. const font = (this.webFontName ? this.webFontName : newValue);
  157. this.vertShift = this.fontShifts[font] || 0;
  158. },
  159. webFontName: function(newValue) {
  160. const font = (newValue ? newValue : this.fontName);
  161. this.vertShift = this.fontShifts[font] || 0;
  162. },
  163. wallpaper: function(newValue) {
  164. if (newValue != '' && this.pageChangeAnimation == 'flip')
  165. this.pageChangeAnimation = '';
  166. },
  167. dualPageMode(newValue) {
  168. if (newValue && this.pageChangeAnimation == 'flip' || this.pageChangeAnimation == 'rightShift')
  169. this.pageChangeAnimation = '';
  170. },
  171. textColor: function(newValue) {
  172. this.textColorFiltered = newValue;
  173. },
  174. textColorFiltered: function(newValue) {
  175. if (hex.test(newValue))
  176. this.textColor = newValue;
  177. },
  178. backgroundColor: function(newValue) {
  179. this.bgColorFiltered = newValue;
  180. },
  181. bgColorFiltered: function(newValue) {
  182. if (hex.test(newValue))
  183. this.backgroundColor = newValue;
  184. },
  185. dualDivColor(newValue) {
  186. this.dualDivColorFiltered = newValue;
  187. },
  188. dualDivColorFiltered(newValue) {
  189. if (hex.test(newValue))
  190. this.dualDivColor = newValue;
  191. },
  192. statusBarColor(newValue) {
  193. this.statusBarColorFiltered = newValue;
  194. },
  195. statusBarColorFiltered(newValue) {
  196. if (hex.test(newValue))
  197. this.statusBarColor = newValue;
  198. },
  199. },
  200. };
  201. class SettingsPage {
  202. _options = componentOptions;
  203. form = {};
  204. selectedTab = 'profiles';
  205. selectedViewTab = 'mode';
  206. fontBold = false;
  207. fontItalic = false;
  208. vertShift = 0;
  209. tabsScrollable = false;
  210. textColorFiltered = '';
  211. bgColorFiltered = '';
  212. dualDivColorFiltered = '';
  213. statusBarColorFiltered = '';
  214. webFonts = [];
  215. fonts = [];
  216. setsChanged = false;
  217. created() {
  218. this.commit = this.$store.commit;
  219. this.reader = this.$store.state.reader;
  220. this.settingsChanged();//no await
  221. }
  222. mounted() {
  223. this.$watch(
  224. '$refs.tabs.scrollable',
  225. (newValue) => {
  226. this.tabsScrollable = newValue && !this.$root.isMobileDevice;
  227. }
  228. );
  229. }
  230. init() {
  231. this.$refs.window.init();
  232. this.inited = true;
  233. }
  234. async settingsChanged() {
  235. if (_.isEqual(this.form, this.settings))
  236. return;
  237. this.setsChanged = true;
  238. try {
  239. this.form = _.cloneDeep(this.settings);
  240. const form = this.form;
  241. this.fontBold = (form.fontWeight == 'bold');
  242. this.fontItalic = (form.fontStyle == 'italic');
  243. this.fonts = rstore.fonts;
  244. this.webFonts = rstore.webFonts;
  245. const font = (form.webFontName ? form.webFontName : form.fontName);
  246. this.vertShift = form.fontShifts[font] || 0;
  247. this.textColorFiltered = form.textColor;
  248. this.bgColorFiltered = form.backgroundColor;
  249. this.dualDivColorFiltered = form.dualDivColor;
  250. this.statusBarColorFiltered = form.statusBarColor;
  251. } finally {
  252. await this.$nextTick();
  253. this.setsChanged = false;
  254. }
  255. }
  256. get mode() {
  257. return this.$store.state.config.mode;
  258. }
  259. get isExternalConverter() {
  260. return this.$store.state.config.useExternalBookConverter;
  261. }
  262. get settings() {
  263. return this.$store.state.reader.settings;
  264. }
  265. get configBucEnabled() {
  266. return this.$store.state.config.bucEnabled;
  267. }
  268. get wallpaperOptions() {
  269. let result = [{label: 'Нет', value: ''}];
  270. const userWallpapers = _.cloneDeep(this.userWallpapers);
  271. userWallpapers.sort((a, b) => a.label.localeCompare(b.label));
  272. for (const wp of userWallpapers) {
  273. if (wallpaperStorage.keyExists(wp.cssClass))
  274. result.push({label: wp.label, value: wp.cssClass});
  275. }
  276. for (let i = 1; i <= 17; i++) {
  277. result.push({label: i, value: `paper${i}`});
  278. }
  279. return result;
  280. }
  281. get fontsOptions() {
  282. let result = [];
  283. this.fonts.forEach(font => {
  284. result.push({label: (font.label ? font.label : font.name), value: font.name});
  285. });
  286. return result;
  287. }
  288. get webFontsOptions() {
  289. let result = [{label: 'Нет', value: ''}];
  290. this.webFonts.forEach(font => {
  291. result.push({label: font.name, value: font.name});
  292. });
  293. return result;
  294. }
  295. get pageChangeAnimationOptions() {
  296. let result = [
  297. {label: 'Нет', value: ''},
  298. {label: 'Вверх-вниз', value: 'downShift'},
  299. (!this.dualPageMode ? {label: 'Вправо-влево', value: 'rightShift'} : null),
  300. {label: 'Протаивание', value: 'thaw'},
  301. {label: 'Мерцание', value: 'blink'},
  302. {label: 'Вращение', value: 'rotate'},
  303. (this.wallpaper == '' && !this.dualPageMode ? {label: 'Листание', value: 'flip'} : null),
  304. ];
  305. result = result.filter(v => v);
  306. return result;
  307. }
  308. get predefineTextColors() {
  309. return defPalette.concat([
  310. '#ffffff',
  311. '#000000',
  312. '#202020',
  313. '#323232',
  314. '#aaaaaa',
  315. '#00c0c0',
  316. '#ebe2c9',
  317. '#cfdc99',
  318. '#478355',
  319. '#909080',
  320. ]);
  321. }
  322. get predefineBackgroundColors() {
  323. return defPalette.concat([
  324. '#ffffff',
  325. '#000000',
  326. '#202020',
  327. '#ebe2c9',
  328. '#cfdc99',
  329. '#478355',
  330. '#a6caf0',
  331. '#909080',
  332. '#808080',
  333. '#c8c8c8',
  334. ]);
  335. }
  336. colorPanStyle(type) {
  337. let result = 'width: 30px; height: 30px; border: 1px solid black; border-radius: 4px;';
  338. switch (type) {
  339. case 'text':
  340. result += `background-color: ${this.textColor};`
  341. break;
  342. case 'bg':
  343. result += `background-color: ${this.backgroundColor};`
  344. break;
  345. case 'div':
  346. result += `background-color: ${this.dualDivColor};`
  347. break;
  348. case 'statusbar':
  349. result += `background-color: ${this.statusBarColor};`
  350. break;
  351. }
  352. return result;
  353. }
  354. needReload() {
  355. this.$root.notify.warning('Необходимо обновить страницу (F5), чтобы изменения возымели эффект');
  356. }
  357. needTextReload() {
  358. this.$root.notify.warning('Необходимо обновить книгу в обход кэша, чтобы изменения возымели эффект');
  359. }
  360. close() {
  361. this.$emit('do-action', {action: 'settings'});
  362. }
  363. async setDefaults() {
  364. try {
  365. if (await this.$root.stdDialog.confirm('Подтвердите установку настроек по умолчанию:', ' ')) {
  366. this.form = Object.assign({}, rstore.settingDefaults);
  367. for (let prop in rstore.settingDefaults) {
  368. this[prop] = this.form[prop];
  369. }
  370. }
  371. } catch (e) {
  372. //
  373. }
  374. }
  375. loadWallpaperFileClick() {
  376. this.$refs.file.click();
  377. }
  378. loadWallpaperFile() {
  379. const file = this.$refs.file.files[0];
  380. if (file.size > 10*1024*1024) {
  381. this.$root.stdDialog.alert('Файл обоев не должен превышать в размере 10Mb', 'Ошибка');
  382. return;
  383. }
  384. if (file.type != 'image/png' && file.type != 'image/jpeg') {
  385. this.$root.stdDialog.alert('Файл обоев должен иметь тип PNG или JPEG', 'Ошибка');
  386. return;
  387. }
  388. if (this.userWallpapers.length >= 100) {
  389. this.$root.stdDialog.alert('Превышено максимальное количество пользовательских обоев.', 'Ошибка');
  390. return;
  391. }
  392. this.$refs.file.value = '';
  393. if (file) {
  394. const reader = new FileReader();
  395. reader.onload = (e) => {
  396. (async() => {
  397. const data = e.target.result;
  398. const key = utils.toHex(cryptoUtils.sha256(data));
  399. const label = `#${key.substring(0, 4)}`;
  400. const cssClass = `user-paper${key}`;
  401. const newUserWallpapers = _.cloneDeep(this.userWallpapers);
  402. const index = _.findIndex(newUserWallpapers, (item) => (item.cssClass == cssClass));
  403. if (index < 0)
  404. newUserWallpapers.push({label, cssClass});
  405. if (!wallpaperStorage.keyExists(cssClass)) {
  406. await wallpaperStorage.setData(cssClass, data);
  407. //отправим data на сервер в файл `/upload/${key}`
  408. try {
  409. //const res =
  410. await readerApi.uploadFileBuf(data);
  411. //console.log(res);
  412. } catch (e) {
  413. console.error(e);
  414. }
  415. }
  416. this.userWallpapers = newUserWallpapers;
  417. this.wallpaper = cssClass;
  418. })();
  419. }
  420. reader.readAsDataURL(file);
  421. }
  422. }
  423. async delWallpaper() {
  424. if (this.wallpaper.indexOf('user-paper') == 0) {
  425. const newUserWallpapers = [];
  426. for (const wp of this.userWallpapers) {
  427. if (wp.cssClass != this.wallpaper) {
  428. newUserWallpapers.push(wp);
  429. }
  430. }
  431. await wallpaperStorage.removeData(this.wallpaper);
  432. this.userWallpapers = newUserWallpapers;
  433. this.wallpaper = '';
  434. }
  435. }
  436. async downloadWallpaper() {
  437. if (this.wallpaper.indexOf('user-paper') != 0)
  438. return;
  439. try {
  440. const d = this.$refs.download;
  441. const dataUrl = await wallpaperStorage.getData(this.wallpaper);
  442. if (!dataUrl)
  443. throw new Error('Файл обоев не найден');
  444. d.href = dataUrl;
  445. d.download = `wallpaper-#${this.wallpaper.replace('user-paper', '').substring(0, 4)}`;
  446. d.click();
  447. } catch (e) {
  448. this.$root.stdDialog.alert(e.message, 'Ошибка', {color: 'negative'});
  449. }
  450. }
  451. keyHook(event) {
  452. if (!this.$root.stdDialog.active && event.type == 'keydown' && event.key == 'Escape') {
  453. this.close();
  454. }
  455. return true;
  456. }
  457. }
  458. export default vueComponent(SettingsPage);
  459. //-----------------------------------------------------------------------------
  460. </script>
  461. <style scoped>
  462. .tab {
  463. justify-content: initial;
  464. }
  465. .label-7 {
  466. width: 75px;
  467. }
  468. .label-2, .label-5 {
  469. width: 110px;
  470. }
  471. .label-6 {
  472. width: 100px;
  473. }
  474. .input {
  475. max-width: 150px;
  476. }
  477. .no-mp {
  478. margin: 0;
  479. padding: 0;
  480. }
  481. .col-left {
  482. width: 150px;
  483. }
  484. </style>
  485. <style>
  486. .sets-tab-panel {
  487. overflow-x: hidden;
  488. overflow-y: auto;
  489. font-size: 90%;
  490. padding: 0 10px 15px 10px;
  491. }
  492. .sets-part-header {
  493. border-top: 2px solid #bbbbbb;
  494. font-weight: bold;
  495. font-size: 110%;
  496. margin-top: 15px;
  497. margin-bottom: 5px;
  498. }
  499. .sets-label {
  500. display: flex;
  501. flex-direction: column;
  502. justify-content: center;
  503. text-align: right;
  504. margin-right: 10px;
  505. overflow: hidden;
  506. }
  507. .sets-item {
  508. width: 100%;
  509. margin-top: 5px;
  510. margin-bottom: 5px;
  511. }
  512. .sets-button {
  513. margin: 3px 15px 3px 0;
  514. padding: 0 5px 0 5px;
  515. }
  516. </style>