@folijs/agent 0.0.0-stage → 0.1.1
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/CHANGELOG.md +87 -0
- package/LICENSE +21 -0
- package/README.md +156 -2
- package/dist/handles.d.ts +2 -0
- package/dist/handles.d.ts.map +1 -0
- package/dist/handles.js +72 -0
- package/dist/handles.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/operations.d.ts +20 -0
- package/dist/operations.d.ts.map +1 -0
- package/dist/operations.js +383 -0
- package/dist/operations.js.map +1 -0
- package/dist/read.d.ts +50 -0
- package/dist/read.d.ts.map +1 -0
- package/dist/read.js +233 -0
- package/dist/read.js.map +1 -0
- package/dist/result.d.ts +11 -0
- package/dist/result.d.ts.map +1 -0
- package/dist/result.js +7 -0
- package/dist/result.js.map +1 -0
- package/dist/session.d.ts +116 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/session.js +68 -0
- package/dist/session.js.map +1 -0
- package/dist/templates.d.ts +43 -0
- package/dist/templates.d.ts.map +1 -0
- package/dist/templates.js +182 -0
- package/dist/templates.js.map +1 -0
- package/package.json +38 -3
- package/src/handles.ts +73 -0
- package/src/index.ts +11 -0
- package/src/operations.ts +496 -0
- package/src/read.ts +284 -0
- package/src/result.ts +26 -0
- package/src/session.ts +203 -0
- package/src/templates.ts +252 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# @folijs/agent
|
|
2
|
+
|
|
3
|
+
## 0.1.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- d58907d: A read keeps every block on one line whatever the document holds: a carriage return, a line or paragraph separator (U+2028, U+2029) and a next-line character (U+0085) show as `↵`, as a line break does, in a block's text and in everything a document names that is shown (a style, a list number, a control's tag, title, items and value, a field's instruction and a merge field's name in what `updateFields` says; `styles()` and `controls()` give the names that way, and take them back that way). A document could otherwise start a line that reads as another block. A refusal that quotes a block's text quotes at most 2,000 characters of it.
|
|
8
|
+
- 617ed0b: A second round of the bug sweep: the suspects the first could not prove, each now shown by a test and fixed (or dismissed).
|
|
9
|
+
|
|
10
|
+
**core**
|
|
11
|
+
- Deleting from a paragraph into a table, or the rows of a merge's first cell while tracking, no longer leaves covered cells with no merge above; a tracked row delete leaves the caret in a row not marked deleted.
|
|
12
|
+
- Pasting over cells of one row no longer puts the row's content control around the pasted paragraphs.
|
|
13
|
+
- Renaming a bookmark follows `REF "Name"` and `HYPERLINK \l Name` field codes.
|
|
14
|
+
- `\* Roman` and `\* Alphabetic` (an initial capital) are the uppercase formats; `\* Hex` rounds a fraction.
|
|
15
|
+
- A date control keeps a year under 100 (it was read as 19xx).
|
|
16
|
+
- `history({ depth: 0 })` keeps no undo events; `Fragment.cut` past the end throws a `RangeError`.
|
|
17
|
+
- A style that sets a hanging indent (0 too) clears the first-line indent it inherits.
|
|
18
|
+
|
|
19
|
+
**view**
|
|
20
|
+
- A table whose rows are all header rows is no longer repeated whole on each page it runs to.
|
|
21
|
+
- Ctrl+Shift+V that brings no paste event no longer makes the next paste plain.
|
|
22
|
+
- A pasted `<p> </p>` is an empty paragraph.
|
|
23
|
+
- `destroy()` leaves no cursor style on the host.
|
|
24
|
+
- A `PAGE` field reads every `\*` switch (`\* MERGEFORMAT \* roman`), and `Roman` / `Alphabetic` as uppercase.
|
|
25
|
+
- Header and footer text holding U+E000 is kept beside a page number.
|
|
26
|
+
|
|
27
|
+
**react**
|
|
28
|
+
- The Bookmarks dialog follows the document while open, and says so when a delete is refused.
|
|
29
|
+
- A comments pane mounted before its canvas positions its threads once the pages are there.
|
|
30
|
+
|
|
31
|
+
**docx**
|
|
32
|
+
- Styles, numbering, comments, notes, headers and footers that bind WordprocessingML to another prefix (or none) are written well-formed, as the main document already was.
|
|
33
|
+
- A namespace prefix declared on a hyperlink, a tracked change, a comment or a note no longer makes export throw.
|
|
34
|
+
- A tracked change's `w:date` in a form the model does not hold is kept on export.
|
|
35
|
+
- Move sources (`w:moveFrom`) are written with `w:t`, as Word writes them, not `w:delText`.
|
|
36
|
+
|
|
37
|
+
**agent**
|
|
38
|
+
- A block-level control showing its placeholder over several paragraphs reads `[empty: …]` once.
|
|
39
|
+
- A new paragraph takes a style's `next` style only when the document has that paragraph style.
|
|
40
|
+
- a4945da: Fixes from a bug sweep of every package, each with a regression test.
|
|
41
|
+
|
|
42
|
+
**core**
|
|
43
|
+
- Marks toggled at a caret no longer follow the selection: moving it (or `editor.select()`) drops them.
|
|
44
|
+
- A plugin's change appended to a selection-only transaction is an undo step of its own.
|
|
45
|
+
- `canJoin` and `liftTarget` no longer say yes where `join` and `lift` throw; a structure step out of range fails instead of throwing a `RangeError`.
|
|
46
|
+
- Tables: inserting or deleting a row never leaves the caret in a cell a merge covers; Tab from such a cell goes to the next cell; a row inserted above a header row is a header row, so the header keeps repeating.
|
|
47
|
+
- Fields: `SEQ id bookmark` reads a bookmark after it or around the numbered field; a `MERGEFIELD` date given as `yyyy-mm-dd` is not a day early west of UTC; date pictures take `D` and `Y` in either case.
|
|
48
|
+
- Content controls: a multi-line text control takes CRLF and CR line ends; a date typed in a format with `am/pm` is read; a drop-down given an item's value picks that item before another item's display text.
|
|
49
|
+
- Accepting a paragraph deleted whole before a table removes it, as rejecting an inserted one does.
|
|
50
|
+
- Renaming a bookmark follows links whose anchor differs only in case.
|
|
51
|
+
- The `chicago` number format no longer throws on a large counter.
|
|
52
|
+
- `parseDocument` refuses a custom node its extension is not loaded for when it holds `content` or `text`, instead of dropping them.
|
|
53
|
+
|
|
54
|
+
**view**
|
|
55
|
+
- Pasted control characters and lone surrogates are dropped (they made the document unexportable to `.docx`); a vertical tab in plain text ends the paragraph.
|
|
56
|
+
- A rule (`<hr>`) at an end of pasted HTML, or alone, is pasted where it was.
|
|
57
|
+
- A long run of spaces in preformatted pasted text no longer hangs the page.
|
|
58
|
+
- Pasted `details`, `summary`, `center`, `fieldset`, `legend`, `form` and `caption` are blocks of their own.
|
|
59
|
+
- `destroy()` removes `data-foli-cells` from the host.
|
|
60
|
+
- A page break before the first paragraph after a continuous section break starts a page.
|
|
61
|
+
- A deleted break breaks nothing in the `final` and `simple` review views.
|
|
62
|
+
- `renderDocument` (and so PDF export) hides deleted text and draws list numbers in table cells.
|
|
63
|
+
|
|
64
|
+
**react**
|
|
65
|
+
- Replace and an open comment form are no longer usable after the editor turns read-only.
|
|
66
|
+
- The control properties dialog leaves unedited list items alone (a value with a `|` was cut).
|
|
67
|
+
- The link dialog closes when another document is set; opening one toolbar dialog closes any other.
|
|
68
|
+
|
|
69
|
+
**docx**
|
|
70
|
+
- Export writes well-formed XML for documents that bind WordprocessingML or relationships to another prefix (or none), in the body, the relationships part and the content types.
|
|
71
|
+
- No-break spaces at the ends of a `w:t` are kept on import; a line end inside a `w:t` is read as a space.
|
|
72
|
+
- A comment's start tag (its date with an offset, attributes Foli does not model) is kept on an unedited export.
|
|
73
|
+
- A namespace prefix declared on a paragraph or row no longer makes export throw.
|
|
74
|
+
|
|
75
|
+
**agent**
|
|
76
|
+
- `deleteBlock` no longer removes other authors' or untracked text untracked from a paragraph the session's author inserted.
|
|
77
|
+
- A drop-down control's `value` is its display text, as `items` lists and `fillControl` takes.
|
|
78
|
+
- Comment initials of an author whose name starts outside the BMP are well-formed.
|
|
79
|
+
- Updated dependencies [617ed0b]
|
|
80
|
+
- Updated dependencies [a4945da]
|
|
81
|
+
- @folijs/core@1.1.0
|
|
82
|
+
|
|
83
|
+
## 0.1.0
|
|
84
|
+
|
|
85
|
+
### Minor Changes
|
|
86
|
+
|
|
87
|
+
- First release. An LLM agent reads and edits a document without computing positions: `openSession(document, { author })` gives the body as text with a handle per block (`read`), and operations that take handles (`replaceText`, `insertAfter`, `insertBefore`, `deleteBlock`, `setStyle`, `comment`, `controls`, `fillControl`, `updateFields`). Every edit is a tracked change in the author's name, for a person to accept or reject in Foli or in Word. Results are values: the blocks as they read now, or a refusal with a code and a message written for the model. No DOM and no Node built-ins. Unstable (0.x): outside the 1.0 promise of the other packages.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Josep Audenis
|
|
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,3 +1,157 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @folijs/agent
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Agent editing for [Foli](https://github.com/josep-audenis/foli): a session over a document whose
|
|
4
|
+
edits are tracked changes in the agent's name, for a person to accept or reject in Foli or in
|
|
5
|
+
Word. No DOM and no Node built-ins: runs in Node, browsers, workers and sandboxes. Its only
|
|
6
|
+
dependency is `@folijs/core`, a peer at an exact version.
|
|
7
|
+
|
|
8
|
+
> **Unstable (0.x).** Outside the 1.0 promise of the other packages: its API may change in any
|
|
9
|
+
> 0.x release. Changes in `CHANGELOG.md`. `npm install @folijs/agent @folijs/core`
|
|
10
|
+
> (and `@folijs/docx` to read and write `.docx`, as below).
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { openSession } from "@folijs/agent";
|
|
14
|
+
import { exportDocx, importDocx } from "@folijs/docx";
|
|
15
|
+
|
|
16
|
+
const { document, package: pkg } = await importDocx(bytes);
|
|
17
|
+
const session = openSession(document, { author: "Agent" });
|
|
18
|
+
const read = session.read(); // { ok: true, text, blocks, next }
|
|
19
|
+
const out = await exportDocx(session.document(), pkg);
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Reading
|
|
23
|
+
|
|
24
|
+
`session.read()` gives the document's body as text, one block per line, as it would be with
|
|
25
|
+
every tracked change accepted:
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
[b1] Title: MUTUAL NON-DISCLOSURE AGREEMENT
|
|
29
|
+
[b6] heading 1, 1.: Definitions and interpretation
|
|
30
|
+
[b7] Clause, 1.1, changed, 2 comments: In this Agreement the following definitions apply:
|
|
31
|
+
[b11] table, 3 rows, 2 columns
|
|
32
|
+
[b12] r1 c1, Normal: Item
|
|
33
|
+
[b20] not editable: w:sdt
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
- `[b7]` is the block's **handle**: what every operation takes. It names the same block for the
|
|
37
|
+
whole session, whatever is edited around it. A new block gets a new handle; a block that is
|
|
38
|
+
removed, or joined into the one after it, loses its own. (A paragraph is its paragraph mark,
|
|
39
|
+
as in Word: Enter in the middle of one leaves the handle on the text after the caret.) Undo
|
|
40
|
+
does not bring a lost handle back: read again.
|
|
41
|
+
- Then: where the block is in a table (`r1 c1`), its paragraph style, its list number, `changed`
|
|
42
|
+
when it holds tracked changes, how many comments are anchored in it.
|
|
43
|
+
- After the colon, the text. A line break inside a paragraph shows as `↵`. What is not text
|
|
44
|
+
stands in brackets: `[picture]`, `[note]`, `[page break]`, `[empty: Offer date]` (a content
|
|
45
|
+
control not filled in yet). Deleted text, hidden text and field codes are left out.
|
|
46
|
+
- `read({ from: "b40", limit: 50 })` reads a long document in parts; `next` is the handle to go
|
|
47
|
+
on from. `session.block("b7")` gives one block. `blocks` has the same as data.
|
|
48
|
+
|
|
49
|
+
`read`, `block` and every operation return a value: `{ ok: true, … }`, or
|
|
50
|
+
`{ ok: false, code, message }` with a message written for the model (`There is no block b99.
|
|
51
|
+
Handles come from read: [b1], [b2]…`). `styles()`, `controls()` and `document()` return theirs
|
|
52
|
+
bare. Only `openSession` throws: on a document that does not validate, or an empty author.
|
|
53
|
+
|
|
54
|
+
Not read: headers, footers, footnotes and endnotes.
|
|
55
|
+
|
|
56
|
+
## Editing
|
|
57
|
+
|
|
58
|
+
Every edit is a tracked change by the session's author. Each operation returns the blocks it
|
|
59
|
+
changed as they read now (`blocks`, and `text` in the read format), so there is no need to read
|
|
60
|
+
again.
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
session.replaceText("b25", "three (3) years", "five (5) years");
|
|
64
|
+
session.replaceText("b5", "Party", "Side", { all: true });
|
|
65
|
+
session.insertAfter("b11", ["A new clause.", { text: "Schedule", style: "Heading 1" }]);
|
|
66
|
+
session.insertBefore("b6", ["Recitals"]);
|
|
67
|
+
session.deleteBlock("b17");
|
|
68
|
+
session.setStyle("b21", "Heading 2");
|
|
69
|
+
session.comment("b25", "Why three years?", { on: "three (3) years" });
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
- **`replaceText(handle, find, replacement, { all })`**: `find` is text as `read` shows it,
|
|
73
|
+
capitals and spaces included. It must be in the block exactly once, or `all` replaces every
|
|
74
|
+
place. The replacement takes the formatting of what it replaces; `""` deletes. `↵` or `\n`
|
|
75
|
+
is a line break. What `read` shows in brackets is not text and cannot be in `find`.
|
|
76
|
+
- **`insertAfter` / `insertBefore(handle, paragraphs)`**: one paragraph or a list, next to a
|
|
77
|
+
paragraph, a table or anything else read. A new paragraph is formatted as Enter there gives
|
|
78
|
+
in Word: as the paragraph next to it (style, numbering, indentation), or after a heading as
|
|
79
|
+
body text. One that names a `style` (by the name `read` shows) has that style only. The block
|
|
80
|
+
keeps its handle; the new ones come back with theirs.
|
|
81
|
+
- **`deleteBlock(handle)`**: a paragraph, or a table with all its cells. It returns a sentence,
|
|
82
|
+
not blocks. A paragraph that must stay is emptied instead, and the result says why: the last
|
|
83
|
+
one of the document or of a cell, one before a table or other block that is not a paragraph,
|
|
84
|
+
one that ends a section. Already empty, it is refused. A paragraph the session inserted
|
|
85
|
+
itself goes for real.
|
|
86
|
+
|
|
87
|
+
- **`styles()`** lists the paragraph styles by the names `read` shows. **`setStyle(handle,
|
|
88
|
+
style)`** puts a paragraph in one (by name or id, capitals do not count): a tracked
|
|
89
|
+
formatting change, the paragraph's direct formatting kept.
|
|
90
|
+
- **`comment(handle, text, { on })`** comments a paragraph in the author's name: the whole
|
|
91
|
+
block, or `on`, text that is in it once. A line break in `text` starts a new paragraph of the
|
|
92
|
+
comment. Comments are not tracked changes: rejecting the session's changes leaves them.
|
|
93
|
+
A locked content control takes comments, as in Word, and nothing else.
|
|
94
|
+
|
|
95
|
+
Refusals a model can act on: `no-match` (with the block's text), `several-matches` (with how
|
|
96
|
+
many), `unknown-style` (with the styles there are), `gone`, `unknown-handle`, `not-a-paragraph`,
|
|
97
|
+
`read-only` (a locked content control, content Foli does not edit), `invalid` (an argument of
|
|
98
|
+
the wrong kind), `refused` (the document would not take the change). No method of a session
|
|
99
|
+
throws.
|
|
100
|
+
|
|
101
|
+
## Templates
|
|
102
|
+
|
|
103
|
+
A template's blanks are Word's content controls and merge fields. A read shows an unfilled
|
|
104
|
+
control as `[empty: Offer date]` and a merge field as `«CandidateFirstName»`.
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
session.controls();
|
|
108
|
+
// [{ id: "/word/document.xml#0", name: "offer_date", tag: "offer_date", title: "Offer date",
|
|
109
|
+
// kind: "date", value: null, empty: true, readOnly: false, block: "b2" },
|
|
110
|
+
// { name: "position", kind: "dropDown", items: ["Data Engineer", "Senior Data Engineer"], … }]
|
|
111
|
+
session.fillControl("offer_date", "2026-12-01");
|
|
112
|
+
session.fillControl("position", "Senior Data Engineer");
|
|
113
|
+
session.fillControl("remote", true);
|
|
114
|
+
session.updateFields({ data: { CandidateFirstName: "Ada" } });
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
- **`controls()`**: the body's content controls with their values, and the handle of the block
|
|
118
|
+
each is in. Not those in a block deleted as a tracked change.
|
|
119
|
+
- **`fillControl(name, value)`**: the control with that `id`, else every control with that tag,
|
|
120
|
+
else with that title. Text for a text control, one of its items for a drop-down list, a day
|
|
121
|
+
as `yyyy-mm-dd` for a date (any other text is kept as typed, as Word does), true or false for
|
|
122
|
+
a check box; `null` empties it (not a check box). Refused with what would work: the controls there are, a
|
|
123
|
+
list's items, `read-only` for a locked control. When controls share the name and only some
|
|
124
|
+
take the value, the result is ok and `unfilled` says which did not and why.
|
|
125
|
+
- **`updateFields({ data })`**: updates the document's fields (dates, cross-references, a table
|
|
126
|
+
of contents) and fills its merge fields from `data`, by name, capitals not counting. `text`
|
|
127
|
+
says what was left as it was: merge fields without data, and page numbers, which need a page
|
|
128
|
+
layout the session does not have. It returns no blocks: read again.
|
|
129
|
+
|
|
130
|
+
## For the model
|
|
131
|
+
|
|
132
|
+
The session is a library: an app gives its methods to a model as tools. `@folijs/mcp` does that
|
|
133
|
+
for MCP clients, over `.docx` files in a folder. What a model needs to be told fits in a system prompt:
|
|
134
|
+
|
|
135
|
+
```text
|
|
136
|
+
You edit a document through tools. read gives it a line per block:
|
|
137
|
+
[b7] Clause, 1.1, changed: text
|
|
138
|
+
[b7] is the block's handle: pass it to the other tools. After it come where the block is in a
|
|
139
|
+
table (r1 c1), its style, its list number, "changed" if it has tracked changes, its comments;
|
|
140
|
+
after the colon, its text. ↵ is a line break. A line "table, 3 rows, 2 columns" is a table: its
|
|
141
|
+
cells' blocks follow, indented. A line "not editable: …" is content you cannot change.
|
|
142
|
+
Words in brackets ([picture], [note], [empty: Offer date]) are not text: never put them in
|
|
143
|
+
find. [empty: …] is a content control to fill with fillControl; «Name» is a merge field to fill
|
|
144
|
+
with updateFields.
|
|
145
|
+
A handle stays its block's while you edit; a deleted block's is refused. A tool that changes
|
|
146
|
+
the document returns the blocks it changed as they are now: do not read again to check (after
|
|
147
|
+
updateFields do: it returns none). A refusal says what to do instead: follow it.
|
|
148
|
+
Give find the exact text from the line, with its capitals, long enough to be there once.
|
|
149
|
+
Every edit you make is a tracked change a person will accept or reject: change only what the
|
|
150
|
+
task asks, and use comment to explain a change that is not obvious.
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Limits
|
|
154
|
+
|
|
155
|
+
Unstable: the API may change in any 0.x release. What is not there yet, and what rejecting the
|
|
156
|
+
agent's changes does not bring back exactly:
|
|
157
|
+
[limitations](https://github.com/josep-audenis/foli/blob/main/docs/limitations.md#agent-editing-folijsagent).
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handles.d.ts","sourceRoot":"","sources":["../src/handles.ts"],"names":[],"mappings":""}
|
package/dist/handles.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @internal
|
|
3
|
+
* Names for blocks that outlive edits: `b1`, `b2`… in the order they were first read.
|
|
4
|
+
*
|
|
5
|
+
* A handle is the position after its block, moved through every transaction's mapping: the
|
|
6
|
+
* block's closing, which for a paragraph is Word's paragraph mark, what a paragraph is in Word.
|
|
7
|
+
* Enter in a paragraph gives the text before the caret a new mark, so the handle stays with what
|
|
8
|
+
* comes after it; a paragraph joined with the next one loses its mark, and its handle; accepting
|
|
9
|
+
* a deleted paragraph or rejecting an inserted one leaves the paragraphs around under their
|
|
10
|
+
* handles. `w14:paraId` cannot do this: paragraphs Foli creates have none (roadmap, Phase 13).
|
|
11
|
+
*
|
|
12
|
+
* A block that is removed and comes back by undo comes back without its handle.
|
|
13
|
+
*/
|
|
14
|
+
export class Handles {
|
|
15
|
+
#editor;
|
|
16
|
+
/** Each handle's position: after its block. */
|
|
17
|
+
#ends = new Map();
|
|
18
|
+
#byEnd = null;
|
|
19
|
+
#issued = 0;
|
|
20
|
+
constructor(editor) {
|
|
21
|
+
this.#editor = editor;
|
|
22
|
+
let last = editor.state.doc;
|
|
23
|
+
editor.subscribe((state, transactions) => {
|
|
24
|
+
// `setDocument` tells without a transaction: no position of the old document holds.
|
|
25
|
+
if (!transactions.length && state.doc !== last)
|
|
26
|
+
this.#ends.clear();
|
|
27
|
+
last = state.doc;
|
|
28
|
+
for (const tr of transactions) {
|
|
29
|
+
if (!tr.steps.length)
|
|
30
|
+
continue;
|
|
31
|
+
for (const [handle, end] of this.#ends) {
|
|
32
|
+
// The two sides of the block's closing. A block removed, or joined with the next,
|
|
33
|
+
// loses one of them or sees them part, and the handle goes.
|
|
34
|
+
const before = tr.mapping.mapResult(end - 1, 1);
|
|
35
|
+
const after = tr.mapping.mapResult(end, -1);
|
|
36
|
+
if (before.deleted || after.deleted || after.pos !== before.pos + 1)
|
|
37
|
+
this.#ends.delete(handle);
|
|
38
|
+
else
|
|
39
|
+
this.#ends.set(handle, after.pos);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
this.#byEnd = null;
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
/** The handle of the block `node` at `pos` (before it), a new one if it has none yet. */
|
|
46
|
+
at(pos, node) {
|
|
47
|
+
const end = pos + node.nodeSize;
|
|
48
|
+
this.#byEnd ??= new Map([...this.#ends].map(([handle, at]) => [at, handle]));
|
|
49
|
+
let handle = this.#byEnd.get(end);
|
|
50
|
+
if (!handle) {
|
|
51
|
+
handle = `b${++this.#issued}`;
|
|
52
|
+
this.#ends.set(handle, end);
|
|
53
|
+
this.#byEnd.set(end, handle);
|
|
54
|
+
}
|
|
55
|
+
return handle;
|
|
56
|
+
}
|
|
57
|
+
/** Whether `handle` was ever issued. */
|
|
58
|
+
issued(handle) {
|
|
59
|
+
const n = /^b([1-9]\d*)$/.exec(handle);
|
|
60
|
+
return !!n && Number(n[1]) <= this.#issued;
|
|
61
|
+
}
|
|
62
|
+
/** Where the block `handle` names is now (before it), with the block; null when it is gone. */
|
|
63
|
+
resolve(handle) {
|
|
64
|
+
const end = this.#ends.get(handle);
|
|
65
|
+
const doc = this.#editor.state.doc;
|
|
66
|
+
if (end === undefined || end > doc.content.size)
|
|
67
|
+
return null;
|
|
68
|
+
const node = doc.resolve(end).nodeBefore;
|
|
69
|
+
return node?.isBlock ? { pos: end - node.nodeSize, node } : null;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=handles.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handles.js","sourceRoot":"","sources":["../src/handles.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,OAAO;IACT,OAAO,CAAS;IACzB,+CAA+C;IACtC,KAAK,GAAG,IAAI,GAAG,EAAkB,CAAC;IAC3C,MAAM,GAA+B,IAAI,CAAC;IAC1C,OAAO,GAAG,CAAC,CAAC;IAEZ,YAAY,MAAc;QACxB,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;QACtB,IAAI,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC;QAC5B,MAAM,CAAC,SAAS,CAAC,CAAC,KAAK,EAAE,YAAY,EAAE,EAAE;YACvC,oFAAoF;YACpF,IAAI,CAAC,YAAY,CAAC,MAAM,IAAI,KAAK,CAAC,GAAG,KAAK,IAAI;gBAAE,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;YACnE,IAAI,GAAG,KAAK,CAAC,GAAG,CAAC;YACjB,KAAK,MAAM,EAAE,IAAI,YAAY,EAAE,CAAC;gBAC9B,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,MAAM;oBAAE,SAAS;gBAC/B,KAAK,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;oBACvC,kFAAkF;oBAClF,4DAA4D;oBAC5D,MAAM,MAAM,GAAG,EAAE,CAAC,OAAO,CAAC,SAAS,CAAC,GAAG,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC;oBAChD,MAAM,KAAK,GAAG,EAAE,CAAC,OAAO,CAAC,SAAS,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC;oBAC5C,IAAI,MAAM,CAAC,OAAO,IAAI,KAAK,CAAC,OAAO,IAAI,KAAK,CAAC,GAAG,KAAK,MAAM,CAAC,GAAG,GAAG,CAAC;wBACjE,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;;wBACvB,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC;gBACzC,CAAC;YACH,CAAC;YACD,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;QACrB,CAAC,CAAC,CAAC;IACL,CAAC;IAED,yFAAyF;IACzF,EAAE,CAAC,GAAW,EAAE,IAAU;QACxB,MAAM,GAAG,GAAG,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC;QAChC,IAAI,CAAC,MAAM,KAAK,IAAI,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC;QAC7E,IAAI,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAClC,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,MAAM,GAAG,IAAI,EAAE,IAAI,CAAC,OAAO,EAAE,CAAC;YAC9B,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;YAC5B,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;QAC/B,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,wCAAwC;IACxC,MAAM,CAAC,MAAc;QACnB,MAAM,CAAC,GAAG,eAAe,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QACvC,OAAO,CAAC,CAAC,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,OAAO,CAAC;IAC7C,CAAC;IAED,+FAA+F;IAC/F,OAAO,CAAC,MAAc;QACpB,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACnC,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC;QACnC,IAAI,GAAG,KAAK,SAAS,IAAI,GAAG,GAAG,GAAG,CAAC,OAAO,CAAC,IAAI;YAAE,OAAO,IAAI,CAAC;QAC7D,MAAM,IAAI,GAAG,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,UAAU,CAAC;QACzC,OAAO,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,GAAG,GAAG,IAAI,CAAC,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;IACnE,CAAC;CACF"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type { Changed, CommentOptions, NewParagraph, ReplaceOptions } from "./operations.js";
|
|
2
|
+
export { type Block, formatBlocks } from "./read.js";
|
|
3
|
+
export type { Ok, Refusal, Result } from "./result.js";
|
|
4
|
+
export { openSession, type ReadOptions, type ReadResult, type Session, type SessionOptions, } from "./session.js";
|
|
5
|
+
export type { Control, Unfilled, UpdateFieldsOptions } from "./templates.js";
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAC7F,OAAO,EAAE,KAAK,KAAK,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AACrD,YAAY,EAAE,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACvD,OAAO,EACL,WAAW,EACX,KAAK,WAAW,EAChB,KAAK,UAAU,EACf,KAAK,OAAO,EACZ,KAAK,cAAc,GACpB,MAAM,cAAc,CAAC;AACtB,YAAY,EAAE,OAAO,EAAE,QAAQ,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAc,YAAY,EAAE,MAAM,WAAW,CAAC;AAErD,OAAO,EACL,WAAW,GAKZ,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type Block } from "./read.js";
|
|
2
|
+
/** A paragraph to insert: its text ("\n" or ↵ a line break, a tab a tab), in `style` if given. */
|
|
3
|
+
export type NewParagraph = string | {
|
|
4
|
+
text: string;
|
|
5
|
+
style?: string;
|
|
6
|
+
};
|
|
7
|
+
/** What an operation changed: the blocks as they are now, and as `read` would show them. */
|
|
8
|
+
export interface Changed {
|
|
9
|
+
blocks: Block[];
|
|
10
|
+
text: string;
|
|
11
|
+
}
|
|
12
|
+
export interface ReplaceOptions {
|
|
13
|
+
/** Replace every place `find` is in the block. Default: refuse when there are several. */
|
|
14
|
+
all?: boolean;
|
|
15
|
+
}
|
|
16
|
+
export interface CommentOptions {
|
|
17
|
+
/** Text in the block to anchor the comment to, as `read` shows it. Default: the whole block. */
|
|
18
|
+
on?: string;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=operations.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"operations.d.ts","sourceRoot":"","sources":["../src/operations.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAE,KAAK,KAAK,EAAqC,MAAM,WAAW,CAAC;AAG1E,kGAAkG;AAClG,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAErE,4FAA4F;AAC5F,MAAM,WAAW,OAAO;IACtB,MAAM,EAAE,KAAK,EAAE,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,cAAc;IAC7B,0FAA0F;IAC1F,GAAG,CAAC,EAAE,OAAO,CAAC;CACf;AAwZD,MAAM,WAAW,cAAc;IAC7B,gGAAgG;IAChG,EAAE,CAAC,EAAE,MAAM,CAAC;CACb"}
|