@aseity/igrid-react 0.0.0-stage → 0.1.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.
@@ -0,0 +1,148 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { useLayoutEffect, useRef, useState, } from "react";
3
+ export function useSelection(workbook, sheet, panes, zoom, headerWidth, headerHeight) {
4
+ const [state, setState] = useState(() => {
5
+ const saved = workbook.view(sheet);
6
+ const [anchor, focus = anchor] = saved.selection.split(":");
7
+ return { anchor, focus, mode: 0, active: saved.activeCell };
8
+ });
9
+ const dragging = useRef(false);
10
+ const pendingReveal = useRef(undefined);
11
+ useLayoutEffect(() => {
12
+ const pending = pendingReveal.current;
13
+ if (!pending)
14
+ return;
15
+ pendingReveal.current = undefined;
16
+ const rect = workbook.selection(sheet, pending.target).rectangle;
17
+ const saved = workbook.view(sheet);
18
+ const element = pending.element;
19
+ element.scrollLeft =
20
+ visibleScroll(rect.x, rect.width, saved.frozenWidth, element.scrollLeft / zoom, element.clientWidth / zoom - headerWidth) * zoom;
21
+ element.scrollTop =
22
+ visibleScroll(rect.y, rect.height, saved.frozenHeight, element.scrollTop / zoom, element.clientHeight / zoom - headerHeight) * zoom;
23
+ });
24
+ const selection = workbook.selection(sheet, state.anchor, state.focus, state.mode);
25
+ const activeSelection = workbook.selection(sheet, state.active);
26
+ const active = activeSelection.activeCell;
27
+ const cell = workbook.cell(sheet, active);
28
+ const locate = (event) => {
29
+ const rect = event.currentTarget.getBoundingClientRect();
30
+ const x = (event.clientX - rect.left) / zoom - headerWidth;
31
+ const y = (event.clientY - rect.top) / zoom - headerHeight;
32
+ const pane = panes.find((pane) => Math.max(0, x) >= pane.left &&
33
+ Math.max(0, x) < pane.left + pane.width &&
34
+ Math.max(0, y) >= pane.top &&
35
+ Math.max(0, y) < pane.top + pane.height);
36
+ if (!pane || (x < 0 && y < 0))
37
+ return undefined;
38
+ return {
39
+ reference: workbook.hitTest(sheet, Math.max(0, x) - pane.left + pane.x, Math.max(0, y) - pane.top + pane.y),
40
+ mode: x < 0 ? 1 : y < 0 ? 2 : 0,
41
+ };
42
+ };
43
+ return {
44
+ selection,
45
+ multiple: selection.address !== activeSelection.address,
46
+ extent: {
47
+ width: state.mode === 1
48
+ ? 0
49
+ : selection.rectangle.x + selection.rectangle.width,
50
+ height: state.mode === 2
51
+ ? 0
52
+ : selection.rectangle.y + selection.rectangle.height,
53
+ },
54
+ onCopy(event) {
55
+ event.preventDefault();
56
+ event.clipboardData.setData("text/plain", workbook.copySelection(sheet, state.anchor, state.focus, state.mode));
57
+ },
58
+ formulaText: cell?.formula ??
59
+ (typeof cell?.value === "boolean"
60
+ ? cell.value
61
+ ? "TRUE"
62
+ : "FALSE"
63
+ : String(cell?.value ?? "")),
64
+ onKeyDown(event) {
65
+ const key = event.key === "Tab"
66
+ ? event.shiftKey
67
+ ? "ArrowLeft"
68
+ : "ArrowRight"
69
+ : event.key === "Enter"
70
+ ? event.shiftKey
71
+ ? "ArrowUp"
72
+ : "ArrowDown"
73
+ : event.key;
74
+ const control = event.ctrlKey || event.metaKey;
75
+ if (!key.startsWith("Arrow") &&
76
+ key !== "PageUp" &&
77
+ key !== "PageDown" &&
78
+ !(control && (key === "Home" || key === "End")))
79
+ return;
80
+ event.preventDefault();
81
+ const extend = event.shiftKey && event.key !== "Tab" && event.key !== "Enter";
82
+ const target = workbook.navigate(sheet, extend ? state.focus : active, key, control, Math.max(1, event.currentTarget.clientHeight / zoom -
83
+ headerHeight -
84
+ workbook.view(sheet).frozenHeight));
85
+ setState((previous) => ({
86
+ anchor: extend ? previous.anchor : target,
87
+ focus: target,
88
+ active: extend ? previous.active : target,
89
+ mode: 0,
90
+ }));
91
+ pendingReveal.current = { element: event.currentTarget, target };
92
+ },
93
+ pointer: {
94
+ onPointerDown(event) {
95
+ if (event.button !== 0)
96
+ return;
97
+ const hit = locate(event);
98
+ if (!hit)
99
+ return;
100
+ event.preventDefault();
101
+ event.currentTarget.focus({ preventScroll: true });
102
+ event.currentTarget.setPointerCapture(event.pointerId);
103
+ dragging.current = true;
104
+ setState((previous) => ({
105
+ anchor: event.shiftKey ? previous.anchor : hit.reference,
106
+ focus: hit.reference,
107
+ active: event.shiftKey ? previous.active : hit.reference,
108
+ mode: hit.mode,
109
+ }));
110
+ },
111
+ onPointerMove(event) {
112
+ if (!dragging.current)
113
+ return;
114
+ const hit = locate(event);
115
+ if (hit)
116
+ setState((previous) => ({ ...previous, focus: hit.reference }));
117
+ },
118
+ onPointerUp() {
119
+ dragging.current = false;
120
+ },
121
+ onLostPointerCapture() {
122
+ dragging.current = false;
123
+ },
124
+ },
125
+ };
126
+ }
127
+ export function SelectionOutline({ rectangle, pane, multiple, }) {
128
+ return (_jsx("div", { className: "igrid-selection", "aria-hidden": "true", style: {
129
+ position: "absolute",
130
+ pointerEvents: "none",
131
+ left: rectangle.x - pane.x,
132
+ top: rectangle.y - pane.y,
133
+ width: rectangle.width,
134
+ height: rectangle.height,
135
+ boxSizing: "border-box",
136
+ border: "2px solid var(--igrid-active-border)",
137
+ background: multiple ? "rgb(0 0 0 / 10%)" : "transparent",
138
+ } }));
139
+ }
140
+ function visibleScroll(start, size, frozen, scroll, available) {
141
+ if (start < frozen)
142
+ return scroll;
143
+ if (start < frozen + scroll)
144
+ return Math.max(0, start - frozen);
145
+ if (start + size > scroll + available)
146
+ return Math.max(0, start + size - available);
147
+ return scroll;
148
+ }
package/dist/style.css ADDED
@@ -0,0 +1,150 @@
1
+ .igrid {
2
+ display: flex;
3
+ flex-direction: column;
4
+ height: 100%;
5
+ min-height: 0;
6
+ --igrid-background: #fff;
7
+ --igrid-text: #202020;
8
+ --igrid-header-background: #f3f4f6;
9
+ --igrid-header-text: #555;
10
+ --igrid-grid-line: #dedede;
11
+ --igrid-border: #ccc;
12
+ --igrid-active-background: #e6f1ff;
13
+ --igrid-active-border: #3174ba;
14
+ color-scheme: light;
15
+ background: var(--igrid-background);
16
+ color: var(--igrid-text);
17
+ font-family: system-ui, sans-serif;
18
+ }
19
+ .igrid nav {
20
+ display: flex;
21
+ gap: 4px;
22
+ padding: 8px 0;
23
+ }
24
+ .igrid button,
25
+ .igrid input {
26
+ border: 1px solid var(--igrid-border);
27
+ border-radius: 4px;
28
+ padding: 6px 12px;
29
+ background: var(--igrid-background);
30
+ color: var(--igrid-text);
31
+ }
32
+ .igrid button {
33
+ cursor: pointer;
34
+ }
35
+ .igrid button[aria-pressed="true"] {
36
+ background: var(--igrid-active-background);
37
+ border-color: var(--igrid-active-border);
38
+ }
39
+ .igrid-zoom {
40
+ font-size: 13px;
41
+ }
42
+ .igrid-zoom input {
43
+ box-sizing: content-box;
44
+ width: 4em;
45
+ margin: 0 4px;
46
+ }
47
+ .igrid-formula {
48
+ display: flex;
49
+ align-items: center;
50
+ gap: 0;
51
+ padding: 4px 12px;
52
+ border-bottom: 1px solid
53
+ color-mix(in srgb, var(--igrid-border) 45%, transparent);
54
+ }
55
+ .igrid-formula input {
56
+ box-sizing: border-box;
57
+ height: 24px;
58
+ border: 0;
59
+ border-radius: 0;
60
+ padding: 2px 12px;
61
+ background: transparent;
62
+ font: inherit;
63
+ font-size: 13px;
64
+ }
65
+ .igrid-formula input:focus-visible {
66
+ outline: none;
67
+ }
68
+ .igrid-formula > input {
69
+ flex: 0 0 80px;
70
+ width: 80px;
71
+ border-right: 1px solid
72
+ color-mix(in srgb, var(--igrid-border) 45%, transparent);
73
+ color: var(--igrid-header-text);
74
+ font-size: 12px;
75
+ font-variant-numeric: tabular-nums;
76
+ }
77
+ .igrid-formula-symbol {
78
+ display: grid;
79
+ place-items: center;
80
+ flex: 0 0 36px;
81
+ color: color-mix(in srgb, var(--igrid-header-text) 55%, transparent);
82
+ user-select: none;
83
+ }
84
+ .igrid-formula-value {
85
+ position: relative;
86
+ flex: 1;
87
+ min-width: 0;
88
+ height: 24px;
89
+ --igrid-formula-function: #8250a5;
90
+ --igrid-formula-reference: #2463a0;
91
+ --igrid-formula-string: #397047;
92
+ --igrid-formula-number: #9a5a23;
93
+ }
94
+ .igrid-formula-value input,
95
+ .igrid-formula-highlight {
96
+ width: 100%;
97
+ padding: 2px 12px 2px 0;
98
+ font: inherit;
99
+ font-size: 13px;
100
+ line-height: 20px;
101
+ letter-spacing: normal;
102
+ }
103
+ .igrid-formula-value input {
104
+ position: relative;
105
+ color: transparent;
106
+ caret-color: var(--igrid-text);
107
+ }
108
+ .igrid-formula-highlight {
109
+ position: absolute;
110
+ inset: 0;
111
+ box-sizing: border-box;
112
+ overflow: hidden;
113
+ white-space: pre;
114
+ pointer-events: none;
115
+ }
116
+ .igrid-formula-highlight > span {
117
+ display: block;
118
+ }
119
+ .igrid-formula-function {
120
+ color: var(--igrid-formula-function);
121
+ }
122
+ .igrid-formula-reference,
123
+ .igrid-formula-cell {
124
+ color: var(--igrid-formula-reference);
125
+ }
126
+ .igrid-formula-string {
127
+ color: var(--igrid-formula-string);
128
+ }
129
+ .igrid-formula-number {
130
+ color: var(--igrid-formula-number);
131
+ }
132
+ @media (forced-colors: active) {
133
+ .igrid-formula-highlight {
134
+ display: none;
135
+ }
136
+ .igrid-formula-value input {
137
+ color: FieldText;
138
+ }
139
+ }
140
+
141
+ .igrid-viewport {
142
+ position: relative;
143
+ flex: 1;
144
+ min-height: 0;
145
+ }
146
+ .igrid-formula,
147
+ .igrid nav,
148
+ .igrid-zoom {
149
+ flex-shrink: 0;
150
+ }
package/dist/text.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import type { DrawList } from "@aseity/igrid-engine";
2
+ export declare function drawText(context: CanvasRenderingContext2D, list: DrawList, index: number): void;
package/dist/text.js ADDED
@@ -0,0 +1,202 @@
1
+ export function drawText(context, list, index) {
2
+ const [x, y, width, height] = list.rectangles.subarray(index * 4, index * 4 + 4);
3
+ const [horizontal, vertical, indent, wrap, kind] = list.text_layout.subarray(index * 5, index * 5 + 5);
4
+ const [size, flags, color] = list.cell_styles.subarray(index * 5, index * 5 + 3);
5
+ const base = style(list.font_names[index], size, flags, list.text_colors[index] || color);
6
+ context.font = base.font;
7
+ const padding = 2 + indent * context.measureText(" ").width;
8
+ const available = Math.max(0, width - padding - 2);
9
+ let segments = textSegments(context, list, index, base, available);
10
+ const naturalWidth = segments.reduce((sum, segment) => sum + segment.width, 0);
11
+ if (kind === 2 && naturalWidth > available) {
12
+ context.font = base.font;
13
+ const hashes = "#".repeat(Math.max(1, Math.floor(available / context.measureText("#").width)));
14
+ segments = [
15
+ { ...base, text: hashes, width: context.measureText(hashes).width },
16
+ ];
17
+ }
18
+ const lines = wrap ? wrapLines(context, segments, available) : [segments];
19
+ const lineHeight = ((Math.max(base.size, ...segments.map((segment) => segment.size)) * 96) /
20
+ 72) *
21
+ 1.25;
22
+ context.textBaseline = "alphabetic";
23
+ const metrics = lines
24
+ .flat()
25
+ .filter((segment) => !segment.spacing)
26
+ .map((segment) => {
27
+ context.font = segment.font;
28
+ return context.measureText("Mg");
29
+ });
30
+ const ascent = Math.max(0, ...metrics.map((metric) => metric.fontBoundingBoxAscent));
31
+ const descent = Math.max(0, ...metrics.map((metric) => metric.fontBoundingBoxDescent));
32
+ const blockHeight = (lines.length - 1) * lineHeight + ascent + descent;
33
+ const top = vertical === 0
34
+ ? y + 2
35
+ : vertical === 1
36
+ ? y + (height - blockHeight) / 2
37
+ : y + height - blockHeight - 1;
38
+ const [left, right] = list.text_bounds.subarray(index * 2, index * 2 + 2);
39
+ context.save();
40
+ context.beginPath();
41
+ context.rect(left + 1, y + 1, Math.max(0, right - left - 2), Math.max(0, height - 2));
42
+ context.clip();
43
+ context.textAlign = "left";
44
+ context.textBaseline = "alphabetic";
45
+ lines.forEach((line, lineIndex) => {
46
+ const lineWidth = line.reduce((sum, segment) => sum + segment.width, 0);
47
+ let cursor = horizontal === 3
48
+ ? x + width - padding - lineWidth
49
+ : horizontal === 2
50
+ ? x + (width - lineWidth) / 2
51
+ : x + padding;
52
+ const baseline = top + lineIndex * lineHeight + ascent;
53
+ for (const segment of line) {
54
+ context.font = segment.font;
55
+ context.fillStyle = segment.color;
56
+ if (!segment.spacing)
57
+ context.fillText(segment.text, cursor, baseline);
58
+ decorations(context, segment, cursor, baseline);
59
+ cursor += segment.width;
60
+ }
61
+ });
62
+ context.restore();
63
+ }
64
+ function style(name, size, flags, rgba) {
65
+ return {
66
+ size,
67
+ flags,
68
+ color: `#${rgba.toString(16).padStart(8, "0")}`,
69
+ font: `${flags & 2 ? "italic " : ""}${flags & 1 ? "bold " : ""}${(size * 96) / 72}px ${JSON.stringify(name)}, Calibri, Carlito, Arial, sans-serif`,
70
+ };
71
+ }
72
+ function textSegments(context, list, index, base, available) {
73
+ const text = list.strings[list.text_indices[index]];
74
+ const segments = [];
75
+ for (let i = 0; i < list.text_runs.length; i += 6) {
76
+ const [cell, start, length, size, flags, color] = list.text_runs.subarray(i, i + 6);
77
+ if (cell !== index)
78
+ continue;
79
+ const run = style(list.run_font_names[i / 6], size, flags, color);
80
+ context.font = run.font;
81
+ const value = text.slice(start, start + length);
82
+ segments.push({
83
+ ...run,
84
+ text: value,
85
+ width: context.measureText(value).width,
86
+ });
87
+ }
88
+ if (segments.length)
89
+ return segments;
90
+ context.font = base.font;
91
+ const spacing = glyphs(list.text_spacing, index);
92
+ const fills = glyphs(list.text_fills, index);
93
+ let natural = context.measureText(text).width;
94
+ for (const glyph of spacing.values())
95
+ natural +=
96
+ context.measureText(glyph).width - context.measureText(" ").width;
97
+ let segment = "";
98
+ const flush = () => {
99
+ if (segment)
100
+ segments.push({
101
+ ...base,
102
+ text: segment,
103
+ width: context.measureText(segment).width,
104
+ });
105
+ segment = "";
106
+ };
107
+ for (let offset = 0; offset <= text.length; offset++) {
108
+ const fill = fills.get(offset), space = spacing.get(offset);
109
+ if (fill || space)
110
+ flush();
111
+ if (fill) {
112
+ const glyphWidth = context.measureText(fill).width;
113
+ const repeated = fill.repeat(glyphWidth > 0
114
+ ? Math.max(0, Math.floor((available - natural) / glyphWidth))
115
+ : 0);
116
+ segments.push({
117
+ ...base,
118
+ text: repeated,
119
+ width: context.measureText(repeated).width,
120
+ });
121
+ }
122
+ if (space)
123
+ segments.push({
124
+ ...base,
125
+ text: "",
126
+ spacing: true,
127
+ width: context.measureText(space).width,
128
+ });
129
+ else if (offset < text.length)
130
+ segment += text[offset];
131
+ }
132
+ flush();
133
+ return segments;
134
+ }
135
+ function glyphs(data, index) {
136
+ const result = new Map();
137
+ for (let i = 0; i < data.length; i += 3)
138
+ if (data[i] === index)
139
+ result.set(data[i + 1], String.fromCodePoint(data[i + 2]));
140
+ return result;
141
+ }
142
+ function wrapLines(context, segments, available) {
143
+ const lines = [[]];
144
+ let width = 0;
145
+ const newline = () => {
146
+ lines.push([]);
147
+ width = 0;
148
+ };
149
+ for (const segment of segments) {
150
+ context.font = segment.font;
151
+ for (const token of segment.text.split(/(\n|[^\S\n]+)/)) {
152
+ if (!token)
153
+ continue;
154
+ if (token === "\n") {
155
+ newline();
156
+ continue;
157
+ }
158
+ let tokenWidth = context.measureText(token).width;
159
+ if (width && width + tokenWidth > available)
160
+ newline();
161
+ if (!width && /^\s+$/.test(token))
162
+ continue;
163
+ if (tokenWidth <= available) {
164
+ lines[lines.length - 1].push({
165
+ ...segment,
166
+ text: token,
167
+ width: tokenWidth,
168
+ });
169
+ width += tokenWidth;
170
+ continue;
171
+ }
172
+ for (const character of token) {
173
+ tokenWidth = context.measureText(character).width;
174
+ if (width && width + tokenWidth > available)
175
+ newline();
176
+ lines[lines.length - 1].push({
177
+ ...segment,
178
+ text: character,
179
+ width: tokenWidth,
180
+ });
181
+ width += tokenWidth;
182
+ }
183
+ }
184
+ }
185
+ return lines;
186
+ }
187
+ function decorations(context, segment, x, y) {
188
+ context.strokeStyle = segment.color;
189
+ context.lineWidth = Math.max(1, segment.size / 11);
190
+ context.setLineDash([]);
191
+ for (const offset of [
192
+ segment.flags & 4 ? 2 : null,
193
+ segment.flags & 8 ? -segment.size * 0.4 : null,
194
+ ]) {
195
+ if (offset === null)
196
+ continue;
197
+ context.beginPath();
198
+ context.moveTo(x, y + offset);
199
+ context.lineTo(x + segment.width, y + offset);
200
+ context.stroke();
201
+ }
202
+ }
package/package.json CHANGED
@@ -1,6 +1,35 @@
1
1
  {
2
2
  "name": "@aseity/igrid-react",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "exports": {
6
+ ".": {
7
+ "types": "./dist/index.d.ts",
8
+ "default": "./dist/index.js"
9
+ },
10
+ "./package.json": "./package.json",
11
+ "./styles.css": "./dist/style.css"
12
+ },
13
+ "dependencies": {
14
+ "@aseity/igrid-engine": "0.1.0",
15
+ "echarts": "6.1.0"
16
+ },
17
+ "peerDependencies": {
18
+ "react": "^19.0.0"
19
+ },
20
+ "files": [
21
+ "dist",
22
+ "README.md",
23
+ "CHANGELOG.md",
24
+ "LICENSE"
25
+ ],
26
+ "sideEffects": [
27
+ "**/*.css"
28
+ ],
29
+ "description": "Read-only React XLSX viewer powered by Rust/WASM and Canvas2D.",
30
+ "license": "MIT",
31
+ "publishConfig": {
32
+ "access": "public",
33
+ "registry": "https://registry.npmjs.org/"
34
+ }
35
+ }