index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. const { readFileSync } = require('fs');
  2. /** 包名 */
  3. const PACKAGE_NAME = 'ccc-png-auto-compress';
  4. /**
  5. * i18n
  6. * @param {string} key
  7. * @returns {string}
  8. */
  9. const translate = (key) => Editor.T(`${PACKAGE_NAME}.${key}`);
  10. /** 扩展名 */
  11. const EXTENSION_NAME = translate('name');
  12. // 注册面板
  13. Editor.Panel.extend({
  14. /** HTML */
  15. template: readFileSync(Editor.url(`packages://${PACKAGE_NAME}/panel.setting/index.html`), 'utf8'),
  16. /** 样式 */
  17. style: readFileSync(Editor.url(`packages://${PACKAGE_NAME}/panel.setting/index.css`), 'utf8'),
  18. /**
  19. * 当面板渲染成功后触发
  20. */
  21. ready() {
  22. // 创建 Vue 实例
  23. const app = new window.Vue({
  24. /**
  25. * 挂载目标
  26. * @type {string | Element}
  27. */
  28. el: this.shadowRoot,
  29. /**
  30. * 数据对象
  31. */
  32. data: {
  33. // 多语言文本
  34. titleLabel: translate('setting'),
  35. repositoryLabel: translate('repository'),
  36. applyLabel: translate('apply'),
  37. // 配置
  38. enabled: false,
  39. excludeFolders: '',
  40. excludeFiles: '',
  41. // 参数
  42. minQuality: 40,
  43. maxQuality: 80,
  44. colors: 256,
  45. speed: 3,
  46. // 按钮状态
  47. isProcessing: false,
  48. },
  49. /**
  50. * 实例函数
  51. * @type {{ [key: string]: Function }}
  52. */
  53. methods: {
  54. /**
  55. * 应用按钮回调
  56. * @param {*} event
  57. */
  58. onApplyBtnClick(event) {
  59. this.saveConfig();
  60. },
  61. /**
  62. * 读取配置
  63. */
  64. readConfig() {
  65. Editor.Ipc.sendToMain(`${PACKAGE_NAME}:read-config`, (error, config) => {
  66. if (error || !config) return;
  67. for (const key in config) {
  68. const value = config[key];
  69. if (Array.isArray(value)) {
  70. this[key] = value.join(',').replace(/,/g, ',\n');
  71. } else {
  72. this[key] = value;
  73. }
  74. }
  75. });
  76. },
  77. /**
  78. * 保存配置
  79. */
  80. saveConfig() {
  81. if (this.isProcessing) return;
  82. this.isProcessing = true;
  83. // 配置
  84. const excludeFolders = this.excludeFolders.split(',').map(value => value.trim()),
  85. excludeFiles = this.excludeFiles.split(',').map(value => value.trim()),
  86. config = {
  87. enabled: this.enabled,
  88. excludeFolders,
  89. excludeFiles,
  90. // 参数
  91. minQuality: this.minQuality,
  92. maxQuality: this.maxQuality,
  93. colors: this.colors,
  94. speed: this.speed,
  95. };
  96. // 发消息给主进程保存配置
  97. Editor.Ipc.sendToMain(`${PACKAGE_NAME}:save-config`, config, () => {
  98. this.isProcessing = false;
  99. });
  100. },
  101. },
  102. });
  103. // 读取配置
  104. app.readConfig();
  105. }
  106. });