Skip to content

Commit

Permalink
fix: logic of mark element option
Browse files Browse the repository at this point in the history
  • Loading branch information
Kholid060 committed Nov 13, 2021
1 parent 27f2915 commit 2468e6d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/background/workflow-engine/blocks-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,13 +501,13 @@ export function conditions({ data, outputs }, prevBlockData) {

export function repeatTask({ data, id, outputs }) {
return new Promise((resolve) => {
if (this.repeatedTasks[id] === data.repeatFor) {
if (this.repeatedTasks[id] >= data.repeatFor) {
resolve({
data: data.repeatFor,
nextBlockId: getBlockConnection({ outputs }),
});
} else {
this.repeatedTasks[id] = (this.repeatedTasks[id] || 0) + 1;
this.repeatedTasks[id] = (this.repeatedTasks[id] || 1) + 1;

resolve({
data: data.repeatFor,
Expand Down
55 changes: 29 additions & 26 deletions src/content/blocks-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,37 @@
import simulateEvent from '@/utils/simulate-event';
import handleFormElement from '@/utils/handle-form-element';

function isElementUnique(element, { data, id }) {
if (!data.markEl) return true;

const blockId = `block--${id}`;

if (element.hasAttribute(blockId)) return false;

element.setAttribute(blockId, '');

return true;
function markElement(el, { id, data }) {
if (data.markEl) {
el.setAttribute(`block--${id}`, '');
}
}
function handleElement({ data, id }, callback) {
if (!data || !data.selector) return null;

const element = data.multiple
? document.querySelectorAll(data.selector)
: document.querySelector(data.selector);
try {
const blockIdAttr = `block--${id}`;
const selector = data.markEl
? `${data.selector.trim()}:not([${blockIdAttr}])`
: data.selector;

if (typeof callback === 'boolean' && callback) return element;
const element = data.multiple
? document.querySelectorAll(selector)
: document.querySelector(selector);

if (data.multiple) {
element.forEach((el) => {
if (isElementUnique(el, { id, data })) callback(el);
});
} else if (element) {
if (isElementUnique(element, { id, data })) callback(element);
if (typeof callback === 'boolean' && callback) return element;

if (data.multiple) {
element.forEach((el) => {
markElement(el, { id, data });
callback(el);
});
} else if (element) {
markElement(element, { id, data });
callback(element);
}
} catch (error) {
console.error(error);
}
}

Expand Down Expand Up @@ -161,20 +166,18 @@ export function forms(block) {
if (data.multiple) {
const promises = Array.from(elements).map((element) => {
return new Promise((eventResolve) => {
if (isElementUnique(element, block))
handleFormElement(element, data, eventResolve);
else eventResolve('');
markElement(element, block);
handleFormElement(element, data, eventResolve);
});
});

Promise.allSettled(promises).then(() => {
resolve('');
});
} else if (elements) {
if (isElementUnique(elements, block))
handleFormElement(elements, data, resolve);
markElement(element, block);
handleFormElement(elements, data, resolve);
} else {
alert('else?');
resolve('');
}
});
Expand Down
1 change: 0 additions & 1 deletion src/utils/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,6 @@ export const eventList = [
{ id: 'touchcancel', name: 'Touch cancel', type: 'touch-event' },
{ id: 'keydown', name: 'Keydown', type: 'keyboard-event' },
{ id: 'keyup', name: 'Keyup', type: 'keyboard-event' },
{ id: 'keypress', name: 'Keypress', type: 'keyboard-event' },
{ id: 'submit', name: 'Submit', type: 'submit-event' },
{ id: 'wheel', name: 'Wheel', type: 'wheel-event' },
];
Expand Down

0 comments on commit 2468e6d

Please sign in to comment.