SettingsPage.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. <q-tab class="tab" name="profiles" icon="la la-users" label="Профили" />
  24. <q-tab class="tab" name="view" icon="la la-eye" label="Вид" />
  25. <q-tab class="tab" name="toolbar" icon="la la-grip-horizontal" label="Панель" />
  26. <q-tab class="tab" name="keys" icon="la la-gamepad" label="Управление" />
  27. <q-tab class="tab" name="pagemove" icon="la la-school" label="Листание" />
  28. <q-tab class="tab" name="convert" icon="la la-magic" label="Конвертир." />
  29. <q-tab class="tab" name="update" icon="la la-retweet" label="Обновление" />
  30. <q-tab class="tab" name="others" icon="la la-list-ul" label="Прочее" />
  31. <q-tab class="tab" name="reset" icon="la la-broom" label="Сброс" />
  32. </q-tabs>
  33. </div>
  34. <div class="col fit">
  35. <!-- Профили --------------------------------------------------------------------->
  36. <ProfilesTab v-if="selectedTab == 'profiles'" :form="form" />
  37. <!-- Вид ------------------------------------------------------------------------->
  38. <ViewTab v-if="selectedTab == 'view'" :form="form" />
  39. <!-- Кнопки ---------------------------------------------------------------------->
  40. <ToolBarTab v-if="selectedTab == 'toolbar'" :form="form" />
  41. <!-- Управление ------------------------------------------------------------------>
  42. <KeysTab v-if="selectedTab == 'keys'" :form="form" />
  43. <!-- Листание -------------------------------------------------------------------->
  44. <PageMoveTab v-if="selectedTab == 'pagemove'" :form="form" />
  45. <!-- Конвертирование ------------------------------------------------------------->
  46. <ConvertTab v-if="selectedTab == 'convert'" :form="form" />
  47. <!-- Обновление ------------------------------------------------------------------>
  48. <UpdateTab v-if="selectedTab == 'update'" :form="form" />
  49. <!-- Прочее ---------------------------------------------------------------------->
  50. <OthersTab v-if="selectedTab == 'others'" :form="form" />
  51. <!-- Сброс ----------------------------------------------------------------------->
  52. <ResetTab v-if="selectedTab == 'reset'" :form="form" @tab-event="tabEvent" />
  53. </div>
  54. </div>
  55. </Window>
  56. </template>
  57. <script>
  58. //-----------------------------------------------------------------------------
  59. import vueComponent from '../../vueComponent.js';
  60. import _ from 'lodash';
  61. //stuff
  62. import * as utils from '../../../share/utils';
  63. import * as cryptoUtils from '../../../share/cryptoUtils';
  64. import Window from '../../share/Window.vue';
  65. import wallpaperStorage from '../share/wallpaperStorage';
  66. import readerApi from '../../../api/reader';
  67. import rstore from '../../../store/modules/reader';
  68. //pages
  69. import ProfilesTab from './ProfilesTab/ProfilesTab.vue';
  70. import ViewTab from './ViewTab/ViewTab.vue';
  71. import ToolBarTab from './ToolBarTab/ToolBarTab.vue';
  72. import KeysTab from './KeysTab/KeysTab.vue';
  73. import PageMoveTab from './PageMoveTab/PageMoveTab.vue';
  74. import ConvertTab from './ConvertTab/ConvertTab.vue';
  75. import UpdateTab from './UpdateTab/UpdateTab.vue';
  76. import OthersTab from './OthersTab/OthersTab.vue';
  77. import ResetTab from './ResetTab/ResetTab.vue';
  78. const hex = /^#[0-9a-fA-F]{3}([0-9a-fA-F]{3})?$/;
  79. const componentOptions = {
  80. components: {
  81. Window,
  82. //pages
  83. ProfilesTab,
  84. ViewTab,
  85. ToolBarTab,
  86. KeysTab,
  87. PageMoveTab,
  88. ConvertTab,
  89. UpdateTab,
  90. OthersTab,
  91. ResetTab,
  92. },
  93. watch: {
  94. settings: function() {
  95. this.settingsChanged();//no await
  96. },
  97. form: {
  98. handler() {
  99. if (this.inited && !this.setsChanged) {
  100. this.debouncedCommitSettings();
  101. }
  102. },
  103. deep: true,
  104. },
  105. fontBold: function(newValue) {
  106. this.fontWeight = (newValue ? 'bold' : '');
  107. },
  108. fontItalic: function(newValue) {
  109. this.fontStyle = (newValue ? 'italic' : '');
  110. },
  111. vertShift: function(newValue) {
  112. const font = (this.webFontName ? this.webFontName : this.fontName);
  113. if (this.fontShifts[font] != newValue || this.fontVertShift != newValue) {
  114. this.fontShifts = Object.assign({}, this.fontShifts, {[font]: newValue});
  115. this.fontVertShift = newValue;
  116. }
  117. },
  118. fontName: function(newValue) {
  119. const font = (this.webFontName ? this.webFontName : newValue);
  120. this.vertShift = this.fontShifts[font] || 0;
  121. },
  122. webFontName: function(newValue) {
  123. const font = (newValue ? newValue : this.fontName);
  124. this.vertShift = this.fontShifts[font] || 0;
  125. },
  126. wallpaper: function(newValue) {
  127. if (newValue != '' && this.pageChangeAnimation == 'flip')
  128. this.pageChangeAnimation = '';
  129. },
  130. /*this.$watch('form.dualPageMode', (newValue) => {
  131. console.log(newValue);
  132. })*/
  133. dualPageMode(newValue) {
  134. if (newValue && this.pageChangeAnimation == 'flip' || this.pageChangeAnimation == 'rightShift')
  135. this.pageChangeAnimation = '';
  136. },
  137. textColor: function(newValue) {
  138. this.textColorFiltered = newValue;
  139. },
  140. textColorFiltered: function(newValue) {
  141. if (hex.test(newValue))
  142. this.textColor = newValue;
  143. },
  144. backgroundColor: function(newValue) {
  145. this.bgColorFiltered = newValue;
  146. },
  147. bgColorFiltered: function(newValue) {
  148. if (hex.test(newValue))
  149. this.backgroundColor = newValue;
  150. },
  151. statusBarColor(newValue) {
  152. this.statusBarColorFiltered = newValue;
  153. },
  154. statusBarColorFiltered(newValue) {
  155. if (hex.test(newValue))
  156. this.statusBarColor = newValue;
  157. },
  158. },
  159. };
  160. class SettingsPage {
  161. _options = componentOptions;
  162. form = {};
  163. selectedTab = 'profiles';
  164. setsChanged = false;
  165. fontBold = false;
  166. fontItalic = false;
  167. vertShift = 0;
  168. textColorFiltered = '';
  169. bgColorFiltered = '';
  170. statusBarColorFiltered = '';
  171. webFonts = [];
  172. fonts = [];
  173. created() {
  174. this.commit = this.$store.commit;
  175. this.debouncedCommitSettings = _.debounce(() => {
  176. this.commit('reader/setSettings', _.cloneDeep(this.form));
  177. }, 50);
  178. this.settingsChanged();//no await
  179. }
  180. mounted() {
  181. }
  182. init() {
  183. this.$refs.window.init();
  184. this.inited = true;
  185. }
  186. async settingsChanged() {
  187. this.setsChanged = true;
  188. try {
  189. this.form = _.cloneDeep(this.settings);
  190. const form = this.form;
  191. this.fontBold = (form.fontWeight == 'bold');
  192. this.fontItalic = (form.fontStyle == 'italic');
  193. this.fonts = rstore.fonts;
  194. this.webFonts = rstore.webFonts;
  195. const font = (form.webFontName ? form.webFontName : form.fontName);
  196. this.vertShift = form.fontShifts[font] || 0;
  197. this.textColorFiltered = form.textColor;
  198. this.bgColorFiltered = form.backgroundColor;
  199. this.dualDivColorFiltered = form.dualDivColor;
  200. this.statusBarColorFiltered = form.statusBarColor;
  201. } finally {
  202. await this.$nextTick();
  203. this.setsChanged = false;
  204. }
  205. }
  206. get settings() {
  207. return this.$store.state.reader.settings;
  208. }
  209. get wallpaperOptions() {
  210. let result = [{label: 'Нет', value: ''}];
  211. const userWallpapers = _.cloneDeep(this.userWallpapers);
  212. userWallpapers.sort((a, b) => a.label.localeCompare(b.label));
  213. for (const wp of userWallpapers) {
  214. if (wallpaperStorage.keyExists(wp.cssClass))
  215. result.push({label: wp.label, value: wp.cssClass});
  216. }
  217. for (let i = 1; i <= 17; i++) {
  218. result.push({label: i, value: `paper${i}`});
  219. }
  220. return result;
  221. }
  222. get fontsOptions() {
  223. let result = [];
  224. this.fonts.forEach(font => {
  225. result.push({label: (font.label ? font.label : font.name), value: font.name});
  226. });
  227. return result;
  228. }
  229. get webFontsOptions() {
  230. let result = [{label: 'Нет', value: ''}];
  231. this.webFonts.forEach(font => {
  232. result.push({label: font.name, value: font.name});
  233. });
  234. return result;
  235. }
  236. needReload() {
  237. this.$root.notify.warning('Необходимо обновить страницу (F5), чтобы изменения возымели эффект');
  238. }
  239. needTextReload() {
  240. this.$root.notify.warning('Необходимо обновить книгу в обход кэша, чтобы изменения возымели эффект');
  241. }
  242. close() {
  243. this.$emit('do-action', {action: 'settings'});
  244. }
  245. async setDefaults() {
  246. try {
  247. if (await this.$root.stdDialog.confirm('Подтвердите установку настроек по умолчанию:', ' ')) {
  248. this.form = _.cloneDeep(rstore.settingDefaults);
  249. }
  250. } catch (e) {
  251. //
  252. }
  253. }
  254. tabEvent(event) {
  255. if (!event || !event.action)
  256. return;
  257. switch (event.action) {
  258. case 'set-defaults': this.setDefaults(); break;
  259. }
  260. }
  261. loadWallpaperFileClick() {
  262. this.$refs.file.click();
  263. }
  264. loadWallpaperFile() {
  265. const file = this.$refs.file.files[0];
  266. if (file.size > 10*1024*1024) {
  267. this.$root.stdDialog.alert('Файл обоев не должен превышать в размере 10Mb', 'Ошибка');
  268. return;
  269. }
  270. if (file.type != 'image/png' && file.type != 'image/jpeg') {
  271. this.$root.stdDialog.alert('Файл обоев должен иметь тип PNG или JPEG', 'Ошибка');
  272. return;
  273. }
  274. if (this.userWallpapers.length >= 100) {
  275. this.$root.stdDialog.alert('Превышено максимальное количество пользовательских обоев.', 'Ошибка');
  276. return;
  277. }
  278. this.$refs.file.value = '';
  279. if (file) {
  280. const reader = new FileReader();
  281. reader.onload = (e) => {
  282. (async() => {
  283. const data = e.target.result;
  284. const key = utils.toHex(cryptoUtils.sha256(data));
  285. const label = `#${key.substring(0, 4)}`;
  286. const cssClass = `user-paper${key}`;
  287. const newUserWallpapers = _.cloneDeep(this.userWallpapers);
  288. const index = _.findIndex(newUserWallpapers, (item) => (item.cssClass == cssClass));
  289. if (index < 0)
  290. newUserWallpapers.push({label, cssClass});
  291. if (!wallpaperStorage.keyExists(cssClass)) {
  292. await wallpaperStorage.setData(cssClass, data);
  293. //отправим data на сервер в файл `/upload/${key}`
  294. try {
  295. //const res =
  296. await readerApi.uploadFileBuf(data);
  297. //console.log(res);
  298. } catch (e) {
  299. console.error(e);
  300. }
  301. }
  302. this.userWallpapers = newUserWallpapers;
  303. this.wallpaper = cssClass;
  304. })();
  305. }
  306. reader.readAsDataURL(file);
  307. }
  308. }
  309. async delWallpaper() {
  310. if (this.wallpaper.indexOf('user-paper') == 0) {
  311. const newUserWallpapers = [];
  312. for (const wp of this.userWallpapers) {
  313. if (wp.cssClass != this.wallpaper) {
  314. newUserWallpapers.push(wp);
  315. }
  316. }
  317. await wallpaperStorage.removeData(this.wallpaper);
  318. this.userWallpapers = newUserWallpapers;
  319. this.wallpaper = '';
  320. }
  321. }
  322. async downloadWallpaper() {
  323. if (this.wallpaper.indexOf('user-paper') != 0)
  324. return;
  325. try {
  326. const d = this.$refs.download;
  327. const dataUrl = await wallpaperStorage.getData(this.wallpaper);
  328. if (!dataUrl)
  329. throw new Error('Файл обоев не найден');
  330. d.href = dataUrl;
  331. d.download = `wallpaper-#${this.wallpaper.replace('user-paper', '').substring(0, 4)}`;
  332. d.click();
  333. } catch (e) {
  334. this.$root.stdDialog.alert(e.message, 'Ошибка', {color: 'negative'});
  335. }
  336. }
  337. keyHook(event) {
  338. if (!this.$root.stdDialog.active && event.type == 'keydown' && event.key == 'Escape') {
  339. this.close();
  340. }
  341. return true;
  342. }
  343. }
  344. export default vueComponent(SettingsPage);
  345. //-----------------------------------------------------------------------------
  346. </script>
  347. <style scoped>
  348. .tab {
  349. justify-content: initial;
  350. }
  351. </style>
  352. <style>
  353. .sets-tab-panel {
  354. overflow-x: hidden;
  355. overflow-y: auto;
  356. font-size: 90%;
  357. padding: 0 10px 15px 10px;
  358. }
  359. .sets-part-header {
  360. border-top: 2px solid #bbbbbb;
  361. font-weight: bold;
  362. font-size: 110%;
  363. margin-top: 15px;
  364. margin-bottom: 5px;
  365. }
  366. .sets-label {
  367. display: flex;
  368. flex-direction: column;
  369. justify-content: center;
  370. text-align: right;
  371. margin-right: 10px;
  372. overflow: hidden;
  373. }
  374. .sets-item {
  375. width: 100%;
  376. margin-top: 5px;
  377. margin-bottom: 5px;
  378. }
  379. .sets-button {
  380. margin: 3px 15px 3px 0;
  381. padding: 0 5px 0 5px;
  382. }
  383. </style>