Skip to content

Commit 968e2a4

Browse files
committed
docs: intro sliceDelta
1 parent 090a180 commit 968e2a4

2 files changed

Lines changed: 102 additions & 0 deletions

File tree

pages/docs/api/js.mdx

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Note: Under the hood, Loro combines a Fugue-based CRDT core with Eg-walker-inspi
7171
- **Create rich text**: [`getText`](#LoroDoc.getText) - Initialize a collaborative text container
7272
- **Edit text**: [`insert`](#LoroText.insert), [`delete`](#LoroText.delete), [`applyDelta`](#LoroText.applyDelta)
7373
- **Apply formatting**: [`mark`](#LoroText.mark) - Add bold, italic, links, custom styles
74+
- **Copy styled snippets**: [`sliceDelta`](#LoroText.sliceDelta) - Get a Delta for a range (UTF-16; use `sliceDeltaUtf8` for byte offsets)
7475
- **Track cursor positions**: [`getCursor`](#LoroText.getCursor) + [`getCursorPos`](#LoroDoc.getCursorPos) - Stable positions across edits
7576
- **Configure styles**: [`configTextStyle`](#LoroDoc.configTextStyle) - Define expand behavior for marks
7677

@@ -2195,6 +2196,71 @@ const delta = text.toDelta();
21952196
```
21962197
</Indent>
21972198

2199+
<Method id="LoroText.sliceDelta">
2200+
```typescript no_run
2201+
sliceDelta(start: number, end: number): Delta<string>[]
2202+
```
2203+
</Method>
2204+
<Indent>
2205+
Returns a Quill-style Delta for a subsection of the text, using UTF-16 indices. Useful for copying a styled span. Use `sliceDeltaUtf8` if you need UTF-8 byte offsets instead.
2206+
2207+
**Parameters:**
2208+
- `start` - Start UTF-16 code unit index (inclusive)
2209+
- `end` - End UTF-16 code unit index (exclusive)
2210+
2211+
**Example:**
2212+
```ts threeslash
2213+
import { LoroDoc } from "loro-crdt";
2214+
import { expect } from "expect";
2215+
const doc = new LoroDoc();
2216+
doc.configTextStyle({
2217+
bold: { expand: "after" },
2218+
comment: { expand: "none" },
2219+
});
2220+
const text = doc.getText("text");
2221+
2222+
text.insert(0, "Hello World!");
2223+
text.mark({ start: 0, end: 5 }, "bold", true);
2224+
text.mark({ start: 6, end: 11 }, "comment", "greeting");
2225+
2226+
const snippet = text.sliceDelta(1, 8);
2227+
expect(snippet).toStrictEqual([
2228+
{ insert: "ello", attributes: { bold: true } },
2229+
{ insert: " " },
2230+
{ insert: "Wo", attributes: { comment: "greeting" } },
2231+
]);
2232+
```
2233+
</Indent>
2234+
2235+
<Method id="LoroText.sliceDeltaUtf8">
2236+
```typescript no_run
2237+
sliceDeltaUtf8(start: number, end: number): Delta<string>[]
2238+
```
2239+
</Method>
2240+
<Indent>
2241+
Returns a Quill-style Delta for a subsection of the text using **UTF-8 byte offsets**. Choose this when your offsets come from UTF-8 encoded buffers.
2242+
2243+
**Parameters:**
2244+
- `start` - Start byte offset (inclusive)
2245+
- `end` - End byte offset (exclusive)
2246+
2247+
**Example:**
2248+
```ts threeslash
2249+
import { LoroDoc } from "loro-crdt";
2250+
import { expect } from "expect";
2251+
const doc = new LoroDoc();
2252+
const text = doc.getText("text");
2253+
text.insert(0, "Hi 👋");
2254+
2255+
const enc = new TextEncoder();
2256+
const start = enc.encode("Hi ").length; // 3 bytes
2257+
const end = enc.encode("Hi 👋").length; // 7 bytes
2258+
2259+
const delta = text.sliceDeltaUtf8(start, end);
2260+
expect(delta).toStrictEqual([{ insert: "👋" }]);
2261+
```
2262+
</Indent>
2263+
21982264
<Method id="LoroText.applyDelta">
21992265
```typescript no_run
22002266
applyDelta(delta: Delta<string>[]): void

pages/docs/tutorial/text.mdx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,15 @@ Delete text at the given range.
224224

225225
Get a string slice.
226226

227+
### `sliceDelta(start: number, end: number): Delta<string>[]`
228+
229+
Get a Quill-style Delta slice for the given UTF-16 range. Use
230+
`sliceDeltaUtf8` to slice by UTF-8 byte offsets instead.
231+
232+
### `sliceDeltaUtf8(start: number, end: number): Delta<string>[]`
233+
234+
Get a Quill-style Delta slice for the given UTF-8 byte range.
235+
227236
### `toString(): string`
228237

229238
Get the plain text value.
@@ -352,6 +361,33 @@ console.log(text.toDelta());
352361
// ]
353362
```
354363

364+
### Slice a Delta snippet
365+
366+
Use `sliceDelta(start, end)` when you only need a portion of the Delta (for example, to copy a styled snippet). It uses UTF-16 indices just like other text APIs; use `sliceDeltaUtf8` if you need to slice by UTF-8 byte offsets instead.
367+
368+
```ts twoslash
369+
import { LoroDoc } from "loro-crdt";
370+
import { expect } from "expect";
371+
// ---cut---
372+
const doc = new LoroDoc();
373+
doc.configTextStyle({
374+
bold: { expand: "after" },
375+
comment: { expand: "none" },
376+
});
377+
const text = doc.getText("text");
378+
379+
text.insert(0, "Hello World!");
380+
text.mark({ start: 0, end: 5 }, "bold", true);
381+
text.mark({ start: 6, end: 11 }, "comment", "greeting");
382+
383+
const snippet = text.sliceDelta(1, 8);
384+
expect(snippet).toStrictEqual([
385+
{ insert: "ello", attributes: { bold: true } },
386+
{ insert: " " },
387+
{ insert: "Wo", attributes: { comment: "greeting" } },
388+
]);
389+
```
390+
355391
### `mark(range: {start: number, end: number}, key: string, value: any): void`
356392

357393
Mark the given range with a key-value pair.

0 commit comments

Comments
 (0)