@rtif-sdk/core 1.0.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/LICENSE +21 -0
- package/README.md +70 -0
- package/dist/apply.d.ts +31 -0
- package/dist/apply.d.ts.map +1 -0
- package/dist/apply.js +604 -0
- package/dist/apply.js.map +1 -0
- package/dist/error.d.ts +19 -0
- package/dist/error.d.ts.map +1 -0
- package/dist/error.js +21 -0
- package/dist/error.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/model.d.ts +58 -0
- package/dist/model.d.ts.map +1 -0
- package/dist/model.js +6 -0
- package/dist/model.js.map +1 -0
- package/dist/normalize.d.ts +40 -0
- package/dist/normalize.d.ts.map +1 -0
- package/dist/normalize.js +74 -0
- package/dist/normalize.js.map +1 -0
- package/dist/operations.d.ts +90 -0
- package/dist/operations.d.ts.map +1 -0
- package/dist/operations.js +6 -0
- package/dist/operations.js.map +1 -0
- package/dist/queries.d.ts +68 -0
- package/dist/queries.d.ts.map +1 -0
- package/dist/queries.js +159 -0
- package/dist/queries.js.map +1 -0
- package/dist/resolve.d.ts +58 -0
- package/dist/resolve.d.ts.map +1 -0
- package/dist/resolve.js +90 -0
- package/dist/resolve.js.map +1 -0
- package/dist/serialization.d.ts +89 -0
- package/dist/serialization.d.ts.map +1 -0
- package/dist/serialization.js +39 -0
- package/dist/serialization.js.map +1 -0
- package/dist/validate.d.ts +38 -0
- package/dist/validate.d.ts.map +1 -0
- package/dist/validate.js +93 -0
- package/dist/validate.js.map +1 -0
- package/package.json +25 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Cory Robinson
|
|
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
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# @rtif-sdk/core
|
|
2
|
+
|
|
3
|
+
Zero-dependency core library for the RTIF (Rich Text Input Format) document model. Provides types, pure functions for applying operations, normalization, offset resolution, and validation.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @rtif-sdk/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Create a document
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import type { Document } from '@rtif-sdk/core';
|
|
17
|
+
|
|
18
|
+
const doc: Document = {
|
|
19
|
+
version: 1,
|
|
20
|
+
blocks: [
|
|
21
|
+
{ id: 'b1', type: 'text', spans: [{ text: 'Hello world' }] },
|
|
22
|
+
],
|
|
23
|
+
};
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### Apply an operation
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { apply } from '@rtif-sdk/core';
|
|
30
|
+
|
|
31
|
+
const { doc: newDoc, inverse } = apply(doc, {
|
|
32
|
+
type: 'insert_text',
|
|
33
|
+
offset: 5,
|
|
34
|
+
text: ',',
|
|
35
|
+
});
|
|
36
|
+
// newDoc.blocks[0].spans[0].text === 'Hello, world'
|
|
37
|
+
|
|
38
|
+
// Undo by applying the inverse
|
|
39
|
+
const { doc: original } = apply(newDoc, inverse);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Resolve offsets
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
import { resolve, docLength } from '@rtif-sdk/core';
|
|
46
|
+
|
|
47
|
+
const len = docLength(doc); // total character count including virtual \n separators
|
|
48
|
+
const pos = resolve(doc, 5); // { blockIndex: 0, localOffset: 5 }
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Validate a document
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { validate } from '@rtif-sdk/core';
|
|
55
|
+
|
|
56
|
+
const result = validate(doc);
|
|
57
|
+
if (!result.valid) {
|
|
58
|
+
console.error(result.errors);
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Operations
|
|
63
|
+
|
|
64
|
+
RTIF defines exactly 8 operation types: `insert_text`, `delete_text`, `split_block`, `merge_block`, `set_span_marks`, `set_block_attrs`, `set_block_type`, `set_meta`.
|
|
65
|
+
|
|
66
|
+
Every operation is deterministic and invertible — `apply()` always returns the inverse operation for undo.
|
|
67
|
+
|
|
68
|
+
## License
|
|
69
|
+
|
|
70
|
+
MIT
|
package/dist/apply.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Operation application — the single pure function that transforms documents.
|
|
3
|
+
* See docs/spec/operations.md for specification.
|
|
4
|
+
*/
|
|
5
|
+
import type { Document } from './model.js';
|
|
6
|
+
import type { Operation } from './operations.js';
|
|
7
|
+
export interface ApplyResult {
|
|
8
|
+
/** The new document after applying the operation */
|
|
9
|
+
readonly doc: Document;
|
|
10
|
+
/** The inverse operation (for undo) */
|
|
11
|
+
readonly inverse: Operation;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Apply an operation to a document, returning the new document and its inverse.
|
|
15
|
+
*
|
|
16
|
+
* - Immutable: returns a new Document, never mutates in place.
|
|
17
|
+
* - Deterministic: same (doc, op) → same (doc', inverse) on every platform.
|
|
18
|
+
* - Normalizes affected blocks before returning.
|
|
19
|
+
*
|
|
20
|
+
* @param doc - The document to transform
|
|
21
|
+
* @param op - The operation to apply
|
|
22
|
+
* @returns The new document and the inverse operation
|
|
23
|
+
* @throws {RtifError} If the operation is invalid
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* const { doc: newDoc, inverse } = apply(doc, { type: 'insert_text', offset: 0, text: 'hi' });
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare function apply(doc: Document, op: Operation): ApplyResult;
|
|
31
|
+
//# sourceMappingURL=apply.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"apply.d.ts","sourceRoot":"","sources":["../src/apply.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAS,QAAQ,EAAQ,MAAM,YAAY,CAAC;AACxD,OAAO,KAAK,EAAsC,SAAS,EAAkE,MAAM,iBAAiB,CAAC;AAKrJ,MAAM,WAAW,WAAW;IAC1B,oDAAoD;IACpD,QAAQ,CAAC,GAAG,EAAE,QAAQ,CAAC;IACvB,uCAAuC;IACvC,QAAQ,CAAC,OAAO,EAAE,SAAS,CAAC;CAC7B;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,KAAK,CAAC,GAAG,EAAE,QAAQ,EAAE,EAAE,EAAE,SAAS,GAAG,WAAW,CAmB/D"}
|