Skip to content

Commit

Permalink
refactor: divide create and open context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
shaokeyibb authored and Enter-tainer committed Sep 8, 2024
1 parent 4239c54 commit e8057f6
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions frontend/lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const _registerDialog = ({
</${tag}>
`.trim(),
);
dialog = document.querySelector(
dialog = parent.querySelector(
isClass ? `.${idOrClass}` : `#${idOrClass}`,
)! as HTMLElement;

Expand Down Expand Up @@ -177,14 +177,8 @@ const _unselectOffsetParagraph = () => {
selectedOffset = null;
};

const _openContextMenu = ({ el }: { el: HTMLElement }) => {
const contextMenu = el.querySelector(`.review-context-menu`) as
| HTMLDivElement
| undefined;
if (contextMenu) {
contextMenu.style.display = "";
return;
}
const _createContextMenu = ({ el }: { el: HTMLElement }) => {
if (el.querySelector(`.review-context-menu`)) return;
_registerDialog({
idOrClass: "review-context-menu",
content: `
Expand Down Expand Up @@ -214,17 +208,25 @@ const _openContextMenu = ({ el }: { el: HTMLElement }) => {
innerEl.addEventListener("mouseleave", () => {
delete el.dataset.reviewFocused;
});
innerEl.style.display = "none";
},
});
};

const _openContextMenu = ({ el }: { el: HTMLElement }) => {
const contextMenu = el.querySelector(`.review-context-menu`) as
| HTMLDivElement
| undefined;
if (!contextMenu) return;
contextMenu.style.display = "";
};

const _closeContextMenu = ({ el }: { el: HTMLElement }) => {
const contextMenu = el.querySelector(
`.review-context-menu:not([style*="display: none"])`,
) as HTMLDivElement | undefined;
if (contextMenu) {
contextMenu.style.display = "none";
}
if (!contextMenu) return;
contextMenu.style.display = "none";
};

const _openCommentsPanel = async () => {
Expand Down Expand Up @@ -1034,12 +1036,7 @@ export function setupReview(
});
});
// pre render context menu
_openContextMenu({
el: offset,
});
_closeContextMenu({
el: offset,
});
_createContextMenu({ el: offset });
}

// clear cache
Expand Down

0 comments on commit e8057f6

Please sign in to comment.