Viewport and display commands
Preview version
The jsMind V2 programming API is currently a preview. The API will change in later updates. Please check this documentation anytime for the interface that is current.
The display commands reachable as jm.commands, and the view handle they drive. They read, render or change how the map is shown, and never change the map itself. The zoom factor they accept is clamped to the range 0.1–4.
This page is generated from the published typings dist/jsmind.d.ts with TypeDoc.
JmCommander
Class
Issues commands that change what is displayed, never what is stored.
Constructor
constructor(view: JmViewHandle)Creates a commander.
Parameters
| Name | Type | Description |
|---|---|---|
view | JmViewHandle | The view's capabilities. |
centerOn
centerOn(nodeId: string): voidMoves the map so that one node's centre sits in the middle of the viewport, leaving the zoom alone.
Throws
JsMindErrorWhen the instance was created with{ view: { viewportMode: 'native' } }, before the id is looked at. See JmCommander.zoomTo.JsMindErrorWhen this view has not been given a map yet, or the id is not one of that map's nodes. Closing the map does not empty the view: centring afterwards still acts on the last map shown, instead of throwing.
Parameters
| Name | Type | Description |
|---|---|---|
nodeId | string | Id of a node in the open mind map. |
exportImage
exportImage(options?: JmExportImageOptions): Promise<JmExportImageResult>Renders the displayed map to an image.
Returns: The encoded image plus its dimensions.
Parameters
| Name | Type | Description |
|---|---|---|
options (optional) | JmExportImageOptions | Scale, format and quality. |
fitToView
fitToView(mode?: JmFitToViewMode): voidFits the map to the viewport, in the directions the caller allows.
Remarks
With no mode both mismatches are corrected: a map too large for the box is shrunk until it fits, and one too small is magnified until it fills the box - the fit a single "show me the whole map" button asks for. 'shrink' and 'expand' each name one direction by taking the map's natural size as a bound, so a caller that knows which state it is in can correct that one and leave the other alone.
Nothing on the open path calls this. Opening a map shows it as laid out; fitting it is a decision for whoever is showing it, made when they want it - which is why a page either calls it after open or does not.
Throws
JsMindErrorWhen the instance was created with{ view: { viewportMode: 'native' } }, beforemodeor the map is looked at. See JmCommander.zoomTo.JsMindErrorWhenmodeis given as neither of the two values, so a typo in a JavaScript caller fails loudly instead of quietly shrinking.JsMindErrorWhen this view has not been given a map yet.
Parameters
| Name | Type | Description |
|---|---|---|
mode (optional) | JmFitToViewMode | 'shrink' for a map too large for the box, 'expand' for one too small. Omit it to correct both. |
getViewport
getViewport(): JmViewportReads the current viewport.
Returns: The origin, the zoom factor, the visible size and the mode.
panBy
panBy(dx: number, dy: number): voidMoves the viewport by a distance on screen.
Throws
JsMindErrorWhen the instance was created with{ view: { viewportMode: 'native' } }. See JmCommander.zoomTo.
Parameters
| Name | Type | Description |
|---|---|---|
dx | number | Horizontal travel in viewport pixels. |
dy | number | Vertical travel in viewport pixels. |
panTo
panTo(x: number, y: number): voidPuts a map-space point at the viewport's top-left corner.
Throws
JsMindErrorWhen the instance was created with{ view: { viewportMode: 'native' } }. See JmCommander.zoomTo.
Parameters
| Name | Type | Description |
|---|---|---|
x | number | Map-space x to show at the left edge. |
y | number | Map-space y to show at the top edge. |
zoomTo
zoomTo(scale: number, anchor?: JmViewportAnchor): voidChanges the zoom factor: a point stays put on an axis the map overflows, and an axis the map fits is centred.
Throws
JsMindErrorWhen the instance was created with{ view: { viewportMode: 'native' } }. Native scrolling is in charge then, and this command would have to switch modes to do anything - which is the one thing a running instance does not do.
Parameters
| Name | Type | Description |
|---|---|---|
scale | number | Requested factor, clamped by the view. |
anchor (optional) | JmViewportAnchor | Point to hold still, in the view's own pixels. Defaults to the middle of the viewport. |
JmViewHandle
Interface
What the application layer is allowed to ask of the view.
capture
capture(options?: JmExportImageOptions): Promise<JmExportImageResult>Renders the currently displayed map to an image.
Returns: The encoded image plus its dimensions.
Parameters
| Name | Type | Description |
|---|---|---|
options (optional) | JmExportImageOptions | Scale, format and quality. |
centerOn
centerOn(nodeId: string): voidMoves the map so that one node's centre sits in the middle of the viewport.
Remarks
The zoom is left alone. This is a move, and a caller that centred a node at the factor the reader had chosen should still be at that factor afterwards - a command with "zoom to fit" in its name is a different one.
The origin is the one JmViewHandle.panTo would write for that position, so it is clamped the same way: a node near the content's edge gets as close to the middle as the band allows, and while the whole map fits the viewport the call is a no-op because there is nowhere to move to.
A node whose ancestor is folded still has the layout position it is not drawn at, and this centres that position. Expanding the ancestor would be a change to the mind, which a display command does not make.
Throws
JsMindErrorWhen the instance was created with{ view: { viewportMode: 'native' } }, before the id is looked at. See JmViewHandle.zoomTo.JsMindErrorWhen this view has not been given a map yet, or when the id is not one of that map's nodes.
A closed map is not the same as no map: nothing tells the view a map was closed, it keeps the last one it was shown, and this centres a node of that map instead of throwing.
Parameters
| Name | Type | Description |
|---|---|---|
nodeId | string | Id of a node in the open mind map. |
fitToView
fitToView(mode?: JmFitToViewMode): voidFits the map to the viewport, centred, in the directions the caller allows.
Remarks
The zoom is chosen from the content and the box alone, so two calls on the same map and box give the same numbers - which is what a toolbar button pressed twice, or a gesture repeated, relies on.
With no mode both disagreements between a map and a box are corrected: a map too large to show whole is scaled down until all of it fits, and one smaller than its box is scaled up until it fills it. That is the unqualified "fit" a viewer with a single button offers.
Naming a mode narrows it to one direction, for callers that already know which state they are in and must leave the other one alone:
'shrink'is for a map too large to show whole: the zoom is scaled down until all of it fits, and never past its natural size - a map that already fits stays at 1x rather than being blown up. This is the one to call from a "show me everything" button or a return-from-a-corner gesture.'expand'is for a map smaller than its box: the zoom is scaled up until the map fills it, and never down - a map that already overflows stays at 1x rather than being shrunk. This is the one for a page whose canvas is the frame and wants no empty margin in it.
Every call centres the map. The two modes are bounds on the same fit - 'shrink' takes the natural size as a ceiling, 'expand' as a floor - and calling with neither takes it as no bound at all. So each call corrects exactly what it was asked to and does nothing to the rest.
Unlike JmViewHandle.centerOn this does move a map that fits: the pan's band is empty at exactly-fitting zoom, and a fit that waited for a clamp to move it would leave the map wherever the last gesture left it.
Throws
JsMindErrorWhen the instance was created with{ view: { viewportMode: 'native' } }, beforemodeis looked at. See JmViewHandle.zoomTo.JsMindErrorWhenmodeis given as neither of the two values, so a typo in a JavaScript caller fails loudly instead of quietly shrinking.JsMindErrorWhen this view has not been given a map yet - there is no content to fit. A map that was closed counts as open here: the view keeps the last one it was shown, as it does for JmViewHandle.centerOn.
Parameters
| Name | Type | Description |
|---|---|---|
mode (optional) | JmFitToViewMode | Which mismatch to correct. Omit it to correct both. |
getViewport
getViewport(): JmViewportReads the current viewport.
Returns: The origin, the zoom factor, the visible size and the mode.
panBy
panBy(dx: number, dy: number): voidMoves the viewport by a distance on screen.
Remarks
The delta is in viewport pixels, not map pixels: a drag reports how far the pointer travelled, and dividing by the zoom is arithmetic the view already owns. Positive dx moves the map right, which shows map points further left. Clamped the same way as JmViewHandle.panTo, including its no-op while the map fits the viewport.
Throws
JsMindErrorWhen the instance was created with{ view: { viewportMode: 'native' } }. See JmViewHandle.zoomTo.
Parameters
| Name | Type | Description |
|---|---|---|
dx | number | Horizontal travel in viewport pixels. |
dy | number | Vertical travel in viewport pixels. |
panTo
panTo(x: number, y: number): voidPuts a map-space point at the viewport's top-left corner.
Remarks
x/y are the same pair JmViewHandle.getViewport reports, so a caller can read where the view is looking, keep it, and put it back. The move is clamped to the content's bounds: the map cannot be sent into an empty viewport it would then be unable to leave.
A clamp needs a band to clamp into, and there is one only while the map is larger than the viewport. When the whole map fits at the current zoom the band is empty, and this call leaves the origin unchanged: no-op, not a move to the nearest edge and not a centring. No pan writes an origin outside that band, which is also the answer for a centerOn built on top of this one: a node can only be brought to the middle while the map overflows the view, and while the map fits there is nothing to bring into view.
Throws
JsMindErrorWhen the instance was created with{ view: { viewportMode: 'native' } }. See JmViewHandle.zoomTo.
Parameters
| Name | Type | Description |
|---|---|---|
x | number | Map-space x to show at the left edge. |
y | number | Map-space y to show at the top edge. |
zoomTo
zoomTo(scale: number, anchor?: JmViewportAnchor): voidChanges the zoom factor: a point stays put on an axis the map overflows, and an axis the map fits is centred.
Remarks
The anchor is explicit because a zoom has to keep something fixed, and which something decides how the gesture feels: a wheel zooms about the pointer, a control without a pointer position zooms about the middle of the box. Passing the anchor in keeps the two from being mixed by accident, and keeps the mapping from a screen point to a map point inside the view.
The held point is honoured while the map still overflows the box on that axis. Where the map fits an axis, the origin is the middle instead - so a zoom cannot leave a map off a box it would otherwise fit, the state no pan can bring back. A zoom to the factor already in effect writes the state it found, origin included.
Throws
JsMindErrorWhen the instance was created with{ view: { viewportMode: 'native' } }. Native scrolling is in charge then, and this command would have to switch modes to do anything - which is the one thing a running instance does not do.
Parameters
| Name | Type | Description |
|---|---|---|
scale | number | Requested factor, clamped to the range the view accepts. |
anchor (optional) | JmViewportAnchor | Point to hold still, in the view's own pixels. Defaults to the middle of the viewport. |
JmViewport
Interface
Where the view is looking, how large it renders the map, and how much of it the box shows.
Properties
| Name | Type | Description |
|---|---|---|
canPan | boolean | Whether the library can move the map at this zoom. In 'viewport' mode, true while the content overflows the visible box on at least one axis - exactly when JmViewHandle.panTo is allowed to change the origin. A caller deciding before a gesture starts needs this, because the browser settles touch-action before the first touchmove: a map that fits should hand the gesture to the page rather than swallow a drag that can only be a no-op. Asking here is also what keeps that caller from measuring the content itself, which would fork the layout from the view's own. In 'native' mode it is always false: the browser is scrolling the map, and the clamp that would otherwise answer this has no band to work in. The question it answers is "can the library move the map", not "can the picture move" - in that mode the browser moves it. |
height | number | Visible height in CSS pixels. |
mode | JmViewportMode | Which mechanism owns the box. See JmViewportMode. Reported so a host or a test can assert the mode instead of reading overflow, which is an implementation detail. In 'native' mode x, y and scale are the identity: no command can move the origin there, and the browser's scroll offset is deliberately not reported as one. |
scale | number | Current zoom factor, 1 being unscaled. |
width | number | Visible width in CSS pixels. |
x | number | Map-space x shown at the viewport's left edge. A position in the map, not a scroll offset: it is the number JmViewHandle.panTo writes, so what a caller reads here is what it can put back there. In 'native' mode it is always 0 - nothing writes it there, and the browser's scroll offset is not reported as an origin. See JmViewport.mode. |
y | number | Map-space y shown at the viewport's top edge. See JmViewport.x. |
JmViewportMode
Type
Which mechanism owns the container's inner box.
Remarks
Two mechanisms move the map, and only one can be in charge: the browser scrolling it inside the box ('native'), or a transform written by the library ('viewport'). A transform moves content without extending the scrollable area, so the two cannot both be present.
'viewport' is the default: v2 moves the map with a transform unless the caller asks otherwise. 'native' has to be named explicitly, because the library does not take the browser's input events - a host that only calls create() and open() and then shows a map larger than the box gets a canvas the browser cannot scroll and the library cannot move. Such a host has to wire the gestures itself, or pass 'native' and keep the scrollbars.
The mode is a construction option and never changes afterwards. Taking the box over folds the current scroll offset into the viewport origin and replaces overflow, so going back would move the map and lose the gesture's place; and switching forward under the reader's hand is the same jump in the other direction.
In 'native' mode the library does not move the map at all: the five commands on JmViewHandle throw instead of silently doing nothing, and getViewport() reports the identity.
JmViewportAnchor
Interface
A point in the view's own box, measured from its top-left corner.
Remarks
This is not a map coordinate and not a page coordinate: it is what a pointer event carries once the box's own origin has been subtracted, which is the one thing a caller holding a pointer has to work out. Turning it into a map position is the view's job, and JmViewHandle.zoomTo is the only place it crosses back in.
Properties
| Name | Type | Description |
|---|---|---|
x | number | Horizontal position in the view's own box, in pixels. |
y | number | Vertical position in the view's own box, in pixels. |
JmFitToViewMode
Type
Which mismatch between the map and its box a fit corrects.
Remarks
A map and a box can disagree in two ways, and they want different answers: a map too large to show whole has to be scaled down, and a map smaller than the box may be scaled up to fill it - or left at its natural size, which is what a viewer showing text wants. JmViewHandle.fitToView takes one of these and does that one thing, never the other; leaving it out corrects both.