Skip to content

Commit

Permalink
feat: support centering the active node
Browse files Browse the repository at this point in the history
close #57
  • Loading branch information
gera2ld committed Dec 24, 2024
1 parent 84ac772 commit b1a8a5f
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,28 @@ let root: INode | undefined;
let style: HTMLStyleElement;
let active:
| {
node: INode;
el: Element;
}
node: INode;
el: Element;
}
| undefined;
const activeNodeOptions: {
placement?: 'center' | 'visible';
} = {};

const handlers = {
async setData(data: { root?: INode; jsonOptions?: IMarkmapJSONOptions }) {
async setData(data: {
root?: INode;
jsonOptions?: IMarkmapJSONOptions & {
activeNode?: {
placement?: 'center' | 'visible';
};
};
}) {
await mm.setData((root = data.root), {
...defaultOptions,
...deriveOptions(data.jsonOptions),
});
activeNodeOptions.placement = data.jsonOptions?.activeNode?.placement;
if (firstTime) {
mm.fit();
firstTime = false;
Expand Down Expand Up @@ -170,8 +181,10 @@ function findActiveNode({
return best && { node: best, needRerender };
}

function highlightNode(node: INode) {
mm.ensureView(node, {
async function highlightNode(node: INode) {
await mm[
activeNodeOptions.placement === 'center' ? 'centerNode' : 'ensureVisible'
](node, {
bottom: 80,
});
const g = mm.findElement(node)?.g;
Expand Down

0 comments on commit b1a8a5f

Please sign in to comment.