A Rich Text Editor Data Model
Warning: Before 1.0.0 release, the api may change. It's not prepared for production now.
Rich Text Manipulation Operations
Using npm:
$ npm install flowerpiece
Using yarn:
import { Model } from 'flowerpiece'
cconst model = new Model()
const { operations, queries } = model
const changes = model.change((operations) => {
operations.insert(0, 'test', {})
})
insert(offset: number, length: number, text: string, meta: PieceMeta | null): DocumentChange
insertText(offset: number, text: string, meta: PieceMeta | null): DocumentChange
insertLineBrea(offset: number, meta: PieceMeta | null): DocumentChange
insertLine(offset: number, meta: PieceMeta | null): DocumentChange
insertNonText(offset: number, meta: PieceMeta): DocumentChange
delete(offset: number, length: number): DocumentChange
deleteLine(lineNumber: number): DocumentChange
format(offset: number, length: number, meta: PieceMeta): DocumentChange
formatText(offset: number, length: number, meta: PieceMeta): DocumentChange
formatNonText(offset: number, length: number, meta: PieceMeta): DocumentChange
formatLine(lineNumber: number, meta: PieceMeta): DocumentChange
formatInLine(lineNumber: number, meta: PieceMeta): DocumentChange
formatTextInLine(lineNumber: number, meta: PieceMeta): DocumentChange
formatNonTextInLine(linetNumber: number, meat: PieceMeta): DocumentChange
getLine(lineNumber: number): Piece[]
getLineMeta(lineNumber: number): PieceMeta | null
forEachLine(callback: (line: IPiece[], lineNumber: number)
change(callback: (operations: operations) => void)
A Piece represet a piece content of the whole document
interface Piece {
// Text Content
text: string
// Length of this piece
length: number
// Meta info of this piece
meta: PieceMeta | null
}
A Line is a list of pieces
interface Line {
meta: PieceMeta
pieces: Piece[]
}
Diff indicate which line of content is newly added, removed or modified after operation
interface Diff {
// Diff type
type: 'insert' | 'remove' | 'replace'
// Which line of content has change
lineNumber: number
}
Must Be Plain Object
interface PieceMeta {
[key: string]: any
}
interface DocumentChange {
type: 'insert' | 'delete' | 'format'
diffs: Diff[]
startOffset: number
length: number
}