handler-javascript-code.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { getBlockConnection } from '../helper';
  2. export async function javascriptCode({ outputs, data, ...block }, { refData }) {
  3. const nextBlockId = getBlockConnection({ outputs });
  4. try {
  5. if (data.everyNewTab) {
  6. const isScriptExist = this.preloadScripts.find(
  7. ({ id }) => id === block.id
  8. );
  9. if (!isScriptExist) {
  10. this.preloadScripts.push({ ...block, data });
  11. }
  12. }
  13. if (!this.activeTab.id) {
  14. if (!data.everyNewTab) {
  15. throw new Error('no-tab');
  16. } else {
  17. return { data: '', nextBlockId };
  18. }
  19. }
  20. const result = await this._sendMessageToTab({ ...block, data, refData });
  21. if (result) {
  22. if (result.columns.data?.$error) {
  23. throw new Error(result.columns.data.message);
  24. }
  25. if (result.variables) {
  26. Object.keys(result.variables).forEach((varName) => {
  27. this.setVariable(varName, result.variables[varName]);
  28. });
  29. }
  30. if (result.columns.insert && result.columns.data) {
  31. const params = Array.isArray(result.columns.data)
  32. ? result.columns.data
  33. : [result.columns.data];
  34. this.addDataToColumn(params);
  35. }
  36. }
  37. return {
  38. nextBlockId,
  39. data: result?.columns.data || {},
  40. };
  41. } catch (error) {
  42. error.nextBlockId = nextBlockId;
  43. throw error;
  44. }
  45. }
  46. export default javascriptCode;