| 53 | export const refreshHook = new Hook<[]>(); |
| 54 | |
| 55 | export class Markmap { |
| 56 | options = { ...defaultOptions }; |
| 57 | |
| 58 | state: IMarkmapState; |
| 59 | |
| 60 | svg: ID3SVGElement; |
| 61 | |
| 62 | styleNode: d3.Selection<HTMLStyleElement, INode, HTMLElement, INode>; |
| 63 | |
| 64 | g: d3.Selection<SVGGElement, INode, HTMLElement, INode>; |
| 65 | |
| 66 | zoom: d3.ZoomBehavior<SVGElement, INode>; |
| 67 | |
| 68 | private _observer: ResizeObserver; |
| 69 | |
| 70 | private _disposeList: (() => void)[] = []; |
| 71 | |
| 72 | constructor( |
| 73 | svg: string | SVGElement | ID3SVGElement, |
| 74 | opts?: Partial<IMarkmapOptions>, |
| 75 | ) { |
| 76 | this.svg = (svg as ID3SVGElement).datum |
| 77 | ? (svg as ID3SVGElement) |
| 78 | : select(svg as string); |
| 79 | this.styleNode = this.svg.append('style'); |
| 80 | this.zoom = zoom<SVGElement, INode>() |
| 81 | .filter((event) => { |
| 82 | if (this.options.scrollForPan) { |
| 83 | // Pan with wheels, zoom with ctrl+wheels |
| 84 | if (event.type === 'wheel') return event.ctrlKey && !event.button; |
| 85 | } |
| 86 | return (!event.ctrlKey || event.type === 'wheel') && !event.button; |
| 87 | }) |
| 88 | .on('zoom', this.handleZoom); |
| 89 | this.setOptions(opts); |
| 90 | this.state = { |
| 91 | id: this.options.id || this.svg.attr('id') || getId(), |
| 92 | rect: { x1: 0, y1: 0, x2: 0, y2: 0 }, |
| 93 | }; |
| 94 | this.g = this.svg.append('g'); |
| 95 | this.g.append('g').attr('class', 'markmap-highlight'); |
| 96 | this._observer = new ResizeObserver( |
| 97 | debounce(() => { |
| 98 | this.renderData(); |
| 99 | }, 100), |
| 100 | ); |
| 101 | this._disposeList.push( |
| 102 | refreshHook.tap(() => { |
| 103 | this.setData(); |
| 104 | }), |
| 105 | () => this._observer.disconnect(), |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | getStyleContent(): string { |
| 110 | const { style } = this.options; |
| 111 | const { id } = this.state; |
| 112 | const styleText = typeof style === 'function' ? style(id) : ''; |
nothing calls this directly
no test coverage detected