editorAutocomplete.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { getBlocks } from '../getSharedData';
  2. const blocks = getBlocks();
  3. const autocompleteKeys = {
  4. loopId: 'loopData',
  5. refKey: 'googleSheets',
  6. variableName: 'variables',
  7. };
  8. function getData(blockName, blockData) {
  9. const keys = blocks[blockName]?.autocomplete;
  10. const dataList = {};
  11. if (!keys) return dataList;
  12. keys.forEach((key) => {
  13. const value = blockData[key];
  14. if (!value) return;
  15. const autocompleteKey = autocompleteKeys[key];
  16. if (!dataList[autocompleteKey]) dataList[autocompleteKey] = {};
  17. dataList[autocompleteKey][value] = '';
  18. });
  19. return dataList;
  20. }
  21. const extractBlocksAutocomplete = {
  22. trigger(blockId, data) {
  23. if (!this[blockId].variables) this[blockId].variables = {};
  24. data.parameters?.forEach((param) => {
  25. this[blockId].variables[param.name] = '';
  26. });
  27. if (data.type === 'context-menu') {
  28. Object.assign(this[blockId].variables, {
  29. $ctxElSelector: '',
  30. $ctxTextSelection: '',
  31. $ctxLink: '',
  32. $ctxMediaUrl: '',
  33. });
  34. }
  35. },
  36. 'blocks-group': function (blockId, data) {
  37. data.blocks.forEach((block) => {
  38. this[block.itemId] = getData(block.id, block.data);
  39. });
  40. },
  41. 'insert-data': function (blockId, data) {
  42. if (!this[blockId].variables) this[blockId].variables = {};
  43. data.dataList.forEach((item) => {
  44. if (item.type !== 'variable' || !item.name.trim()) return;
  45. this[blockId].variables[item.name] = '';
  46. });
  47. },
  48. };
  49. export default function (label, { data, id }) {
  50. const autocompleteData = { [id]: {} };
  51. if (extractBlocksAutocomplete[label]) {
  52. extractBlocksAutocomplete[label].call(autocompleteData, id, data);
  53. } else {
  54. autocompleteData[id] = getData(label, data);
  55. }
  56. return autocompleteData;
  57. }