Skip to content

Commit

Permalink
refactor(SelectTranslator): improve class structure
Browse files Browse the repository at this point in the history
  • Loading branch information
vitonsky committed Aug 4, 2021
1 parent 73686e4 commit a68243f
Showing 1 changed file with 49 additions and 50 deletions.
99 changes: 49 additions & 50 deletions src/modules/SelectTranslator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ export class SelectTranslator {
private root: HTMLElement | null = null;

// Flag which set while every selection event and reset while button shown
private selectionFlag = false;
private unhandledSelection = false;
private selectionFlagUpdater = () => {
this.selectionFlag = true;
this.unhandledSelection = true;
};

public start() {
Expand Down Expand Up @@ -153,7 +153,7 @@ export class SelectTranslator {
)
return;

this.mountComponent();
this.hidePopup();
};

private context = Symbol('context');
Expand Down Expand Up @@ -210,63 +210,62 @@ export class SelectTranslator {
}

// Skip if it shown not first time
if (this.options.showOnceForSelection && !this.selectionFlag) return;
if (this.options.showOnceForSelection && !this.unhandledSelection) return;

const selectedText = selection.toString();
if (selectedText.length > 0) {
this.mountComponent({
text: selectedText,
x: pageX,
y: pageY,
});
this.showPopup(selectedText, pageX, pageY);
}
});
};

// TODO: refactor me to `mountComponent(child?: React.ReactNode)`, `showPopup`, `hidePopup`
private mountComponent = (data?: { text: string; x: number; y: number }) => {
private showPopup = (text: string, x: number, y: number) => {
// Update selection value
this.unhandledSelection = false;

const {
pageLanguage,
quickTranslate,
detectedLangFirst,
rememberDirection,
zIndex,
timeoutForHideButton,
focusOnTranslateButton,
showOriginalText,
} = this.options;

this.mountComponent(
<SelectTranslatorPopup
closeHandler={() => this.mountComponent()}
translate={translate}
{...{
pageLanguage,
showOriginalText,
quickTranslate,
detectedLangFirst,
rememberDirection,
zIndex,
timeoutForHideButton,
focusOnTranslateButton,
x,
y,
text,
}}
/>,
);
};

private hidePopup = () => {
this.mountComponent();
};

// Render shadow root with preloading resources
private mountComponent = (child?: React.ReactNode) => {
// Skip when root node is not exist
if (this.root === null) return;

// Render popup child if data are specify
let content: undefined | React.ReactNode = undefined;
if (data !== undefined) {
this.selectionFlag = false;

const {
pageLanguage,
quickTranslate,
detectedLangFirst,
rememberDirection,
zIndex,
timeoutForHideButton,
focusOnTranslateButton,
showOriginalText,
} = this.options;

const { x, y, text } = data;
content = (
<SelectTranslatorPopup
closeHandler={() => this.mountComponent()}
translate={translate}
{...{
pageLanguage,
showOriginalText,
quickTranslate,
detectedLangFirst,
rememberDirection,
zIndex,
timeoutForHideButton,
focusOnTranslateButton,
x,
y,
text,
}}
/>
);
}

const styles = ['common.css', 'contentscript.css'];

ReactDOM.render(
<root.div style={{ all: 'unset' }} mode="closed">
{/* Include styles and scripts */}
Expand All @@ -277,7 +276,7 @@ export class SelectTranslator {
href={browser.runtime.getURL(path)}
/>
))}
{content}
{child}
</root.div>,
this.root,
);
Expand Down

0 comments on commit a68243f

Please sign in to comment.