Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed support of inline anchor insert position #907

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions cli/plasmo/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@ type Async<T> = Promise<T> | T

type Getter<T, P = any> = (props?: P) => Async<T>

type InsertPosition = "beforebegin" | "afterbegin" | "beforeend" | "afterend"

type ElementInsertOptions = {
element: Element
insertPosition?: InsertPosition
}

type ElementInsertOptionsList = ElementInsertOptions[]

type GetElement = Getter<Element>
type GetElementInsertOptions = Getter<ElementInsertOptions>

type PlasmoCSUIOverlayAnchor = {
element: Element
Expand All @@ -24,8 +34,8 @@ type PlasmoCSUIOverlayAnchor = {
type PlasmoCSUIInlineAnchor = {
element: Element
type: "inline"
insertPosition?: InsertPosition,
root?: Root
insertPosition?: "beforebegin" | "afterbegin" | "beforeend" | "afterend"
}

export type PlasmoCSUIAnchor = PlasmoCSUIOverlayAnchor | PlasmoCSUIInlineAnchor
Expand Down Expand Up @@ -67,8 +77,8 @@ export type PlasmoGetRootContainer = (
export type PlasmoGetOverlayAnchor = GetElement
export type PlasmoGetOverlayAnchorList = Getter<NodeList>

export type PlasmoGetInlineAnchor = GetElement
export type PlasmoGetInlineAnchorList = Getter<NodeList>
export type PlasmoGetInlineAnchor = GetElement | GetElementInsertOptions
export type PlasmoGetInlineAnchorList = Getter<NodeList | ElementInsertOptionsList>

export type PlasmoMountShadowHost = (
props: {
Expand Down
36 changes: 29 additions & 7 deletions cli/plasmo/templates/static/common/csui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,23 +184,45 @@ export function createAnchorObserver<T>(Mount: PlasmoCSUI<T>) {

const renderList: PlasmoCSUIAnchor[] = []

if (!!inlineAnchor && !mountedInlineAnchorSet.has(inlineAnchor)) {
renderList.push({
element: inlineAnchor,
type: "inline"
})
if (!!inlineAnchor) {
if (inlineAnchor instanceof Element) {
if (!mountedInlineAnchorSet.has(inlineAnchor)) {
renderList.push({
element: inlineAnchor,
type: "inline"
})
}
} else if (
inlineAnchor.element instanceof Element
&& !mountedInlineAnchorSet.has(inlineAnchor.element)
) {
renderList.push({
element: inlineAnchor.element,
type: "inline",
insertPosition: inlineAnchor.insertPosition
})
}
}

if ((inlineAnchorList?.length || 0) > 0) {
inlineAnchorList.forEach((inlineAnchor) => {
if (
inlineAnchor instanceof Element &&
!mountedInlineAnchorSet.has(inlineAnchor)
Comment on lines -197 to -198
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: seems like Prettier is not doing its job here :-?

inlineAnchor instanceof Element &&
!mountedInlineAnchorSet.has(inlineAnchor)
) {
renderList.push({
element: inlineAnchor,
type: "inline"
})
} else if (
inlineAnchor.element instanceof Element &&
!mountedInlineAnchorSet.has(inlineAnchor.element)
) {
renderList.push({
element: inlineAnchor.element,
type: "inline",
insertPosition: inlineAnchor.insertPosition
})
}
})
}
Expand Down
Loading