@flatbiz/antd
Version:
123 lines (114 loc) • 4.09 kB
JavaScript
/*! @flatjs/forge MIT @flatbiz/antd */
import { classNames } from '@dimjs/utils/class-names/class-names';
import { a as _slicedToArray } from './_rollupPluginBabelHelpers-BspM60Sw.js';
import { useRef, useCallback, useState, useEffect } from 'react';
import { createRoot } from 'react-dom/client';
import { useDebounceFn } from 'ahooks';
import MindMap from 'simple-mind-map';
import { getUuid } from '@flatbiz/utils';
import { jsx } from 'react/jsx-runtime';
function useCallbackRef(fn) {
var ref = useRef(fn);
ref.current = fn;
return useCallback(function () {
var _ref$current;
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.call.apply(_ref$current, [ref].concat(args));
}, []);
}
var XMindPreview = function XMindPreview(props) {
var data = props.data,
className = props.className,
style = props.style,
renderNode = props.renderNode;
var _useState = useState(false),
_useState2 = _slicedToArray(_useState, 2),
isInitData = _useState2[0],
setIsInitData = _useState2[1];
var comRef = useRef({
id: '',
mindMap: {}
});
/** 防止dom id重复,id在一个组件内需要时唯一的 */
if (!comRef.current.id) {
comRef.current.id = "mindMapContainer-".concat(getUuid());
}
useEffect(function () {
var mindMap = new MindMap({
el: document.getElementById(comRef.current.id),
fit: true,
data: data,
readonly: true,
isUseCustomNodeContent: !!renderNode,
customCreateNodeContent: function customCreateNodeContent(node) {
var customRenderOption = renderNode === null || renderNode === void 0 ? void 0 : renderNode(node.nodeData);
// 默认渲染
if (!customRenderOption) return null;
var el = document.createElement('div');
// 自定义渲染
var width = customRenderOption.width,
height = customRenderOption.height,
reactNode = customRenderOption.reactNode;
el.style.width = "".concat(width || 200, "px");
el.style.height = "".concat(height || 22, "px");
var root = createRoot(el);
root.render(reactNode);
return el;
}
});
comRef.current.mindMap = mindMap;
if (data) {
setIsInitData(true);
}
return function () {
mindMap.destroy();
};
}, []);
/** 数据更新重置状态 */
useEffect(function () {
var mindMap = comRef.current.mindMap;
if (!mindMap) return;
/** 只有第一次渲染的时候更新画布位置 */
if (!isInitData) {
handleListenOnce('node_tree_render_end', function () {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
mindMap.view.fit();
setIsInitData(true);
});
}
mindMap.setData(data || {});
}, [data]);
// 屏幕自适应
useEffect(function () {
window.addEventListener('resize', handleResize);
return function () {
window.removeEventListener('resize', handleResize);
};
}, []);
var _useDebounceFn = useDebounceFn(function () {
var _comRef$current$mindM;
comRef === null || comRef === void 0 || (_comRef$current$mindM = comRef.current.mindMap) === null || _comRef$current$mindM === void 0 || _comRef$current$mindM.resize();
}),
handleResizeDebounce = _useDebounceFn.run;
var handleResize = useCallbackRef(handleResizeDebounce);
// 监听一次事件
var handleListenOnce = useCallbackRef(function (eventName, callback) {
var _fun = function fun() {
callback();
comRef.current.mindMap.off(eventName, _fun);
};
comRef.current.mindMap.on(eventName, _fun);
});
return /*#__PURE__*/jsx("div", {
className: classNames('x-mind-preview-panel-wrap', className),
style: style,
children: /*#__PURE__*/jsx("div", {
id: comRef.current.id
})
});
};
export { XMindPreview as X };
//# sourceMappingURL=x-mind-preview-Bx7UmHAu.js.map