/**
* 创建插件容器
*
* Create a plugin container
* @param type - 插件类型 | plugin type
* @param cover - 容器是否覆盖整个画布 | Whether the container covers the entire canvas
* @param style - 额外样式 | Additional style
* @returns 插件容器 | plugin container
*/
export function createPluginContainer(type: string, cover = true, style?: Partial): HTMLElement {
const container = document.createElement('div');
container.setAttribute('class', `g6-${type}`);
Object.assign(container.style, {
position: 'absolute',
display: 'block',
});
if (cover) {
Object.assign(container.style, {
position: 'unset',
gridArea: '1 / 1 / 2 / 2',
inset: '0px',
height: '100%',
width: '100%',
overflow: 'hidden',
pointerEvents: 'none',
});
}
if (style) Object.assign(container.style, style);
return container;
}
/**
* 创建 DOM 元素,如果存在则删除,再创建一个新的
*
* Create a DOM element, if exists, remove it and create a new one.
* @param id - id | id
* @param tag - 标签 | tag
* @param style - 样式 | style
* @param innerHTML - 内容 | innerHTML
* @param container - 容器 | container
* @returns 创建的 DOM 元素 | created DOM element
*/
export function insertDOM(
id: string,
tag = 'div',
style: Partial = {},
innerHTML = '',
container: HTMLElement = document.body,
) {
const dom = document.getElementById(id);
if (dom) dom.remove();
const div = document.createElement(tag);
div.innerHTML = innerHTML;
div.id = id;
Object.assign(div.style, style);
container.appendChild(div);
return div;
}