Skip to content

Commit 92212a7

Browse files
authored
feat: improve TypeScript type definitions with optional parameters (#665)
* feat: improve TypeScript type definitions with optional parameters - Convert required configuration fields to optional in JsMindRuntimeOptions - Add support for 'visible' value in node_overflow option - Update type definitions to match official documentation examples - Enhance test coverage for optional parameters and new type features - Improve API flexibility by making most options optional except container - Update JSDoc comments to reflect optional parameter changes * docs: fix JSDoc direction parameter types to match Direction.of() implementation - Update direction parameter JSDoc in add_node, insert_node_before, insert_node_after, move_node functions - Add support for numeric strings ('-1', '0', '1') and numbers (-1, 0, 1) in addition to string values - Document that Direction.of() function supports multiple input types as shown in jsmind.common.js - Improve API documentation accuracy and developer experience
1 parent 4403188 commit 92212a7

8 files changed

Lines changed: 356 additions & 45 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ node_modules/
33
es6/*.js
44
es6/*.js.map
55
types/generated/
6+
7+
.augment/

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export { default } from './jsmind';
55
export { Node } from './jsmind.node';
66
export { Mind } from './jsmind.mind';
77

8-
// Export strict options and meta types from generated
8+
// Export user-facing options type - now with proper optional fields
99
export type { JsMindRuntimeOptions as JsMindOptions } from './jsmind.option';
1010
export type {
1111
MindMapMeta,

src/jsmind.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export default class jsMind {
143143
}
144144
/**
145145
* Set theme name.
146-
* @param {string|null} theme
146+
* @param {string|null=} theme
147147
*/
148148
set_theme(theme) {
149149
var theme_old = this.options.theme;
@@ -334,7 +334,10 @@ export default class jsMind {
334334
this.layout.collapse_all();
335335
this.view.relayout();
336336
}
337-
/** @param {number} depth */
337+
/**
338+
* Expand nodes up to a specified depth level.
339+
* @param {number} depth
340+
*/
338341
expand_to_depth(depth) {
339342
this.layout.expand_to_depth(depth);
340343
this.view.relayout();
@@ -412,12 +415,12 @@ export default class jsMind {
412415
return this.mind.get_node(node);
413416
}
414417
/**
415-
* Add a node under parent.
418+
* Add a new node to the mind map.
416419
* @param {string | import('./jsmind.node.js').Node} parent_node
417420
* @param {string} node_id
418421
* @param {string} topic
419422
* @param {Record<string, any>=} data
420-
* @param {number=} direction
423+
* @param {('left'|'center'|'right'|'-1'|'0'|'1'|number)=} direction - Direction for node placement. Supports string values ('left', 'center', 'right'), numeric strings ('-1', '0', '1'), and numbers (-1, 0, 1)
421424
* @returns {import('./jsmind.node.js').Node|null}
422425
*/
423426
add_node(parent_node, node_id, topic, data, direction) {
@@ -452,7 +455,7 @@ export default class jsMind {
452455
* @param {string} node_id
453456
* @param {string} topic
454457
* @param {Record<string, any>=} data
455-
* @param {number=} direction
458+
* @param {('left'|'center'|'right'|'-1'|'0'|'1'|number)=} direction - Direction for node placement. Supports string values ('left', 'center', 'right'), numeric strings ('-1', '0', '1'), and numbers (-1, 0, 1)
456459
* @returns {import('./jsmind.node.js').Node|null}
457460
*/
458461
insert_node_before(node_before, node_id, topic, data, direction) {
@@ -485,7 +488,7 @@ export default class jsMind {
485488
* @param {string} node_id
486489
* @param {string} topic
487490
* @param {Record<string, any>=} data
488-
* @param {number=} direction
491+
* @param {('left'|'center'|'right'|'-1'|'0'|'1'|number)=} direction - Direction for node placement. Supports string values ('left', 'center', 'right'), numeric strings ('-1', '0', '1'), and numbers (-1, 0, 1)
489492
* @returns {import('./jsmind.node.js').Node|null}
490493
*/
491494
insert_node_after(node_after, node_id, topic, data, direction) {
@@ -552,7 +555,11 @@ export default class jsMind {
552555
return false;
553556
}
554557
}
555-
/** @param {string} node_id @param {string} topic */
558+
/**
559+
* Update the topic (text content) of a node.
560+
* @param {string} node_id
561+
* @param {string} topic
562+
*/
556563
update_node(node_id, topic) {
557564
if (this.get_editable()) {
558565
if (_util.text.is_empty(topic)) {
@@ -584,9 +591,9 @@ export default class jsMind {
584591
/**
585592
* Move a node and optionally change direction.
586593
* @param {string} node_id
587-
* @param {string=} before_id
594+
* @param {string=} before_id - The ID of the node before which to place the moved node. Special values: "_first_", "_last_"
588595
* @param {string=} parent_id
589-
* @param {number=} direction
596+
* @param {('left'|'center'|'right'|'-1'|'0'|'1'|number)=} direction - Direction for node placement. Supports string values ('left', 'center', 'right'), numeric strings ('-1', '0', '1'), and numbers (-1, 0, 1). Only effective for second-level nodes (children of root). If not provided, direction will be determined automatically.
590597
*/
591598
move_node(node_id, before_id, parent_id, direction) {
592599
if (this.get_editable()) {
@@ -648,7 +655,10 @@ export default class jsMind {
648655
is_node_visible(node) {
649656
return this.layout.is_visible(node);
650657
}
651-
/** @param {string | import('./jsmind.node.js').Node} node */
658+
/**
659+
* Scroll the mind map to center the specified node.
660+
* @param {string | import('./jsmind.node.js').Node} node
661+
*/
652662
scroll_node_to_center(node) {
653663
if (!Node.is_node(node)) {
654664
var the_node = this.get_node(node);
@@ -740,6 +750,7 @@ export default class jsMind {
740750
return n;
741751
}
742752
/**
753+
* Set background and foreground colors for a node.
743754
* @param {string} node_id
744755
* @param {string=} bg_color
745756
* @param {string=} fg_color
@@ -763,6 +774,7 @@ export default class jsMind {
763774
}
764775
}
765776
/**
777+
* Set font style for a node.
766778
* @param {string} node_id
767779
* @param {number=} size
768780
* @param {string=} weight
@@ -793,8 +805,9 @@ export default class jsMind {
793805
}
794806
}
795807
/**
808+
* Set background image for a node.
796809
* @param {string} node_id
797-
* @param {string} image
810+
* @param {string=} image
798811
* @param {number=} width
799812
* @param {number=} height
800813
* @param {number=} rotation

src/jsmind.mind.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class Mind {
6363
* @param {string} node_id
6464
* @param {string} topic
6565
* @param {Record<string, any>=} data
66-
* @param {number=} direction
66+
* @param {('left'|'center'|'right'|'-1'|'0'|'1'|number)=} direction - Direction for node placement. Supports string values ('left', 'center', 'right'), numeric strings ('-1', '0', '1'), and numbers (-1, 0, 1)
6767
* @param {boolean=} expanded
6868
* @param {number=} idx
6969
* @returns {Node | null}
@@ -102,7 +102,7 @@ export class Mind {
102102
* @param {string} node_id
103103
* @param {string} topic
104104
* @param {Record<string, any>=} data
105-
* @param {number=} direction
105+
* @param {('left'|'center'|'right'|'-1'|'0'|'1'|number)=} direction - Direction for node placement. Supports string values ('left', 'center', 'right'), numeric strings ('-1', '0', '1'), and numbers (-1, 0, 1)
106106
* @returns {Node | null}
107107
*/
108108
insert_node_before(node_before, node_id, topic, data, direction) {
@@ -144,7 +144,7 @@ export class Mind {
144144
* @param {string} node_id
145145
* @param {string} topic
146146
* @param {Record<string, any>=} data
147-
* @param {number=} direction
147+
* @param {('left'|'center'|'right'|'-1'|'0'|'1'|number)=} direction - Direction for node placement. Supports string values ('left', 'center', 'right'), numeric strings ('-1', '0', '1'), and numbers (-1, 0, 1)
148148
* @returns {Node | null}
149149
*/
150150
insert_node_after(node_after, node_id, topic, data, direction) {
@@ -184,9 +184,9 @@ export class Mind {
184184
/**
185185
* Move a node to new parent/position.
186186
* @param {Node} node
187-
* @param {string=} before_id
187+
* @param {string=} before_id - The ID of the node before which to place the moved node. Special values: "_first_", "_last_"
188188
* @param {string=} parent_id
189-
* @param {number=} direction
189+
* @param {('left'|'center'|'right'|'-1'|'0'|'1'|number)=} direction - Direction for node placement. Supports string values ('left', 'center', 'right'), numeric strings ('-1', '0', '1'), and numbers (-1, 0, 1)
190190
* @returns {Node | null}
191191
*/
192192
move_node(node, before_id, parent_id, direction) {
@@ -202,7 +202,7 @@ export class Mind {
202202
/**
203203
* Propagate direction to descendants.
204204
* @param {Node} node
205-
* @param {number=} direction
205+
* @param {('left'|'center'|'right'|'-1'|'0'|'1'|number)=} direction - Direction for node placement. Supports string values ('left', 'center', 'right'), numeric strings ('-1', '0', '1'), and numbers (-1, 0, 1)
206206
*/
207207
_flow_node_direction(node, direction) {
208208
if (typeof direction === 'undefined') {
@@ -248,7 +248,7 @@ export class Mind {
248248
* @param {Node} node
249249
* @param {string} before_id
250250
* @param {string} parent_id
251-
* @param {number=} direction
251+
* @param {('left'|'center'|'right'|'-1'|'0'|'1'|number)=} direction - Direction for node placement. Supports string values ('left', 'center', 'right'), numeric strings ('-1', '0', '1'), and numbers (-1, 0, 1)
252252
* @returns {Node | null}
253253
*/
254254
_move_node(node, before_id, parent_id, direction) {

src/jsmind.option.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,31 @@ import { util } from './jsmind.util.js';
1111
/**
1212
* @typedef {{
1313
* container: string|HTMLElement,
14-
* editable: boolean,
15-
* theme: (string|null),
16-
* mode: ('full'|'side'),
17-
* support_html: boolean,
18-
* log_level: 'debug'|'info'|'warn'|'error'|'disable',
19-
* view: {
20-
* engine: 'canvas'|'svg',
21-
* enable_device_pixel_ratio: boolean,
22-
* hmargin: number,
23-
* vmargin: number,
24-
* line_width: number,
25-
* line_color: string,
26-
* line_style: 'curved'|'straight',
14+
* editable?: boolean,
15+
* theme?: (string|null),
16+
* mode?: ('full'|'side'),
17+
* support_html?: boolean,
18+
* log_level?: 'debug'|'info'|'warn'|'error'|'disable',
19+
* view?: {
20+
* engine?: 'canvas'|'svg',
21+
* enable_device_pixel_ratio?: boolean,
22+
* hmargin?: number,
23+
* vmargin?: number,
24+
* line_width?: number,
25+
* line_color?: string,
26+
* line_style?: 'curved'|'straight',
2727
* custom_line_render?: (this: object, arg:{ ctx: CanvasRenderingContext2D|SVGPathElement, start_point:{x:number,y:number}, end_point:{x:number,y:number} })=>void,
28-
* draggable: boolean,
29-
* hide_scrollbars_when_draggable: boolean,
30-
* node_overflow: 'hidden'|'wrap',
31-
* zoom: { min:number, max:number, step:number, mask_key:number },
32-
* custom_node_render: (null|((jm: import('./jsmind.js').default, ele: HTMLElement, node: import('./jsmind.node.js').Node)=>void)),
33-
* expander_style: 'char'|'number'
28+
* draggable?: boolean,
29+
* hide_scrollbars_when_draggable?: boolean,
30+
* node_overflow?: 'hidden'|'wrap'|'visible',
31+
* zoom?: { min?:number, max?:number, step?:number, mask_key?:number },
32+
* custom_node_render?: (null|((jm: import('./jsmind.js').default, ele: HTMLElement, node: import('./jsmind.node.js').Node)=>void)),
33+
* expander_style?: 'char'|'number'
3434
* },
35-
* layout: { hspace:number, vspace:number, pspace:number, cousin_space:number },
36-
* default_event_handle: { enable_mousedown_handle:boolean, enable_click_handle:boolean, enable_dblclick_handle:boolean, enable_mousewheel_handle:boolean },
37-
* shortcut: { enable:boolean, handles: Record<string,(jm: import('./jsmind.js').default, e: KeyboardEvent)=>void>, mapping: Record<string, number|number[]>, id_generator?: ()=>string },
38-
* plugin: Record<string, object>
35+
* layout?: { hspace?:number, vspace?:number, pspace?:number, cousin_space?:number },
36+
* default_event_handle?: { enable_mousedown_handle?:boolean, enable_click_handle?:boolean, enable_dblclick_handle?:boolean, enable_mousewheel_handle?:boolean },
37+
* shortcut?: { enable?:boolean, handles?: Record<string,(jm: import('./jsmind.js').default, e: KeyboardEvent)=>void>, mapping?: Record<string, number|number[]>, id_generator?: ()=>string },
38+
* plugin?: Record<string, object>
3939
* }} JsMindRuntimeOptions
4040
*/
4141
/** @type {JsMindRuntimeOptions} */
@@ -99,7 +99,7 @@ const default_options = {
9999

100100
/**
101101
* Merge user options with defaults. Throws if container missing.
102-
* @param {Partial<JsMindRuntimeOptions>} options
102+
* @param {JsMindRuntimeOptions} options
103103
* @returns {JsMindRuntimeOptions}
104104
*/
105105
export function merge_option(options) {

0 commit comments

Comments
 (0)