@zseven-w/pen-core 0.7.0 → 0.7.2

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.
Files changed (3) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +164 -21
  3. package/package.json +16 -2
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ZSeven—W
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,89 +1,232 @@
1
1
  # @zseven-w/pen-core
2
2
 
3
- Core document operations for [OpenPencil](https://github.com/nicepkg/openpencil) — tree manipulation, layout engine, design variables, boolean path operations, and more.
3
+ Core document operations for [OpenPencil](https://github.com/ZSeven-W/openpencil) — tree manipulation, layout engine, design variables, boolean path operations, 3-way merge, and more.
4
4
 
5
5
  ## Install
6
6
 
7
7
  ```bash
8
8
  npm install @zseven-w/pen-core
9
+ # or
10
+ bun add @zseven-w/pen-core
9
11
  ```
10
12
 
13
+ ## Overview
14
+
15
+ `pen-core` is the foundation layer of the OpenPencil stack. It provides pure functions for every document operation — tree CRUD, auto-layout computation, variable resolution, SVG path booleans, and document normalization. All operations are immutable (structural sharing) and framework-free.
16
+
11
17
  ## Features
12
18
 
13
19
  ### Document Tree Operations
14
20
 
15
21
  Create, query, and mutate the document tree:
16
22
 
17
- ```ts
23
+ ```typescript
18
24
  import {
19
25
  createEmptyDocument,
20
26
  findNodeInTree,
27
+ findParentInTree,
21
28
  insertNodeInTree,
22
29
  removeNodeFromTree,
23
30
  updateNodeInTree,
24
- deepCloneNode,
25
31
  flattenNodes,
32
+ isDescendantOf,
33
+ getNodeBounds,
26
34
  } from '@zseven-w/pen-core';
27
35
 
28
36
  const doc = createEmptyDocument();
29
37
  const node = findNodeInTree(doc.children, 'node-id');
38
+ const parent = findParentInTree(doc.children, 'node-id');
39
+ const flat = flattenNodes(doc.children); // all nodes in flat array
40
+ ```
41
+
42
+ ### Node Cloning
43
+
44
+ Deep clone nodes with optional ID regeneration:
45
+
46
+ ```typescript
47
+ import { deepCloneNode, cloneNodeWithNewIds, cloneNodesWithNewIds } from '@zseven-w/pen-core';
48
+
49
+ const clone = deepCloneNode(node); // preserve IDs
50
+ const fresh = cloneNodeWithNewIds(node); // new IDs for all descendants
51
+ const batch = cloneNodesWithNewIds([a, b, c]); // batch clone
30
52
  ```
31
53
 
32
54
  ### Multi-Page Support
33
55
 
34
- ```ts
35
- import { getActivePage, getActivePageChildren, migrateToPages } from '@zseven-w/pen-core';
56
+ ```typescript
57
+ import {
58
+ getActivePage,
59
+ getActivePageChildren,
60
+ setActivePageChildren,
61
+ migrateToPages,
62
+ ensureDocumentNodeIds,
63
+ } from '@zseven-w/pen-core';
64
+
65
+ // Migrate a single-page document to multi-page format
66
+ const multiPageDoc = migrateToPages(doc);
36
67
  ```
37
68
 
38
69
  ### Layout Engine
39
70
 
40
- Automatic layout computation with auto-sizing, padding, and gap support:
71
+ Flexbox-like auto-layout computation supporting `fill_container`, `fit_content`, gap, padding, and alignment:
41
72
 
42
- ```ts
73
+ ```typescript
43
74
  import {
44
- inferLayout,
45
75
  computeLayoutPositions,
76
+ inferLayout,
46
77
  fitContentWidth,
47
78
  fitContentHeight,
79
+ resolvePadding,
80
+ getNodeWidth,
81
+ getNodeHeight,
82
+ isNodeVisible,
48
83
  } from '@zseven-w/pen-core';
84
+
85
+ // Compute absolute positions for all children in a layout container
86
+ const positions = computeLayoutPositions(frame, frame.children);
87
+
88
+ // Infer layout direction from child arrangement
89
+ const layout = inferLayout(children); // 'horizontal' | 'vertical' | 'none'
90
+ ```
91
+
92
+ ### Text Measurement
93
+
94
+ Estimate text dimensions for layout without a browser DOM:
95
+
96
+ ```typescript
97
+ import {
98
+ estimateTextWidth,
99
+ estimateTextWidthPrecise,
100
+ estimateTextHeight,
101
+ resolveTextContent,
102
+ hasCjkText,
103
+ defaultLineHeight,
104
+ } from '@zseven-w/pen-core';
105
+
106
+ const width = estimateTextWidth('Hello World', 16, 400); // font size, weight
107
+ const height = estimateTextHeight(textNode, containerWidth);
108
+ const isCjk = hasCjkText('こんにちは'); // true
49
109
  ```
50
110
 
51
111
  ### Design Variables
52
112
 
53
- Resolve `$variable` references against theme axes:
113
+ Resolve `$variable` references against document variables and theme axes:
54
114
 
55
- ```ts
115
+ ```typescript
56
116
  import {
117
+ isVariableRef,
57
118
  resolveVariableRef,
58
119
  resolveNodeForCanvas,
59
120
  replaceVariableRefsInTree,
121
+ getDefaultTheme,
60
122
  } from '@zseven-w/pen-core';
123
+
124
+ isVariableRef('$primary'); // true
125
+
126
+ // Resolve all $refs in a node tree for rendering
127
+ const resolved = resolveNodeForCanvas(node, doc.variables, doc.themes);
128
+
129
+ // Rename $old-name → $new-name across entire tree
130
+ replaceVariableRefsInTree(children, 'old-name', 'new-name');
61
131
  ```
62
132
 
63
133
  ### Boolean Path Operations
64
134
 
65
135
  Union, subtract, intersect, and exclude paths via Paper.js:
66
136
 
67
- ```ts
68
- import { executeBooleanOp, BooleanOpType } from '@zseven-w/pen-core';
137
+ ```typescript
138
+ import { executeBooleanOp, canBooleanOp, BooleanOpType } from '@zseven-w/pen-core';
139
+
140
+ if (canBooleanOp(selectedNodes)) {
141
+ const result = executeBooleanOp(selectedNodes, BooleanOpType.Union);
142
+ }
69
143
  ```
70
144
 
71
- ### Text Measurement
145
+ ### Document Normalization
146
+
147
+ Sanitize and fix documents from external sources (Figma imports, AI generation):
72
148
 
73
- Estimate text dimensions for layout without a browser:
149
+ ```typescript
150
+ import { normalizePenDocument } from '@zseven-w/pen-core';
74
151
 
75
- ```ts
76
- import { estimateTextWidth, estimateTextHeight } from '@zseven-w/pen-core';
152
+ const cleaned = normalizePenDocument(rawDoc);
153
+ // Fixes: fill type "color" → "solid", gradient stop position → offset,
154
+ // sizing strings, padding arrays. Preserves $variable refs.
77
155
  ```
78
156
 
79
- ### Document Normalization
157
+ ### Layout Normalization
80
158
 
81
- Sanitize and fix documents imported from external sources:
159
+ Repair AI-generated layout issues:
82
160
 
83
- ```ts
84
- import { normalizePenDocument } from '@zseven-w/pen-core';
161
+ ```typescript
162
+ import {
163
+ normalizeTreeLayout,
164
+ unwrapFakePhoneMockups,
165
+ stripRedundantSectionFills,
166
+ normalizeStrokeFillSchema,
167
+ } from '@zseven-w/pen-core';
168
+
169
+ // Infer missing layout modes, strip child x/y in layout containers
170
+ normalizeTreeLayout(rootNode);
171
+ ```
172
+
173
+ ### 3-Way Document Merge
174
+
175
+ Diff and merge document trees for collaborative editing and git integration:
176
+
177
+ ```typescript
178
+ import { diffDocuments, mergeDocuments } from '@zseven-w/pen-core';
179
+
180
+ // One-direction diff: base → current
181
+ const patches = diffDocuments(base, current);
182
+ // patches: NodePatch[] — add, remove, modify, move
183
+
184
+ // 3-way merge: base + ours + theirs
185
+ const result = mergeDocuments(base, ours, theirs);
186
+ // result: { document, conflicts }
85
187
  ```
86
188
 
189
+ ### Design.md Parser
190
+
191
+ Parse and generate design specification documents:
192
+
193
+ ```typescript
194
+ import {
195
+ parseDesignMd,
196
+ generateDesignMd,
197
+ designMdColorsToVariables,
198
+ extractDesignMdFromDocument,
199
+ } from '@zseven-w/pen-core';
200
+ ```
201
+
202
+ ### Path Anchors
203
+
204
+ Convert between anchor point representation and SVG path data:
205
+
206
+ ```typescript
207
+ import {
208
+ anchorsToPathData,
209
+ pathDataToAnchors,
210
+ getPathBoundsFromAnchors,
211
+ inferPathAnchorPointType,
212
+ } from '@zseven-w/pen-core';
213
+ ```
214
+
215
+ ## API Reference
216
+
217
+ | Category | Key Functions |
218
+ | ------------- | ---------------------------------------------------------------------------------------------- |
219
+ | **Tree CRUD** | `findNodeInTree`, `insertNodeInTree`, `removeNodeFromTree`, `updateNodeInTree`, `flattenNodes` |
220
+ | **Cloning** | `deepCloneNode`, `cloneNodeWithNewIds`, `cloneNodesWithNewIds` |
221
+ | **Pages** | `getActivePage`, `migrateToPages`, `ensureDocumentNodeIds` |
222
+ | **Layout** | `computeLayoutPositions`, `inferLayout`, `fitContentWidth`, `fitContentHeight` |
223
+ | **Text** | `estimateTextWidth`, `estimateTextHeight`, `hasCjkText` |
224
+ | **Variables** | `resolveVariableRef`, `resolveNodeForCanvas`, `replaceVariableRefsInTree` |
225
+ | **Boolean** | `executeBooleanOp`, `canBooleanOp` |
226
+ | **Normalize** | `normalizePenDocument`, `normalizeTreeLayout` |
227
+ | **Merge** | `diffDocuments`, `mergeDocuments` |
228
+ | **IDs** | `generateId` |
229
+
87
230
  ## License
88
231
 
89
- MIT
232
+ [MIT](./LICENSE)
package/package.json CHANGED
@@ -1,7 +1,21 @@
1
1
  {
2
2
  "name": "@zseven-w/pen-core",
3
- "version": "0.7.0",
3
+ "version": "0.7.2",
4
4
  "description": "Core document operations, tree utils, variables, layout engine for OpenPencil",
5
+ "homepage": "https://github.com/ZSeven-W/openpencil/tree/main/packages/pen-core",
6
+ "bugs": {
7
+ "url": "https://github.com/ZSeven-W/openpencil/issues"
8
+ },
9
+ "license": "MIT",
10
+ "author": {
11
+ "name": "ZSeven-W",
12
+ "email": "xkayshen@gmail.com"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "https://github.com/ZSeven-W/openpencil.git",
17
+ "directory": "packages/pen-core"
18
+ },
5
19
  "files": [
6
20
  "src"
7
21
  ],
@@ -16,7 +30,7 @@
16
30
  "typecheck": "tsc --noEmit"
17
31
  },
18
32
  "dependencies": {
19
- "@zseven-w/pen-types": "0.7.0",
33
+ "@zseven-w/pen-types": "0.7.2",
20
34
  "nanoid": "^5.1.6",
21
35
  "paper": "^0.12.18"
22
36
  },