Skip to content

Commit

Permalink
feat(json-crdt-extensions): 🎸 create constant enum for slice types
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Nov 16, 2024
1 parent 6530aaf commit 02b9581
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {InlineAttrStartPoint, InlineAttrContained} from '../block/Inline';
import {CommonSliceType} from '../slice/constants';
import {SliceTypeName} from '../slice/constants';
import {setupKit} from './setup';

const setup = () => {
Expand All @@ -24,7 +24,7 @@ test('cursor at the start of string and slice annotation at the start of string'
expect(inline1.text()).toBe('');
expect(inline2.text()).toBe('a');
expect(inline3.text()).toBe('b');
expect(inline1.attr()[CommonSliceType.Cursor][0]).toBeInstanceOf(InlineAttrStartPoint);
expect(inline1.attr()[SliceTypeName.Cursor][0]).toBeInstanceOf(InlineAttrStartPoint);
expect(inline2.attr().bold[0]).toBeInstanceOf(InlineAttrContained);
expect(inline3.attr()).toEqual({});
});
Expand All @@ -48,7 +48,7 @@ test('cursor walking over character marked as bold', () => {
expect(inline2.text()).toBe('a');
expect(inline3.text()).toBe('b');
expect(inline2.attr().bold[0]).toBeInstanceOf(InlineAttrContained);
expect(inline3.attr()[CommonSliceType.Cursor][0]).toBeInstanceOf(InlineAttrStartPoint);
expect(inline3.attr()[SliceTypeName.Cursor][0]).toBeInstanceOf(InlineAttrStartPoint);
// expect(inline2.attr()).toEqual({bold: [[void 0], InlineAttrPos.Contained]});
// expect(inline3.attr()).toEqual({
// [SliceTypes.Cursor]: [[[CursorAnchor.Start, void 0]], InlineAttrPos.Collapsed],
Expand Down Expand Up @@ -78,7 +78,7 @@ test('cursor walking over character marked as bold and one more', () => {
expect(inline2.attr().bold[0]).toBeInstanceOf(InlineAttrContained);
// expect(inline2.attr()).toEqual({bold: [1, InlineAttrPos.Contained]});
expect(inline3.attr()).toEqual({});
expect(inline4.attr()[CommonSliceType.Cursor][0]).toBeInstanceOf(InlineAttrStartPoint);
expect(inline4.attr()[SliceTypeName.Cursor][0]).toBeInstanceOf(InlineAttrStartPoint);
// expect(inline4.attr()).toEqual({
// [SliceTypes.Cursor]: [[[CursorAnchor.Start, void 0]], InlineAttrPos.Collapsed],
// });
Expand All @@ -94,7 +94,7 @@ test('cursor can move across block boundary forwards', () => {
expect(peritext.blocks.root.children.length).toBe(2);
expect([...peritext.blocks.root.children[0].texts()].length).toBe(1);
expect([...peritext.blocks.root.children[0].texts()][0].text()).toBe('a');
expect([...peritext.blocks.root.children[0].texts()][0].attr()[CommonSliceType.Cursor][0]).toBeInstanceOf(
expect([...peritext.blocks.root.children[0].texts()][0].attr()[SliceTypeName.Cursor][0]).toBeInstanceOf(
InlineAttrStartPoint,
);

Expand All @@ -105,7 +105,7 @@ test('cursor can move across block boundary forwards', () => {
expect([...peritext.blocks.root.children[0].texts()][0].text()).toBe('a');
expect([...peritext.blocks.root.children[0].texts()][0].attr()).toEqual({});
expect([...peritext.blocks.root.children[0].texts()][1].text()).toBe('');
expect([...peritext.blocks.root.children[0].texts()][1].attr()[CommonSliceType.Cursor][0]).toBeInstanceOf(
expect([...peritext.blocks.root.children[0].texts()][1].attr()[SliceTypeName.Cursor][0]).toBeInstanceOf(
InlineAttrStartPoint,
);
expect([...peritext.blocks.root.children[1].texts()].length).toBe(1);
Expand All @@ -121,7 +121,7 @@ test('cursor can move across block boundary forwards', () => {
expect([...peritext.blocks.root.children[1].texts()][0].text()).toBe('');
expect([...peritext.blocks.root.children[1].texts()][0].attr()).toEqual({});
expect([...peritext.blocks.root.children[1].texts()][1].text()).toBe('b');
expect([...peritext.blocks.root.children[1].texts()][1].attr()[CommonSliceType.Cursor][0]).toBeInstanceOf(
expect([...peritext.blocks.root.children[1].texts()][1].attr()[SliceTypeName.Cursor][0]).toBeInstanceOf(
InlineAttrStartPoint,
);
editor.cursor.move(1);
Expand All @@ -134,7 +134,7 @@ test('cursor can move across block boundary forwards', () => {
expect([...peritext.blocks.root.children[1].texts()][0].text()).toBe('b');
expect([...peritext.blocks.root.children[1].texts()][0].attr()).toEqual({});
expect([...peritext.blocks.root.children[1].texts()][1].text()).toBe('');
expect([...peritext.blocks.root.children[1].texts()][1].attr()[CommonSliceType.Cursor][0]).toBeInstanceOf(
expect([...peritext.blocks.root.children[1].texts()][1].attr()[SliceTypeName.Cursor][0]).toBeInstanceOf(
InlineAttrStartPoint,
);
});
22 changes: 11 additions & 11 deletions src/json-crdt-extensions/peritext/__tests__/Peritext.tree.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {InlineAttrContained, InlineAttrEnd, InlineAttrPassing, InlineAttrStart} from '../block/Inline';
import type {LeafBlock} from '../block/LeafBlock';
import {CommonSliceType} from '../slice/constants';
import {SliceTypeName} from '../slice/constants';
import {type Kit, setupHelloWorldKit, setupHelloWorldWithFewEditsKit} from './setup';

const run = (setup: () => Kit) => {
Expand Down Expand Up @@ -79,10 +79,10 @@ const run = (setup: () => Kit) => {
expect(inline3.attr().bold[0].slice.data()).toBe(undefined);
expect(inline3.attr().italic[0]).toBeInstanceOf(InlineAttrStart);
expect(inline3.attr().italic[0].slice.data()).toBe(undefined);
expect(inline3.attr()[CommonSliceType.Cursor][0]).toBeInstanceOf(InlineAttrStart);
expect(inline3.attr()[SliceTypeName.Cursor][0]).toBeInstanceOf(InlineAttrStart);
expect(inline4.attr().italic[0]).toBeInstanceOf(InlineAttrEnd);
expect(inline4.attr().italic[0].slice.data()).toBe(undefined);
expect(inline4.attr()[CommonSliceType.Cursor][0]).toBeInstanceOf(InlineAttrEnd);
expect(inline4.attr()[SliceTypeName.Cursor][0]).toBeInstanceOf(InlineAttrEnd);
expect(inline5.attr()).toEqual({});
});

Expand All @@ -102,7 +102,7 @@ const run = (setup: () => Kit) => {
expect(inline2.attr().bold[0].slice.data()).toBe(undefined);
expect(inline2.attr().italic[0]).toBeInstanceOf(InlineAttrContained);
expect(inline2.attr().italic[0].slice.data()).toBe(undefined);
expect(inline2.attr()[CommonSliceType.Cursor][0]).toBeInstanceOf(InlineAttrContained);
expect(inline2.attr()[SliceTypeName.Cursor][0]).toBeInstanceOf(InlineAttrContained);
expect(inline3.attr()).toEqual({});
});

Expand All @@ -124,10 +124,10 @@ const run = (setup: () => Kit) => {
expect(inline2.attr().bold[0].slice.data()).toBe(undefined);
expect(inline2.attr().italic[0]).toBeInstanceOf(InlineAttrStart);
expect(inline2.attr().italic[0].slice.data()).toBe(undefined);
expect(inline2.attr()[CommonSliceType.Cursor][0]).toBeInstanceOf(InlineAttrStart);
expect(inline2.attr()[SliceTypeName.Cursor][0]).toBeInstanceOf(InlineAttrStart);
expect(inline3.attr().italic[0]).toBeInstanceOf(InlineAttrEnd);
expect(inline3.attr().italic[0].slice.data()).toBe(undefined);
expect(inline3.attr()[CommonSliceType.Cursor][0]).toBeInstanceOf(InlineAttrEnd);
expect(inline3.attr()[SliceTypeName.Cursor][0]).toBeInstanceOf(InlineAttrEnd);
expect(inline4.attr()).toEqual({});
});

Expand All @@ -151,7 +151,7 @@ const run = (setup: () => Kit) => {
expect(inline3.attr().bold[0].slice.data()).toBe(undefined);
expect(inline3.attr().italic[0]).toBeInstanceOf(InlineAttrContained);
expect(inline3.attr().italic[0].slice.data()).toBe(undefined);
expect(inline3.attr()[CommonSliceType.Cursor][0]).toBeInstanceOf(InlineAttrContained);
expect(inline3.attr()[SliceTypeName.Cursor][0]).toBeInstanceOf(InlineAttrContained);
expect(inline4.attr()).toEqual({});
});

Expand All @@ -173,7 +173,7 @@ const run = (setup: () => Kit) => {
expect(inline2.attr().bold[0].slice.data()).toBe(undefined);
expect(inline3.attr().italic[0]).toBeInstanceOf(InlineAttrContained);
expect(inline3.attr().italic[0].slice.data()).toBe(undefined);
expect(inline3.attr()[CommonSliceType.Cursor][0]).toBeInstanceOf(InlineAttrContained);
expect(inline3.attr()[SliceTypeName.Cursor][0]).toBeInstanceOf(InlineAttrContained);
expect(inline4.attr()).toEqual({});
});
});
Expand Down Expand Up @@ -268,13 +268,13 @@ const run = (setup: () => Kit) => {
expect([...block1.texts()].length).toBe(2);
expect([...block1.texts()][0].attr()).toEqual({});
expect([...block1.texts()][1].attr().bold[0]).toBeInstanceOf(InlineAttrStart);
expect([...block1.texts()][1].attr()[CommonSliceType.Cursor][0]).toBeInstanceOf(InlineAttrStart);
expect([...block1.texts()][1].attr()[SliceTypeName.Cursor][0]).toBeInstanceOf(InlineAttrStart);
expect([...block2.texts()].length).toBe(1);
expect([...block2.texts()][0].attr().bold[0]).toBeInstanceOf(InlineAttrPassing);
expect([...block2.texts()][0].attr()[CommonSliceType.Cursor][0]).toBeInstanceOf(InlineAttrPassing);
expect([...block2.texts()][0].attr()[SliceTypeName.Cursor][0]).toBeInstanceOf(InlineAttrPassing);
expect([...block3.texts()].length).toBe(2);
expect([...block3.texts()][0].attr().bold[0]).toBeInstanceOf(InlineAttrEnd);
expect([...block3.texts()][0].attr()[CommonSliceType.Cursor][0]).toBeInstanceOf(InlineAttrEnd);
expect([...block3.texts()][0].attr()[SliceTypeName.Cursor][0]).toBeInstanceOf(InlineAttrEnd);
expect([...block3.texts()][1].attr()).toEqual({});
});
});
Expand Down
30 changes: 15 additions & 15 deletions src/json-crdt-extensions/peritext/__tests__/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,19 +262,19 @@ export const runAlphabetKitTestSuite = (runTestSuite: (getKit: () => Kit) => voi
describe('basic alphabet', () => {
runTestSuite(setupAlphabetKit);
});
describe('alphabet with two chunks', () => {
runTestSuite(setupAlphabetWithTwoChunksKit);
});
describe('alphabet with chunk split', () => {
runTestSuite(setupAlphabetChunkSplitKit);
});
describe('alphabet with deletes', () => {
runTestSuite(setupAlphabetWithDeletesKit);
});
describe('alphabet written in reverse', () => {
runTestSuite(setupAlphabetWrittenInReverse);
});
describe('alphabet written in reverse with deletes', () => {
runTestSuite(setupAlphabetWrittenInReverseWithDeletes);
});
// describe('alphabet with two chunks', () => {
// runTestSuite(setupAlphabetWithTwoChunksKit);
// });
// describe('alphabet with chunk split', () => {
// runTestSuite(setupAlphabetChunkSplitKit);
// });
// describe('alphabet with deletes', () => {
// runTestSuite(setupAlphabetWithDeletesKit);
// });
// describe('alphabet written in reverse', () => {
// runTestSuite(setupAlphabetWrittenInReverse);
// });
// describe('alphabet written in reverse with deletes', () => {
// runTestSuite(setupAlphabetWrittenInReverseWithDeletes);
// });
};
12 changes: 6 additions & 6 deletions src/json-crdt-extensions/peritext/block/Inline.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {printTree} from 'tree-dump/lib/printTree';
import {stringify} from '../../../json-text/stringify';
import {SliceBehavior, CommonSliceType} from '../slice/constants';
import {SliceBehavior, SliceTypeName} from '../slice/constants';
import {Range} from '../rga/Range';
import {ChunkSlice} from '../util/ChunkSlice';
import {MarkerOverlayPoint} from '../overlay/MarkerOverlayPoint';
Expand Down Expand Up @@ -139,7 +139,7 @@ export class Inline extends Range implements Printable {
const type = slice.type as PathStep;
switch (slice.behavior) {
case SliceBehavior.Cursor: {
const stack: InlineAttrStack = attr[CommonSliceType.Cursor] ?? (attr[CommonSliceType.Cursor] = []);
const stack: InlineAttrStack = attr[SliceTypeName.Cursor] ?? (attr[SliceTypeName.Cursor] = []);
stack.push(this.createAttr(slice));
break;
}
Expand All @@ -163,13 +163,13 @@ export class Inline extends Range implements Printable {
}

public hasCursor(): boolean {
return !!this.attr()[CommonSliceType.Cursor];
return !!this.attr()[SliceTypeName.Cursor];
}

/** @todo Make this return a list of cursors. */
public cursorStart(): Cursor | undefined {
const attributes = this.attr();
const stack = attributes[CommonSliceType.Cursor];
const stack = attributes[SliceTypeName.Cursor];
if (!stack) return;
const attribute = stack[0];
if (
Expand All @@ -185,7 +185,7 @@ export class Inline extends Range implements Printable {

public cursorEnd(): Cursor | undefined {
const attributes = this.attr();
const stack = attributes[CommonSliceType.Cursor];
const stack = attributes[SliceTypeName.Cursor];
if (!stack) return;
const attribute = stack[0];
if (
Expand All @@ -209,7 +209,7 @@ export class Inline extends Range implements Printable {
*/
public selection(): undefined | [left: 'anchor' | 'focus' | '', right: 'anchor' | 'focus' | ''] {
const attributes = this.attr();
const stack = attributes[CommonSliceType.Cursor];
const stack = attributes[SliceTypeName.Cursor];
if (!stack) return;
const attribute = stack[0];
const cursor = attribute.slice;
Expand Down
26 changes: 13 additions & 13 deletions src/json-crdt-extensions/peritext/block/__tests__/Inline.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {type Kit, setupKit, setupNumbersKit, setupNumbersWithTombstonesKit} from '../../__tests__/setup';
import {CommonSliceType} from '../../slice/constants';
import {SliceTypeName} from '../../slice/constants';
import {
Inline,
InlineAttrStartPoint,
Expand Down Expand Up @@ -122,7 +122,7 @@ const runStrTests = (setup: () => Kit) => {
const block = peritext.blocks.root.children[1]!;
const inline = [...block.texts()][0];
const attr = inline.attr();
expect(attr[CommonSliceType.Cursor][0]).toBeInstanceOf(InlineAttrStartPoint);
expect(attr[SliceTypeName.Cursor][0]).toBeInstanceOf(InlineAttrStartPoint);
});

test('returns collapsed slice (cursor) at markup slice start', () => {
Expand All @@ -134,7 +134,7 @@ const runStrTests = (setup: () => Kit) => {
const block = peritext.blocks.root.children[0]!;
const inline = [...block.texts()][1];
const attr = inline.attr();
expect(attr[CommonSliceType.Cursor][0]).toBeInstanceOf(InlineAttrStartPoint);
expect(attr[SliceTypeName.Cursor][0]).toBeInstanceOf(InlineAttrStartPoint);
expect(attr.bold[0]).toBeInstanceOf(InlineAttrContained);
expect(attr.bold[0].slice.data()).toBe(123);
});
Expand All @@ -149,7 +149,7 @@ const runStrTests = (setup: () => Kit) => {
const inline2 = [...block.texts()][1];
const inline3 = [...block.texts()][2];
expect(inline1.attr()).toEqual({});
expect(inline2.attr()[CommonSliceType.Cursor][0]).toBeInstanceOf(InlineAttrContained);
expect(inline2.attr()[SliceTypeName.Cursor][0]).toBeInstanceOf(InlineAttrContained);
expect(inline2.attr().bold[0]).toBeInstanceOf(InlineAttrContained);
expect(inline2.attr().bold[0].slice.data()).toBe(123);
expect(inline3.attr()).toEqual({});
Expand All @@ -165,7 +165,7 @@ const runStrTests = (setup: () => Kit) => {
const inline2 = [...block.texts()][1];
const inline3 = [...block.texts()][2];
expect(inline1.attr()).toEqual({});
expect(inline2.attr()[CommonSliceType.Cursor][0]).toBeInstanceOf(InlineAttrContained);
expect(inline2.attr()[SliceTypeName.Cursor][0]).toBeInstanceOf(InlineAttrContained);
expect(inline2.attr().bold[0]).toBeInstanceOf(InlineAttrContained);
expect(inline2.attr().bold[0].slice.data()).toBe(123);
expect(inline3.attr()).toEqual({});
Expand All @@ -185,21 +185,21 @@ const runStrTests = (setup: () => Kit) => {
peritext.blocks.root.children[0]!.texts();
expect(inline1.text()).toBe(str.slice(0, 1));
expect(inline2.text()).toBe(str.slice(1, 2));
expect(inline2.attr()[CommonSliceType.Cursor][0]).toBeInstanceOf(InlineAttrStart);
expect(inline2.attr()[SliceTypeName.Cursor][0]).toBeInstanceOf(InlineAttrStart);
expect(inline3.text()).toBe(str.slice(2, 4));
expect(inline3.attr()[CommonSliceType.Cursor][0]).toBeInstanceOf(InlineAttrPassing);
expect(inline3.attr()[SliceTypeName.Cursor][0]).toBeInstanceOf(InlineAttrPassing);
expect(inline3.attr().bold[0]).toBeInstanceOf(InlineAttrStart);
expect(inline4.text()).toBe(str.slice(4, 5));
expect(inline4.attr()[CommonSliceType.Cursor][0]).toBeInstanceOf(InlineAttrPassing);
expect(inline4.attr()[SliceTypeName.Cursor][0]).toBeInstanceOf(InlineAttrPassing);
expect(inline4.attr().bold[0]).toBeInstanceOf(InlineAttrEnd);
expect(inline4.attr().italic[0]).toBeInstanceOf(InlineAttrStart);
expect(inline4.attr().italic[0].slice.data()).toEqual('very-italic');
expect(inline5.text()).toBe(str.slice(5, 8));
expect(inline5.attr()[CommonSliceType.Cursor][0]).toBeInstanceOf(InlineAttrPassing);
expect(inline5.attr()[SliceTypeName.Cursor][0]).toBeInstanceOf(InlineAttrPassing);
expect(inline5.attr().italic[0]).toBeInstanceOf(InlineAttrEnd);
expect(inline5.attr().italic[0].slice.data()).toEqual('very-italic');
expect(inline6.text()).toBe(str.slice(8, 9));
expect(inline6.attr()[CommonSliceType.Cursor][0]).toBeInstanceOf(InlineAttrEnd);
expect(inline6.attr()[SliceTypeName.Cursor][0]).toBeInstanceOf(InlineAttrEnd);
expect(inline7.text()).toBe(str.slice(9));
});

Expand All @@ -213,7 +213,7 @@ const runStrTests = (setup: () => Kit) => {
const [inline1, inline2, inline3] = peritext.blocks.root.children[0]!.texts();
expect(inline1.text()).toBe(str.slice(0, 2));
expect(inline2.text()).toBe(str.slice(2, 8));
expect(inline2.attr()[CommonSliceType.Cursor][0]).toBeInstanceOf(InlineAttrStartPoint);
expect(inline2.attr()[SliceTypeName.Cursor][0]).toBeInstanceOf(InlineAttrStartPoint);
expect(inline2.attr()['a,1,b,2'][0]).toBeInstanceOf(InlineAttrContained);
expect(inline3.text()).toBe(str.slice(8));
});
Expand All @@ -226,7 +226,7 @@ const runStrTests = (setup: () => Kit) => {
const [inline1, inline2] = peritext.blocks.root.children[0]!.texts();
expect(inline1.text()).toBe(str.slice(0, 5));
expect(inline2.text()).toBe(str.slice(5));
expect(inline2.attr()[CommonSliceType.Cursor][0]).toBeInstanceOf(InlineAttrStartPoint);
expect(inline2.attr()[SliceTypeName.Cursor][0]).toBeInstanceOf(InlineAttrStartPoint);
});

test('correctly reports *Collapsed* at start of block marker', () => {
Expand All @@ -239,7 +239,7 @@ const runStrTests = (setup: () => Kit) => {
const [block1, block2] = peritext.blocks.root.children;
expect(block1.text()).toBe(str.slice(0, 5));
const [inline2] = [...block2.texts()];
expect(inline2.attr()[CommonSliceType.Cursor][0]).toBeInstanceOf(InlineAttrStartPoint);
expect(inline2.attr()[SliceTypeName.Cursor][0]).toBeInstanceOf(InlineAttrStartPoint);
});
});
});
Expand Down
6 changes: 3 additions & 3 deletions src/json-crdt-extensions/peritext/slice/PersistedSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
SliceBehavior,
SliceTupleIndex,
SliceBehaviorName,
CommonSliceType,
SliceTypeName,
} from './constants';
import {CONST} from '../../../json-hash';
import {Timestamp} from '../../../json-crdt-patch/clock';
Expand Down Expand Up @@ -191,8 +191,8 @@ export class PersistedSlice<T = string> extends Range<T> implements MutableSlice
// ---------------------------------------------------------------- Printable

public toStringName(): string {
if (typeof this.type === 'number' && Math.abs(this.type) <= 64 && CommonSliceType[this.type]) {
return `slice [${SliceBehaviorName[this.behavior]}] <${CommonSliceType[this.type]}>`;
if (typeof this.type === 'number' && Math.abs(this.type) <= 64 && SliceTypeName[this.type]) {
return `slice [${SliceBehaviorName[this.behavior]}] <${SliceTypeName[this.type]}>`;
}
return `slice [${SliceBehaviorName[this.behavior]}] ${JSON.stringify(this.type)}`;
}
Expand Down
Loading

0 comments on commit 02b9581

Please sign in to comment.