Skip to content

Commit

Permalink
style: 💄 fix linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Dec 1, 2024
1 parent 98429d2 commit 8e48422
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 109 deletions.
3 changes: 2 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"useIsArray": "off",
"noAssignInExpressions": "off",
"noConfusingLabels": "off",
"noConfusingVoidType": "off"
"noConfusingVoidType": "off",
"noConstEnum": "off"
},
"complexity": {
"noStaticOnlyClass": "off",
Expand Down
1 change: 0 additions & 1 deletion src/json-crdt-extensions/peritext/block/LeafBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export interface IBlock<Attr = unknown> {
}

export class LeafBlock<Attr = unknown> extends Block<Attr> {

// ------------------------------------------------------------------- export

public toJson(): PeritextMlElement {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,9 @@ const runTests = (setup: () => Kit) => {
const fragment = peritext.fragment(peritext.rangeAt(4, 10));
fragment.refresh();
const html = toJsonMl(fragment.toJson());
expect(html).toEqual([
'',
null,
['p', null, 'efghij'],
['p', null, 'klm'],
]);
expect(html).toEqual(['', null, ['p', null, 'efghij'], ['p', null, 'klm']]);
});

test('can export two paragraphs with inline formatting', () => {
const {editor, peritext} = setup();
editor.cursor.setAt(10);
Expand All @@ -35,15 +30,7 @@ const runTests = (setup: () => Kit) => {
expect(html).toEqual([
'',
null,
['p', null,
'ef',
['b', null, 'g'],
['i', null,
['b', null, 'h'],
],
['i', null, 'i'],
'j',
],
['p', null, 'ef', ['b', null, 'g'], ['i', null, ['b', null, 'h']], ['i', null, 'i'], 'j'],
['p', null, 'klm'],
]);
});
Expand Down Expand Up @@ -84,7 +71,7 @@ const runTests = (setup: () => Kit) => {
const html = toHtml(json, ' ');
expect(html).toBe('<div>\n <p>efghij</p>\n <p>klm</p>\n</div>');
});

test('can export two paragraphs with inline formatting', () => {
const {editor, peritext} = setup();
editor.cursor.setAt(10);
Expand All @@ -100,7 +87,7 @@ const runTests = (setup: () => Kit) => {
const html = toHtml(json, '');
expect(html).toEqual('<p>ef<b>g</b><i><b>h</b></i><i>i</i>j</p><p>klm</p>');
});

test('can export two paragraphs with inline formatting (formatted)', () => {
const {editor, peritext} = setup();
editor.cursor.setAt(10);
Expand All @@ -114,9 +101,11 @@ const runTests = (setup: () => Kit) => {
fragment.refresh();
const json = fragment.toJson();
const html = toHtml(json, ' ');
expect(html).toEqual('<p>\n ef\n <b>g</b>\n <i>\n <b>h</b>\n </i>\n <i>i</i>\n j\n</p>\n<p>klm</p>');
expect(html).toEqual(
'<p>\n ef\n <b>g</b>\n <i>\n <b>h</b>\n </i>\n <i>i</i>\n j\n</p>\n<p>klm</p>',
);
});

test('can export two paragraphs with inline formatting (formatted, wrapped in <div>)', () => {
const {editor, peritext} = setup();
editor.cursor.setAt(10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ const runTests = (setup: () => Kit) => {
const fragment = peritext.fragment(peritext.rangeAt(4, 10));
fragment.refresh();
const json = fragment.toJson();
expect(json).toEqual([
'',
null,
[0, null, 'efghij'],
[0, null, 'klm'],
]);
expect(json).toEqual(['', null, [0, null, 'efghij'], [0, null, 'klm']]);
});

test('can export two paragraphs with inline formatting', () => {
Expand All @@ -33,12 +28,12 @@ const runTests = (setup: () => Kit) => {
expect(json).toEqual([
'',
null,
[0, null,
[
0,
null,
'ef',
[CommonSliceType.b, {inline: true}, 'g'],
[CommonSliceType.i, {inline: true},
[CommonSliceType.b, {inline: true}, 'h'],
],
[CommonSliceType.i, {inline: true}, [CommonSliceType.b, {inline: true}, 'h']],
[CommonSliceType.i, {inline: true}, 'i'],
'j',
],
Expand Down
6 changes: 5 additions & 1 deletion src/json-crdt-extensions/peritext/block/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export type PeritextMlNode = string | PeritextMlElement;
export type PeritextMlElement = [tag: string | number, attrs: null | PeritextMlAttributes, ...children: PeritextMlNode[]];
export type PeritextMlElement = [
tag: string | number,
attrs: null | PeritextMlAttributes,
...children: PeritextMlNode[],
];
export interface PeritextMlAttributes {
inline?: boolean;
data?: unknown;
Expand Down
13 changes: 5 additions & 8 deletions src/json-crdt-extensions/peritext/editor/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,12 +702,7 @@ export class Editor<T = string> implements Printable {
(behavior << SliceHeaderShift.Behavior) +
(start.anchor << SliceHeaderShift.X1Anchor) +
(end.anchor << SliceHeaderShift.X2Anchor);
const viewSlice: ViewSlice = [
header,
start.viewPos(),
end.viewPos(),
type,
];
const viewSlice: ViewSlice = [header, start.viewPos(), end.viewPos(), type];
const data = slice.data();
if (data !== void 0) viewSlice.push(data);
viewSlices.push(viewSlice);
Expand All @@ -729,8 +724,10 @@ export class Editor<T = string> implements Printable {
const anchor2: Anchor = (header & SliceHeaderMask.X2Anchor) >>> SliceHeaderShift.X2Anchor;
const behavior: SliceBehavior = (header & SliceHeaderMask.Behavior) >>> SliceHeaderShift.Behavior;
const range = txt.rangeAt(Math.max(0, x1 - offset + pos), x2 - x1);
if (anchor1 === Anchor.Before) range.start.refBefore(); else range.start.refAfter();
if (anchor2 === Anchor.Before) range.end.refBefore(); else range.end.refAfter();
if (anchor1 === Anchor.Before) range.start.refBefore();
else range.start.refAfter();
if (anchor2 === Anchor.Before) range.end.refBefore();
else range.end.refAfter();
txt.savedSlices.ins(range, behavior, type, data);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,7 @@ const testSuite = (setup: () => Kit) => {
const range = peritext.rangeAt(2, 5);
peritext.refresh();
const json = editor.export(range);
expect(json).toEqual(['cdefg', 2, [
[
expect.any(Number),
3,
6,
'bold',
],
]]);
expect(json).toEqual(['cdefg', 2, [[expect.any(Number), 3, 6, 'bold']]]);
});

test('range which start in bold text', () => {
Expand All @@ -34,14 +27,7 @@ const testSuite = (setup: () => Kit) => {
editor.cursor.setAt(5, 15);
peritext.refresh();
const json = editor.export(editor.cursor);
expect(json).toEqual(['fghijklmnopqrst', 5, [
[
expect.any(Number),
3,
13,
CommonSliceType.b,
],
]]);
expect(json).toEqual(['fghijklmnopqrst', 5, [[expect.any(Number), 3, 13, CommonSliceType.b]]]);
});

test('range which ends in bold text', () => {
Expand All @@ -51,14 +37,7 @@ const testSuite = (setup: () => Kit) => {
const range = peritext.rangeAt(0, 5);
peritext.refresh();
const json = editor.export(range);
expect(json).toEqual(['abcde', 0, [
[
expect.any(Number),
3,
13,
CommonSliceType.b,
],
]]);
expect(json).toEqual(['abcde', 0, [[expect.any(Number), 3, 13, CommonSliceType.b]]]);
});
});

Expand Down
14 changes: 2 additions & 12 deletions src/json-crdt-extensions/peritext/editor/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,6 @@ export type CharPredicate<T> = (char: T) => boolean;
export type Position<T = string> = number | [at: number, anchor: 0 | 1] | Point<T>;
export type TextRangeUnit = 'point' | 'char' | 'word' | 'line' | 'block' | 'all';

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

export type ViewSlice = [
header: number,
x1: number,
x2: number,
type: SliceType,
data?: unknown,
];
export type ViewSlice = [header: number, x1: number, x2: number, type: SliceType, data?: unknown];
10 changes: 5 additions & 5 deletions src/json-crdt-extensions/peritext/export/export.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {SliceTypeName} from "../slice";
import {toHtml as _toHtml} from "../../../json-ml/toHtml";
import type {JsonMlNode} from "../../../json-ml";
import type {PeritextMlNode} from "../block/types";
import {SliceTypeName} from '../slice';
import {toHtml as _toHtml} from '../../../json-ml/toHtml';
import type {JsonMlNode} from '../../../json-ml';
import type {PeritextMlNode} from '../block/types';

export const toJsonMl = (json: PeritextMlNode): JsonMlNode => {
if (typeof json === 'string') return json;
const [tag, attr, ...children] = json;
const namedTag = tag === '' ? tag : SliceTypeName[tag as any];
const htmlTag = namedTag ?? (attr?.inline ? 'span' : 'div');
const htmlAttr = attr && (attr.data !== void 0) ? {'data-attr': JSON.stringify(attr.data)} : null;
const htmlAttr = attr && attr.data !== void 0 ? {'data-attr': JSON.stringify(attr.data)} : null;
const htmlNode: JsonMlNode = [htmlTag, htmlAttr];
const length = children.length;
for (let i = 0; i < length; i++) htmlNode.push(toJsonMl(children[i]));
Expand Down
17 changes: 3 additions & 14 deletions src/json-ml/__tests__/toHtml.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,32 +54,21 @@ test('can escape attribute values', () => {
});

test('can format HTML with tabbing', () => {
const ml: JsonMlNode = ['div', null,
['hr', {foo: 'bar'}],
['span', null, 'text'],
];
const ml: JsonMlNode = ['div', null, ['hr', {foo: 'bar'}], ['span', null, 'text']];
const html = toHtml(ml, ' ');
// console.log(html);
expect(html).toBe('<div>\n <hr foo="bar" />\n <span>text</span>\n</div>');
});

test('can format HTML fragment with tabbing', () => {
const ml: JsonMlNode = ['', null,
['hr', {foo: 'bar'}],
['span', null, 'text'],
];
const ml: JsonMlNode = ['', null, ['hr', {foo: 'bar'}], ['span', null, 'text']];
const html = toHtml(ml, ' ');
// console.log(html);
expect(html).toBe('<hr foo="bar" />\n<span>text</span>');
});

test('can format HTML fragment with tabbing - 2', () => {
const ml: JsonMlNode = ['div', null,
['', null,
['hr', {foo: 'bar'}],
['span', null, 'text'],
],
];
const ml: JsonMlNode = ['div', null, ['', null, ['hr', {foo: 'bar'}], ['span', null, 'text']]];
const html = toHtml(ml, ' ');
// console.log(html);
expect(html).toBe('<div>\n <hr foo="bar" />\n <span>text</span>\n</div>');
Expand Down
25 changes: 15 additions & 10 deletions src/json-ml/toHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,23 @@ export const toHtml = (node: JsonMlNode, tab: string = '', ident: string = ''):
const doIdent = !!tab;
let childrenStr = '';
let textOnlyChildren = true;
for (let i = 0; i < childrenLength; i++) if (typeof children[i] !== 'string') {
textOnlyChildren = false;
break;
}
if (textOnlyChildren) for (let i = 0; i < childrenLength; i++)
childrenStr += escapeText(children[i] as string);
else for (let i = 0; i < childrenLength; i++)
childrenStr += (doIdent ? ((!isFragment || i) ? '\n' : '') : '') + toHtml(children[i], tab, childrenIdent);
for (let i = 0; i < childrenLength; i++)
if (typeof children[i] !== 'string') {
textOnlyChildren = false;
break;
}
if (textOnlyChildren) for (let i = 0; i < childrenLength; i++) childrenStr += escapeText(children[i] as string);
else
for (let i = 0; i < childrenLength; i++)
childrenStr += (doIdent ? (!isFragment || i ? '\n' : '') : '') + toHtml(children[i], tab, childrenIdent);
if (isFragment) return childrenStr;
let attrStr = '';
if (attrs) for (const key in attrs) attrStr += ' ' + key + '="' + escapeAttr(attrs[key] + '') + '"';
const htmlHead = '<' + tag + attrStr;
return ident +
(childrenStr ? (htmlHead + '>' + childrenStr + ((doIdent && !textOnlyChildren) ? '\n' + ident : '') + '</' + tag + '>') : htmlHead + ' />');
return (
ident +
(childrenStr
? htmlHead + '>' + childrenStr + (doIdent && !textOnlyChildren ? '\n' + ident : '') + '</' + tag + '>'
: htmlHead + ' />')
);
};
4 changes: 1 addition & 3 deletions src/json-ml/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,16 @@ export type JsonMlElement = [
* Tag name of the element. An empty string `''` tag represents a *fragment* -
* a list of nodes. Similar to a `DocumentFragment` in the DOM, or
* `React.Fragment` `<>` in React.
*
*
* When converting to HTML, an empty string tag is not rendered and numeric
* tags are converted to strings.
*/
tag: '' | string | number,

/**
* Attributes of the element. `null` if there are no attributes. Attribute
* object values are converted to strings when formatting to HTML.
*/
attrs: null | Record<string, unknown>,

/**
* Child nodes of the element. Can be a mix of strings and elements.
*/
Expand Down

0 comments on commit 8e48422

Please sign in to comment.