workflow-data.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import Workflow from '@/models/workflow';
  2. import { parseJSON, fileSaver, openFilePicker, isObject } from './helper';
  3. export function importWorkflow(attrs = {}) {
  4. openFilePicker(['application/json'], attrs)
  5. .then((files) => {
  6. const getDrawflow = ({ drawflow }) => {
  7. if (isObject(drawflow)) return JSON.stringify(drawflow);
  8. return drawflow;
  9. };
  10. const handleOnLoadReader = ({ target }) => {
  11. const workflow = JSON.parse(target.result);
  12. if (workflow.includedWorkflows) {
  13. Object.keys(workflow.includedWorkflows).forEach((workflowId) => {
  14. const isWorkflowExists = Workflow.query()
  15. .where('id', workflowId)
  16. .exists();
  17. if (isWorkflowExists) return;
  18. const currentWorkflow = workflow.includedWorkflows[workflowId];
  19. currentWorkflow.table =
  20. currentWorkflow.table || currentWorkflow.dataColumns;
  21. delete currentWorkflow.dataColumns;
  22. Workflow.insert({
  23. data: {
  24. ...currentWorkflow,
  25. drawflow: getDrawflow(workflow.includedWorkflows[workflowId]),
  26. id: workflowId,
  27. createdAt: Date.now(),
  28. },
  29. });
  30. });
  31. delete workflow.includedWorkflows;
  32. }
  33. workflow.table = workflow.table || workflow.dataColumns;
  34. delete workflow.dataColumns;
  35. Workflow.insert({
  36. data: {
  37. ...workflow,
  38. drawflow: getDrawflow(workflow),
  39. createdAt: Date.now(),
  40. },
  41. });
  42. };
  43. files.forEach((file) => {
  44. const reader = new FileReader();
  45. reader.onload = handleOnLoadReader;
  46. reader.readAsText(file);
  47. });
  48. })
  49. .catch((error) => {
  50. console.error(error);
  51. });
  52. }
  53. export function convertWorkflow(workflow, additionalKeys = []) {
  54. if (!workflow) return null;
  55. const keys = [
  56. 'name',
  57. 'icon',
  58. 'table',
  59. 'version',
  60. 'drawflow',
  61. 'settings',
  62. 'globalData',
  63. 'description',
  64. ...additionalKeys,
  65. ];
  66. const content = {
  67. extVersion: chrome.runtime.getManifest().version,
  68. };
  69. keys.forEach((key) => {
  70. content[key] = workflow[key];
  71. });
  72. return content;
  73. }
  74. function findIncludedWorkflows({ drawflow }, maxDepth = 3, workflows = {}) {
  75. if (maxDepth === 0) return workflows;
  76. const blocks = parseJSON(drawflow, null)?.drawflow.Home.data;
  77. if (!blocks) return workflows;
  78. Object.values(blocks).forEach(({ data, name }) => {
  79. if (name !== 'execute-workflow' || workflows[data.workflowId]) return;
  80. const workflow = Workflow.find(data.workflowId);
  81. if (workflow && !workflow.isProtected) {
  82. workflows[data.workflowId] = convertWorkflow(workflow);
  83. findIncludedWorkflows(workflow, maxDepth - 1, workflows);
  84. }
  85. });
  86. return workflows;
  87. }
  88. export function exportWorkflow(workflow) {
  89. if (workflow.isProtected) return;
  90. const includedWorkflows = findIncludedWorkflows(workflow);
  91. const content = convertWorkflow(workflow);
  92. content.includedWorkflows = includedWorkflows;
  93. const blob = new Blob([JSON.stringify(content)], {
  94. type: 'application/json',
  95. });
  96. const url = URL.createObjectURL(blob);
  97. fileSaver(`${workflow.name}.json`, url);
  98. }
  99. export default {
  100. export: exportWorkflow,
  101. import: importWorkflow,
  102. };