| 1 |
/* ************************************************************************ */ |
| 2 |
/* */ |
| 3 |
/* This file is part of Frama-C. */ |
| 4 |
/* */ |
| 5 |
/* Copyright (C) 2007-2022 */ |
| 6 |
/* CEA (Commissariat à l'énergie atomique et aux énergies */ |
| 7 |
/* alternatives) */ |
| 8 |
/* */ |
| 9 |
/* you can redistribute it and/or modify it under the terms of the GNU */ |
| 10 |
/* Lesser General Public License as published by the Free Software */ |
| 11 |
/* Foundation, version 2.1. */ |
| 12 |
/* */ |
| 13 |
/* It is distributed in the hope that it will be useful, */ |
| 14 |
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 15 |
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ |
| 16 |
/* GNU Lesser General Public License for more details. */ |
| 17 |
/* */ |
| 18 |
/* See the GNU Lesser General Public License version 2.1 */ |
| 19 |
/* for more details (enclosed in the file licenses/LGPLv2.1). */ |
| 20 |
/* */ |
| 21 |
/* ************************************************************************ */ |
| 22 |
|
| 23 |
/* eslint-disable @typescript-eslint/explicit-function-return-type */ |
| 24 |
|
| 25 |
// -------------------------------------------------------------------------- |
| 26 |
// --- Dispatch Layout |
| 27 |
// -------------------------------------------------------------------------- |
| 28 |
|
| 29 |
/** |
| 30 |
|
| 31 |
This module allows to declare components anywhere in a component hierarchy |
| 32 |
and to render them a totally different place. |
| 33 |
|
| 34 |
You shall wrap dispatched components inside a `<Dispatch.Item>` container, |
| 35 |
and render them wherever you want with `<Dispatch.Render>`. Each target |
| 36 |
place can display only one uniquely identified item. |
| 37 |
|
| 38 |
This can be also used to display some item among many in one unique place. |
| 39 |
|
| 40 |
@packageDocumentation |
| 41 |
@module dome/layout/dispatch |
| 42 |
*/ |
| 43 |
|
| 44 |
import React from 'react'; |
| 45 |
import { Event } from 'dome'; |
| 46 |
|
| 47 |
// -------------------------------------------------------------------------- |
| 48 |
// --- Global Dispatcher |
| 49 |
// -------------------------------------------------------------------------- |
| 50 |
|
| 51 |
class ITEM { |
| 52 |
event: Event; |
| 53 |
rendered = false; |
| 54 |
content: React.ReactNode = undefined; |
| 55 |
constructor(id: string) { |
| 56 |
this.event = new Event(`dome-dispatch-${id}`); |
| 57 |
} |
| 58 |
|
| 59 |
update(content: React.ReactNode): void { |
| 60 |
this.content = content; |
| 61 |
if (this.rendered) { |
| 62 |
this.rendered = false; |
| 63 |
setImmediate(this.event.emit); |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
const ITEMS = new Map<string, ITEM>(); |
| 69 |
|
| 70 |
const getItem = (id: string): ITEM => { |
| 71 |
let item = ITEMS.get(id); |
| 72 |
if (!item) { |
| 73 |
item = new ITEM(id); |
| 74 |
ITEMS.set(id, item); |
| 75 |
} |
| 76 |
return item; |
| 77 |
}; |
| 78 |
|
| 79 |
// -------------------------------------------------------------------------- |
| 80 |
// --- Element Definition |
| 81 |
// -------------------------------------------------------------------------- |
| 82 |
|
| 83 |
export interface ElementProps { |
| 84 |
/** Element identifier. */ |
| 85 |
id: string; |
| 86 |
/** Element contents. */ |
| 87 |
children?: React.ReactNode; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
Define an identified element. |
| 92 |
|
| 93 |
The `<DefineElement />` renders `null` and register its contents under |
| 94 |
the specified identifier, which would be rendered by `<RenderElement />` |
| 95 |
everywhere else. |
| 96 |
|
| 97 |
Multiple definitions of the same element might produce |
| 98 |
unpredictable results. |
| 99 |
*/ |
| 100 |
export function DefineElement(props: ElementProps) { |
| 101 |
React.useEffect(() => { |
| 102 |
const item = getItem(props.id); |
| 103 |
item.update(props.children); |
| 104 |
return () => item.update(undefined); |
| 105 |
}); |
| 106 |
return null; |
| 107 |
} |
| 108 |
|
| 109 |
// -------------------------------------------------------------------------- |
| 110 |
// --- Render Elements |
| 111 |
// -------------------------------------------------------------------------- |
| 112 |
|
| 113 |
export interface RenderProps { |
| 114 |
id?: string; |
| 115 |
children?: React.ReactNode | ((content: React.ReactNode) => React.ReactNode); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
Render an identified element. |
| 120 |
|
| 121 |
If the render element has a function as children, it is passed the content |
| 122 |
of the item, or `undefined` if there is no identifier or no |
| 123 |
corresponding `<DefineElement />` currently mounted. |
| 124 |
*/ |
| 125 |
export function RenderElement(props: RenderProps) { |
| 126 |
const [age, setAge] = React.useState(0); |
| 127 |
const { id, children } = props; |
| 128 |
const item = id ? getItem(id) : undefined; |
| 129 |
React.useEffect(() => { |
| 130 |
if (item) { |
| 131 |
const { event } = item; |
| 132 |
const trigger = () => setAge(age + 1); |
| 133 |
event.on(trigger); |
| 134 |
return () => event.off(trigger); |
| 135 |
} |
| 136 |
return undefined; |
| 137 |
}, [age, item]); |
| 138 |
if (item) item.rendered = true; |
| 139 |
if (typeof (children) === 'function') |
| 140 |
return children(item?.content); |
| 141 |
const content = item?.content || children; |
| 142 |
return content; |
| 143 |
} |
| 144 |
|
| 145 |
// -------------------------------------------------------------------------- |