Skip to content

Commit

Permalink
improve(plugin): reset option for slot container
Browse files Browse the repository at this point in the history
  • Loading branch information
xyhp915 authored and tiensonqin committed Nov 16, 2021
1 parent 5f7f238 commit 5a3e93f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
1 change: 1 addition & 0 deletions libs/src/LSPlugin.ts
Expand Up @@ -34,6 +34,7 @@ export type UIBaseOptions = {
style?: CSS.Properties
attrs?: Record<string, string>
close?: 'outside' | string
reset?: boolean // reset slot content or not
}

export type UIPathIdentity = {
Expand Down
21 changes: 16 additions & 5 deletions libs/src/helpers.ts
Expand Up @@ -243,8 +243,7 @@ export function setupInjectedUI (
let float: boolean

const pl = this
const id = `${ui.key}-${slot}-${pl.id}`
const key = `${ui.key}-${pl.id}`


if ('slot' in ui) {
slot = ui.slot
Expand All @@ -255,6 +254,9 @@ export function setupInjectedUI (
float = true
}

const id = `${ui.key}-${slot}-${pl.id}`
const key = `${ui.key}--${pl.id}`

const target = float ? document.body : (selector && document.querySelector(selector))
if (!target) {
console.error(`${this.debugTag} can not resolve selector target ${selector}`)
Expand All @@ -270,7 +272,7 @@ export function setupInjectedUI (
ADD_ATTR: ['allow', 'src', 'allowfullscreen', 'frameborder', 'scrolling']
})
} else { // remove ui
injectedUIEffects.get(key)?.call(null)
injectedUIEffects.get(id)?.call(null)
return
}

Expand Down Expand Up @@ -334,6 +336,15 @@ export function setupInjectedUI (
pl._setupDraggableContainer(el, { key, close: () => teardownUI(), title: attrs?.title }))
}

if (!!slot && ui.reset) {
const exists = Array.from(target.querySelectorAll('[data-injected-ui]'))
.map((it: HTMLElement) => it.id)

exists?.forEach((exist: string) => {
injectedUIEffects.get(exist)?.call(null)
})
}

target.appendChild(el);

// TODO: How handle events
Expand All @@ -354,11 +365,11 @@ export function setupInjectedUI (

teardownUI = () => {
disposeFloat?.()
injectedUIEffects.delete(key)
injectedUIEffects.delete(id)
target!.removeChild(el)
}

injectedUIEffects.set(key, teardownUI)
injectedUIEffects.set(id, teardownUI)
return teardownUI
}

Expand Down
21 changes: 14 additions & 7 deletions resources/js/lsplugin.core.js

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions src/main/frontend/components/plugins.cljs
Expand Up @@ -351,12 +351,13 @@
(rum/defc hook-ui-slot
([type payload] (hook-ui-slot type payload nil))
([type payload opts]
(let [id (str "slot__" (util/rand-str 8))]
(let [rs (util/rand-str 8)
id (str "slot__" rs)]
(rum/use-effect!
(fn []
(plugin-handler/hook-plugin-app type {:slot id :payload payload} nil)
#())
[])
[id])
[:div.lsp-hook-ui-slot
(merge opts {:id id})])))

Expand Down
2 changes: 1 addition & 1 deletion src/main/frontend/components/plugins.css
Expand Up @@ -465,7 +465,7 @@ html[data-theme='dark'] {

.block-content {
.lsp-hook-ui-slot {
display: flex;
display: inline-flex;

[data-injected-ui] {
display: inline-flex;
Expand Down

0 comments on commit 5a3e93f

Please sign in to comment.