ts-visio 1.7.0 → 1.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -27,6 +27,7 @@ Built using specific schema-level abstractions to handle the complex internal st
27
27
  - **Document Metadata**: Read and write document properties (title, author, description, keywords, company, dates) via `doc.getMetadata()` / `doc.setMetadata()`.
28
28
  - **Named Connection Points**: Define specific ports on shapes (`Top`, `Right`, etc.) and connect to them precisely using `fromPort`/`toPort` on any connector API.
29
29
  - **StyleSheets**: Create document-level named styles with fill, line, and text properties via `doc.createStyle()` and apply them to shapes at creation time (`styleId`) or post-creation (`shape.applyStyle()`).
30
+ - **Color Palette**: Register named colors in the document's color table via `doc.addColor()` and look them up by index or hex value with `doc.getColors()` / `doc.getColorIndex()`.
30
31
 
31
32
  Feature gaps are being tracked in [FEATURES.md](./FEATURES.md).
32
33
 
@@ -153,7 +154,7 @@ await shape1.setStyle({ fillColor: '#00FF00' })
153
154
  Use specific arrowheads (Crow's Foot, etc.)
154
155
 
155
156
  ```typescript
156
- import { ArrowHeads } from 'ts-visio/utils/StyleHelpers';
157
+ import { ArrowHeads } from 'ts-visio';
157
158
 
158
159
  await page.connectShapes(shape1, shape2, ArrowHeads.One, ArrowHeads.CrowsFoot);
159
160
  // OR
@@ -449,7 +450,7 @@ Supported values: `'rectangle'` (default), `'ellipse'`, `'diamond'`, `'rounded-r
449
450
  Control line appearance and routing algorithm on any connector.
450
451
 
451
452
  ```typescript
452
- import { ArrowHeads } from 'ts-visio/utils/StyleHelpers';
453
+ import { ArrowHeads } from 'ts-visio';
453
454
 
454
455
  // Styled connector with crow's foot arrow and custom line
455
456
  await shape1.connectTo(shape2, ArrowHeads.One, ArrowHeads.CrowsFoot, {
@@ -664,6 +665,67 @@ const styles = doc.getStyles();
664
665
  `StyleProps` supports: `fillColor`, `lineColor`, `lineWeight` (pt), `linePattern`, `fontColor`, `fontSize` (pt), `bold`, `italic`, `underline`, `strikethrough`, `fontFamily`, `horzAlign`, `verticalAlign`, `spaceBefore`, `spaceAfter`, `lineSpacing`, `textMarginTop/Bottom/Left/Right` (in).
665
666
  Local shape properties always override inherited stylesheet values.
666
667
 
668
+ #### 30. Reading Connectors Back
669
+ Enumerate connectors on a page — including those loaded from an existing `.vsdx` file — and inspect or delete them.
670
+
671
+ ```typescript
672
+ import { VisioDocument } from 'ts-visio';
673
+
674
+ const doc = await VisioDocument.load(buffer); // or VisioDocument.create()
675
+ const page = doc.pages[0];
676
+
677
+ // Read all connector shapes
678
+ const connectors = page.getConnectors();
679
+
680
+ for (const conn of connectors) {
681
+ console.log(`Connector ${conn.id}: ${conn.fromShapeId} → ${conn.toShapeId}`);
682
+ console.log(' fromPort:', conn.fromPort); // 'center' | { name } | { index }
683
+ console.log(' toPort:', conn.toPort);
684
+ console.log(' style:', conn.style); // lineColor, lineWeight, linePattern, routing
685
+ console.log(' arrows:', conn.beginArrow, conn.endArrow);
686
+ }
687
+
688
+ // Delete a specific connector
689
+ await connectors[0].delete();
690
+
691
+ // After delete the connector is gone but the shapes remain
692
+ console.log(page.getConnectors().length); // 0
693
+ console.log(page.getShapes().length); // shapes still exist
694
+ ```
695
+
696
+ #### 31. Color Palette
697
+ Register colors in the document's indexed color table (`<Colors>` in `document.xml`). Colors are identified by an integer IX that you can reference anywhere a hex color is accepted.
698
+
699
+ ```typescript
700
+ // Register colors — returns the integer index (IX)
701
+ const blueIx = doc.addColor('#4472C4'); // → 2 (first user color)
702
+ const redIx = doc.addColor('#FF0000'); // → 3
703
+ const greenIx = doc.addColor('#00B050'); // → 4
704
+
705
+ // De-duplication: adding the same color returns the existing index
706
+ doc.addColor('#4472C4'); // → 2 (no duplicate created)
707
+
708
+ // Shorthand, missing #, and case variations are all normalised
709
+ doc.addColor('#FFF'); // same as #FFFFFF → returns 1 (built-in white)
710
+ doc.addColor('4472c4'); // same as #4472C4 → returns 2
711
+
712
+ // Read the full palette (sorted by index)
713
+ const palette = doc.getColors();
714
+ // [
715
+ // { index: 0, rgb: '#000000' }, // built-in black
716
+ // { index: 1, rgb: '#FFFFFF' }, // built-in white
717
+ // { index: 2, rgb: '#4472C4' },
718
+ // { index: 3, rgb: '#FF0000' },
719
+ // { index: 4, rgb: '#00B050' },
720
+ // ]
721
+
722
+ // Look up an index by hex value
723
+ doc.getColorIndex('#4472C4'); // → 2
724
+ doc.getColorIndex('#123456'); // → undefined (not registered)
725
+ ```
726
+
727
+ Built-in colors: IX 0 = `#000000` (black), IX 1 = `#FFFFFF` (white). User colors start at IX 2 and increment sequentially.
728
+
667
729
  ---
668
730
 
669
731
  ## Examples
@@ -0,0 +1,53 @@
1
+ import { ConnectorStyle, ConnectionTarget } from './types/VisioTypes';
2
+ import { ShapeModifier } from './ShapeModifier';
3
+ /**
4
+ * Raw data extracted from the page XML for a single connector shape.
5
+ * Returned by `ShapeReader.readConnectors()` and wrapped by the `Connector` class.
6
+ */
7
+ export interface ConnectorData {
8
+ /** Visio shape ID of the connector (1D shape). */
9
+ id: string;
10
+ /** ID of the shape at the connector's begin-point (BeginX). */
11
+ fromShapeId: string;
12
+ /** ID of the shape at the connector's end-point (EndX). */
13
+ toShapeId: string;
14
+ /** Connection point used on the from-shape. */
15
+ fromPort: ConnectionTarget;
16
+ /** Connection point used on the to-shape. */
17
+ toPort: ConnectionTarget;
18
+ /** Line style extracted from the connector's Line section. */
19
+ style: ConnectorStyle;
20
+ /** Begin-arrow head value (Visio ArrowHeads integer string). */
21
+ beginArrow: string;
22
+ /** End-arrow head value (Visio ArrowHeads integer string). */
23
+ endArrow: string;
24
+ }
25
+ /**
26
+ * A read/delete wrapper around a connector shape found on a page.
27
+ * Obtain instances via `page.getConnectors()`.
28
+ */
29
+ export declare class Connector {
30
+ private readonly pageId;
31
+ private readonly modifier;
32
+ /** Visio shape ID of this connector. */
33
+ readonly id: string;
34
+ /** ID of the shape this connector starts from. */
35
+ readonly fromShapeId: string;
36
+ /** ID of the shape this connector ends at. */
37
+ readonly toShapeId: string;
38
+ /** Connection point used on the from-shape ('center', `{ name }`, or `{ index }`). */
39
+ readonly fromPort: ConnectionTarget;
40
+ /** Connection point used on the to-shape ('center', `{ name }`, or `{ index }`). */
41
+ readonly toPort: ConnectionTarget;
42
+ /** Line style (color, weight, pattern, routing) for this connector. */
43
+ readonly style: ConnectorStyle;
44
+ /** Begin-arrow head value string (e.g. `'0'` = none, `'1'` = standard). */
45
+ readonly beginArrow: string;
46
+ /** End-arrow head value string (e.g. `'0'` = none, `'1'` = standard). */
47
+ readonly endArrow: string;
48
+ constructor(data: ConnectorData, pageId: string, modifier: ShapeModifier);
49
+ /**
50
+ * Delete this connector from the page (removes the shape and its Connect entries).
51
+ */
52
+ delete(): Promise<void>;
53
+ }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Connector = void 0;
4
+ /**
5
+ * A read/delete wrapper around a connector shape found on a page.
6
+ * Obtain instances via `page.getConnectors()`.
7
+ */
8
+ class Connector {
9
+ constructor(data, pageId, modifier) {
10
+ this.pageId = pageId;
11
+ this.modifier = modifier;
12
+ this.id = data.id;
13
+ this.fromShapeId = data.fromShapeId;
14
+ this.toShapeId = data.toShapeId;
15
+ this.fromPort = data.fromPort;
16
+ this.toPort = data.toPort;
17
+ this.style = data.style;
18
+ this.beginArrow = data.beginArrow;
19
+ this.endArrow = data.endArrow;
20
+ }
21
+ /**
22
+ * Delete this connector from the page (removes the shape and its Connect entries).
23
+ */
24
+ async delete() {
25
+ await this.modifier.deleteShape(this.pageId, this.id);
26
+ }
27
+ }
28
+ exports.Connector = Connector;
package/dist/Page.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { VisioPage, ConnectorStyle, PageOrientation, PageSizeName, ConnectionTarget } from './types/VisioTypes';
2
+ import { Connector } from './Connector';
2
3
  import { VisioPackage } from './VisioPackage';
3
4
  import { ShapeModifier } from './ShapeModifier';
4
5
  import { NewShapeProps } from './types/VisioTypes';
@@ -50,6 +51,12 @@ export declare class Page {
50
51
  */
51
52
  findShapes(predicate: (shape: Shape) => boolean): Shape[];
52
53
  addShape(props: NewShapeProps, parentId?: string): Promise<Shape>;
54
+ /**
55
+ * Return all connector shapes on the page.
56
+ * Each `Connector` exposes the from/to shape IDs, port targets, line style, arrows,
57
+ * and a `delete()` method to remove the connector.
58
+ */
59
+ getConnectors(): Connector[];
53
60
  connectShapes(fromShape: Shape, toShape: Shape, beginArrow?: string, endArrow?: string, style?: ConnectorStyle, fromPort?: ConnectionTarget, toPort?: ConnectionTarget): Promise<void>;
54
61
  addImage(data: Buffer, name: string, x: number, y: number, width: number, height: number): Promise<Shape>;
55
62
  addContainer(props: NewShapeProps): Promise<Shape>;
package/dist/Page.js CHANGED
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Page = void 0;
4
4
  const VisioTypes_1 = require("./types/VisioTypes");
5
+ const Connector_1 = require("./Connector");
5
6
  const ShapeReader_1 = require("./ShapeReader");
6
7
  const ShapeModifier_1 = require("./ShapeModifier");
7
8
  const Shape_1 = require("./Shape");
@@ -124,6 +125,22 @@ class Page {
124
125
  });
125
126
  return new Shape_1.Shape(internalStub, this.id, this.pkg, this.modifier);
126
127
  }
