Skip to content

Commit

Permalink
feat(json-crdt-extensions): 🎸 simplify export view range interface
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Nov 25, 2024
1 parent f9bc89e commit 0b8c7b7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
23 changes: 13 additions & 10 deletions src/json-crdt-extensions/peritext/editor/Editor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {printTree} from 'tree-dump/lib/printTree';
import {Cursor} from './Cursor';
import {stringify} from '../../../json-text/stringify';
import {CursorAnchor, SliceBehavior} from '../slice/constants';
import {CursorAnchor, SliceBehavior, SliceHeaderShift} from '../slice/constants';
import {EditorSlices} from './EditorSlices';
import {next, prev} from 'sonic-forest/lib/util';
import {isLetter, isPunctuation, isWhitespace} from './util';
Expand Down Expand Up @@ -673,25 +673,28 @@ export class Editor<T = string> implements Printable {
r.start.refBefore();
r.end.refAfter();
const text = r.text();
const viewSlices: ViewRange[1] = [];
const view: ViewRange = [text, viewSlices];
const offset = r.start.viewPos();
const viewSlices: ViewSlice[] = [];
const view: ViewRange = [text, offset, viewSlices];
const overlay = this.txt.overlay;
const slices = overlay.findOverlapping(r);
const offset = r.start.viewPos();
for (const slice of slices) {
const behavior = slice.behavior;
switch (behavior) {
case SliceBehavior.One:
case SliceBehavior.Many:
case SliceBehavior.Erase:
case SliceBehavior.Marker: {
const {behavior, type, start, end} = slice;
const header: number =
(behavior << SliceHeaderShift.Behavior) +
(start.anchor << SliceHeaderShift.X1Anchor) +
(end.anchor << SliceHeaderShift.X2Anchor);
const viewSlice: ViewSlice = [
slice.start.viewPos() - offset,
slice.start.anchor,
slice.end.viewPos() - offset,
slice.end.anchor,
slice.behavior,
slice.type,
header,
start.viewPos(),
end.viewPos(),
type,
];
const data = slice.data();
if (data !== void 0) viewSlice.push(data);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import {type Kit, runAlphabetKitTestSuite} from '../../__tests__/setup';
import {Anchor} from '../../rga/constants';
import {SliceBehavior} from '../../slice/constants';

const testSuite = (setup: () => Kit) => {
describe('.export()', () => {
test('can export whole un-annotated document', () => {
const {editor} = setup();
editor.selectAll();
const json = editor.export(editor.cursor);
expect(json).toEqual(['abcdefghijklmnopqrstuvwxyz', []]);
expect(json).toEqual(['abcdefghijklmnopqrstuvwxyz', 0, []]);
});

test('can export range, which contains bold text', () => {
Expand All @@ -18,13 +16,11 @@ const testSuite = (setup: () => Kit) => {
const range = peritext.rangeAt(2, 5);
peritext.refresh();
const json = editor.export(range);
expect(json).toEqual(['cdefg', [
expect(json).toEqual(['cdefg', 2, [
[
1,
Anchor.Before,
4,
Anchor.After,
SliceBehavior.One,
expect.any(Number),
3,
6,
'bold',
],
]]);
Expand Down
7 changes: 2 additions & 5 deletions src/json-crdt-extensions/peritext/editor/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type {UndefIterator} from '../../../util/iterator';
import type {Anchor} from '../rga/constants';
import type {Point} from '../rga/Point';
import type {SliceType} from '../slice';
import type {SliceBehavior} from '../slice/constants';
import type {ChunkSlice} from '../util/ChunkSlice';

export type CharIterator<T> = UndefIterator<ChunkSlice<T>>;
Expand All @@ -13,15 +11,14 @@ export type TextRangeUnit = 'point' | 'char' | 'word' | 'line' | 'block' | 'all'

export type ViewRange = [
text: string,
textPosition: number,
slices: ViewSlice[],
];

export type ViewSlice = [
header: number,
x1: number,
a1: Anchor,
x2: number,
a2: Anchor,
behavior: SliceBehavior,
type: SliceType,
data?: unknown,
];

0 comments on commit 0b8c7b7

Please sign in to comment.