@rtif-sdk/core
v1.13.0
Published
RTIF core data model, operations, and utilities
Downloads
1,470
Maintainers
Readme
@rtif-sdk/core
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.
Install
npm install @rtif-sdk/coreUsage
Create a document
import type { Document } from '@rtif-sdk/core';
const doc: Document = {
version: 1,
blocks: [
{ id: 'b1', type: 'text', spans: [{ text: 'Hello world' }] },
],
};Apply an operation
import { apply } from '@rtif-sdk/core';
const { doc: newDoc, inverse } = apply(doc, {
type: 'insert_text',
offset: 5,
text: ',',
});
// newDoc.blocks[0].spans[0].text === 'Hello, world'
// Undo by applying the inverse
const { doc: original } = apply(newDoc, inverse);Resolve offsets
import { resolve, docLength } from '@rtif-sdk/core';
const len = docLength(doc); // total character count including virtual \n separators
const pos = resolve(doc, 5); // { blockIndex: 0, localOffset: 5 }Validate a document
import { validate } from '@rtif-sdk/core';
const result = validate(doc);
if (!result.valid) {
console.error(result.errors);
}Operations
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.
Every operation is deterministic and invertible — apply() always returns the inverse operation for undo.
License
MIT
