From 6017ec59640b0fcd70f7279cbbe9987af9a18062 Mon Sep 17 00:00:00 2001 From: Daniil Volynkin Date: Fri, 10 Nov 2023 05:13:39 +0700 Subject: [PATCH] feat: Add position option for inline insertion (#790) * Add position option for inline insertion * Changed PlasmoCSUIAnchor to use a Discriminating Union --- cli/plasmo/src/type.ts | 12 ++++++++++-- cli/plasmo/templates/static/common/csui.ts | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/cli/plasmo/src/type.ts b/cli/plasmo/src/type.ts index dafa40d66..a7ae39371 100644 --- a/cli/plasmo/src/type.ts +++ b/cli/plasmo/src/type.ts @@ -14,11 +14,19 @@ type Getter = (props?: P) => Async type GetElement = Getter -export type PlasmoCSUIAnchor = { +type PlasmoCSUIOverlayAnchor = { element: Element - type: "overlay" | "inline" + type: "overlay" } +type PlasmoCSUIInlineAnchor = { + element: Element + type: "inline" + insertPosition?: "beforebegin" | "afterbegin" | "beforeend" | "afterend" +} + +export type PlasmoCSUIAnchor = PlasmoCSUIOverlayAnchor | PlasmoCSUIInlineAnchor + export type PlasmoCSUIProps = { anchor?: PlasmoCSUIAnchor } diff --git a/cli/plasmo/templates/static/common/csui.ts b/cli/plasmo/templates/static/common/csui.ts index a910b9cce..118161bb2 100644 --- a/cli/plasmo/templates/static/common/csui.ts +++ b/cli/plasmo/templates/static/common/csui.ts @@ -50,7 +50,7 @@ async function injectAnchor( mountState }) } else if (anchor.type === "inline") { - anchor.element.insertAdjacentElement("afterend", shadowHost) + anchor.element.insertAdjacentElement(anchor.insertPosition || "afterend", shadowHost) } else { document.documentElement.prepend(shadowHost) }