Skip to content

Commit

Permalink
style(json-crdt-extensions): 💄 fix linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jan 6, 2025
1 parent ce95148 commit 0820643
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 63 deletions.
2 changes: 1 addition & 1 deletion src/json-crdt-extensions/peritext/block/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {SliceBehavior} from '../slice/constants';
import type {SliceBehavior} from '../slice/constants';

export type PeritextMlNode = string | PeritextMlElement;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@ test('a single paragraph', () => {
peritext.editor.import(0, rangeView);
peritext.refresh();
const json = peritext.blocks.toJson();
expect(json).toEqual(['', null,
[CommonSliceType.p, null, 'Hello world'],
]);
expect(json).toEqual(['', null, [CommonSliceType.p, null, 'Hello world']]);
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,13 @@ describe('.fromHtml()', () => {
test('a single paragraph', () => {
const html = '<p>Hello world</p>';
const peritextMl = fromHtml(html);
expect(peritextMl).toEqual([
'',
null,
[CommonSliceType.p, null, 'Hello world'],
]);
expect(peritextMl).toEqual(['', null, [CommonSliceType.p, null, 'Hello world']]);
});

test('a paragraph with trailing text', () => {
const html = '<p>Hello world</p> more text';
const peritextMl = fromHtml(html);
expect(peritextMl).toEqual([
'',
null,
[CommonSliceType.p, null, 'Hello world'],
' more text',
]);
expect(peritextMl).toEqual(['', null, [CommonSliceType.p, null, 'Hello world'], ' more text']);
});

test('text formatted as italic', () => {
Expand All @@ -33,10 +24,12 @@ describe('.fromHtml()', () => {
null,
[CommonSliceType.p, null, 'Hello world'],
'\n',
[CommonSliceType.p, null,
[CommonSliceType.i, {behavior: SliceBehavior.One, inline:true}, 'italic'],
[
CommonSliceType.p,
null,
[CommonSliceType.i, {behavior: SliceBehavior.One, inline: true}, 'italic'],
' text, ',
[CommonSliceType.i, {behavior: SliceBehavior.One, inline:true}, 'more italic'],
[CommonSliceType.i, {behavior: SliceBehavior.One, inline: true}, 'more italic'],
],
]);
});
Expand All @@ -61,31 +54,47 @@ describe('.toViewRange()', () => {
const html = '<p>Hello world</p><p>Goodbye world</p>';
const peritextMl = fromHtml(html);
const view = toViewRange(peritextMl);
expect(view).toEqual(['\nHello world\nGoodbye world', 0, [
[0, 0, 0, 0],
[0, 12, 12, 0],
]]);
expect(view).toEqual([
'\nHello world\nGoodbye world',
0,
[
[0, 0, 0, 0],
[0, 12, 12, 0],
],
]);
});

test('two paragraphs with whitespace gap', () => {
const html = ' <p>Hello world</p>\n <p>Goodbye world</p>';
const peritextMl = fromHtml(html);
const view = toViewRange(peritextMl);
expect(view).toEqual(['\nHello world\nGoodbye world', 0, [
[0, 0, 0, 0],
[0, 12, 12, 0],
]]);
expect(view).toEqual([
'\nHello world\nGoodbye world',
0,
[
[0, 0, 0, 0],
[0, 12, 12, 0],
],
]);
});

test('single inline annotation', () => {
const html = 'here is some <em>italic</em> text';
const peritextMl = fromHtml(html);
const view = toViewRange(peritextMl);
expect(view).toEqual(['here is some italic text', 0, [
[(SliceBehavior.One << SliceHeaderShift.Behavior) +
(Anchor.Before << SliceHeaderShift.X1Anchor) +
(Anchor.After << SliceHeaderShift.X2Anchor),
13, 19, CommonSliceType.i],
]]);
expect(view).toEqual([
'here is some italic text',
0,
[
[
(SliceBehavior.One << SliceHeaderShift.Behavior) +
(Anchor.Before << SliceHeaderShift.X1Anchor) +
(Anchor.After << SliceHeaderShift.X2Anchor),
13,
19,
CommonSliceType.i,
],
],
]);
});
});
});
18 changes: 10 additions & 8 deletions src/json-crdt-extensions/peritext/lazy/import-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ViewRangeBuilder {
private text = '';
private slices: ViewSlice[] = [];

constructor (private registry: SliceRegistry) {}
constructor(private registry: SliceRegistry) {}

private build0(node: PeritextMlNode, depth = 0): void {
const skipWhitespace = depth < 2;
Expand All @@ -32,7 +32,8 @@ class ViewRangeBuilder {
const length = node.length;
const inline = !!attr?.inline;
if (!!type || type === 0) {
let end: number = 0, header: number = 0;
let end: number = 0,
header: number = 0;
if (!inline) {
this.text += '\n';
end = start;
Expand All @@ -48,18 +49,19 @@ class ViewRangeBuilder {
}
for (let i = 2; i < length; i++) this.build0(node[i] as PeritextMlNode, depth + 1);
if (!!type || type === 0) {
let end: number = 0, header: number = 0;
let end: number = 0,
header: number = 0;
if (inline) {
end = this.text.length;
const behavior: SliceBehavior = attr?.behavior ?? SliceBehavior.Many;
header =
(behavior << SliceHeaderShift.Behavior) +
(Anchor.Before << SliceHeaderShift.X1Anchor) +
(Anchor.After << SliceHeaderShift.X2Anchor);
const slice: ViewSlice = [header, start, end, type];
const data = attr?.data;
if (data) slice.push(data);
this.slices.push(slice);
const slice: ViewSlice = [header, start, end, type];
const data = attr?.data;
if (data) slice.push(data);
this.slices.push(slice);
}
}
}
Expand Down Expand Up @@ -102,7 +104,7 @@ export const fromJsonMl = (jsonml: JsonMlNode, registry: SliceRegistry = default
node[1] = attr;
}
return node;
}
};

export const fromHast = (hast: THtmlToken, registry?: SliceRegistry): PeritextMlNode => {
const jsonml = _fromHast(hast);
Expand Down
2 changes: 1 addition & 1 deletion src/json-crdt-extensions/peritext/lazy/import-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export const fromMdast = (mdast: IRoot, registry: SliceRegistry = defaultRegistr

export const fromMarkdown = (markdown: string, registry?: SliceRegistry): PeritextMlNode => {
const mdast = block.parsef(markdown);
return fromMdast(mdast, registry);
return fromMdast(mdast, registry);
};
13 changes: 8 additions & 5 deletions src/json-crdt-extensions/peritext/registry/SliceRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import {PeritextMlElement} from '../block/types';
import {NodeBuilder} from '../../../json-crdt-patch';
import type {PeritextMlElement} from '../block/types';
import type {NodeBuilder} from '../../../json-crdt-patch';
import {SliceBehavior} from '../slice/constants';
import type {JsonMlElement} from 'very-small-parser/lib/html/json-ml/types';
import type {FromHtmlConverter, SliceTypeDefinition, ToHtmlConverter} from './types';

export class SliceRegistry {
private map: Map<string | number, SliceTypeDefinition<any, any, any>> = new Map();
private toHtmlMap: Map<string | number, ToHtmlConverter<any>> = new Map();
private fromHtmlMap: Map<string, [def: SliceTypeDefinition<any, any, any>, converter: FromHtmlConverter][]> = new Map();
private fromHtmlMap: Map<string, [def: SliceTypeDefinition<any, any, any>, converter: FromHtmlConverter][]> =
new Map();

public add<Type extends number | string, Schema extends NodeBuilder, Inline extends boolean = true>(def: SliceTypeDefinition<Type, Schema, Inline>): void {
public add<Type extends number | string, Schema extends NodeBuilder, Inline extends boolean = true>(
def: SliceTypeDefinition<Type, Schema, Inline>,
): void {
const {type, toHtml, fromHtml} = def;
this.map.set(type, def);
if (toHtml) this.toHtmlMap.set(type, toHtml);
Expand Down Expand Up @@ -47,7 +50,7 @@ export class SliceRegistry {
const result = converter(el);
if (result) {
const attr = result[1] ?? (result[1] = {});
attr.inline = def.inline ?? (def.type < 0);
attr.inline = def.inline ?? def.type < 0;
attr.behavior = !attr.inline ? SliceBehavior.Marker : (def.behavior ?? SliceBehavior.Many);
return result;
}
Expand Down
14 changes: 7 additions & 7 deletions src/json-crdt-extensions/peritext/registry/registry.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {s} from "../../../json-crdt-patch";
import {JsonNodeView} from "../../../json-crdt/nodes";
import {SchemaToJsonNode} from "../../../json-crdt/schema/types";
import {CommonSliceType} from "../slice";
import {s} from '../../../json-crdt-patch';
import type {JsonNodeView} from '../../../json-crdt/nodes';
import type {SchemaToJsonNode} from '../../../json-crdt/schema/types';
import {CommonSliceType} from '../slice';
import {SliceBehavior} from '../slice/constants';
import {SliceRegistry} from "./SliceRegistry";
import {SliceRegistry} from './SliceRegistry';

const undefSchema = s.con(undefined);

Expand Down Expand Up @@ -56,12 +56,12 @@ registry.def(CommonSliceType.a, aSchema, SliceBehavior.Many, true, {
title: attr.title ?? '',
};
return [CommonSliceType.a, {data, inline: true}];
}
},
},
});

// TODO: add more default annotations
// comment = SliceTypeCon.comment,
// comment = SliceTypeCon.comment,
// font = SliceTypeCon.font,
// col = SliceTypeCon.col,
// bg = SliceTypeCon.bg,
Expand Down
16 changes: 11 additions & 5 deletions src/json-crdt-extensions/peritext/registry/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import type {PeritextMlElement} from '../block/types';
import type {JsonMlElement} from 'very-small-parser/lib/html/json-ml/types';
import type {SliceBehavior} from '../slice/constants';

export interface SliceTypeDefinition<Type extends number | string = number | string, Schema extends NodeBuilder = NodeBuilder, Inline extends boolean = true> {
export interface SliceTypeDefinition<
Type extends number | string = number | string,
Schema extends NodeBuilder = NodeBuilder,
Inline extends boolean = true,
> {
type: Type;
schema: Schema;
behavior?: SliceBehavior;
Expand All @@ -16,8 +20,10 @@ export interface SliceTypeDefinition<Type extends number | string = number | str
};
}

export type ToHtmlConverter<El extends PeritextMlElement<any, any, any> = PeritextMlElement<string | number, unknown, boolean>> =
(element: El) => [tag: string, attr: Record<string, string> | null];
export type ToHtmlConverter<
El extends PeritextMlElement<any, any, any> = PeritextMlElement<string | number, unknown, boolean>,
> = (element: El) => [tag: string, attr: Record<string, string> | null];

export type FromHtmlConverter<El extends PeritextMlElement<any, any, any> = PeritextMlElement<string | number, unknown, boolean>> =
(jsonml: JsonMlElement) => El;
export type FromHtmlConverter<
El extends PeritextMlElement<any, any, any> = PeritextMlElement<string | number, unknown, boolean>,
> = (jsonml: JsonMlElement) => El;
8 changes: 4 additions & 4 deletions src/json-crdt-patch/builder/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ export namespace nodes {
* age: s.con(0),
* });
* ```
*
*
* Specify optional keys as the second argument:
*
*
* ```ts
* s.obj(
* {
Expand All @@ -145,9 +145,9 @@ export namespace nodes {
* },
* )
* ```
*
*
* Or, specify only the type, using the `optional` method:
*
*
* ```ts
* s.obj({
* href: s.str('https://example.com'),
Expand Down

0 comments on commit 0820643

Please sign in to comment.