Skip to content

Commit

Permalink
refactor finding parent section & column
Browse files Browse the repository at this point in the history
  • Loading branch information
aghontpi committed Oct 10, 2021
1 parent eda22a7 commit 2b42d89
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/Components/HtmlWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useQuillEditor } from '../Hooks/Quill.hook';
import { findClosestParent, findUniqueIdentifier } from '../Utils/closestParent';
import { detectEmptyElement } from '../Utils/detectEmptyBody';
import { findElementInJson } from '../Utils/findElementInMjmlJson';
import { findColumnOfElement } from '../Utils/findElementsColumn';
import { findColumnOfElement } from '../Utils/findElementsParent';
import { generateDropItemPlaceholder } from '../Utils/generateDropItemPlaceholder';

interface HtmlWrapperProps {
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/closestParent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { findElementInJson } from './findElementInMjmlJson';
import { findColumnOfElement } from './findElementsColumn';
import { findColumnOfElement } from './findElementsParent';

const findClosestParent = (element: HTMLElement) => {
const closest = element.closest('.mjml-tag');
Expand Down
19 changes: 0 additions & 19 deletions src/Utils/findElementsColumn.ts

This file was deleted.

27 changes: 27 additions & 0 deletions src/Utils/findElementsParent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { findUniqueIdentifier } from './closestParent';

const recursivelyFindParent = (node: HTMLElement | any, parentClassPartial: string): any => {
if (!node) {
return null;
}
let parent = node.closest('.mjml-tag');
if (parent && parent.classList) {
const uniqueIdentifier = findUniqueIdentifier(parent, parent.classList);
if (uniqueIdentifier && uniqueIdentifier.includes(parentClassPartial)) {
return [parent, uniqueIdentifier];
}
parent = parent.parentElement;
}

return recursivelyFindParent(parent, parentClassPartial);
};

const findColumnOfElement = (node: HTMLElement | any): any => {
return recursivelyFindParent(node, 'mj-column');
};

const findSectionOfElement = (node: HTMLElement | any): any => {
return recursivelyFindParent(node, 'mj-section');
};

export { findColumnOfElement, findSectionOfElement };

0 comments on commit 2b42d89

Please sign in to comment.