sfora-cli 0.9.0 → 0.11.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 +147 -6
- package/dist/SforaFs.js +278 -10
- package/dist/api-client.d.ts +290 -5
- package/dist/api-client.js +307 -22
- package/dist/block-commands.d.ts +84 -0
- package/dist/block-commands.js +155 -0
- package/dist/cli.js +323 -29
- package/dist/format/__tests__/byteStable.d.ts +5 -0
- package/dist/format/__tests__/byteStable.js +64 -0
- package/dist/format/blockSplice.d.ts +135 -0
- package/dist/format/blockSplice.js +330 -0
- package/dist/format/blocks/dropClosure.d.ts +81 -0
- package/dist/format/blocks/dropClosure.js +196 -0
- package/dist/format/blocks/markdown-block-catalog.d.ts +18 -0
- package/dist/format/blocks/markdown-block-catalog.js +162 -0
- package/dist/format/blocks/markdown-block-ids.d.mts +1 -0
- package/dist/format/blocks/markdown-block-ids.mjs +25 -0
- package/dist/format/blocks/parsers.d.ts +105 -0
- package/dist/format/blocks/parsers.js +442 -0
- package/dist/format/blocks/structured-block-schema.d.ts +8 -0
- package/dist/format/blocks/structured-block-schema.js +30 -0
- package/dist/format/callout.d.ts +128 -0
- package/dist/format/callout.js +227 -0
- package/dist/format/cardMarkdown.d.ts +2 -0
- package/dist/format/cardMarkdown.js +10 -0
- package/dist/format/checklist.d.ts +34 -0
- package/dist/format/checklist.js +158 -0
- package/dist/format/formatAxes.d.ts +228 -0
- package/dist/format/formatAxes.js +454 -0
- package/dist/format/index.d.ts +19 -4
- package/dist/format/index.js +28 -4
- package/dist/format/lineGeometry.d.ts +100 -0
- package/dist/format/lineGeometry.js +424 -0
- package/dist/format/lint/appliesTo.d.ts +92 -0
- package/dist/format/lint/appliesTo.js +369 -0
- package/dist/format/lint/config.d.ts +106 -0
- package/dist/format/lint/config.js +205 -0
- package/dist/format/lint/fixAll.d.ts +62 -0
- package/dist/format/lint/fixAll.js +107 -0
- package/dist/format/lint/frontmatterSchema.d.ts +181 -0
- package/dist/format/lint/frontmatterSchema.js +660 -0
- package/dist/format/lint/index.d.ts +49 -0
- package/dist/format/lint/index.js +51 -0
- package/dist/format/lint/lintSource.d.ts +56 -0
- package/dist/format/lint/lintSource.js +188 -0
- package/dist/format/lint/rules/broken-wiki-link.d.ts +2 -0
- package/dist/format/lint/rules/broken-wiki-link.js +45 -0
- package/dist/format/lint/rules/frontmatter-schema.d.ts +2 -0
- package/dist/format/lint/rules/frontmatter-schema.js +92 -0
- package/dist/format/lint/rules/index.d.ts +11 -0
- package/dist/format/lint/rules/index.js +32 -0
- package/dist/format/lint/rules/malformed-callout.d.ts +2 -0
- package/dist/format/lint/rules/malformed-callout.js +88 -0
- package/dist/format/lint/rules/malformed-checklist.d.ts +2 -0
- package/dist/format/lint/rules/malformed-checklist.js +65 -0
- package/dist/format/lint/rules/malformed-frontmatter.d.ts +2 -0
- package/dist/format/lint/rules/malformed-frontmatter.js +98 -0
- package/dist/format/lint/rules/malformed-structured-block.d.ts +2 -0
- package/dist/format/lint/rules/malformed-structured-block.js +134 -0
- package/dist/format/lint/rules/malformed-wiki-link.d.ts +2 -0
- package/dist/format/lint/rules/malformed-wiki-link.js +43 -0
- package/dist/format/lint/rules/orphan-reference.d.ts +2 -0
- package/dist/format/lint/rules/orphan-reference.js +87 -0
- package/dist/format/lint/severity.d.ts +15 -0
- package/dist/format/lint/severity.js +50 -0
- package/dist/format/lint/textEdits.d.ts +86 -0
- package/dist/format/lint/textEdits.js +162 -0
- package/dist/format/lint/types.d.ts +116 -0
- package/dist/format/lint/types.js +16 -0
- package/dist/format/markdown/dates.js +2 -0
- package/dist/format/markdown/document.js +2 -0
- package/dist/format/markdown/index.js +2 -0
- package/dist/format/markdown/mentions.js +2 -0
- package/dist/format/markdown/slug.d.ts +28 -0
- package/dist/format/markdown/slug.js +65 -0
- package/dist/format/markdown/yaml.js +2 -0
- package/dist/format/noteMarkdown.js +2 -0
- package/dist/format/parseWithFallback.d.ts +13 -0
- package/dist/format/parseWithFallback.js +98 -0
- package/dist/format/plaintext.d.ts +5 -0
- package/dist/format/plaintext.js +51 -0
- package/dist/format/postMarkdown.js +3 -1
- package/dist/format/sheetCellSpans.d.ts +95 -0
- package/dist/format/sheetCellSpans.js +223 -0
- package/dist/format/sheetSelection.d.ts +136 -0
- package/dist/format/sheetSelection.js +282 -0
- package/dist/format/taskUploadFilename.d.ts +6 -0
- package/dist/format/taskUploadFilename.js +13 -0
- package/dist/format/textStats.d.ts +23 -0
- package/dist/format/textStats.js +80 -0
- package/dist/format/wayfinder.d.ts +50 -0
- package/dist/format/wayfinder.js +203 -0
- package/dist/format/wikiLinks.d.ts +78 -0
- package/dist/format/wikiLinks.js +266 -0
- package/dist/index.d.ts +26 -1
- package/dist/index.js +20 -3
- package/dist/mcp-server.js +5 -2
- package/dist/opener.d.ts +23 -0
- package/dist/opener.js +26 -0
- package/dist/render.d.ts +132 -0
- package/dist/render.js +208 -0
- package/dist/shell-commands.d.ts +34 -0
- package/dist/shell-commands.js +108 -0
- package/dist/watch.d.ts +79 -0
- package/dist/watch.js +113 -0
- package/dist/web-url.d.ts +39 -0
- package/dist/web-url.js +63 -0
- package/package.json +7 -6
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where every sheet cell lives in the authored bytes.
|
|
3
|
+
*
|
|
4
|
+
* `parseSheet` hands the reader a grid of DECODED strings — trimmed, escapes
|
|
5
|
+
* resolved, short rows padded. That is the right shape to draw and the wrong
|
|
6
|
+
* shape to write back through: re-serializing the whole grid from it would
|
|
7
|
+
* rewrite the author's column padding, their outer pipes, their delimiter row,
|
|
8
|
+
* every byte they lined up by hand.
|
|
9
|
+
*
|
|
10
|
+
* So an edit is a splice, not a re-serialization. This module answers "which
|
|
11
|
+
* byte range is that cell?" and writes a single cell in place. Everything the
|
|
12
|
+
* author wrote outside the edited cell survives untouched, which is what makes
|
|
13
|
+
* the no-op case byte-identical by construction rather than by luck:
|
|
14
|
+
* `writeSheetCell(source, r, c, currentText)` returns the same string back.
|
|
15
|
+
*
|
|
16
|
+
* The escape rules here are a second reading of the same grammar `sheetCells`
|
|
17
|
+
* in `blocks/parsers.ts` reads, and a second reading is only trustworthy if
|
|
18
|
+
* something checks it against the first. `sheetCellSpans.test.ts` does: every
|
|
19
|
+
* span's slice is fed back through `parseSheet` and must equal the cell the
|
|
20
|
+
* parser reports. A divergence is a test failure, never a silent misalignment.
|
|
21
|
+
*
|
|
22
|
+
* The CODE-SPAN half is no longer a second reading at all. While this module
|
|
23
|
+
* lived in `src/components/editor/` it could not reach the engine's private
|
|
24
|
+
* helpers, so it carried a hand-rolled `codeSpanMask` that closed a backtick
|
|
25
|
+
* run with `indexOf` — which lets a longer run close a shorter one, where
|
|
26
|
+
* CommonMark (and `lineGeometry.codeSpanMask`, which `sheetCells` uses) wants
|
|
27
|
+
* the closing run to be exactly as long as the opening one. Moving the file
|
|
28
|
+
* into the engine (board card #278) retires the copy: both readings of "where
|
|
29
|
+
* are the code spans" are now the one function, so the two cannot drift.
|
|
30
|
+
*/
|
|
31
|
+
/** One cell's trimmed content, addressed in the block source. */
|
|
32
|
+
export type SheetCellSpan = {
|
|
33
|
+
/** Offset of the first byte of trimmed content. */
|
|
34
|
+
start: number;
|
|
35
|
+
/** Offset just past the last byte. `start === end` for an empty cell. */
|
|
36
|
+
end: number;
|
|
37
|
+
/** The text the reader shows — the same string `parseSheet` reports. */
|
|
38
|
+
text: string;
|
|
39
|
+
};
|
|
40
|
+
/** One source line's cells, plus where a missing trailing cell would go. */
|
|
41
|
+
export type SheetRowSpan = {
|
|
42
|
+
/** Cells present in the source. May be shorter than the column count. */
|
|
43
|
+
cells: SheetCellSpan[];
|
|
44
|
+
/** End-of-line offset, where a short row grows. */
|
|
45
|
+
appendAt: number;
|
|
46
|
+
/** Whether the line closes with a pipe, which decides the append shape. */
|
|
47
|
+
trailingPipe: boolean;
|
|
48
|
+
};
|
|
49
|
+
export type SheetCellGrid = {
|
|
50
|
+
columns: number;
|
|
51
|
+
header: SheetRowSpan;
|
|
52
|
+
rows: SheetRowSpan[];
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Build the source-anchored grid, or null when the source is not a sheet the
|
|
56
|
+
* reader would draw — the same null the renderer already falls back on.
|
|
57
|
+
*
|
|
58
|
+
* The cell TEXTS come from `parseSheet`, never from this file's own decoding:
|
|
59
|
+
* one reader of the grammar owns what a cell says, and this module only owns
|
|
60
|
+
* where it sits. When the two disagree about how many cells the header has,
|
|
61
|
+
* the grid degrades to null instead of handing back a misaligned map.
|
|
62
|
+
*/
|
|
63
|
+
export declare function sheetCellGrid(source: string): SheetCellGrid | null;
|
|
64
|
+
/**
|
|
65
|
+
* Encode a value for a table cell.
|
|
66
|
+
*
|
|
67
|
+
* The inverse of the engine's decoding over the alphabet that decoding
|
|
68
|
+
* touches: `\` and `|`. Everything else is left as the author typed it —
|
|
69
|
+
* the sheet renders cell text literally, so nothing else needs a spelling.
|
|
70
|
+
* A row cannot hold a line break, so whitespace runs collapse to one space.
|
|
71
|
+
*/
|
|
72
|
+
export declare function encodeSheetCell(value: string): string;
|
|
73
|
+
export type SheetWriteResult = {
|
|
74
|
+
ok: true;
|
|
75
|
+
source: string;
|
|
76
|
+
/** The value as the reader will now show it, after normalization. */
|
|
77
|
+
value: string;
|
|
78
|
+
/** False when the write was a no-op — `source` is then the input string. */
|
|
79
|
+
changed: boolean;
|
|
80
|
+
/** True when a line break or padding was collapsed to fit a row. */
|
|
81
|
+
normalized: boolean;
|
|
82
|
+
} | {
|
|
83
|
+
ok: false;
|
|
84
|
+
reason: "no-grid" | "row-out-of-range" | "column-out-of-range";
|
|
85
|
+
/** Always the untouched input, so a caller can assign unconditionally. */
|
|
86
|
+
source: string;
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Write one cell and return the whole block source back.
|
|
90
|
+
*
|
|
91
|
+
* Total by construction: every refusal names itself, nothing returns
|
|
92
|
+
* `undefined`, and the unchanged source always comes back on the result so a
|
|
93
|
+
* caller cannot accidentally drop the document by not checking `ok`.
|
|
94
|
+
*/
|
|
95
|
+
export declare function writeSheetCell(source: string, rowIndex: number, columnIndex: number, value: string): SheetWriteResult;
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
// GENERATED by scripts/sync-format.mjs from packages/markdown/src — DO NOT EDIT.
|
|
2
|
+
// Edit packages/markdown/src and re-run the sync (any sfora-cli build does it).
|
|
3
|
+
/**
|
|
4
|
+
* Where every sheet cell lives in the authored bytes.
|
|
5
|
+
*
|
|
6
|
+
* `parseSheet` hands the reader a grid of DECODED strings — trimmed, escapes
|
|
7
|
+
* resolved, short rows padded. That is the right shape to draw and the wrong
|
|
8
|
+
* shape to write back through: re-serializing the whole grid from it would
|
|
9
|
+
* rewrite the author's column padding, their outer pipes, their delimiter row,
|
|
10
|
+
* every byte they lined up by hand.
|
|
11
|
+
*
|
|
12
|
+
* So an edit is a splice, not a re-serialization. This module answers "which
|
|
13
|
+
* byte range is that cell?" and writes a single cell in place. Everything the
|
|
14
|
+
* author wrote outside the edited cell survives untouched, which is what makes
|
|
15
|
+
* the no-op case byte-identical by construction rather than by luck:
|
|
16
|
+
* `writeSheetCell(source, r, c, currentText)` returns the same string back.
|
|
17
|
+
*
|
|
18
|
+
* The escape rules here are a second reading of the same grammar `sheetCells`
|
|
19
|
+
* in `blocks/parsers.ts` reads, and a second reading is only trustworthy if
|
|
20
|
+
* something checks it against the first. `sheetCellSpans.test.ts` does: every
|
|
21
|
+
* span's slice is fed back through `parseSheet` and must equal the cell the
|
|
22
|
+
* parser reports. A divergence is a test failure, never a silent misalignment.
|
|
23
|
+
*
|
|
24
|
+
* The CODE-SPAN half is no longer a second reading at all. While this module
|
|
25
|
+
* lived in `src/components/editor/` it could not reach the engine's private
|
|
26
|
+
* helpers, so it carried a hand-rolled `codeSpanMask` that closed a backtick
|
|
27
|
+
* run with `indexOf` — which lets a longer run close a shorter one, where
|
|
28
|
+
* CommonMark (and `lineGeometry.codeSpanMask`, which `sheetCells` uses) wants
|
|
29
|
+
* the closing run to be exactly as long as the opening one. Moving the file
|
|
30
|
+
* into the engine (board card #278) retires the copy: both readings of "where
|
|
31
|
+
* are the code spans" are now the one function, so the two cannot drift.
|
|
32
|
+
*/
|
|
33
|
+
import { isSheetRow, parseSheet } from "./blocks/parsers.js";
|
|
34
|
+
import { codeSpanMask } from "./lineGeometry.js";
|
|
35
|
+
const ASCII_PUNCTUATION = /[!-/:-@[-`{-~]/;
|
|
36
|
+
const SHEET_SEPARATOR_CELL = /^:?-+:?$/;
|
|
37
|
+
function isEscapedAt(text, index) {
|
|
38
|
+
let backslashes = 0;
|
|
39
|
+
for (let i = index - 1; i >= 0 && text[i] === "\\"; i--)
|
|
40
|
+
backslashes++;
|
|
41
|
+
return backslashes % 2 === 1;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Split one already-trimmed row into the byte ranges between its delimiters.
|
|
45
|
+
* Offsets are relative to `line`; the caller shifts them into the source.
|
|
46
|
+
*/
|
|
47
|
+
function rowCells(line) {
|
|
48
|
+
let from = 0;
|
|
49
|
+
let to = line.length;
|
|
50
|
+
if (line.startsWith("|"))
|
|
51
|
+
from = 1;
|
|
52
|
+
const trailingPipe = line.endsWith("|") && !isEscapedAt(line, line.length - 1);
|
|
53
|
+
if (trailingPipe)
|
|
54
|
+
to = line.length - 1;
|
|
55
|
+
const content = line.slice(from, to);
|
|
56
|
+
const inCode = codeSpanMask(content);
|
|
57
|
+
const cells = [];
|
|
58
|
+
let cellStart = 0;
|
|
59
|
+
for (let i = 0; i < content.length; i++) {
|
|
60
|
+
const character = content[i];
|
|
61
|
+
if (character === "\\") {
|
|
62
|
+
const next = content[i + 1];
|
|
63
|
+
// Same rule as the engine: `\|` is a literal pipe even inside a code
|
|
64
|
+
// span; every other escape belongs to inline parsing and stops there.
|
|
65
|
+
if (next === "|" || (next !== undefined && !inCode[i] && ASCII_PUNCTUATION.test(next))) {
|
|
66
|
+
i++;
|
|
67
|
+
}
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (character === "|" && !inCode[i]) {
|
|
71
|
+
cells.push({ start: cellStart, end: i });
|
|
72
|
+
cellStart = i + 1;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
cells.push({ start: cellStart, end: content.length });
|
|
77
|
+
return {
|
|
78
|
+
cells: cells.map((cell) => ({ start: cell.start + from, end: cell.end + from })),
|
|
79
|
+
trailingPipe,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function sourceLines(source) {
|
|
83
|
+
const out = [];
|
|
84
|
+
let start = 0;
|
|
85
|
+
for (let i = 0; i <= source.length; i++) {
|
|
86
|
+
if (i === source.length || source[i] === "\n") {
|
|
87
|
+
let end = i;
|
|
88
|
+
if (end > start && source[end - 1] === "\r")
|
|
89
|
+
end -= 1;
|
|
90
|
+
out.push({ start, end, text: source.slice(start, end) });
|
|
91
|
+
start = i + 1;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return out;
|
|
95
|
+
}
|
|
96
|
+
/** Trim a raw range in place, so the author's padding stays outside the span. */
|
|
97
|
+
function trimRange(source, start, end) {
|
|
98
|
+
let from = start;
|
|
99
|
+
let to = end;
|
|
100
|
+
while (from < to && /\s/.test(source[from]))
|
|
101
|
+
from++;
|
|
102
|
+
while (to > from && /\s/.test(source[to - 1]))
|
|
103
|
+
to--;
|
|
104
|
+
return { start: from, end: to };
|
|
105
|
+
}
|
|
106
|
+
function rowSpan(source, line, texts) {
|
|
107
|
+
const trimmed = line.text.trim();
|
|
108
|
+
const lead = line.text.length - line.text.trimStart().length;
|
|
109
|
+
const base = line.start + lead;
|
|
110
|
+
const { cells, trailingPipe } = rowCells(trimmed);
|
|
111
|
+
return {
|
|
112
|
+
cells: cells.map((cell, index) => {
|
|
113
|
+
const range = trimRange(source, base + cell.start, base + cell.end);
|
|
114
|
+
return { start: range.start, end: range.end, text: texts[index] ?? source.slice(range.start, range.end) };
|
|
115
|
+
}),
|
|
116
|
+
appendAt: base + trimmed.length,
|
|
117
|
+
trailingPipe,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Build the source-anchored grid, or null when the source is not a sheet the
|
|
122
|
+
* reader would draw — the same null the renderer already falls back on.
|
|
123
|
+
*
|
|
124
|
+
* The cell TEXTS come from `parseSheet`, never from this file's own decoding:
|
|
125
|
+
* one reader of the grammar owns what a cell says, and this module only owns
|
|
126
|
+
* where it sits. When the two disagree about how many cells the header has,
|
|
127
|
+
* the grid degrades to null instead of handing back a misaligned map.
|
|
128
|
+
*/
|
|
129
|
+
export function sheetCellGrid(source) {
|
|
130
|
+
const data = parseSheet(source);
|
|
131
|
+
if (!data)
|
|
132
|
+
return null;
|
|
133
|
+
const lines = sourceLines(source).filter((line) => {
|
|
134
|
+
const text = line.text.trim();
|
|
135
|
+
return text.length > 0 && isSheetRow(text);
|
|
136
|
+
});
|
|
137
|
+
if (lines.length < 2)
|
|
138
|
+
return null;
|
|
139
|
+
const headerSpan = rowSpan(source, lines[0], data.columns.map((column) => column.label));
|
|
140
|
+
if (headerSpan.cells.length !== data.columns.length)
|
|
141
|
+
return null;
|
|
142
|
+
const separatorCells = rowCells(lines[1].text.trim()).cells;
|
|
143
|
+
const hasSeparator = separatorCells.length === data.columns.length &&
|
|
144
|
+
separatorCells.every((cell) => {
|
|
145
|
+
const trimmed = lines[1].text.trim().slice(cell.start, cell.end).trim();
|
|
146
|
+
return SHEET_SEPARATOR_CELL.test(trimmed);
|
|
147
|
+
});
|
|
148
|
+
const bodyLines = lines.slice(hasSeparator ? 2 : 1);
|
|
149
|
+
if (bodyLines.length !== data.rows.length)
|
|
150
|
+
return null;
|
|
151
|
+
return {
|
|
152
|
+
columns: data.columns.length,
|
|
153
|
+
header: headerSpan,
|
|
154
|
+
rows: bodyLines.map((line, index) => rowSpan(source, line, data.rows[index])),
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Encode a value for a table cell.
|
|
159
|
+
*
|
|
160
|
+
* The inverse of the engine's decoding over the alphabet that decoding
|
|
161
|
+
* touches: `\` and `|`. Everything else is left as the author typed it —
|
|
162
|
+
* the sheet renders cell text literally, so nothing else needs a spelling.
|
|
163
|
+
* A row cannot hold a line break, so whitespace runs collapse to one space.
|
|
164
|
+
*/
|
|
165
|
+
export function encodeSheetCell(value) {
|
|
166
|
+
return value.replace(/\s+/g, " ").trim().replace(/[\\|]/g, (match) => `\\${match}`);
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Write one cell and return the whole block source back.
|
|
170
|
+
*
|
|
171
|
+
* Total by construction: every refusal names itself, nothing returns
|
|
172
|
+
* `undefined`, and the unchanged source always comes back on the result so a
|
|
173
|
+
* caller cannot accidentally drop the document by not checking `ok`.
|
|
174
|
+
*/
|
|
175
|
+
export function writeSheetCell(source, rowIndex, columnIndex, value) {
|
|
176
|
+
const grid = sheetCellGrid(source);
|
|
177
|
+
if (!grid)
|
|
178
|
+
return { ok: false, reason: "no-grid", source };
|
|
179
|
+
if (rowIndex < 0 || rowIndex >= grid.rows.length) {
|
|
180
|
+
return { ok: false, reason: "row-out-of-range", source };
|
|
181
|
+
}
|
|
182
|
+
if (columnIndex < 0 || columnIndex >= grid.columns) {
|
|
183
|
+
return { ok: false, reason: "column-out-of-range", source };
|
|
184
|
+
}
|
|
185
|
+
const encoded = encodeSheetCell(value);
|
|
186
|
+
const shown = decodeForDisplay(encoded);
|
|
187
|
+
const normalized = shown !== value;
|
|
188
|
+
const row = grid.rows[rowIndex];
|
|
189
|
+
const current = row.cells[columnIndex];
|
|
190
|
+
// The no-op door. Nothing is spliced, so the bytes cannot drift — this is
|
|
191
|
+
// why the round trip is identity and not merely "usually identity". An
|
|
192
|
+
// absent cell in a short row counts: the reader already draws it as empty,
|
|
193
|
+
// so writing empty into it is asking for what is already true.
|
|
194
|
+
const shown0 = current ? current.text : "";
|
|
195
|
+
if (shown0 === value) {
|
|
196
|
+
return { ok: true, source, value, changed: false, normalized: false };
|
|
197
|
+
}
|
|
198
|
+
if (current) {
|
|
199
|
+
const next = source.slice(0, current.start) + encoded + source.slice(current.end);
|
|
200
|
+
return { ok: true, source: next, value: shown, changed: next !== source, normalized };
|
|
201
|
+
}
|
|
202
|
+
// A short row: grow it to reach the column, rather than refusing an edit the
|
|
203
|
+
// reader already drew as an empty cell.
|
|
204
|
+
const additions = [];
|
|
205
|
+
for (let index = row.cells.length; index <= columnIndex; index++) {
|
|
206
|
+
additions.push(index === columnIndex ? encoded : "");
|
|
207
|
+
}
|
|
208
|
+
const inserted = additions
|
|
209
|
+
.map((cell) => row.trailingPipe
|
|
210
|
+
? cell
|
|
211
|
+
? ` ${cell} |`
|
|
212
|
+
: " |"
|
|
213
|
+
: cell
|
|
214
|
+
? ` | ${cell}`
|
|
215
|
+
: " |")
|
|
216
|
+
.join("");
|
|
217
|
+
const next = source.slice(0, row.appendAt) + inserted + source.slice(row.appendAt);
|
|
218
|
+
return { ok: true, source: next, value: shown, changed: true, normalized };
|
|
219
|
+
}
|
|
220
|
+
/** What the reader will show for bytes this module just wrote. */
|
|
221
|
+
function decodeForDisplay(encoded) {
|
|
222
|
+
return encoded.replace(/\\([\\|])/g, "$1");
|
|
223
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keyboard-first selection for the sheet block.
|
|
3
|
+
*
|
|
4
|
+
* The shape is BlockSuite's data-view model, kept because it earns its keep: a
|
|
5
|
+
* selection is a plain serializable VALUE — a focus coordinate plus a rung —
|
|
6
|
+
* not a DOM range, not a set of element references. That is what lets the same
|
|
7
|
+
* selection survive a re-render, a re-parse of the block source, a round trip
|
|
8
|
+
* through JSON, and (later) a collaborative cursor. Reference:
|
|
9
|
+
* context/AFFiNE/blocksuite/affine/data-view/src/view-presets/table/selection.ts
|
|
10
|
+
* and .../table/pc/controller/hotkeys.ts.
|
|
11
|
+
*
|
|
12
|
+
* Three of their behaviours are deliberately NOT copied.
|
|
13
|
+
*
|
|
14
|
+
* 1. The inescapable Escape loop. In their hotkeys, Escape on a cell promotes
|
|
15
|
+
* to row selection and Escape on a row selection converts the rows straight
|
|
16
|
+
* back into an area selection — the two rungs trade places forever and the
|
|
17
|
+
* table never lets go. Here Escape walks a strictly decreasing ladder
|
|
18
|
+
* (editing → cell → row → none) and then reports `ignored` so the key
|
|
19
|
+
* belongs to whatever encloses the block. `selectionRung` is the proof
|
|
20
|
+
* obligation: every Escape lowers it, so the walk terminates.
|
|
21
|
+
*
|
|
22
|
+
* 2. Tab dead-ending. Theirs wraps to the next row and stops moving at the last
|
|
23
|
+
* cell while still swallowing the key, so a keyboard user is trapped in the
|
|
24
|
+
* grid with no way forward. Here Tab past the last cell RELEASES — the
|
|
25
|
+
* caller lets the browser move focus on — and the selection value is left
|
|
26
|
+
* intact so tabbing back in lands where you left.
|
|
27
|
+
*
|
|
28
|
+
* 3. Silent undefined. Their cell lookups go through a DOM query that returns
|
|
29
|
+
* `undefined` and the handler simply does nothing, so a coordinate that has
|
|
30
|
+
* drifted out of range is indistinguishable from a key that did nothing.
|
|
31
|
+
* This reducer is total: coordinates are clamped against the shape on every
|
|
32
|
+
* transition, and every call returns a named outcome.
|
|
33
|
+
*/
|
|
34
|
+
/** How many body rows and columns the block is currently drawing. */
|
|
35
|
+
export type SheetShape = {
|
|
36
|
+
rows: number;
|
|
37
|
+
columns: number;
|
|
38
|
+
};
|
|
39
|
+
export type SheetSelection = {
|
|
40
|
+
kind: "none";
|
|
41
|
+
} | {
|
|
42
|
+
kind: "row";
|
|
43
|
+
rowIndex: number;
|
|
44
|
+
} | {
|
|
45
|
+
kind: "cell";
|
|
46
|
+
rowIndex: number;
|
|
47
|
+
columnIndex: number;
|
|
48
|
+
isEditing: boolean;
|
|
49
|
+
};
|
|
50
|
+
export declare const SHEET_SELECTION_NONE: SheetSelection;
|
|
51
|
+
/**
|
|
52
|
+
* The ladder rung. Escape lowers it by exactly one, which is the whole reason
|
|
53
|
+
* the Escape walk terminates rather than ping-ponging.
|
|
54
|
+
*/
|
|
55
|
+
export declare function selectionRung(selection: SheetSelection): 0 | 1 | 2 | 3;
|
|
56
|
+
export type SheetAction = {
|
|
57
|
+
type: "focusCell";
|
|
58
|
+
rowIndex: number;
|
|
59
|
+
columnIndex: number;
|
|
60
|
+
edit?: boolean;
|
|
61
|
+
} | {
|
|
62
|
+
type: "move";
|
|
63
|
+
direction: "up" | "down" | "left" | "right";
|
|
64
|
+
} | {
|
|
65
|
+
type: "step";
|
|
66
|
+
direction: "forward" | "back";
|
|
67
|
+
} | {
|
|
68
|
+
type: "enter";
|
|
69
|
+
} | {
|
|
70
|
+
type: "escape";
|
|
71
|
+
} | {
|
|
72
|
+
type: "typeChar";
|
|
73
|
+
char: string;
|
|
74
|
+
} | {
|
|
75
|
+
type: "clear";
|
|
76
|
+
} | {
|
|
77
|
+
type: "commit";
|
|
78
|
+
value: string;
|
|
79
|
+
then: "stay" | "down" | "forward" | "back";
|
|
80
|
+
} | {
|
|
81
|
+
type: "release";
|
|
82
|
+
};
|
|
83
|
+
export type SheetOutcome =
|
|
84
|
+
/** The block consumed the key. */
|
|
85
|
+
"handled"
|
|
86
|
+
/** The block is done: focus should move to the next thing after it. */
|
|
87
|
+
| "released-forward"
|
|
88
|
+
/** The block is done: focus should move to the thing before it. */
|
|
89
|
+
| "released-back"
|
|
90
|
+
/** Not ours — let the key bubble to whatever encloses the block. */
|
|
91
|
+
| "ignored";
|
|
92
|
+
/** A write the caller must apply to the block source. */
|
|
93
|
+
export type SheetWrite = {
|
|
94
|
+
rowIndex: number;
|
|
95
|
+
columnIndex: number;
|
|
96
|
+
value: string;
|
|
97
|
+
};
|
|
98
|
+
export type SheetTransition = {
|
|
99
|
+
selection: SheetSelection;
|
|
100
|
+
outcome: SheetOutcome;
|
|
101
|
+
/** Null unless the transition changed a cell's value. */
|
|
102
|
+
write: SheetWrite | null;
|
|
103
|
+
/** The text an opening editor starts with, or null when none opens. */
|
|
104
|
+
draft: string | null;
|
|
105
|
+
};
|
|
106
|
+
export type SheetContext = {
|
|
107
|
+
shape: SheetShape;
|
|
108
|
+
/** The current text of a cell. Total: out-of-range reads answer "". */
|
|
109
|
+
read: (rowIndex: number, columnIndex: number) => string;
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* Re-seat a selection against a new shape. The block source can change under
|
|
113
|
+
* the selection — an agent writes a row, a paste shortens one — and the value
|
|
114
|
+
* has to stay meaningful without being thrown away.
|
|
115
|
+
*/
|
|
116
|
+
export declare function reseatSelection(selection: SheetSelection, shape: SheetShape): SheetSelection;
|
|
117
|
+
export declare function sheetSelectionReducer(selection: SheetSelection, action: SheetAction, context: SheetContext): SheetTransition;
|
|
118
|
+
/**
|
|
119
|
+
* A keyboard event, read as an action. Split from the reducer so the key map
|
|
120
|
+
* is a table a test can walk without a DOM.
|
|
121
|
+
*
|
|
122
|
+
* Returns null for keys the block has no opinion about, which is the same
|
|
123
|
+
* statement as `ignored` — nothing here silently swallows a key.
|
|
124
|
+
*/
|
|
125
|
+
export declare function sheetKeyAction(event: {
|
|
126
|
+
key: string;
|
|
127
|
+
shiftKey?: boolean;
|
|
128
|
+
metaKey?: boolean;
|
|
129
|
+
ctrlKey?: boolean;
|
|
130
|
+
altKey?: boolean;
|
|
131
|
+
}): SheetAction | null;
|
|
132
|
+
/**
|
|
133
|
+
* Where Enter should land after committing an edit — down a row, matching
|
|
134
|
+
* every spreadsheet. Kept as a named function so the hook and its test agree.
|
|
135
|
+
*/
|
|
136
|
+
export declare const ENTER_COMMIT_LANDING: "down";
|