Skip to content
Snippets Groups Projects
Commit 8ee05a9a authored by Liliana Sanfilippo's avatar Liliana Sanfilippo
Browse files

markup mindmap

parent 1ac893cd
No related branches found
No related tags found
No related merge requests found
Pipeline #397633 failed
import React, { useState, useRef, useEffect } from 'react'; import React, { useState, useRef, useEffect } from 'react';
import { Markmap } from 'markmap-view'; import { Markmap } from 'markmap-view';
import { transformer } from './mindmap'; import { transformer } from './mindmap';
import 'markmap-toolbar/dist/style.css'; import 'markmap-toolbar/dist/style.css';
const initValue = `# markmap const initValue = `# Stakeholders
- Patients
- beautiful - hi
- useful - Industry
- easy - Academia
- interactive - Medical Professionals
`; `;
export default function MarkmapHooks() { export default function MarkmapHooks() {
const [value, setValue] = useState(initValue); const [value] = useState(initValue);
// Ref for SVG element // Ref for SVG element
const refSvg = useRef<SVGSVGElement>(); const refSvg = useRef<SVGSVGElement>(null); // Initialize with null
// Ref for markmap object // Ref for markmap object
const refMm = useRef<Markmap>(); const refMm = useRef<Markmap | null>(null); // Initialize with null
useEffect(() => { useEffect(() => {
// Create markmap and save to refMm // Create markmap and save to refMm
if (refMm.current) return; if (refMm.current || !refSvg.current) return; // Add null check
const mm = Markmap.create(refSvg.current); const mm = Markmap.create(refSvg.current);
refMm.current = mm; refMm.current = mm;
// Add double-click event listener to prevent zoom
const svgElement = refSvg.current;
if (svgElement) {
const handleDoubleClick = (event: MouseEvent) => {
event.preventDefault();
};
svgElement.addEventListener('dblclick', handleDoubleClick);
// Cleanup event listener on unmount
return () => {
svgElement.removeEventListener('dblclick', handleDoubleClick);
};
}
}, [refSvg.current]); }, [refSvg.current]);
useEffect(() => { useEffect(() => {
...@@ -33,15 +46,12 @@ export default function MarkmapHooks() { ...@@ -33,15 +46,12 @@ export default function MarkmapHooks() {
if (!mm) return; if (!mm) return;
const { root } = transformer.transform(value); const { root } = transformer.transform(value);
mm.setData(root); mm.setData(root);
mm.fit(); mm.fit();
}, [refMm.current, value]); }, [refMm.current, value]);
return ( return (
<React.Fragment> <React.Fragment>
<div className="flex-1"> <div className="flex-1"></div>
</div>
<svg className="flex-1" ref={refSvg} /> <svg className="flex-1" ref={refSvg} />
</React.Fragment> </React.Fragment>
); );
......
...@@ -3,5 +3,7 @@ import { Transformer } from 'markmap-lib'; ...@@ -3,5 +3,7 @@ import { Transformer } from 'markmap-lib';
export const transformer = new Transformer(); export const transformer = new Transformer();
const { scripts, styles } = transformer.getAssets(); const { scripts, styles } = transformer.getAssets();
loadCSS(styles);
loadJS(scripts); // Provide default empty array if styles or scripts are undefined
loadCSS(styles ?? []);
loadJS(scripts ?? []);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment