workflow.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { Model } from '@vuex-orm/core';
  2. import { nanoid } from 'nanoid';
  3. import browser from 'webextension-polyfill';
  4. import Log from './log';
  5. class Workflow extends Model {
  6. static entity = 'workflows';
  7. static primaryKey = 'id';
  8. static autoSave = true;
  9. static fields() {
  10. return {
  11. id: this.uid(() => nanoid()),
  12. name: this.string(''),
  13. icon: this.string('riGlobalLine'),
  14. data: this.attr(null),
  15. drawflow: this.attr(''),
  16. dataColumns: this.attr([]),
  17. description: this.string(''),
  18. pass: this.string(''),
  19. trigger: this.attr(null),
  20. isProtected: this.boolean(false),
  21. version: this.string(''),
  22. globalData: this.string('[{ "key": "value" }]'),
  23. createdAt: this.number(Date.now()),
  24. isDisabled: this.boolean(false),
  25. settings: this.attr({
  26. blockDelay: 0,
  27. saveLog: true,
  28. debugMode: false,
  29. onError: 'stop-workflow',
  30. executedBlockOnWeb: false,
  31. }),
  32. logs: this.hasMany(Log, 'workflowId'),
  33. };
  34. }
  35. static async insert(payload) {
  36. const res = await super.insert(payload);
  37. await this.store().dispatch('saveToStorage', 'workflows');
  38. return res;
  39. }
  40. static async afterDelete({ id }) {
  41. try {
  42. const { visitWebTriggers, shortcuts } = await browser.storage.local.get([
  43. 'visitWebTriggers',
  44. 'shortcuts',
  45. ]);
  46. const index = visitWebTriggers.findIndex((item) => item.id === id);
  47. if (index !== -1) {
  48. visitWebTriggers.splice(index, 1);
  49. }
  50. const keyboardShortcuts = shortcuts || {};
  51. delete keyboardShortcuts[id];
  52. await browser.storage.local.set({
  53. visitWebTriggers,
  54. shortcuts: keyboardShortcuts,
  55. });
  56. await browser.alarms.clear(id);
  57. } catch (error) {
  58. console.error(error);
  59. }
  60. }
  61. }
  62. export default Workflow;