128
+ /**
129
+ * Return all connector shapes on the page.
130
+ * Each `Connector` exposes the from/to shape IDs, port targets, line style, arrows,
131
+ * and a `delete()` method to remove the connector.
132
+ */
133
+ getConnectors() {
134
+ const reader = new ShapeReader_1.ShapeReader(this.pkg);
135
+ try {
136
+ const data = reader.readConnectors(this.pagePath);
137
+ return data.map(d => new Connector_1.Connector(d, this.id, this.modifier));
138
+ }
139
+ catch (e) {
140
+ console.warn(`Could not read connectors for page ${this.id}:`, e);
141
+ return [];
142
+ }
143
+ }
127
144
  async connectShapes(fromShape, toShape, beginArrow, endArrow, style, fromPort, toPort) {
128
145
  await this.modifier.addConnector(this.id, fromShape.id, toShape.id, beginArrow, endArrow, style, fromPort, toPort);
129
146
  }
@@ -131,6 +131,12 @@ export declare class ShapeModifier {
131
131
  }
132
132
  export interface ShapeStyle {
133
133
  fillColor?: string;
134
+ /** Border/stroke colour as a CSS hex string (e.g. `'#cc0000'`). */
135
+ lineColor?: string;
136
+ /** Stroke weight in **points**. Stored internally as inches (pt / 72). */
137
+ lineWeight?: number;
138
+ /** Line pattern. 0 = none, 1 = solid (default), 2 = dash, 3 = dot, 4 = dash-dot. */
139
+ linePattern?: number;
134
140
  fontColor?: string;
135
141
  bold?: boolean;
136
142
  /** Italic text. */
@@ -400,6 +400,18 @@ class ShapeModifier {
400
400
  shape.Section = shape.Section.filter((s) => s['@_N'] !== 'Fill');
401
401
  shape.Section.push((0, StyleHelpers_1.createFillSection)(style.fillColor));
402
402
  }
403
+ // Update/Add Line
404
+ const hasLineProps = style.lineColor !== undefined
405
+ || style.lineWeight !== undefined
406
+ || style.linePattern !== undefined;
407
+ if (hasLineProps) {
408
+ shape.Section = shape.Section.filter((s) => s['@_N'] !== 'Line');
409
+ shape.Section.push((0, StyleHelpers_1.createLineSection)({
410
+ color: style.lineColor,
411
+ weight: style.lineWeight !== undefined ? (style.lineWeight / 72).toString() : undefined,
412
+ pattern: style.linePattern !== undefined ? style.linePattern.toString() : undefined,
413
+ }));
414
+ }
403
415
  // Update/Add Character (Font/Text Style)
404
416
  const hasCharProps = style.fontColor !== undefined
405
417
  || style.bold !== undefined
@@ -1,5 +1,6 @@
1
1
  import { VisioPackage } from './VisioPackage';
2
2
  import { VisioShape } from './types/VisioTypes';
3
+ import { ConnectorData } from './Connector';
3
4
  export declare class ShapeReader {
4
5
  private pkg;
5
6
  private parser;
@@ -15,6 +16,14 @@ export declare class ShapeReader {
15
16
  * Returns undefined if not found.
16
17
  */
17
18
  readShapeById(path: string, shapeId: string): VisioShape | undefined;
19
+ /**
20
+ * Read all connector shapes from a page XML file.
21
+ * A shape is considered a connector if it has `ObjType=2` or a `BeginX` cell,
22
+ * and is referenced in the page's `<Connects>` section.
23
+ */
24
+ readConnectors(path: string): ConnectorData[];
25
+ /** Decode a Visio ToPart integer string to a ConnectionTarget. */
26
+ private decodeToPart;
18
27
  private gatherShapes;
19
28
  private findShapeById;
20
29
  private parseShape;
@@ -63,6 +63,125 @@ class ShapeReader {
63
63
  return undefined;
64
64
  return this.findShapeById((0, VisioParsers_1.asArray)(shapesData), shapeId);
65
65
  }
66
+ /**
67
+ * Read all connector shapes from a page XML file.
68
+ * A shape is considered a connector if it has `ObjType=2` or a `BeginX` cell,
69
+ * and is referenced in the page's `<Connects>` section.
70
+ */
71
+ readConnectors(path) {
72
+ let content;
73
+ try {
74
+ content = this.pkg.getFileText(path);
75
+ }
76
+ catch {
77
+ return [];
78
+ }
79
+ const parsed = this.parser.parse(content);
80
+ const shapesData = parsed.PageContents?.Shapes?.Shape;
81
+ if (!shapesData)
82
+ return [];
83
+ // Build a flat map of all shapes by ID (including nested)
84
+ const shapeMap = new Map();
85
+ const collectShapes = (list) => {
86
+ for (const s of list) {
87
+ shapeMap.set(s['@_ID'], s);
88
+ if (s.Shapes?.Shape)
89
+ collectShapes((0, VisioParsers_1.asArray)(s.Shapes.Shape));
90
+ }
91
+ };
92
+ collectShapes((0, VisioParsers_1.asArray)(shapesData));
93
+ // Identify connector shapes by ObjType=2 or presence of BeginX cell
94
+ const connectorShapes = [];
95
+ for (const [, shape] of shapeMap) {
96
+ const cells = (0, VisioParsers_1.parseCells)(shape);
97
+ if (cells['ObjType']?.V === '2' || cells['BeginX'] !== undefined) {
98
+ connectorShapes.push(shape);
99
+ }
100
+ }
101
+ if (connectorShapes.length === 0)
102
+ return [];
103
+ // Group <Connect> elements by connector ID (FromSheet)
104
+ const connectsRaw = parsed.PageContents?.Connects?.Connect;
105
+ const connects = (0, VisioParsers_1.asArray)(connectsRaw);
106
+ const connectsByConnector = new Map();
107
+ for (const c of connects) {
108
+ const fromSheet = c['@_FromSheet'];
109
+ const fromCell = c['@_FromCell'];
110
+ if (!connectsByConnector.has(fromSheet)) {
111
+ connectsByConnector.set(fromSheet, {});
112
+ }
113
+ const entry = connectsByConnector.get(fromSheet);
114
+ if (fromCell === 'BeginX')
115
+ entry.beginConnect = c;
116
+ else if (fromCell === 'EndX')
117
+ entry.endConnect = c;
118
+ }
119
+ const ROUTING_BY_VALUE = {
120
+ '1': 'orthogonal',
121
+ '2': 'straight',
122
+ '16': 'curved',
123
+ };
124
+ const result = [];
125
+ for (const connShape of connectorShapes) {
126
+ const connId = connShape['@_ID'];
127
+ const entry = connectsByConnector.get(connId);
128
+ if (!entry?.beginConnect || !entry?.endConnect)
129
+ continue;
130
+ const { beginConnect, endConnect } = entry;
131
+ const fromShapeId = beginConnect['@_ToSheet'];
132
+ const toShapeId = endConnect['@_ToSheet'];
133
+ const fromPort = this.decodeToPart(beginConnect['@_ToPart']);
134
+ const toPort = this.decodeToPart(endConnect['@_ToPart']);
135
+ // Extract line style from the connector's Line section
136
+ const sections = (0, VisioParsers_1.asArray)(connShape.Section);
137
+ let lineColor;
138
+ let lineWeight;
139
+ let linePattern;
140
+ for (const sec of sections) {
141
+ if (sec['@_N'] === 'Line') {
142
+ const lineCells = (0, VisioParsers_1.parseCells)(sec);
143
+ if (lineCells['LineColor']?.V)
144
+ lineColor = lineCells['LineColor'].V;
145
+ if (lineCells['LineWeight']?.V)
146
+ lineWeight = parseFloat(lineCells['LineWeight'].V) * 72; // in→pt
147
+ if (lineCells['LinePattern']?.V)
148
+ linePattern = parseInt(lineCells['LinePattern'].V, 10);
149
+ }
150
+ }
151
+ const cells = (0, VisioParsers_1.parseCells)(connShape);
152
+ const routeStyleVal = cells['ShapeRouteStyle']?.V;
153
+ const routing = routeStyleVal ? ROUTING_BY_VALUE[routeStyleVal] : undefined;
154
+ const style = {};
155
+ if (lineColor !== undefined)
156
+ style.lineColor = lineColor;
157
+ if (lineWeight !== undefined)
158
+ style.lineWeight = lineWeight;
159
+ if (linePattern !== undefined)
160
+ style.linePattern = linePattern;
161
+ if (routing !== undefined)
162
+ style.routing = routing;
163
+ result.push({
164
+ id: connId,
165
+ fromShapeId,
166
+ toShapeId,
167
+ fromPort,
168
+ toPort,
169
+ style,
170
+ beginArrow: cells['BeginArrow']?.V ?? '0',
171
+ endArrow: cells['EndArrow']?.V ?? '0',
172
+ });
173
+ }
174
+ return result;
175
+ }
176
+ /** Decode a Visio ToPart integer string to a ConnectionTarget. */
177
+ decodeToPart(toPart) {
178
+ const part = toPart !== undefined ? parseInt(toPart, 10) : 3;
179
+ if (isNaN(part) || part === 3)
180
+ return 'center';
181
+ if (part >= 100)
182
+ return { index: part - 100 };
183
+ return 'center';
184
+ }
66
185
  gatherShapes(rawShapes, result) {
67
186
  for (const s of rawShapes) {
68
187
  result.push(this.parseShape(s));
@@ -1,5 +1,5 @@
1
1
  import { Page } from './Page';
2
- import { DocumentMetadata, StyleProps, StyleRecord } from './types/VisioTypes';
2
+ import { DocumentMetadata, StyleProps, StyleRecord, ColorEntry } from './types/VisioTypes';
3
3
  export declare class VisioDocument {
4
4
  private pkg;
5
5
  private pageManager;
@@ -7,6 +7,7 @@ export declare class VisioDocument {
7
7
  private mediaManager;
8
8
  private metadataManager;
9
9
  private styleSheetManager;
10
+ private colorManager;
10
11
  private constructor();
11
12
  static create(): Promise<VisioDocument>;
12
13
  static load(pathOrBuffer: string | Buffer | ArrayBuffer | Uint8Array): Promise<VisioDocument>;
@@ -56,5 +57,43 @@ export declare class VisioDocument {
56
57
  * Return all stylesheets defined in the document (including built-in styles).
57
58
  */
58
59
  getStyles(): StyleRecord[];
60
+ /**
61
+ * Add a color to the document-level color palette and return its integer index (IX).
62
+ *
63
+ * If the color is already registered the existing index is returned without
64
+ * creating a duplicate. The two built-in colors are always present:
65
+ * - index 0 → `#000000` (black)
66
+ * - index 1 → `#FFFFFF` (white)
67
+ *
68
+ * User colors receive indices starting at 2.
69
+ *
70
+ * @param hex CSS hex string — `'#4472C4'`, `'#ABC'`, or `'4472c4'` are all accepted.
71
+ * @returns Integer IX that uniquely identifies this color in the palette.
72
+ *
73
+ * @example
74
+ * const blueIx = doc.addColor('#4472C4'); // → 2
75
+ * const redIx = doc.addColor('#FF0000'); // → 3
76
+ * doc.addColor('#4472C4'); // → 2 (de-duplicated)
77
+ */
78
+ addColor(hex: string): number;
79
+ /**
80
+ * Return all color entries in the document palette, ordered by index.
81
+ *
82
+ * @example
83
+ * doc.addColor('#4472C4');
84
+ * doc.getColors();
85
+ * // → [{ index: 0, rgb: '#000000' }, { index: 1, rgb: '#FFFFFF' }, { index: 2, rgb: '#4472C4' }]
86
+ */
87
+ getColors(): ColorEntry[];
88
+ /**
89
+ * Look up the palette index of a color by its hex value.
90
+ * Returns `undefined` if the color has not been added to the palette.
91
+ *
92
+ * @example
93
+ * doc.addColor('#4472C4');
94
+ * doc.getColorIndex('#4472C4'); // → 2
95
+ * doc.getColorIndex('#FF0000'); // → undefined
96
+ */
97
+ getColorIndex(hex: string): number | undefined;
59
98
  save(filename?: string): Promise<Buffer>;
60
99
  }
@@ -40,6 +40,7 @@ const Page_1 = require("./Page");
40
40
  const MediaManager_1 = require("./core/MediaManager");
41
41
  const MetadataManager_1 = require("./core/MetadataManager");
42
42
  const StyleSheetManager_1 = require("./core/StyleSheetManager");
43
+ const ColorManager_1 = require("./core/ColorManager");
43
44
  class VisioDocument {
44
45
  constructor(pkg) {
45
46
  this.pkg = pkg;
@@ -48,6 +49,7 @@ class VisioDocument {
48
49
  this.mediaManager = new MediaManager_1.MediaManager(pkg);
49
50
  this.metadataManager = new MetadataManager_1.MetadataManager(pkg);
50
51
  this.styleSheetManager = new StyleSheetManager_1.StyleSheetManager(pkg);
52
+ this.colorManager = new ColorManager_1.ColorManager(pkg);
51
53
  }
52
54
  static async create() {
53
55
  const pkg = await VisioPackage_1.VisioPackage.create();
@@ -169,6 +171,50 @@ class VisioDocument {
169
171
  getStyles() {
170
172
  return this.styleSheetManager.getStyles();
171
173
  }
174
+ /**
175
+ * Add a color to the document-level color palette and return its integer index (IX).
176
+ *
177
+ * If the color is already registered the existing index is returned without
178
+ * creating a duplicate. The two built-in colors are always present:
179
+ * - index 0 → `#000000` (black)
180
+ * - index 1 → `#FFFFFF` (white)
181
+ *
182
+ * User colors receive indices starting at 2.
183
+ *
184
+ * @param hex CSS hex string — `'#4472C4'`, `'#ABC'`, or `'4472c4'` are all accepted.
185
+ * @returns Integer IX that uniquely identifies this color in the palette.
186
+ *
187
+ * @example
188
+ * const blueIx = doc.addColor('#4472C4'); // → 2
189
+ * const redIx = doc.addColor('#FF0000'); // → 3
190
+ * doc.addColor('#4472C4'); // → 2 (de-duplicated)
191
+ */
192
+ addColor(hex) {
193
+ return this.colorManager.addColor(hex);
194
+ }
195
+ /**
196
+ * Return all color entries in the document palette, ordered by index.
197
+ *
198
+ * @example
199
+ * doc.addColor('#4472C4');
200
+ * doc.getColors();
201
+ * // → [{ index: 0, rgb: '#000000' }, { index: 1, rgb: '#FFFFFF' }, { index: 2, rgb: '#4472C4' }]
202
+ */
203
+ getColors() {
204
+ return this.colorManager.getColors();
205
+ }
206
+ /**
207
+ * Look up the palette index of a color by its hex value.
208
+ * Returns `undefined` if the color has not been added to the palette.
209
+ *
210
+ * @example
211
+ * doc.addColor('#4472C4');
212
+ * doc.getColorIndex('#4472C4'); // → 2
213
+ * doc.getColorIndex('#FF0000'); // → undefined
214
+ */
215
+ getColorIndex(hex) {
216
+ return this.colorManager.getColorIndex(hex);
217
+ }
172
218
  async save(filename) {
173
219
  return this.pkg.save(filename);
174
220
  }
@@ -0,0 +1,57 @@
1
+ import { VisioPackage } from '../VisioPackage';
2
+ import { ColorEntry } from '../types/VisioTypes';
3
+ /**
4
+ * Manages the document-level color palette stored in `<Colors>` inside
5
+ * `visio/document.xml`.
6
+ *
7
+ * The palette is a simple indexed table — Visio identifies colors by their
8
+ * zero-based integer index (IX) as well as by their hex RGB value.
9
+ *
10
+ * Built-in entries (always present):
11
+ * - IX 0 → #000000 (black)
12
+ * - IX 1 → #FFFFFF (white)
13
+ *
14
+ * User-added colors receive sequential indices starting at 2.
15
+ */
16
+ export declare class ColorManager {
17
+ private pkg;
18
+ private parser;
19
+ private builder;
20
+ constructor(pkg: VisioPackage);
21
+ /**
22
+ * Add a color to the document palette and return its integer index (IX).
23
+ *
24
+ * If the color is already in the palette the existing index is returned
25
+ * without creating a duplicate. Built-in colors (black = 0, white = 1)
26
+ * are returned directly.
27
+ *
28
+ * @param hex CSS hex string — `'#4472C4'`, `'#abc'`, `'4472c4'` all accepted.
29
+ * @returns Integer IX that can be used as a color reference.
30
+ *
31
+ * @example
32
+ * const ix = doc.addColor('#4472C4'); // → 2 (first user color)
33
+ * await page.addShape({ text: 'Hi', fillColor: '#4472C4', ... });
34
+ */
35
+ addColor(hex: string): number;
36
+ /**
37
+ * Return all color entries currently in the document palette,
38
+ * ordered by index ascending.
39
+ */
40
+ getColors(): ColorEntry[];
41
+ /**
42
+ * Look up the index of a color in the palette by its hex value.
43
+ * Returns `undefined` if the color has not been registered.
44
+ *
45
+ * @example
46
+ * const ix = doc.getColorIndex('#4472C4'); // 2, or undefined if not added
47
+ */
48
+ getColorIndex(hex: string): number | undefined;
49
+ private getParsedDoc;
50
+ private saveParsedDoc;
51
+ /** Ensure `<Colors>` exists and contains the two built-in entries. */
52
+ private ensureColors;
53
+ /** Next IX = max existing index + 1, always at least 2. */
54
+ private nextIndex;
55
+ /** Minimal built-in palette returned when <Colors> is absent. */
56
+ private builtIns;
57
+ }
@@ -0,0 +1,137 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ColorManager = void 0;
4
+ const XmlHelper_1 = require("../utils/XmlHelper");
5
+ /**
6
+ * Manages the document-level color palette stored in `<Colors>` inside
7
+ * `visio/document.xml`.
8
+ *
9
+ * The palette is a simple indexed table — Visio identifies colors by their
10
+ * zero-based integer index (IX) as well as by their hex RGB value.
11
+ *
12
+ * Built-in entries (always present):
13
+ * - IX 0 → #000000 (black)
14
+ * - IX 1 → #FFFFFF (white)
15
+ *
16
+ * User-added colors receive sequential indices starting at 2.
17
+ */
18
+ class ColorManager {
19
+ constructor(pkg) {
20
+ this.pkg = pkg;
21
+ this.parser = (0, XmlHelper_1.createXmlParser)();
22
+ this.builder = (0, XmlHelper_1.createXmlBuilder)();
23
+ }
24
+ // ── Public API ────────────────────────────────────────────────────────────
25
+ /**
26
+ * Add a color to the document palette and return its integer index (IX).
27
+ *
28
+ * If the color is already in the palette the existing index is returned
29
+ * without creating a duplicate. Built-in colors (black = 0, white = 1)
30
+ * are returned directly.
31
+ *
32
+ * @param hex CSS hex string — `'#4472C4'`, `'#abc'`, `'4472c4'` all accepted.
33
+ * @returns Integer IX that can be used as a color reference.
34
+ *
35
+ * @example
36
+ * const ix = doc.addColor('#4472C4'); // → 2 (first user color)
37
+ * await page.addShape({ text: 'Hi', fillColor: '#4472C4', ... });
38
+ */
39
+ addColor(hex) {
40
+ const normalized = normalizeHex(hex);
41
+ const parsed = this.getParsedDoc();
42
+ const doc = parsed['VisioDocument'];
43
+ this.ensureColors(doc);
44
+ const colors = doc['Colors'];
45
+ const entries = toArray(colors['ColorEntry']);
46
+ // De-duplicate: return existing index if the color is already registered
47
+ const existing = entries.find((e) => normalizeHex(e['@_RGB']) === normalized);
48
+ if (existing)
49
+ return parseInt(existing['@_IX'], 10);
50
+ // Append at the next sequential index
51
+ const nextIX = this.nextIndex(entries);
52
+ entries.push({ '@_IX': nextIX.toString(), '@_RGB': normalized });
53
+ colors['ColorEntry'] = entries;
54
+ this.saveParsedDoc(parsed);
55
+ return nextIX;
56
+ }
57
+ /**
58
+ * Return all color entries currently in the document palette,
59
+ * ordered by index ascending.
60
+ */
61
+ getColors() {
62
+ const parsed = this.getParsedDoc();
63
+ const doc = parsed['VisioDocument'];
64
+ const colors = doc?.['Colors'];
65
+ if (!colors)
66
+ return this.builtIns();
67
+ return toArray(colors['ColorEntry'])
68
+ .map((e) => ({
69
+ index: parseInt(e['@_IX'], 10),
70
+ rgb: normalizeHex(e['@_RGB']),
71
+ }))
72
+ .sort((a, b) => a.index - b.index);
73
+ }
74
+ /**
75
+ * Look up the index of a color in the palette by its hex value.
76
+ * Returns `undefined` if the color has not been registered.
77
+ *
78
+ * @example
79
+ * const ix = doc.getColorIndex('#4472C4'); // 2, or undefined if not added
80
+ */
81
+ getColorIndex(hex) {
82
+ const normalized = normalizeHex(hex);
83
+ return this.getColors().find(e => e.rgb === normalized)?.index;
84
+ }
85
+ // ── Private helpers ───────────────────────────────────────────────────────
86
+ getParsedDoc() {
87
+ const xml = this.pkg.getFileText('visio/document.xml');
88
+ return this.parser.parse(xml);
89
+ }
90
+ saveParsedDoc(parsed) {
91
+ this.pkg.updateFile('visio/document.xml', (0, XmlHelper_1.buildXml)(this.builder, parsed));
92
+ }
93
+ /** Ensure `<Colors>` exists and contains the two built-in entries. */
94
+ ensureColors(doc) {
95
+ if (!doc['Colors'])
96
+ doc['Colors'] = {};
97
+ const colors = doc['Colors'];
98
+ const entries = toArray(colors['ColorEntry']);
99
+ if (!entries.some((e) => e['@_IX'] === '0')) {
100
+ entries.unshift({ '@_IX': '0', '@_RGB': '#000000' });
101
+ }
102
+ if (!entries.some((e) => e['@_IX'] === '1')) {
103
+ const blackPos = entries.findIndex((e) => e['@_IX'] === '0');
104
+ entries.splice(blackPos + 1, 0, { '@_IX': '1', '@_RGB': '#FFFFFF' });
105
+ }
106
+ colors['ColorEntry'] = entries;
107
+ }
108
+ /** Next IX = max existing index + 1, always at least 2. */
109
+ nextIndex(entries) {
110
+ const max = entries.reduce((m, e) => {
111
+ const ix = parseInt(e['@_IX'], 10);
112
+ return isNaN(ix) ? m : Math.max(m, ix);
113
+ }, 1);
114
+ return max + 1;
115
+ }
116
+ /** Minimal built-in palette returned when <Colors> is absent. */
117
+ builtIns() {
118
+ return [
119
+ { index: 0, rgb: '#000000' },
120
+ { index: 1, rgb: '#FFFFFF' },
121
+ ];
122
+ }
123
+ }
124
+ exports.ColorManager = ColorManager;
125
+ // ── Module-level helpers ──────────────────────────────────────────────────────
126
+ /** Normalise any hex string to uppercase `#RRGGBB`. */
127
+ function normalizeHex(hex) {
128
+ let h = hex.startsWith('#') ? hex.slice(1) : hex;
129
+ if (h.length === 3)
130
+ h = h[0] + h[0] + h[1] + h[1] + h[2] + h[2];
131
+ return '#' + h.toUpperCase();
132
+ }
133
+ function toArray(val) {
134
+ if (!val)
135
+ return [];
136
+ return Array.isArray(val) ? val : [val];
137
+ }
package/dist/index.d.ts CHANGED
@@ -6,8 +6,12 @@ export { ShapeModifier } from './ShapeModifier';
6
6
  export { VisioDocument } from './VisioDocument';
7
7
  export { Page } from './Page';
8
8
  export { Shape } from './Shape';
9
+ export { Connector } from './Connector';
10
+ export type { ConnectorData } from './Connector';
9
11
  export type { ShapeData, ShapeHyperlink } from './Shape';
10
12
  export { Layer } from './Layer';
11
13
  export { SchemaDiagram } from './SchemaDiagram';
12
14
  export { VisioValidator } from './core/VisioValidator';
13
15
  export * from './types/VisioTypes';
16
+ export { ArrowHeads, hexToRgb } from './utils/StyleHelpers';
17
+ export type { ShapeStyle } from './ShapeModifier';
package/dist/index.js CHANGED
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.VisioValidator = exports.SchemaDiagram = exports.Layer = exports.Shape = exports.Page = exports.VisioDocument = exports.ShapeModifier = exports.ShapeReader = exports.PageManager = exports.VisioPackage = void 0;
17
+ exports.hexToRgb = exports.ArrowHeads = exports.VisioValidator = exports.SchemaDiagram = exports.Layer = exports.Connector = exports.Shape = exports.Page = exports.VisioDocument = exports.ShapeModifier = exports.ShapeReader = exports.PageManager = exports.VisioPackage = void 0;
18
18
  var VisioPackage_1 = require("./VisioPackage");
19
19
  Object.defineProperty(exports, "VisioPackage", { enumerable: true, get: function () { return VisioPackage_1.VisioPackage; } });
20
20
  var PageManager_1 = require("./core/PageManager");
@@ -29,6 +29,8 @@ var Page_1 = require("./Page");
29
29
  Object.defineProperty(exports, "Page", { enumerable: true, get: function () { return Page_1.Page; } });
30
30
  var Shape_1 = require("./Shape");
31
31
  Object.defineProperty(exports, "Shape", { enumerable: true, get: function () { return Shape_1.Shape; } });
32
+ var Connector_1 = require("./Connector");
33
+ Object.defineProperty(exports, "Connector", { enumerable: true, get: function () { return Connector_1.Connector; } });
32
34
  var Layer_1 = require("./Layer");
33
35
  Object.defineProperty(exports, "Layer", { enumerable: true, get: function () { return Layer_1.Layer; } });
34
36
  var SchemaDiagram_1 = require("./SchemaDiagram");
@@ -36,3 +38,6 @@ Object.defineProperty(exports, "SchemaDiagram", { enumerable: true, get: functio
36
38
  var VisioValidator_1 = require("./core/VisioValidator");
37
39
  Object.defineProperty(exports, "VisioValidator", { enumerable: true, get: function () { return VisioValidator_1.VisioValidator; } });
38
40
  __exportStar(require("./types/VisioTypes"), exports);
41
+ var StyleHelpers_1 = require("./utils/StyleHelpers");
42
+ Object.defineProperty(exports, "ArrowHeads", { enumerable: true, get: function () { return StyleHelpers_1.ArrowHeads; } });
43
+ Object.defineProperty(exports, "hexToRgb", { enumerable: true, get: function () { return StyleHelpers_1.hexToRgb; } });
@@ -146,6 +146,19 @@ export interface ConnectorStyle {
146
146
  }
147
147
  /** Non-rectangular geometry variants supported by ShapeBuilder. */
148
148
  export type ShapeGeometry = 'rectangle' | 'ellipse' | 'diamond' | 'rounded-rectangle' | 'triangle' | 'parallelogram';
149
+ /**
150
+ * A single entry in the document-level color palette (`<Colors>` in `document.xml`).
151
+ * Returned by `doc.getColors()` and created via `doc.addColor()`.
152
+ */
153
+ export interface ColorEntry {
154
+ /**
155
+ * Zero-based integer index (IX). Can be passed as a color reference
156
+ * anywhere a hex string is accepted (e.g. `fillColor`, `lineColor`).
157
+ */
158
+ index: number;
159
+ /** Normalized CSS hex string, always uppercase `#RRGGBB`. */
160
+ rgb: string;
161
+ }
149
162
  /** A reference to a document-level stylesheet, returned by `doc.createStyle()`. */
150
163
  export interface StyleRecord {
151
164
  /** Zero-based integer ID used as `LineStyle` / `FillStyle` / `TextStyle` on shapes. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-visio",
3
- "version": "1.7.0",
3
+ "version": "1.10.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {
@@ -25,6 +25,7 @@
25
25
  },
26
26
  "devDependencies": {
27
27
  "@types/node": "^25.0.9",
28
+ "tsx": "^4.21.0",
28
29
  "typescript": "^5.9.3",
29
30
  "vitest": "^4.0.17"
30
31
  }