From 9c91828f048ff39c328c6729bfdc9e5e57c8bf90 Mon Sep 17 00:00:00 2001 From: Theo Costantini Date: Sun, 26 Apr 2020 18:01:43 +0200 Subject: [PATCH] Web integration button position can be overridden --- src/content-scripts/web-integration/index.ts | 55 +++++++++++++++++--- 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/src/content-scripts/web-integration/index.ts b/src/content-scripts/web-integration/index.ts index 25e949e..e8c5b87 100644 --- a/src/content-scripts/web-integration/index.ts +++ b/src/content-scripts/web-integration/index.ts @@ -2,6 +2,18 @@ import * as Enum from '@/utils/Enum'; import Button from '@/content-scripts/web-integration/Button'; import Settings from '@/utils/Settings'; +interface Position { + inPage: boolean; + x: Enum.WebIntegrationX; + y: Enum.WebIntegrationY; +} + +interface PositionnOverride { + inPage?: boolean; + x?: Enum.WebIntegrationX; + y?: Enum.WebIntegrationY; +} + interface OverlayParameters { /** * CSS selector used to find the search value for the content. @@ -48,6 +60,14 @@ interface OverlayParameters { * This method receives one parameter which is the button node. */ appendInPage: (node: HTMLElement) => void; + + /** + * Overrides button position settings. + * + * If a page needs the button to be placed in a specific way because it can't be placed any other + * ways, use this object to override the user settings and force the button position. + */ + positionOverride?: PositionnOverride; } /** @@ -73,6 +93,31 @@ function getTitle(node: HTMLElement): string { return node.innerText; } +/** + * Get button position. + */ +function getPosition(settings: ALSearch.Settings, positionOverride?: PositionnOverride): Position { + const p = { + inPage: settings.integration.overlay.inPage, + x: settings.integration.overlay.x, + y: settings.integration.overlay.y, + }; + + if (positionOverride && Object.prototype.hasOwnProperty.call(positionOverride, 'inPage')) { + p.inPage = positionOverride.inPage!; + } + + if (positionOverride && Object.prototype.hasOwnProperty.call(positionOverride, 'x')) { + p.x = positionOverride.x!; + } + + if (positionOverride && Object.prototype.hasOwnProperty.call(positionOverride, 'y')) { + p.y = positionOverride.y!; + } + + return p; +} + /** * Create and add a button in the page if the user has enabled web integration. * @@ -103,12 +148,10 @@ export default async function create(args: OverlayParameters): Promise